示例#1
0
        private static IEnumerable <string> EnumerateDevicePath(Guid guid)
        {
            var devicePathList = new List <string>();
            var hDevInfo       = SetupApi.SetupDiGetClassDevs(ref guid, IntPtr.Zero, IntPtr.Zero, SetupApi.DIGCF_PRESENT | SetupApi.DIGCF_DEVICEINTERFACE);

            var spid = new SetupApi.SP_DEVICE_INTERFACE_DATA();

            spid.cbSize = Marshal.SizeOf(spid);
            int memberindex = 0;

            while (SetupApi.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, memberindex, ref spid))
            {
                int bufferSize = 0;
                SetupApi.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref spid, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero);

                var buffer = Marshal.AllocHGlobal(bufferSize);
                Marshal.WriteInt32(buffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);

                var da = new SetupApi.SP_DEVINFO_DATA();
                da.cbSize = Marshal.SizeOf(da);

                SetupApi.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref spid, buffer, bufferSize, ref bufferSize, ref da);
                IntPtr pDevicePathName = new IntPtr(buffer.ToInt64() + 4);
                string pathName        = Marshal.PtrToStringUni(pDevicePathName);

                Marshal.FreeHGlobal(buffer);
                buffer = IntPtr.Zero;
                memberindex++;

                Console.WriteLine(pathName);
                devicePathList.Add(pathName);
            }

            return(devicePathList);
        }
示例#2
0
        /// <summary>
        /// Gets a list of <see cref="WinUsbRegistry"/> classes for the specified interface guid.
        /// </summary>
        /// <param name="deviceInterfaceGuid">The DeviceInterfaceGUID to search for.</param>
        /// <param name="deviceRegistryList">A list of device paths associated with the <paramref name="deviceInterfaceGuid"/>.</param>
        /// <returns>True of one or more device paths was found.</returns>
        /// <remarks>
        /// Each <see cref="WinUsbRegistry"/> in the <paramref name="deviceRegistryList"/> represents a seperate WinUSB device (interface).
        /// </remarks>
        public static bool GetWinUsbRegistryList(Guid deviceInterfaceGuid, out List <WinUsbRegistry> deviceRegistryList)
        {
            deviceRegistryList = new List <WinUsbRegistry>();

            int devicePathIndex = 0;

            SetupApi.SP_DEVICE_INTERFACE_DATA    interfaceData = SetupApi.SP_DEVICE_INTERFACE_DATA.Empty;
            SetupApi.DeviceInterfaceDetailHelper detailHelper;

            SetupApi.SP_DEVINFO_DATA devInfoData = SetupApi.SP_DEVINFO_DATA.Empty;

            // [1]
            IntPtr deviceInfo = SetupApi.SetupDiGetClassDevs(ref deviceInterfaceGuid, null, IntPtr.Zero, SetupApi.DICFG.PRESENT | SetupApi.DICFG.DEVICEINTERFACE);

            if (deviceInfo != IntPtr.Zero)
            {
                while ((SetupApi.SetupDiEnumDeviceInterfaces(deviceInfo, null, ref deviceInterfaceGuid, devicePathIndex, ref interfaceData)))
                {
                    int length = 1024;
                    detailHelper = new SetupApi.DeviceInterfaceDetailHelper(length);
                    bool bResult = SetupApi.SetupDiGetDeviceInterfaceDetail(deviceInfo, ref interfaceData, detailHelper.Handle, length, out length, ref devInfoData);
                    if (bResult)
                    {
                        WinUsbRegistry regInfo = new WinUsbRegistry();

                        SetupApi.getSPDRPProperties(deviceInfo, ref devInfoData, regInfo.mDeviceProperties);

                        // Use the actual winusb device path for SYMBOLIC_NAME_KEY. This will be used to open the device.
                        regInfo.mDeviceProperties.Add(SYMBOLIC_NAME_KEY, detailHelper.DevicePath);
#if VERY_DEBUG
                        Debug.WriteLine(detailHelper.DevicePath);
#endif

                        regInfo.mDeviceInterfaceGuids = new Guid[] { deviceInterfaceGuid };

                        StringBuilder sbDeviceID = new StringBuilder(1024);
                        if (SetupApi.CM_Get_Device_ID(devInfoData.DevInst, sbDeviceID, sbDeviceID.Capacity, 0) == SetupApi.CR.SUCCESS)
                        {
                            regInfo.mDeviceProperties[DEVICE_ID_KEY] = sbDeviceID.ToString();
                        }
                        deviceRegistryList.Add(regInfo);
                    }

                    devicePathIndex++;
                }
            }
            if (devicePathIndex == 0)
            {
                UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetDevicePathList", typeof(SetupApi));
            }

            if (deviceInfo != IntPtr.Zero)
            {
                SetupApi.SetupDiDestroyDeviceInfoList(deviceInfo);
            }

            return(devicePathIndex > 0);
        }
