Example #1
0
    private static string GetDevicePath(IntPtr hInfoSet, ref Win32Usb.DeviceInterfaceData oInterface)
    {
        uint nRequiredSize = 0;

        // Get the device interface details
        if (!Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
        {
            Win32Usb.DeviceInterfaceDetailData oDetail = new Win32Usb.DeviceInterfaceDetailData();
            oDetail.Size = 5;   // 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
            if (Win32Usb.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
            {
                return(oDetail.DevicePath);
            }
        }
        return(null);
    }
Example #2
0
    public static string FindDevicePath(int nVid, int nPid)
    {
        //string strPath = string.Empty;
        string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string
        Guid   gHid      = Win32Usb.HIDGuid;
        //HidD_GetHidGuid(out gHid);	// next, get the GUID from Windows that it uses to represent the HID USB interface
        IntPtr hInfoSet = Win32Usb.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, (uint)(Win32Usb.DIGCF_DEVICEINTERFACE | Win32Usb.DIGCF_PRESENT));   // this gets a list of all HID devices currently connected to the computer (InfoSet)

        try
        {
            Win32Usb.DeviceInterfaceData oInterface = new Win32Usb.DeviceInterfaceData();       // build up a device interface data block
            oInterface.Size = Marshal.SizeOf(oInterface);
            // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
            // to get device details for each device connected
            int nIndex = 0;
            while (Win32Usb.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface)) // this gets the device interface information for a device at index 'nIndex' in the memory block
            {
                string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);                               // get the device path (see helper method 'GetDevicePath')

                if (strDevicePath.IndexOf(strSearch) >= 0)                                                    // do a string search, if we find the VID/PID string then we found our device!
                {
                    return(strDevicePath);
                }
                nIndex++;       // if we get here, we didn't find our device. So move on to the next one.
            }
        }
        catch (Exception ex)
        {
            Debug.LogWarning(string.Format("[警告]打开usbhid设备错误 :{0}WinError:{1:X8}", ex.ToString(), Marshal.GetLastWin32Error()));
            //Console.WriteLine(ex.ToString());
        }
        finally
        {
            // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
            Win32Usb.SetupDiDestroyDeviceInfoList(hInfoSet);
        }
        return("");      // oops, didn't find our device
    }
Example #3
0
    public static string FindDevicePath(int nVid, int nPid)
    {
        //string strPath = string.Empty;
        string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string
        Guid gHid = Win32Usb.HIDGuid;
        //HidD_GetHidGuid(out gHid);	// next, get the GUID from Windows that it uses to represent the HID USB interface
        IntPtr hInfoSet = Win32Usb.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, (uint)(Win32Usb.DIGCF_DEVICEINTERFACE | Win32Usb.DIGCF_PRESENT));	// this gets a list of all HID devices currently connected to the computer (InfoSet)
        try
        {
            Win32Usb.DeviceInterfaceData oInterface = new Win32Usb.DeviceInterfaceData();	// build up a device interface data block
            oInterface.Size = Marshal.SizeOf(oInterface);
            // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
            // to get device details for each device connected
            int nIndex = 0;
            while (Win32Usb.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)nIndex, ref oInterface))	// this gets the device interface information for a device at index 'nIndex' in the memory block
            {

                string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);	// get the device path (see helper method 'GetDevicePath')

                if (strDevicePath.IndexOf(strSearch) >= 0)	// do a string search, if we find the VID/PID string then we found our device!
                {
                    return strDevicePath;
                }
                nIndex++;	// if we get here, we didn't find our device. So move on to the next one.
            }
        }
        catch (Exception ex)
        {

            Debug.LogWarning(string.Format("[����]��usbhid�豸���� :{0}WinError:{1:X8}", ex.ToString(),Marshal.GetLastWin32Error()));
            //Console.WriteLine(ex.ToString());
        }
        finally
        {
            // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
            Win32Usb.SetupDiDestroyDeviceInfoList(hInfoSet);
        }
        return "";	// oops, didn't find our device
    }