1
2
3
4
5 package winapi
6
7 import (
8 "syscall"
9 "unsafe"
10
11 "golang.org/x/sys/windows"
12 )
13
14 var _ unsafe.Pointer
15
16
17
18 const (
19 errnoERROR_IO_PENDING = 997
20 )
21
22 var (
23 errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
24 errERROR_EINVAL error = syscall.EINVAL
25 )
26
27
28
29 func errnoErr(e syscall.Errno) error {
30 switch e {
31 case 0:
32 return errERROR_EINVAL
33 case errnoERROR_IO_PENDING:
34 return errERROR_IO_PENDING
35 }
36
37
38
39 return e
40 }
41
42 var (
43 modadvapi32 = windows.NewLazySystemDLL("advapi32.dll")
44 modbindfltapi = windows.NewLazySystemDLL("bindfltapi.dll")
45 modcfgmgr32 = windows.NewLazySystemDLL("cfgmgr32.dll")
46 modiphlpapi = windows.NewLazySystemDLL("iphlpapi.dll")
47 modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
48 modnetapi32 = windows.NewLazySystemDLL("netapi32.dll")
49 modntdll = windows.NewLazySystemDLL("ntdll.dll")
50 modoffreg = windows.NewLazySystemDLL("offreg.dll")
51
52 procLogonUserW = modadvapi32.NewProc("LogonUserW")
53 procBfSetupFilter = modbindfltapi.NewProc("BfSetupFilter")
54 procCM_Get_DevNode_PropertyW = modcfgmgr32.NewProc("CM_Get_DevNode_PropertyW")
55 procCM_Get_Device_ID_ListA = modcfgmgr32.NewProc("CM_Get_Device_ID_ListA")
56 procCM_Get_Device_ID_List_SizeA = modcfgmgr32.NewProc("CM_Get_Device_ID_List_SizeA")
57 procCM_Locate_DevNodeW = modcfgmgr32.NewProc("CM_Locate_DevNodeW")
58 procSetJobCompartmentId = modiphlpapi.NewProc("SetJobCompartmentId")
59 procClosePseudoConsole = modkernel32.NewProc("ClosePseudoConsole")
60 procCopyFileW = modkernel32.NewProc("CopyFileW")
61 procCreatePseudoConsole = modkernel32.NewProc("CreatePseudoConsole")
62 procCreateRemoteThread = modkernel32.NewProc("CreateRemoteThread")
63 procGetActiveProcessorCount = modkernel32.NewProc("GetActiveProcessorCount")
64 procIsProcessInJob = modkernel32.NewProc("IsProcessInJob")
65 procLocalAlloc = modkernel32.NewProc("LocalAlloc")
66 procLocalFree = modkernel32.NewProc("LocalFree")
67 procOpenJobObjectW = modkernel32.NewProc("OpenJobObjectW")
68 procQueryInformationJobObject = modkernel32.NewProc("QueryInformationJobObject")
69 procQueryIoRateControlInformationJobObject = modkernel32.NewProc("QueryIoRateControlInformationJobObject")
70 procResizePseudoConsole = modkernel32.NewProc("ResizePseudoConsole")
71 procSearchPathW = modkernel32.NewProc("SearchPathW")
72 procSetIoRateControlInformationJobObject = modkernel32.NewProc("SetIoRateControlInformationJobObject")
73 procNetLocalGroupAddMembers = modnetapi32.NewProc("NetLocalGroupAddMembers")
74 procNetLocalGroupGetInfo = modnetapi32.NewProc("NetLocalGroupGetInfo")
75 procNetUserAdd = modnetapi32.NewProc("NetUserAdd")
76 procNetUserDel = modnetapi32.NewProc("NetUserDel")
77 procNtCreateFile = modntdll.NewProc("NtCreateFile")
78 procNtCreateJobObject = modntdll.NewProc("NtCreateJobObject")
79 procNtOpenDirectoryObject = modntdll.NewProc("NtOpenDirectoryObject")
80 procNtOpenJobObject = modntdll.NewProc("NtOpenJobObject")
81 procNtQueryDirectoryObject = modntdll.NewProc("NtQueryDirectoryObject")
82 procNtQueryInformationProcess = modntdll.NewProc("NtQueryInformationProcess")
83 procNtQuerySystemInformation = modntdll.NewProc("NtQuerySystemInformation")
84 procNtSetInformationFile = modntdll.NewProc("NtSetInformationFile")
85 procRtlNtStatusToDosError = modntdll.NewProc("RtlNtStatusToDosError")
86 procORCloseHive = modoffreg.NewProc("ORCloseHive")
87 procORCreateHive = modoffreg.NewProc("ORCreateHive")
88 procORSaveHive = modoffreg.NewProc("ORSaveHive")
89 )
90
91 func LogonUser(username *uint16, domain *uint16, password *uint16, logonType uint32, logonProvider uint32, token *windows.Token) (err error) {
92 r1, _, e1 := syscall.Syscall6(procLogonUserW.Addr(), 6, uintptr(unsafe.Pointer(username)), uintptr(unsafe.Pointer(domain)), uintptr(unsafe.Pointer(password)), uintptr(logonType), uintptr(logonProvider), uintptr(unsafe.Pointer(token)))
93 if r1 == 0 {
94 err = errnoErr(e1)
95 }
96 return
97 }
98
99 func BfSetupFilter(jobHandle windows.Handle, flags uint32, virtRootPath *uint16, virtTargetPath *uint16, virtExceptions **uint16, virtExceptionPathCount uint32) (hr error) {
100 hr = procBfSetupFilter.Find()
101 if hr != nil {
102 return
103 }
104 r0, _, _ := syscall.Syscall6(procBfSetupFilter.Addr(), 6, uintptr(jobHandle), uintptr(flags), uintptr(unsafe.Pointer(virtRootPath)), uintptr(unsafe.Pointer(virtTargetPath)), uintptr(unsafe.Pointer(virtExceptions)), uintptr(virtExceptionPathCount))
105 if int32(r0) < 0 {
106 if r0&0x1fff0000 == 0x00070000 {
107 r0 &= 0xffff
108 }
109 hr = syscall.Errno(r0)
110 }
111 return
112 }
113
114 func CMGetDevNodeProperty(dnDevInst uint32, propertyKey *DevPropKey, propertyType *uint32, propertyBuffer *uint16, propertyBufferSize *uint32, uFlags uint32) (hr error) {
115 r0, _, _ := syscall.Syscall6(procCM_Get_DevNode_PropertyW.Addr(), 6, uintptr(dnDevInst), uintptr(unsafe.Pointer(propertyKey)), uintptr(unsafe.Pointer(propertyType)), uintptr(unsafe.Pointer(propertyBuffer)), uintptr(unsafe.Pointer(propertyBufferSize)), uintptr(uFlags))
116 if int32(r0) < 0 {
117 if r0&0x1fff0000 == 0x00070000 {
118 r0 &= 0xffff
119 }
120 hr = syscall.Errno(r0)
121 }
122 return
123 }
124
125 func CMGetDeviceIDList(pszFilter *byte, buffer *byte, bufferLen uint32, uFlags uint32) (hr error) {
126 r0, _, _ := syscall.Syscall6(procCM_Get_Device_ID_ListA.Addr(), 4, uintptr(unsafe.Pointer(pszFilter)), uintptr(unsafe.Pointer(buffer)), uintptr(bufferLen), uintptr(uFlags), 0, 0)
127 if int32(r0) < 0 {
128 if r0&0x1fff0000 == 0x00070000 {
129 r0 &= 0xffff
130 }
131 hr = syscall.Errno(r0)
132 }
133 return
134 }
135
136 func CMGetDeviceIDListSize(pulLen *uint32, pszFilter *byte, uFlags uint32) (hr error) {
137 r0, _, _ := syscall.Syscall(procCM_Get_Device_ID_List_SizeA.Addr(), 3, uintptr(unsafe.Pointer(pulLen)), uintptr(unsafe.Pointer(pszFilter)), uintptr(uFlags))
138 if int32(r0) < 0 {
139 if r0&0x1fff0000 == 0x00070000 {
140 r0 &= 0xffff
141 }
142 hr = syscall.Errno(r0)
143 }
144 return
145 }
146
147 func CMLocateDevNode(pdnDevInst *uint32, pDeviceID string, uFlags uint32) (hr error) {
148 var _p0 *uint16
149 _p0, hr = syscall.UTF16PtrFromString(pDeviceID)
150 if hr != nil {
151 return
152 }
153 return _CMLocateDevNode(pdnDevInst, _p0, uFlags)
154 }
155
156 func _CMLocateDevNode(pdnDevInst *uint32, pDeviceID *uint16, uFlags uint32) (hr error) {
157 r0, _, _ := syscall.Syscall(procCM_Locate_DevNodeW.Addr(), 3, uintptr(unsafe.Pointer(pdnDevInst)), uintptr(unsafe.Pointer(pDeviceID)), uintptr(uFlags))
158 if int32(r0) < 0 {
159 if r0&0x1fff0000 == 0x00070000 {
160 r0 &= 0xffff
161 }
162 hr = syscall.Errno(r0)
163 }
164 return
165 }
166
167 func SetJobCompartmentId(handle windows.Handle, compartmentId uint32) (win32Err error) {
168 r0, _, _ := syscall.Syscall(procSetJobCompartmentId.Addr(), 2, uintptr(handle), uintptr(compartmentId), 0)
169 if r0 != 0 {
170 win32Err = syscall.Errno(r0)
171 }
172 return
173 }
174
175 func ClosePseudoConsole(hpc windows.Handle) {
176 syscall.Syscall(procClosePseudoConsole.Addr(), 1, uintptr(hpc), 0, 0)
177 return
178 }
179
180 func CopyFileW(existingFileName *uint16, newFileName *uint16, failIfExists int32) (err error) {
181 r1, _, e1 := syscall.Syscall(procCopyFileW.Addr(), 3, uintptr(unsafe.Pointer(existingFileName)), uintptr(unsafe.Pointer(newFileName)), uintptr(failIfExists))
182 if r1 == 0 {
183 err = errnoErr(e1)
184 }
185 return
186 }
187
188 func createPseudoConsole(size uint32, hInput windows.Handle, hOutput windows.Handle, dwFlags uint32, hpcon *windows.Handle) (hr error) {
189 r0, _, _ := syscall.Syscall6(procCreatePseudoConsole.Addr(), 5, uintptr(size), uintptr(hInput), uintptr(hOutput), uintptr(dwFlags), uintptr(unsafe.Pointer(hpcon)), 0)
190 if int32(r0) < 0 {
191 if r0&0x1fff0000 == 0x00070000 {
192 r0 &= 0xffff
193 }
194 hr = syscall.Errno(r0)
195 }
196 return
197 }
198
199 func CreateRemoteThread(process windows.Handle, sa *windows.SecurityAttributes, stackSize uint32, startAddr uintptr, parameter uintptr, creationFlags uint32, threadID *uint32) (handle windows.Handle, err error) {
200 r0, _, e1 := syscall.Syscall9(procCreateRemoteThread.Addr(), 7, uintptr(process), uintptr(unsafe.Pointer(sa)), uintptr(stackSize), uintptr(startAddr), uintptr(parameter), uintptr(creationFlags), uintptr(unsafe.Pointer(threadID)), 0, 0)
201 handle = windows.Handle(r0)
202 if handle == 0 {
203 err = errnoErr(e1)
204 }
205 return
206 }
207
208 func GetActiveProcessorCount(groupNumber uint16) (amount uint32) {
209 r0, _, _ := syscall.Syscall(procGetActiveProcessorCount.Addr(), 1, uintptr(groupNumber), 0, 0)
210 amount = uint32(r0)
211 return
212 }
213
214 func IsProcessInJob(procHandle windows.Handle, jobHandle windows.Handle, result *int32) (err error) {
215 r1, _, e1 := syscall.Syscall(procIsProcessInJob.Addr(), 3, uintptr(procHandle), uintptr(jobHandle), uintptr(unsafe.Pointer(result)))
216 if r1 == 0 {
217 err = errnoErr(e1)
218 }
219 return
220 }
221
222 func LocalAlloc(flags uint32, size int) (ptr uintptr) {
223 r0, _, _ := syscall.Syscall(procLocalAlloc.Addr(), 2, uintptr(flags), uintptr(size), 0)
224 ptr = uintptr(r0)
225 return
226 }
227
228 func LocalFree(ptr uintptr) {
229 syscall.Syscall(procLocalFree.Addr(), 1, uintptr(ptr), 0, 0)
230 return
231 }
232
233 func OpenJobObject(desiredAccess uint32, inheritHandle int32, lpName *uint16) (handle windows.Handle, err error) {
234 r0, _, e1 := syscall.Syscall(procOpenJobObjectW.Addr(), 3, uintptr(desiredAccess), uintptr(inheritHandle), uintptr(unsafe.Pointer(lpName)))
235 handle = windows.Handle(r0)
236 if handle == 0 {
237 err = errnoErr(e1)
238 }
239 return
240 }
241
242 func QueryInformationJobObject(jobHandle windows.Handle, infoClass uint32, jobObjectInfo unsafe.Pointer, jobObjectInformationLength uint32, lpReturnLength *uint32) (err error) {
243 r1, _, e1 := syscall.Syscall6(procQueryInformationJobObject.Addr(), 5, uintptr(jobHandle), uintptr(infoClass), uintptr(jobObjectInfo), uintptr(jobObjectInformationLength), uintptr(unsafe.Pointer(lpReturnLength)), 0)
244 if r1 == 0 {
245 err = errnoErr(e1)
246 }
247 return
248 }
249
250 func QueryIoRateControlInformationJobObject(jobHandle windows.Handle, volumeName *uint16, ioRateControlInfo **JOBOBJECT_IO_RATE_CONTROL_INFORMATION, infoBlockCount *uint32) (ret uint32, err error) {
251 r0, _, e1 := syscall.Syscall6(procQueryIoRateControlInformationJobObject.Addr(), 4, uintptr(jobHandle), uintptr(unsafe.Pointer(volumeName)), uintptr(unsafe.Pointer(ioRateControlInfo)), uintptr(unsafe.Pointer(infoBlockCount)), 0, 0)
252 ret = uint32(r0)
253 if ret == 0 {
254 err = errnoErr(e1)
255 }
256 return
257 }
258
259 func resizePseudoConsole(hPc windows.Handle, size uint32) (hr error) {
260 r0, _, _ := syscall.Syscall(procResizePseudoConsole.Addr(), 2, uintptr(hPc), uintptr(size), 0)
261 if int32(r0) < 0 {
262 if r0&0x1fff0000 == 0x00070000 {
263 r0 &= 0xffff
264 }
265 hr = syscall.Errno(r0)
266 }
267 return
268 }
269
270 func SearchPath(lpPath *uint16, lpFileName *uint16, lpExtension *uint16, nBufferLength uint32, lpBuffer *uint16, lpFilePath *uint16) (size uint32, err error) {
271 r0, _, e1 := syscall.Syscall6(procSearchPathW.Addr(), 6, uintptr(unsafe.Pointer(lpPath)), uintptr(unsafe.Pointer(lpFileName)), uintptr(unsafe.Pointer(lpExtension)), uintptr(nBufferLength), uintptr(unsafe.Pointer(lpBuffer)), uintptr(unsafe.Pointer(lpFilePath)))
272 size = uint32(r0)
273 if size == 0 {
274 err = errnoErr(e1)
275 }
276 return
277 }
278
279 func SetIoRateControlInformationJobObject(jobHandle windows.Handle, ioRateControlInfo *JOBOBJECT_IO_RATE_CONTROL_INFORMATION) (ret uint32, err error) {
280 r0, _, e1 := syscall.Syscall(procSetIoRateControlInformationJobObject.Addr(), 2, uintptr(jobHandle), uintptr(unsafe.Pointer(ioRateControlInfo)), 0)
281 ret = uint32(r0)
282 if ret == 0 {
283 err = errnoErr(e1)
284 }
285 return
286 }
287
288 func netLocalGroupAddMembers(serverName *uint16, groupName *uint16, level uint32, buf *byte, totalEntries uint32) (status error) {
289 r0, _, _ := syscall.Syscall6(procNetLocalGroupAddMembers.Addr(), 5, uintptr(unsafe.Pointer(serverName)), uintptr(unsafe.Pointer(groupName)), uintptr(level), uintptr(unsafe.Pointer(buf)), uintptr(totalEntries), 0)
290 if r0 != 0 {
291 status = syscall.Errno(r0)
292 }
293 return
294 }
295
296 func netLocalGroupGetInfo(serverName *uint16, groupName *uint16, level uint32, bufptr **byte) (status error) {
297 r0, _, _ := syscall.Syscall6(procNetLocalGroupGetInfo.Addr(), 4, uintptr(unsafe.Pointer(serverName)), uintptr(unsafe.Pointer(groupName)), uintptr(level), uintptr(unsafe.Pointer(bufptr)), 0, 0)
298 if r0 != 0 {
299 status = syscall.Errno(r0)
300 }
301 return
302 }
303
304 func netUserAdd(serverName *uint16, level uint32, buf *byte, parm_err *uint32) (status error) {
305 r0, _, _ := syscall.Syscall6(procNetUserAdd.Addr(), 4, uintptr(unsafe.Pointer(serverName)), uintptr(level), uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(parm_err)), 0, 0)
306 if r0 != 0 {
307 status = syscall.Errno(r0)
308 }
309 return
310 }
311
312 func netUserDel(serverName *uint16, username *uint16) (status error) {
313 r0, _, _ := syscall.Syscall(procNetUserDel.Addr(), 2, uintptr(unsafe.Pointer(serverName)), uintptr(unsafe.Pointer(username)), 0)
314 if r0 != 0 {
315 status = syscall.Errno(r0)
316 }
317 return
318 }
319
320 func NtCreateFile(handle *uintptr, accessMask uint32, oa *ObjectAttributes, iosb *IOStatusBlock, allocationSize *uint64, fileAttributes uint32, shareAccess uint32, createDisposition uint32, createOptions uint32, eaBuffer *byte, eaLength uint32) (status uint32) {
321 r0, _, _ := syscall.Syscall12(procNtCreateFile.Addr(), 11, uintptr(unsafe.Pointer(handle)), uintptr(accessMask), uintptr(unsafe.Pointer(oa)), uintptr(unsafe.Pointer(iosb)), uintptr(unsafe.Pointer(allocationSize)), uintptr(fileAttributes), uintptr(shareAccess), uintptr(createDisposition), uintptr(createOptions), uintptr(unsafe.Pointer(eaBuffer)), uintptr(eaLength), 0)
322 status = uint32(r0)
323 return
324 }
325
326 func NtCreateJobObject(jobHandle *windows.Handle, desiredAccess uint32, objAttributes *ObjectAttributes) (status uint32) {
327 r0, _, _ := syscall.Syscall(procNtCreateJobObject.Addr(), 3, uintptr(unsafe.Pointer(jobHandle)), uintptr(desiredAccess), uintptr(unsafe.Pointer(objAttributes)))
328 status = uint32(r0)
329 return
330 }
331
332 func NtOpenDirectoryObject(handle *uintptr, accessMask uint32, oa *ObjectAttributes) (status uint32) {
333 r0, _, _ := syscall.Syscall(procNtOpenDirectoryObject.Addr(), 3, uintptr(unsafe.Pointer(handle)), uintptr(accessMask), uintptr(unsafe.Pointer(oa)))
334 status = uint32(r0)
335 return
336 }
337
338 func NtOpenJobObject(jobHandle *windows.Handle, desiredAccess uint32, objAttributes *ObjectAttributes) (status uint32) {
339 r0, _, _ := syscall.Syscall(procNtOpenJobObject.Addr(), 3, uintptr(unsafe.Pointer(jobHandle)), uintptr(desiredAccess), uintptr(unsafe.Pointer(objAttributes)))
340 status = uint32(r0)
341 return
342 }
343
344 func NtQueryDirectoryObject(handle uintptr, buffer *byte, length uint32, singleEntry bool, restartScan bool, context *uint32, returnLength *uint32) (status uint32) {
345 var _p0 uint32
346 if singleEntry {
347 _p0 = 1
348 }
349 var _p1 uint32
350 if restartScan {
351 _p1 = 1
352 }
353 r0, _, _ := syscall.Syscall9(procNtQueryDirectoryObject.Addr(), 7, uintptr(handle), uintptr(unsafe.Pointer(buffer)), uintptr(length), uintptr(_p0), uintptr(_p1), uintptr(unsafe.Pointer(context)), uintptr(unsafe.Pointer(returnLength)), 0, 0)
354 status = uint32(r0)
355 return
356 }
357
358 func NtQueryInformationProcess(processHandle windows.Handle, processInfoClass uint32, processInfo unsafe.Pointer, processInfoLength uint32, returnLength *uint32) (status uint32) {
359 r0, _, _ := syscall.Syscall6(procNtQueryInformationProcess.Addr(), 5, uintptr(processHandle), uintptr(processInfoClass), uintptr(processInfo), uintptr(processInfoLength), uintptr(unsafe.Pointer(returnLength)), 0)
360 status = uint32(r0)
361 return
362 }
363
364 func NtQuerySystemInformation(systemInfoClass int, systemInformation unsafe.Pointer, systemInfoLength uint32, returnLength *uint32) (status uint32) {
365 r0, _, _ := syscall.Syscall6(procNtQuerySystemInformation.Addr(), 4, uintptr(systemInfoClass), uintptr(systemInformation), uintptr(systemInfoLength), uintptr(unsafe.Pointer(returnLength)), 0, 0)
366 status = uint32(r0)
367 return
368 }
369
370 func NtSetInformationFile(handle uintptr, iosb *IOStatusBlock, information uintptr, length uint32, class uint32) (status uint32) {
371 r0, _, _ := syscall.Syscall6(procNtSetInformationFile.Addr(), 5, uintptr(handle), uintptr(unsafe.Pointer(iosb)), uintptr(information), uintptr(length), uintptr(class), 0)
372 status = uint32(r0)
373 return
374 }
375
376 func RtlNtStatusToDosError(status uint32) (winerr error) {
377 r0, _, _ := syscall.Syscall(procRtlNtStatusToDosError.Addr(), 1, uintptr(status), 0, 0)
378 if r0 != 0 {
379 winerr = syscall.Errno(r0)
380 }
381 return
382 }
383
384 func ORCloseHive(key syscall.Handle) (regerrno error) {
385 r0, _, _ := syscall.Syscall(procORCloseHive.Addr(), 1, uintptr(key), 0, 0)
386 if r0 != 0 {
387 regerrno = syscall.Errno(r0)
388 }
389 return
390 }
391
392 func ORCreateHive(key *syscall.Handle) (regerrno error) {
393 r0, _, _ := syscall.Syscall(procORCreateHive.Addr(), 1, uintptr(unsafe.Pointer(key)), 0, 0)
394 if r0 != 0 {
395 regerrno = syscall.Errno(r0)
396 }
397 return
398 }
399
400 func ORSaveHive(key syscall.Handle, file string, OsMajorVersion uint32, OsMinorVersion uint32) (regerrno error) {
401 var _p0 *uint16
402 _p0, regerrno = syscall.UTF16PtrFromString(file)
403 if regerrno != nil {
404 return
405 }
406 return _ORSaveHive(key, _p0, OsMajorVersion, OsMinorVersion)
407 }
408
409 func _ORSaveHive(key syscall.Handle, file *uint16, OsMajorVersion uint32, OsMinorVersion uint32) (regerrno error) {
410 r0, _, _ := syscall.Syscall6(procORSaveHive.Addr(), 4, uintptr(key), uintptr(unsafe.Pointer(file)), uintptr(OsMajorVersion), uintptr(OsMinorVersion), 0, 0)
411 if r0 != 0 {
412 regerrno = syscall.Errno(r0)
413 }
414 return
415 }
416
View as plain text