SP_DEVICE_INTERFACE_DATA structure (转)

SP_DEVICE_INTERFACE_DATA structure

 

An SP_DEVICE_INTERFACE_DATA structure defines a device interface in a device information set.

Syntax

C++
 typedef struct _SP_DEVICE_INTERFACE_DATA { DWORD     cbSize; GUID      InterfaceClassGuid; DWORD     Flags; ULONG_PTR Reserved; } SP_DEVICE_INTERFACE_DATA, *PSP_DEVICE_INTERFACE_DATA;

Members

cbSize

The size, in bytes, of the SP_DEVICE_INTERFACE_DATA structure. For more information, see the Remarks section.

InterfaceClassGuid

The GUID for the class to which the device interface belongs.

Flags

Can be one or more of the following:

 

SPINT_ACTIVE

The interface is active (enabled).

SPINT_DEFAULT

The interface is the default interface for the device class.

SPINT_REMOVED

The interface is removed.

Reserved

Reserved. Do not use.

Remarks

A SetupAPI function that takes an instance of the SP_DEVICE_INTERFACE_DATA structure as a parameter verifies whether the cbSize member of the supplied structure is equal to the size, in bytes, of the structure. If the cbSize member is not set correctly, the function will fail and set an error code of ERROR_INVALID_USER_BUFFER.

Requirements

在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的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值