-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathUSBController.cs
250 lines (216 loc) · 9.15 KB
/
USBController.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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using Vanara.PInvoke;
namespace FullTrustProcess
{
public static class USBController
{
[DllImport("setupapi.dll")]
private static extern int CM_Get_Parent(
ref uint pdnDevInst,
uint dnDevInst,
int ulFlags);
[DllImport("setupapi.dll", CharSet = CharSet.Unicode)]
private static extern int CM_Request_Device_Eject(
uint dnDevInst,
out PNP_VETO_TYPE pVetoType,
StringBuilder pszVetoName,
int ulNameLength,
int ulFlags);
[DllImport("setupapi.dll", EntryPoint = "CM_Request_Device_Eject", CharSet = CharSet.Unicode)]
private static extern int CM_Request_Device_Eject_NoUi(
uint dnDevInst,
IntPtr pVetoType,
StringBuilder pszVetoName,
int ulNameLength,
int ulFlags);
private enum PNP_VETO_TYPE
{
PNP_VetoTypeUnknown = 0,
PNP_VetoLegacyDevice = 1,
PNP_VetoPendingClose = 2,
PNP_VetoWindowsApp = 3,
PNP_VetoWindowsService = 4,
PNP_VetoOutstandingOpen = 5,
PNP_VetoDevice = 6,
PNP_VetoDriver = 7,
PNP_VetoIllegalDeviceRequest = 8,
PNP_VetoInsufficientPower = 9,
PNP_VetoNonDisableable = 10,
PNP_VetoLegacyDriver = 11,
PNP_VetoInsufficientRights = 12
}
private const string GUID_DEVINTERFACE_VOLUME = "53f5630d-b6bf-11d0-94f2-00a0c91efb8b";
private const string GUID_DEVINTERFACE_DISK = "53f56307-b6bf-11d0-94f2-00a0c91efb8b";
private const string GUID_DEVINTERFACE_FLOPPY = "53f56311-b6bf-11d0-94f2-00a0c91efb8b";
private const string GUID_DEVINTERFACE_CDROM = "53f56308-b6bf-11d0-94f2-00a0c91efb8b";
private const int CR_SUCCESS = 0;
private static int GetDeviceNumber(HFILE DeviceHandle)
{
IntPtr buffer = Marshal.AllocHGlobal(1024);
try
{
if (Kernel32.DeviceIoControl(DeviceHandle, Kernel32.IOControlCode.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, buffer, 1024, out uint bytesReturned))
{
if (bytesReturned > 0)
{
return Convert.ToInt32(Marshal.PtrToStructure<Kernel32.STORAGE_DEVICE_NUMBER>(buffer).DeviceNumber);
}
else
{
return -1;
}
}
else
{
return -1;
}
}
catch
{
return -1;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
private static uint GetDrivesDevInstByDeviceNumber(long DeviceNumber, Kernel32.DRIVE_TYPE DriveType)
{
Guid Type_Guid;
switch (DriveType)
{
case Kernel32.DRIVE_TYPE.DRIVE_REMOVABLE:
case Kernel32.DRIVE_TYPE.DRIVE_FIXED:
{
Type_Guid = new Guid(GUID_DEVINTERFACE_DISK);
break;
}
case Kernel32.DRIVE_TYPE.DRIVE_CDROM:
{
Type_Guid = new Guid(GUID_DEVINTERFACE_CDROM);
break;
}
default:
{
throw new ArgumentException("Parameter is invalid", nameof(DriveType));
}
}
using (SetupAPI.SafeHDEVINFO hDevInfo = SetupAPI.SetupDiGetClassDevs(Type_Guid, Flags: SetupAPI.DIGCF.DIGCF_PRESENT | SetupAPI.DIGCF.DIGCF_DEVICEINTERFACE))
{
if (hDevInfo.IsInvalid || hDevInfo.IsNull)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
for (uint Index = 0; ; Index++)
{
SetupAPI.SP_DEVICE_INTERFACE_DATA Interface_Data = new SetupAPI.SP_DEVICE_INTERFACE_DATA
{
cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(SetupAPI.SP_DEVICE_INTERFACE_DATA)))
};
if (!SetupAPI.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, Type_Guid, Index, ref Interface_Data))
{
int Error = Marshal.GetLastWin32Error();
if (Error != Win32Error.ERROR_NO_MORE_ITEMS)
{
throw new Win32Exception(Error);
}
break;
}
SetupAPI.SP_DEVINFO_DATA devData = new SetupAPI.SP_DEVINFO_DATA
{
cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(SetupAPI.SP_DEVINFO_DATA)))
};
if (!SetupAPI.SetupDiGetDeviceInterfaceDetail(hDevInfo, Interface_Data, IntPtr.Zero, 0, out uint Size, ref devData))
{
int Error = Marshal.GetLastWin32Error();
if (Error != Win32Error.ERROR_INSUFFICIENT_BUFFER)
{
throw new Win32Exception(Error);
}
}
IntPtr Buffer = Marshal.AllocHGlobal(Convert.ToInt32(Size));
try
{
SetupAPI.SP_DEVICE_INTERFACE_DETAIL_DATA Interface_Detail_Data = new SetupAPI.SP_DEVICE_INTERFACE_DETAIL_DATA
{
cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(SetupAPI.SP_DEVICE_INTERFACE_DETAIL_DATA)))
};
Marshal.StructureToPtr(Interface_Detail_Data, Buffer, false);
if (!SetupAPI.SetupDiGetDeviceInterfaceDetail(hDevInfo, Interface_Data, Buffer, Size, out _, ref devData))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
string DevicePath = Marshal.PtrToStringAuto((IntPtr)(Buffer.ToInt64() + Marshal.SizeOf(typeof(int))));
using (Kernel32.SafeHFILE hDrive = Kernel32.CreateFile(DevicePath, 0, FileShare.ReadWrite, null, FileMode.Open, FileFlagsAndAttributes.SECURITY_ANONYMOUS))
{
if (!hDrive.IsInvalid && !hDrive.IsNull && DeviceNumber == GetDeviceNumber(hDrive))
{
return devData.DevInst;
}
}
}
finally
{
Marshal.FreeHGlobal(Buffer);
}
}
}
return 0;
}
public static bool EjectDevice(string Path)
{
if (string.IsNullOrEmpty(Path) || string.IsNullOrEmpty(System.IO.Path.GetPathRoot(Path)))
{
throw new ArgumentNullException(nameof(Path), "Argument is not legal");
}
try
{
int DeviceNumber = 0;
using (Kernel32.SafeHFILE hVolume = Kernel32.CreateFile($@"\\.\{Path.Substring(0, 2)}", 0, FileShare.ReadWrite, null, FileMode.Open, FileFlagsAndAttributes.SECURITY_ANONYMOUS))
{
if (hVolume.IsNull || hVolume.IsInvalid)
{
return false;
}
DeviceNumber = GetDeviceNumber(hVolume);
}
if (DeviceNumber >= 0)
{
uint DevInst = GetDrivesDevInstByDeviceNumber(DeviceNumber, Kernel32.GetDriveType(Path));
if (DevInst == 0)
{
return false;
}
uint DevInstParent = 0;
if (CM_Get_Parent(ref DevInstParent, DevInst, 0) == CR_SUCCESS)
{
for (int i = 0; i < 5; i++)
{
if (CM_Request_Device_Eject(DevInstParent, out PNP_VETO_TYPE Result, null, 0, 0) == CR_SUCCESS)
{
return true;
}
else
{
Debug.WriteLine($"Could not reject the USB device, PNP_VETO reason: {Enum.GetName(typeof(PNP_VETO_TYPE), Result)}");
}
Thread.Sleep(300);
}
}
}
return false;
}
catch (Exception ex)
{
Debug.WriteLine($"Could not reject the USB device, exception was threw, reason: {ex.Message}");
return false;
}
}
}
}