SetupDiGetDeviceRegistryProperty(,,SPDRP_xxx,,,)的结果

本文深入探讨了USB人体学输入设备的属性获取方法,通过使用Windows API函数,展示了如何枚举设备并获取其详细信息,如设备描述、硬件ID、兼容ID等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDEVINFO hDevInfo = SetupDiGetClassDevs(0, "USB", 0, DIGCF_ALLCLASSES | DIGCF_PRESENT)

DWORD dwIndex = 0;
SP_DEVINFO_DATA spDevInfoData = {0};
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
SetupDiEnumDeviceInfo(hDevInfo, dwIndex++, &spDevInfoData)

SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_xxx,,,)  
#define SPDRP_DEVICEDESC                  (0x00000000)  // DeviceDesc (R/W)               
"USB 人体学输入设备" 
#define SPDRP_HARDWAREID                  (0x00000001)  // HardwareID (R/W)               
"USB\Vid_09da&Pid_000a&Rev_0002" 
#define SPDRP_COMPATIBLEIDS               (0x00000002)  // CompatibleIDs (R/W)            
"USB\Class_03&SubClass_01&Prot_02" 
#define SPDRP_UNUSED0                     (0x00000003)  // unused                          "" 
#define SPDRP_SERVICE                     (0x00000004)  // Service (R/W)                  
"HidUsb" 
#define SPDRP_UNUSED1                     (0x00000005)  // unused                          "" 
#define SPDRP_UNUSED2                     (0x00000006)  // unused                          "" 
#define SPDRP_CLASS                       (0x00000007)  // Class (R--tied to ClassGUID)   
"HIDClass" 
#define SPDRP_CLASSGUID                   (0x00000008)  // ClassGUID (R/W)                
"{745A17A0-74D3-11D0-B6FE-00A0C90F57DA}" 
#define SPDRP_DRIVER                      (0x00000009)  // Driver (R/W)                   
"{745A17A0-74D3-11D0-B6FE-00A0C90F57DA}\0008" 
#define SPDRP_CONFIGFLAGS                 (0x0000000A)  // ConfigFlags (R/W)               "" 
#define SPDRP_MFG                         (0x0000000B)  // Mfg (R/W)                      
"(标准系统设备)" 
#define SPDRP_FRIENDLYNAME                (0x0000000C)  // FriendlyName (R/W)              "" 
#define SPDRP_LOCATION_INFORMATION        (0x0000000D)  // LocationInformation (R/W)      
"PS/2+USB Mouse" 
#define SPDRP_PHYSICAL_DEVICE_OBJECT_NAME (0x0000000E)  // PhysicalDeviceObjectName (R)   
"\Device\USBPDO-8" 
#define SPDRP_CAPABILITIES                (0x0000000F)  // Capabilities (R)                "" 
#define SPDRP_UI_NUMBER                   (0x00000010)  // UiNumber (R)                    "" 
#define SPDRP_UPPERFILTERS                (0x00000011)  // UpperFilters (R/W)              "" 
#define SPDRP_LOWERFILTERS                (0x00000012)  // LowerFilters (R/W)              "" 
#define SPDRP_BUSTYPEGUID                 (0x00000013)  // BusTypeGUID (R)                
{bceb7d9d-5dc8-d111-9eb4-006008c3a19a} 
#define SPDRP_LEGACYBUSTYPE               (0x00000014)  // LegacyBusType (R)              
0f 
#define SPDRP_BUSNUMBER                   (0x00000015)  // BusNumber (R)                   "" 
#define SPDRP_ENUMERATOR_NAME             (0x00000016)  // Enumerator Name (R)            
"USB" 
#define SPDRP_SECURITY                    (0x00000017)  // Security (R/W, binary form)     "" 
#define SPDRP_SECURITY_SDS                (0x00000018)  // Security (W, SDS form)          "" 
#define SPDRP_DEVTYPE                     (0x00000019)  // Device Type (R/W)               "" 
#define SPDRP_EXCLUSIVE                   (0x0000001A)  // Device is exclusive-access (R/W)"" 
#define SPDRP_CHARACTERISTICS             (0x0000001B)  // Device Characteristics (R/W)    "" 
#define SPDRP_ADDRESS                     (0x0000001C)  // Device Address (R)             
01 
#define SPDRP_UI_NUMBER_DESC_FORMAT       (0X0000001D)  // UiNumberDescFormat (R/W)        "" 
#define SPDRP_DEVICE_POWER_DATA           (0x0000001E)  // Device Power Data (R)          
"8" 
#define SPDRP_REMOVAL_POLICY              (0x0000001F)  // Removal Policy (R)             
03 
#define SPDRP_REMOVAL_POLICY_HW_DEFAULT   (0x00000020)  // Hardware Removal Policy (R)    
03 
#define SPDRP_REMOVAL_POLICY_OVERRIDE     (0x00000021)  // Removal Policy Override (RW)    "" 
#define SPDRP_INSTALL_STATE               (0x00000022)  // Device Install State (R)        "" 
#define SPDRP_LOCATION_PATHS              (0x00000023)  // Device Location Paths (R)       "" 
#define SPDRP_BASE_CONTAINERID            (0x00000024)  // Base ContainerID (R)            "" 

