-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCore.cs
218 lines (181 loc) · 7.05 KB
/
Core.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
using System;
using System.Runtime.InteropServices;
using System.IO;
namespace HIDUSBLib
{
internal class Core
{
public IntPtr _hHid = (IntPtr)(INVALID_HANDLE_VALUE); // HID device handle
public string _strDevicePathName = string.Empty;
public IntPtr _hDevInfo = (IntPtr)(INVALID_HANDLE_VALUE); // Device infoset handle
public const UInt32 DIGCF_PRESENT = 0x00000002;
public const UInt32 DIGCF_DEVICEINTERFACE = 0x00000010;
public const UInt32 DIGCF_INTERFACEDEVICE = 0x00000010;
public const UInt32 GENERIC_READ = 0x80000000;
public const UInt32 GENERIC_WRITE = 0x40000000;
public const UInt32 FILE_SHARE_READ = 0x00000001;
public const UInt32 FILE_SHARE_WRITE = 0x00000002;
public const int OPEN_EXISTING = 3;
public const int EV_RXFLAG = 0x0002;
public const int INVALID_HANDLE_VALUE = -1;
public const int ERROR_INVALID_HANDLE = 6;
public const int FILE_FLAG_OVERLAPED = 0x40000000;
private GUID _guid = new GUID();
public SP_DEVICE_INTERFACE_DATA _deviceInterfaceData;
public SP_DEVICE_INTERFACE_DETAIL_DATA _deviceInterfaceDetailData;
public HIDP_CAPS _hidpCaps;
// GUID structure
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct GUID
{
public UInt32 Data1;
public UInt16 Data2;
public UInt16 Data3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] data4;
}
[DllImport("setupapi.dll", SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs(
ref GUID ClassGuid,
[MarshalAs(UnmanagedType.LPTStr)] string Enumerator,
IntPtr hwndParent,
UInt32 Flags
);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiEnumDeviceInterfaces(
IntPtr DeviceInfoSet,
IntPtr DeviceInfoData,
ref GUID lpHidGuid,
UInt32 MemberIndex,
ref SP_DEVICE_INTERFACE_DATA lpDeviceInterfaceData
);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,
UInt32 DeviceInterfaceDetailDataSize,
ref UInt32 RequiredSize,
IntPtr DeviceInfoData
);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr DeviceInfoSet,
ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
IntPtr DeviceInterfaceDetailData,
UInt32 DeviceInterfaceDetailDataSize,
ref UInt32 RequiredSize,
IntPtr DeviceInfoData);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateFile(
string lpFileName,
UInt32 dwDesiredAccess,
UInt32 dwShareMode,
IntPtr lpSecurityAttributes,
UInt32 dwCreationDisposition,
UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile
);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReadFile(
IntPtr hFile,
[Out] byte[] lpBuffer,
UInt32 nNumberOfBytesToRead,
out UInt32 lpNumberOfBytesRead,
IntPtr lpOverlapped
);
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
// File I/O stuff
[DllImport("kernel32.dll")]
static public extern int CloseHandle(int hObject);
[DllImport("kernel32.dll")]
static public extern IntPtr WriteFile(IntPtr hFile, byte[] lpBuffer, UInt32 nNumberOfBytesToWrite, out UInt32 uNumberOfBytesWritten, IntPtr lpOverlapped);
// Managed Code wrappers for the DLL Calls
public void HidD_GetHidGuid()
{
HidApiDecl.HidD_GetHidGuid(ref _guid);
}
public IntPtr SetupDiGetClassDevs()
{
_hDevInfo = SetupDiGetClassDevs(ref _guid, null, IntPtr.Zero, DIGCF_INTERFACEDEVICE | DIGCF_PRESENT);
return _hDevInfo;
}
public bool SetupDiEnumDeviceInterfaces(UInt32 memberIndex)
{
_deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
_deviceInterfaceData.cbSize = Marshal.SizeOf(_deviceInterfaceData);
bool bResult = SetupDiEnumDeviceInterfaces(_hDevInfo, IntPtr.Zero, ref _guid, memberIndex, ref _deviceInterfaceData);
return bResult;
}
public bool SetupDiGetDeviceInterfaceDetail()
{
UInt32 uDeviceInterfaceDetailDataSize = 0, uRequiredSize = 0;
_deviceInterfaceDetailData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
_deviceInterfaceDetailData.cbSize = 5; // …
SetupDiGetDeviceInterfaceDetail(
_hDevInfo,
ref _deviceInterfaceData,
IntPtr.Zero,
uDeviceInterfaceDetailDataSize,
ref uRequiredSize,
IntPtr.Zero);
uDeviceInterfaceDetailDataSize = uRequiredSize;
bool bResult = SetupDiGetDeviceInterfaceDetail(
_hDevInfo,
ref _deviceInterfaceData,
ref _deviceInterfaceDetailData,
uDeviceInterfaceDetailDataSize,
ref uRequiredSize,
IntPtr.Zero);
_strDevicePathName = _deviceInterfaceDetailData.DevicePath;
return bResult;
}
public int CreateFile(string DeviceName)
{
_hHid = CreateFile(
DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if (_hHid == (IntPtr)INVALID_HANDLE_VALUE) return 0;
else return 1;
}
public int CloseHandle()
{
_hHid = (IntPtr)INVALID_HANDLE_VALUE;
// return CloseHandle(hObject); // DOES NOT WORK (hangs most of the time)
return -1;
}
public bool HidD_GetPreparsedData(ref IntPtr pHidpPreparsedData)
{
return HidApiDecl.HidD_GetPreparsedData(_hHid, ref pHidpPreparsedData);
}
public int HidP_GetCaps(IntPtr pHidpPreparsedData)
{
_hidpCaps = new HIDP_CAPS();
return HidApiDecl.HidP_GetCaps(pHidpPreparsedData, ref _hidpCaps);
}
public byte[] ReadFile(UInt32 uInputReportByteLength)
{
UInt32 uBytesRead = 0;
if (_hHid == (IntPtr)INVALID_HANDLE_VALUE) return null;
byte[] buffer = new byte[uInputReportByteLength];
if (ReadFile(_hHid, buffer, uInputReportByteLength, out uBytesRead, IntPtr.Zero)) return buffer;
else return null;
}
public bool SetupDiDestroyDeviceInfoList()
{
bool bResult = SetupDiDestroyDeviceInfoList(_hDevInfo);
_hDevInfo = (IntPtr)INVALID_HANDLE_VALUE;
return bResult;
}
public bool HidD_FreePreparsedData(IntPtr pHidpPreparsedData)
{
return SetupDiDestroyDeviceInfoList(pHidpPreparsedData);
}
}
}