From 9635a0b3a69272aac998fad700344fac392d2adf Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 4 Feb 2019 08:23:55 +0100 Subject: Add support for setupapi.SetupDiClassNameFromGuid() Signed-off-by: Simon Rozman --- setupapi/setupapi_windows.go | 14 ++++++++++++++ setupapi/setupapi_windows_test.go | 19 +++++++++++++++++++ setupapi/types_windows.go | 11 +++++++++++ setupapi/zsetupapi_windows.go | 13 +++++++++++++ 4 files changed, 57 insertions(+) diff --git a/setupapi/setupapi_windows.go b/setupapi/setupapi_windows.go index c7a42c1..df00aad 100644 --- a/setupapi/setupapi_windows.go +++ b/setupapi/setupapi_windows.go @@ -13,6 +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 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 @@ -24,6 +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) { + var _p0 [MAX_CLASS_NAME_LEN]uint16 + + err = setupDiClassNameFromGuid(ClassGUID, &_p0[0], MAX_CLASS_NAME_LEN, nil) + if err != nil { + return + } + + ClassName = windows.UTF16ToString(_p0[:]) + return +} + // SetupDiGetClassDevsEx function returns a handle to a device information set that contains requested device information elements for a local or a remote computer. func SetupDiGetClassDevsEx(ClassGUID *windows.GUID, Enumerator string, hwndParent uintptr, Flags DIGCF, DeviceInfoSet DevInfo, MachineName string) (handle DevInfo, err error) { var _p0 *uint16 diff --git a/setupapi/setupapi_windows_test.go b/setupapi/setupapi_windows_test.go index 1d3c33d..e26032d 100644 --- a/setupapi/setupapi_windows_test.go +++ b/setupapi/setupapi_windows_test.go @@ -6,6 +6,7 @@ package setupapi import ( + "strings" "syscall" "testing" @@ -19,6 +20,24 @@ func init() { computerName, _ = windows.ComputerName() } +func TestSetupDiClassNameFromGuid(t *testing.T) { + className, err := SetupDiClassNameFromGuid(&deviceClassNetGUID) + if err != nil { + t.Errorf("Error calling SetupDiClassNameFromGuid: %s", err.Error()) + } else if strings.ToLower(className) != "net" { + t.Errorf("SetupDiClassNameFromGuid(%x) should return \"Net\"", deviceClassNetGUID) + } + + _, err = SetupDiClassNameFromGuid(nil) + if err == nil { + t.Errorf("SetupDiClassNameFromGuid(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") + } + } +} + func TestSetupDiGetClassDevsEx(t *testing.T) { devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "PCI", 0, DIGCF_PRESENT, DevInfo(0), computerName) if err == nil { diff --git a/setupapi/types_windows.go b/setupapi/types_windows.go index bc6f66b..fa47a5a 100644 --- a/setupapi/types_windows.go +++ b/setupapi/types_windows.go @@ -9,6 +9,17 @@ import ( "golang.org/x/sys/windows" ) +const ( + MAX_DEVICE_ID_LEN = 200 + MAX_DEVNODE_ID_LEN = MAX_DEVICE_ID_LEN + MAX_GUID_STRING_LEN = 39 // 38 chars + terminator null + MAX_CLASS_NAME_LEN = 32 + MAX_PROFILE_LEN = 80 + MAX_CONFIG_VALUE = 9999 + MAX_INSTANCE_VALUE = 9999 + CONFIGMG_VERSION = 0x0400 +) + // DIGCF flags controll what is included in the device information set built by SetupDiGetClassDevs type DIGCF uint32 diff --git a/setupapi/zsetupapi_windows.go b/setupapi/zsetupapi_windows.go index a8c61ad..082bde4 100644 --- a/setupapi/zsetupapi_windows.go +++ b/setupapi/zsetupapi_windows.go @@ -39,6 +39,7 @@ func errnoErr(e syscall.Errno) error { var ( modsetupapi = windows.NewLazySystemDLL("setupapi.dll") + procSetupDiClassNameFromGuidW = modsetupapi.NewProc("SetupDiClassNameFromGuidW") procSetupDiGetClassDevsExW = modsetupapi.NewProc("SetupDiGetClassDevsExW") procSetupDiDestroyDeviceInfoList = modsetupapi.NewProc("SetupDiDestroyDeviceInfoList") procSetupDiGetDeviceInfoListDetailW = modsetupapi.NewProc("SetupDiGetDeviceInfoListDetailW") @@ -51,6 +52,18 @@ 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) + if r1 == 0 { + if e1 != 0 { + err = errnoErr(e1) + } else { + err = syscall.EINVAL + } + } + return +} + func setupDiGetClassDevsEx(ClassGUID *windows.GUID, Enumerator *uint16, hwndParent uintptr, Flags DIGCF, DeviceInfoSet DevInfo, MachineName *uint16, reserved uintptr) (handle DevInfo, err error) { r0, _, e1 := syscall.Syscall9(procSetupDiGetClassDevsExW.Addr(), 7, uintptr(unsafe.Pointer(ClassGUID)), uintptr(unsafe.Pointer(Enumerator)), uintptr(hwndParent), uintptr(Flags), uintptr(DeviceInfoSet), uintptr(unsafe.Pointer(MachineName)), uintptr(reserved), 0, 0) handle = DevInfo(r0) -- cgit v1.2.3