diff --git a/.gitignore b/.gitignore index 896600c..723ef36 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1 @@ -.idea/workspace.xml -.idea/libraries/Go_SDK.xml -.idea/vcs.xml -.idea/libraries/GOPATH__w32_.xml -.idea/w32.iml -.idea/modules.xml \ No newline at end of file +.idea \ No newline at end of file diff --git a/advapi32.go b/advapi32.go index a1e5612..0650db8 100644 --- a/advapi32.go +++ b/advapi32.go @@ -73,7 +73,7 @@ func ControlTrace(hTrace TRACEHANDLE, lpSessionName string, props *EVENT_TRACE_P ret, _, _ := procControlTrace.Call( uintptr(unsafe.Pointer(hTrace)), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpSessionName))), + uintptr(pointerStringWithoutError(lpSessionName)), uintptr(unsafe.Pointer(props)), uintptr(dwControl)) @@ -88,7 +88,7 @@ func StartTrace(lpSessionName string, props *EVENT_TRACE_PROPERTIES) (hTrace TRA ret, _, _ := procStartTrace.Call( uintptr(unsafe.Pointer(&hTrace)), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpSessionName))), + uintptr(pointerStringWithoutError(lpSessionName)), uintptr(unsafe.Pointer(props))) if ret == ERROR_SUCCESS { @@ -119,7 +119,7 @@ func RegCreateKey(hKey HKEY, subKey string) HKEY { var result HKEY ret, _, _ := procRegCreateKeyEx.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), + uintptr(pointerStringWithoutError(subKey)), uintptr(0), uintptr(0), uintptr(0), @@ -135,7 +135,7 @@ func RegOpenKeyEx(hKey HKEY, subKey string, samDesired uint32) HKEY { var result HKEY ret, _, _ := procRegOpenKeyEx.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), + uintptr(pointerStringWithoutError(subKey)), uintptr(0), uintptr(samDesired), uintptr(unsafe.Pointer(&result))) @@ -161,11 +161,11 @@ func RegGetRaw(hKey HKEY, subKey string, value string) []byte { var bufLen uint32 var valptr unsafe.Pointer if len(value) > 0 { - valptr = unsafe.Pointer(syscall.StringToUTF16Ptr(value)) + valptr = pointerStringWithoutError(value) } procRegGetValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), + uintptr(pointerStringWithoutError(subKey)), uintptr(valptr), uintptr(RRF_RT_ANY), 0, @@ -179,7 +179,7 @@ func RegGetRaw(hKey HKEY, subKey string, value string) []byte { buf := make([]byte, bufLen) ret, _, _ := procRegGetValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), + uintptr(pointerStringWithoutError(subKey)), uintptr(valptr), uintptr(RRF_RT_ANY), 0, @@ -196,7 +196,7 @@ func RegGetRaw(hKey HKEY, subKey string, value string) []byte { func RegSetBinary(hKey HKEY, subKey string, value []byte) (errno int) { var lptr, vptr unsafe.Pointer if len(subKey) > 0 { - lptr = unsafe.Pointer(syscall.StringToUTF16Ptr(subKey)) + lptr = pointerStringWithoutError(subKey) } if len(value) > 0 { vptr = unsafe.Pointer(&value[0]) @@ -215,7 +215,7 @@ func RegSetBinary(hKey HKEY, subKey string, value []byte) (errno int) { func RegSetString(hKey HKEY, subKey string, value string) (errno int) { var lptr, vptr unsafe.Pointer if len(subKey) > 0 { - lptr = unsafe.Pointer(syscall.StringToUTF16Ptr(subKey)) + lptr = pointerStringWithoutError(subKey) } var buf []uint16 if len(value) > 0 { @@ -239,7 +239,7 @@ func RegSetString(hKey HKEY, subKey string, value string) (errno int) { func RegSetUint32(hKey HKEY, subKey string, value uint32) (errno int) { var lptr unsafe.Pointer if len(subKey) > 0 { - lptr = unsafe.Pointer(syscall.StringToUTF16Ptr(subKey)) + lptr = pointerStringWithoutError(subKey) } vptr := unsafe.Pointer(&value) ret, _, _ := procRegSetValueEx.Call( @@ -257,8 +257,8 @@ func RegGetString(hKey HKEY, subKey string, value string) string { var bufLen uint32 procRegGetValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), + uintptr(pointerStringWithoutError(subKey)), + uintptr(pointerStringWithoutError(value)), uintptr(RRF_RT_REG_SZ), 0, 0, @@ -271,8 +271,8 @@ func RegGetString(hKey HKEY, subKey string, value string) string { buf := make([]uint16, bufLen) ret, _, _ := procRegGetValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), + uintptr(pointerStringWithoutError(subKey)), + uintptr(pointerStringWithoutError(value)), uintptr(RRF_RT_REG_SZ), 0, uintptr(unsafe.Pointer(&buf[0])), @@ -289,8 +289,8 @@ func RegGetUint32(hKey HKEY, subKey string, value string) (data uint32, errno in var dataLen uint32 = uint32(unsafe.Sizeof(data)) ret, _, _ := procRegGetValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), + uintptr(pointerStringWithoutError(subKey)), + uintptr(pointerStringWithoutError(value)), uintptr(RRF_RT_REG_DWORD), 0, uintptr(unsafe.Pointer(&data)), @@ -303,8 +303,8 @@ func RegGetUint32(hKey HKEY, subKey string, value string) (data uint32, errno in func RegSetKeyValue(hKey HKEY, subKey string, valueName string, dwType uint32, data uintptr, cbData uint16) (errno int) { ret, _, _ := procRegSetKeyValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(valueName))), + uintptr(pointerStringWithoutError(subKey))), + uintptr(pointerStringWithoutError(valueName))), uintptr(dwType), data, uintptr(cbData)) @@ -316,8 +316,8 @@ func RegSetKeyValue(hKey HKEY, subKey string, valueName string, dwType uint32, d func RegDeleteKeyValue(hKey HKEY, subKey string, valueName string) (errno int) { ret, _, _ := procRegDeleteKeyValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(valueName)))) + uintptr(pointerStringWithoutError(subKey)), + uintptr(pointerStringWithoutError(valueName))) return int(ret) } @@ -325,7 +325,7 @@ func RegDeleteKeyValue(hKey HKEY, subKey string, valueName string) (errno int) { func RegDeleteValue(hKey HKEY, valueName string) (errno int) { ret, _, _ := procRegDeleteValue.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(valueName)))) + uintptr(pointerStringWithoutError(valueName))) return int(ret) } @@ -333,7 +333,7 @@ func RegDeleteValue(hKey HKEY, valueName string) (errno int) { func RegDeleteTree(hKey HKEY, subKey string) (errno int) { ret, _, _ := procRegDeleteTree.Call( uintptr(hKey), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey)))) + uintptr(pointerStringWithoutError(subKey))) return int(ret) } @@ -355,8 +355,8 @@ func RegEnumKeyEx(hKey HKEY, index uint32) string { func OpenEventLog(servername string, sourcename string) HANDLE { ret, _, _ := procOpenEventLog.Call( - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(servername))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(sourcename)))) + uintptr(pointerStringWithoutError(servername)), + uintptr(pointerStringWithoutError(sourcename))) return HANDLE(ret) } @@ -384,10 +384,10 @@ func CloseEventLog(eventlog HANDLE) bool { func OpenSCManager(lpMachineName, lpDatabaseName string, dwDesiredAccess uint32) (HANDLE, error) { var p1, p2 uintptr if len(lpMachineName) > 0 { - p1 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpMachineName))) + p1 = uintptr(pointerStringWithoutError(lpMachineName)) } if len(lpDatabaseName) > 0 { - p2 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpDatabaseName))) + p2 = uintptr(pointerStringWithoutError(lpDatabaseName)) } ret, _, _ := procOpenSCManager.Call( p1, @@ -412,7 +412,7 @@ func CloseServiceHandle(hSCObject HANDLE) error { func OpenService(hSCManager HANDLE, lpServiceName string, dwDesiredAccess uint32) (HANDLE, error) { ret, _, _ := procOpenService.Call( uintptr(hSCManager), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceName))), + uintptr(pointerStringWithoutError(lpServiceName)), uintptr(dwDesiredAccess)) if ret == 0 { @@ -433,7 +433,7 @@ func StartService(hService HANDLE, lpServiceArgVectors []string) error { } else { lpArgs := make([]uintptr, l) for i := 0; i < l; i++ { - lpArgs[i] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceArgVectors[i]))) + lpArgs[i] = uintptr(pointerStringWithoutError(lpServiceArgVectors[i])) } ret, _, _ = procStartService.Call( diff --git a/gdiplus.go b/gdiplus.go index f3a8fca..9190ef8 100644 --- a/gdiplus.go +++ b/gdiplus.go @@ -104,7 +104,7 @@ var ( func GdipCreateBitmapFromFile(filename string) (*uintptr, error) { var bitmap *uintptr ret, _, _ := procGdipCreateBitmapFromFile.Call( - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(filename))), + uintptr(pointerStringWithoutError(filename)), uintptr(unsafe.Pointer(&bitmap))) if ret != Ok { diff --git a/kernel32.go b/kernel32.go index b2d0be2..b96260d 100644 --- a/kernel32.go +++ b/kernel32.go @@ -235,8 +235,8 @@ func CreateProcessA(lpApplicationName *string, } procCreateProcessA.Call( - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(*lpApplicationName))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpCommandLine))), + uintptr(pointerStringWithoutError(*lpApplicationName)), + uintptr(pointerStringWithoutError(lpCommandLine)), uintptr(unsafe.Pointer(lpProcessAttributes)), uintptr(unsafe.Pointer(lpThreadAttributes)), uintptr(inherit), @@ -293,7 +293,7 @@ func GetProcAddress(hProcess HANDLE, procname string) (addr uintptr, err error) if procname == "" { pn = 0 } else { - pn = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(procname))) + pn = uintptr(pointerStringWithoutError(procname)) } ret, _, err := procGetProcAddress.Call(uintptr(hProcess), pn) @@ -328,7 +328,7 @@ func GetModuleHandle(modulename string) HINSTANCE { if modulename == "" { mn = 0 } else { - mn = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(modulename))) + mn = uintptr(pointerStringWithoutError(modulename)) } ret, _, _ := procGetModuleHandle.Call(mn) return HINSTANCE(ret) @@ -601,7 +601,7 @@ func SetConsoleTextAttribute(hConsoleOutput HANDLE, wAttributes uint16) bool { func GetDiskFreeSpaceEx(dirName string) (r bool, freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes uint64) { ret, _, _ := procGetDiskFreeSpaceEx.Call( - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(dirName))), + uintptr(pointerStringWithoutError(dirName)), uintptr(unsafe.Pointer(&freeBytesAvailable)), uintptr(unsafe.Pointer(&totalNumberOfBytes)), uintptr(unsafe.Pointer(&totalNumberOfFreeBytes))) diff --git a/oleaut32.go b/oleaut32.go index 379b832..dc9f8f7 100644 --- a/oleaut32.go +++ b/oleaut32.go @@ -29,7 +29,7 @@ func VariantInit(v *VARIANT) { } func SysAllocString(v string) (ss *int16) { - pss, _, _ := procSysAllocString.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v)))) + pss, _, _ := procSysAllocString.Call(uintptr(pointerStringWithoutError(v))) ss = (*int16)(unsafe.Pointer(pss)) return } diff --git a/shell32.go b/shell32.go index 09cba52..afa537f 100644 --- a/shell32.go +++ b/shell32.go @@ -22,6 +22,7 @@ var ( procSHBrowseForFolder = modshell32.NewProc("SHBrowseForFolderW") procSHGetPathFromIDList = modshell32.NewProc("SHGetPathFromIDListW") procShellExecute = modshell32.NewProc("ShellExecuteW") + procShellNotifyIconW = modshell32.NewProc("Shell_NotifyIconW") ) func SHBrowseForFolder(bi *BROWSEINFO) uintptr { @@ -89,19 +90,19 @@ func DragFinish(hDrop HDROP) { func ShellExecute(hwnd HWND, lpOperation, lpFile, lpParameters, lpDirectory string, nShowCmd int) error { var op, param, directory uintptr if len(lpOperation) != 0 { - op = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpOperation))) + op = uintptr(pointerStringWithoutError(lpOperation)) } if len(lpParameters) != 0 { - param = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpParameters))) + param = uintptr(pointerStringWithoutError(lpParameters)) } if len(lpDirectory) != 0 { - directory = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpDirectory))) + directory = uintptr(pointerStringWithoutError(lpDirectory)) } ret, _, _ := procShellExecute.Call( uintptr(hwnd), op, - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpFile))), + uintptr(pointerStringWithoutError(lpFile)), param, directory, uintptr(nShowCmd)) @@ -146,8 +147,18 @@ func ShellExecute(hwnd HWND, lpOperation, lpFile, lpParameters, lpDirectory stri func ExtractIcon(lpszExeFileName string, nIconIndex int) HICON { ret, _, _ := procExtractIcon.Call( 0, - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpszExeFileName))), + uintptr(pointerStringWithoutError(lpszExeFileName)), uintptr(nIconIndex)) return HICON(ret) } + +// Shell_NotifyIconW Sends a message to the taskbar's status area. +func Shell_NotifyIconW(dwMessage string, lpData int) bool { + ret, _, _ := procShellNotifyIconW.Call( + 0, + uintptr(pointerStringWithoutError(lpszExeFileName)), + uintptr(nIconIndex)) + + return bool(ret) +} diff --git a/user32.go b/user32.go index a31bdcf..ad22f49 100644 --- a/user32.go +++ b/user32.go @@ -186,11 +186,11 @@ func FindWindowExW(hwndParent, hwndChildAfter HWND, className, windowName *uint1 func FindWindowExS(hwndParent, hwndChildAfter HWND, className, windowName *string) HWND { var class *uint16 = nil if className != nil { - class = syscall.StringToUTF16Ptr(*className) + class, _ = syscall.UTF16PtrFromString(*className) } var window *uint16 = nil if windowName != nil { - window = syscall.StringToUTF16Ptr(*windowName) + window, _ = syscall.UTF16PtrFromString(*windowName) } return FindWindowExW(hwndParent, hwndChildAfter, class, window) } @@ -207,11 +207,11 @@ func FindWindowW(className, windowName *uint16) HWND { func FindWindowS(className, windowName *string) HWND { var class *uint16 = nil if className != nil { - class = syscall.StringToUTF16Ptr(*className) + class, _ = syscall.UTF16PtrFromString(*className) } var window *uint16 = nil if windowName != nil { - window = syscall.StringToUTF16Ptr(*windowName) + window, _ = syscall.UTF16PtrFromString(*windowName) } return FindWindowW(class, window) } @@ -267,7 +267,7 @@ func LoadIcon(instance HINSTANCE, iconName *uint16) HICON { func LoadIconS(instance HINSTANCE, iconName *string) HICON { var icon *uint16 = nil if iconName != nil { - icon = syscall.StringToUTF16Ptr(*iconName) + icon, _ = syscall.UTF16PtrFromString(*iconName) } return LoadIcon(instance, icon) } @@ -284,7 +284,7 @@ func LoadCursor(instance HINSTANCE, cursorName *uint16) HCURSOR { func LoadCursorS(instance HINSTANCE, cursorName *string) HCURSOR { var cursor *uint16 = nil if cursorName != nil { - cursor = syscall.StringToUTF16Ptr(*cursorName) + cursor, _ = syscall.UTF16PtrFromString(*cursorName) } return LoadCursor(instance, cursor) } @@ -329,11 +329,11 @@ func CreateWindowExS(exStyle uint, className, windowName *string, instance HINSTANCE, param unsafe.Pointer) HWND { var class *uint16 = nil if className != nil { - class = syscall.StringToUTF16Ptr(*className) + class, _ = syscall.UTF16PtrFromString(*className) } var window *uint16 = nil if windowName != nil { - window = syscall.StringToUTF16Ptr(*windowName) + window, _ = syscall.UTF16PtrFromString(*windowName) } return CreateWindowEx( exStyle, @@ -456,7 +456,7 @@ func WaitMessage() bool { func SetWindowText(hwnd HWND, text string) { procSetWindowText.Call( uintptr(hwnd), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text)))) + uintptr(pointerStringWithoutError(text))) } func GetWindowTextLength(hwnd HWND) int { @@ -644,8 +644,8 @@ func GetWindowThreadProcessId(hwnd HWND) (HANDLE, int) { func MessageBox(hwnd HWND, text, caption string, flags uint) int { ret, _, _ := procMessageBox.Call( uintptr(hwnd), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), + uintptr(pointerStringWithoutError(text)), + uintptr(pointerStringWithoutError(caption)), uintptr(flags)) return int(ret) @@ -875,7 +875,7 @@ func FillRect(hDC HDC, lprc *RECT, hbr HBRUSH) bool { func DrawText(hDC HDC, text string, uCount int, lpRect *RECT, uFormat uint) int { ret, _, _ := procDrawText.Call( uintptr(hDC), - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), + uintptr(pointerStringWithoutError(text)), uintptr(uCount), uintptr(unsafe.Pointer(lpRect)), uintptr(uFormat)) diff --git a/utils.go b/utils.go index 208bbca..c11334b 100644 --- a/utils.go +++ b/utils.go @@ -87,7 +87,7 @@ func ComGetIDsOfName(disp *IDispatch, names []string) []int32 { wnames := make([]*uint16, len(names)) dispid := make([]int32, len(names)) for i := 0; i < len(names); i++ { - wnames[i] = syscall.StringToUTF16Ptr(names[i]) + wnames[i], _ = syscall.UTF16PtrFromString(names[i]) } hr, _, _ := syscall.Syscall6(disp.lpVtbl.pGetIDsOfNames, 6, uintptr(unsafe.Pointer(disp)), @@ -242,3 +242,13 @@ func IsErrSuccess(err error) bool { } return false } + +func pointerStringWithError(data string) (unsafe.Pointer, error) { + pp, err := syscall.UTF16PtrFromString(data) + return unsafe.Pointer(pp), err +} + +func pointerStringWithoutError(data string) unsafe.Pointer { + pp, _ := syscall.UTF16PtrFromString(data) + return unsafe.Pointer(pp) +}