syscall.go 777 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. //go:build windows
  2. package combridge
  3. import (
  4. "unsafe"
  5. "golang.org/x/sys/windows"
  6. )
  7. var (
  8. modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
  9. procGlobalAlloc = modkernel32.NewProc("GlobalAlloc")
  10. procGlobalFree = modkernel32.NewProc("GlobalFree")
  11. uintptrSize = unsafe.Sizeof(uintptr(0))
  12. )
  13. func allocUintptrObject(size int) (uintptr, []uintptr) {
  14. v := globalAlloc(uintptr(size) * uintptrSize)
  15. slice := unsafe.Slice((*uintptr)(unsafe.Pointer(v)), size)
  16. return v, slice
  17. }
  18. func globalAlloc(dwBytes uintptr) uintptr {
  19. ret, _, _ := procGlobalAlloc.Call(uintptr(0), dwBytes)
  20. if ret == 0 {
  21. panic("globalAlloc failed")
  22. }
  23. return ret
  24. }
  25. func globalFree(data uintptr) {
  26. ret, _, _ := procGlobalFree.Call(data)
  27. if ret != 0 {
  28. panic("globalFree failed")
  29. }
  30. }