Beispiel #1
0
        private void EnumerateDevices()
        {
            List <HidDevice> NewList = new List <HidDevice>();

            // Platform Device Enumeration

            int index = 0;

            NativeApi.SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;

            using (DeviceInfoHandle DeviceHandle = NativeApi.SetupDiGetClassDevs(ref HidGuid, null, 0,
                                                                                 NativeApi.DIGCF_DEVICEINTERFACE | NativeApi.DIGCF_PRESENT))
            {
                DeviceInterfaceData        = new NativeApi.SP_DEVICE_INTERFACE_DATA();
                DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData);

                while (NativeApi.SetupDiEnumDeviceInterfaces(DeviceHandle, 0, ref HidGuid, index++, ref DeviceInterfaceData))
                {
                    int requiredSize = 0;

                    NativeApi.SetupDiGetDeviceInterfaceDetail(DeviceHandle, ref DeviceInterfaceData, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero);

                    IntPtr DetailDataBuffer = Marshal.AllocHGlobal(requiredSize);
                    Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemDefaultCharSize);

                    NativeApi.SetupDiGetDeviceInterfaceDetail(DeviceHandle, ref DeviceInterfaceData, DetailDataBuffer, requiredSize, ref requiredSize, IntPtr.Zero);

                    IntPtr pDevPath = new IntPtr(DetailDataBuffer.ToInt32() + 4);

                    String devPath = Marshal.PtrToStringAuto(pDevPath);


                    try
                    {
                        NewList.Add(new HidDevice(devPath));
                    }
                    catch { }

                    Marshal.FreeHGlobal(DetailDataBuffer);
                }
            }

            // Maintain Existing Devices
            foreach (HidDevice HidDev in NewList)
            {
                if (DevList.Exists(new Predicate <HidDevice>(HidDev.Equals)))
                {
                    NewList.RemoveAll(new Predicate <HidDevice>(HidDev.Equals));
                    NewList.Add(DevList.Find(new Predicate <HidDevice>(HidDev.Equals)));
                }
                else
                {
                    HidDev.IsCheckedOut = false;
                }
            }

            // Swap Device Lists
            DevList = NewList;

            //OnDeviceChange(this, new EventArgs());
        }
Beispiel #2
0
        private void EnumerateDevices()
        {
            List<HidDevice> NewList = new List<HidDevice>();

            // Platform Device Enumeration

            int index = 0;
            NativeApi.SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;

            using (DeviceInfoHandle DeviceHandle = NativeApi.SetupDiGetClassDevs(ref HidGuid, null, 0,
                NativeApi.DIGCF_DEVICEINTERFACE | NativeApi.DIGCF_PRESENT))
            {

                DeviceInterfaceData = new NativeApi.SP_DEVICE_INTERFACE_DATA();
                DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData);

                while (NativeApi.SetupDiEnumDeviceInterfaces(DeviceHandle, 0, ref HidGuid, index++, ref DeviceInterfaceData))
                {
                    int requiredSize = 0;

                    NativeApi.SetupDiGetDeviceInterfaceDetail(DeviceHandle, ref DeviceInterfaceData, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero);

                    IntPtr DetailDataBuffer = Marshal.AllocHGlobal(requiredSize);
                    Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemDefaultCharSize);

                    NativeApi.SetupDiGetDeviceInterfaceDetail(DeviceHandle, ref DeviceInterfaceData, DetailDataBuffer, requiredSize, ref requiredSize, IntPtr.Zero);

                    IntPtr pDevPath = new IntPtr(DetailDataBuffer.ToInt32() + 4);

                    String devPath = Marshal.PtrToStringAuto(pDevPath);

                    try
                    {
                        NewList.Add(new HidDevice(devPath));
                    }
                    catch { }

                    Marshal.FreeHGlobal(DetailDataBuffer);
                }
            }

            // Maintain Existing Devices
            foreach (HidDevice HidDev in NewList)
            {
                if (DevList.Exists(new Predicate<HidDevice>(HidDev.Equals)))
                {
                    NewList.RemoveAll(new Predicate<HidDevice>(HidDev.Equals));
                    NewList.Add(DevList.Find(new Predicate<HidDevice>(HidDev.Equals)));
                }
                else
                {
                    HidDev.IsCheckedOut = false;
                }
            }

            // Swap Device Lists
            DevList = NewList;

            //OnDeviceChange(this, new EventArgs());
        }