-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinting.go
67 lines (48 loc) · 1.41 KB
/
printing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package walk
import (
"os"
"unsafe"
)
import . "walk/winapi"
func defaultPrinterNamePtr() *uint16 {
var bufLen uint32
GetDefaultPrinter(nil, &bufLen)
buf := make([]uint16, bufLen)
if !GetDefaultPrinter(&buf[0], &bufLen) {
panic("failed to retrieve default printer name")
}
return &buf[0]
}
func defaultPrinterName() string {
return UTF16PtrToString(defaultPrinterNamePtr())
}
// DefaultPrinterName returns the name of the default printer.
func DefaultPrinterName() (name string, err os.Error) {
defer func() {
if x := recover(); x != nil {
err = toError(x)
}
}()
return defaultPrinterName(), nil
}
// PrinterNames returns the names of the installed printers.
func PrinterNames() ([]string, os.Error) {
var bufLen, count uint32
EnumPrinters(PRINTER_ENUM_LOCAL, nil, 4, nil, 0, &bufLen, &count)
if bufLen == 0 {
return make([]string, 0), nil
}
buf := make([]byte, int(bufLen))
if !EnumPrinters(PRINTER_ENUM_LOCAL, nil, 4, &buf[0], bufLen, &bufLen, &count) {
return nil, newError("EnumPrinters failed")
}
printers := (*[1 << 24]PRINTER_INFO_4)(unsafe.Pointer(&buf[0]))
printerNames := make([]string, count)
for i := 0; i < int(count); i++ {
printerNames[i] = UTF16PtrToString(printers[i].PPrinterName)
}
return printerNames, nil
}