private static string GetDeviceName()
{
Guid gHid;
HidD_GetHidGuid(out gHid); // next, get the GUID from Windows that it uses to represent the HID USB interface
IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); // this gets a list of all HID devices currently connected to the computer (InfoSet)
try
{
uint SPDRP_DEVICEDESC = 0x00000000;
uint SPDRP_DRIVER = 0x00000009;
int BUFFER_SIZE = 256;
bool Success = true;
int i = 0;
while (Success)
{
// create a Device Interface Data structure
DeviceInterfaceData oInterface = new DeviceInterfaceData(); // build up a device interface data block
oInterface.Size = Marshal.SizeOf(oInterface);
// start the enumeration
Success = SetupDiEnumDeviceInterfaces(hInfoSet, IntPtr.Zero, ref gHid, (uint)i, ref oInterface);
if (Success)
{
// build a Device Interface Detail Data structure
DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();
oDetail.Size = (IntPtr.Size == 4) ? 5 : 8; // hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
DeviceInfoData da = new DeviceInfoData();
da.Size = (uint)Marshal.SizeOf(da);
// now we can get some more detailed information
uint nRequiredSize = 0;
int nBytes = BUFFER_SIZE;
//hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero;
if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, (uint)nBytes, ref nRequiredSize, ref da))
{
// get the Device Description and DriverKeyName
uint RequiredSize;
uint RegType;
byte[] ptrBuf = new byte[BUFFER_SIZE];
if (SetupDiGetDeviceRegistryProperty(hInfoSet, ref oInterface, SPDRP_DEVICEDESC, out RegType, ptrBuf, (uint)BUFFER_SIZE, out RequiredSize))
{
string ControllerDeviceDesc = System.Text.Encoding.UTF8.GetString(ptrBuf);// Marshal.PtrToStringAuto(ptrBuf);
}
if (SetupDiGetDeviceRegistryProperty(hInfoSet, ref oInterface, SPDRP_DRIVER, out RegType, ptrBuf, (uint)BUFFER_SIZE, out RequiredSize))
{
string ControllerDriverKeyName = System.Text.Encoding.UTF8.GetString(ptrBuf);
}
}
}
i++;
}
}
finally
{
// Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
SetupDiDestroyDeviceInfoList(hInfoSet);
}
return "";
}