Advertisement
Guest User

Untitled

a guest
May 17th, 2018
4,882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.93 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Threading;
  6. using System.Runtime.InteropServices;
  7. using System.Diagnostics;
  8. using System.IO;
  9.  
  10. #region WinAPI
  11.  
  12.     [DllImport("user32.dll")]
  13.     static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  14.  
  15.     [DllImport("setupapi.dll", SetLastError = true)]
  16.     static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, int Flags);
  17.  
  18.     [DllImport("setupapi.dll", SetLastError = true)]
  19.     static extern bool SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, int memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
  20.  
  21.     [DllImport(@"setupapi.dll", SetLastError = true)]
  22.     static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr DeviceInfoData);
  23.  
  24.     [DllImport(@"setupapi.dll", SetLastError = true)]
  25.     static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr DeviceInfoData);
  26.  
  27.     [DllImport(@"kernel32.dll", SetLastError = true)]
  28.     static extern IntPtr CreateFile(string fileName, uint fileAccess, uint fileShare, FileMapProtection securityAttributes, uint creationDisposition, uint flags, IntPtr overlapped);
  29.  
  30.     [DllImport("kernel32.dll")]
  31.     static extern bool WriteFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToWrite, ref uint lpNumberOfBytesWritten, IntPtr lpOverlapped);
  32.  
  33.     [DllImport("kernel32.dll")]
  34.     static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, IntPtr lpOverlapped);
  35.  
  36.     [DllImport("hid.dll")]
  37.     static extern void HidD_GetHidGuid(ref Guid Guid);
  38.  
  39.     [DllImport("hid.dll", SetLastError = true)]
  40.     static extern bool HidD_GetPreparsedData(IntPtr HidDeviceObject, ref IntPtr PreparsedData);
  41.  
  42.     [DllImport("hid.dll", SetLastError = true)]
  43.     static extern bool HidD_GetAttributes(IntPtr DeviceObject, ref HIDD_ATTRIBUTES Attributes);
  44.  
  45.     [DllImport("hid.dll", SetLastError=true)]
  46.     static extern uint HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
  47.  
  48.     [DllImport("hid.dll", SetLastError = true)]
  49.     static extern int HidP_GetButtonCaps(HIDP_REPORT_TYPE ReportType, [In, Out] HIDP_BUTTON_CAPS[] ButtonCaps, ref ushort ButtonCapsLength, IntPtr PreparsedData);
  50.  
  51.     [DllImport("hid.dll", SetLastError = true)]
  52.     static extern int HidP_GetValueCaps(HIDP_REPORT_TYPE ReportType, [In, Out] HIDP_VALUE_CAPS[] ValueCaps, ref ushort ValueCapsLength, IntPtr PreparsedData);
  53.  
  54.     [DllImport("hid.dll", SetLastError = true)]
  55.     static extern int HidP_MaxUsageListLength(HIDP_REPORT_TYPE ReportType, ushort UsagePage, IntPtr PreparsedData);
  56.  
  57.     [DllImport("hid.dll", SetLastError = true)]
  58.     static extern int HidP_SetUsages(HIDP_REPORT_TYPE ReportType, ushort UsagePage, short LinkCollection, short Usages, ref int UsageLength, IntPtr PreparsedData, IntPtr Report, int ReportLength);
  59.  
  60.     [DllImport("hid.dll", SetLastError = true)]
  61.     static extern int HidP_SetUsageValue(HIDP_REPORT_TYPE ReportType, ushort UsagePage, short LinkCollection, ushort Usage, ulong UsageValue, IntPtr PreparsedData, IntPtr Report, int ReportLength);
  62.  
  63.     [DllImport("setupapi.dll", SetLastError = true)]
  64.     static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
  65.  
  66.     [DllImport("kernel32.dll", SetLastError=true)]
  67.     static extern bool CloseHandle(IntPtr hObject);
  68.  
  69.     [DllImport("kernel32.dll")]
  70.     static extern IntPtr GlobalFree(object hMem);
  71.  
  72.     [DllImport("hid.dll", SetLastError=true)]
  73.     static extern bool HidD_FreePreparsedData(ref IntPtr PreparsedData);
  74.  
  75.     [DllImport("kernel32.dll")]
  76.     static extern uint GetLastError();
  77.  
  78. #endregion
  79.  
  80. #region Init Variable
  81.  
  82.     IntPtr  hardwareDeviceInfo;
  83.  
  84.     bool    cancel   = true;
  85.     bool    HID_quit = false;
  86.     int     nbrDevices;
  87.     int     iHIDD;
  88.     bool    isConnected = false;
  89.  
  90.     ushort DEVICE_VID;
  91.     ushort DEVICE_PID;
  92.     ushort USAGE_PAGE;
  93.     ushort USAGE;
  94.     byte   REPORT_ID;
  95.  
  96.     const int  DIGCF_DEFAULT         = 0x00000001;
  97.     const int  DIGCF_PRESENT         = 0x00000002;
  98.     const int  DIGCF_ALLCLASSES      = 0x00000004;
  99.     const int  DIGCF_PROFILE         = 0x00000008;
  100.     const int  DIGCF_DEVICEINTERFACE = 0x00000010;
  101.                                      
  102.     const uint GENERIC_READ         = 0x80000000;
  103.     const uint GENERIC_WRITE        = 0x40000000;
  104.     const uint GENERIC_EXECUTE      = 0x20000000;
  105.     const uint GENERIC_ALL          = 0x10000000;
  106.                                      
  107.     const uint FILE_SHARE_READ      = 0x00000001;  
  108.     const uint FILE_SHARE_WRITE     = 0x00000002;  
  109.     const uint FILE_SHARE_DELETE    = 0x00000004;  
  110.  
  111.     const uint CREATE_NEW           = 1;
  112.     const uint CREATE_ALWAYS        = 2;
  113.     const uint OPEN_EXISTING        = 3;
  114.     const uint OPEN_ALWAYS          = 4;
  115.     const uint TRUNCATE_EXISTING    = 5;
  116.  
  117.     const int  HIDP_STATUS_SUCCESS   = 1114112;
  118.     const int  DEVICE_PATH           = 260;
  119.     const int  INVALID_HANDLE_VALUE = -1;
  120.  
  121.     enum FileMapProtection : uint
  122.     {
  123.         PageReadonly = 0x02,
  124.         PageReadWrite = 0x04,
  125.         PageWriteCopy = 0x08,
  126.         PageExecuteRead = 0x20,
  127.         PageExecuteReadWrite = 0x40,
  128.         SectionCommit = 0x8000000,
  129.         SectionImage = 0x1000000,
  130.         SectionNoCache = 0x10000000,
  131.         SectionReserve = 0x4000000,
  132.     }
  133.  
  134.     enum HIDP_REPORT_TYPE : ushort
  135.     {
  136.         HidP_Input   = 0x00,
  137.         HidP_Output  = 0x01,
  138.         HidP_Feature = 0x02,
  139.     }
  140.  
  141.     [StructLayout(LayoutKind.Sequential)]
  142.     struct LIST_ENTRY
  143.     {
  144.         public IntPtr Flink;
  145.         public IntPtr Blink;
  146.     }
  147.  
  148.     [StructLayout(LayoutKind.Sequential)]
  149.     struct DEVICE_LIST_NODE
  150.     {
  151.         public LIST_ENTRY      Hdr;
  152.         public IntPtr          NotificationHandle;
  153.         public HID_DEVICE      HidDeviceInfo;
  154.         public bool            DeviceOpened;
  155.     }
  156.  
  157.     [StructLayout(LayoutKind.Sequential)]
  158.     struct SP_DEVICE_INTERFACE_DATA
  159.     {
  160.         public  Int32   cbSize;
  161.         public  Guid    interfaceClassGuid;
  162.         public  Int32   flags;
  163.         private UIntPtr reserved;
  164.     }
  165.  
  166.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  167.     struct SP_DEVICE_INTERFACE_DETAIL_DATA
  168.     {
  169.         public int cbSize;
  170.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DEVICE_PATH)]
  171.         public string DevicePath;
  172.     }    
  173.  
  174.     [StructLayout(LayoutKind.Sequential)]
  175.     struct SP_DEVINFO_DATA
  176.     {
  177.        public int       cbSize;
  178.        public Guid      classGuid;
  179.        public UInt32    devInst;
  180.        public IntPtr    reserved;
  181.     }
  182.  
  183.     [StructLayout(LayoutKind.Sequential)]
  184.     struct HIDP_CAPS
  185.     {
  186.         [MarshalAs(UnmanagedType.U2)]
  187.         public UInt16 Usage;
  188.         [MarshalAs(UnmanagedType.U2)]
  189.         public UInt16 UsagePage;
  190.         [MarshalAs(UnmanagedType.U2)]
  191.         public UInt16 InputReportByteLength;
  192.         [MarshalAs(UnmanagedType.U2)]
  193.         public UInt16 OutputReportByteLength;
  194.         [MarshalAs(UnmanagedType.U2)]
  195.         public UInt16 FeatureReportByteLength;
  196.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
  197.         public UInt16[] Reserved;
  198.         [MarshalAs(UnmanagedType.U2)]
  199.         public UInt16 NumberLinkCollectionNodes;
  200.         [MarshalAs(UnmanagedType.U2)]
  201.         public UInt16 NumberInputButtonCaps;
  202.         [MarshalAs(UnmanagedType.U2)]
  203.         public UInt16 NumberInputValueCaps;
  204.         [MarshalAs(UnmanagedType.U2)]
  205.         public UInt16 NumberInputDataIndices;
  206.         [MarshalAs(UnmanagedType.U2)]
  207.         public UInt16 NumberOutputButtonCaps;
  208.         [MarshalAs(UnmanagedType.U2)]
  209.         public UInt16 NumberOutputValueCaps;
  210.         [MarshalAs(UnmanagedType.U2)]
  211.         public UInt16 NumberOutputDataIndices;
  212.         [MarshalAs(UnmanagedType.U2)]
  213.         public UInt16 NumberFeatureButtonCaps;
  214.         [MarshalAs(UnmanagedType.U2)]
  215.         public UInt16 NumberFeatureValueCaps;
  216.         [MarshalAs(UnmanagedType.U2)]
  217.         public UInt16 NumberFeatureDataIndices;
  218.     };
  219.  
  220.     [StructLayout(LayoutKind.Sequential)]
  221.     struct HIDD_ATTRIBUTES
  222.     {
  223.         public Int32 Size;
  224.         public Int16 VendorID;
  225.         public Int16 ProductID;
  226.         public Int16 VersionNumber;
  227.     }
  228.  
  229.     [StructLayout(LayoutKind.Sequential)]
  230.     public struct ButtonData
  231.     {
  232.          public Int32 UsageMin;
  233.          public Int32 UsageMax;
  234.          public Int32 MaxUsageLength;
  235.          public Int16 Usages;
  236.     }
  237.  
  238.     [StructLayout(LayoutKind.Sequential)]
  239.     public struct ValueData
  240.     {
  241.          public ushort  Usage;
  242.          public ushort  Reserved;
  243.  
  244.          public ulong   Value;
  245.          public long    ScaledValue;
  246.     }
  247.  
  248.     [StructLayout(LayoutKind.Explicit)]
  249.     struct HID_DATA
  250.     {
  251.         [FieldOffset(0)]
  252.         public bool     IsButtonData;
  253.         [FieldOffset(1)]
  254.         public byte     Reserved;
  255.         [FieldOffset(2)]
  256.         public ushort UsagePage;
  257.         [FieldOffset(4)]
  258.         public Int32    Status;
  259.         [FieldOffset(8)]
  260.         public Int32    ReportID;
  261.         [FieldOffset(16)]
  262.         public bool     IsDataSet;
  263.  
  264.         [FieldOffset(17)]
  265.         public ButtonData ButtonData;
  266.         [FieldOffset(17)]
  267.         public ValueData ValueData;
  268.     }
  269.  
  270.     [StructLayout(LayoutKind.Sequential)]
  271.     public struct HIDP_Range
  272.     {
  273.         public ushort UsageMin,         UsageMax;
  274.         public ushort StringMin,        StringMax;
  275.         public ushort DesignatorMin,    DesignatorMax;
  276.         public ushort DataIndexMin,     DataIndexMax;
  277.     }
  278.  
  279.     [StructLayout(LayoutKind.Sequential)]
  280.     public struct HIDP_NotRange
  281.     {
  282.         public ushort Usage,            Reserved1;
  283.         public ushort StringIndex,      Reserved2;
  284.         public ushort DesignatorIndex,  Reserved3;
  285.         public ushort DataIndex,        Reserved4;
  286.     }
  287.  
  288.     [StructLayout(LayoutKind.Explicit)]
  289.     struct HIDP_BUTTON_CAPS
  290.     {
  291.         [FieldOffset(0)]
  292.         public ushort UsagePage;
  293.         [FieldOffset(2)]
  294.         public byte ReportID;
  295.         [FieldOffset(3), MarshalAs(UnmanagedType.U1)]
  296.         public bool IsAlias;
  297.         [FieldOffset(4)]
  298.         public short BitField;
  299.         [FieldOffset(6)]
  300.         public short LinkCollection;
  301.         [FieldOffset(8)]
  302.         public short LinkUsage;
  303.         [FieldOffset(10)]
  304.         public short LinkUsagePage;
  305.         [FieldOffset(12), MarshalAs(UnmanagedType.U1)]
  306.         public bool IsRange;
  307.         [FieldOffset(13), MarshalAs(UnmanagedType.U1)]
  308.         public bool IsStringRange;
  309.         [FieldOffset(14), MarshalAs(UnmanagedType.U1)]
  310.         public bool IsDesignatorRange;
  311.         [FieldOffset(15), MarshalAs(UnmanagedType.U1)]
  312.         public bool IsAbsolute;
  313.         [FieldOffset(16), MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
  314.         public int[] Reserved;
  315.  
  316.         [FieldOffset(56)]
  317.         public HIDP_Range Range;
  318.         [FieldOffset(56)]
  319.         public HIDP_NotRange NotRange;
  320.     }
  321.  
  322.     [StructLayout(LayoutKind.Explicit)]
  323.     struct HIDP_VALUE_CAPS
  324.     {
  325.         [FieldOffset(0)]
  326.         public ushort UsagePage;
  327.         [FieldOffset(2)]
  328.         public byte ReportID;
  329.         [FieldOffset(3), MarshalAs(UnmanagedType.U1)]
  330.         public bool IsAlias;
  331.         [FieldOffset(4)]
  332.         public ushort BitField;
  333.         [FieldOffset(6)]
  334.         public ushort LinkCollection;
  335.         [FieldOffset(8)]
  336.         public ushort LinkUsage;
  337.         [FieldOffset(10)]
  338.         public ushort LinkUsagePage;
  339.         [FieldOffset(12), MarshalAs(UnmanagedType.U1)]
  340.         public bool IsRange;
  341.         [FieldOffset(13), MarshalAs(UnmanagedType.U1)]
  342.         public bool IsStringRange;
  343.         [FieldOffset(14), MarshalAs(UnmanagedType.U1)]
  344.         public bool IsDesignatorRange;
  345.         [FieldOffset(15), MarshalAs(UnmanagedType.U1)]
  346.         public bool IsAbsolute;
  347.         [FieldOffset(16), MarshalAs(UnmanagedType.U1)]
  348.         public bool HasNull;
  349.         [FieldOffset(17)]
  350.         public byte Reserved;
  351.         [FieldOffset(18)]
  352.         public short BitSize;
  353.         [FieldOffset(20)]
  354.         public short ReportCount;
  355.         [FieldOffset(22)]
  356.         public ushort Reserved2a;
  357.         [FieldOffset(24)]
  358.         public ushort Reserved2b;
  359.         [FieldOffset(26)]
  360.         public ushort Reserved2c;
  361.         [FieldOffset(28)]
  362.         public ushort Reserved2d;
  363.         [FieldOffset(30)]
  364.         public ushort Reserved2e;
  365.         [FieldOffset(32)]
  366.         public int UnitsExp;
  367.         [FieldOffset(36)]
  368.         public int Units;
  369.         [FieldOffset(40)]
  370.         public int LogicalMin;
  371.         [FieldOffset(44)]
  372.         public int LogicalMax;
  373.         [FieldOffset(48)]
  374.         public int PhysicalMin;
  375.         [FieldOffset(52)]
  376.         public int PhysicalMax;
  377.  
  378.         [FieldOffset(56)]
  379.         public HIDP_Range Range;
  380.         [FieldOffset(56)]
  381.         public HIDP_NotRange NotRange;
  382.     }
  383.  
  384.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  385.     struct HID_DEVICE
  386.     {
  387.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DEVICE_PATH)]
  388.         public string             DevicePath;
  389.         public IntPtr             HidDevice;
  390.         public bool               OpenedForRead;
  391.         public bool               OpenedForWrite;
  392.         public bool               OpenedOverlapped;
  393.         public bool               OpenedExclusive;
  394.  
  395.         public IntPtr             Ppd;
  396.         public HIDP_CAPS          Caps;
  397.         public HIDD_ATTRIBUTES    Attributes;
  398.  
  399.         public IntPtr[]           InputReportBuffer;
  400.         public HID_DATA[]         InputData;
  401.         public Int32              InputDataLength;
  402.         public HIDP_BUTTON_CAPS[] InputButtonCaps;
  403.         public HIDP_VALUE_CAPS[]  InputValueCaps;
  404.  
  405.         public IntPtr[]           OutputReportBuffer;
  406.         public HID_DATA[]         OutputData;
  407.         public Int32              OutputDataLength;
  408.         public HIDP_BUTTON_CAPS[] OutputButtonCaps;
  409.         public HIDP_VALUE_CAPS[]  OutputValueCaps;
  410.  
  411.         public IntPtr[]           FeatureReportBuffer;
  412.         public HID_DATA[]         FeatureData;
  413.         public Int32              FeatureDataLength;
  414.         public HIDP_BUTTON_CAPS[] FeatureButtonCaps;
  415.         public HIDP_VALUE_CAPS[]  FeatureValueCaps;
  416.     }
  417.  
  418. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement