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 <simon@rozman.si>
This commit is contained in:
Simon Rozman 2019-02-04 08:39:31 +01:00 committed by Jason A. Donenfeld
parent 9635a0b3a6
commit 3e0e61dd26
3 changed files with 29 additions and 14 deletions

View File

@ -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
}

View File

@ -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")
}
}
}

View File

@ -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)