-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWinHID.cs
398 lines (345 loc) · 14.6 KB
/
WinHID.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Runtime.InteropServices;
// Data structures from https://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=1244702
namespace DiceKeysWindowsCommandLine
{
public partial class HID
{
#region WinAPI
[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("hid.dll", SetLastError = true)]
static extern bool HidD_FreePreparsedData(ref IntPtr PreparsedData);
[DllImport("kernel32.dll")]
static extern uint GetLastError();
#endregion
#region DLL Var
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 UInt16 VendorID;
public UInt16 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
}
}