namespace DeviceEnumerator
{
using System;
using System.Runtime.InteropServices;
public class SetupAPI
{
[DllImport("hid.dll", SetLastError=true)]
public static extern unsafe void HidD_GetHidGuid(
ref Guid lpHidGuid
);
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiGetClassDevs(
ref Guid lpGuid,
int* Enumerator,
int* hwndParent,
ClassDevsFlags Flags
);
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiGetClassDevs(
int* guid,
int* Enumerator,
int* hwndParent,
ClassDevsFlags Flags
);
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public int Reserved;
}
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiEnumDeviceInfo(
int DeviceInfoSet,
int Index,
ref SP_DEVINFO_DATA DeviceInfoData
);
[Flags]
public enum ClassDevsFlags
{
DIGCF_DEFAULT = 0x00000001,
DIGCF_PRESENT = 0x00000002,
DIGCF_ALLCLASSES = 0x00000004,
DIGCF_PROFILE = 0x00000008,
DIGCF_DEVICEINTERFACE = 0x00000010,
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public int Flags;
public int Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet= CharSet.Ansi)]
public unsafe struct PSP_DEVICE_INTERFACE_DETAIL_DATA
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= 256)]
public string DevicePath;
}
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiEnumDeviceInterfaces(
int DeviceInfoSet,
int DeviceInfoData,
ref Guid lpHidGuid,
int MemberIndex,
ref SP_DEVICE_INTERFACE_DATA lpDeviceInterfaceData);
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiGetDeviceInterfaceDetail(
int DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA lpDeviceInterfaceData,
int* aPtr,
int detailSize,
ref int requiredSize,
int* bPtr);
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiGetDeviceInterfaceDetail(
int DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA lpDeviceInterfaceData,
ref PSP_DEVICE_INTERFACE_DETAIL_DATA myPSP_DEVICE_INTERFACE_DETAIL_DATA,
int detailSize,
ref int requiredSize,
int* bPtr);
public enum RegPropertyType
{
SPDRP_DEVICEDESC = 0x00000000,
SPDRP_HARDWAREID = 0x00000001,
SPDRP_COMPATIBLEIDS = 0x00000002,
SPDRP_UNUSED0 = 0x00000003,
SPDRP_SERVICE = 0x00000004,
SPDRP_UNUSED1 = 0x00000005,
SPDRP_UNUSED2 = 0x00000006,
SPDRP_CLASS = 0x00000007,
SPDRP_CLASSGUID = 0x00000008,
SPDRP_DRIVER = 0x00000009,
SPDRP_CONFIGFLAGS = 0x0000000A,
SPDRP_MFG = 0x0000000B,
SPDRP_FRIENDLYNAME = 0x0000000C,
SPDRP_LOCATION_INFORMATION = 0x0000000D ,
SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E,
SPDRP_CAPABILITIES = 0x0000000F,
SPDRP_UI_NUMBER = 0x00000010,
SPDRP_UPPERFILTERS = 0x00000011,
SPDRP_LOWERFILTERS = 0x00000012,
SPDRP_BUSTYPEGUID = 0x00000013,
SPDRP_LEGACYBUSTYPE = 0x00000014,
SPDRP_BUSNUMBER = 0x00000015,
SPDRP_ENUMERATOR_NAME = 0x00000016,
SPDRP_SECURITY = 0x00000017,
SPDRP_SECURITY_SDS = 0x00000018,
SPDRP_DEVTYPE = 0x00000019,
SPDRP_EXCLUSIVE = 0x0000001A,
SPDRP_CHARACTERISTICS = 0x0000001B,
SPDRP_ADDRESS = 0x0000001C,
SPDRP_UI_NUMBER_DESC_FORMAT = 0x0000001E,
SPDRP_MAXIMUM_PROPERTY = 0x0000001F
}
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiGetDeviceRegistryProperty(
int DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
RegPropertyType Property,
int* PropertyRegDataType,
int* PropertyBuffer,
int PropertyBufferSize,
ref int RequiredSize
);
[StructLayout(LayoutKind.Sequential, CharSet= CharSet.Ansi)]
public unsafe struct DATA_BUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst= 1024)]
public string Buffer;
}
[DllImport("setupapi.dll", SetLastError=true)]
public static extern unsafe int SetupDiGetDeviceRegistryProperty(
int DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
RegPropertyType Property,
int* PropertyRegDataType,
ref DATA_BUFFER PropertyBuffer,
int PropertyBufferSize,
ref int RequiredSize
);
}
}
//main class
using System;
using System.Runtime.InteropServices;
namespace DeviceEnumerator
{
class Class1
{
[STAThread]
static unsafe void Main(string[] args)
{
Guid guid = new Guid("{36FC9E60-C465-11CF-8056-444553540000}");
int PnPHandle = SetupAPI.SetupDiGetClassDevs(
ref guid,
null,
null,
SetupAPI.ClassDevsFlags.DIGCF_PRESENT
);
int result = -1;
int DeviceIndex = 0;
while (result != 0)
{
SetupAPI.SP_DEVINFO_DATA DeviceInfoData = new SetupAPI.SP_DEVINFO_DATA();
DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData);
result = SetupAPI.SetupDiEnumDeviceInfo(PnPHandle, DeviceIndex, ref DeviceInfoData);
if (result == 1)
{
Console.WriteLine("{0}:\n\t{1}\n\t{2}\n\t{3}\n\t{4}",
GetRegistryProperty(PnPHandle, ref DeviceInfoData, SetupAPI.RegPropertyType.SPDRP_DEVICEDESC),
GetRegistryProperty(PnPHandle, ref DeviceInfoData, SetupAPI.RegPropertyType.SPDRP_CLASS ),
GetRegistryProperty(PnPHandle, ref DeviceInfoData, SetupAPI.RegPropertyType.SPDRP_CLASSGUID ),
GetRegistryProperty(PnPHandle, ref DeviceInfoData, SetupAPI.RegPropertyType.SPDRP_DRIVER ),
GetRegistryProperty(PnPHandle, ref DeviceInfoData, SetupAPI.RegPropertyType.SPDRP_MFG )
);
}
DeviceIndex++;
}
Console.ReadLine();
}
public unsafe static string GetRegistryProperty(int PnPHandle, ref SetupAPI.SP_DEVINFO_DATA DeviceInfoData, SetupAPI.RegPropertyType Property)
{
int RequiredSize = 0;
SetupAPI.DATA_BUFFER Buffer = new SetupAPI.DATA_BUFFER();
int result = SetupAPI.SetupDiGetDeviceRegistryProperty(
PnPHandle,
ref DeviceInfoData,
Property,
null,
ref Buffer,
1024,
ref RequiredSize
);
return Buffer.Buffer;
}
}
}