From 6df407bc07cea62a53772325c60de06a3c869dd2 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 8 Jun 2019 22:13:16 +0200 Subject: [PATCH] windows: correct types and error values of internal GUID handling This corrects the Windows int type to be the more correct int32 Go analog, as well as not using GetLastError() for the error value of the GUID string parsing function. Change-Id: I9716f991ef649f7d299295e3f4e75d3986ec3a74 Reviewed-on: https://go-review.googlesource.com/c/sys/+/181397 Run-TryBot: Jason Donenfeld TryBot-Result: Gobot Gobot Reviewed-by: Alex Brainman --- windows/syscall_windows.go | 6 +++--- windows/syscall_windows_test.go | 4 ++++ windows/zsyscall_windows.go | 16 ++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 74b72a0b..1d1170ab 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -288,8 +288,8 @@ func NewCallbackCDecl(fn interface{}) uintptr { //sys SetVolumeLabel(rootPathName *uint16, volumeName *uint16) (err error) = SetVolumeLabelW //sys SetVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16) (err error) = SetVolumeMountPointW //sys MessageBox(hwnd Handle, text *uint16, caption *uint16, boxtype uint32) (ret int32, err error) [failretval==0] = user32.MessageBoxW -//sys clsidFromString(lpsz *uint16, pclsid *GUID) (err error) [failretval!=0] = ole32.CLSIDFromString -//sys stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int) (chars int) = ole32.StringFromGUID2 +//sys clsidFromString(lpsz *uint16, pclsid *GUID) (ret error) = ole32.CLSIDFromString +//sys stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int32) (chars int32) = ole32.StringFromGUID2 //sys coCreateGuid(pguid *GUID) (ret error) = ole32.CoCreateGuid // syscall interface implementation for other packages @@ -1276,7 +1276,7 @@ func GenerateGUID() (GUID, error) { // in the form of "{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}". func (guid GUID) String() string { var str [100]uint16 - chars := stringFromGUID2(&guid, &str[0], len(str)) + chars := stringFromGUID2(&guid, &str[0], int32(len(str))) if chars <= 1 { return "" } diff --git a/windows/syscall_windows_test.go b/windows/syscall_windows_test.go index b157625f..9f78e2d9 100644 --- a/windows/syscall_windows_test.go +++ b/windows/syscall_windows_test.go @@ -191,4 +191,8 @@ func TestGUID(t *testing.T) { if guid2 != guid { t.Fatalf("Did not parse string back to original GUID = %q; want %q", guid2, guid) } + _, err = windows.GUIDFromString("not-a-real-guid") + if err != syscall.Errno(windows.CO_E_CLASSSTRING) { + t.Fatalf("Bad GUID string error = %v; want CO_E_CLASSSTRING", err) + } } diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index f37d7d42..6c35b90c 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -2436,21 +2436,17 @@ func MessageBox(hwnd Handle, text *uint16, caption *uint16, boxtype uint32) (ret return } -func clsidFromString(lpsz *uint16, pclsid *GUID) (err error) { - r1, _, e1 := syscall.Syscall(procCLSIDFromString.Addr(), 2, uintptr(unsafe.Pointer(lpsz)), uintptr(unsafe.Pointer(pclsid)), 0) - if r1 != 0 { - if e1 != 0 { - err = errnoErr(e1) - } else { - err = syscall.EINVAL - } +func clsidFromString(lpsz *uint16, pclsid *GUID) (ret error) { + r0, _, _ := syscall.Syscall(procCLSIDFromString.Addr(), 2, uintptr(unsafe.Pointer(lpsz)), uintptr(unsafe.Pointer(pclsid)), 0) + if r0 != 0 { + ret = syscall.Errno(r0) } return } -func stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int) (chars int) { +func stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int32) (chars int32) { r0, _, _ := syscall.Syscall(procStringFromGUID2.Addr(), 3, uintptr(unsafe.Pointer(rguid)), uintptr(unsafe.Pointer(lpsz)), uintptr(cchMax)) - chars = int(r0) + chars = int32(r0) return }