[RXCE]
[reactos.git] / rostests / win32 / DriverLoading / Application / umode.c
1 #include "DriverTester.h"
2
3
4 BOOL
5 RegisterDriver(LPCWSTR lpDriverName,
6 LPCWSTR lpPathName)
7 {
8 SC_HANDLE hSCManager;
9 SC_HANDLE hService;
10
11 hSCManager = OpenSCManagerW(NULL,
12 NULL,
13 SC_MANAGER_ALL_ACCESS);
14 if (!hSCManager)
15 return FALSE;
16
17 retry:
18 hService = CreateServiceW(hSCManager,
19 lpDriverName,
20 lpDriverName,
21 SERVICE_ALL_ACCESS,
22 SERVICE_KERNEL_DRIVER,
23 SERVICE_DEMAND_START,
24 SERVICE_ERROR_NORMAL,
25 lpPathName,
26 NULL,
27 NULL,
28 NULL,
29 NULL,
30 NULL);
31
32 if (hService)
33 {
34 CloseServiceHandle(hService);
35 CloseServiceHandle(hSCManager);
36 return TRUE;
37 }
38 else
39 {
40 DWORD err = GetLastError();
41
42 if (err == ERROR_SERVICE_MARKED_FOR_DELETE)
43 {
44 StopDriver(DRIVER_NAME);
45 goto retry;
46 }
47
48 CloseServiceHandle(hSCManager);
49
50 // return TRUE if the driver is already registered
51 return (err == ERROR_SERVICE_EXISTS);
52 }
53 }
54
55 BOOL
56 StartDriver(LPCWSTR lpDriverName)
57 {
58 SC_HANDLE hSCManager;
59 SC_HANDLE hService;
60 BOOL bRet;
61
62 hSCManager = OpenSCManagerW(NULL,
63 NULL,
64 SC_MANAGER_ALL_ACCESS);
65 if (!hSCManager)
66 return FALSE;
67
68 hService = OpenServiceW(hSCManager,
69 lpDriverName,
70 SERVICE_ALL_ACCESS);
71 if (!hService)
72 {
73 CloseServiceHandle(hSCManager);
74 return FALSE;
75 }
76
77 bRet = StartServiceW(hService, 0, NULL);
78 if (!bRet)
79 {
80 if (GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)
81 {
82 wprintf(L"%s.sys already running\n", DRIVER_NAME);
83 bRet = TRUE;
84 }
85 }
86
87 CloseServiceHandle(hService);
88 CloseServiceHandle(hSCManager);
89
90 return bRet;
91 }
92
93 BOOL
94 StopDriver(LPCWSTR lpDriverName)
95 {
96 SC_HANDLE hSCManager;
97 SC_HANDLE hService;
98 SERVICE_STATUS serviceStatus;
99 BOOL bRet;
100
101 hSCManager = OpenSCManagerW(NULL,
102 NULL,
103 SC_MANAGER_ALL_ACCESS);
104 if (!hSCManager)
105 return FALSE;
106
107 hService = OpenServiceW(hSCManager,
108 lpDriverName,
109 SERVICE_ALL_ACCESS);
110 if (!hService)
111 {
112 CloseServiceHandle(hSCManager);
113 return FALSE;
114 }
115
116 bRet = ControlService(hService,
117 SERVICE_CONTROL_STOP,
118 &serviceStatus);
119 if (!bRet)
120 {
121 if (GetLastError() == ERROR_SERVICE_NOT_ACTIVE)
122 {
123 wprintf(L"%s.sys wasn't running\n", DRIVER_NAME);
124 bRet = TRUE;
125 }
126 }
127
128 CloseServiceHandle(hService);
129 CloseServiceHandle(hSCManager);
130
131 return bRet;
132 }
133
134 BOOL
135 UnregisterDriver(LPCWSTR lpDriverName)
136 {
137 SC_HANDLE hService;
138 SC_HANDLE hSCManager;
139 BOOL bRet;
140
141 hSCManager = OpenSCManagerW(NULL,
142 NULL,
143 SC_MANAGER_ALL_ACCESS);
144 if (!hSCManager)
145 return FALSE;
146
147 hService = OpenServiceW(hSCManager,
148 lpDriverName,
149 SERVICE_ALL_ACCESS);
150 if (!hService)
151 {
152 CloseServiceHandle(hSCManager);
153 return FALSE;
154 }
155
156 bRet = DeleteService(hService);
157
158 CloseServiceHandle(hService);
159 CloseServiceHandle(hSCManager);
160
161 return bRet;
162 }