forked from antonlahti/go-winapi
/
kernel32.go
207 lines (171 loc) · 4.48 KB
/
kernel32.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
// 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 winapi
import (
"syscall"
"unsafe"
)
const MAX_PATH = 260
// Error codes
const (
ERROR_SUCCESS = 0
ERROR_FILE_NOT_FOUND = 2
ERROR_INVALID_PARAMETER = 87
ERROR_INSUFFICIENT_BUFFER = 122
ERROR_MORE_DATA = 234
)
// GlobalAlloc flags
const (
GHND = 0x0042
GMEM_FIXED = 0x0000
GMEM_MOVEABLE = 0x0002
GMEM_ZEROINIT = 0x0040
GPTR = 0x004
)
var (
// Library
libkernel32 uintptr
// Functions
fileTimeToSystemTime uintptr
getLastError uintptr
getLogicalDriveStrings uintptr
getModuleHandle uintptr
getThreadLocale uintptr
globalAlloc uintptr
globalFree uintptr
globalLock uintptr
globalUnlock uintptr
moveMemory uintptr
mulDiv uintptr
setLastError uintptr
systemTimeToFileTime uintptr
)
type (
ATOM uint16
HANDLE uintptr
HGLOBAL HANDLE
HINSTANCE HANDLE
LCID uint32
)
type FILETIME struct {
DwLowDateTime uint32
DwHighDateTime uint32
}
type SYSTEMTIME struct {
WYear uint16
WMonth uint16
WDayOfWeek uint16
WDay uint16
WHour uint16
WMinute uint16
WSecond uint16
WMilliseconds uint16
}
func init() {
// Library
libkernel32 = MustLoadLibrary("kernel32.dll")
// Functions
fileTimeToSystemTime = MustGetProcAddress(libkernel32, "FileTimeToSystemTime")
getLastError = MustGetProcAddress(libkernel32, "GetLastError")
getLogicalDriveStrings = MustGetProcAddress(libkernel32, "GetLogicalDriveStringsW")
getModuleHandle = MustGetProcAddress(libkernel32, "GetModuleHandleW")
getThreadLocale = MustGetProcAddress(libkernel32, "GetThreadLocale")
globalAlloc = MustGetProcAddress(libkernel32, "GlobalAlloc")
globalFree = MustGetProcAddress(libkernel32, "GlobalFree")
globalLock = MustGetProcAddress(libkernel32, "GlobalLock")
globalUnlock = MustGetProcAddress(libkernel32, "GlobalUnlock")
moveMemory = MustGetProcAddress(libkernel32, "RtlMoveMemory")
mulDiv = MustGetProcAddress(libkernel32, "MulDiv")
setLastError = MustGetProcAddress(libkernel32, "SetLastError")
systemTimeToFileTime = MustGetProcAddress(libkernel32, "SystemTimeToFileTime")
}
func FileTimeToSystemTime(lpFileTime *FILETIME, lpSystemTime *SYSTEMTIME) bool {
ret, _, _ := syscall.Syscall(fileTimeToSystemTime, 2,
uintptr(unsafe.Pointer(lpFileTime)),
uintptr(unsafe.Pointer(lpSystemTime)),
0)
return ret != 0
}
func GetLastError() uint32 {
ret, _, _ := syscall.Syscall(getLastError, 0,
0,
0,
0)
return uint32(ret)
}
func GetLogicalDriveStrings(nBufferLength uint32, lpBuffer *uint16) uint32 {
ret, _, _ := syscall.Syscall(getLogicalDriveStrings, 2,
uintptr(nBufferLength),
uintptr(unsafe.Pointer(lpBuffer)),
0)
return uint32(ret)
}
func GetModuleHandle(lpModuleName *uint16) HINSTANCE {
ret, _, _ := syscall.Syscall(getModuleHandle, 1,
uintptr(unsafe.Pointer(lpModuleName)),
0,
0)
return HINSTANCE(ret)
}
func GetThreadLocale() LCID {
ret, _, _ := syscall.Syscall(getThreadLocale, 0,
0,
0,
0)
return LCID(ret)
}
func GlobalAlloc(uFlags uint32, dwBytes uintptr) HGLOBAL {
ret, _, _ := syscall.Syscall(globalAlloc, 2,
uintptr(uFlags),
dwBytes,
0)
return HGLOBAL(ret)
}
func GlobalFree(hMem HGLOBAL) HGLOBAL {
ret, _, _ := syscall.Syscall(globalFree, 1,
uintptr(hMem),
0,
0)
return HGLOBAL(ret)
}
func GlobalLock(hMem HGLOBAL) unsafe.Pointer {
ret, _, _ := syscall.Syscall(globalLock, 1,
uintptr(hMem),
0,
0)
return unsafe.Pointer(ret)
}
func GlobalUnlock(hMem HGLOBAL) bool {
ret, _, _ := syscall.Syscall(globalUnlock, 1,
uintptr(hMem),
0,
0)
return ret != 0
}
func MoveMemory(destination, source unsafe.Pointer, length uintptr) {
syscall.Syscall(moveMemory, 3,
uintptr(unsafe.Pointer(destination)),
uintptr(source),
uintptr(length))
}
func MulDiv(nNumber, nNumerator, nDenominator int32) int32 {
ret, _, _ := syscall.Syscall(mulDiv, 3,
uintptr(nNumber),
uintptr(nNumerator),
uintptr(nDenominator))
return int32(ret)
}
func SetLastError(dwErrorCode uint32) {
syscall.Syscall(setLastError, 1,
uintptr(dwErrorCode),
0,
0)
}
func SystemTimeToFileTime(lpSystemTime *SYSTEMTIME, lpFileTime *FILETIME) bool {
ret, _, _ := syscall.Syscall(systemTimeToFileTime, 2,
uintptr(unsafe.Pointer(lpSystemTime)),
uintptr(unsafe.Pointer(lpFileTime)),
0)
return ret != 0
}