Add support for setupapi.SetupDiGetDeviceInfoListDetail()
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
		
							parent
							
								
									d41bc015cc
								
							
						
					
					
						commit
						45959c116a
					
				@ -7,40 +7,17 @@ package setupapi
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"syscall"
 | 
			
		||||
	"unsafe"
 | 
			
		||||
 | 
			
		||||
	"golang.org/x/sys/windows"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	SP_MAX_MACHINENAME_LENGTH = windows.MAX_PATH + 3
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DIGCF uint32
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	DIGCF_DEFAULT         DIGCF = 0x00000001 // only valid with DIGCF_DEVICEINTERFACE
 | 
			
		||||
	DIGCF_PRESENT         DIGCF = 0x00000002
 | 
			
		||||
	DIGCF_ALLCLASSES      DIGCF = 0x00000004
 | 
			
		||||
	DIGCF_PROFILE         DIGCF = 0x00000008
 | 
			
		||||
	DIGCF_DEVICEINTERFACE DIGCF = 0x00000010
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DevInfo windows.Handle
 | 
			
		||||
 | 
			
		||||
// The SetupDiDestroyDeviceInfoList function deletes a device information set and frees all associated memory.
 | 
			
		||||
func (h DevInfo) Close() error {
 | 
			
		||||
	if h != DevInfo(windows.InvalidHandle) {
 | 
			
		||||
		return SetupDiDestroyDeviceInfoList(h)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//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	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
 | 
			
		||||
 | 
			
		||||
// The 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) {
 | 
			
		||||
// 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
 | 
			
		||||
	if Enumerator != "" {
 | 
			
		||||
		_p0, err = syscall.UTF16PtrFromString(Enumerator)
 | 
			
		||||
@ -55,5 +32,23 @@ func SetupDiGetClassDevsEx(ClassGuid *windows.GUID, Enumerator string, hwndParen
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return setupDiGetClassDevsEx(ClassGuid, _p0, hwndParent, Flags, DeviceInfoSet, _p1, 0)
 | 
			
		||||
	return setupDiGetClassDevsEx(ClassGUID, _p0, hwndParent, Flags, DeviceInfoSet, _p1, 0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
	_p0.Size = uint32(unsafe.Sizeof(_p0))
 | 
			
		||||
 | 
			
		||||
	err = setupDiGetDeviceInfoListDetail(DeviceInfoSet, &_p0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	data = &DevInfoListDetailData{
 | 
			
		||||
		ClassGUID:           _p0.ClassGUID,
 | 
			
		||||
		RemoteMachineHandle: _p0.RemoteMachineHandle,
 | 
			
		||||
		RemoteMachineName:   windows.UTF16ToString(_p0.RemoteMachineName[:]),
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -12,15 +12,15 @@ import (
 | 
			
		||||
	"golang.org/x/sys/windows"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var guidDeviceClassNet = windows.GUID{0x4d36e972, 0xe325, 0x11ce, [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}
 | 
			
		||||
var computerName string
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	computerName, _ = windows.ComputerName()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSetupDiGetClassDevsEx(t *testing.T) {
 | 
			
		||||
	guidDeviceClassNet := windows.GUID{0x4d36e972, 0xe325, 0x11ce, [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}
 | 
			
		||||
 | 
			
		||||
	compName, err := windows.ComputerName()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("Error getting computer name: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "PCI", 0, DIGCF_PRESENT, DevInfo(0), compName)
 | 
			
		||||
	dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "PCI", 0, DIGCF_PRESENT, DevInfo(0), computerName)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		dev_info_list.Close()
 | 
			
		||||
	} else {
 | 
			
		||||
@ -37,3 +37,53 @@ func TestSetupDiGetClassDevsEx(t *testing.T) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSetupDiGetDeviceInfoListDetailLocal(t *testing.T) {
 | 
			
		||||
	dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "", 0, DIGCF_PRESENT, DevInfo(0), "")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer SetupDiDestroyDeviceInfoList(dev_info_list)
 | 
			
		||||
 | 
			
		||||
	data, err := SetupDiGetDeviceInfoListDetail(dev_info_list)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("Error calling SetupDiGetDeviceInfoListDetail: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if data.ClassGUID != guidDeviceClassNet {
 | 
			
		||||
		t.Error("SetupDiGetDeviceInfoListDetail returned different class GUID")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if data.RemoteMachineHandle != windows.Handle(0) {
 | 
			
		||||
		t.Error("SetupDiGetDeviceInfoListDetail returned non-NULL remote machine handle")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if data.RemoteMachineName != "" {
 | 
			
		||||
		t.Error("SetupDiGetDeviceInfoListDetail returned non-NULL remote machine name")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSetupDiGetDeviceInfoListDetailRemote(t *testing.T) {
 | 
			
		||||
	dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "", 0, DIGCF_PRESENT, DevInfo(0), computerName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer SetupDiDestroyDeviceInfoList(dev_info_list)
 | 
			
		||||
 | 
			
		||||
	data, err := SetupDiGetDeviceInfoListDetail(dev_info_list)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("Error calling SetupDiGetDeviceInfoListDetail: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if data.ClassGUID != guidDeviceClassNet {
 | 
			
		||||
		t.Error("SetupDiGetDeviceInfoListDetail returned different class GUID")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if data.RemoteMachineHandle == windows.Handle(0) {
 | 
			
		||||
		t.Error("SetupDiGetDeviceInfoListDetail returned NULL remote machine handle")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if data.RemoteMachineName != computerName {
 | 
			
		||||
		t.Error("SetupDiGetDeviceInfoListDetail returned different remote machine name")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										51
									
								
								setupapi/types_windows.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								setupapi/types_windows.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,51 @@
 | 
			
		||||
/* SPDX-License-Identifier: MIT
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package setupapi
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"golang.org/x/sys/windows"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DIGCF uint32
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	DIGCF_DEFAULT         DIGCF = 0x00000001 // only valid with DIGCF_DEVICEINTERFACE
 | 
			
		||||
	DIGCF_PRESENT         DIGCF = 0x00000002
 | 
			
		||||
	DIGCF_ALLCLASSES      DIGCF = 0x00000004
 | 
			
		||||
	DIGCF_PROFILE         DIGCF = 0x00000008
 | 
			
		||||
	DIGCF_DEVICEINTERFACE DIGCF = 0x00000010
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DevInfo windows.Handle
 | 
			
		||||
 | 
			
		||||
// SetupDiDestroyDeviceInfoList function deletes a device information set and frees all associated memory.
 | 
			
		||||
func (h DevInfo) Close() error {
 | 
			
		||||
	if h != DevInfo(windows.InvalidHandle) {
 | 
			
		||||
		return SetupDiDestroyDeviceInfoList(h)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	// SP_MAX_MACHINENAME_LENGTH defines maximum length of a machine name in the format expected by ConfigMgr32 CM_Connect_Machine (i.e., "\\\\MachineName\0").
 | 
			
		||||
	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 {
 | 
			
		||||
	Size                uint32
 | 
			
		||||
	ClassGUID           windows.GUID
 | 
			
		||||
	RemoteMachineHandle windows.Handle
 | 
			
		||||
	RemoteMachineName   [SP_MAX_MACHINENAME_LENGTH]uint16
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DevInfoListDetailData is a structure for detailed information on a device information set (used for SetupDiGetDeviceInfoListDetail which supercedes the functionality of SetupDiGetDeviceInfoListClass).
 | 
			
		||||
type DevInfoListDetailData struct {
 | 
			
		||||
	ClassGUID           windows.GUID
 | 
			
		||||
	RemoteMachineHandle windows.Handle
 | 
			
		||||
	RemoteMachineName   string
 | 
			
		||||
}
 | 
			
		||||
@ -39,12 +39,13 @@ func errnoErr(e syscall.Errno) error {
 | 
			
		||||
var (
 | 
			
		||||
	modsetupapi = windows.NewLazySystemDLL("setupapi.dll")
 | 
			
		||||
 | 
			
		||||
	procSetupDiGetClassDevsExW       = modsetupapi.NewProc("SetupDiGetClassDevsExW")
 | 
			
		||||
	procSetupDiDestroyDeviceInfoList = modsetupapi.NewProc("SetupDiDestroyDeviceInfoList")
 | 
			
		||||
	procSetupDiGetClassDevsExW          = modsetupapi.NewProc("SetupDiGetClassDevsExW")
 | 
			
		||||
	procSetupDiDestroyDeviceInfoList    = modsetupapi.NewProc("SetupDiDestroyDeviceInfoList")
 | 
			
		||||
	procSetupDiGetDeviceInfoListDetailW = modsetupapi.NewProc("SetupDiGetDeviceInfoListDetailW")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
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)
 | 
			
		||||
	if handle == DevInfo(windows.InvalidHandle) {
 | 
			
		||||
		if e1 != 0 {
 | 
			
		||||
@ -67,3 +68,15 @@ func SetupDiDestroyDeviceInfoList(DeviceInfoSet DevInfo) (err error) {
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 {
 | 
			
		||||
			err = errnoErr(e1)
 | 
			
		||||
		} else {
 | 
			
		||||
			err = syscall.EINVAL
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user