forked from mthiffau/usbeject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Native.cs
203 lines (173 loc) · 6.91 KB
/
Native.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
// UsbEject version 1.0 March 2006
// written by Simon Mourier <email: simon [underscore] mourier [at] hotmail [dot] com>
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace UsbEject.Library
{
internal sealed class Native
{
// from winuser.h
internal const int WM_DEVICECHANGE = 0x0219;
// from winbase.h
internal const int INVALID_HANDLE_VALUE = -1;
internal const int GENERIC_READ = unchecked((int)0x80000000);
internal const int FILE_SHARE_READ = 0x00000001;
internal const int FILE_SHARE_WRITE = 0x00000002;
internal const int OPEN_EXISTING = 3;
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool GetVolumeNameForVolumeMountPoint(
string volumeName,
StringBuilder uniqueVolumeName,
int uniqueNameBufferCapacity);
[DllImport("Kernel32.dll", SetLastError = true)]
internal static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("Kernel32.dll", SetLastError = true)]
internal static extern bool DeviceIoControl(IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize, out int lpBytesReturned, IntPtr lpOverlapped);
[DllImport("Kernel32.dll", SetLastError = true)]
internal static extern bool CloseHandle(IntPtr hObject);
// from winerror.h
internal const int ERROR_NO_MORE_ITEMS = 259;
internal const int ERROR_INSUFFICIENT_BUFFER = 122;
internal const int ERROR_INVALID_DATA = 13;
// from winioctl.h
internal const string GUID_DEVINTERFACE_VOLUME = "53f5630d-b6bf-11d0-94f2-00a0c91efb8b";
internal const string GUID_DEVINTERFACE_DISK = "53f56307-b6bf-11d0-94f2-00a0c91efb8b";
internal const int IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = 0x00560000;
internal const int IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x002d1080;
[StructLayout(LayoutKind.Sequential)]
internal struct DISK_EXTENT
{
internal int DiskNumber;
internal long StartingOffset;
internal long ExtentLength;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STORAGE_DEVICE_NUMBER
{
public int DeviceType;
public int DeviceNumber;
public int PartitionNumber;
}
// from cfg.h
internal enum PNP_VETO_TYPE
{
Ok,
TypeUnknown,
LegacyDevice,
PendingClose,
WindowsApp,
WindowsService,
OutstandingOpen,
Device,
Driver,
IllegalDeviceRequest,
InsufficientPower,
NonDisableable,
LegacyDriver,
}
// from cfgmgr32.h
[DllImport("setupapi.dll")]
internal static extern int CM_Get_Parent(
ref int pdnDevInst,
int dnDevInst,
int ulFlags);
[DllImport("setupapi.dll")]
internal static extern int CM_Get_Device_ID(
int dnDevInst,
StringBuilder buffer,
int bufferLen,
int ulFlags);
[DllImport("setupapi.dll")]
internal static extern int CM_Request_Device_Eject(
int dnDevInst,
out PNP_VETO_TYPE pVetoType,
StringBuilder pszVetoName,
int ulNameLength,
int ulFlags
);
[DllImport("setupapi.dll", EntryPoint = "CM_Request_Device_Eject")]
internal static extern int CM_Request_Device_Eject_NoUi(
int dnDevInst,
IntPtr pVetoType,
StringBuilder pszVetoName,
int ulNameLength,
int ulFlags
);
// from setupapi.h
internal const int DIGCF_PRESENT = (0x00000002);
internal const int DIGCF_DEVICEINTERFACE = (0x00000010);
internal const int SPDRP_DEVICEDESC = 0x00000000;
internal const int SPDRP_CAPABILITIES = 0x0000000F;
internal const int SPDRP_CLASS = 0x00000007;
internal const int SPDRP_CLASSGUID = 0x00000008;
internal const int SPDRP_FRIENDLYNAME = 0x0000000C;
[StructLayout(LayoutKind.Sequential)]
internal class SP_DEVINFO_DATA
{
internal int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
internal Guid classGuid = Guid.Empty; // temp
internal int devInst = 0; // dumy
internal int reserved = 0;
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]
internal struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
internal int cbSize;
internal short devicePath;
}
[StructLayout(LayoutKind.Sequential)]
internal class SP_DEVICE_INTERFACE_DATA
{
internal int cbSize = Marshal.SizeOf(typeof(SP_DEVICE_INTERFACE_DATA));
internal Guid interfaceClassGuid = Guid.Empty; // temp
internal int flags = 0;
internal int reserved = 0;
}
[DllImport("setupapi.dll")]
internal static extern IntPtr SetupDiGetClassDevs(
ref Guid classGuid,
int enumerator,
IntPtr hwndParent,
int flags);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool SetupDiEnumDeviceInterfaces(
IntPtr deviceInfoSet,
SP_DEVINFO_DATA deviceInfoData,
ref Guid interfaceClassGuid,
int memberIndex,
SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
[DllImport("setupapi.dll")]
internal static extern bool SetupDiOpenDeviceInfo(
IntPtr deviceInfoSet,
string deviceInstanceId,
IntPtr hwndParent,
int openFlags,
SP_DEVINFO_DATA deviceInfoData
);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr deviceInfoSet,
SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
IntPtr deviceInterfaceDetailData,
int deviceInterfaceDetailDataSize,
ref int requiredSize,
SP_DEVINFO_DATA deviceInfoData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiGetDeviceRegistryProperty(
IntPtr deviceInfoSet,
SP_DEVINFO_DATA deviceInfoData,
int property,
out int propertyRegDataType,
IntPtr propertyBuffer,
int propertyBufferSize,
out int requiredSize
);
[DllImport("setupapi.dll")]
internal static extern uint SetupDiDestroyDeviceInfoList(
IntPtr deviceInfoSet);
private Native()
{
}
}
}