SetupApi引入
internal class SetupAPI
{
// Flags for CM_Locate_DevNode
public const uint CM_LOCATE_DEVNODE_NORMAL = 0x00000000;
public const uint CM_LOCATE_DEVNODE_PHANTOM = 0x00000001;
public const uint CM_LOCATE_DEVNODE_CANCELREMOVE = 0x00000002;
public const uint CM_LOCATE_DEVNODE_NOVALIDATION = 0x00000004;
public const uint CM_LOCATE_DEVNODE_BITS = 0x00000007;
// Flags for CM_Disable_DevNode
public const uint CM_DISABLE_POLITE = 0x00000000;
public const uint CM_DISABLE_ABSOLUTE = 0x00000001;
public const uint CM_DISABLE_HARDWARE = 0x00000002;
public const uint CM_DISABLE_UI_NOT_OK = 0x00000004;
public const uint CM_DISABLE_PERSIST = 0x00000008;
public const uint CM_DISABLE_BITS = 0x0000000F;
// Flags for CM_Query_And_Remove_SubTree
public const uint CM_REMOVE_UI_OK = 0x00000000;
public const uint CM_REMOVE_UI_NOT_OK = 0x00000001;
public const uint CM_REMOVE_NO_RESTART = 0x00000002;
public const uint CM_REMOVE_BITS = 0x00000003;
// 过滤设备,获取需要的设备
[DllImport("setupapi.dll")]
public static extern IntPtr SetupDiGetClassDevs(ref Guid classGuid, string Enumerator, IntPtr hwndParent, uint Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Unicode)]
public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, uint MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll")]
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property, uint PropertyRegDataType, StringBuilder PropertyBuffer, uint PropertyBufferSize, IntPtr RequiredSize);
[DllImport("setupapi.dll")]
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property, uint PropertyRegDataType, byte[] PropertyBuffer, uint PropertyBufferSize, IntPtr RequiredSize);
[DllImport("setupapi.dll", CharSet = CharSet.Unicode)]
public static extern bool SetupDiGetDeviceInstanceId(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, StringBuilder DeviceInstanceId, int DeviceInstanceIdSize, out int RequiredSize);
[DllImport("setupapi.dll")]
public static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
}
调用API获取设备的信息:
Guid myGuid = Guid.Empty;
//Guid myGuid = new Guid("36fc9e60-c465-11cf-8056-444553540000");
IntPtr NewDeviceInfoSet;
// 通过类型获取设备信息
NewDeviceInfoSet = SetupAPI.SetupDiGetClassDevs(ref myGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (NewDeviceInfoSet.ToInt32() == INVALID_HANDLE_VALUE)
{
logError.Error("设备不可用");
return;
}
SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA
{
cbSize = 28,
// 正常状态
DevInst = 0,
ClassGuid = Guid.Empty,
Reserved = 0
};
uint i;
for (i = 0; SetupAPI.SetupDiEnumDeviceInfo(NewDeviceInfoSet, i, ref deviceInfoData); i++)
{
//Thread.Sleep(50);
// 位置路径
byte[] locationPath = new byte[MAX_DEV_LEN];
SetupAPI.SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref deviceInfoData, SPDRP_LOCATION_PATHS, 0, locationPath, MAX_DEV_LEN, IntPtr.Zero);
// port 位置
StringBuilder location = new StringBuilder();
location.Capacity = MAX_DEV_LEN;
SetupAPI.SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref deviceInfoData, SPDRP_LOCATION_INFORMATION, 0, location, MAX_DEV_LEN, IntPtr.Zero);
StringBuilder services = new StringBuilder();
services.Capacity = MAX_DEV_LEN;
SetupAPI.SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref deviceInfoData, SPDRP_SERVICE, 0, services, MAX_DEV_LEN, IntPtr.Zero);
//if (Encoding.UTF8.GetString(locationPath).IndexOf("PCIROOT") != -1 && //!string.IsNullOrWhiteSpace(Convert.ToString(location)) && //!string.IsNullOrWhiteSpace(Convert.ToString(services)) && //!ExceptService(Convert.ToString(services)))
//{
StringBuilder DeviceType = new StringBuilder();
DeviceType.Capacity = MAX_DEV_LEN;
SetupAPI.SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref deviceInfoData, SPDRP_CLASS, 0, DeviceType, MAX_DEV_LEN, IntPtr.Zero);
StringBuilder deviceInstanceId = new StringBuilder(256);
int requiredSize = 0;
SetupAPI.SetupDiGetDeviceInstanceId(NewDeviceInfoSet, ref deviceInfoData, deviceInstanceId, deviceInstanceId.Capacity, out requiredSize);
//logInfo.Info($"type = {DeviceType}");
//logInfo.Info($"ID = {deviceInstanceId.ToString().Split(new char[] { '\\' })[deviceInstanceId.ToString().Split(new char[] { '\\' }).Length - 1]}");
//logInfo.Info($"location = {location}");
//logInfo.Info($"locationpath = {Encoding.UTF8.GetString(locationPath)}");
//logInfo.Info($"service = {services}");
//logInfo.Info($"classguid={classGuid}");
//logInfo.Info($"bustypeGuid={bustypeGuid}");
//}
}
//释放当前设备占用内存
SetupAPI.SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
获取到的设备信息可以通过USBTreeView工具来一一验证。