Ejemplo n.º 1
0
        private static IEnumerable <SP_DEVICE_INTERFACE_DATA> SetupDiEnumDeviceInterfacesHelper(
            SafeDeviceInfoSetHandle lpDeviceInfoSet,
            SP_DEVINFO_DATA?deviceInfoData,
            Guid interfaceClassGuid)
        {
            int index = 0;

            while (true)
            {
                var data = SP_DEVICE_INTERFACE_DATA.Create();

                bool result = SetupDiEnumDeviceInterfaces(
                    lpDeviceInfoSet,
                    deviceInfoData,
                    ref interfaceClassGuid,
                    index,
                    ref data);

                if (!result)
                {
                    var lastError = GetLastError();
                    if (lastError != Win32ErrorCode.ERROR_NO_MORE_ITEMS)
                    {
                        throw new Win32Exception(lastError);
                    }

                    yield break;
                }

                yield return(data);

                index++;
            }
        }
        /// <summary>
        /// 获取 HID 设备路径列表
        /// </summary>
        /// <returns></returns>
        public static string[] GetHIDDevicesPath()
        {
            Guid HidGuid = Guid.Empty;

            HID.HidD_GetHidGuid(ref HidGuid);
            List <string> deviceList = new List <string>(64);

            IntPtr HidInfoSet = SetupAPI.SetupAPI.SetupDiGetClassDevs(ref HidGuid, IntPtr.Zero, IntPtr.Zero, DIGCF.PRESENT | DIGCF.DEVICEINTERFACE);

            if (HidInfoSet == IntPtr.Zero)
            {
                return(deviceList.ToArray());
            }

            bool hasNext;
            int  index = 0;
            SP_DEVICE_INTERFACE_DATA interfaceInfo = SP_DEVICE_INTERFACE_DATA.Create();

            do
            {
                hasNext = SetupAPI.SetupAPI.SetupDiEnumDeviceInterfaces(HidInfoSet, IntPtr.Zero, ref HidGuid, index, ref interfaceInfo);

                if (hasNext)
                {
                    int bufferSize = 0;
                    SetupAPI.SetupAPI.SetupDiGetDeviceInterfaceDetail(HidInfoSet, ref interfaceInfo, IntPtr.Zero, bufferSize, ref bufferSize, IntPtr.Zero);

                    IntPtr pDetail = Marshal.AllocHGlobal(bufferSize);
                    Marshal.WriteInt32(pDetail, (IntPtr.Size == 4) ? (Marshal.SystemDefaultCharSize + 4) : 8);
                    var result = SetupAPI.SetupAPI.SetupDiGetDeviceInterfaceDetail(HidInfoSet, ref interfaceInfo, pDetail, bufferSize, ref bufferSize, IntPtr.Zero);

                    if (result)
                    {
                        IntPtr ptr = new IntPtr(pDetail.ToInt32() + 4);
                        deviceList.Add(Marshal.PtrToStringAuto(ptr));
                        //Console.WriteLine(Marshal.PtrToStringAuto(ptr));
                        Marshal.FreeHGlobal(pDetail);
                    }

                    index++;
                }
            }while (hasNext);

            SetupAPI.SetupAPI.SetupDiDestroyDeviceInfoList(HidInfoSet);

            return(deviceList.ToArray());
        }