Beispiel #1
0
 protected static extern bool HidD_GetAttributes(IntPtr hFile, ref HidDAttributes pAttributes);
Beispiel #2
0
        //public static FileStream Open(string tSerial, string tMan)
        public static FileStream Open(UInt16 vid, UInt16 pid, int report_length)
        {
            FileStream devFile = null;

            Guid gHid;

            HidD_GetHidGuid(out gHid);

            // create list of HID devices present right now
            var hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            var iface = new DeviceInterfaceData(); // allocate mem for interface descriptor

            iface.Size = Marshal.SizeOf(iface);    // set size field
            uint index = 0;                        // interface index

            // Enumerate all interfaces with HID GUID
            while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface))
            {
                var  detIface = new DeviceInterfaceDetailData();             // detailed interface information
                uint reqSize  = (uint)Marshal.SizeOf(detIface);              // required size
                detIface.Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5; // Size depends on arch (32 / 64 bit), distinguish by IntPtr size

                // get device path
                SetupDiGetDeviceInterfaceDetail(hInfoSet, ref iface, ref detIface, reqSize, ref reqSize, IntPtr.Zero);
                var path = detIface.DevicePath;

                System.Console.WriteLine("Path: {0}", path);

                // Open filehandle to device
                var handle = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);

                if (handle == INVALID_HANDLE_VALUE)
                {
                    //System.Console.WriteLine("Invalid handle");
                    index++;
                    continue;
                }

                IntPtr         lpData;
                HidDAttributes pAttributes = new HidDAttributes();
                if (HidD_GetPreparsedData(handle, out lpData))
                {
                    HidCaps oCaps;
                    HidP_GetCaps(lpData, out oCaps);         // extract the device capabilities from the internal buffer
                    int inp  = oCaps.InputReportByteLength;  // get the input...
                    int outp = oCaps.OutputReportByteLength; // ... and output report length
                    HidD_FreePreparsedData(ref lpData);
                    System.Console.WriteLine("Input: {0}, Output: {1}", inp, outp);

                    // we have report length matching our input / output report, so we create a device file in each case
                    if (inp == report_length && outp == report_length)
                    {
                        HidD_GetAttributes(handle, ref pAttributes);

                        //Check PID&VID
                        if (pAttributes.ProductID == pid && pAttributes.VendorID == vid)
                        {
                            var shandle = new SafeFileHandle(handle, false);
                            devFile = new FileStream(shandle, FileAccess.Read | FileAccess.Write, 32, true);
                            break;
                        }
                    }
                }
                index++;
            }
            SetupDiDestroyDeviceInfoList(hInfoSet);
            return(devFile);
        }