Menu

[2c7fdc]: / include / ServiceCmd.cpp  Maximize  Restore  History

Download this file

347 lines (318 with data), 10.5 kB

  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
//////////////////////////////////////////////////////////////////////////
// Service Helpers.
//
// Functions to stop/start/install and uninstall a service.
//
// Copyright (c) 2004 MySolutions NORDIC (http://www.medin.nu)
//
// Date: 2004-03-13
// Author: Michael Medin (mickem@medin.nu)
//
// This software is provided "AS IS", without a warranty of any kind.
// You are free to use/modify this code but leave this header intact.
//
//////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include "ServiceCmd.h"
#include <strEx.h>
#include <tchar.h>
#include <iostream>
#include <msvc_wrappers.h>
namespace serviceControll {
/**
* Installs the service
*
* @param szName
* @param szDisplayName
* @param szDependencies
*
* @author mickem
*
* @date 03-13-2004
*
*/
void Install(LPCTSTR szName, LPCTSTR szDisplayName, LPCTSTR szDependencies, DWORD dwServiceType) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
TCHAR szPath[512];
if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
throw SCException(_T("Could not get module"));
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = CreateService(
schSCManager, // SCManager database
szName, // name of service
szDisplayName, // name to display
SERVICE_ALL_ACCESS, // desired access
dwServiceType, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
szDependencies, // dependencies
NULL, // LocalSystem account
NULL); // no password
if (!schService) {
DWORD err = GetLastError();
CloseServiceHandle(schSCManager);
if (err==ERROR_SERVICE_EXISTS) {
throw SCException(_T("Service already installed!"));
}
throw SCException(_T("Unable to install service."), err);
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
void ModifyServiceType(LPCTSTR szName, DWORD dwServiceType) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
TCHAR szPath[512];
if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
throw SCException(_T("Could not get module"));
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = OpenService(schSCManager, szName, SERVICE_ALL_ACCESS);
if (!schService) {
DWORD err = GetLastError();
CloseServiceHandle(schSCManager);
throw SCException(_T("Unable to open service."), err);
}
BOOL result = ChangeServiceConfig(schService, dwServiceType, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE , NULL, NULL, NULL,
NULL, NULL, NULL, NULL);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (result != TRUE)
throw SCException(_T("Could not change service information"));
}
DWORD GetServiceType(LPCTSTR szName) {
LPQUERY_SERVICE_CONFIG lpqscBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
if (lpqscBuf == NULL) {
throw SCException(_T("Could not allocate memory"));
}
SC_HANDLE schService;
SC_HANDLE schSCManager;
TCHAR szPath[512];
if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
throw SCException(_T("Could not get module"));
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = OpenService(schSCManager, szName, SERVICE_ALL_ACCESS);
if (!schService) {
DWORD err = GetLastError();
CloseServiceHandle(schSCManager);
throw SCException(_T("Unable to open service."), err);
}
DWORD dwBytesNeeded = 0;
BOOL success = QueryServiceConfig(schService, lpqscBuf, 4096, &dwBytesNeeded);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (success != TRUE)
throw SCException(_T("Could not query service information"));
DWORD ret = lpqscBuf->dwServiceType;
LocalFree(lpqscBuf);
return ret;
}
/**
* Stars the service.
*
* @param name The name of the service to start
*
* @author mickem
*
* @date 03-13-2004
*
*/
void Start(std::wstring name) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
SERVICE_STATUS ssStatus;
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS );
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = OpenService(schSCManager, name.c_str(), SERVICE_ALL_ACCESS);
if (schService) {
// try to stop the service
if ( StartService(schService,0,NULL) ) {
std::wcout << _T("Starting ") << name;
Sleep( 1000 );
while( QueryServiceStatus( schService, &ssStatus ) ) {
if ( ssStatus.dwCurrentState == SERVICE_START_PENDING ) {
std::wcout << _T(".");
Sleep( 1000 );
} else
break;
}
if ( ssStatus.dwCurrentState != SERVICE_RUNNING ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw SCException(_T("Service failed to start."));
}
}
CloseServiceHandle(schService);
} else {
CloseServiceHandle(schSCManager);
throw SCException(_T("OpenService failed."));
}
CloseServiceHandle(schSCManager);
}
bool isStarted(std::wstring name) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
SERVICE_STATUS ssStatus;
bool ret = false;
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS );
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = OpenService(schSCManager, name.c_str(), SERVICE_ALL_ACCESS);
if (schService) {
if ( QueryServiceStatus( schService, &ssStatus ) ) {
if ( ssStatus.dwCurrentState == SERVICE_RUNNING ) {
ret = true;
} else if ( ssStatus.dwCurrentState == SERVICE_START_PENDING ) {
ret = true;
}
}
CloseServiceHandle(schService);
} else {
CloseServiceHandle(schSCManager);
throw SCException(_T("OpenService failed."));
}
CloseServiceHandle(schSCManager);
return ret;
}
/**
* Stops and removes the service
*
* @param name The name of the service to uninstall
*
* @author mickem
*
* @date 03-13-2004
*
*/
void Uninstall(std::wstring name) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
Stop(name);
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = OpenService(schSCManager, name.c_str(), SERVICE_ALL_ACCESS);
if (schService) {
if(!DeleteService(schService)) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw SCException(_T("DeleteService failed."));
}
CloseServiceHandle(schService);
} else {
CloseServiceHandle(schSCManager);
throw SCException(_T("OpenService failed."));
}
CloseServiceHandle(schSCManager);
}
/**
* Stops the service
*
* @param name The name of the serive to stop
*
* @author MickeM
*
* @date 03-13-2004
*
*/
void Stop(std::wstring name) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
SERVICE_STATUS ssStatus;
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = OpenService(schSCManager, name.c_str(), SERVICE_ALL_ACCESS);
if (schService) {
// try to stop the service
if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) ) {
std::wcout << _T("Stopping service.");
Sleep( 1000 );
while( QueryServiceStatus( schService, &ssStatus ) ) {
if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) {
std::wcout << _T(".");
Sleep( 1000 );
} else
break;
}
std::wcout << std::endl;
if ( ssStatus.dwCurrentState != SERVICE_STOPPED ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw SCException(_T("Service failed to stop."));
}
}
CloseServiceHandle(schService);
} else {
CloseServiceHandle(schSCManager);
throw SCException(_T("OpenService failed."));
}
CloseServiceHandle(schSCManager);
}
void StopNoWait(std::wstring name) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
SERVICE_STATUS ssStatus;
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
schService = OpenService(schSCManager, name.c_str(), SERVICE_ALL_ACCESS);
if (schService) {
// try to stop the service
ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus );
CloseServiceHandle(schService);
} else {
CloseServiceHandle(schSCManager);
throw SCException(_T("OpenService failed."));
}
CloseServiceHandle(schSCManager);
}
typedef BOOL (WINAPI*PFChangeServiceConfig2)(SC_HANDLE hService,DWORD dwInfoLevel,LPVOID lpInfo);
void SetDescription(std::wstring name, std::wstring desc) {
PFChangeServiceConfig2 FChangeServiceConfig2;
HMODULE ADVAPI= ::LoadLibrary(_T("Advapi32"));
if (!ADVAPI) {
throw SCException(_T("Couldn't set extended service info (ignore this on NT4)."));
}
#ifdef UNICODE
FChangeServiceConfig2 = (PFChangeServiceConfig2)::GetProcAddress(ADVAPI, "ChangeServiceConfig2W");
#else
FChangeServiceConfig2 = (PFChangeServiceConfig2)::GetProcAddress(ADVAPI, _TEXT("ChangeServiceConfig2A"));
#endif
if (!FChangeServiceConfig2) {
FreeLibrary(ADVAPI);
throw SCException(_T("Couldn't set extended service info (ignore this on NT4)."));
}
SERVICE_DESCRIPTION descr;
SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
throw SCException(_T("OpenSCManager failed."));
SC_HANDLE schService = OpenService(schSCManager, name.c_str(), SERVICE_ALL_ACCESS);
if (!schService) {
FreeLibrary(ADVAPI);
CloseServiceHandle(schSCManager);
throw SCException(_T("OpenService failed."));
}
TCHAR* d = new TCHAR[desc.length()+2];
wcsncpy_s(d, desc.length()+2, desc.c_str(), desc.length());
descr.lpDescription = d;
BOOL bResult = FChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &descr);
delete [] d;
FreeLibrary(ADVAPI);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (!bResult)
throw SCException(_T("ChangeServiceConfig2 failed."));
}
}