I'm currently evaluating Go to build a few tools and utilities for internal use in my company. So far, it is aces in my book with the exception of printing.Despite having searched for the past week or so, I have been unable to find any example illustrating how a document can be sent to a printer (network/local). The tools that I'll be using will need the ability to print reports/vouchers/etc which could be in any format (raw/pdf/text/etc).In the worst case, I would have to use another utility to send an intermediate file (generated by the utilities built with Go) to the printer but I'm hoping that there's something in Go that can help me with this.I'd greatly appreciate any pointers or a nudge in the right direction.--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I have written the equivalent program using Win32 bindings for the printer but I'm not sure how to approach this using Go.
I guess I'll start looking for examples of Go which make use of Win32 calls.
--
dll = syscall.MustLoadDLL("winspool.drv")getDefaultPrinter = dll.MustFindProc("GetDefaultPrinterW")
var pn[256] uint16plen := len(pn)getDefaultPrinter.Call(uintptr(unsafe.Pointer(&pn)), uintptr(unsafe.Pointer(&plen)))printerName := syscall.UTF16ToString(pn[:])return printerName, syscall.StringToUTF16(printerName)
var printerHandle uintptrr,e1,e2 := openPrinter.Call(uintptr(unsafe.Pointer(&printerName)), uintptr(unsafe.Pointer(&printerHandle)), 0)fmt.Println(r)fmt.Println(e1)fmt.Println(e2)
I suspect your OpenPrinter first parameter is wrong. The doco says "A pointer to a null-terminated string that specifies the name of the printer or print server". Since you use W version of OpenPrinter (OpenPrinterW), you should give it array of uint16s with 0 at the end. So, your code is no good, because you are passing address of Go string, which , I suspect, contains internal Go structure and not string data itself. But even string data is no good, because it would be utf8 encoded (probably ascii in your case). Try using syscall.UTF16PtrFromString instead.
Show us your code. That way we could just run it here and adjust appropriately.
Alex
dll = syscall.MustLoadDLL("winspool.drv")getDefaultPrinter = dll.MustFindProc("GetDefaultPrinterW")
openPrinter = dll.MustFindProc("OpenPrinterW")
var printerHandle uintptrr,e1,e2 := openPrinter.Call(uintptr(unsafe.Pointer(&printerName)), uintptr(unsafe.Pointer(&printerHandle)), 0)fmt.Println(r)fmt.Println(e1)fmt.Println(e2)
var pn[256] uint16plen := len(pn)
r, e1, e2 := getDefaultPrinter.Call(uintptr(unsafe.Pointer(&pn)), uintptr(unsafe.Pointer(&plen)))r=re1=e1e2=e2printerName := syscall.UTF16ToString(pn[:])//fmt.Println(printerName)//fmt.Println(syscall.StringToUTF16(printerName))return printerName, syscall.StringToUTF16(printerName)
printerName, printerName16 := getDefaultPrinterName();//fmt.Println(printerName)//fmt.Println(printerName16)openPrinterFunc(printerName, printerName16)
"syscall""unsafe""io/ioutil"
pDocName []bytepOutputFile []bytepDatatype []byte
dll = syscall.MustLoadDLL("winspool.drv")getDefaultPrinter = dll.MustFindProc("GetDefaultPrinterW")openPrinter = dll.MustFindProc("OpenPrinterW")
startDocPrinter = dll.MustFindProc("StartDocPrinterW")startPagePrinter = dll.MustFindProc("StartPagePrinter")writePrinter = dll.MustFindProc("WritePrinter")endPagePrinter = dll.MustFindProc("EndPagePrinter")endDocPrinter = dll.MustFindProc("EndDocPrinter")closePrinter= dll.MustFindProc("ClosePrinter")
printerName, printerName16 := getDefaultPrinterName();printerHandle := openPrinterFunc(printerName, printerName16)startPrinter(printerHandle)startPagePrinter.Call(printerHandle)writePrinterFunc(printerHandle)endPagePrinter.Call(printerHandle)endDocPrinter.Call(printerHandle)closePrinter.Call(printerHandle)
fileContents,_ := ioutil.ReadFile(FILENAME)var contentLen uintptr = uintptr(len(fileContents))var writtenLen intwritePrinter.Call(printerHandle, uintptr(unsafe.Pointer(&fileContents[0])), contentLen, uintptr(unsafe.Pointer(&writtenLen)))
di := DOC_INFO_1{[]byte(""), nil, []byte("RAW")}startDocPrinter.Call(printerHandle, 1, uintptr(unsafe.Pointer(&di)))
var printerHandle uintptropenPrinter.Call(uintptr(unsafe.Pointer(&printerName16[0])), uintptr(unsafe.Pointer(&printerHandle)), 0)return(printerHandle)
var pn[256] uint16plen := len(pn)
getDefaultPrinter.Call(uintptr(unsafe.Pointer(&pn)), uintptr(unsafe.Pointer(&plen)))printerName := syscall.UTF16ToString(pn[:])return printerName, syscall.StringToUTF16(printerName)