-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathwinspool.go
More file actions
135 lines (112 loc) · 3.6 KB
/
winspool.go
File metadata and controls
135 lines (112 loc) · 3.6 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2010 The win Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package win
import (
"golang.org/x/sys/windows"
"syscall"
"unsafe"
)
// EnumPrinters flags
const (
PRINTER_ENUM_DEFAULT = 0x00000001
PRINTER_ENUM_LOCAL = 0x00000002
PRINTER_ENUM_CONNECTIONS = 0x00000004
PRINTER_ENUM_FAVORITE = 0x00000004
PRINTER_ENUM_NAME = 0x00000008
PRINTER_ENUM_REMOTE = 0x00000010
PRINTER_ENUM_SHARED = 0x00000020
PRINTER_ENUM_NETWORK = 0x00000040
)
// Printer access flags
const (
PRINTER_ACCESS_ADMINISTER = 0x00000004
PRINTER_ACCESS_USE = 0x00000008
PRINTER_ACCESS_MANAGE_LIMITED = 0x00000040
PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE)
)
type PRINTER_INFO_4 struct {
PPrinterName *uint16
PServerName *uint16
Attributes uint32
}
type PRINTER_DEFAULTS struct {
PDatatype *uint16
LPDevMode *DEVMODE
DesiredAccess ACCESS_MASK
}
var (
// Library
libwinspool *windows.LazyDLL
// Functions
deviceCapabilities *windows.LazyProc
documentProperties *windows.LazyProc
enumPrinters *windows.LazyProc
getDefaultPrinter *windows.LazyProc
openPrinter *windows.LazyProc
closePrinter *windows.LazyProc
)
func init() {
// Library
libwinspool = windows.NewLazySystemDLL("winspool.drv")
// Functions
deviceCapabilities = libwinspool.NewProc("DeviceCapabilitiesW")
documentProperties = libwinspool.NewProc("DocumentPropertiesW")
enumPrinters = libwinspool.NewProc("EnumPrintersW")
getDefaultPrinter = libwinspool.NewProc("GetDefaultPrinterW")
openPrinter = libwinspool.NewProc("OpenPrinterW")
closePrinter = libwinspool.NewProc("ClosePrinter")
}
func DeviceCapabilities(pDevice, pPort *uint16, fwCapability uint16, pOutput *uint16, pDevMode *DEVMODE) uint32 {
ret, _, _ := syscall.Syscall6(deviceCapabilities.Addr(), 5,
uintptr(unsafe.Pointer(pDevice)),
uintptr(unsafe.Pointer(pPort)),
uintptr(fwCapability),
uintptr(unsafe.Pointer(pOutput)),
uintptr(unsafe.Pointer(pDevMode)),
0)
return uint32(ret)
}
func DocumentProperties(hWnd HWND, hPrinter HANDLE, pDeviceName *uint16, pDevModeOutput, pDevModeInput *DEVMODE, fMode uint32) int32 {
ret, _, _ := syscall.Syscall6(documentProperties.Addr(), 6,
uintptr(hWnd),
uintptr(hPrinter),
uintptr(unsafe.Pointer(pDeviceName)),
uintptr(unsafe.Pointer(pDevModeOutput)),
uintptr(unsafe.Pointer(pDevModeInput)),
uintptr(fMode))
return int32(ret)
}
func EnumPrinters(Flags uint32, Name *uint16, Level uint32, pPrinterEnum *byte, cbBuf uint32, pcbNeeded, pcReturned *uint32) bool {
ret, _, _ := syscall.Syscall9(enumPrinters.Addr(), 7,
uintptr(Flags),
uintptr(unsafe.Pointer(Name)),
uintptr(Level),
uintptr(unsafe.Pointer(pPrinterEnum)),
uintptr(cbBuf),
uintptr(unsafe.Pointer(pcbNeeded)),
uintptr(unsafe.Pointer(pcReturned)),
0,
0)
return ret != 0
}
func GetDefaultPrinter(pszBuffer *uint16, pcchBuffer *uint32) bool {
ret, _, _ := syscall.Syscall(getDefaultPrinter.Addr(), 2,
uintptr(unsafe.Pointer(pszBuffer)),
uintptr(unsafe.Pointer(pcchBuffer)),
0)
return ret != 0
}
func OpenPrinter(pPrinterName *uint16, phPrinter *HANDLE, pDefault *PRINTER_DEFAULTS) bool {
ret, _, _ := syscall.Syscall(openPrinter, 3,
uintptr(unsafe.Pointer(pPrinterName)),
uintptr(unsafe.Pointer(phPrinter)),
uintptr(unsafe.Pointer(pDefault)))
return ret != 0
}
func ClosePrinter(phPrinter *HANDLE) bool {
ret, _, _ := syscall.Syscall(closePrinter, 1,
uintptr(unsafe.Pointer(phPrinter)), 0, 0)
return ret != 0
}