forked from nomi-san/parsec-vdd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.cs
448 lines (373 loc) · 17.2 KB
/
Device.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace ParsecVDisplay
{
internal static unsafe class Device
{
public enum Status
{
OK,
INACCESSIBLE,
UNKNOWN,
UNKNOWN_PROBLEM,
DISABLED,
DRIVER_ERROR,
RESTART_REQUIRED,
DISABLED_SERVICE,
NOT_INSTALLED
}
public static Status QueryStatus(string guid, string devId)
{
var status = Status.INACCESSIBLE;
var devInfoData = new Native.SP_DEVINFO_DATA();
devInfoData.cbSize = sizeof(Native.SP_DEVINFO_DATA);
var classGuid = Guid.Parse(guid);
var devInfo = Native.SetupDiGetClassDevsA(ref classGuid, null, null, Native.DIGCF_PRESENT);
if (devInfo != Native.INVALID_HANDLE_VALUE)
{
bool foundProp = false;
uint deviceIndex = 0;
do
{
if (!Native.SetupDiEnumDeviceInfo(devInfo, deviceIndex, &devInfoData))
break;
int requiredSize = 0;
Native.SetupDiGetDeviceRegistryPropertyA(devInfo, &devInfoData,
Native.SPDRP_HARDWAREID, null, null, 0, &requiredSize);
if (requiredSize > 0)
{
uint regDataType = 0;
IntPtr propBuffer = Marshal.AllocHGlobal(requiredSize);
if (Native.SetupDiGetDeviceRegistryPropertyA(
devInfo,
&devInfoData,
Native.SPDRP_HARDWAREID,
®DataType,
(void*)propBuffer,
requiredSize,
&requiredSize))
{
if (regDataType == Native.REG_SZ || regDataType == Native.REG_MULTI_SZ)
{
for (IntPtr cp = propBuffer; ; cp += Native.lstrlenA(cp) + 1)
{
if (cp == (IntPtr)(0) || *(byte*)cp == 0 || (ulong)cp >= (ulong)(propBuffer + requiredSize))
{
status = Status.NOT_INSTALLED;
goto except;
}
if (devId.Equals(Marshal.PtrToStringAnsi(cp)))
break;
}
foundProp = true;
uint devStatus, devProblemNum;
if (Native.CM_Get_DevNode_Status(&devStatus, &devProblemNum, devInfoData.DevInst, 0) != Native.CR_SUCCESS)
{
status = Status.NOT_INSTALLED;
goto except;
}
if ((devStatus & (Native.DN_DRIVER_LOADED | Native.DN_STARTED)) != 0)
{
status = Status.OK;
}
else if ((devStatus & Native.DN_HAS_PROBLEM) != 0)
{
switch (devProblemNum)
{
case Native.CM_PROB_NEED_RESTART:
status = Status.RESTART_REQUIRED;
break;
case Native.CM_PROB_DISABLED:
case Native.CM_PROB_HARDWARE_DISABLED:
status = Status.DISABLED;
break;
case Native.CM_PROB_DISABLED_SERVICE:
status = Status.DISABLED_SERVICE;
break;
default:
if (devProblemNum == Native.CM_PROB_FAILED_POST_START)
status = Status.DRIVER_ERROR;
else
status = Status.UNKNOWN_PROBLEM;
break;
}
}
else
{
status = Status.UNKNOWN;
}
}
}
except:
Marshal.FreeHGlobal(propBuffer);
}
++deviceIndex;
} while (!foundProp);
if (!foundProp && Marshal.GetLastWin32Error() != 0)
status = Status.NOT_INSTALLED;
Native.SetupDiDestroyDeviceInfoList(devInfo);
}
return status;
}
public static bool OpenHandle(string guid, out IntPtr handle)
{
handle = IntPtr.Zero;
var interfaceGuid = Guid.Parse(guid);
var devInfo = Native.SetupDiGetClassDevsA(ref interfaceGuid,
null, null, Native.DIGCF_PRESENT | Native.DIGCF_DEVICEINTERFACE);
if (devInfo != Native.INVALID_HANDLE_VALUE)
{
var devInterface = new Native.SP_DEVICE_INTERFACE_DATA();
devInterface.cbSize = sizeof(Native.SP_DEVICE_INTERFACE_DATA);
for (uint i = 0; Native.SetupDiEnumDeviceInterfaces(devInfo, null, ref interfaceGuid, i, &devInterface); ++i)
{
int detailSize = 0;
Native.SetupDiGetDeviceInterfaceDetailA(devInfo, &devInterface, null, 0, &detailSize, null);
var detail = (Native.SP_DEVICE_INTERFACE_DETAIL_DATA_A*)Marshal.AllocHGlobal(detailSize);
detail->cbSize = sizeof(Native.SP_DEVICE_INTERFACE_DETAIL_DATA_A);
if (Native.SetupDiGetDeviceInterfaceDetailA(devInfo, &devInterface, detail, detailSize, &detailSize, null))
{
handle = Native.CreateFileA(&detail->DevicePath,
Native.GENERIC_READ | Native.GENERIC_WRITE,
Native.FILE_SHARE_READ | Native.FILE_SHARE_WRITE,
null,
Native.OPEN_EXISTING,
Native.FILE_ATTRIBUTE_NORMAL | Native.FILE_FLAG_NO_BUFFERING | Native.FILE_FLAG_OVERLAPPED | Native.FILE_FLAG_WRITE_THROUGH,
null);
if (handle != IntPtr.Zero && handle != Native.INVALID_HANDLE_VALUE)
break;
}
Marshal.FreeHGlobal((IntPtr)detail);
}
Native.SetupDiDestroyDeviceInfoList(devInfo);
}
return handle != IntPtr.Zero
&& handle != Native.INVALID_HANDLE_VALUE;
}
public static void CloseHandle(IntPtr handle)
{
if (handle != IntPtr.Zero && handle != Native.INVALID_HANDLE_VALUE)
{
Native.CloseHandle(handle);
}
}
public static string GetDeviceDescription(uint devInst)
{
uint propType;
int length = 128 * sizeof(ushort);
var buffer = stackalloc byte[length];
Native.CM_Get_DevNode_PropertyW(devInst,
ref Native.DEVPROPKEY.Device_DeviceDesc, &propType, buffer, &length, 0);
return Marshal.PtrToStringUni((IntPtr)buffer);
}
public static DateTime GetDeviceLastArrival(uint devInst)
{
uint propType;
long lastArrival;
int bufferLength = sizeof(long);
Native.CM_Get_DevNode_PropertyW(devInst,
ref Native.DEVPROPKEY.Device_LastArrivalDate, &propType, &lastArrival, &bufferLength, 0);
return DateTime.FromFileTime(lastArrival);
}
public static bool GetParentDeviceInstance(uint devInst, out uint parentInst, out string parentId)
{
if (Native.CM_Get_Parent(out parentInst, devInst, 0) == 0)
{
byte[] idBuffer = new byte[Native.MAX_DEVICE_ID_LEN];
Native.CM_Get_Device_IDA(parentInst, idBuffer, idBuffer.Length, 0);
parentId = Encoding.ASCII.GetString(idBuffer).TrimEnd('\0');
return true;
}
parentInst = 0;
parentId = string.Empty;
return false;
}
public static bool GetDeviceInstance(string deviceId, out uint devInst)
{
return Native.CM_Locate_DevNodeA(out devInst, deviceId, 0) == 0;
}
static class Native
{
public const int MAX_DEVICE_ID_LEN = 200;
public static readonly IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint FILE_SHARE_READ = 0x1;
public const uint FILE_SHARE_WRITE = 0x2;
public const uint OPEN_EXISTING = 3;
public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
public const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
public const uint FILE_FLAG_WRITE_THROUGH = 0x80000000;
public const uint DIGCF_PRESENT = 0x2;
public const uint DIGCF_DEVICEINTERFACE = 0x10;
public const uint SPDRP_HARDWAREID = 0x1;
public const uint REG_SZ = 1;
public const uint REG_MULTI_SZ = 7;
public const uint CR_SUCCESS = 0;
public const uint CM_PROB_NEED_RESTART = 0x0000000E; // requires restart
public const uint CM_PROB_DISABLED = 0x00000016; // devinst is disabled
public const uint CM_PROB_HARDWARE_DISABLED = 0x0000001D; // device disabled
public const uint CM_PROB_DISABLED_SERVICE = 0x00000020; // service's Start = 4
public const uint CM_PROB_FAILED_POST_START = 0x0000002B; // The drivers set the device state to failed
public const uint DN_DRIVER_LOADED = 0x00000002; // Has Register_Device_Driver
public const uint DN_STARTED = 0x00000008; // Is currently configured
public const uint DN_HAS_PROBLEM = 0x00000400; // Need device installer
public static bool IsValidHandle(IntPtr handle)
{
return handle != IntPtr.Zero
&& handle != (IntPtr)(-1);
}
[DllImport("kernel32.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int lstrlenA(
IntPtr lpString);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateFileA(
char* lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
void* lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
void* hTemplateFile);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeviceIoControl(
IntPtr device, uint code,
void* lpInBuffer, int nInBufferSize,
void* lpOutBuffer, int nOutBufferSize,
IntPtr lpBytesReturned,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetOverlappedResult(
IntPtr handle,
ref OVERLAPPED lpOverlapped,
out uint lpNumberOfBytesTransferred,
[MarshalAs(UnmanagedType.Bool)] bool bWait
);
[StructLayout(LayoutKind.Sequential)]
public struct OVERLAPPED
{
public IntPtr Internal;
public IntPtr InternalHigh;
public IntPtr Pointer;
public IntPtr hEvent;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public uint Flags;
IntPtr _Reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public uint DevInst;
IntPtr _Reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA_A
{
public int cbSize;
public char DevicePath;
}
[DllImport("setupapi.dll")]
public static extern IntPtr SetupDiGetClassDevsA(
ref Guid ClassGuid,
void* Enumerator,
void* hwndParent,
uint Flags);
[DllImport("setupapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiEnumDeviceInterfaces(
IntPtr DeviceInfoSet,
SP_DEVINFO_DATA* DeviceInfoData,
ref Guid InterfaceClassGuid,
uint MemberIndex,
SP_DEVICE_INTERFACE_DATA* DeviceInterfaceData);
[DllImport("setupapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiGetDeviceInterfaceDetailA(
IntPtr DeviceInfoSet,
SP_DEVICE_INTERFACE_DATA* DeviceInterfaceData,
void* DeviceInterfaceDetailData,
int DeviceInterfaceDetailDataSize,
int* RequiredSize,
SP_DEVINFO_DATA* DeviceInfoData);
[DllImport("setupapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiDestroyDeviceInfoList(
IntPtr DeviceInfoSet);
[DllImport("setupapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiEnumDeviceInfo(
IntPtr DeviceInfoSet,
uint MemberIndex,
SP_DEVINFO_DATA* DeviceInfoData);
[DllImport("setupapi.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiGetDeviceRegistryPropertyA(
IntPtr DeviceInfoSet,
SP_DEVINFO_DATA* DeviceInfoData,
uint Property,
uint* PropertyRegDataType,
void* PropertyBuffer,
int PropertyBufferSize,
int* RequiredSize);
[DllImport("setupapi.dll")]
public static extern uint CM_Get_DevNode_Status(
uint* pulStatus,
uint* pulProblemNumber,
uint dnDevInst,
uint ulFlags);
[DllImport("setupapi.dll")]
public static extern uint CM_Get_Parent(out uint pdnDevInst, uint dnDevInst, uint ulFlags);
[DllImport("setupapi.dll")]
public static extern uint CM_Get_Device_IDA(uint dnDevInst, byte[] Buffer, int BufferLen, uint ulFlags);
[StructLayout(LayoutKind.Sequential)]
public struct DEVPROPKEY
{
public Guid fmtid;
public uint pid;
public static DEVPROPKEY Device_LastArrivalDate = new DEVPROPKEY
{
fmtid = Guid.Parse("{83DA6326-97A6-4088-9453-A1923F573B29}"),
pid = 102,
};
public static DEVPROPKEY Device_DeviceDesc = new DEVPROPKEY
{
fmtid = Guid.Parse("{A45C254E-DF1C-4EFD-8020-67D146A850E0}"),
pid = 2
};
}
[DllImport("CfgMgr32.dll")]
public static extern uint CM_Get_DevNode_PropertyW(
uint dnDevInst,
ref DEVPROPKEY PropertyKey,
uint* PropertyType,
void* PropertyBuffer,
int* PropertyBufferSize,
uint ulFlags);
[DllImport("CfgMgr32.dll", CharSet = CharSet.Ansi)]
public static extern uint CM_Locate_DevNodeA(out uint devInst, string deviceId, uint flags);
}
}
}