示例#3
0
        public IEnumerable <HidDeviceInfo> CollectInfo()
        {
            var result = new List <HidDeviceInfo>();

            Guid hidGuid;

            Hid.HidD_GetHidGuid(out hidGuid);

            var deviceInfoList = SetupApi.SetupDiGetClassDevs(ref hidGuid, null, IntPtr.Zero,
                                                              Constants.DIGCF_DEVICEINTERFACE | Constants.DIGCF_PRESENT);

            if (deviceInfoList != IntPtr.Zero && deviceInfoList != Constants.INVALID_HANDLE_VALUE)
            {
                var deviceInfo = new SetupApi.SP_DEVICE_INTERFACE_DATA();
                deviceInfo.cbSize = Marshal.SizeOf(deviceInfo);

                for (var i = 0; ; i++)
                {
                    if (!SetupApi.SetupDiEnumDeviceInterfaces(deviceInfoList, 0, ref hidGuid, Convert.ToUInt32(i),
                                                              ref deviceInfo))
                    {
                        break;
                    }

                    var path = this.GetPath(deviceInfoList, deviceInfo);

                    var device = new HidDevice();

                    if (device.Open(path))
                    {
                        var attributes = device.GetAttributes();
                        var vendor     = device.GetVendorString();
                        var product    = device.GetProductString();

                        result.Add(new HidDeviceInfo(
                                       path,
                                       attributes.VendorID,
                                       attributes.ProductID,
                                       attributes.VersionString,
                                       vendor,
                                       product));

                        device.Close();
                    }
                }

                SetupApi.SetupDiDestroyDeviceInfoList(deviceInfoList);
            }

            return(result);
        }
示例#4
0
        private string GetPath(IntPtr deviceInfoList, SetupApi.SP_DEVICE_INTERFACE_DATA deviceInfo)
        {
            var deviceInterfaceDetailData = IntPtr.Zero;

            uint needed;

            if (!SetupApi.SetupDiGetDeviceInterfaceDetail(deviceInfoList, ref deviceInfo,
                                                          IntPtr.Zero, 0,
                                                          out needed, IntPtr.Zero))
            {
                var error = Marshal.GetLastWin32Error();

                if (error != 122)
                {
                    return(string.Empty);
                }
            }


            var spDeviceInterfaceData = new SetupApi.SP_DEVICE_INTERFACE_DATA();

            spDeviceInterfaceData.cbSize = Marshal.SizeOf(spDeviceInterfaceData);

            try {
                deviceInterfaceDetailData = Marshal.AllocHGlobal((int)(8 + needed));
                var size = IntPtr.Size == 8 ? 8 : 6;
                Marshal.WriteInt32(deviceInterfaceDetailData, size);
                if (!SetupApi.SetupDiGetDeviceInterfaceDetail(deviceInfoList, ref deviceInfo,
                                                              deviceInterfaceDetailData, needed, out needed, IntPtr.Zero))
                {
                    var error = Marshal.GetLastWin32Error();
                }

                var pStr = IntPtr.Add(deviceInterfaceDetailData, sizeof(uint));
                var path = Marshal.PtrToStringUni(pStr);

                return(path);
            } finally {
                Marshal.FreeHGlobal(deviceInterfaceDetailData);
            }
        }
示例#5
0
        /// <summary>
        /// Gets a list of WinUSB device paths for the specified interface guid.
        /// </summary>
        /// <param name="deviceInterfaceGuid">The DeviceInterfaceGUID to search for.</param>
        /// <param name="devicePathList">A list of device paths associated with the <paramref name="deviceInterfaceGuid"/>.</param>
        /// <returns>True of one or more device paths was found.</returns>
        /// <remarks>
        /// Each device path string in the <paramref name="devicePathList"/> represents a seperate WinUSB device (interface).
        /// </remarks>
        /// <seealso cref="GetWinUsbRegistryList"/>
        public static bool GetDevicePathList(Guid deviceInterfaceGuid, out List <String> devicePathList)
        {
            devicePathList = new List <string>();
            int devicePathIndex = 0;

            SetupApi.SP_DEVICE_INTERFACE_DATA    interfaceData = SetupApi.SP_DEVICE_INTERFACE_DATA.Empty;
            SetupApi.DeviceInterfaceDetailHelper detailHelper;

            IntPtr deviceInfo = SetupApi.SetupDiGetClassDevs(ref deviceInterfaceGuid, null, IntPtr.Zero, SetupApi.DICFG.PRESENT | SetupApi.DICFG.DEVICEINTERFACE);

            if (deviceInfo != IntPtr.Zero)
            {
                while ((SetupApi.SetupDiEnumDeviceInterfaces(deviceInfo, null, ref deviceInterfaceGuid, devicePathIndex, ref interfaceData)))
                {
                    int length = 1024;
                    detailHelper = new SetupApi.DeviceInterfaceDetailHelper(length);
                    bool bResult = SetupApi.SetupDiGetDeviceInterfaceDetail(deviceInfo, ref interfaceData, detailHelper.Handle, length, out length, null);
                    if (bResult)
                    {
                        devicePathList.Add(detailHelper.DevicePath);
                    }

                    devicePathIndex++;
                }
            }
            if (devicePathIndex == 0)
            {
                UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetDevicePathList", typeof(SetupApi));
            }

            if (deviceInfo != IntPtr.Zero)
            {
                SetupApi.SetupDiDestroyDeviceInfoList(deviceInfo);
            }

            return(devicePathIndex > 0);
        }