This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
forked from JamesHovious/w32
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel32.go
722 lines (607 loc) · 20.1 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// Copyright 2010-2012 The W32 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 w32
import (
"encoding/binary"
"syscall"
"unsafe"
)
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procCopyMemory = modkernel32.NewProc("RtlCopyMemory")
procCloseHandle = modkernel32.NewProc("CloseHandle")
procCreateProcessA = modkernel32.NewProc("CreateProcessA")
procCreateProcessW = modkernel32.NewProc("CreateProcessW")
procCreateRemoteThread = modkernel32.NewProc("CreateRemoteThread")
procCreateToolhelp32Snapshot = modkernel32.NewProc("CreateToolhelp32Snapshot")
procFindResource = modkernel32.NewProc("FindResourceW")
procGetConsoleScreenBufferInfo = modkernel32.NewProc("GetConsoleScreenBufferInfo")
procGetConsoleWindow = modkernel32.NewProc("GetConsoleWindow")
procGetCurrentThread = modkernel32.NewProc("GetCurrentThread")
procGetDiskFreeSpaceEx = modkernel32.NewProc("GetDiskFreeSpaceExW")
procGetExitCodeProcess = modkernel32.NewProc("GetExitCodeProcess")
procGetLastError = modkernel32.NewProc("GetLastError")
procGetLogicalDrives = modkernel32.NewProc("GetLogicalDrives")
procGetModuleHandle = modkernel32.NewProc("GetModuleHandleW")
procGetProcAddress = modkernel32.NewProc("GetProcAddress")
procGetProcessTimes = modkernel32.NewProc("GetProcessTimes")
procGetSystemTime = modkernel32.NewProc("GetSystemTime")
procGetSystemTimes = modkernel32.NewProc("GetSystemTimes")
procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
procGetUserDefaultLCID = modkernel32.NewProc("GetUserDefaultLCID")
procGlobalAlloc = modkernel32.NewProc("GlobalAlloc")
procGlobalFree = modkernel32.NewProc("GlobalFree")
procGlobalLock = modkernel32.NewProc("GlobalLock")
procGlobalUnlock = modkernel32.NewProc("GlobalUnlock")
procLoadLibraryA = modkernel32.NewProc("LoadLibraryA")
procLoadResource = modkernel32.NewProc("LoadResource")
procLockResource = modkernel32.NewProc("LockResource")
procLstrcpy = modkernel32.NewProc("lstrcpyW")
procLstrlen = modkernel32.NewProc("lstrlenW")
procProcess32First = modkernel32.NewProc("Process32FirstW")
procProcess32Next = modkernel32.NewProc("Process32NextW")
procModule32First = modkernel32.NewProc("Module32FirstW")
procModule32Next = modkernel32.NewProc("Module32NextW")
procMoveMemory = modkernel32.NewProc("RtlMoveMemory")
procMulDiv = modkernel32.NewProc("MulDiv")
procOpenProcess = modkernel32.NewProc("OpenProcess")
procQueryPerformanceCounter = modkernel32.NewProc("QueryPerformanceCounter")
procQueryPerformanceFrequency = modkernel32.NewProc("QueryPerformanceFrequency")
procReadProcessMemory = modkernel32.NewProc("ReadProcessMemory")
procSetConsoleCtrlHandler = modkernel32.NewProc("SetConsoleCtrlHandler")
procSetConsoleTextAttribute = modkernel32.NewProc("SetConsoleTextAttribute")
procSetSystemTime = modkernel32.NewProc("SetSystemTime")
procSizeofResource = modkernel32.NewProc("SizeofResource")
procTerminateProcess = modkernel32.NewProc("TerminateProcess")
procVirtualAlloc = modkernel32.NewProc("VirtualAlloc")
procVirtualAllocEx = modkernel32.NewProc("VirtualAllocEx")
procVirtualFreeEx = modkernel32.NewProc("VirtualFreeEx")
procVirtualProtect = modkernel32.NewProc("VirtualProtect")
procVirtualQuery = modkernel32.NewProc("VirtualQuery")
procVirtualQueryEx = modkernel32.NewProc("VirtualQueryEx")
procWaitForSingleObject = modkernel32.NewProc("WaitForSingleObject")
procWriteProcessMemory = modkernel32.NewProc("WriteProcessMemory")
procResumeThread = modkernel32.NewProc("ResumeThread")
procSuspendThread = modkernel32.NewProc("SuspendThread")
)
// DWORD SuspendThread(
// HANDLE hThread
// );
func SuspendThread(hThread HANDLE) (count int, e error) {
ret, _, lastErr := procSuspendThread.Call(uintptr(hThread))
if ret == 0xffffffff {
e = lastErr
return
}
return int(ret), nil
}
// DWORD ResumeThread(
// HANDLE hThread
// );
func ResumeThread(hThread HANDLE) (count int, e error) {
ret, _, lastErr := procResumeThread.Call(uintptr(hThread))
if ret == 0xffffffff {
e = lastErr
return
}
return int(ret), nil
}
func GetExitCodeProcess(hProcess HANDLE) (code uintptr, e error) {
ret, _, lastErr := procGetExitCodeProcess.Call(
uintptr(hProcess),
uintptr(unsafe.Pointer(&code)),
)
if ret == 0 {
e = lastErr
}
return
}
// DWORD WINAPI WaitForSingleObject(
// _In_ HANDLE hHandle,
// _In_ DWORD dwMilliseconds
// );
func WaitForSingleObject(hHandle HANDLE, msecs uint32) (ok bool, e error) {
ret, _, lastErr := procWaitForSingleObject.Call(
uintptr(hHandle),
uintptr(msecs),
)
if ret == WAIT_OBJECT_0 {
ok = true
return
}
// don't set e for timeouts, or it will be ERROR_SUCCESS which is
// confusing
if ret != WAIT_TIMEOUT {
e = lastErr
}
return
}
func CreateProcessW(
lpApplicationName, lpCommandLine string,
lpProcessAttributes, lpThreadAttributes *SECURITY_ATTRIBUTES,
bInheritHandles BOOL,
dwCreationFlags uint32,
lpEnvironment unsafe.Pointer,
lpCurrentDirectory string,
lpStartupInfo *STARTUPINFOW,
lpProcessInformation *PROCESS_INFORMATION,
) (e error) {
var lpAN, lpCL, lpCD *uint16
if len(lpApplicationName) > 0 {
lpAN, e = syscall.UTF16PtrFromString(lpApplicationName)
if e != nil {
return
}
}
if len(lpCommandLine) > 0 {
lpCL, e = syscall.UTF16PtrFromString(lpCommandLine)
if e != nil {
return
}
}
if len(lpCurrentDirectory) > 0 {
lpCD, e = syscall.UTF16PtrFromString(lpCurrentDirectory)
if e != nil {
return
}
}
ret, _, lastErr := procCreateProcessW.Call(
uintptr(unsafe.Pointer(lpAN)),
uintptr(unsafe.Pointer(lpCL)),
uintptr(unsafe.Pointer(lpProcessAttributes)),
uintptr(unsafe.Pointer(lpProcessInformation)),
uintptr(bInheritHandles),
uintptr(dwCreationFlags),
uintptr(lpEnvironment),
uintptr(unsafe.Pointer(lpCD)),
uintptr(unsafe.Pointer(lpStartupInfo)),
uintptr(unsafe.Pointer(lpProcessInformation)),
)
if ret == 0 {
e = lastErr
}
return
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366902(v=vs.85).aspx
func VirtualQuery(lpAddress uintptr, lpBuffer *MEMORY_BASIC_INFORMATION, dwLength int) int {
ret, _, _ := procVirtualQuery.Call(
lpAddress,
uintptr(unsafe.Pointer(lpBuffer)),
uintptr(dwLength))
return int(ret) // TODO check for errors
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366907(v=vs.85).aspx
func VirtualQueryEx(hProcess HANDLE, lpAddress uintptr, lpBuffer *MEMORY_BASIC_INFORMATION, dwLength int) int {
ret, _, _ := procVirtualQueryEx.Call(
uintptr(hProcess), // The handle to a process.
lpAddress,
uintptr(unsafe.Pointer(lpBuffer)),
uintptr(dwLength))
return int(ret) // TODO check for errors
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa366898(v=vs.85).aspx
func VirtualProtect(lpAddress uintptr, dwSize int, flNewProtect int, lpflOldProtect *DWORD) bool {
ret, _, _ := procVirtualProtect.Call(
lpAddress,
uintptr(dwSize),
uintptr(flNewProtect),
uintptr(unsafe.Pointer(lpflOldProtect)))
return ret != 0
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
func CreateProcessA(lpApplicationName string,
lpCommandLine string,
lpProcessAttributes *syscall.SecurityAttributes,
lpThreadAttributes *syscall.SecurityAttributes,
bInheritHandles bool,
dwCreationFlags uint32,
lpEnvironment string,
lpCurrentDirectory string,
lpStartupInfo *syscall.StartupInfo,
lpProcessInformation *syscall.ProcessInformation) (bool, error) {
inherit := 0
if bInheritHandles {
inherit = 1
}
ret, _, err := procCreateProcessA.Call(
uintptr(pointerStringWithoutError(lpApplicationName)),
uintptr(pointerStringWithoutError(lpCommandLine)),
uintptr(unsafe.Pointer(lpProcessAttributes)),
uintptr(unsafe.Pointer(lpThreadAttributes)),
uintptr(inherit),
uintptr(dwCreationFlags),
uintptr(pointerStringWithoutError(lpEnvironment)),
uintptr(pointerStringWithoutError(lpCurrentDirectory)),
uintptr(unsafe.Pointer(lpStartupInfo)),
uintptr(unsafe.Pointer(lpProcessInformation)))
if ret == 0 && !IsErrSuccess(err) {
return false, err
}
return ret != 0, nil
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366890(v=vs.85).aspx
func VirtualAllocEx(hProcess HANDLE, lpAddress int, dwSize int, flAllocationType int, flProtect int) (addr uintptr, err error) {
ret, _, err := procVirtualAllocEx.Call(
uintptr(hProcess), // The handle to a process.
uintptr(lpAddress), // The pointer that specifies a desired starting address for the region of pages that you want to allocate.
uintptr(dwSize), // The size of the region of memory to allocate, in bytes.
uintptr(flAllocationType),
uintptr(flProtect))
if int(ret) == 0 {
return ret, err
}
return ret, nil
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx
func VirtualAlloc(lpAddress int, dwSize int, flAllocationType int, flProtect int) (addr uintptr, err error) {
ret, _, err := procVirtualAlloc.Call(
uintptr(lpAddress), // The starting address of the region to allocate
uintptr(dwSize), // The size of the region of memory to allocate, in bytes.
uintptr(flAllocationType),
uintptr(flProtect))
if int(ret) == 0 {
return ret, err
}
return ret, nil
}
// https://github.com/AllenDang/w32/pull/62/commits/08a52ff508c6b2b9b9bf5ee476109b903dcf219d
func VirtualFreeEx(hProcess HANDLE, lpAddress, dwSize uintptr, dwFreeType uint32) bool {
ret, _, _ := procVirtualFreeEx.Call(
uintptr(hProcess),
lpAddress,
dwSize,
uintptr(dwFreeType),
)
return ret != 0
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683212(v=vs.85).aspx
func GetProcAddress(hProcess HANDLE, procname string) (addr uintptr, err error) {
var pn uintptr
if procname == "" {
pn = 0
} else {
pn = uintptr(pointerStringWithoutError(procname))
}
ret, _, err := procGetProcAddress.Call(uintptr(hProcess), pn)
if int(ret) == 0 {
return ret, err
}
return ret, nil
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682437(v=vs.85).aspx
// Credit: https://github.com/contester/runlib/blob/master/win32/win32_windows.go#L577
func CreateRemoteThread(hprocess HANDLE, sa *syscall.SecurityAttributes,
stackSize uint32, startAddress uint32, parameter uintptr, creationFlags uint32) (syscall.Handle, uint32, error) {
var threadId uint32
r1, _, e1 := procCreateRemoteThread.Call(
uintptr(hprocess),
uintptr(unsafe.Pointer(sa)),
uintptr(stackSize),
uintptr(startAddress),
uintptr(parameter),
uintptr(creationFlags),
uintptr(unsafe.Pointer(&threadId)))
if int(r1) == 0 {
return syscall.InvalidHandle, 0, e1
}
return syscall.Handle(r1), threadId, nil
}
func GetModuleHandle(modulename string) HINSTANCE {
var mn uintptr
if modulename == "" {
mn = 0
} else {
mn = uintptr(pointerStringWithoutError(modulename))
}
ret, _, _ := procGetModuleHandle.Call(mn)
return HINSTANCE(ret)
}
func MulDiv(number, numerator, denominator int) int {
ret, _, _ := procMulDiv.Call(
uintptr(number),
uintptr(numerator),
uintptr(denominator))
return int(ret)
}
func GetConsoleWindow() HWND {
ret, _, _ := procGetConsoleWindow.Call()
return HWND(ret)
}
func GetCurrentThread() HANDLE {
ret, _, _ := procGetCurrentThread.Call()
return HANDLE(ret)
}
func GetLogicalDrives() uint32 {
ret, _, _ := procGetLogicalDrives.Call()
return uint32(ret)
}
func GetUserDefaultLCID() uint32 {
ret, _, _ := procGetUserDefaultLCID.Call()
return uint32(ret)
}
func Lstrlen(lpString string) int {
ret, _, _ := procLstrlen.Call(uintptr(pointerStringWithoutError(lpString)))
return int(ret)
}
func Lstrcpy(buf []uint16, lpString string) {
procLstrcpy.Call(
uintptr(unsafe.Pointer(&buf[0])),
uintptr(pointerStringWithoutError(lpString)))
}
func GlobalAlloc(uFlags uint, dwBytes uint32) HGLOBAL {
ret, _, _ := procGlobalAlloc.Call(
uintptr(uFlags),
uintptr(dwBytes))
if ret == 0 {
panic("GlobalAlloc failed")
}
return HGLOBAL(ret)
}
func GlobalFree(hMem HGLOBAL) {
ret, _, _ := procGlobalFree.Call(uintptr(hMem))
if ret != 0 {
panic("GlobalFree failed")
}
}
func GlobalLock(hMem HGLOBAL) unsafe.Pointer {
ret, _, _ := procGlobalLock.Call(uintptr(hMem))
if ret == 0 {
panic("GlobalLock failed")
}
return unsafe.Pointer(ret)
}
func GlobalUnlock(hMem HGLOBAL) bool {
ret, _, _ := procGlobalUnlock.Call(uintptr(hMem))
return ret != 0
}
func MoveMemory(destination, source unsafe.Pointer, length uint32) {
procMoveMemory.Call(
uintptr(unsafe.Pointer(destination)),
uintptr(source),
uintptr(length))
}
func FindResource(hModule HMODULE, lpName, lpType string) (HRSRC, error) {
ret, _, _ := procFindResource.Call(
uintptr(hModule),
uintptr(pointerStringWithoutError(lpName)),
uintptr(pointerStringWithoutError(lpType)))
if ret == 0 {
return 0, syscall.GetLastError()
}
return HRSRC(ret), nil
}
func SizeofResource(hModule HMODULE, hResInfo HRSRC) uint32 {
ret, _, _ := procSizeofResource.Call(
uintptr(hModule),
uintptr(hResInfo))
if ret == 0 {
panic("SizeofResource failed")
}
return uint32(ret)
}
func LockResource(hResData HGLOBAL) unsafe.Pointer {
ret, _, _ := procLockResource.Call(uintptr(hResData))
if ret == 0 {
panic("LockResource failed")
}
return unsafe.Pointer(ret)
}
func LoadResource(hModule HMODULE, hResInfo HRSRC) HGLOBAL {
ret, _, _ := procLoadResource.Call(
uintptr(hModule),
uintptr(hResInfo))
if ret == 0 {
panic("LoadResource failed")
}
return HGLOBAL(ret)
}
func GetLastError() uint32 {
ret, _, _ := procGetLastError.Call()
return uint32(ret)
}
func OpenProcess(desiredAccess uint32, inheritHandle bool, processId uint32) (handle HANDLE, err error) {
inherit := 0
if inheritHandle {
inherit = 1
}
ret, _, err := procOpenProcess.Call(
uintptr(desiredAccess),
uintptr(inherit),
uintptr(processId))
if err != nil && IsErrSuccess(err) {
err = nil
}
handle = HANDLE(ret)
return
}
func TerminateProcess(hProcess HANDLE, uExitCode uint) bool {
ret, _, _ := procTerminateProcess.Call(
uintptr(hProcess),
uintptr(uExitCode))
return ret != 0
}
func CloseHandle(object HANDLE) bool {
ret, _, _ := procCloseHandle.Call(
uintptr(object))
return ret != 0
}
func CreateToolhelp32Snapshot(flags, processId uint32) HANDLE {
ret, _, _ := procCreateToolhelp32Snapshot.Call(
uintptr(flags),
uintptr(processId))
if ret <= 0 {
return HANDLE(0)
}
return HANDLE(ret)
}
func Process32First(snapshot HANDLE, pe *PROCESSENTRY32) bool {
if pe.Size == 0 {
pe.Size = uint32(unsafe.Sizeof(*pe))
}
ret, _, _ := procProcess32First.Call(
uintptr(snapshot),
uintptr(unsafe.Pointer(pe)))
return ret != 0
}
func Process32Next(snapshot HANDLE, pe *PROCESSENTRY32) bool {
if pe.Size == 0 {
pe.Size = uint32(unsafe.Sizeof(*pe))
}
ret, _, _ := procProcess32Next.Call(
uintptr(snapshot),
uintptr(unsafe.Pointer(pe)))
return ret != 0
}
func Module32First(snapshot HANDLE, me *MODULEENTRY32) bool {
ret, _, _ := procModule32First.Call(
uintptr(snapshot),
uintptr(unsafe.Pointer(me)))
return ret != 0
}
func Module32Next(snapshot HANDLE, me *MODULEENTRY32) bool {
ret, _, _ := procModule32Next.Call(
uintptr(snapshot),
uintptr(unsafe.Pointer(me)))
return ret != 0
}
func GetSystemTimes(lpIdleTime, lpKernelTime, lpUserTime *FILETIME) bool {
ret, _, _ := procGetSystemTimes.Call(
uintptr(unsafe.Pointer(lpIdleTime)),
uintptr(unsafe.Pointer(lpKernelTime)),
uintptr(unsafe.Pointer(lpUserTime)))
return ret != 0
}
// GetSystemInfo retrieves information about the current system.
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724381(v=vs.85).aspx
func GetSystemInfo(sysinfo *SYSTEM_INFO) {
procGetSystemInfo.Call(uintptr(unsafe.Pointer(sysinfo)))
}
func GetProcessTimes(hProcess HANDLE, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime *FILETIME) bool {
ret, _, _ := procGetProcessTimes.Call(
uintptr(hProcess),
uintptr(unsafe.Pointer(lpCreationTime)),
uintptr(unsafe.Pointer(lpExitTime)),
uintptr(unsafe.Pointer(lpKernelTime)),
uintptr(unsafe.Pointer(lpUserTime)))
return ret != 0
}
func GetConsoleScreenBufferInfo(hConsoleOutput HANDLE) *CONSOLE_SCREEN_BUFFER_INFO {
var csbi CONSOLE_SCREEN_BUFFER_INFO
ret, _, _ := procGetConsoleScreenBufferInfo.Call(
uintptr(hConsoleOutput),
uintptr(unsafe.Pointer(&csbi)))
if ret == 0 {
return nil
}
return &csbi
}
func SetConsoleTextAttribute(hConsoleOutput HANDLE, wAttributes uint16) bool {
ret, _, _ := procSetConsoleTextAttribute.Call(
uintptr(hConsoleOutput),
uintptr(wAttributes))
return ret != 0
}
func GetDiskFreeSpaceEx(dirName string) (r bool,
freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes uint64) {
ret, _, _ := procGetDiskFreeSpaceEx.Call(
uintptr(pointerStringWithoutError(dirName)),
uintptr(unsafe.Pointer(&freeBytesAvailable)),
uintptr(unsafe.Pointer(&totalNumberOfBytes)),
uintptr(unsafe.Pointer(&totalNumberOfFreeBytes)))
return ret != 0,
freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes
}
func GetSystemTime() (time SYSTEMTIME, err error) {
_, _, err = procGetSystemTime.Call(
uintptr(unsafe.Pointer(&time)))
if !IsErrSuccess(err) {
return
}
err = nil
return
}
func SetSystemTime(time *SYSTEMTIME) (err error) {
_, _, err = procSetSystemTime.Call(
uintptr(unsafe.Pointer(time)))
if !IsErrSuccess(err) {
return
}
err = nil
return
}
//Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or the operation fails.
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms681674(v=vs.85).aspx
func WriteProcessMemory(hProcess HANDLE, lpBaseAddress uint32, data []byte, size uint) (err error) {
var numBytesRead uintptr
_, _, err = procWriteProcessMemory.Call(uintptr(hProcess),
uintptr(lpBaseAddress),
uintptr(unsafe.Pointer(&data[0])),
uintptr(size),
uintptr(unsafe.Pointer(&numBytesRead)))
if !IsErrSuccess(err) {
return
}
err = nil
return
}
//Write process memory with a source of uint32
func WriteProcessMemoryAsUint32(hProcess HANDLE, lpBaseAddress uint32, data uint32) (err error) {
bData := make([]byte, 4)
binary.LittleEndian.PutUint32(bData, data)
err = WriteProcessMemory(hProcess, lpBaseAddress, bData, 4)
if err != nil {
return
}
return
}
//Reads data from an area of memory in a specified process. The entire area to be read must be accessible or the operation fails.
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms680553(v=vs.85).aspx
func ReadProcessMemory(hProcess HANDLE, lpBaseAddress uint32, size uint) (data []byte, err error) {
var numBytesRead uintptr
data = make([]byte, size)
_, _, err = procReadProcessMemory.Call(uintptr(hProcess),
uintptr(lpBaseAddress),
uintptr(unsafe.Pointer(&data[0])),
uintptr(size),
uintptr(unsafe.Pointer(&numBytesRead)))
if !IsErrSuccess(err) {
return
}
err = nil
return
}
// Read process memory and convert the returned data to uint32
func ReadProcessMemoryAsUint32(hProcess HANDLE, lpBaseAddress uint32) (buffer uint32, err error) {
data, err := ReadProcessMemory(hProcess, lpBaseAddress, 4)
if err != nil {
return
}
buffer = binary.LittleEndian.Uint32(data)
return
}
//Adds or removes an application-defined HandlerRoutine function from the list of handler functions for the calling process.
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016(v=vs.85).aspx
func SetConsoleCtrlHandler(handlerRoutine func(DWORD) int32, add uint) (err error) {
_, _, err = procSetConsoleCtrlHandler.Call(uintptr(unsafe.Pointer(&handlerRoutine)),
uintptr(add))
if !IsErrSuccess(err) {
return
}
err = nil
return
}
func QueryPerformanceCounter() uint64 {
result := uint64(0)
procQueryPerformanceCounter.Call(
uintptr(unsafe.Pointer(&result)),
)
return result
}
func QueryPerformanceFrequency() uint64 {
result := uint64(0)
procQueryPerformanceFrequency.Call(
uintptr(unsafe.Pointer(&result)),
)
return result
}