1 /***********************************************************************\
2 *                                winsvc.d                               *
3 *                                                                       *
4 *                       Windows API header module                       *
5 *                                                                       *
6 *                 Translated from MinGW Windows headers                 *
7 *                           by Stewart Gordon                           *
8 *                                                                       *
9 *                       Placed into public domain                       *
10 \***********************************************************************/
11 module windows.winsvc;
12 pragma(lib, "advapi32");
13 nothrow:
14 private import windows.w32api, windows.windef;
15 
16 // FIXME: check Windows version support
17 
18 const TCHAR[]
19 	SERVICES_ACTIVE_DATABASE = "ServicesActive",
20 	SERVICES_FAILED_DATABASE = "ServicesFailed";
21 
22 const TCHAR SC_GROUP_IDENTIFIER = '+';
23 
24 const DWORD
25 	SC_MANAGER_ALL_ACCESS         = 0xf003f,
26 	SC_MANAGER_CONNECT            =  1,
27 	SC_MANAGER_CREATE_SERVICE     =  2,
28 	SC_MANAGER_ENUMERATE_SERVICE  =  4,
29 	SC_MANAGER_LOCK               =  8,
30 	SC_MANAGER_QUERY_LOCK_STATUS  = 16,
31 	SC_MANAGER_MODIFY_BOOT_CONFIG = 32;
32 
33 const DWORD SERVICE_NO_CHANGE = 0xffffffff;
34 
35 enum : DWORD {
36 	SERVICE_STOPPED = 1,
37 	SERVICE_START_PENDING,
38 	SERVICE_STOP_PENDING,
39 	SERVICE_RUNNING,
40 	SERVICE_CONTINUE_PENDING,
41 	SERVICE_PAUSE_PENDING,
42 	SERVICE_PAUSED // = 7
43 }
44 
45 const DWORD
46 	SERVICE_ACCEPT_STOP                  =   1,
47 	SERVICE_ACCEPT_PAUSE_CONTINUE        =   2,
48 	SERVICE_ACCEPT_SHUTDOWN              =   4,
49 	SERVICE_ACCEPT_PARAMCHANGE           =   8,
50 	SERVICE_ACCEPT_NETBINDCHANGE         =  16,
51 	SERVICE_ACCEPT_HARDWAREPROFILECHANGE =  32,
52 	SERVICE_ACCEPT_POWEREVENT            =  64,
53 	SERVICE_ACCEPT_SESSIONCHANGE         = 128;
54 
55 enum : DWORD {
56 	SERVICE_CONTROL_STOP = 1,
57 	SERVICE_CONTROL_PAUSE,
58 	SERVICE_CONTROL_CONTINUE,
59 	SERVICE_CONTROL_INTERROGATE,
60 	SERVICE_CONTROL_SHUTDOWN,
61 	SERVICE_CONTROL_PARAMCHANGE,
62 	SERVICE_CONTROL_NETBINDADD,
63 	SERVICE_CONTROL_NETBINDREMOVE,
64 	SERVICE_CONTROL_NETBINDENABLE,
65 	SERVICE_CONTROL_NETBINDDISABLE,
66 	SERVICE_CONTROL_DEVICEEVENT,
67 	SERVICE_CONTROL_HARDWAREPROFILECHANGE,
68 	SERVICE_CONTROL_POWEREVENT,
69 	SERVICE_CONTROL_SESSIONCHANGE, // = 14
70 }
71 
72 enum : DWORD {
73 	SERVICE_ACTIVE = 1,
74 	SERVICE_INACTIVE,
75 	SERVICE_STATE_ALL
76 }
77 
78 const DWORD
79 	SERVICE_QUERY_CONFIG         = 0x0001,
80 	SERVICE_CHANGE_CONFIG        = 0x0002,
81 	SERVICE_QUERY_STATUS         = 0x0004,
82 	SERVICE_ENUMERATE_DEPENDENTS = 0x0008,
83 	SERVICE_START                = 0x0010,
84 	SERVICE_STOP                 = 0x0020,
85 	SERVICE_PAUSE_CONTINUE       = 0x0040,
86 	SERVICE_INTERROGATE          = 0x0080,
87 	SERVICE_USER_DEFINED_CONTROL = 0x0100,
88 	SERVICE_ALL_ACCESS           = 0x01FF | STANDARD_RIGHTS_REQUIRED;
89 
90 // This is not documented on the MSDN site
91 const SERVICE_RUNS_IN_SYSTEM_PROCESS = 1;
92 
93 enum : DWORD {
94 	SERVICE_CONFIG_DESCRIPTION         = 1,
95 	SERVICE_CONFIG_FAILURE_ACTIONS,
96 	SERVICE_CONFIG_DELAYED_AUTO_START_INFO,
97 	SERVICE_CONFIG_FAILURE_ACTIONS_FLAG,
98 	SERVICE_CONFIG_SERVICE_SID_INFO,
99 	SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO,
100 	SERVICE_CONFIG_PRESHUTDOWN_INFO // = 7
101 }
102 
103 struct SERVICE_STATUS {
104 	DWORD dwServiceType;
105 	DWORD dwCurrentState;
106 	DWORD dwControlsAccepted;
107 	DWORD dwWin32ExitCode;
108 	DWORD dwServiceSpecificExitCode;
109 	DWORD dwCheckPoint;
110 	DWORD dwWaitHint;
111 }
112 alias SERVICE_STATUS* LPSERVICE_STATUS;
113 
114 struct ENUM_SERVICE_STATUSA {
115 	LPSTR          lpServiceName;
116 	LPSTR          lpDisplayName;
117 	SERVICE_STATUS ServiceStatus;
118 }
119 alias ENUM_SERVICE_STATUSA* LPENUM_SERVICE_STATUSA;
120 
121 struct ENUM_SERVICE_STATUSW {
122 	LPWSTR         lpServiceName;
123 	LPWSTR         lpDisplayName;
124 	SERVICE_STATUS ServiceStatus;
125 }
126 alias ENUM_SERVICE_STATUSW* LPENUM_SERVICE_STATUSW;
127 
128 struct QUERY_SERVICE_CONFIGA {
129 	DWORD dwServiceType;
130 	DWORD dwStartType;
131 	DWORD dwErrorControl;
132 	LPSTR lpBinaryPathName;
133 	LPSTR lpLoadOrderGroup;
134 	DWORD dwTagId;
135 	LPSTR lpDependencies;
136 	LPSTR lpServiceStartName;
137 	LPSTR lpDisplayName;
138 }
139 alias QUERY_SERVICE_CONFIGA* LPQUERY_SERVICE_CONFIGA;
140 
141 struct QUERY_SERVICE_CONFIGW {
142 	DWORD  dwServiceType;
143 	DWORD  dwStartType;
144 	DWORD  dwErrorControl;
145 	LPWSTR lpBinaryPathName;
146 	LPWSTR lpLoadOrderGroup;
147 	DWORD  dwTagId;
148 	LPWSTR lpDependencies;
149 	LPWSTR lpServiceStartName;
150 	LPWSTR lpDisplayName;
151 }
152 alias QUERY_SERVICE_CONFIGW* LPQUERY_SERVICE_CONFIGW;
153 
154 struct QUERY_SERVICE_LOCK_STATUSA {
155 	DWORD fIsLocked;
156 	LPSTR lpLockOwner;
157 	DWORD dwLockDuration;
158 }
159 alias QUERY_SERVICE_LOCK_STATUSA* LPQUERY_SERVICE_LOCK_STATUSA;
160 
161 struct QUERY_SERVICE_LOCK_STATUSW {
162 	DWORD  fIsLocked;
163 	LPWSTR lpLockOwner;
164 	DWORD  dwLockDuration;
165 }
166 alias QUERY_SERVICE_LOCK_STATUSW* LPQUERY_SERVICE_LOCK_STATUSW;
167 
168 extern (Windows) {
169 	alias void function(DWORD, LPSTR*)  LPSERVICE_MAIN_FUNCTIONA;
170 	alias void function(DWORD, LPWSTR*) LPSERVICE_MAIN_FUNCTIONW;
171 }
172 
173 struct SERVICE_TABLE_ENTRYA {
174 	LPSTR                    lpServiceName;
175 	LPSERVICE_MAIN_FUNCTIONA lpServiceProc;
176 }
177 alias SERVICE_TABLE_ENTRYA* LPSERVICE_TABLE_ENTRYA;
178 
179 struct SERVICE_TABLE_ENTRYW {
180 	LPWSTR                   lpServiceName;
181 	LPSERVICE_MAIN_FUNCTIONW lpServiceProc;
182 }
183 alias SERVICE_TABLE_ENTRYW* LPSERVICE_TABLE_ENTRYW;
184 
185 mixin DECLARE_HANDLE!("SC_HANDLE");
186 alias SC_HANDLE* LPSC_HANDLE;
187 alias void* SC_LOCK;
188 mixin DECLARE_HANDLE!("SERVICE_STATUS_HANDLE");
189 
190 extern (Windows) {
191 	alias void function(DWORD) LPHANDLER_FUNCTION;
192 	alias DWORD function(DWORD, DWORD, LPVOID, LPVOID) LPHANDLER_FUNCTION_EX;
193 }
194 
195 static if (_WIN32_WINNT >= 0x500) {
196 	struct SERVICE_STATUS_PROCESS {
197 		DWORD dwServiceType;
198 		DWORD dwCurrentState;
199 		DWORD dwControlsAccepted;
200 		DWORD dwWin32ExitCode;
201 		DWORD dwServiceSpecificExitCode;
202 		DWORD dwCheckPoint;
203 		DWORD dwWaitHint;
204 		DWORD dwProcessId;
205 		DWORD dwServiceFlags;
206 	}
207 	alias SERVICE_STATUS_PROCESS* LPSERVICE_STATUS_PROCESS;
208 
209 	enum SC_STATUS_TYPE {
210 		SC_STATUS_PROCESS_INFO = 0
211 	}
212 
213 	enum SC_ENUM_TYPE {
214 		SC_ENUM_PROCESS_INFO = 0
215 	}
216 
217 	struct ENUM_SERVICE_STATUS_PROCESSA {
218 		LPSTR                  lpServiceName;
219 		LPSTR                  lpDisplayName;
220 		SERVICE_STATUS_PROCESS ServiceStatusProcess;
221 	}
222 	alias ENUM_SERVICE_STATUS_PROCESSA* LPENUM_SERVICE_STATUS_PROCESSA;
223 
224 	struct ENUM_SERVICE_STATUS_PROCESSW {
225 		LPWSTR                 lpServiceName;
226 		LPWSTR                 lpDisplayName;
227 		SERVICE_STATUS_PROCESS ServiceStatusProcess;
228 	}
229 	alias ENUM_SERVICE_STATUS_PROCESSW* LPENUM_SERVICE_STATUS_PROCESSW;
230 
231 	struct SERVICE_DESCRIPTIONA {
232 		LPSTR lpDescription;
233 	}
234 	alias SERVICE_DESCRIPTIONA* LPSERVICE_DESCRIPTIONA;
235 
236 	struct SERVICE_DESCRIPTIONW {
237 		LPWSTR lpDescription;
238 	}
239 	alias SERVICE_DESCRIPTIONW* LPSERVICE_DESCRIPTIONW;
240 
241 	enum SC_ACTION_TYPE {
242 		SC_ACTION_NONE,
243 		SC_ACTION_RESTART,
244 		SC_ACTION_REBOOT,
245 		SC_ACTION_RUN_COMMAND
246 	}
247 
248 	struct SC_ACTION {
249 		SC_ACTION_TYPE Type;
250 		DWORD          Delay;
251 	}
252 	alias SC_ACTION* LPSC_ACTION;
253 
254 	struct SERVICE_FAILURE_ACTIONSA {
255 		DWORD      dwResetPeriod;
256 		LPSTR      lpRebootMsg;
257 		LPSTR      lpCommand;
258 		DWORD      cActions;
259 		SC_ACTION* lpsaActions;
260 	}
261 	alias SERVICE_FAILURE_ACTIONSA* LPSERVICE_FAILURE_ACTIONSA;
262 
263 	struct SERVICE_FAILURE_ACTIONSW {
264 		DWORD      dwResetPeriod;
265 		LPWSTR     lpRebootMsg;
266 		LPWSTR     lpCommand;
267 		DWORD      cActions;
268 		SC_ACTION* lpsaActions;
269 	}
270 	alias SERVICE_FAILURE_ACTIONSW* LPSERVICE_FAILURE_ACTIONSW;
271 }
272 
273 extern (Windows) {
274 	BOOL ChangeServiceConfigA(SC_HANDLE, DWORD, DWORD, DWORD, LPCSTR,
275 	  LPCSTR, LPDWORD, LPCSTR, LPCSTR, LPCSTR, LPCSTR);
276 	BOOL ChangeServiceConfigW(SC_HANDLE, DWORD, DWORD, DWORD, LPCWSTR,
277 	  LPCWSTR, LPDWORD, LPCWSTR, LPCWSTR, LPCWSTR, LPCWSTR);
278 	BOOL CloseServiceHandle(SC_HANDLE);
279 	BOOL ControlService(SC_HANDLE, DWORD, LPSERVICE_STATUS);
280 	SC_HANDLE CreateServiceA(SC_HANDLE, LPCSTR, LPCSTR, DWORD, DWORD,
281 	  DWORD, DWORD, LPCSTR, LPCSTR, PDWORD, LPCSTR, LPCSTR, LPCSTR);
282 	SC_HANDLE CreateServiceW(SC_HANDLE, LPCWSTR, LPCWSTR, DWORD, DWORD,
283 	  DWORD, DWORD, LPCWSTR, LPCWSTR, PDWORD, LPCWSTR, LPCWSTR, LPCWSTR);
284 	BOOL DeleteService(SC_HANDLE);
285 	BOOL EnumDependentServicesA(SC_HANDLE, DWORD, LPENUM_SERVICE_STATUSA,
286 	  DWORD, PDWORD, PDWORD);
287 	BOOL EnumDependentServicesW(SC_HANDLE, DWORD, LPENUM_SERVICE_STATUSW,
288 	  DWORD, PDWORD, PDWORD);
289 	BOOL EnumServicesStatusA(SC_HANDLE, DWORD, DWORD, LPENUM_SERVICE_STATUSA,
290 	  DWORD, PDWORD, PDWORD, PDWORD);
291 	BOOL EnumServicesStatusW(SC_HANDLE, DWORD, DWORD, LPENUM_SERVICE_STATUSW,
292 	  DWORD, PDWORD, PDWORD, PDWORD);
293 	BOOL GetServiceDisplayNameA(SC_HANDLE, LPCSTR, LPSTR, PDWORD);
294 	BOOL GetServiceDisplayNameW(SC_HANDLE, LPCWSTR, LPWSTR, PDWORD);
295 	BOOL GetServiceKeyNameA(SC_HANDLE, LPCSTR, LPSTR, PDWORD);
296 	BOOL GetServiceKeyNameW(SC_HANDLE, LPCWSTR, LPWSTR, PDWORD);
297 	SC_LOCK LockServiceDatabase(SC_HANDLE);
298 	BOOL NotifyBootConfigStatus(BOOL);
299 	SC_HANDLE OpenSCManagerA(LPCSTR, LPCSTR, DWORD);
300 	SC_HANDLE OpenSCManagerW(LPCWSTR, LPCWSTR, DWORD);
301 	SC_HANDLE OpenServiceA(SC_HANDLE, LPCSTR, DWORD);
302 	SC_HANDLE OpenServiceW(SC_HANDLE, LPCWSTR, DWORD);
303 	BOOL QueryServiceConfigA(SC_HANDLE, LPQUERY_SERVICE_CONFIGA, DWORD,
304 	  PDWORD);
305 	BOOL QueryServiceConfigW(SC_HANDLE, LPQUERY_SERVICE_CONFIGW, DWORD,
306 	  PDWORD);
307 	BOOL QueryServiceLockStatusA(SC_HANDLE, LPQUERY_SERVICE_LOCK_STATUSA,
308 	  DWORD, PDWORD);
309 	BOOL QueryServiceLockStatusW(SC_HANDLE, LPQUERY_SERVICE_LOCK_STATUSW,
310 	  DWORD, PDWORD);
311 	BOOL QueryServiceObjectSecurity(SC_HANDLE, SECURITY_INFORMATION,
312 	  PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
313 	BOOL QueryServiceStatus(SC_HANDLE, LPSERVICE_STATUS);
314 	SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerA(LPCSTR,
315 	  LPHANDLER_FUNCTION);
316 	SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerW(LPCWSTR,
317 	  LPHANDLER_FUNCTION);
318 	BOOL SetServiceObjectSecurity(SC_HANDLE, SECURITY_INFORMATION,
319 	  PSECURITY_DESCRIPTOR);
320 	BOOL SetServiceStatus(SERVICE_STATUS_HANDLE, LPSERVICE_STATUS);
321 	BOOL StartServiceA(SC_HANDLE, DWORD, LPCSTR*);
322 	BOOL StartServiceW(SC_HANDLE, DWORD, LPCWSTR*);
323 	BOOL StartServiceCtrlDispatcherA(LPSERVICE_TABLE_ENTRYA);
324 	BOOL StartServiceCtrlDispatcherW(LPSERVICE_TABLE_ENTRYW);
325 	BOOL UnlockServiceDatabase(SC_LOCK);
326 
327 	static if (_WIN32_WINNT >= 0x500) {
328 		BOOL EnumServicesStatusExA(SC_HANDLE, SC_ENUM_TYPE, DWORD, DWORD, LPBYTE,
329 		  DWORD, LPDWORD, LPDWORD, LPDWORD, LPCSTR);
330 		BOOL EnumServicesStatusExW(SC_HANDLE, SC_ENUM_TYPE, DWORD, DWORD, LPBYTE,
331 		  DWORD, LPDWORD, LPDWORD, LPDWORD, LPCWSTR);
332 		BOOL QueryServiceConfig2A(SC_HANDLE, DWORD, LPBYTE, DWORD, LPDWORD);
333 		BOOL QueryServiceConfig2W(SC_HANDLE, DWORD, LPBYTE, DWORD, LPDWORD);
334 		BOOL QueryServiceStatusEx(SC_HANDLE, SC_STATUS_TYPE, LPBYTE, DWORD,
335 		  LPDWORD);
336 		SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerExA(LPCSTR,
337 		  LPHANDLER_FUNCTION_EX, LPVOID);
338 		SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerExW(LPCWSTR,
339 		  LPHANDLER_FUNCTION_EX, LPVOID);
340 	}
341 
342 	static if (_WIN32_WINNT >= 0x501) {
343 		BOOL ChangeServiceConfig2A(SC_HANDLE, DWORD, LPVOID);
344 		BOOL ChangeServiceConfig2W(SC_HANDLE, DWORD, LPVOID);
345 	}
346 }
347 
348 version (Unicode) {
349 	alias ENUM_SERVICE_STATUSW ENUM_SERVICE_STATUS;
350 	alias QUERY_SERVICE_CONFIGW QUERY_SERVICE_CONFIG;
351 	alias QUERY_SERVICE_LOCK_STATUSW QUERY_SERVICE_LOCK_STATUS;
352 	alias LPSERVICE_MAIN_FUNCTIONW LPSERVICE_MAIN_FUNCTION;
353 	alias SERVICE_TABLE_ENTRYW SERVICE_TABLE_ENTRY;
354 	alias ChangeServiceConfigW ChangeServiceConfig;
355 	alias CreateServiceW CreateService;
356 	alias EnumDependentServicesW EnumDependentServices;
357 	alias EnumServicesStatusW EnumServicesStatus;
358 	alias GetServiceDisplayNameW GetServiceDisplayName;
359 	alias GetServiceKeyNameW GetServiceKeyName;
360 	alias OpenSCManagerW OpenSCManager;
361 	alias OpenServiceW OpenService;
362 	alias QueryServiceConfigW QueryServiceConfig;
363 	alias QueryServiceLockStatusW QueryServiceLockStatus;
364 	alias RegisterServiceCtrlHandlerW RegisterServiceCtrlHandler;
365 	alias StartServiceW StartService;
366 	alias StartServiceCtrlDispatcherW StartServiceCtrlDispatcher;
367 
368 	static if (_WIN32_WINNT >= 0x500) {
369 		alias ENUM_SERVICE_STATUS_PROCESSW ENUM_SERVICE_STATUS_PROCESS;
370 		alias SERVICE_DESCRIPTIONW SERVICE_DESCRIPTION;
371 		alias SERVICE_FAILURE_ACTIONSW SERVICE_FAILURE_ACTIONS;
372 		alias EnumServicesStatusExW EnumServicesStatusEx;
373 		alias QueryServiceConfig2W QueryServiceConfig2;
374 		alias RegisterServiceCtrlHandlerExW RegisterServiceCtrlHandlerEx;
375 	}
376 
377 	static if (_WIN32_WINNT >= 0x501) {
378 		alias ChangeServiceConfig2W ChangeServiceConfig2;
379 	}
380 
381 } else {
382 	alias ENUM_SERVICE_STATUSA ENUM_SERVICE_STATUS;
383 	alias QUERY_SERVICE_CONFIGA QUERY_SERVICE_CONFIG;
384 	alias QUERY_SERVICE_LOCK_STATUSA QUERY_SERVICE_LOCK_STATUS;
385 	alias LPSERVICE_MAIN_FUNCTIONA LPSERVICE_MAIN_FUNCTION;
386 	alias SERVICE_TABLE_ENTRYA SERVICE_TABLE_ENTRY;
387 	alias ChangeServiceConfigA ChangeServiceConfig;
388 	alias CreateServiceA CreateService;
389 	alias EnumDependentServicesA EnumDependentServices;
390 	alias EnumServicesStatusA EnumServicesStatus;
391 	alias GetServiceDisplayNameA GetServiceDisplayName;
392 	alias GetServiceKeyNameA GetServiceKeyName;
393 	alias OpenSCManagerA OpenSCManager;
394 	alias OpenServiceA OpenService;
395 	alias QueryServiceConfigA QueryServiceConfig;
396 	alias QueryServiceLockStatusA QueryServiceLockStatus;
397 	alias RegisterServiceCtrlHandlerA RegisterServiceCtrlHandler;
398 	alias StartServiceA StartService;
399 	alias StartServiceCtrlDispatcherA StartServiceCtrlDispatcher;
400 
401 	static if (_WIN32_WINNT >= 0x500) {
402 		alias ENUM_SERVICE_STATUS_PROCESSA ENUM_SERVICE_STATUS_PROCESS;
403 		alias SERVICE_DESCRIPTIONA SERVICE_DESCRIPTION;
404 		alias SERVICE_FAILURE_ACTIONSA SERVICE_FAILURE_ACTIONS;
405 		alias EnumServicesStatusExA EnumServicesStatusEx;
406 		alias QueryServiceConfig2A QueryServiceConfig2;
407 		alias RegisterServiceCtrlHandlerExA RegisterServiceCtrlHandlerEx;
408 	}
409 
410 	static if (_WIN32_WINNT >= 0x501) {
411 		alias ChangeServiceConfig2A ChangeServiceConfig2;
412 	}
413 
414 }
415 
416 alias ENUM_SERVICE_STATUS* LPENUM_SERVICE_STATUS;
417 alias QUERY_SERVICE_CONFIG* LPQUERY_SERVICE_CONFIG;
418 alias QUERY_SERVICE_LOCK_STATUS* LPQUERY_SERVICE_LOCK_STATUS;
419 alias SERVICE_TABLE_ENTRY* LPSERVICE_TABLE_ENTRY;
420 
421 static if (_WIN32_WINNT >= 0x500) {
422 	alias ENUM_SERVICE_STATUS_PROCESS* LPENUM_SERVICE_STATUS_PROCESS;
423 	alias SERVICE_DESCRIPTION* LPSERVICE_DESCRIPTION;
424 	alias SERVICE_FAILURE_ACTIONS* LPSERVICE_FAILURE_ACTIONS;
425 }