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
|
/*++
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:
EventTest.c
Abstract:
Simple console test app for the event.sys driver.
Enviroment:
User Mode
Revision History:
--*/
//
// INCLUDES
//
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <strsafe.h>
#include "public.h"
BOOLEAN
ManageDriver(
_In_ LPCTSTR DriverName,
_In_ LPCTSTR ServiceName,
_In_ USHORT Function
);
BOOLEAN
SetupDriverName(
_Inout_updates_bytes_all_(BufferLength) PCHAR DriverLocation,
_In_ ULONG BufferLength
);
#define USAGE() {\
printf("event <delay> <0/1>\n");\
printf("\twhere <delay> = time to delay the event signal in seconds.\n");\
printf("\t 0 for IRP based and 1 for event based notification.\n");\
}
//
// MAIN
//
VOID __cdecl
main(
_In_ ULONG argc,
_In_reads_(argc) PCHAR argv[]
)
{
BOOL bStatus;
HANDLE hDevice;
ULONG ulReturnedLength;
REGISTER_EVENT registerEvent;
FLOAT fDelay = 3;
UINT type = EVENT_BASED;
DWORD errNum = 0;
TCHAR driverLocation[MAX_PATH] = { 0 };
if ( (argc < 3) || (argv[1] == NULL) || (argv[2] == NULL) ) {
USAGE();
exit(1);
}
if (sscanf_s( argv[1], "%f", &fDelay ) == 0) {
printf("sscanf_s failed\n");
exit(1);
}
if (sscanf_s( argv[2], "%d", &type ) == 0) {
printf("sscanf_s failed\n");
exit(1);
}
//
// open the device
//
if ((hDevice = CreateFile(
"\\\\.\\Event_Sample", // lpFileName
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
)) == INVALID_HANDLE_VALUE) {
errNum = GetLastError();
if (errNum != ERROR_FILE_NOT_FOUND) {
printf("CreateFile failed! ERROR_FILE_NOT_FOUND = %d\n", errNum);
return ;
}
//
// The driver is not started yet so let us the install driver.
// First setup full path to driver name.
//
if (!SetupDriverName(driverLocation, sizeof(driverLocation) )) {
return ;
}
if (!ManageDriver(DRIVER_NAME,
driverLocation,
DRIVER_FUNC_INSTALL
)) {
printf("Unable to install driver. \n");
//
// Error - remove driver.
//
ManageDriver(DRIVER_NAME,
driverLocation,
DRIVER_FUNC_REMOVE
);
return;
}
//
// Now open the device again.
//
hDevice = CreateFile(
"\\\\.\\Event_Sample", // lpFileName
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if ( hDevice == INVALID_HANDLE_VALUE ){
printf ( "Error: CreatFile Failed : %d\n", GetLastError());
return;
}
}
//
// set the event signal delay. Use relative time for this sample
//
registerEvent.DueTime.QuadPart = -((LONGLONG)(fDelay * 10.0E6));
registerEvent.Type = type;
if (type == EVENT_BASED) {
//
//
//
registerEvent.hEvent = CreateEvent(
NULL, // lpEventAttributes
TRUE, // bManualReset
FALSE, // bInitialState
#ifdef DBG
"TEST_EVENT" // use WinObj to view named events for DBG
#else
NULL // lpName
#endif
);
if ( !registerEvent.hEvent ) {
printf("CreateEvent error = %d\n", GetLastError() );
} else {
printf("Event HANDLE = %p\n", registerEvent.hEvent );
printf("Press any key to exit.\n");
while( !_kbhit() ) {
bStatus = DeviceIoControl(
hDevice, // Handle to device
IOCTL_REGISTER_EVENT, // IO Control code
®isterEvent, // Input Buffer to driver.
SIZEOF_REGISTER_EVENT, // Length of input buffer in bytes.
NULL, // Output Buffer from driver.
0, // Length of output buffer in bytes.
&ulReturnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
if ( !bStatus ) {
printf("Ioctl failed with code %d\n", GetLastError() );
break;
} else {
printf("Waiting for Event...\n");
WaitForSingleObject(registerEvent.hEvent,
INFINITE );
printf("Event signalled.\n\n");
ResetEvent( registerEvent.hEvent);
//printf("Event reset.\n");
}
}
}
}else if (type == IRP_BASED) {
printf("Press any key to exit.\n");
registerEvent.hEvent = NULL;
registerEvent.Type = IRP_BASED;
while( !_kbhit() ) {
bStatus = DeviceIoControl(
hDevice, // Handle to device
IOCTL_REGISTER_EVENT, // IO Control code
®isterEvent, // Input Buffer to driver.
SIZEOF_REGISTER_EVENT, // Length of input buffer in bytes.
NULL, // Output Buffer from driver.
0, // Length of output buffer in bytes.
&ulReturnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
if ( !bStatus ) {
printf("Ioctl failed with code %d\n", GetLastError() );
break;
}
printf("Event occurred.\n\n");
printf("\nRegistering event again....\n\n");
}
}else { //unknown type
USAGE();
}
//
// close the handle to the device.
//
CloseHandle(hDevice);
//
// Unload the driver if loaded. Ignore any errors.
//
if (driverLocation[0] != (TCHAR)0) {
ManageDriver(DRIVER_NAME,
driverLocation,
DRIVER_FUNC_REMOVE
);
}
return;
}
|