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