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
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
Enum.c
Abstract:
This application simulates the plugin, unplug or ejection
of devices.
Environment:
usermode console application
Revision History:
Eliyas Yakub Oct 14, 1998
--*/
#include <basetyps.h>
#include <stdlib.h>
#include <wtypes.h>
#include <strsafe.h>
#include <cfgmgr32.h>
#include <initguid.h>
#include <stdio.h>
#include <string.h>
#include <winioctl.h>
#include "public.h"
#include <dontuse.h>
//
// Prototypes
//
BOOLEAN
GetDevicePath(
_In_ LPCGUID InterfaceGuid,
_Out_writes_(BufLen) PWCHAR DevicePath,
_In_ size_t BufLen
);
BOOLEAN
OpenBusInterface(
_In_z_ LPCWSTR DevicePath
);
#define USAGE \
"Usage: Enum [-p SerialNo] Plugs in a device. SerialNo must be greater than zero.\n\
[-u SerialNo or 0] Unplugs device(s) - specify 0 to unplug all \
the devices enumerated so far.\n\
[-e SerialNo or 0] Ejects device(s) - specify 0 to eject all \
the devices enumerated so far.\n"
#define MAX_DEVPATH_LENGTH 256
BOOLEAN bPlugIn, bUnplug, bEject;
ULONG SerialNo;
INT __cdecl
main(
_In_ ULONG argc,
_In_reads_(argc) PCHAR argv[]
)
{
WCHAR devicePath[MAX_DEVPATH_LENGTH] = { 0 };
bPlugIn = bUnplug = bEject = FALSE;
if(argc <3) {
goto usage;
}
if(argv[1][0] == '-') {
if(tolower(argv[1][1]) == 'p') {
if(argv[2])
SerialNo = (USHORT)atol(argv[2]);
bPlugIn = TRUE;
}
else if(tolower(argv[1][1]) == 'u') {
if(argv[2])
SerialNo = (ULONG)atol(argv[2]);
bUnplug = TRUE;
}
else if(tolower(argv[1][1]) == 'e') {
if(argv[2])
SerialNo = (ULONG)atol(argv[2]);
bEject = TRUE;
}
else {
goto usage;
}
}
else
goto usage;
if(bPlugIn && 0 == SerialNo)
goto usage;
if (GetDevicePath(&GUID_DEVINTERFACE_BUSENUM_TOASTER,
devicePath,
sizeof(devicePath) / sizeof(devicePath[0]))) {
OpenBusInterface(devicePath);
}
return 0;
usage:
printf(USAGE);
exit(0);
}
BOOLEAN
OpenBusInterface (
_In_z_ LPCWSTR DevicePath
)
{
HANDLE file;
ULONG bytes;
BUSENUM_UNPLUG_HARDWARE unplug;
BUSENUM_EJECT_HARDWARE eject;
PBUSENUM_PLUGIN_HARDWARE hardware;
BOOLEAN bSuccess = FALSE;
printf("Opening %ws\n", DevicePath);
file = CreateFile(DevicePath,
GENERIC_READ, // Only read access
0, // FILE_SHARE_READ | FILE_SHARE_WRITE
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL); // No template file
if (INVALID_HANDLE_VALUE == file) {
printf("CreateFile failed: 0x%x", GetLastError());
goto End;
}
printf("Bus interface opened!!!\n");
//
// Enumerate Devices
//
if(bPlugIn) {
printf("SerialNo. of the device to be enumerated: %d\n", SerialNo);
hardware = malloc (bytes = (sizeof (BUSENUM_PLUGIN_HARDWARE) +
BUS_HARDWARE_IDS_LENGTH));
if(hardware) {
hardware->Size = sizeof (BUSENUM_PLUGIN_HARDWARE);
hardware->SerialNo = SerialNo;
} else {
printf("Couldn't allocate %d bytes for busenum plugin hardware structure.\n", bytes);
goto End;
}
//
// Allocate storage for the Device ID
//
memcpy (hardware->HardwareIDs,
BUS_HARDWARE_IDS,
BUS_HARDWARE_IDS_LENGTH);
if (!DeviceIoControl (file,
IOCTL_BUSENUM_PLUGIN_HARDWARE ,
hardware, bytes,
NULL, 0,
&bytes, NULL)) {
free (hardware);
printf("PlugIn failed:0x%x\n", GetLastError());
goto End;
}
free (hardware);
}
//
// Removes a device if given the specific Id of the device. Otherwise this
// ioctls removes all the devices that are enumerated so far.
//
if(bUnplug) {
printf("Unplugging device(s)....\n");
unplug.Size = bytes = sizeof (unplug);
unplug.SerialNo = SerialNo;
if (!DeviceIoControl (file,
IOCTL_BUSENUM_UNPLUG_HARDWARE,
&unplug, bytes,
NULL, 0,
&bytes, NULL)) {
printf("Unplug failed: 0x%x\n", GetLastError());
goto End;
}
}
//
// Ejects a device if given the specific Id of the device. Otherwise this
// ioctls ejects all the devices that are enumerated so far.
//
if(bEject) {
printf("Ejecting Device(s)\n");
eject.Size = bytes = sizeof (eject);
eject.SerialNo = SerialNo;
if (!DeviceIoControl (file,
IOCTL_BUSENUM_EJECT_HARDWARE,
&eject, bytes,
NULL, 0,
&bytes, NULL)) {
printf("Eject failed: 0x%x\n", GetLastError());
goto End;
}
}
printf("Success!!!\n");
bSuccess = TRUE;
End:
if (INVALID_HANDLE_VALUE != file) {
CloseHandle(file);
}
return bSuccess;
}
BOOLEAN
GetDevicePath(
_In_ LPCGUID InterfaceGuid,
_Out_writes_(BufLen) PWCHAR DevicePath,
_In_ size_t BufLen
)
{
CONFIGRET cr = CR_SUCCESS;
PWSTR deviceInterfaceList = NULL;
ULONG deviceInterfaceListLength = 0;
PWSTR nextInterface;
HRESULT hr = E_FAIL;
BOOLEAN bRet = TRUE;
cr = CM_Get_Device_Interface_List_Size(
&deviceInterfaceListLength,
(LPGUID)InterfaceGuid,
NULL,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
printf("Error 0x%x retrieving device interface list size.\n", cr);
goto clean0;
}
if (deviceInterfaceListLength <= 1) {
bRet = FALSE;
printf("Error: No active device interfaces found.\n"
" Is the sample driver loaded?");
goto clean0;
}
deviceInterfaceList = (PWSTR)malloc(deviceInterfaceListLength * sizeof(WCHAR));
if (deviceInterfaceList == NULL) {
printf("Error allocating memory for device interface list.\n");
goto clean0;
}
ZeroMemory(deviceInterfaceList, deviceInterfaceListLength * sizeof(WCHAR));
cr = CM_Get_Device_Interface_List(
(LPGUID)InterfaceGuid,
NULL,
deviceInterfaceList,
deviceInterfaceListLength,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
if (cr != CR_SUCCESS) {
printf("Error 0x%x retrieving device interface list.\n", cr);
goto clean0;
}
nextInterface = deviceInterfaceList + wcslen(deviceInterfaceList) + 1;
if (*nextInterface != UNICODE_NULL) {
printf("Warning: More than one device interface instance found. \n"
"Selecting first matching device.\n\n");
}
hr = StringCchCopy(DevicePath, BufLen, deviceInterfaceList);
if (FAILED(hr)) {
bRet = FALSE;
printf("Error: StringCchCopy failed with HRESULT 0x%x", hr);
goto clean0;
}
clean0:
if (deviceInterfaceList != NULL) {
free(deviceInterfaceList);
}
if (CR_SUCCESS != cr) {
bRet = FALSE;
}
return bRet;
}
|