Beispiel #1
0
        internal static HidDevice Open(string devicePath)
        {
            IntPtr handle = Kernel32.CreateFile(
                devicePath,
                (uint)FileAccess.Read,
                FileShare.ReadWrite,
                IntPtr.Zero,
                FileMode.Open,
                FileAttributes.Device,
                IntPtr.Zero);

            if (handle != Kernel32.InvalidHandleValue)
            {
                Hid.DeviceAttributes attributes;
                if (Hid.HidD_GetAttributes(handle, out attributes))
                {
                    return(new HidDevice(
                               handle,
                               devicePath,
                               attributes.VendorId,
                               attributes.ProductId,
                               attributes.VersionNumber));
                }
                // If we could not get the hid attributes,
                // then what we opened was not a hid device,
                // we need to close the handle nonetheless.
                Kernel32.CloseHandle(handle);
            }
            throw new Exception();
        }
Beispiel #2
0
 // Use Open to get device objects
 private HidDevice(IntPtr handle, string devicePath, int vendorId, int productId, int versionNumber)
 {
     this.handle   = handle;
     DevicePath    = devicePath;
     VendorId      = vendorId;
     ProductId     = productId;
     VersionNumber = versionNumber;
     Hid.HidD_GetPreparsedData(handle, out preparsedData);
 }
Beispiel #3
0
        public static void Initialize(IntPtr windowHandle)
        {
            // Initialize device event dispatcher.
            Guid hidClassGuid;

            Hid.HidD_GetHidGuid(out hidClassGuid);
            deviceEventNotifier = new DeviceEventNotifier(windowHandle, hidClassGuid);
            // Register for device removal/arrival events so we can keep
            // track of the currently attached devices of interest.
            deviceEventNotifier.DeviceArrival += DeviceAdded;
            deviceEventNotifier.DeviceRemoval += DeviceRemoved;
            // Open all devices we can find for now.
            devices = new Dictionary <string, HidDevice>();
            var devs = HidDevice.GetDevices(vendorId, productIds);

            foreach (var dev in devs)
            {
                devices.Add(dev.DevicePath.ToUpper(), dev);
            }
        }
Beispiel #4
0
        public static void Init(IntPtr windowHandle, params LogitechKeyboardType[] keyboardsToListenFor)
        {
            KeyboardsToListenFor = keyboardsToListenFor;

            // Initialize device event dispatcher.
            Guid hidClassGuid;

            Hid.HidD_GetHidGuid(out hidClassGuid);
            deviceEventNotifier = new DeviceEventNotifier(windowHandle, hidClassGuid);
            // Register for device removal/arrival events so we can keep
            // track of the currently attached devices of interest.
            deviceEventNotifier.DeviceArrival += DeviceAdded;
            deviceEventNotifier.DeviceRemoval += DeviceRemoved;

            devices = new List <LogitechKeyboard>();

            // Open all devices we can find for now.
            var devs = HidDevice.Open(VENDOR_ID, keyboardsToListenFor.Select(d => d.ProductID));

            foreach (var dev in devs)
            {
                devices.Add(new LogitechKeyboard(dev, keyboardsToListenFor.Where(k => k.ProductID == dev.ProductId).First()));
            }
        }
Beispiel #5
0
        internal static List <HidDevice> Open(int vendorId, IEnumerable <int> productIds)
        {
            // Obtain system-defined GUID for HIDClass devices.
            Guid hidClassGuid;

            Hid.HidD_GetHidGuid(out hidClassGuid);
            var deviceList = new List <HidDevice>();
            // Obtain handle to an opaque device-information-set
            // describing device interface supported by all HID
            // collections currently installed in the system.
            IntPtr deviceInfoSet = SetupApi.SetupDiGetClassDevs(
                ref hidClassGuid,
                null,
                IntPtr.Zero,
                SetupApi.DiGetFlags.DeviceInterface | SetupApi.DiGetFlags.Present);

            if (deviceInfoSet == Kernel32.InvalidHandleValue)
            {
                throw new Win32Exception();
            }
            // Retrieve all available interface information.
            SetupApi.SpDeviceInterfaceData did = new SetupApi.SpDeviceInterfaceData();
            did.Size = Marshal.SizeOf(did);
            int i = 0;

            while (SetupApi.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref hidClassGuid, i++, ref did))
            {
                // Obtain DevicePath
                var didd = new SetupApi.SpDeviceInterfaceDetailData();
                // On x64 only 8 appears to work while on x86 it must be sizeof (int) + sizeof (TCHAR)
                didd.Size = IntPtr.Size == 8 ? 8 : IntPtr.Size + Marshal.SystemDefaultCharSize;
                if (SetupApi.SetupDiGetDeviceInterfaceDetail(
                        deviceInfoSet,
                        ref did,
                        ref didd,
                        Marshal.SizeOf(didd),
                        IntPtr.Zero,
                        IntPtr.Zero))
                {
                    // Write access causes failure if not running elevated.
                    IntPtr hDevice = Kernel32.CreateFile(
                        didd.DevicePath,
                        (uint)FileAccess.Read,                       // we just need read
                        FileShare.ReadWrite,                         // and we don't want to restrict others from write
                        IntPtr.Zero,
                        FileMode.Open,
                        FileAttributes.Device,
                        IntPtr.Zero);
                    if (hDevice != Kernel32.InvalidHandleValue)
                    {
                        Hid.DeviceAttributes attributes;
                        if (Hid.HidD_GetAttributes(hDevice, out attributes))
                        {
                            if (vendorId == attributes.VendorId && productIds.Contains(attributes.ProductId))
                            {
                                deviceList.Add(
                                    new HidDevice(
                                        hDevice,
                                        didd.DevicePath,
                                        vendorId,
                                        attributes.ProductId,
                                        attributes.VersionNumber));
                                continue;
                            }
                        }
                        // Close the wrong device handle
                        Kernel32.CloseHandle(hDevice);
                    }
                }
            }
            // Free the device-information-set.
            SetupApi.SetupDiDestroyDeviceInfoList(deviceInfoSet);
            return(deviceList);
        }
Beispiel #6
0
 internal bool SetOutputReport(byte[] reportBuffer)
 {
     return(Hid.HidD_SetOutputReport(handle, reportBuffer, reportBuffer.Length));
 }
Beispiel #7
0
 internal bool GetInputReport(byte[] reportBuffer)
 {
     return(Hid.HidD_GetInputReport(handle, reportBuffer, reportBuffer.Length));
 }
Beispiel #8
0
 internal bool SetFeature(byte[] reportBuffer)
 {
     return(Hid.HidD_SetFeature(handle, reportBuffer, reportBuffer.Length));
 }
Beispiel #9
0
 public void Dispose()
 {
     Kernel32.CloseHandle(handle);
     Hid.HidD_FreePreparsedData(preparsedData);
 }
Beispiel #10
0
 internal Hid.Caps GetCaps()
 {
     Hid.Caps caps;
     Hid.HidP_GetCaps(preparsedData, out caps);
     return(caps);
 }
Beispiel #11
0
 public static extern int HidP_GetCaps(IntPtr preparsedData, out Hid.Caps caps);