unsafe static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA lpDeviceInterfaceData, ref NativeDeviceInterfaceDetailData lpDeviceInterfaceDetail, int detailSize, ref int requiredSize, IntPtr bPtr);
static extern Boolean SetupDiGetDeviceInterfaceDetail( IntPtr hDevInfo, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref NativeDeviceInterfaceDetailData deviceInterfaceDetailData, UInt32 deviceInterfaceDetailDataSize, out UInt32 requiredSize, ref SP_DEVINFO_DATA deviceInfoData );
private bool CheckConnectivity() { // Get the HID class guid var guid = HidD_GetHidGuid(); // Get info about all connected HID devices var h = SetupApiInterop.SetupDiGetClassDevs(ref guid, IntPtr.Zero, IntPtr.Zero, (uint)DiGetClassFlags.DIGCF_DEVICEINTERFACE); if (h != INVALID_HANDLE_VALUE) { var success = true; UInt32 i = 1; while (success) { // Create the device interface data structure var dia = new SP_DEVICE_INTERFACE_DATA(); dia.cbSize = Marshal.SizeOf(dia); // Start the enumeration success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref guid, i, ref dia); if (success) { var da = new SP_DEVINFO_DATA(); da.cbSize = (uint)Marshal.SizeOf(da); // Build a Device Interface Detail Data structure var didd = new NativeDeviceInterfaceDetailData(); didd.size = 4 + Marshal.SystemDefaultCharSize; // now we can get some more detailed information uint nRequiredSize = 0; uint nBytes = (uint)BUFFER_SIZE; if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, out nRequiredSize, ref da)) { // current InstanceID is at the "USBSTOR" level, so we // need up "move up" one level to get to the "USB" level UInt32 ptrPrevious; CM_Get_Parent(out ptrPrevious, da.DevInst, 0); // Now we get the InstanceID of the USB level device IntPtr ptrInstanceBuf = Marshal.AllocHGlobal((int)nBytes); CM_Get_Device_ID(ptrPrevious, ptrInstanceBuf, (int)nBytes, 0); string InstanceID = Marshal.PtrToStringAuto(ptrInstanceBuf); Marshal.FreeHGlobal(ptrInstanceBuf); } i++; } } } SetupDiDestroyDeviceInfoList(h); return(false); }