-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin32api.go
217 lines (183 loc) · 5.23 KB
/
win32api.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package win32api
import (
"github.com/turutcrane/win32api/internal/alloc"
"syscall"
"unsafe"
)
//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zwin32api_windows.go win32api.go w32api_windows.go
type ATOM int16
type BOOL int32
type BYTE byte
type COLORREF DWORD
type DWORD uint32
type HANDLE syscall.Handle
type HBRUSH syscall.Handle
type HCURSOR syscall.Handle
type HDC syscall.Handle
type HDWP syscall.Handle
type HFONT syscall.Handle
type HGDIOBJ syscall.Handle
type HGLRC syscall.Handle
type HGLOBAL syscall.Handle
type HICON syscall.Handle
type HINSTANCE syscall.Handle
type HKL syscall.Handle
type HMENU syscall.Handle
type HMODULE syscall.Handle
type HWND syscall.Handle
type HRESULT LONG
type HRGN syscall.Handle
type HRSRC syscall.Handle
type LONG int32
type LPVOID uintptr
type LRESULT uintptr
type WPARAM uintptr // UINT_PTR
type LPARAM uintptr // LONG_PTR
type WORD int16
type SHORT int16
type LARGE_INTEGER int64
type WCHAR uint16
type USHORT uint16
type ULONG uint32
type PWSTR *uint16
type ProcessDpiAwareness int
type MessageId UINT
type KnownFolderFlag UINT
type UINT uint32
type INT_PTR uintptr
type WNDPROC uintptr
type DLGPROC uintptr
type LPFRHOOKPROC uintptr
// type WndProc func(hWnd win32api.HWND, message win32api.UINT, wParam win32api.WPARAM, lParam win32api.LPARAM) win32api.LRESULT
type WndProc func(hWnd HWND, message UINT, wParam WPARAM, lParam LPARAM) LRESULT
//type LPCWSTR *uint16
//type LONG_PTR uintptr
type GUID struct {
Data1 ULONG
Data2 USHORT
Data3 USHORT
Data4 [8]BYTE
}
type KNOWNFOLDERID GUID
type REFKNOWNFOLDERID uintptr
func MakeIntResource(id uint16) *uint16 {
return (*uint16)(unsafe.Pointer(uintptr(id)))
}
func ToPCreatestruct(lparam LPARAM) *Createstruct {
return (*Createstruct)(unsafe.Pointer(lparam))
}
func WndProcToWNDPROC(wp WndProc) WNDPROC {
return WNDPROC(syscall.NewCallback(wp))
}
func Makepoints(lParam LPARAM) (points Points) {
points.X = SHORT(lParam & 0xff)
points.Y = SHORT((lParam >> 16) & 0xff)
return points
}
//sys SetLastError(dwErrCode DWORD) = SetLastError
// GetWindowLongPtr does not return syscall.EINVAL
//sys getWindowLongPtr(hWnd HWND, nIndex int) (longPtr uintptr, err error) = user32.GetWindowLongPtrW
func GetWindowLongPtr(hWnd HWND, nIndex int) (longPtr uintptr, err error) {
longPtr, err = getWindowLongPtr(hWnd, nIndex)
if err == syscall.EINVAL {
err = nil
}
return
}
// SetWindowLongPtr does not return syscall.EINTVAL
//sys setWindowLongPtr(hWnd HWND, nIndex int, dwNewLong uintptr) (longPtr uintptr, err error) = user32.SetWindowLongPtrW
func SetWindowLongPtr(hWnd HWND, nIndex int, dwNewLong uintptr) (longPtr uintptr, err error) {
SetLastError(0)
longPtr, err = setWindowLongPtr(hWnd, nIndex, dwNewLong)
if err == syscall.EINVAL {
err = nil
}
return
}
func BoolToBOOL(b bool) BOOL {
if b {
return 1
}
return 0
}
func BOOLToBool(b BOOL) bool {
if b != 0 {
return true
}
return false
}
func LOWORD(w WPARAM) uintptr {
return uintptr(w & 0xffff)
}
func HIWORD(w WPARAM) uintptr {
return uintptr((w >> 16) & 0xffff)
}
func GET_X_LPARAM(lParam LPARAM) int {
low := lParam & 0xffff
var x int
if low&0x8000 != 0 {
x = int(low) - 0x10000
} else {
x = int(low)
}
return x
}
func GET_Y_LPARAM(lParam LPARAM) int {
high := (lParam >> 16) & 0xffff
var y int
if high&0x8000 != 0 {
y = int(high) - 0x10000
} else {
y = int(high)
}
return y
}
func GET_WHEEL_DELTA_WPARAM(wParam WPARAM) int {
high := (wParam >> 16) & 0xffff
var delta int
if high&0x8000 != 0 {
delta = int(high) - 0x10000
} else {
delta = int(high)
}
return delta
}
func LParamToPRect(lParam LPARAM) *Rect {
return (*Rect)(unsafe.Pointer(lParam))
}
func DialogBoxParam(hInstance HINSTANCE, lpTemplateName *uint16, hWndParent HWND, lpDialogFunc DLGPROC, dwInitParam LPARAM) (r INT_PTR, err error) {
r, err = dialogBoxParamW(hInstance, lpTemplateName, hWndParent, lpDialogFunc, dwInitParam)
// If the function fails because the hWndParent parameter is invalid, the return value is zero.
if err != nil && r == 0 {
err = syscall.EINVAL
}
return r, err
}
//sys windowFromPoint(point uintptr) (r HWND) = user32.WindowFromPoint
func WindowFromPoint(point Point) (r HWND) {
var xy uintptr
x := uintptr(point.X)
y := uintptr(point.Y)
mask32 := (^uintptr(0) >> (64 - 32))
xy = (x & mask32) | (y << 32)
return windowFromPoint(xy)
}
func SHGetFolderPath(hwnd HWND, csidl int, hToken HANDLE, dwFlags DWORD) (r HRESULT, path string) {
ptr := alloc.Malloc(alloc.MaxPath * alloc.SizeofTCHAR)
r0, _, _ := syscall.Syscall6(procSHGetFolderPathW.Addr(), 5, uintptr(hwnd), uintptr(csidl), uintptr(hToken), uintptr(dwFlags), ptr, 0)
r = HRESULT(r0)
slice := (*[1 << 30]uint16)(unsafe.Pointer(ptr))[:]
path = syscall.UTF16ToString(slice)
return r, path
}
func SHGetKnownFolderPath(rfid REFKNOWNFOLDERID, dwFlags DWORD, hToken HANDLE) (r HRESULT, path string) {
ptr := alloc.Malloc(alloc.SizeofPWSTR)
defer alloc.Free(ptr)
r0, _, _ := syscall.Syscall6(procSHGetKnownFolderPath.Addr(), 4, uintptr(rfid), uintptr(dwFlags), uintptr(hToken), uintptr(unsafe.Pointer(ptr)), 0, 0)
r = HRESULT(r0)
pptr := (**uint16)(unsafe.Pointer(ptr))
defer CoTaskMemFree(LPVOID(unsafe.Pointer(*pptr)))
slice := (*[1 << 30]uint16)(unsafe.Pointer(*pptr))[:]
path = syscall.UTF16ToString(slice)
return r, path
}