コード例 #1
0
ファイル: USBDevice.cs プロジェクト: borkaborka/gmit
      private void findDevice(uint pid, uint vid) {
         Native.HidD_GetHidGuid(ref _hidGuid);
         hHidDeviceInfo = Native.SetupDiGetClassDevs(ref _hidGuid, null, IntPtr.Zero, Native.DIGCF_PRESENT | Native.DIGCF_DEVICEINTERFACE);
         if (hHidDeviceInfo.ToInt32() > -1) {
            uint i = 0;
            while (!isValid && i < Native.MAX_USB_DEVICES) {
               var hidDeviceInterfaceData = new Native.SP_DEVICE_INTERFACE_DATA();
               hidDeviceInterfaceData.cbSize = Marshal.SizeOf(hidDeviceInterfaceData);
               if (Native.SetupDiEnumDeviceInterfaces(hHidDeviceInfo, 0, ref _hidGuid, i, ref hidDeviceInterfaceData)) {
                  bool detailResult;
                  uint length, required;
                  Native.SetupDiGetDeviceInterfaceDetail(hHidDeviceInfo, ref hidDeviceInterfaceData, IntPtr.Zero, 0, out length, IntPtr.Zero);
                  var hidDeviceInterfaceDetailData = new Native.PSP_DEVICE_INTERFACE_DETAIL_DATA();
                  hidDeviceInterfaceDetailData.cbSize = 5; //DWORD cbSize (size 4) + Char[0] (size 1) for 32bit only!
                  var hDeviceInterfaceDetailData = Marshal.AllocHGlobal(Marshal.SizeOf(hidDeviceInterfaceDetailData));
                  Marshal.StructureToPtr(hidDeviceInterfaceDetailData, hDeviceInterfaceDetailData, true);
                  detailResult = Native.SetupDiGetDeviceInterfaceDetail(hHidDeviceInfo, ref hidDeviceInterfaceData, hDeviceInterfaceDetailData, length, out required, IntPtr.Zero);
                  Marshal.PtrToStructure(hDeviceInterfaceDetailData, hidDeviceInterfaceDetailData);
                  if (detailResult) {
                     base.handle = Native.CreateFile(hidDeviceInterfaceDetailData.DevicePath, 
                        Native.GENERIC_READ |
                        Native.GENERIC_WRITE,
                        Native.FILE_SHARE_READ |
                        Native.FILE_SHARE_WRITE, 
                        IntPtr.Zero,
                        Native.OPEN_EXISTING,
                        Native.FILE_FLAG_OVERLAPPED, 
                        IntPtr.Zero);
                     if (base.handle.ToInt32() > -1) {
                        Native.HIDD_ATTRIBUTES hidDeviceAttributes;
                        if (Native.HidD_GetAttributes(base.handle, out hidDeviceAttributes)) {
                           if ((hidDeviceAttributes.VendorID == vid) && (hidDeviceAttributes.ProductID == pid)) {
                              isValid = true;
                              ProductID = pid;
                              VendorID = vid;
                              VersionNumber = hidDeviceAttributes.VersionNumber;
                              IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string;
                              if (Native.HidD_GetProductString(this.handle, buffer, 126)) Name = Marshal.PtrToStringAuto(buffer);
                              if (Native.HidD_GetSerialNumberString(this.handle, buffer, 126)) SerialNumber = Marshal.PtrToStringAuto(buffer);
                              Marshal.FreeHGlobal(buffer);
                              var capabilities = new Native.HIDP_CAPS();
                              IntPtr hPreparsedData;
                              if (Native.HidD_GetPreparsedData(this.handle, out hPreparsedData)) {
                                 if (Native.HidP_GetCaps(hPreparsedData, out capabilities)) FeatureReportLength = capabilities.FeatureReportByteLength;
                                 Native.HidD_FreePreparsedData(hPreparsedData);
                              }
                              break;
                           }
                        } else {
                           Native.CloseHandle(base.handle);
                        }
                     }
                  }
                  Marshal.FreeHGlobal(hDeviceInterfaceDetailData);
               }
               i++;

            }
         }
      }
コード例 #2
0
        private int InitKeyboard()
        {
            bool   flag      = true;
            int    num1      = 0;
            Guid   guid      = new Guid("4d1e55b2-f16f-11cf-88cb-001111000030");
            IntPtr classDevs = Native.SetupDiGetClassDevs(ref guid, 0, IntPtr.Zero, 18);

            classDevs.ToInt32();
            int memberIndex = 0;

            while (flag)
            {
                Native.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new Native.SP_DEVICE_INTERFACE_DATA();
                flag = Native.SetupDiEnumDeviceInterfaces(classDevs, (Native.SP_DEVINFO_DATA)null, ref guid, memberIndex, deviceInterfaceData);
                if (flag)
                {
                    Native.SP_DEVINFO_DATA deviceInfoData = new Native.SP_DEVINFO_DATA();
                    int requiredSize = 0;
                    Native.SetupDiGetDeviceInterfaceDetail(classDevs, deviceInterfaceData, IntPtr.Zero, 0, ref requiredSize, deviceInfoData);
                    IntPtr num2 = Marshal.AllocHGlobal(requiredSize);
                    Marshal.StructureToPtr((object)new Native.SP_DEVICE_INTERFACE_DETAIL_DATA()
                    {
                        cbSize = (IntPtr.Size != 8 ? 4 + Marshal.SystemDefaultCharSize : 8)
                    }, num2, false);
                    Native.SetupDiGetDeviceInterfaceDetail(classDevs, deviceInterfaceData, num2, requiredSize, ref requiredSize, deviceInfoData);
                    string stringAuto = Marshal.PtrToStringAuto((IntPtr)((int)num2 + Marshal.SizeOf(typeof(int))));
                    Marshal.FreeHGlobal(num2);
                    if (stringAuto.IndexOf("04d9") > 0 && stringAuto.IndexOf("8008") > 0 || stringAuto.IndexOf("1044") > 0 && stringAuto.IndexOf("7a38") > 0 || stringAuto.IndexOf("1044") > 0 && stringAuto.IndexOf("7a39") > 0)
                    {
                        SECURITY_ATTRIBUTES securityAttributes = new SECURITY_ATTRIBUTES();
                        securityAttributes.nLength = Marshal.SizeOf((object)securityAttributes);
                        securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
                        securityAttributes.bInheritHandle       = 0;
                        IntPtr file = Win32.CreateFile(stringAuto, 3221225472U, 3, IntPtr.Zero, 3, 0, 0);
                        if (file.ToInt32() != -1)
                        {
                            IntPtr PreparsedData = new IntPtr();
                            Win32.HidD_GetPreparsedData(file, ref PreparsedData);
                            HIDP_CAPS Capabilities = new HIDP_CAPS();
                            Win32.HidP_GetCaps(PreparsedData, ref Capabilities);
                            if ((int)Capabilities.FeatureReportByteLength > 0)
                            {
                                _deviceHandle_2016 = file;
                                break;
                            }
                        }
                    }
                    else if ((stringAuto.IndexOf("1044") <= 0 || stringAuto.IndexOf("7a03") <= 0) && (stringAuto.IndexOf("1044") <= 0 || stringAuto.IndexOf("7a04") <= 0) && stringAuto.IndexOf("1044") > 0)
                    {
                        stringAuto.IndexOf("7a06");
                    }
                    ++memberIndex;
                }
            }
            return(num1);
        }