在C#中使用SetupApi获取SPDRP_LOCATION_PATHS的步骤如下: 1. 首先要导入`SetupApi`相关的命名空间: ```csharp using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32.SafeHandles; ``` 2. 定义`SetupApi`中相关的结构体和常量: ```csharp private const int DIGCF_ALLCLASSES = 0x00000004; private const int DIGCF_PRESENT = 0x00000002; private const int SPDRP_LOCATION_PATHS = 0x0015; private const int MAX_DEVICE_ID_LEN = 200; [StructLayout(LayoutKind.Sequential)] public struct SP_DEVINFO_DATA { public int cbSize; public Guid ClassGuid; public int DevInst; public IntPtr Reserved; } [StructLayout(LayoutKind.Sequential)] public struct SP_DEVICE_INTERFACE_DATA { public int cbSize; public Guid InterfaceClassGuid; public int Flags; public IntPtr Reserved; } [StructLayout(LayoutKind.Sequential)] public struct SP_DEVICE_INTERFACE_DETAIL_DATA { public int cbSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string DevicePath; } [DllImport("setupapi.dll", SetLastError = true)] public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, int Flags); [DllImport("setupapi.dll", SetLastError = true)] public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, int MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData); [DllImport("setupapi.dll", SetLastError = true)] public static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref Guid InterfaceClassGuid, int MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData); [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr DeviceInfoData); [DllImport("setupapi.dll", SetLastError = true)] public static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, int Property, out int PropertyRegDataType, StringBuilder PropertyBuffer, int PropertyBufferSize, out int RequiredSize); ``` 3. 实现获取设备路径的方法`GetDevicePath`: ```csharp public static string GetDevicePath(Guid classGuid) { IntPtr deviceInfoSet = SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_ALLCLASSES | DIGCF_PRESENT); if (deviceInfoSet == IntPtr.Zero) { return null; } SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)); int memberIndex = 0; while (SetupDiEnumDeviceInfo(deviceInfoSet, memberIndex, ref deviceInfoData)) { StringBuilder deviceInstanceId = new StringBuilder(MAX_DEVICE_ID_LEN); if (SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, SPDRP_LOCATION_PATHS, out _, deviceInstanceId, MAX_DEVICE_ID_LEN, out _)) { return deviceInstanceId.ToString(); } memberIndex++; } SetupDiDestroyDeviceInfoList(deviceInfoSet); return null; } ``` 4. 调用`GetDevicePath`方法获取设备路径: ```csharp Guid classGuid = new Guid("{4d36e978-e325-11ce-bfc1-08002be10318}"); // USB设备类GUID string devicePath = GetDevicePath(classGuid); ``` 以上就是在C#中使用SetupApi获取SPDRP_LOCATION_PATHS的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值