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