]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Add support for setupapi.SetupDiEnumDeviceInfo()
authorSimon Rozman <simon@rozman.si>
Fri, 1 Feb 2019 11:17:09 +0000 (12:17 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 5 Feb 2019 11:59:42 +0000 (12:59 +0100)
Signed-off-by: Simon Rozman <simon@rozman.si>
setupapi/setupapi_windows.go
setupapi/setupapi_windows_test.go
setupapi/types_windows.go
setupapi/zsetupapi_windows.go

index 31a0bffabd9dd2cf978b82cc37a08ae50cc4fcfb..dad096761a599f88929900b133d711afa569ce0e 100644 (file)
@@ -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
+}
index 017fc3dde0f9349b5c57625bc0ccf2579812cceb..8aa72211067742de356c9291923fa17da4176d21 100644 (file)
@@ -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")
+               }
+       }
+}
index a5bda6dcb2039d7b8034c5d8ef4bfd5ac7f31b7d..e94d112dea8317983802b00820871b878f5bd903 100644 (file)
@@ -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
+}
index 47f54647f83d25499e1cbd78219db66fe8d6f365..0896d45c59e9f2ce6564768e1f3146b86e46816c 100644 (file)
@@ -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
+}