public static extern bool SetupDiGetDeviceInterfaceDetail( IntPtr deviceInfoSet, ref SpDeviceInterfaceData deviceInterfaceData, IntPtr deviceInterfaceDetailData, Int32 deviceInterfaceDetailDataSize, ref Int32 requiredSize, IntPtr deviceInfoData);
public static bool FindHidDevices(ref string[] listOfDevicePathNames, ref int numberOfDevicesFound) { Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Method called"); // Initialise the internal variables required for performing the search var bufferSize = 0; var detailDataBuffer = IntPtr.Zero; bool deviceFound; var deviceInfoSet = new IntPtr(); var lastDevice = false; int listIndex; var deviceInterfaceData = new SpDeviceInterfaceData(); // Get the required GUID var systemHidGuid = new Guid(); Hid.HidD_GetHidGuid(ref systemHidGuid); Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Fetched GUID for HID devices ({0})", systemHidGuid.ToString()); try { // Here we populate a list of plugged-in devices matching our class GUID (DIGCF_PRESENT specifies that the list // should only contain devices which are plugged in) Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Using SetupDiGetClassDevs to get all devices with the correct GUID"); deviceInfoSet = SetupApi.SetupDiGetClassDevs(ref systemHidGuid, IntPtr.Zero, IntPtr.Zero, Constants.DigcfPresent | Constants.DigcfDeviceinterface); // Reset the deviceFound flag and the memberIndex counter deviceFound = false; listIndex = 0; deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); // Look through the retrieved list of class GUIDs looking for a match on our interface GUID do { //Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Enumerating devices"); var success = SetupApi.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref systemHidGuid, listIndex, ref deviceInterfaceData); if (!success) { //Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> No more devices left - giving up"); lastDevice = true; } else { // The target device has been found, now we need to retrieve the device path so we can open // the read and write handles required for USB communication // First call is just to get the required buffer size for the real request SetupApi.SetupDiGetDeviceInterfaceDetail (deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero); // Allocate some memory for the buffer detailDataBuffer = Marshal.AllocHGlobal(bufferSize); Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8); // Second call gets the detailed data buffer //Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Getting details of the device"); SetupApi.SetupDiGetDeviceInterfaceDetail (deviceInfoSet, ref deviceInterfaceData, detailDataBuffer, bufferSize, ref bufferSize, IntPtr.Zero); // Skip over cbsize (4 bytes) to get the address of the devicePathName. var pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4); // Get the String containing the devicePathName. listOfDevicePathNames[listIndex] = Marshal.PtrToStringAuto(pDevicePathName); //Debug.WriteLine(string.Format("usbGenericHidCommunication:findHidDevices() -> Found matching device (memberIndex {0})", memberIndex)); deviceFound = true; } listIndex = listIndex + 1; } while (lastDevice != true); } catch (Exception) { // Something went badly wrong... output some debug and return false to indicated device discovery failure Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> EXCEPTION: Something went south whilst trying to get devices with matching GUIDs - giving up!"); return false; } finally { // Clean up the unmanaged memory allocations if (detailDataBuffer != IntPtr.Zero) { // Free the memory allocated previously by AllocHGlobal. Marshal.FreeHGlobal(detailDataBuffer); } if (deviceInfoSet != IntPtr.Zero) { SetupApi.SetupDiDestroyDeviceInfoList(deviceInfoSet); } } if (deviceFound) { Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> Found {0} devices with matching GUID", (listIndex - 1).ToString(CultureInfo.InvariantCulture)); numberOfDevicesFound = listIndex - 2; } else Debug.WriteLine("usbGenericHidCommunication:findHidDevices() -> No matching devices found"); return deviceFound; }
public static extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, IntPtr deviceInfoData, ref Guid hidGuid, Int32 memberIndex, ref SpDeviceInterfaceData deviceInterfaceData);