From 3e0e61dd26d900eb6eaf826200241cbf84cd068d Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 4 Feb 2019 08:39:31 +0100 Subject: [PATCH] Replace SetupDiClassNameFromGuid() with SetupDiClassNameFromGuidEx() The former is only a subset of the later. To minimize future maintenance, we'll provide support for extended version only. Signed-off-by: Simon Rozman --- setupapi/setupapi_windows.go | 16 ++++++++++++---- setupapi/setupapi_windows_test.go | 21 ++++++++++++++------- setupapi/zsetupapi_windows.go | 6 +++--- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/setupapi/setupapi_windows.go b/setupapi/setupapi_windows.go index df00aad..6b138df 100644 --- a/setupapi/setupapi_windows.go +++ b/setupapi/setupapi_windows.go @@ -13,7 +13,7 @@ import ( "golang.org/x/sys/windows/registry" ) -//sys setupDiClassNameFromGuid(ClassGUID *windows.GUID, ClassName *uint16, ClassNameSize uint32, RequiredSize *uint32) (err error) = setupapi.SetupDiClassNameFromGuidW +//sys setupDiClassNameFromGuidEx(ClassGUID *windows.GUID, ClassName *uint16, ClassNameSize uint32, RequiredSize *uint32, MachineName *uint16, Reserved uintptr) (err error) = setupapi.SetupDiClassNameFromGuidExW //sys setupDiGetClassDevsEx(ClassGUID *windows.GUID, Enumerator *uint16, hwndParent uintptr, Flags DIGCF, DeviceInfoSet DevInfo, MachineName *uint16, reserved uintptr) (handle DevInfo, err error) [failretval==DevInfo(windows.InvalidHandle)] = setupapi.SetupDiGetClassDevsExW //sys SetupDiDestroyDeviceInfoList(DeviceInfoSet DevInfo) (err error) = setupapi.SetupDiDestroyDeviceInfoList //sys setupDiGetDeviceInfoListDetail(DeviceInfoSet DevInfo, DeviceInfoSetDetailData *_SP_DEVINFO_LIST_DETAIL_DATA) (err error) = setupapi.SetupDiGetDeviceInfoListDetailW @@ -25,11 +25,19 @@ import ( //sys SetupDiSetClassInstallParams(DeviceInfoSet DevInfo, DeviceInfoData *SP_DEVINFO_DATA, ClassInstallParams *SP_CLASSINSTALL_HEADER, ClassInstallParamsSize uint32) (err error) = setupapi.SetupDiSetClassInstallParamsW //sys SetupDiCallClassInstaller(InstallFunction DI_FUNCTION, DeviceInfoSet DevInfo, DeviceInfoData *SP_DEVINFO_DATA) (err error) = setupapi.SetupDiCallClassInstaller -// SetupDiClassNameFromGuid function retrieves the class name associated with a class GUID. -func SetupDiClassNameFromGuid(ClassGUID *windows.GUID) (ClassName string, err error) { +// SetupDiClassNameFromGuidEx function retrieves the class name associated with a class GUID. The class can be installed on a local or remote computer. +func SetupDiClassNameFromGuidEx(ClassGUID *windows.GUID, MachineName string) (ClassName string, err error) { var _p0 [MAX_CLASS_NAME_LEN]uint16 - err = setupDiClassNameFromGuid(ClassGUID, &_p0[0], MAX_CLASS_NAME_LEN, nil) + var _p1 *uint16 + if MachineName != "" { + _p1, err = syscall.UTF16PtrFromString(MachineName) + if err != nil { + return + } + } + + err = setupDiClassNameFromGuidEx(ClassGUID, &_p0[0], MAX_CLASS_NAME_LEN, nil, _p1, 0) if err != nil { return } diff --git a/setupapi/setupapi_windows_test.go b/setupapi/setupapi_windows_test.go index e26032d..4afd21a 100644 --- a/setupapi/setupapi_windows_test.go +++ b/setupapi/setupapi_windows_test.go @@ -20,20 +20,27 @@ func init() { computerName, _ = windows.ComputerName() } -func TestSetupDiClassNameFromGuid(t *testing.T) { - className, err := SetupDiClassNameFromGuid(&deviceClassNetGUID) +func TestSetupDiClassNameFromGuidEx(t *testing.T) { + className, err := SetupDiClassNameFromGuidEx(&deviceClassNetGUID, "") if err != nil { - t.Errorf("Error calling SetupDiClassNameFromGuid: %s", err.Error()) + t.Errorf("Error calling SetupDiClassNameFromGuidEx: %s", err.Error()) } else if strings.ToLower(className) != "net" { - t.Errorf("SetupDiClassNameFromGuid(%x) should return \"Net\"", deviceClassNetGUID) + t.Errorf("SetupDiClassNameFromGuidEx(%x) should return \"Net\"", deviceClassNetGUID) } - _, err = SetupDiClassNameFromGuid(nil) + className, err = SetupDiClassNameFromGuidEx(&deviceClassNetGUID, computerName) + if err != nil { + t.Errorf("Error calling SetupDiClassNameFromGuidEx: %s", err.Error()) + } else if strings.ToLower(className) != "net" { + t.Errorf("SetupDiClassNameFromGuidEx(%x) should return \"Net\"", deviceClassNetGUID) + } + + _, err = SetupDiClassNameFromGuidEx(nil, "") if err == nil { - t.Errorf("SetupDiClassNameFromGuid(nil) should fail") + t.Errorf("SetupDiClassNameFromGuidEx(nil) should fail") } else { if errWin, ok := err.(syscall.Errno); !ok || errWin != 1784 /*ERROR_INVALID_USER_BUFFER*/ { - t.Errorf("SetupDiClassNameFromGuid(nil) should fail with ERROR_INVALID_USER_BUFFER") + t.Errorf("SetupDiClassNameFromGuidEx(nil) should fail with ERROR_INVALID_USER_BUFFER") } } } diff --git a/setupapi/zsetupapi_windows.go b/setupapi/zsetupapi_windows.go index 082bde4..33d8fb0 100644 --- a/setupapi/zsetupapi_windows.go +++ b/setupapi/zsetupapi_windows.go @@ -39,7 +39,7 @@ func errnoErr(e syscall.Errno) error { var ( modsetupapi = windows.NewLazySystemDLL("setupapi.dll") - procSetupDiClassNameFromGuidW = modsetupapi.NewProc("SetupDiClassNameFromGuidW") + procSetupDiClassNameFromGuidExW = modsetupapi.NewProc("SetupDiClassNameFromGuidExW") procSetupDiGetClassDevsExW = modsetupapi.NewProc("SetupDiGetClassDevsExW") procSetupDiDestroyDeviceInfoList = modsetupapi.NewProc("SetupDiDestroyDeviceInfoList") procSetupDiGetDeviceInfoListDetailW = modsetupapi.NewProc("SetupDiGetDeviceInfoListDetailW") @@ -52,8 +52,8 @@ var ( procSetupDiCallClassInstaller = modsetupapi.NewProc("SetupDiCallClassInstaller") ) -func setupDiClassNameFromGuid(ClassGUID *windows.GUID, ClassName *uint16, ClassNameSize uint32, RequiredSize *uint32) (err error) { - r1, _, e1 := syscall.Syscall6(procSetupDiClassNameFromGuidW.Addr(), 4, uintptr(unsafe.Pointer(ClassGUID)), uintptr(unsafe.Pointer(ClassName)), uintptr(ClassNameSize), uintptr(unsafe.Pointer(RequiredSize)), 0, 0) +func setupDiClassNameFromGuidEx(ClassGUID *windows.GUID, ClassName *uint16, ClassNameSize uint32, RequiredSize *uint32, MachineName *uint16, Reserved uintptr) (err error) { + r1, _, e1 := syscall.Syscall6(procSetupDiClassNameFromGuidExW.Addr(), 6, uintptr(unsafe.Pointer(ClassGUID)), uintptr(unsafe.Pointer(ClassName)), uintptr(ClassNameSize), uintptr(unsafe.Pointer(RequiredSize)), uintptr(unsafe.Pointer(MachineName)), uintptr(Reserved)) if r1 == 0 { if e1 != 0 { err = errnoErr(e1)