예제 #1
0
        /* open hid device */
        public bool Open()
        {
            /* opens hid device file */
            HIDHandel = HIDNativeAPIs.CreateFile(HWFullPath,
                                                 HIDNativeAPIs.GENERIC_READ | HIDNativeAPIs.GENERIC_WRITE,
                                                 HIDNativeAPIs.FILE_SHARE_READ | HIDNativeAPIs.FILE_SHARE_WRITE,
                                                 IntPtr.Zero, HIDNativeAPIs.OPEN_EXISTING, 0, IntPtr.Zero);

            /* whops */
            if (HIDHandel.IsInvalid)
            {
                return(false);
            }

            GetHIDDevInfos();
            int bufSize = (int)OutputBuffSize;

            if (bufSize == 0)
            {
                bufSize = DefBUfferSize;
            }
            /* prepare stream - sync buf size should same as output buf */
            _fileStream = new FileStream(HIDHandel, FileAccess.ReadWrite, bufSize, false);
            /* report status */
            return(true);
        }
예제 #2
0
        /* get device path */
        private string GetPath(IntPtr hInfoSet, ref DeviceInterfaceData iface)
        {
            /* detailed interface information */
            var detIface = new DeviceInterfaceDetailData();
            /* required size */
            uint reqSize = (uint)Marshal.SizeOf(detIface);

            /* set size. The cbSize member always contains the size of the
             * fixed part of the data structure, not a size reflecting the
             * variable-length string at the end. */
            /* now stay with me and look at that x64/x86 maddness! */
            detIface.Size = Marshal.SizeOf(typeof(IntPtr)) == 8 ? 8 : 5;

            /* get device path */
            bool status = HIDNativeAPIs.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref iface, ref detIface, reqSize, ref reqSize, IntPtr.Zero);

            /* whops */
            if (!status)
            {
                /* fail! */
                throw new Win32Exception();
            }

            /* return device path */
            return(detIface.DevicePath);
        }
예제 #3
0
 public byte[] GetInputReport(byte reportID)
 {
     byte[] revbuf = new byte[16];
     revbuf[0] = reportID;
     if (!HIDNativeAPIs.HidD_GetInputReport(HIDHandel, revbuf, (uint)revbuf.Length))
     {
         revbuf = null;
     }
     return(revbuf);
 }
예제 #4
0
        private void GetHIDDevInfos()
        {
            //get capabilities - use getPreParsedData, and getCaps
            //store the report lengths
            IntPtr    ptrToPreParsedData = new IntPtr();
            bool      ppdSucsess         = HIDNativeAPIs.HidD_GetPreparsedData(HIDHandel, ref ptrToPreParsedData);
            HIDP_CAPS capabilities       = new HIDP_CAPS();
            int       hidCapsSucsess     = HIDNativeAPIs.HidP_GetCaps(ptrToPreParsedData, ref capabilities);

            //Save buff size
            OutputBuffSize = capabilities.OutputReportByteLength;
            InputBuffSize  = capabilities.InputReportByteLength;
            //Call freePreParsedData to release some stuff
            HIDNativeAPIs.HidD_FreePreparsedData(ref ptrToPreParsedData);
        }
예제 #5
0
        /* get vid and pid */
        private HiddAttributtes GetVidPid(SafeFileHandle handle)
        {
            /* attributes structure */
            var attr = new HiddAttributtes();

            /* set size */
            attr.Size = Marshal.SizeOf(attr);

            /* get attributes */
            if (HIDNativeAPIs.HidD_GetAttributes(handle.DangerousGetHandle(), ref attr) == false)
            {
                /* fail! */
                throw new Win32Exception();
            }
            return(attr);
        }
예제 #6
0
        /* get device product string */
        private string GetSerialNumber(SafeFileHandle handle)
        {
            /* buffer */
            var s = new StringBuilder(256);
            /* returned string */
            string rc = String.Empty;

            /* get string */
            if (HIDNativeAPIs.HidD_GetSerialNumberString(handle.DangerousGetHandle(), s, s.Capacity))
            {
                rc = s.ToString();
            }

            /* report string */
            return(rc);
        }
예제 #7
0
        /* dispose */
        public void Dispose()
        {
            /* deal with file stream */
            if (_fileStream != null)
            {
                /* close stream */
                //_fileStream.Dispose();
                _fileStream.Close();
                /* get rid of object */
                _fileStream = null;
            }

            if (!HIDHandel.IsClosed)
            {
                /* close handle */
                HIDNativeAPIs.CloseHandle(HIDHandel);
            }
        }
예제 #8
0
        /* browse all HID class devices */
        public static List <HIDInfo> BrowseHID()
        {
            /* hid device class guid */
            Guid gHid;
            /* list of device information */
            List <HIDInfo> info = new List <HIDInfo>();

            /* obtain hid guid */
            HIDNativeAPIs.HidD_GetHidGuid(out gHid);
            /* get list of present hid devices */
            //var hInfoSet = GetClassDevs(gHid, (uint)DIGCF.DIGCF_PRESENT | (uint)DIGCF.DIGCF_DEVICEINTERFACE);
            var hInfoSet = GetClassDevs(Guid.Empty, (uint)DIGCF.DIGCF_ALLCLASSES | (uint)DIGCF.DIGCF_DEVICEINTERFACE);

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

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

            /* iterate through all interfaces */
            while (HIDNativeAPIs.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, index, ref iface))
            {
                bool    isWork  = false;
                HIDInfo hidInfo = new HIDInfo(hInfoSet, iface, out isWork);
                if (isWork)
                {
                    info.Add(hidInfo);
                }
                /* next, please */
                index++;
            }

            /* clean up */
            if (HIDNativeAPIs.SetupDiDestroyDeviceInfoList(hInfoSet) == false)
            {
                /* fail! */
                //throw new Win32Exception();
            }

            /* return list */
            return(info);
        }
예제 #9
0
        public bool OpenAsync()
        {
            if (HWFullPath.Contains("28559b57"))
            {
                int my = -1;
            }
            /* opens hid device file */
            HIDHandel = HIDNativeAPIs.CreateFile(HWFullPath,
                                                 HIDNativeAPIs.GENERIC_READ | HIDNativeAPIs.GENERIC_WRITE,
                                                 HIDNativeAPIs.FILE_SHARE_READ | HIDNativeAPIs.FILE_SHARE_WRITE,
                                                 IntPtr.Zero,
                                                 //HIDNativeAPIs.OPEN_EXISTING,
                                                 HIDNativeAPIs.FILE_CREATE_NEW,
                                                 HIDNativeAPIs.FILE_FLAG_OVERLAPPED,
                                                 IntPtr.Zero);

            /* whops */
            if (HIDHandel.IsInvalid)
            {
                int    error = Marshal.GetLastWin32Error();
                string rev   = HIDNativeAPIs.GetSysErrMsg(error);
                return(false);
            }

            GetHIDDevInfos();
            int bufSize = (int)OutputBuffSize;

            if (bufSize == 0)
            {
                bufSize = DefBUfferSize;
            }
            /* prepare stream - async buf size should same as output buf */
            _fileStream = new FileStream(HIDHandel, FileAccess.ReadWrite, bufSize, true);
            /* report status */
            return(true);
        }
예제 #10
0
 static IntPtr GetClassDevs(Guid guid, uint flags)
 {
     return(HIDNativeAPIs.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, flags));
 }
예제 #11
0
 public bool SetOutPutReport(byte[] data)
 {
     return(HIDNativeAPIs.HidD_SetOutputReport(HIDHandel, data, (uint)data.Length));
 }