Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Get Printer Type #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

NewtonRocha
Copy link

@NewtonRocha NewtonRocha commented Sep 12, 2017

On windows we have printers that only accept XPS, that function identify that

Copy link
Owner

@alexbrainman alexbrainman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some general comments first.

Alex

printer.go Outdated
return GetPrinterType(printerName)
}

func GetPrinterType(printerName string) (string, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating package function, I suggest, you add new method to Printer struct. This way new method will not need printerName parameter. You will also not need GetFeaultPrinterType function, because you could use printer.Default.... instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a nice a idea !

printer.go Outdated
return GetPrinterType(printerName)
}

func GetPrinterType(printerName string) (string, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of returning sting, this function should return more info. Maybe create

type DriverInfo struct {
...
Attributes uint32
}

, then you could do:

di, err := myprinter.DriverInfo()
if err != nil {
...
}
if di.Attributes&PRINTER_DRIVER_XPS != 0 {
...
}

We could start DriverInfo struct just with Attributes field and add more fields later as needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i made the Driver info with driver name, enviroment and driver path

printer.go Outdated
}

func GetPrinterType(printerName string) (string, error) {
b := make([]byte, 1024*10)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 10K? How large this data is (from your own experiment)? Maybe start with that instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, is from my experiment, in my experiment the struct has 6k from a thermal printer with generic driver from windows

zapi.go Outdated
@@ -33,6 +36,25 @@ func GetDefaultPrinter(buf *uint16, bufN *uint32) (err error) {
return
}

func GetPrinterDriver(name string, buf *byte, bufN uint32) (err error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is generated by running "go generate" command. Do not change it manually. This file contains all Windows API, so instead of

func GetPrinterDriver(name string, buf *byte, bufN uint32) (err error)

you want

func GetPrinterDriver(h syscall.Handle, env *uint16, level uint32, di *byte, n uint32, needed *uint32) (err error)

So if you add

//sys   GetPrinterDriver(h syscall.Handle, env *uint16, level uint32, di *byte, n uint32, needed *uint32) (err error) = winspool.GetPrinterDriver

and then run "go generate" command, you will get what you want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont know that ! i made the change and generate the code know !

Copy link
Owner

@alexbrainman alexbrainman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking better.

Thank you.

Alex

@@ -74,6 +75,21 @@ const (
//sys StartPagePrinter(h syscall.Handle) (err error) = winspool.StartPagePrinter
//sys EndPagePrinter(h syscall.Handle) (err error) = winspool.EndPagePrinter
//sys EnumPrinters(flags uint32, name *uint16, level uint32, buf *byte, bufN uint32, needed *uint32, returned *uint32) (err error) = winspool.EnumPrintersW
//sys GetPrinterDriver(h syscall.Handle, env *uint16, level uint32, di *byte, n uint32, needed *uint32) (err error) = winspool.GetPrinterDriverW

func convertLPTSTRToString(ptr *uint16) string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to implement that function, because it already exists. Search for syscall.UTF16ToString in net package - it will show how to do that.

}
di := (*DRIVER_INFO_8)(unsafe.Pointer(&b[0]))

ndi := newDriverInfo(di)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just:

return &DriverInfo{
Name: ...,
...
}, nil

and you won't need newDriverInfo function.

@@ -161,6 +164,42 @@ func Open(name string) (*Printer, error) {
return &p, nil
}

func (p *Printer) DriverInfo() (*DriverInfo, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New function needs documentation.

return ndi, nil
}

func (p *Printer) PrintDataType() (string, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove this function. The name does not explain what it does, and it is simple enough to be implemented in couple of lines of code.

n := uint32(len(b))
var needed uint32
var env uint16 = 0
err := GetPrinterDriver(p.h, &env, 8, &b[0], n, &needed)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using &env and not nil here? &end points to empty environment, while nil is default for "the current environment of the calling application and client machine". Which do we want here?

return err
}

err = p.StartDocument(documentName, dataType)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you had some problem with this code always passing "RAW" to StartDocument. What is the problem? How can I reproduce it? Can you create an issue about this problem and make this PR fix the problem?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is: i had a printer that doesnt accept RAW datatype, the problem is that print only accepts XPS documents.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what is the data that you need to write to the printer when you pass "XPS_PASS" to it? Can I send text data? Will it print properly? Does every printer that reports PRINTER_DRIVER_XPS will work with "XPS_PASS"? Are there any references to all of this somewhere on the Internet?

Maybe we could just introduce another flag for the program that describes data type, and default it to "RAW" instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normal string data, you can send normal text, its print properly, the driver do the work, and Yes every printer with PRINTER_DRIVER_XPS will work with XPS_PASS

And yes, we can do other flag for program to describe data type and set RAW as default

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for explaining. I will wait for you to address my other comments now.

Alex

@@ -41,6 +41,7 @@ func listPrinters() error {
if err != nil {
return err
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank line. Unrelated to your change.

"unsafe"
)

const (
PRINTER_DRIVER_XPS uint32 = 0x00000002
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/PRINTER_DRIVER_XPS uint32/PRINTER_DRIVER_XPS/

make PRINTER_DRIVER_XPS untyped. Untyped consts fit anything.

"unsafe"
)

const (
PRINTER_DRIVER_XPS uint32 = 0x00000002
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add PRINTER_DRIVER_PACKAGE_AWARE and others.

const (
PRINTER_DRIVER_XPS uint32 = 0x00000002
RAW = "RAW"
XPS_PASS = "XPS_PASS"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete both RAW and XPS_PASS. Users won't need them.

@NewtonRocha
Copy link
Author

Sorry for delay, i was very busy with my work those days, i will respond yours comments late today @alexbrainman

@alexbrainman
Copy link
Owner

Sorry for delay, i was very busy with my work those days, i will respond yours comments late today

No worries. Take as long as you like.

Alex

@alexbrainman
Copy link
Owner

@NewtonRocha I could not wait for you to make your changes, so copied and adjusted your code. Feel free to comment here https://github.com/alexbrainman/printer/tree/fix1

Thank you.

Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants