From 70bceee22b34a08005cd682a65568886c7347ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Tue, 21 Jun 2022 20:07:20 +0200 Subject: [PATCH] windows: add Global{Alloc,Free} Reference: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalalloc https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalfree --- windows/syscall_windows.go | 2 ++ windows/types_windows.go | 9 +++++++++ windows/zsyscall_windows.go | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 636e5de60..940aa103a 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -364,6 +364,8 @@ func NewCallbackCDecl(fn interface{}) uintptr { //sys SetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error) //sys GetActiveProcessorCount(groupNumber uint16) (ret uint32) //sys GetMaximumProcessorCount(groupNumber uint16) (ret uint32) +//sys GlobalFree(hmem Handle) (handle Handle, err error) [failretval!=0] +//sys GlobalAlloc(flags uint32, length uint32) (ptr uintptr, err error) // Volume Management Functions //sys DefineDosDevice(flags uint32, deviceName *uint16, targetPath *uint16) (err error) = DefineDosDeviceW diff --git a/windows/types_windows.go b/windows/types_windows.go index e19471c6a..32d248728 100644 --- a/windows/types_windows.go +++ b/windows/types_windows.go @@ -3174,3 +3174,12 @@ type ModuleInfo struct { } const ALL_PROCESSOR_GROUPS = 0xFFFF + +// Constants for GlobalAlloc flags. +const ( + GMEM_FIXED = 0x0 + GMEM_MOVEABLE = 0x2 + GMEM_ZEROINIT = 0x40 + GHND = GMEM_MOVEABLE | GMEM_ZEROINIT + GPTR = GMEM_FIXED | GMEM_ZEROINIT +) diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index 68f52c1e6..d8dbd684c 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -286,6 +286,8 @@ var ( procGetVolumePathNameW = modkernel32.NewProc("GetVolumePathNameW") procGetVolumePathNamesForVolumeNameW = modkernel32.NewProc("GetVolumePathNamesForVolumeNameW") procGetWindowsDirectoryW = modkernel32.NewProc("GetWindowsDirectoryW") + procGlobalAlloc = modkernel32.NewProc("GlobalAlloc") + procGlobalFree = modkernel32.NewProc("GlobalFree") procInitializeProcThreadAttributeList = modkernel32.NewProc("InitializeProcThreadAttributeList") procIsWow64Process = modkernel32.NewProc("IsWow64Process") procIsWow64Process2 = modkernel32.NewProc("IsWow64Process2") @@ -2461,6 +2463,24 @@ func getWindowsDirectory(dir *uint16, dirLen uint32) (len uint32, err error) { return } +func GlobalAlloc(flags uint32, length uint32) (ptr uintptr, err error) { + r0, _, e1 := syscall.Syscall(procGlobalAlloc.Addr(), 2, uintptr(flags), uintptr(length), 0) + ptr = uintptr(r0) + if ptr == 0 { + err = errnoErr(e1) + } + return +} + +func GlobalFree(hmem Handle) (handle Handle, err error) { + r0, _, e1 := syscall.Syscall(procGlobalFree.Addr(), 1, uintptr(hmem), 0, 0) + handle = Handle(r0) + if handle != 0 { + err = errnoErr(e1) + } + return +} + func initializeProcThreadAttributeList(attrlist *ProcThreadAttributeList, attrcount uint32, flags uint32, size *uintptr) (err error) { r1, _, e1 := syscall.Syscall6(procInitializeProcThreadAttributeList.Addr(), 4, uintptr(unsafe.Pointer(attrlist)), uintptr(attrcount), uintptr(flags), uintptr(unsafe.Pointer(size)), 0, 0) if r1 == 0 {