-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
436 lines (366 loc) · 11.5 KB
/
main.cpp
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include "resource.h"
#include <string>
#include <stdexcept>
class DriverServiceManager {
private:
SC_HANDLE scmHandle = NULL;
HANDLE hDevice = NULL;
bool isDriverRun = false;
public:
DriverServiceManager() {
scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!scmHandle) {
throw "Failed to open service manager.";
}
}
bool AddDriver(const LPCTSTR driverName, const LPCTSTR driverPath) {
if (!scmHandle) {
return false;
}
SC_HANDLE hService = CreateService(
scmHandle,
driverName,
driverName,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
driverPath,
NULL,
NULL,
NULL,
NULL,
NULL
);
if (!hService) {
return false;
}
CloseServiceHandle(hService);
return true;
}
bool RemoveDriver(const LPTSTR driverName) {
if (!scmHandle) {
return false;
}
SC_HANDLE hService = OpenService(
scmHandle,
driverName,
DELETE
);
if (!hService) {
return false;
}
if (!isDriverRun) {
SERVICE_STATUS status;
if (!ControlService(hService, SERVICE_CONTROL_STOP, &status)) {
CloseServiceHandle(hService);
return false;
}
}
if (!DeleteService(hService)) {
CloseServiceHandle(hService);
return false;
}
CloseServiceHandle(hService);
return true;
}
bool StartDriver(const LPTSTR driverName) {
if (!scmHandle) {
return false;
}
SC_HANDLE hService = OpenService(
scmHandle,
driverName,
SERVICE_START
);
if (!hService) {
return false;
}
if (!StartService(hService, 0, NULL)) {
CloseServiceHandle(hService);
return false;
}
CloseServiceHandle(hService);
isDriverRun = true;
return true;
}
bool StopDriver(const LPTSTR driverName) {
if (!scmHandle) {
return false;
}
SC_HANDLE hService = OpenService(
scmHandle,
driverName,
SERVICE_ALL_ACCESS
);
if (!hService) {
return false;
}
SERVICE_STATUS status;
if (!ControlService(hService, SERVICE_CONTROL_STOP, &status)) {
CloseServiceHandle(hService);
return false;
}
CloseServiceHandle(hService);
isDriverRun = false;
return true;
}
bool OpenDriver(const std::wstring& driverName) {
std::wstring driverPath = L"\\\\.\\" + driverName;
hDevice = CreateFile(
(LPSTR)driverPath.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hDevice == INVALID_HANDLE_VALUE) {
return false;
}
return true;
}
bool CloseDriver(const LPTSTR driverName) {
if (!CloseHandle(hDevice)) {
return false;
}
return true;
}
~DriverServiceManager() {
if (scmHandle) CloseServiceHandle(scmHandle);
}
};
static TCHAR szWindowClass[] = _T("Instdrv");
static TCHAR szTitle[] = _T("Instdrv");
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(wcex.hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
hInst = hInstance;
HWND hWnd = CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
1000, 340,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
void CreateControls(HWND hWnd) {
CreateWindow(_T("EDIT"), _T(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
260, 10, 690, 20,
hWnd, (HMENU)IDC_EDIT_FILE, NULL, NULL);
CreateWindow(_T("EDIT"), _T(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
5, 70, 240, 20,
hWnd, (HMENU)IDC_EDIT_SERVICE, NULL, NULL);
CreateWindow(_T("EDIT"), _T(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
5, 120, 240, 20,
hWnd, (HMENU)IDC_EDIT_SYMBOLIC, NULL, NULL);
CreateWindow(_T("EDIT"), _T(""),
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_MULTILINE,
260, 40, 690, 250,
hWnd, (HMENU)IDC_MAIN, NULL, NULL);
CreateWindow(_T("BUTTON"), _T("Add"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
5, 150, 110, 25,
hWnd, (HMENU)IDC_ADD, NULL, NULL);
CreateWindow(_T("BUTTON"), _T("Start"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
5, 180, 110, 25,
hWnd, (HMENU)IDC_START, NULL, NULL);
CreateWindow(_T("BUTTON"), _T("Open"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
5, 210, 110, 25,
hWnd, (HMENU)IDC_OPEN, NULL, NULL);
CreateWindow(_T("BUTTON"), _T("Remove"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
135, 150, 110, 25,
hWnd, (HMENU)IDC_REMOVE, NULL, NULL);
CreateWindow(_T("BUTTON"), _T("Stop"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
135, 180, 110, 25,
hWnd, (HMENU)IDC_STOP, NULL, NULL);
CreateWindow(_T("BUTTON"), _T("Close"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
135, 210, 110, 25,
hWnd, (HMENU)IDC_CLOSE, NULL, NULL);
CreateWindow(_T("BUTTON"), _T("..."),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
960, 10, 20, 20,
hWnd, (HMENU)IDC_BROWSE, NULL, NULL);
}
void DrawStaticText(HDC hdc) {
TextOut(hdc, 180, 11, _T("File path:"), _tcslen(_T("File path:")));
TextOut(hdc, 80, 50, _T("Service name:"), _tcslen(_T("Service name:")));
TextOut(hdc, 60, 100, _T("Symbolic Link Name:"), _tcslen(_T("Symbolic Link Name:")));
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
CreateControls(hWnd);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
DrawStaticText(hdc);
EndPaint(hWnd, &ps);
break;
case WM_COMMAND: {
switch (LOWORD(wParam))
{
case IDC_ADD: {
TCHAR driverPath[260];
TCHAR driverName[260];
TCHAR symbolicLink[260];
GetDlgItemText(hWnd, IDC_EDIT_FILE, driverPath, 260);
GetDlgItemText(hWnd, IDC_EDIT_SERVICE, driverName, 260);
GetDlgItemText(hWnd, IDC_EDIT_SYMBOLIC, symbolicLink, 260);
DriverServiceManager dsm;
if (dsm.AddDriver(driverName, driverPath)) {
MessageBox(hWnd, _T("Driver added successfully."), _T("Success"), MB_OK);
}
else {
MessageBox(hWnd, _T("Failed to add driver."), _T("Error"), MB_OK);
}
}
break;
case IDC_REMOVE: {
TCHAR driverName[260];
GetDlgItemText(hWnd, IDC_EDIT_SERVICE, driverName, 260);
DriverServiceManager dsm;
if (dsm.RemoveDriver(driverName)) {
MessageBox(hWnd, _T("Driver removed successfully."), _T("Success"), MB_OK);
}
else {
MessageBox(hWnd, _T("Failed to remove driver."), _T("Error"), MB_OK);
}
}
break;
case IDC_START: {
TCHAR driverName[260];
GetDlgItemText(hWnd, IDC_EDIT_SERVICE, driverName, 260);
DriverServiceManager dsm;
if (dsm.StartDriver(driverName)) {
MessageBox(hWnd, _T("Driver started successfully."), _T("Success"), MB_OK);
}
else {
MessageBox(hWnd, _T("Failed to start driver."), _T("Error"), MB_OK);
}
}
break;
case IDC_STOP: {
TCHAR driverName[260];
GetDlgItemText(hWnd, IDC_EDIT_SERVICE, driverName, 260);
DriverServiceManager dsm;
if (dsm.StopDriver(driverName)) {
MessageBox(hWnd, _T("Driver stopped successfully."), _T("Success"), MB_OK);
}
else {
MessageBox(hWnd, _T("Failed to stop driver."), _T("Error"), MB_OK);
}
}
break;
case IDC_OPEN: {
TCHAR driverName[260];
GetDlgItemText(hWnd, IDC_EDIT_SERVICE, driverName, 260);
DriverServiceManager dsm;
if (dsm.OpenDriver((std::wstring&)driverName)) {
MessageBox(hWnd, _T("Driver opened successfully."), _T("Success"), MB_OK);
}
else {
MessageBox(hWnd, _T("Failed to open driver."), _T("Error"), MB_OK);
}
}
break;
case IDC_CLOSE: {
TCHAR driverName[260];
GetDlgItemText(hWnd, IDC_EDIT_SERVICE, driverName, 260);
DriverServiceManager dsm;
if (dsm.CloseDriver(driverName)) {
MessageBox(hWnd, _T("Driver closed successfully."), _T("Success"), MB_OK);
}
else {
MessageBox(hWnd, _T("Failed to close driver."), _T("Error"), MB_OK);
}
}
break;
case IDC_BROWSE: {
OPENFILENAME ofn;
TCHAR szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile) / sizeof(TCHAR);
ofn.lpstrFilter = _T("SYS Files\0*.sys\0All Files\0*.*\0");
ofn.nFilterIndex = 1;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn) == TRUE) {
SetDlgItemText(hWnd, IDC_EDIT_FILE, ofn.lpstrFile);
}
}
break;
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}