Beispiel #1
0
        /// <summary>
        /// Helper method to return the device path given a DeviceInterfaceData structure and an InfoSet handle.
        /// Used in 'FindDevice' so check that method out to see how to get an InfoSet handle and a DeviceInterfaceData.
        /// </summary>
        /// <param name="infoSetPointer">Handle to the InfoSet</param>
        /// <param name="deviceInterfaceData">DeviceInterfaceData structure</param>
        /// <returns>The device path or null if there was some problem</returns>
        internal static string GetDevicePath(IntPtr infoSetPointer, ref Win32USB.DeviceInterfaceData deviceInterfaceData)
        {
            uint requiredSize = 0;

            // Get the device interface details
            if (Win32USB.SetupDiGetDeviceInterfaceDetail(infoSetPointer, ref deviceInterfaceData, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero) == false)
            {
                var deviceInterfaceDetailData = new Win32USB.DeviceInterfaceDetailData();

                if (IntPtr.Size == 8)
                {
                    deviceInterfaceDetailData.Size = 8;
                }
                else
                {
                    deviceInterfaceDetailData.Size = 5;
                }

                if (Win32USB.SetupDiGetDeviceInterfaceDetail(infoSetPointer, ref deviceInterfaceData, ref deviceInterfaceDetailData, requiredSize, ref requiredSize, IntPtr.Zero))
                {
                    return(deviceInterfaceDetailData.DevicePath);
                }
            }

            return(null);
        }
Beispiel #2
0
        public static IEnumerable <DeviceInfo> GetDevices(ushort vendorID, ushort productID)
        {
            Guid gHid = HID.Guid;

            IntPtr infoSetPointer = Win32USB.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, Win32USB.DIGCF_DEVICEINTERFACE | Win32USB.DIGCF_PRESENT);

            try
            {
                var deviceInterfaceData = new Win32USB.DeviceInterfaceData();
                deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData);

                int index = 0;

                while (Win32USB.SetupDiEnumDeviceInterfaces(infoSetPointer, 0, ref gHid, (uint)index, ref deviceInterfaceData))
                {
                    string devicePath = GetDevicePath(infoSetPointer, ref deviceInterfaceData);

                    if (devicePath != null)
                    {
                        if (vendorID == 0 && productID == 0)
                        {
                            yield return(new DeviceInfo(devicePath));
                        }
                        else
                        {
                            if (DeviceInfo.ParseDevicePath(devicePath, out ushort localVendorID, out ushort localProductID, out uint localIdentifier) &&
                                localVendorID == vendorID && localProductID == productID)
                            {
                                yield return(new DeviceInfo(devicePath));
                            }
                        }
                    }

                    index++;
                }
            }
            finally
            {
                Win32USB.SetupDiDestroyDeviceInfoList(infoSetPointer);
            }
        }