-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDeviceClass.cs
284 lines (247 loc) · 11.5 KB
/
DeviceClass.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
// UsbEject version 1.0 March 2006
// written by Simon Mourier <email: simon [underscore] mourier [at] hotmail [dot] com>
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace UsbEject.Library
{
/// <summary>
/// A generic base class for physical device classes.
/// </summary>
public abstract class DeviceClass : IDisposable
{
private IntPtr _deviceInfoSet;
private Guid _classGuid;
private List<Device> _devices;
protected DeviceClass(Guid classGuid)
:this(classGuid, IntPtr.Zero)
{
}
internal virtual Device CreateDevice(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index, int disknum = -1)
{
return new Device(deviceClass, deviceInfoData, path, index, disknum);
}
/// <summary>
/// Initializes a new instance of the DeviceClass class.
/// </summary>
/// <param name="classGuid">A device class Guid.</param>
/// <param name="hwndParent">The handle of the top-level window to be used for any user interface or IntPtr.Zero for no handle.</param>
protected DeviceClass(Guid classGuid, IntPtr hwndParent)
{
_classGuid = classGuid;
_deviceInfoSet = Native.SetupDiGetClassDevs(ref _classGuid, 0, hwndParent, Native.DIGCF_DEVICEINTERFACE | Native.DIGCF_PRESENT);
if (_deviceInfoSet == (IntPtr)Native.INVALID_HANDLE_VALUE)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if (_deviceInfoSet != IntPtr.Zero)
{
Native.SetupDiDestroyDeviceInfoList(_deviceInfoSet);
_deviceInfoSet = IntPtr.Zero;
}
}
/// <summary>
/// Gets the device class's guid.
/// </summary>
public Guid ClassGuid
{
get
{
return _classGuid;
}
}
/// <summary>
/// Gets the list of devices of this device class.
/// </summary>
/// <value>
/// The devices.
/// </value>
/// <exception cref="System.ComponentModel.Win32Exception">
/// </exception>
public List<Device> Devices
{
get
{
if (_devices == null)
{
_devices = new List<Device>();
int index = 0;
while (true)
{
Native.SP_DEVICE_INTERFACE_DATA interfaceData = new Native.SP_DEVICE_INTERFACE_DATA();
interfaceData.cbSize = (uint)Marshal.SizeOf(interfaceData);
if (!Native.SetupDiEnumDeviceInterfaces(_deviceInfoSet, null , ref _classGuid, index, interfaceData))
{
int error = Marshal.GetLastWin32Error();
if (error != Native.ERROR_NO_MORE_ITEMS)
throw new Win32Exception(error);
break;
}
Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
devData.cbSize = (uint)Marshal.SizeOf(devData);
int size = 0;
if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, IntPtr.Zero, 0,ref size, devData))
{
int error = Marshal.GetLastWin32Error();
if (error != Native.ERROR_INSUFFICIENT_BUFFER)
throw new Win32Exception(error);
}
IntPtr buffer = Marshal.AllocHGlobal(size);
Native.SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new Native.SP_DEVICE_INTERFACE_DETAIL_DATA();
detailData.cbSize = Marshal.SizeOf(detailData);
Marshal.WriteInt32(buffer, IntPtr.Size);
//Marshal.StructureToPtr(detailData, buffer, false);
if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, buffer ,size, ref size, devData))
{
Marshal.FreeHGlobal(buffer);
throw new Win32Exception(Marshal.GetLastWin32Error());
}
var strPtr = new IntPtr(buffer.ToInt64() + 4);
string devicePath = Marshal.PtrToStringAuto(strPtr);
//IntPtr pDevicePath = (IntPtr)((int)buffer + Marshal.SizeOf(typeof(int)));
//string devicePath = Marshal.PtrToStringAuto(pDevicePath);
Marshal.FreeHGlobal(buffer);
if (_classGuid.Equals(new Guid(Native.GUID_DEVINTERFACE_DISK)))
{
// Find disks
IntPtr hFile = Native.CreateFile(devicePath, 0, Native.FILE_SHARE_READ | Native.FILE_SHARE_WRITE, IntPtr.Zero, Native.OPEN_EXISTING, 0, IntPtr.Zero);
if (hFile.ToInt32() == Native.INVALID_HANDLE_VALUE)
throw new Win32Exception(Marshal.GetLastWin32Error());
int bytesReturned = 0;
int numBufSize = 0x400; // some big size
IntPtr numBuffer = Marshal.AllocHGlobal(numBufSize);
Native.STORAGE_DEVICE_NUMBER disknum;
try
{
if (!Native.DeviceIoControl(hFile, Native.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, numBuffer, numBufSize, out bytesReturned, IntPtr.Zero))
{
Console.WriteLine("IOCTL failed.");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception calling ioctl: " + ex);
}
finally
{
Native.CloseHandle(hFile);
}
if (bytesReturned > 0)
disknum = (Native.STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(numBuffer, typeof(Native.STORAGE_DEVICE_NUMBER));
else
disknum = new Native.STORAGE_DEVICE_NUMBER() { DeviceNumber = -1, DeviceType = -1, PartitionNumber = -1 };
Device device = CreateDevice(this, devData, devicePath, index, disknum.DeviceNumber);
_devices.Add(device);
Marshal.FreeHGlobal(hFile);
}
else
{
Device device = CreateDevice(this, devData, devicePath, index);
_devices.Add(device);
}
index++;
}
_devices.Sort();
}
return _devices;
}
}
internal Native.SP_DEVINFO_DATA GetInfo(int dnDevInst)
{
StringBuilder sb = new StringBuilder(1024);
int hr = Native.CM_Get_Device_ID(dnDevInst, sb, sb.Capacity, 0);
if (hr != 0)
throw new Win32Exception(hr);
Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
devData.cbSize = (uint)Marshal.SizeOf(devData);
if (!Native.SetupDiOpenDeviceInfo(_deviceInfoSet, sb.ToString(), IntPtr.Zero, 0, devData))
throw new Win32Exception(Marshal.GetLastWin32Error());
return devData;
}
internal string GetProperty(Native.SP_DEVINFO_DATA devData, int property, string defaultValue)
{
if (devData == null)
throw new ArgumentNullException("devData");
int propertyRegDataType = 0;
int requiredSize;
int propertyBufferSize = 1024;
IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize);
if (!Native.SetupDiGetDeviceRegistryProperty(_deviceInfoSet,
devData,
property,
out propertyRegDataType,
propertyBuffer,
propertyBufferSize,
out requiredSize))
{
Marshal.FreeHGlobal(propertyBuffer);
int error = Marshal.GetLastWin32Error();
if (error != Native.ERROR_INVALID_DATA)
throw new Win32Exception(error);
return defaultValue;
}
string value = Marshal.PtrToStringAuto(propertyBuffer);
Marshal.FreeHGlobal(propertyBuffer);
return value;
}
internal int GetProperty(Native.SP_DEVINFO_DATA devData, int property, int defaultValue)
{
if (devData == null)
throw new ArgumentNullException("devData");
int propertyRegDataType = 0;
int requiredSize;
int propertyBufferSize = Marshal.SizeOf(typeof(int));
IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize);
if (!Native.SetupDiGetDeviceRegistryProperty(_deviceInfoSet,
devData,
property,
out propertyRegDataType,
propertyBuffer,
propertyBufferSize,
out requiredSize))
{
Marshal.FreeHGlobal(propertyBuffer);
int error = Marshal.GetLastWin32Error();
if (error != Native.ERROR_INVALID_DATA)
throw new Win32Exception(error);
return defaultValue;
}
int value = (int)Marshal.PtrToStructure(propertyBuffer, typeof(int));
Marshal.FreeHGlobal(propertyBuffer);
return value;
}
internal Guid GetProperty(Native.SP_DEVINFO_DATA devData, int property, Guid defaultValue)
{
if (devData == null)
throw new ArgumentNullException("devData");
int propertyRegDataType = 0;
int requiredSize;
int propertyBufferSize = Marshal.SizeOf(typeof(Guid));
IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize);
if (!Native.SetupDiGetDeviceRegistryProperty(_deviceInfoSet,
devData,
property,
out propertyRegDataType,
propertyBuffer,
propertyBufferSize,
out requiredSize))
{
Marshal.FreeHGlobal(propertyBuffer);
int error = Marshal.GetLastWin32Error();
if (error != Native.ERROR_INVALID_DATA)
throw new Win32Exception(error);
return defaultValue;
}
Guid value = (Guid)Marshal.PtrToStructure(propertyBuffer, typeof(Guid));
Marshal.FreeHGlobal(propertyBuffer);
return value;
}
}
}