From 955d8dfe04fa87d265bb1ccb671db05a323eb86f Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 1 Feb 2019 12:17:09 +0100 Subject: [PATCH] Add support for setupapi.SetupDiEnumDeviceInfo() Signed-off-by: Simon Rozman --- setupapi/setupapi_windows.go | 22 ++++++++++++-- setupapi/setupapi_windows_test.go | 48 ++++++++++++++++++++++--------- setupapi/types_windows.go | 16 +++++++++-- setupapi/zsetupapi_windows.go | 15 +++++++++- 4 files changed, 83 insertions(+), 18 deletions(-) diff --git a/setupapi/setupapi_windows.go b/setupapi/setupapi_windows.go index 31a0bff..dad0967 100644 --- a/setupapi/setupapi_windows.go +++ b/setupapi/setupapi_windows.go @@ -14,7 +14,8 @@ import ( //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 +//sys setupDiGetDeviceInfoListDetail(DeviceInfoSet DevInfo, DeviceInfoSetDetailData *_SP_DEVINFO_LIST_DETAIL_DATA) (err error) = setupapi.SetupDiGetDeviceInfoListDetailW +//sys setupDiEnumDeviceInfo(DeviceInfoSet DevInfo, MemberIndex uint32, DeviceInfoData *_SP_DEVINFO_DATA) (err error) = setupapi.SetupDiEnumDeviceInfo // 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) { @@ -37,7 +38,7 @@ func SetupDiGetClassDevsEx(ClassGUID *windows.GUID, Enumerator string, hwndParen // SetupDiGetDeviceInfoListDetail function retrieves information associated with a device information set including the class GUID, remote computer handle, and remote computer name. func SetupDiGetDeviceInfoListDetail(DeviceInfoSet DevInfo) (data *DevInfoListDetailData, err error) { - var _p0 SP_DEVINFO_LIST_DETAIL_DATA + var _p0 _SP_DEVINFO_LIST_DETAIL_DATA _p0.Size = uint32(unsafe.Sizeof(_p0)) err = setupDiGetDeviceInfoListDetail(DeviceInfoSet, &_p0) @@ -52,3 +53,20 @@ func SetupDiGetDeviceInfoListDetail(DeviceInfoSet DevInfo) (data *DevInfoListDet } return } + +// SetupDiEnumDeviceInfo function returns a SP_DEVINFO_DATA structure that specifies a device information element in a device information set. +func SetupDiEnumDeviceInfo(DeviceInfoSet DevInfo, MemberIndex int) (data *DevInfoData, err error) { + var _p0 _SP_DEVINFO_DATA + _p0.Size = uint32(unsafe.Sizeof(_p0)) + + err = setupDiEnumDeviceInfo(DeviceInfoSet, uint32(MemberIndex), &_p0) + if err != nil { + return + } + + data = &DevInfoData{ + ClassGUID: _p0.ClassGUID, + DevInst: _p0.DevInst, + } + return +} diff --git a/setupapi/setupapi_windows_test.go b/setupapi/setupapi_windows_test.go index 017fc3d..8aa7221 100644 --- a/setupapi/setupapi_windows_test.go +++ b/setupapi/setupapi_windows_test.go @@ -12,7 +12,7 @@ import ( "golang.org/x/sys/windows" ) -var guidDeviceClassNet = windows.GUID{0x4d36e972, 0xe325, 0x11ce, [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}} +var deviceClassNetGUID = windows.GUID{0x4d36e972, 0xe325, 0x11ce, [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}} var computerName string func init() { @@ -20,16 +20,16 @@ func init() { } func TestSetupDiGetClassDevsEx(t *testing.T) { - dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "PCI", 0, DIGCF_PRESENT, DevInfo(0), computerName) + devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "PCI", 0, DIGCF_PRESENT, DevInfo(0), computerName) if err == nil { - dev_info_list.Close() + devInfoList.Close() } else { t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error()) } - dev_info_list, err = SetupDiGetClassDevsEx(nil, "", 0, DIGCF_PRESENT, DevInfo(0), "") + devInfoList, err = SetupDiGetClassDevsEx(nil, "", 0, DIGCF_PRESENT, DevInfo(0), "") if err == nil { - dev_info_list.Close() + devInfoList.Close() t.Errorf("SetupDiGetClassDevsEx(nil, ...) should fail") } else { if errWin, ok := err.(syscall.Errno); !ok || errWin != 87 /*ERROR_INVALID_PARAMETER*/ { @@ -39,18 +39,18 @@ func TestSetupDiGetClassDevsEx(t *testing.T) { } func TestSetupDiGetDeviceInfoListDetailLocal(t *testing.T) { - dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "", 0, DIGCF_PRESENT, DevInfo(0), "") + devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, DIGCF_PRESENT, DevInfo(0), "") if err != nil { t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error()) } - defer SetupDiDestroyDeviceInfoList(dev_info_list) + defer SetupDiDestroyDeviceInfoList(devInfoList) - data, err := SetupDiGetDeviceInfoListDetail(dev_info_list) + data, err := SetupDiGetDeviceInfoListDetail(devInfoList) if err != nil { t.Errorf("Error calling SetupDiGetDeviceInfoListDetail: %s", err.Error()) } - if data.ClassGUID != guidDeviceClassNet { + if data.ClassGUID != deviceClassNetGUID { t.Error("SetupDiGetDeviceInfoListDetail returned different class GUID") } @@ -64,18 +64,18 @@ func TestSetupDiGetDeviceInfoListDetailLocal(t *testing.T) { } func TestSetupDiGetDeviceInfoListDetailRemote(t *testing.T) { - dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "", 0, DIGCF_PRESENT, DevInfo(0), computerName) + devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, DIGCF_PRESENT, DevInfo(0), computerName) if err != nil { t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error()) } - defer SetupDiDestroyDeviceInfoList(dev_info_list) + defer SetupDiDestroyDeviceInfoList(devInfoList) - data, err := SetupDiGetDeviceInfoListDetail(dev_info_list) + data, err := SetupDiGetDeviceInfoListDetail(devInfoList) if err != nil { t.Errorf("Error calling SetupDiGetDeviceInfoListDetail: %s", err.Error()) } - if data.ClassGUID != guidDeviceClassNet { + if data.ClassGUID != deviceClassNetGUID { t.Error("SetupDiGetDeviceInfoListDetail returned different class GUID") } @@ -87,3 +87,25 @@ func TestSetupDiGetDeviceInfoListDetailRemote(t *testing.T) { t.Error("SetupDiGetDeviceInfoListDetail returned different remote machine name") } } + +func TestSetupDiEnumDeviceInfo(t *testing.T) { + devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, DIGCF_PRESENT, DevInfo(0), "") + if err != nil { + t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error()) + } + defer SetupDiDestroyDeviceInfoList(devInfoList) + + for i := 0; true; i++ { + data, err := SetupDiEnumDeviceInfo(devInfoList, i) + if err != nil { + if errWin, ok := err.(syscall.Errno); ok && errWin == 259 /*ERROR_NO_MORE_ITEMS*/ { + break + } + continue + } + + if data.ClassGUID != deviceClassNetGUID { + t.Error("SetupDiEnumDeviceInfo returned different class GUID") + } + } +} diff --git a/setupapi/types_windows.go b/setupapi/types_windows.go index a5bda6d..e94d112 100644 --- a/setupapi/types_windows.go +++ b/setupapi/types_windows.go @@ -37,8 +37,7 @@ const ( SP_MAX_MACHINENAME_LENGTH = windows.MAX_PATH + 3 ) -// SP_DEVINFO_LIST_DETAIL_DATA is a structure for detailed information on a device information set (used for SetupDiGetDeviceInfoListDetail which supercedes the functionality of SetupDiGetDeviceInfoListClass). -type SP_DEVINFO_LIST_DETAIL_DATA struct { +type _SP_DEVINFO_LIST_DETAIL_DATA struct { Size uint32 ClassGUID windows.GUID RemoteMachineHandle windows.Handle @@ -51,3 +50,16 @@ type DevInfoListDetailData struct { RemoteMachineHandle windows.Handle RemoteMachineName string } + +type _SP_DEVINFO_DATA struct { + Size uint32 + ClassGUID windows.GUID + DevInst uint32 // DEVINST handle + _ uintptr +} + +// DevInfoData is a device information structure (references a device instance that is a member of a device information set) +type DevInfoData struct { + ClassGUID windows.GUID + DevInst uint32 // DEVINST handle +} diff --git a/setupapi/zsetupapi_windows.go b/setupapi/zsetupapi_windows.go index 47f5464..0896d45 100644 --- a/setupapi/zsetupapi_windows.go +++ b/setupapi/zsetupapi_windows.go @@ -42,6 +42,7 @@ var ( procSetupDiGetClassDevsExW = modsetupapi.NewProc("SetupDiGetClassDevsExW") procSetupDiDestroyDeviceInfoList = modsetupapi.NewProc("SetupDiDestroyDeviceInfoList") procSetupDiGetDeviceInfoListDetailW = modsetupapi.NewProc("SetupDiGetDeviceInfoListDetailW") + procSetupDiEnumDeviceInfo = modsetupapi.NewProc("SetupDiEnumDeviceInfo") ) func setupDiGetClassDevsEx(ClassGUID *windows.GUID, Enumerator *uint16, hwndParent uintptr, Flags DIGCF, DeviceInfoSet DevInfo, MachineName *uint16, reserved uintptr) (handle DevInfo, err error) { @@ -69,7 +70,7 @@ func SetupDiDestroyDeviceInfoList(DeviceInfoSet DevInfo) (err error) { return } -func setupDiGetDeviceInfoListDetail(DeviceInfoSet DevInfo, DeviceInfoSetDetailData *SP_DEVINFO_LIST_DETAIL_DATA) (err error) { +func setupDiGetDeviceInfoListDetail(DeviceInfoSet DevInfo, DeviceInfoSetDetailData *_SP_DEVINFO_LIST_DETAIL_DATA) (err error) { r1, _, e1 := syscall.Syscall(procSetupDiGetDeviceInfoListDetailW.Addr(), 2, uintptr(DeviceInfoSet), uintptr(unsafe.Pointer(DeviceInfoSetDetailData)), 0) if r1 == 0 { if e1 != 0 { @@ -80,3 +81,15 @@ func setupDiGetDeviceInfoListDetail(DeviceInfoSet DevInfo, DeviceInfoSetDetailDa } return } + +func setupDiEnumDeviceInfo(DeviceInfoSet DevInfo, MemberIndex uint32, DeviceInfoData *_SP_DEVINFO_DATA) (err error) { + r1, _, e1 := syscall.Syscall(procSetupDiEnumDeviceInfo.Addr(), 3, uintptr(DeviceInfoSet), uintptr(MemberIndex), uintptr(unsafe.Pointer(DeviceInfoData))) + if r1 == 0 { + if e1 != 0 { + err = errnoErr(e1) + } else { + err = syscall.EINVAL + } + } + return +}