-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathany_funcs.go
1099 lines (958 loc) Β· 35.3 KB
/
any_funcs.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package wingo
import (
proc2 "github.com/rogeecn/wingo/proc"
"github.com/rogeecn/wingo/util"
"runtime"
"strings"
"syscall"
"time"
"unsafe"
"github.com/rogeecn/wingo/co"
"github.com/rogeecn/wingo/errco"
)
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-adjustwindowrectex
func AdjustWindowRectEx(rc *RECT, style co.WS, hasMenu bool, exStyle co.WS_EX) {
ret, _, err := syscall.Syscall6(proc2.AdjustWindowRectEx.Addr(), 4,
uintptr(unsafe.Pointer(rc)), uintptr(style),
util.BoolToUintptr(hasMenu), uintptr(exStyle), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-allowsetforegroundwindow
func AllowSetForegroundWindow(processId uint32) {
ret, _, err := syscall.Syscall(proc2.AllowSetForegroundWindow.Addr(), 1,
uintptr(processId), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-broadcastsystemmessagew
func BroadcastSystemMessage(
flags co.BSF,
recipients co.BSM,
msg co.WM,
wParam WPARAM,
lParam LPARAM) (broadcastSuccessful bool, receivers co.BSM, e error) {
receivers = recipients
ret, _, err := syscall.Syscall6(proc2.BroadcastSystemMessage.Addr(), 5,
uintptr(flags), uintptr(unsafe.Pointer(&receivers)),
uintptr(msg), uintptr(wParam), uintptr(lParam), 0)
broadcastSuccessful = int(ret) > 1
if ret == 0 {
e = errco.ERROR(err)
}
return
}
// π https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms646912(v=vs.85)
func ChooseColor(cc *CHOOSECOLOR) bool {
ret, _, _ := syscall.Syscall(proc2.ChooseColor.Addr(), 1,
uintptr(unsafe.Pointer(cc)), 0, 0)
if ret == 0 {
dlgErr := CommDlgExtendedError()
if dlgErr == errco.CDERR_OK {
return false
} else {
panic(dlgErr)
}
}
return true
}
// Loads the COM module. This needs to be done only once in your application.
// Typically uses COINIT_APARTMENTTHREADED.
//
// β οΈ You must defer CoUninitialize().
//
// π https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex
func CoInitializeEx(coInit co.COINIT) {
ret, _, _ := syscall.Syscall(proc2.CoInitializeEx.Addr(), 2,
0, uintptr(coInit), 0)
if hr := errco.ERROR(ret); hr != errco.S_OK && hr != errco.S_FALSE {
panic(hr)
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/commdlg/nf-commdlg-commdlgextendederror
func CommDlgExtendedError() errco.CDERR {
ret, _, _ := syscall.Syscall(proc2.CommDlgExtendedError.Addr(), 0,
0, 0, 0)
return errco.CDERR(ret)
}
// Typically used with GetCommandLine().
//
// π https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw
func CommandLineToArgv(cmdLine string) []string {
var pNumArgs int32
ret, _, err := syscall.Syscall(proc2.CommandLineToArgv.Addr(), 2,
uintptr(unsafe.Pointer(Str.ToNativePtr(cmdLine))),
uintptr(unsafe.Pointer(&pNumArgs)), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
lpPtrs := unsafe.Slice((**uint16)(unsafe.Pointer(ret)), pNumArgs) // []*uint16
strs := make([]string, 0, pNumArgs)
for _, lpPtr := range lpPtrs {
strs = append(strs, Str.FromNativePtr(lpPtr))
}
return strs
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfilew
func CopyFile(existingFile, newFile string, failIfExists bool) error {
ret, _, err := syscall.Syscall(proc2.CopyFile.Addr(), 3,
uintptr(unsafe.Pointer(Str.ToNativePtr(existingFile))),
uintptr(unsafe.Pointer(Str.ToNativePtr(newFile))),
util.BoolToUintptr(failIfExists))
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// β οΈ You must defer CoTaskMemFree().
//
// π https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemalloc
func CoTaskMemAlloc(size int) uintptr {
ret, _, _ := syscall.Syscall(proc2.CoTaskMemAlloc.Addr(), 1,
uintptr(size), 0, 0)
if ret == 0 {
panic("CoTaskMemAlloc() failed.")
}
return ret
}
// π https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemfree
func CoTaskMemFree(pv uintptr) {
syscall.Syscall(proc2.CoTaskMemFree.Addr(), 1,
pv, 0, 0)
}
// β οΈ You must defer CoTaskMemFree().
//
// π https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemrealloc
func CoTaskMemRealloc(pv uintptr, size int) uintptr {
ret, _, _ := syscall.Syscall(proc2.CoTaskMemRealloc.Addr(), 2,
pv, uintptr(size), 0)
if ret == 0 {
panic("CoTaskMemRealloc() failed.")
}
return ret
}
// π https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize
func CoUninitialize() {
syscall.Syscall(proc2.CoUninitialize.Addr(), 0, 0, 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createdirectoryw
func CreateDirectory(
pathName string, securityAttributes *SECURITY_ATTRIBUTES) error {
ret, _, err := syscall.Syscall(proc2.CreateDirectory.Addr(), 2,
uintptr(unsafe.Pointer(Str.ToNativePtr(pathName))),
uintptr(unsafe.Pointer(securityAttributes)), 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// β οΈ You must defer HPROCESS.CloseHandle() and HTHREAD.CloseHandle() on
// HProcess and HThread members of PROCESS_INFORMATION.
//
// π https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw
func CreateProcess(
applicationName, commandLine StrOrNil,
processAttributes, threadAttributes *SECURITY_ATTRIBUTES,
inheritHandles bool,
creationFlags co.CREATE,
ptrEnvironment uintptr,
currentDirectory StrOrNil,
startupInfo *STARTUPINFO,
processInformation *PROCESS_INFORMATION) {
ret, _, err := syscall.Syscall12(proc2.CreateProcess.Addr(), 10,
uintptr(variantStrOrNil(applicationName)),
uintptr(variantStrOrNil(commandLine)),
uintptr(unsafe.Pointer(processAttributes)),
uintptr(unsafe.Pointer(threadAttributes)),
util.BoolToUintptr(inheritHandles),
uintptr(creationFlags),
ptrEnvironment,
uintptr(variantStrOrNil(currentDirectory)),
uintptr(unsafe.Pointer(startupInfo)),
uintptr(unsafe.Pointer(processInformation)),
0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-deletefilew
func DeleteFile(fileName string) error {
ret, _, err := syscall.Syscall(proc2.DeleteFile.Addr(), 1,
uintptr(unsafe.Pointer(Str.ToNativePtr(fileName))), 0, 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroycaret
func DestroyCaret() {
ret, _, err := syscall.Syscall(proc2.DestroyCaret.Addr(), 0,
0, 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dispatchmessage
func DispatchMessage(msg *MSG) uintptr {
ret, _, _ := syscall.Syscall(proc2.DispatchMessage.Addr(), 1,
uintptr(unsafe.Pointer(msg)), 0, 0)
return ret
}
// π https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmgetcolorizationcolor
func DwmGetColorizationColor() (color COLORREF, isOpaqueBlend bool) {
bOpaqueBlend := int32(util.BoolToUintptr(isOpaqueBlend)) // BOOL
ret, _, _ := syscall.Syscall(proc2.DwmGetColorizationColor.Addr(), 2,
uintptr(unsafe.Pointer(&color)), uintptr(unsafe.Pointer(&bOpaqueBlend)),
0)
if hr := errco.ERROR(ret); hr != errco.S_OK {
panic(hr)
}
isOpaqueBlend = bOpaqueBlend != 0
return
}
// π https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmiscompositionenabled
func DwmIsCompositionEnabled() bool {
var pfEnabled int32 // BOOL
ret, _, _ := syscall.Syscall(proc2.DwmIsCompositionEnabled.Addr(), 1,
uintptr(unsafe.Pointer(&pfEnabled)), 0, 0)
if hr := errco.ERROR(ret); hr != errco.S_OK {
panic(hr)
}
return pfEnabled != 0
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-endmenu
func EndMenu() {
ret, _, err := syscall.Syscall(proc2.EndMenu.Addr(), 0,
0, 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows
func EnumWindows(enumFunc func(hWnd HWND) bool) {
pPack := &_EnumWindowsPack{f: enumFunc}
if _globalEnumWindowsFuncs == nil {
_globalEnumWindowsFuncs = make(map[*_EnumWindowsPack]struct{}, 2)
}
_globalEnumWindowsFuncs[pPack] = struct{}{} // store pointer in the set
ret, _, err := syscall.Syscall(proc2.EnumWindows.Addr(), 2,
_globalEnumWindowsCallback, uintptr(unsafe.Pointer(pPack)), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
type _EnumWindowsPack struct{ f func(hWnd HWND) bool }
var (
_globalEnumWindowsCallback uintptr = syscall.NewCallback(_EnumWindowsProc)
_globalEnumWindowsFuncs map[*_EnumWindowsPack]struct{}
)
func _EnumWindowsProc(hWnd HWND, lParam LPARAM) uintptr {
pPack := (*_EnumWindowsPack)(unsafe.Pointer(lParam))
retVal := uintptr(0)
if _, isStored := _globalEnumWindowsFuncs[pPack]; isStored {
retVal = util.BoolToUintptr(pPack.f(hWnd))
if retVal == 0 {
delete(_globalEnumWindowsFuncs, pPack) // remove from set
}
}
return retVal
}
// π https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-exitprocess
func ExitProcess(exitCode uint32) {
syscall.Syscall(proc2.ExitProcess.Addr(), 1,
uintptr(exitCode), 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsw
func ExpandEnvironmentStrings(src string) string {
pSrc := Str.ToNativePtr(src)
ret, _, _ := syscall.Syscall(proc2.ExpandEnvironmentStrings.Addr(), 3,
uintptr(unsafe.Pointer(pSrc)), 0, 0)
buf := make([]uint16, ret)
ret, _, err := syscall.Syscall(proc2.ExpandEnvironmentStrings.Addr(), 3,
uintptr(unsafe.Pointer(pSrc)),
uintptr(unsafe.Pointer(&buf[0])), ret)
runtime.KeepAlive(pSrc)
if ret == 0 {
panic(errco.ERROR(err))
}
return Str.FromNativeSlice(buf)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-filetimetosystemtime
func FileTimeToSystemTime(inFileTime *FILETIME, outSystemTime *SYSTEMTIME) {
ret, _, err := syscall.Syscall(proc2.FileTimeToSystemTime.Addr(), 2,
uintptr(unsafe.Pointer(inFileTime)),
uintptr(unsafe.Pointer(outSystemTime)), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-gdiflush
func GdiFlush() bool {
ret, _, _ := syscall.Syscall(proc2.GdiFlush.Addr(), 0,
0, 0, 0)
return ret == 0
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate
func GetAsyncKeyState(virtKeyCode co.VK) uint16 {
ret, _, _ := syscall.Syscall(proc2.GetAsyncKeyState.Addr(), 1,
uintptr(virtKeyCode), 0, 0)
return uint16(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcaretpos
func GetCaretPos() RECT {
var rc RECT
ret, _, err := syscall.Syscall(proc2.GetCaretPos.Addr(), 1,
uintptr(unsafe.Pointer(&rc)), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return rc
}
// π https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinew
func GetCommandLine() string {
ret, _, _ := syscall.Syscall(proc2.GetCommandLine.Addr(), 0,
0, 0, 0)
return Str.FromNativePtr((*uint16)(unsafe.Pointer(ret)))
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory
func GetCurrentDirectory() string {
var buf [_MAX_PATH + 1]uint16
ret, _, err := syscall.Syscall(proc2.GetCurrentDirectory.Addr(), 2,
uintptr(len(buf)), uintptr(unsafe.Pointer(&buf[0])), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return Str.FromNativeSlice(buf[:])
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getcursorpos
func GetCursorPos() POINT {
var pt POINT
ret, _, err := syscall.Syscall(proc2.GetCursorPos.Addr(), 1,
uintptr(unsafe.Pointer(&pt)), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return pt
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdialogbaseunits
func GetDialogBaseUnits() (horz, vert uint16) {
ret, _, _ := syscall.Syscall(proc2.GetDialogBaseUnits.Addr(), 0,
0, 0, 0)
horz, vert = LOWORD(uint32(ret)), HIWORD(uint32(ret))
return
}
// π https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-getdynamictimezoneinformation
func GetDynamicTimeZoneInformation(
timeZoneInfo *DYNAMIC_TIME_ZONE_INFORMATION) co.TIME_ZONE_ID {
ret, _, _ := syscall.Syscall(proc2.GetDynamicTimeZoneInformation.Addr(), 1,
uintptr(unsafe.Pointer(timeZoneInfo)), 0, 0)
return co.TIME_ZONE_ID(ret)
}
// You don't need to call FreeEnvironmentStrings(), it's automatically called.
//
// π https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentstringsw
func GetEnvironmentStrings() map[string]string {
ret, _, err := syscall.Syscall(proc2.GetEnvironmentStrings.Addr(), 0,
0, 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
rawEntries := Str.FromNativePtrMulti((*uint16)(unsafe.Pointer(ret)))
ret, _, err = syscall.Syscall(proc2.FreeEnvironmentStrings.Addr(), 1,
ret, 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
mapEntries := make(map[string]string, len(rawEntries))
for _, entry := range rawEntries {
keyVal := strings.SplitN(entry, "=", 2)
mapEntries[keyVal[0]] = keyVal[1]
}
return mapEntries
}
// π https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileattributesw
func GetFileAttributes(fileName string) (co.FILE_ATTRIBUTE, error) {
ret, _, err := syscall.Syscall(proc2.GetFileAttributes.Addr(), 1,
uintptr(unsafe.Pointer(Str.ToNativePtr(fileName))), 0, 0)
if retAttr := co.FILE_ATTRIBUTE(ret); retAttr == co.FILE_ATTRIBUTE_INVALID {
return retAttr, errco.ERROR(err) // err is extended error information
} else {
return retAttr, nil
}
}
// Automatically allocs the buffer with GetFileVersionInfoSize().
//
// π https://docs.microsoft.com/en-us/windows/win32/api/winver/nf-winver-getfileversioninfow
func GetFileVersionInfo(fileName string) ([]byte, error) {
visz, errSz := GetFileVersionInfoSize(fileName)
if errSz != nil {
return nil, errSz
}
buf := make([]byte, visz) // alloc the buffer
ret, _, err := syscall.Syscall6(proc2.GetFileVersionInfo.Addr(), 4,
uintptr(unsafe.Pointer(Str.ToNativePtr(fileName))),
0, uintptr(visz), uintptr(unsafe.Pointer(&buf[0])), 0, 0)
if ret == 0 {
return nil, errco.ERROR(err)
}
return buf, nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winver/nf-winver-getfileversioninfosizew
func GetFileVersionInfoSize(fileName string) (uint32, error) {
var lpdwHandle uint32
ret, _, err := syscall.Syscall(proc2.GetFileVersionInfoSize.Addr(), 2,
uintptr(unsafe.Pointer(Str.ToNativePtr(fileName))),
uintptr(unsafe.Pointer(&lpdwHandle)), 0)
if ret == 0 {
return 0, errco.ERROR(err)
}
return uint32(ret), nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessagew
func GetMessage(
msg *MSG, hWnd HWND, msgFilterMin, msgFilterMax uint32) (int32, error) {
ret, _, err := syscall.Syscall6(proc2.GetMessage.Addr(), 4,
uintptr(unsafe.Pointer(msg)), uintptr(hWnd),
uintptr(msgFilterMin), uintptr(msgFilterMax),
0, 0)
if int(ret) == -1 {
return 0, errco.ERROR(err)
}
return int32(ret), nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessagepos
func GetMessagePos() POINT {
ret, _, _ := syscall.Syscall(proc2.GetMessagePos.Addr(), 0,
0, 0, 0)
return POINT{
X: int32(LOWORD(uint32(ret))),
Y: int32(HIWORD(uint32(ret))),
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessagetime
func GetMessageTime() time.Duration {
ret, _, _ := syscall.Syscall(proc2.GetMessageTime.Addr(), 0,
0, 0, 0)
return time.Duration(ret * uintptr(time.Millisecond))
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getphysicalcursorpos
func GetPhysicalCursorPos() POINT {
var pt POINT
ret, _, err := syscall.Syscall(proc2.GetPhysicalCursorPos.Addr(), 1,
uintptr(unsafe.Pointer(&pt)), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return pt
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getqueuestatus
func GetQueueStatus(flags co.QS) uint32 {
ret, _, _ := syscall.Syscall(proc2.GetQueueStatus.Addr(), 1,
uintptr(flags), 0, 0)
return uint32(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getstartupinfow
func GetStartupInfo(startupInfo *STARTUPINFO) {
syscall.Syscall(proc2.GetStartupInfo.Addr(), 1,
uintptr(unsafe.Pointer(startupInfo)), 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsyscolor
func GetSysColor(index co.COLOR) COLORREF {
ret, _, _ := syscall.Syscall(proc2.GetSysColor.Addr(), 1,
uintptr(index), 0, 0)
return COLORREF(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
func GetSystemInfo(systemInfo *SYSTEM_INFO) {
syscall.Syscall(proc2.GetSystemInfo.Addr(), 1,
uintptr(unsafe.Pointer(systemInfo)), 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics
func GetSystemMetrics(index co.SM) int32 {
ret, _, _ := syscall.Syscall(proc2.GetSystemMetrics.Addr(), 1,
uintptr(index), 0, 0)
return int32(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtime
func GetSystemTime(systemTime *SYSTEMTIME) {
syscall.Syscall(proc2.GetSystemTime.Addr(), 1,
uintptr(unsafe.Pointer(systemTime)), 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getsystemtimes
func GetSystemTimes(idleTime, kernelTime, userTime *FILETIME) {
ret, _, err := syscall.Syscall(proc2.GetSystemTimes.Addr(), 3,
uintptr(unsafe.Pointer(idleTime)), uintptr(unsafe.Pointer(kernelTime)),
uintptr(unsafe.Pointer(userTime)))
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime
func GetSystemTimeAsFileTime() FILETIME {
var ft FILETIME
syscall.Syscall(proc2.GetSystemTimeAsFileTime.Addr(), 1,
uintptr(unsafe.Pointer(&ft)), 0, 0)
return ft
}
// π https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
func GetSystemTimePreciseAsFileTime() FILETIME {
var ft FILETIME
syscall.Syscall(proc2.GetSystemTimePreciseAsFileTime.Addr(), 1,
uintptr(unsafe.Pointer(&ft)), 0, 0)
return ft
}
// π https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount64
func GetTickCount64() uint64 {
ret, _, _ := syscall.Syscall(proc2.GetTickCount64.Addr(), 0,
0, 0, 0)
return uint64(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-gettimezoneinformation
func GetTimeZoneInformation(
timeZoneInfo *TIME_ZONE_INFORMATION) co.TIME_ZONE_ID {
ret, _, _ := syscall.Syscall(proc2.GetTimeZoneInformation.Addr(), 1,
uintptr(unsafe.Pointer(timeZoneInfo)), 0, 0)
return co.TIME_ZONE_ID(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-gettimezoneinformationforyear
func GetTimeZoneInformationForYear(
wYear uint16,
dtzi *DYNAMIC_TIME_ZONE_INFORMATION, tzi *TIME_ZONE_INFORMATION) {
ret, _, err := syscall.Syscall(proc2.GetTimeZoneInformationForYear.Addr(), 3,
uintptr(wYear),
uintptr(unsafe.Pointer(dtzi)), uintptr(unsafe.Pointer(tzi)))
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getwindowsdirectoryw
func GetWindowsDirectory() string {
var buf [_MAX_PATH + 1]uint16
ret, _, err := syscall.Syscall(proc2.GetWindowsDirectory.Addr(), 2,
uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return Str.FromNativeSlice(buf[:])
}
// π https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms632656(v=vs.85)
func HIBYTE(val uint16) uint8 {
_, hi := util.Break16(val)
return hi
}
// π https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms632657(v=vs.85)
func HIWORD(val uint32) uint16 {
_, hi := util.Break32(val)
return hi
}
// π https://docs.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-initcommoncontrols
func InitCommonControls() {
syscall.Syscall(proc2.InitCommonControls.Addr(), 0, 0, 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/uxtheme/nf-uxtheme-isappthemed
func IsAppThemed() bool {
ret, _, _ := syscall.Syscall(proc2.IsAppThemed.Addr(), 0,
0, 0, 0)
return ret != 0
}
// π https://docs.microsoft.com/en-us/windows/win32/api/uxtheme/nf-uxtheme-iscompositionactive
func IsCompositionActive() bool {
ret, _, _ := syscall.Syscall(proc2.IsCompositionActive.Addr(), 0,
0, 0, 0)
return ret != 0
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-isguithread
func IsGUIThread(convertToGuiThread bool) (bool, error) {
ret, _, _ := syscall.Syscall(proc2.IsGUIThread.Addr(), 1,
util.BoolToUintptr(convertToGuiThread), 0, 0)
if convertToGuiThread && errco.ERROR(ret) == errco.NOT_ENOUGH_MEMORY {
return false, errco.NOT_ENOUGH_MEMORY
}
return ret != 0, nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/uxtheme/nf-uxtheme-isthemeactive
func IsThemeActive() bool {
ret, _, _ := syscall.Syscall(proc2.IsThemeActive.Addr(), 0,
0, 0, 0)
return ret != 0
}
// π https://docs.microsoft.com/en-us/windows/win32/api/versionhelpers/nf-versionhelpers-iswindows10orgreater
func IsWindows10OrGreater() bool {
return IsWindowsVersionOrGreater(
uint32(HIBYTE(uint16(co.WIN32_WINNT_WINTHRESHOLD))),
uint32(LOBYTE(uint16(co.WIN32_WINNT_WINTHRESHOLD))),
0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/versionhelpers/nf-versionhelpers-iswindows7orgreater
func IsWindows7OrGreater() bool {
return IsWindowsVersionOrGreater(
uint32(HIBYTE(uint16(co.WIN32_WINNT_WIN7))),
uint32(LOBYTE(uint16(co.WIN32_WINNT_WIN7))),
0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/versionhelpers/nf-versionhelpers-iswindows8orgreater
func IsWindows8OrGreater() bool {
return IsWindowsVersionOrGreater(
uint32(HIBYTE(uint16(co.WIN32_WINNT_WIN8))),
uint32(LOBYTE(uint16(co.WIN32_WINNT_WIN8))),
0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/versionhelpers/nf-versionhelpers-iswindows8point1orgreater
func IsWindows8Point1OrGreater() bool {
return IsWindowsVersionOrGreater(
uint32(HIBYTE(uint16(co.WIN32_WINNT_WINBLUE))),
uint32(LOBYTE(uint16(co.WIN32_WINNT_WINBLUE))),
0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/versionhelpers/nf-versionhelpers-iswindowsvistaorgreater
func IsWindowsVistaOrGreater() bool {
return IsWindowsVersionOrGreater(
uint32(HIBYTE(uint16(co.WIN32_WINNT_VISTA))),
uint32(LOBYTE(uint16(co.WIN32_WINNT_VISTA))),
0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/versionhelpers/nf-versionhelpers-iswindowsxporgreater
func IsWindowsXpOrGreater() bool {
return IsWindowsVersionOrGreater(
uint32(HIBYTE(uint16(co.WIN32_WINNT_WINXP))),
uint32(LOBYTE(uint16(co.WIN32_WINNT_WINXP))),
0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/versionhelpers/nf-versionhelpers-iswindowsversionorgreater
func IsWindowsVersionOrGreater(
majorVersion, minorVersion uint32, servicePackMajor uint16) bool {
ovi := OSVERSIONINFOEX{
DwMajorVersion: majorVersion,
DwMinorVersion: minorVersion,
WServicePackMajor: servicePackMajor,
}
ovi.SetDwOsVersionInfoSize()
conditionMask := VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(0, co.VER_MAJORVERSION, co.VER_COND_GREATER_EQUAL),
co.VER_MINORVERSION, co.VER_COND_GREATER_EQUAL),
co.VER_SERVICEPACKMAJOR, co.VER_COND_GREATER_EQUAL)
ret, err := VerifyVersionInfo(&ovi,
co.VER_MAJORVERSION|co.VER_MINORVERSION|co.VER_SERVICEPACKMAJOR,
conditionMask)
if err != nil {
panic(err)
}
return ret
}
// π https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms632658(v=vs.85)
func LOBYTE(val uint16) uint8 {
lo, _ := util.Break16(val)
return lo
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-locksetforegroundwindow
func LockSetForegroundWindow(lockCode co.LSFW) {
ret, _, err := syscall.Syscall(proc2.LockSetForegroundWindow.Addr(), 1,
uintptr(lockCode), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms632659(v=vs.85)
func LOWORD(val uint32) uint16 {
lo, _ := util.Break32(val)
return lo
}
// π https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms632660(v=vs.85)
func MAKELONG(lo, hi uint16) uint32 {
return util.Make32(lo, hi)
}
// π https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms632663(v=vs.85)
func MAKEWORD(lo, hi uint8) uint16 {
return util.Make16(lo, hi)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefilew
func MoveFile(existingFile, newFile string) error {
ret, _, err := syscall.Syscall(proc2.MoveFile.Addr(), 2,
uintptr(unsafe.Pointer(Str.ToNativePtr(existingFile))),
uintptr(unsafe.Pointer(Str.ToNativePtr(newFile))),
0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefileexw
func MoveFileEx(existingFile, newFile string, flags co.MOVEFILE) error {
ret, _, err := syscall.Syscall(proc2.MoveFile.Addr(), 2,
uintptr(unsafe.Pointer(Str.ToNativePtr(existingFile))),
uintptr(unsafe.Pointer(Str.ToNativePtr(newFile))),
uintptr(flags))
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// Note: You'll achieve a much better performance with ordinary Go code:
//
// res := int32((int64(n) * int64(num)) / int64(den))
//
// π https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-muldiv
func MulDiv(number, numerator, denominator int32) int32 {
ret, _, _ := syscall.Syscall(proc2.MulDiv.Addr(), 3,
uintptr(number), uintptr(numerator), uintptr(denominator))
return int32(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-peekmessagew
func PeekMessage(
msg *MSG, hWnd HWND,
msgFilterMin, msgFilterMax co.WM, removeMsg co.PM) bool {
ret, _, _ := syscall.Syscall6(proc2.PeekMessage.Addr(), 5,
uintptr(unsafe.Pointer(msg)), uintptr(hWnd),
uintptr(msgFilterMin), uintptr(msgFilterMax), uintptr(removeMsg), 0)
return ret != 0
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postquitmessage
func PostQuitMessage(exitCode int32) {
syscall.Syscall(proc2.PostQuitMessage.Addr(), 1,
uintptr(exitCode), 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postthreadmessagew
func PostThreadMessage(
idThread uint32, msg co.WM, wParam WPARAM, lParam LPARAM) error {
ret, _, err := syscall.Syscall6(proc2.PostThreadMessage.Addr(), 4,
uintptr(idThread), uintptr(msg), uintptr(wParam), uintptr(lParam),
0, 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
func QueryPerformanceCounter() int64 {
var lpPerformanceCount int64
ret, _, err := syscall.Syscall(proc2.QueryPerformanceCounter.Addr(), 1,
uintptr(unsafe.Pointer(&lpPerformanceCount)), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return lpPerformanceCount
}
// π https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency
func QueryPerformanceFrequency() int64 {
var lpFrequency int64
ret, _, err := syscall.Syscall(proc2.QueryPerformanceFrequency.Addr(), 1,
uintptr(unsafe.Pointer(&lpFrequency)), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return lpFrequency
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerclassexw
func RegisterClassEx(wcx *WNDCLASSEX) (ATOM, error) {
wcx.SetCbSize() // safety
ret, _, err := syscall.Syscall(proc2.RegisterClassEx.Addr(), 1,
uintptr(unsafe.Pointer(wcx)), 0, 0)
if ret == 0 {
return ATOM(0), errco.ERROR(err)
}
return ATOM(ret), nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectoryw
func RemoveDirectory(pathName string) error {
ret, _, err := syscall.Syscall(proc2.RemoveDirectory.Addr(), 1,
uintptr(unsafe.Pointer(Str.ToNativePtr(pathName))), 0, 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-replacefilew
func ReplaceFile(
replaced, replacement string,
backup StrOrNil, replaceFlags co.REPLACEFILE) error {
ret, _, err := syscall.Syscall6(proc2.ReplaceFile.Addr(), 6,
uintptr(unsafe.Pointer(Str.ToNativePtr(replaced))),
uintptr(unsafe.Pointer(Str.ToNativePtr(replacement))),
uintptr(variantStrOrNil(backup)), uintptr(replaceFlags), 0, 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setcurrentdirectory
func SetCurrentDirectory(pathName string) error {
ret, _, err := syscall.Syscall(proc2.SetCurrentDirectory.Addr(), 1,
uintptr(unsafe.Pointer(Str.ToNativePtr(pathName))), 0, 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesw
func SetFileAttributes(fileName string, attrs co.FILE_ATTRIBUTE) error {
ret, _, err := syscall.Syscall(proc2.SetFileAttributes.Addr(), 2,
uintptr(unsafe.Pointer(Str.ToNativePtr(fileName))), uintptr(attrs), 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// Available in Windows Vista.
//
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiaware
func SetProcessDPIAware() {
ret, _, _ := syscall.Syscall(proc2.SetProcessDPIAware.Addr(), 0,
0, 0, 0)
if ret == 0 {
panic("SetProcessDPIAware() failed.")
}
}
// Available in Windows 10, version 1703.
//
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiawarenesscontext
func SetProcessDpiAwarenessContext(value co.DPI_AWARE_CTX) error {
ret, _, err := syscall.Syscall(proc2.SetProcessDpiAwarenessContext.Addr(), 1,
uintptr(value), 0, 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// π https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shell_notifyiconw
func ShellNotifyIcon(message co.NIM, data *NOTIFYICONDATA) error {
ret, _, err := syscall.Syscall(proc2.Shell_NotifyIcon.Addr(), 2,
uintptr(message), uintptr(unsafe.Pointer(data)), 0)
if ret == 0 {
return errco.ERROR(err)
}
return nil
}
// Depends of CoInitializeEx().
//
// π https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shgetfileinfow
func SHGetFileInfo(
path string, fileAttributes co.FILE_ATTRIBUTE,
sfi *SHFILEINFO, flags co.SHGFI) {
ret, _, err := syscall.Syscall6(proc2.SHGetFileInfo.Addr(), 5,
uintptr(unsafe.Pointer(Str.ToNativePtr(path))),
uintptr(fileAttributes), uintptr(unsafe.Pointer(sfi)),
unsafe.Sizeof(*sfi), uintptr(flags), 0)
if (flags&co.SHGFI_EXETYPE) == 0 || (flags&co.SHGFI_SYSICONINDEX) == 0 {
if ret == 0 {
panic(errco.ERROR(err))
}
}
if (flags & co.SHGFI_EXETYPE) != 0 {
if ret == 0 {
panic(errco.ERROR(err))
}
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
func Sleep(milliseconds uint32) {
syscall.Syscall(proc2.Sleep.Addr(), 1,
uintptr(milliseconds), 0, 0)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow
func SystemParametersInfo(
uiAction co.SPI, uiParam uint32, pvParam unsafe.Pointer, fWinIni co.SPIF) {
ret, _, err := syscall.Syscall6(proc2.SystemParametersInfo.Addr(), 4,
uintptr(uiAction), uintptr(uiParam), uintptr(pvParam), uintptr(fWinIni),
0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetofiletime
func SystemTimeToFileTime(inSystemTime *SYSTEMTIME, outFileTime *FILETIME) {
ret, _, err := syscall.Syscall(proc2.SystemTimeToFileTime.Addr(), 2,
uintptr(unsafe.Pointer(inSystemTime)),
uintptr(unsafe.Pointer(outFileTime)), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetotzspecificlocaltime