1+ // -----------------------------------------------------------------------------
2+ // Author : hiyohiyo
3+ // Mail : hiyohiyo@crystalmark.info
4+ // Web : http://openlibsys.org/
5+ // License : The modified BSD license
6+ //
7+ // Copyright 2007 OpenLibSys.org. All rights reserved.
8+ // -----------------------------------------------------------------------------
9+
10+ #include " stdafx.h"
11+ #include " Driver.h"
12+ #include < tchar.h>
13+ #include " OlsDll.h"
14+
15+ extern HANDLE gHandle ;
16+
17+ static BOOL InstallDriver (SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath);
18+ static BOOL RemoveDriver (SC_HANDLE hSCManager, LPCTSTR DriverId);
19+ static BOOL StartDriver (SC_HANDLE hSCManager, LPCTSTR DriverId);
20+ static BOOL StopDriver (SC_HANDLE hSCManager, LPCTSTR DriverId);
21+ static BOOL SystemInstallDriver (SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath);
22+ static BOOL IsSystemInstallDriver (SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath);
23+
24+ BOOL ManageDriver (LPCTSTR DriverId, LPCTSTR DriverPath, USHORT Function)
25+ {
26+ SC_HANDLE hSCManager = NULL ;
27+ BOOL rCode = FALSE ;
28+ DWORD error = NO_ERROR;
29+
30+ if (DriverId == NULL || DriverPath == NULL )
31+ {
32+ return FALSE ;
33+ }
34+ hSCManager = OpenSCManager (NULL , NULL , SC_MANAGER_ALL_ACCESS);
35+
36+ if (hSCManager == NULL )
37+ {
38+ return FALSE ;
39+ }
40+
41+ switch (Function)
42+ {
43+ case OLS_DRIVER_INSTALL:
44+ if (InstallDriver (hSCManager, DriverId, DriverPath))
45+ {
46+ rCode = StartDriver (hSCManager, DriverId);
47+ }
48+ break ;
49+ case OLS_DRIVER_REMOVE:
50+ if (! IsSystemInstallDriver (hSCManager, DriverId, DriverPath))
51+ {
52+ StopDriver (hSCManager, DriverId);
53+ rCode = RemoveDriver (hSCManager, DriverId);
54+ }
55+ break ;
56+ case OLS_DRIVER_SYSTEM_INSTALL:
57+ if (IsSystemInstallDriver (hSCManager, DriverId, DriverPath))
58+ {
59+ rCode = TRUE ;
60+ }
61+ else
62+ {
63+ if (! OpenDriver ())
64+ {
65+ StopDriver (hSCManager, DriverId);
66+ RemoveDriver (hSCManager, DriverId);
67+ if (InstallDriver (hSCManager, DriverId, DriverPath))
68+ {
69+ StartDriver (hSCManager, DriverId);
70+ }
71+ OpenDriver ();
72+ }
73+ rCode = SystemInstallDriver (hSCManager, DriverId, DriverPath);
74+ }
75+ break ;
76+ case OLS_DRIVER_SYSTEM_UNINSTALL:
77+ if (! IsSystemInstallDriver (hSCManager, DriverId, DriverPath))
78+ {
79+ rCode = TRUE ;
80+ }
81+ else
82+ {
83+ if (gHandle != INVALID_HANDLE_VALUE)
84+ {
85+ CloseHandle (gHandle );
86+ gHandle = INVALID_HANDLE_VALUE;
87+ }
88+
89+ if (StopDriver (hSCManager, DriverId))
90+ {
91+ rCode = RemoveDriver (hSCManager, DriverId);
92+ }
93+ }
94+ break ;
95+ default :
96+ rCode = FALSE ;
97+ break ;
98+ }
99+
100+ if (hSCManager != NULL )
101+ {
102+ CloseServiceHandle (hSCManager);
103+ }
104+
105+ return rCode;
106+ }
107+
108+ BOOL InstallDriver (SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath)
109+ {
110+ SC_HANDLE hService = NULL ;
111+ BOOL rCode = FALSE ;
112+ DWORD error = NO_ERROR;
113+
114+ hService = CreateService (hSCManager,
115+ DriverId,
116+ DriverId,
117+ SERVICE_ALL_ACCESS,
118+ SERVICE_KERNEL_DRIVER,
119+ SERVICE_DEMAND_START,
120+ SERVICE_ERROR_NORMAL,
121+ DriverPath,
122+ NULL ,
123+ NULL ,
124+ NULL ,
125+ NULL ,
126+ NULL
127+ );
128+
129+ if (hService == NULL )
130+ {
131+ error = GetLastError ();
132+ if (error == ERROR_SERVICE_EXISTS)
133+ {
134+ rCode = TRUE ;
135+ }
136+ }
137+ else
138+ {
139+ rCode = TRUE ;
140+ CloseServiceHandle (hService);
141+ }
142+
143+ return rCode;
144+ }
145+
146+ BOOL SystemInstallDriver (SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath)
147+ {
148+ SC_HANDLE hService = NULL ;
149+ BOOL rCode = FALSE ;
150+
151+ hService = OpenService (hSCManager, DriverId, SERVICE_ALL_ACCESS);
152+
153+ if (hService != NULL )
154+ {
155+ rCode = ChangeServiceConfig (hService,
156+ SERVICE_KERNEL_DRIVER,
157+ SERVICE_AUTO_START,
158+ SERVICE_ERROR_NORMAL,
159+ DriverPath,
160+ NULL ,
161+ NULL ,
162+ NULL ,
163+ NULL ,
164+ NULL ,
165+ NULL
166+ );
167+ CloseServiceHandle (hService);
168+ }
169+
170+ return rCode;
171+ }
172+
173+ BOOL RemoveDriver (SC_HANDLE hSCManager, LPCTSTR DriverId)
174+ {
175+ SC_HANDLE hService = NULL ;
176+ BOOL rCode = FALSE ;
177+
178+ hService = OpenService (hSCManager, DriverId, SERVICE_ALL_ACCESS);
179+ if (hService == NULL )
180+ {
181+ rCode = TRUE ;
182+ }
183+ else
184+ {
185+ rCode = DeleteService (hService);
186+ CloseServiceHandle (hService);
187+ }
188+
189+ return rCode;
190+ }
191+
192+ BOOL StartDriver (SC_HANDLE hSCManager, LPCTSTR DriverId)
193+ {
194+ SC_HANDLE hService = NULL ;
195+ BOOL rCode = FALSE ;
196+ DWORD error = NO_ERROR;
197+
198+ hService = OpenService (hSCManager, DriverId, SERVICE_ALL_ACCESS);
199+
200+ if (hService != NULL )
201+ {
202+ if (! StartService (hService, 0 , NULL ))
203+ {
204+ error = GetLastError ();
205+ if (error == ERROR_SERVICE_ALREADY_RUNNING)
206+ {
207+ rCode = TRUE ;
208+ }
209+ }
210+ else
211+ {
212+ rCode = TRUE ;
213+ }
214+ CloseServiceHandle (hService);
215+ }
216+
217+ return rCode;
218+ }
219+
220+ BOOL StopDriver (SC_HANDLE hSCManager, LPCTSTR DriverId)
221+ {
222+ SC_HANDLE hService = NULL ;
223+ BOOL rCode = FALSE ;
224+ SERVICE_STATUS serviceStatus;
225+ DWORD error = NO_ERROR;
226+
227+ hService = OpenService (hSCManager, DriverId, SERVICE_ALL_ACCESS);
228+
229+ if (hService != NULL )
230+ {
231+ rCode = ControlService (hService, SERVICE_CONTROL_STOP, &serviceStatus);
232+ error = GetLastError ();
233+ CloseServiceHandle (hService);
234+ }
235+
236+ return rCode;
237+ }
238+
239+ BOOL IsSystemInstallDriver (SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath)
240+ {
241+ SC_HANDLE hService = OpenService (hSCManager, DriverId, SERVICE_ALL_ACCESS);
242+ if (hService != NULL )
243+ {
244+ DWORD dwSize;
245+ if (!QueryServiceConfig (hService, NULL , 0 , &dwSize))
246+ {
247+ LPQUERY_SERVICE_CONFIG lpqsc = reinterpret_cast <LPQUERY_SERVICE_CONFIG>(new BYTE[dwSize]);
248+ if (!lpqsc)
249+ {
250+ return FALSE ;
251+ }
252+
253+ if (!QueryServiceConfig (hService, lpqsc, dwSize, &dwSize))
254+ {
255+ if (lpqsc->dwStartType == SERVICE_AUTO_START)
256+ {
257+ CloseServiceHandle (hService);
258+ delete[] lpqsc;
259+ return TRUE ;
260+ }
261+ }
262+ }
263+ }
264+
265+ return FALSE ;
266+ }
0 commit comments