コード例 #1
0
        private NativeMethods.HIDP_CAPS GetDeviceCapabilities(SafeFileHandle hidHandle)
        {
            NativeMethods.HIDP_CAPS capabilities = new NativeMethods.HIDP_CAPS();

            IntPtr preparsedDataPointer = new IntPtr();
            bool   success = NativeMethods.HidD_GetPreparsedData(hidHandle, ref preparsedDataPointer);

            try {
                int result = NativeMethods.HidP_GetCaps(preparsedDataPointer, ref capabilities);
            } finally {
                NativeMethods.HidD_FreePreparsedData(ref preparsedDataPointer);
            }

            return(capabilities);
        }
コード例 #2
0
        private void OpenDevices(List <String> devicePathNames, int vendorId, int productId)
        {
            NativeMethods.HIDD_ATTRIBUTES deviceAttributes = new NativeMethods.HIDD_ATTRIBUTES();
            deviceAttributes.Size = Marshal.SizeOf(typeof(NativeMethods.HIDD_ATTRIBUTES));

            NativeMethods.SECURITY_ATTRIBUTES security = new NativeMethods.SECURITY_ATTRIBUTES();
            security.SecurityDescriptor = 0;
            security.InheritHandle      = 1;
            security.Length             = Marshal.SizeOf(typeof(NativeMethods.SECURITY_ATTRIBUTES));

            for (int i = 0; i < devicePathNames.Count; i++)
            {
                SafeFileHandle handle = NativeMethods.CreateFile(devicePathNames[i], 0, NativeMethods.FILE_SHARE_READ | NativeMethods.FILE_SHARE_WRITE, ref security, NativeMethods.OPEN_EXISTING, 0, 0);

                if (!handle.IsInvalid)
                {
                    bool success = NativeMethods.HidD_GetAttributes(handle, ref deviceAttributes);
                    if (success)
                    {
                        if ((deviceAttributes.VendorID == vendorId) && (deviceAttributes.ProductID == productId))
                        {
                            NativeMethods.HIDP_CAPS deviceCapabilities = GetDeviceCapabilities(handle);

                            // Finally open the device
                            if (deviceCapabilities.OutputReportByteLength == 65)
                            {
                                _controlHandle = NativeMethods.CreateFile(devicePathNames[i], NativeMethods.GENERIC_WRITE, NativeMethods.FILE_SHARE_READ | NativeMethods.FILE_SHARE_WRITE, ref security, NativeMethods.OPEN_EXISTING, 0, 0);
                            }
                            else
                            {
                                _setupHandle = NativeMethods.CreateFile(devicePathNames[i], NativeMethods.GENERIC_WRITE, NativeMethods.FILE_SHARE_READ | NativeMethods.FILE_SHARE_WRITE, ref security, NativeMethods.OPEN_EXISTING, 0, 0);
                            }
                        }
                    }

                    handle.Close();
                }
            }
        }