Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.IO;
- #region WinAPI
- [DllImport("user32.dll")]
- static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
- [DllImport("setupapi.dll", SetLastError = true)]
- static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, int Flags);
- [DllImport("setupapi.dll", SetLastError = true)]
- static extern bool SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, int memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
- [DllImport(@"setupapi.dll", SetLastError = true)]
- 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);
- [DllImport(@"setupapi.dll", SetLastError = true)]
- static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr DeviceInterfaceDetailData, int DeviceInterfaceDetailDataSize, ref int RequiredSize, IntPtr DeviceInfoData);
- [DllImport(@"kernel32.dll", SetLastError = true)]
- static extern IntPtr CreateFile(string fileName, uint fileAccess, uint fileShare, FileMapProtection securityAttributes, uint creationDisposition, uint flags, IntPtr overlapped);
- [DllImport("kernel32.dll")]
- static extern bool WriteFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToWrite, ref uint lpNumberOfBytesWritten, IntPtr lpOverlapped);
- [DllImport("kernel32.dll")]
- static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, IntPtr lpOverlapped);
- [DllImport("hid.dll")]
- static extern void HidD_GetHidGuid(ref Guid Guid);
- [DllImport("hid.dll", SetLastError = true)]
- static extern bool HidD_GetPreparsedData(IntPtr HidDeviceObject, ref IntPtr PreparsedData);
- [DllImport("hid.dll", SetLastError = true)]
- static extern bool HidD_GetAttributes(IntPtr DeviceObject, ref HIDD_ATTRIBUTES Attributes);
- [DllImport("hid.dll", SetLastError=true)]
- static extern uint HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
- [DllImport("hid.dll", SetLastError = true)]
- static extern int HidP_GetButtonCaps(HIDP_REPORT_TYPE ReportType, [In, Out] HIDP_BUTTON_CAPS[] ButtonCaps, ref ushort ButtonCapsLength, IntPtr PreparsedData);
- [DllImport("hid.dll", SetLastError = true)]
- static extern int HidP_GetValueCaps(HIDP_REPORT_TYPE ReportType, [In, Out] HIDP_VALUE_CAPS[] ValueCaps, ref ushort ValueCapsLength, IntPtr PreparsedData);
- [DllImport("hid.dll", SetLastError = true)]
- static extern int HidP_MaxUsageListLength(HIDP_REPORT_TYPE ReportType, ushort UsagePage, IntPtr PreparsedData);
- [DllImport("hid.dll", SetLastError = true)]
- static extern int HidP_SetUsages(HIDP_REPORT_TYPE ReportType, ushort UsagePage, short LinkCollection, short Usages, ref int UsageLength, IntPtr PreparsedData, IntPtr Report, int ReportLength);
- [DllImport("hid.dll", SetLastError = true)]
- static extern int HidP_SetUsageValue(HIDP_REPORT_TYPE ReportType, ushort UsagePage, short LinkCollection, ushort Usage, ulong UsageValue, IntPtr PreparsedData, IntPtr Report, int ReportLength);
- [DllImport("setupapi.dll", SetLastError = true)]
- static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
- [DllImport("kernel32.dll", SetLastError=true)]
- static extern bool CloseHandle(IntPtr hObject);
- [DllImport("kernel32.dll")]
- static extern IntPtr GlobalFree(object hMem);
- [DllImport("hid.dll", SetLastError=true)]
- static extern bool HidD_FreePreparsedData(ref IntPtr PreparsedData);
- [DllImport("kernel32.dll")]
- static extern uint GetLastError();
- #endregion
- #region Init Variable
- IntPtr hardwareDeviceInfo;
- bool cancel = true;
- bool HID_quit = false;
- int nbrDevices;
- int iHIDD;
- bool isConnected = false;
- ushort DEVICE_VID;
- ushort DEVICE_PID;
- ushort USAGE_PAGE;
- ushort USAGE;
- byte REPORT_ID;
- const int DIGCF_DEFAULT = 0x00000001;
- const int DIGCF_PRESENT = 0x00000002;
- const int DIGCF_ALLCLASSES = 0x00000004;
- const int DIGCF_PROFILE = 0x00000008;
- const int DIGCF_DEVICEINTERFACE = 0x00000010;
- const uint GENERIC_READ = 0x80000000;
- const uint GENERIC_WRITE = 0x40000000;
- const uint GENERIC_EXECUTE = 0x20000000;
- const uint GENERIC_ALL = 0x10000000;
- const uint FILE_SHARE_READ = 0x00000001;
- const uint FILE_SHARE_WRITE = 0x00000002;
- const uint FILE_SHARE_DELETE = 0x00000004;
- const uint CREATE_NEW = 1;
- const uint CREATE_ALWAYS = 2;
- const uint OPEN_EXISTING = 3;
- const uint OPEN_ALWAYS = 4;
- const uint TRUNCATE_EXISTING = 5;
- const int HIDP_STATUS_SUCCESS = 1114112;
- const int DEVICE_PATH = 260;
- const int INVALID_HANDLE_VALUE = -1;
- enum FileMapProtection : uint
- {
- PageReadonly = 0x02,
- PageReadWrite = 0x04,
- PageWriteCopy = 0x08,
- PageExecuteRead = 0x20,
- PageExecuteReadWrite = 0x40,
- SectionCommit = 0x8000000,
- SectionImage = 0x1000000,
- SectionNoCache = 0x10000000,
- SectionReserve = 0x4000000,
- }
- enum HIDP_REPORT_TYPE : ushort
- {
- HidP_Input = 0x00,
- HidP_Output = 0x01,
- HidP_Feature = 0x02,
- }
- [StructLayout(LayoutKind.Sequential)]
- struct LIST_ENTRY
- {
- public IntPtr Flink;
- public IntPtr Blink;
- }
- [StructLayout(LayoutKind.Sequential)]
- struct DEVICE_LIST_NODE
- {
- public LIST_ENTRY Hdr;
- public IntPtr NotificationHandle;
- public HID_DEVICE HidDeviceInfo;
- public bool DeviceOpened;
- }
- [StructLayout(LayoutKind.Sequential)]
- struct SP_DEVICE_INTERFACE_DATA
- {
- public Int32 cbSize;
- public Guid interfaceClassGuid;
- public Int32 flags;
- private UIntPtr reserved;
- }
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
- struct SP_DEVICE_INTERFACE_DETAIL_DATA
- {
- public int cbSize;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DEVICE_PATH)]
- public string DevicePath;
- }
- [StructLayout(LayoutKind.Sequential)]
- struct SP_DEVINFO_DATA
- {
- public int cbSize;
- public Guid classGuid;
- public UInt32 devInst;
- public IntPtr reserved;
- }
- [StructLayout(LayoutKind.Sequential)]
- struct HIDP_CAPS
- {
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 Usage;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 UsagePage;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 InputReportByteLength;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 OutputReportByteLength;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 FeatureReportByteLength;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
- public UInt16[] Reserved;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberLinkCollectionNodes;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberInputButtonCaps;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberInputValueCaps;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberInputDataIndices;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberOutputButtonCaps;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberOutputValueCaps;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberOutputDataIndices;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberFeatureButtonCaps;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberFeatureValueCaps;
- [MarshalAs(UnmanagedType.U2)]
- public UInt16 NumberFeatureDataIndices;
- };
- [StructLayout(LayoutKind.Sequential)]
- struct HIDD_ATTRIBUTES
- {
- public Int32 Size;
- public Int16 VendorID;
- public Int16 ProductID;
- public Int16 VersionNumber;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct ButtonData
- {
- public Int32 UsageMin;
- public Int32 UsageMax;
- public Int32 MaxUsageLength;
- public Int16 Usages;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct ValueData
- {
- public ushort Usage;
- public ushort Reserved;
- public ulong Value;
- public long ScaledValue;
- }
- [StructLayout(LayoutKind.Explicit)]
- struct HID_DATA
- {
- [FieldOffset(0)]
- public bool IsButtonData;
- [FieldOffset(1)]
- public byte Reserved;
- [FieldOffset(2)]
- public ushort UsagePage;
- [FieldOffset(4)]
- public Int32 Status;
- [FieldOffset(8)]
- public Int32 ReportID;
- [FieldOffset(16)]
- public bool IsDataSet;
- [FieldOffset(17)]
- public ButtonData ButtonData;
- [FieldOffset(17)]
- public ValueData ValueData;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct HIDP_Range
- {
- public ushort UsageMin, UsageMax;
- public ushort StringMin, StringMax;
- public ushort DesignatorMin, DesignatorMax;
- public ushort DataIndexMin, DataIndexMax;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct HIDP_NotRange
- {
- public ushort Usage, Reserved1;
- public ushort StringIndex, Reserved2;
- public ushort DesignatorIndex, Reserved3;
- public ushort DataIndex, Reserved4;
- }
- [StructLayout(LayoutKind.Explicit)]
- struct HIDP_BUTTON_CAPS
- {
- [FieldOffset(0)]
- public ushort UsagePage;
- [FieldOffset(2)]
- public byte ReportID;
- [FieldOffset(3), MarshalAs(UnmanagedType.U1)]
- public bool IsAlias;
- [FieldOffset(4)]
- public short BitField;
- [FieldOffset(6)]
- public short LinkCollection;
- [FieldOffset(8)]
- public short LinkUsage;
- [FieldOffset(10)]
- public short LinkUsagePage;
- [FieldOffset(12), MarshalAs(UnmanagedType.U1)]
- public bool IsRange;
- [FieldOffset(13), MarshalAs(UnmanagedType.U1)]
- public bool IsStringRange;
- [FieldOffset(14), MarshalAs(UnmanagedType.U1)]
- public bool IsDesignatorRange;
- [FieldOffset(15), MarshalAs(UnmanagedType.U1)]
- public bool IsAbsolute;
- [FieldOffset(16), MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
- public int[] Reserved;
- [FieldOffset(56)]
- public HIDP_Range Range;
- [FieldOffset(56)]
- public HIDP_NotRange NotRange;
- }
- [StructLayout(LayoutKind.Explicit)]
- struct HIDP_VALUE_CAPS
- {
- [FieldOffset(0)]
- public ushort UsagePage;
- [FieldOffset(2)]
- public byte ReportID;
- [FieldOffset(3), MarshalAs(UnmanagedType.U1)]
- public bool IsAlias;
- [FieldOffset(4)]
- public ushort BitField;
- [FieldOffset(6)]
- public ushort LinkCollection;
- [FieldOffset(8)]
- public ushort LinkUsage;
- [FieldOffset(10)]
- public ushort LinkUsagePage;
- [FieldOffset(12), MarshalAs(UnmanagedType.U1)]
- public bool IsRange;
- [FieldOffset(13), MarshalAs(UnmanagedType.U1)]
- public bool IsStringRange;
- [FieldOffset(14), MarshalAs(UnmanagedType.U1)]
- public bool IsDesignatorRange;
- [FieldOffset(15), MarshalAs(UnmanagedType.U1)]
- public bool IsAbsolute;
- [FieldOffset(16), MarshalAs(UnmanagedType.U1)]
- public bool HasNull;
- [FieldOffset(17)]
- public byte Reserved;
- [FieldOffset(18)]
- public short BitSize;
- [FieldOffset(20)]
- public short ReportCount;
- [FieldOffset(22)]
- public ushort Reserved2a;
- [FieldOffset(24)]
- public ushort Reserved2b;
- [FieldOffset(26)]
- public ushort Reserved2c;
- [FieldOffset(28)]
- public ushort Reserved2d;
- [FieldOffset(30)]
- public ushort Reserved2e;
- [FieldOffset(32)]
- public int UnitsExp;
- [FieldOffset(36)]
- public int Units;
- [FieldOffset(40)]
- public int LogicalMin;
- [FieldOffset(44)]
- public int LogicalMax;
- [FieldOffset(48)]
- public int PhysicalMin;
- [FieldOffset(52)]
- public int PhysicalMax;
- [FieldOffset(56)]
- public HIDP_Range Range;
- [FieldOffset(56)]
- public HIDP_NotRange NotRange;
- }
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
- struct HID_DEVICE
- {
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DEVICE_PATH)]
- public string DevicePath;
- public IntPtr HidDevice;
- public bool OpenedForRead;
- public bool OpenedForWrite;
- public bool OpenedOverlapped;
- public bool OpenedExclusive;
- public IntPtr Ppd;
- public HIDP_CAPS Caps;
- public HIDD_ATTRIBUTES Attributes;
- public IntPtr[] InputReportBuffer;
- public HID_DATA[] InputData;
- public Int32 InputDataLength;
- public HIDP_BUTTON_CAPS[] InputButtonCaps;
- public HIDP_VALUE_CAPS[] InputValueCaps;
- public IntPtr[] OutputReportBuffer;
- public HID_DATA[] OutputData;
- public Int32 OutputDataLength;
- public HIDP_BUTTON_CAPS[] OutputButtonCaps;
- public HIDP_VALUE_CAPS[] OutputValueCaps;
- public IntPtr[] FeatureReportBuffer;
- public HID_DATA[] FeatureData;
- public Int32 FeatureDataLength;
- public HIDP_BUTTON_CAPS[] FeatureButtonCaps;
- public HIDP_VALUE_CAPS[] FeatureValueCaps;
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement