-
Notifications
You must be signed in to change notification settings - Fork 17
/
Device.cs
290 lines (263 loc) · 8.59 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
// UsbEject version 1.0 March 2006
// written by Simon Mourier <email: simon [underscore] mourier [at] hotmail [dot] com>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
namespace UsbEject.Library
{
/// <summary>
/// A generic base class for physical devices.
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public class Device : IComparable
{
private string _path;
private DeviceClass _deviceClass;
private string _description;
private string _class;
private string _classGuid;
private int _disknum;
private Device _parent;
private int _index;
private DeviceCapabilities _capabilities = DeviceCapabilities.Unknown;
private List<Device> _removableDevices;
private string _friendlyName;
private Native.SP_DEVINFO_DATA _deviceInfoData;
internal Device(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index, int disknum = -1)
{
if (deviceClass == null)
throw new ArgumentNullException("deviceClass");
if (deviceInfoData == null)
throw new ArgumentNullException("deviceInfoData");
_deviceClass = deviceClass;
_path = path; // may be null
_deviceInfoData = deviceInfoData;
_index = index;
_disknum = disknum;
}
/// <summary>
/// Gets the device's index.
/// </summary>
public int Index
{
get
{
return _index;
}
}
/// <summary>
/// Gets the device's class instance.
/// </summary>
[Browsable(false)]
public DeviceClass DeviceClass
{
get
{
return _deviceClass;
}
}
/// <summary>
/// Gets the device's path.
/// </summary>
public string Path
{
get
{
if (_path == null)
{
}
return _path;
}
}
public int DiskNumber
{
get
{
return _disknum;
}
}
/// <summary>
/// Gets the device's instance handle.
/// </summary>
public uint InstanceHandle
{
get
{
return _deviceInfoData.devInst;
}
}
/// <summary>
/// Gets the device's class name.
/// </summary>
public string Class
{
get
{
if (_class == null)
{
_class = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_CLASS, null);
}
return _class;
}
}
/// <summary>
/// Gets the device's class Guid as a string.
/// </summary>
public string ClassGuid
{
get
{
if (_classGuid == null)
{
_classGuid = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_CLASSGUID, null);
}
return _classGuid;
}
}
/// <summary>
/// Gets the device's description.
/// </summary>
public string Description
{
get
{
if (_description == null)
{
_description = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_DEVICEDESC, null);
}
return _description;
}
}
/// <summary>
/// Gets the device's friendly name.
/// </summary>
public string FriendlyName
{
get
{
if (_friendlyName == null)
{
_friendlyName = _deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_FRIENDLYNAME, null);
}
return _friendlyName;
}
}
/// <summary>
/// Gets the device's capabilities.
/// </summary>
public DeviceCapabilities Capabilities
{
get
{
if (_capabilities == DeviceCapabilities.Unknown)
{
_capabilities = (DeviceCapabilities)_deviceClass.GetProperty(_deviceInfoData, Native.SPDRP_CAPABILITIES, 0);
}
return _capabilities;
}
}
/// <summary>
/// Gets a value indicating whether this device is a USB device.
/// </summary>
public virtual bool IsUsb
{
get
{
if (Class == "USB")
return true;
if (Parent == null)
return false;
return Parent.IsUsb;
}
}
/// <summary>
/// Gets the device's parent device or null if this device has not parent.
/// </summary>
public Device Parent
{
get
{
if (_parent == null)
{
int parentDevInst = 0;
int hr = Native.CM_Get_Parent(ref parentDevInst, _deviceInfoData.devInst, 0);
if (hr == 0)
{
_parent = new Device(_deviceClass, _deviceClass.GetInfo(parentDevInst), null, -1);
}
}
return _parent;
}
}
/// <summary>
/// Gets this device's list of removable devices.
/// Removable devices are parent devices that can be removed.
/// </summary>
public virtual List<Device> RemovableDevices
{
get
{
if (_removableDevices == null)
{
_removableDevices = new List<Device>();
if ((Capabilities & DeviceCapabilities.Removable) != 0)
{
_removableDevices.Add(this);
}
else
{
if (Parent != null)
{
foreach (Device device in Parent.RemovableDevices)
{
_removableDevices.Add(device);
}
}
}
}
return _removableDevices;
}
}
/// <summary>
/// Ejects the device.
/// </summary>
/// <param name="allowUI">Pass true to allow the Windows shell to display any related UI element, false otherwise.</param>
/// <returns>null if no error occured, otherwise a contextual text.</returns>
public string Eject(bool allowUI)
{
foreach (Device device in RemovableDevices)
{
if (allowUI)
{
Native.CM_Request_Device_Eject_NoUi(device.InstanceHandle, IntPtr.Zero, null, 0, 0);
// don't handle errors, there should be a UI for this
}
else
{
StringBuilder sb = new StringBuilder(1024);
Native.PNP_VETO_TYPE veto;
int hr = Native.CM_Request_Device_Eject(device.InstanceHandle, out veto, sb, sb.Capacity, 0);
if (hr != 0)
throw new Win32Exception(hr);
if (veto != Native.PNP_VETO_TYPE.Ok)
return veto.ToString();
}
}
return null;
}
/// <summary>
/// Compares the current instance with another object of the same type.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A 32-bit signed integer that indicates the relative order of the comparands.</returns>
public virtual int CompareTo(object obj)
{
Device device = obj as Device;
if (device == null)
throw new ArgumentException();
return Index.CompareTo(device.Index);
}
}
}