Release 1.9.16.
[wine.git] / dlls / advapi32 / tests / service.c
blob81d2f7422220145522867d2fdd3a1784cad61c59
1 /*
2 * Unit tests for service functions
4 * Copyright (c) 2007 Paul Vriens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "winsvc.h"
29 #include "winnls.h"
30 #include "lmcons.h"
31 #include "aclapi.h"
33 #include "wine/test.h"
35 static const CHAR spooler[] = "Spooler"; /* Should be available on all platforms */
36 static CHAR selfname[MAX_PATH];
38 static BOOL (WINAPI *pChangeServiceConfig2A)(SC_HANDLE,DWORD,LPVOID);
39 static BOOL (WINAPI *pEnumServicesStatusExA)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
40 DWORD, LPBYTE, DWORD, LPDWORD,
41 LPDWORD, LPDWORD, LPCSTR);
42 static BOOL (WINAPI *pEnumServicesStatusExW)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
43 DWORD, LPBYTE, DWORD, LPDWORD,
44 LPDWORD, LPDWORD, LPCWSTR);
45 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
46 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
47 static BOOL (WINAPI *pQueryServiceConfig2A)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
48 static BOOL (WINAPI *pQueryServiceConfig2W)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
49 static BOOL (WINAPI *pQueryServiceStatusEx)(SC_HANDLE, SC_STATUS_TYPE, LPBYTE,
50 DWORD, LPDWORD);
51 static BOOL (WINAPI *pQueryServiceObjectSecurity)(SC_HANDLE, SECURITY_INFORMATION,
52 PSECURITY_DESCRIPTOR, DWORD, LPDWORD);
53 static DWORD (WINAPI *pNotifyServiceStatusChangeW)(SC_HANDLE,DWORD,SERVICE_NOTIFYW*);
55 static void init_function_pointers(void)
57 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
59 pChangeServiceConfig2A = (void*)GetProcAddress(hadvapi32, "ChangeServiceConfig2A");
60 pEnumServicesStatusExA= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExA");
61 pEnumServicesStatusExW= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExW");
62 pGetSecurityInfo = (void *)GetProcAddress(hadvapi32, "GetSecurityInfo");
63 pQueryServiceConfig2A= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2A");
64 pQueryServiceConfig2W= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2W");
65 pQueryServiceStatusEx= (void*)GetProcAddress(hadvapi32, "QueryServiceStatusEx");
66 pQueryServiceObjectSecurity = (void*)GetProcAddress(hadvapi32, "QueryServiceObjectSecurity");
67 pNotifyServiceStatusChangeW = (void*)GetProcAddress(hadvapi32, "NotifyServiceStatusChangeW");
70 static void test_open_scm(void)
72 SC_HANDLE scm_handle;
74 /* No access rights */
75 SetLastError(0xdeadbeef);
76 scm_handle = OpenSCManagerA(NULL, NULL, 0);
77 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
78 CloseServiceHandle(scm_handle);
80 /* Unknown database name */
81 SetLastError(0xdeadbeef);
82 scm_handle = OpenSCManagerA(NULL, "DoesNotExist", SC_MANAGER_CONNECT);
83 ok(!scm_handle, "Expected failure\n");
84 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
85 CloseServiceHandle(scm_handle); /* Just in case */
87 /* MSDN says only ServiceActive is allowed, or NULL */
88 SetLastError(0xdeadbeef);
89 scm_handle = OpenSCManagerA(NULL, SERVICES_FAILED_DATABASEA, SC_MANAGER_CONNECT);
90 ok(!scm_handle, "Expected failure\n");
91 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
92 CloseServiceHandle(scm_handle); /* Just in case */
94 /* Remote unknown host */
95 SetLastError(0xdeadbeef);
96 scm_handle = OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
97 todo_wine
99 ok(!scm_handle, "Expected failure\n");
100 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* w2k8 */,
101 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
103 CloseServiceHandle(scm_handle); /* Just in case */
105 /* Proper call with an empty hostname */
106 scm_handle = OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
107 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
108 CloseServiceHandle(scm_handle);
110 /* Again a correct one */
111 SetLastError(0xdeadbeef);
112 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
113 ok(GetLastError() == ERROR_SUCCESS || broken(GetLastError() == ERROR_IO_PENDING) /* win2k */,
114 "Expected ERROR_SUCCESS, got %u\n", GetLastError());
115 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
116 CloseServiceHandle(scm_handle);
119 static void test_open_svc(void)
121 SC_HANDLE scm_handle, svc_handle;
122 CHAR displayname[4096];
123 DWORD displaysize;
125 /* All NULL (invalid access rights) */
126 SetLastError(0xdeadbeef);
127 svc_handle = OpenServiceA(NULL, NULL, 0);
128 ok(!svc_handle, "Expected failure\n");
129 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
131 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
133 /* NULL service */
134 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
135 SetLastError(0xdeadbeef);
136 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
137 ok(!svc_handle, "Expected failure\n");
138 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
139 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
140 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
141 CloseServiceHandle(scm_handle);
143 /* Nonexistent service */
144 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
145 SetLastError(0xdeadbeef);
146 svc_handle = OpenServiceA(scm_handle, "deadbeef", GENERIC_READ);
147 ok(!svc_handle, "Expected failure\n");
148 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
149 CloseServiceHandle(scm_handle);
151 /* Proper SCM handle but different access rights */
152 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
153 SetLastError(0xdeadbeef);
154 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_WRITE);
155 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
156 skip("Not enough rights to get a handle to the service\n");
157 else
159 ok(svc_handle != NULL, "Expected success, got error %u\n", GetLastError());
160 CloseServiceHandle(svc_handle);
163 /* Test to show we can't open a service with the displayname */
165 /* Retrieve the needed size for the buffer */
166 displaysize = 0;
167 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
168 /* Get the displayname */
169 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
170 /* Try to open the service with this displayname, unless the displayname equals
171 * the servicename as that would defeat the purpose of this test.
173 if (!lstrcmpiA(spooler, displayname))
175 skip("displayname equals servicename\n");
176 CloseServiceHandle(scm_handle);
177 return;
180 SetLastError(0xdeadbeef);
181 svc_handle = OpenServiceA(scm_handle, displayname, GENERIC_READ);
182 ok(!svc_handle, "Expected failure\n");
183 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
184 /* Just in case */
185 CloseServiceHandle(svc_handle);
187 CloseServiceHandle(scm_handle);
190 static void test_create_delete_svc(void)
192 SC_HANDLE scm_handle, svc_handle1, svc_handle2;
193 CHAR username[UNLEN + 1], domain[MAX_PATH];
194 DWORD user_size = UNLEN + 1;
195 CHAR account[UNLEN + 3];
196 static const CHAR servicename [] = "Winetest";
197 static const CHAR pathname [] = "we_dont_care.exe";
198 static const CHAR empty [] = "";
199 static const CHAR password [] = "secret";
200 BOOL spooler_exists = FALSE;
201 BOOL ret;
202 CHAR display[4096];
203 DWORD display_size = sizeof(display);
205 /* Get the username and turn it into an account to be used in some tests */
206 GetUserNameA(username, &user_size);
207 /* Get the domainname to cater for that situation */
208 if (GetEnvironmentVariableA("USERDOMAIN", domain, MAX_PATH))
209 sprintf(account, "%s\\%s", domain, username);
210 else
211 sprintf(account, ".\\%s", username);
213 /* All NULL */
214 SetLastError(0xdeadbeef);
215 svc_handle1 = CreateServiceA(NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
216 ok(!svc_handle1, "Expected failure\n");
217 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
219 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
221 /* Only a valid handle to the Service Control Manager */
222 SetLastError(0xdeadbeef);
223 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
224 ok(!svc_handle1, "Expected failure\n");
225 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
226 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
227 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
229 /* Now with a servicename */
230 SetLastError(0xdeadbeef);
231 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
232 ok(!svc_handle1, "Expected failure\n");
233 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
234 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
235 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
237 /* Or just a binary name */
238 SetLastError(0xdeadbeef);
239 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
240 ok(!svc_handle1, "Expected failure\n");
241 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
242 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
243 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
245 /* Both servicename and binary name (We only have connect rights) */
246 SetLastError(0xdeadbeef);
247 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
248 ok(!svc_handle1, "Expected failure\n");
249 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
251 /* They can even be empty at this stage of parameter checking */
252 SetLastError(0xdeadbeef);
253 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
254 ok(!svc_handle1, "Expected failure\n");
255 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
257 SetLastError(0xdeadbeef);
258 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
259 ok(!svc_handle1, "Expected failure\n");
260 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
262 /* Open the Service Control Manager with minimal rights for creation
263 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
265 CloseServiceHandle(scm_handle);
266 SetLastError(0xdeadbeef);
267 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
268 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
270 skip("Not enough rights to get a handle to the manager\n");
271 return;
274 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
276 /* Empty strings for servicename and binary name are checked */
277 SetLastError(0xdeadbeef);
278 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
279 ok(!svc_handle1, "Expected failure\n");
280 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
282 SetLastError(0xdeadbeef);
283 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
284 ok(!svc_handle1, "Expected failure\n");
285 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
287 SetLastError(0xdeadbeef);
288 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
289 ok(!svc_handle1, "Expected failure\n");
290 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
292 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
293 * an ERROR_INVALID_PARAMETER)
295 SetLastError(0xdeadbeef);
296 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
297 SERVICE_DISABLED, 0, empty, NULL, NULL, NULL, NULL, NULL);
298 ok(!svc_handle1, "Expected failure\n");
299 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
301 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
303 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
304 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
306 SetLastError(0xdeadbeef);
307 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS,
308 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
309 ok(!svc_handle1, "Expected failure\n");
310 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
312 /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
313 SetLastError(0xdeadbeef);
314 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_FILE_SYSTEM_DRIVER | SERVICE_INTERACTIVE_PROCESS,
315 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
316 ok(!svc_handle1, "Expected failure\n");
317 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
319 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
320 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
322 SetLastError(0xdeadbeef);
323 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
324 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, account, password);
325 ok(!svc_handle1, "Expected failure\n");
326 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INVALID_SERVICE_ACCOUNT,
327 "Expected ERROR_INVALID_PARAMETER or ERROR_INVALID_SERVICE_ACCOUNT, got %d\n", GetLastError());
329 /* Illegal (start-type is not a mask and should only be one of the possibilities)
330 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
331 * it's most likely not the wanted start-type)
333 SetLastError(0xdeadbeef);
334 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS,
335 SERVICE_AUTO_START | SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
336 ok(!svc_handle1, "Expected failure\n");
337 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
339 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
340 SetLastError(0xdeadbeef);
341 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
342 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
343 ok(!svc_handle1, "Expected failure\n");
344 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
346 /* Test if ServiceType can be a combined one for drivers */
347 SetLastError(0xdeadbeef);
348 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER,
349 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
350 ok(!svc_handle1, "Expected failure\n");
351 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
353 /* The service already exists (check first, just in case) */
354 svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
355 if (svc_handle1)
357 spooler_exists = TRUE;
358 CloseServiceHandle(svc_handle1);
359 SetLastError(0xdeadbeef);
360 svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
361 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
362 ok(!svc_handle1, "Expected failure\n");
363 ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
365 else
366 skip("Spooler service doesn't exist\n");
368 /* To find an existing displayname we check the 'Spooler' service. Although the registry
369 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
370 * to the servicename and can't be used as well for a new displayname.
372 if (spooler_exists)
374 ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
376 if (!ret)
377 skip("Could not retrieve a displayname for the Spooler service\n");
378 else
380 svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
381 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
382 ok(!svc_handle1, "Expected failure\n");
383 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
384 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
387 else
388 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
390 /* Windows doesn't care about the access rights for creation (which makes
391 * sense as there is no service yet) as long as there are sufficient
392 * rights to the manager.
394 SetLastError(0xdeadbeef);
395 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
396 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
397 ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
399 /* DeleteService however must have proper rights */
400 SetLastError(0xdeadbeef);
401 ret = DeleteService(svc_handle1);
402 ok(!ret, "Expected failure\n");
403 ok(GetLastError() == ERROR_ACCESS_DENIED,
404 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
406 /* Open the service with minimal rights for deletion.
407 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
409 CloseServiceHandle(svc_handle1);
410 svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
412 /* Now that we have the proper rights, we should be able to delete */
413 SetLastError(0xdeadbeef);
414 ret = DeleteService(svc_handle1);
415 ok(ret, "Expected success, got error %u\n", GetLastError());
417 /* Service is marked for delete, but handle is still open. Try to open service again. */
418 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
419 ok(svc_handle2 != NULL, "got %p, error %u\n", svc_handle2, GetLastError());
420 CloseServiceHandle(svc_handle2);
422 CloseServiceHandle(svc_handle1);
423 CloseServiceHandle(scm_handle);
425 /* Wait a while. One of the following tests also does a CreateService for the
426 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
427 * error if we do this too quickly. Vista seems more picky than the others.
429 Sleep(1000);
431 /* And a final NULL check */
432 SetLastError(0xdeadbeef);
433 ret = DeleteService(NULL);
434 ok(!ret, "Expected failure\n");
435 ok(GetLastError() == ERROR_INVALID_HANDLE,
436 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
439 static void test_get_displayname(void)
441 SC_HANDLE scm_handle, svc_handle;
442 BOOL ret;
443 CHAR displayname[4096];
444 WCHAR displaynameW[2048];
445 DWORD displaysize, tempsize, tempsizeW;
446 static const CHAR deadbeef[] = "Deadbeef";
447 static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
448 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
449 static const WCHAR abcW[] = {'A','B','C',0};
450 static const CHAR servicename[] = "Winetest";
451 static const CHAR pathname[] = "we_dont_care.exe";
453 /* Having NULL for the size of the buffer will crash on W2K3 */
455 SetLastError(0xdeadbeef);
456 ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
457 ok(!ret, "Expected failure\n");
458 ok(GetLastError() == ERROR_INVALID_HANDLE,
459 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
461 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
463 SetLastError(0xdeadbeef);
464 ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
465 ok(!ret, "Expected failure\n");
466 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
467 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
468 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
470 SetLastError(0xdeadbeef);
471 displaysize = sizeof(displayname);
472 ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
473 ok(!ret, "Expected failure\n");
474 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
475 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
476 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
478 /* Test for nonexistent service */
479 SetLastError(0xdeadbeef);
480 displaysize = -1;
481 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
482 ok(!ret, "Expected failure\n");
483 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
484 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
486 SetLastError(0xdeadbeef);
487 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
488 ok(!ret, "Expected failure\n");
489 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
490 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
491 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
493 displaysize = 15;
494 strcpy(displayname, "ABC");
495 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
496 ok(!ret, "Expected failure\n");
497 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
498 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
499 todo_wine ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
500 ok(displayname[0] == 0, "Service name not empty\n");
502 displaysize = 15;
503 lstrcpyW( displaynameW, abcW );
504 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
505 ok(!ret, "Expected failure\n");
506 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
507 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
508 ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
509 ok(displaynameW[0] == 0, "Service name not empty\n");
511 displaysize = 0;
512 strcpy(displayname, "ABC");
513 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
514 ok(!ret, "Expected failure\n");
515 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
516 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
517 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
518 ok(displayname[0] == 'A', "Service name changed\n");
520 displaysize = 0;
521 lstrcpyW( displaynameW, abcW );
522 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
523 ok(!ret, "Expected failure\n");
524 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
525 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
526 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
527 ok(displaynameW[0] == 'A', "Service name changed\n");
529 displaysize = 1;
530 strcpy(displayname, "ABC");
531 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
532 ok(!ret, "Expected failure\n");
533 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
534 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
535 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
536 ok(displayname[0] == 0, "Service name not empty\n");
538 displaysize = 1;
539 lstrcpyW( displaynameW, abcW );
540 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
541 ok(!ret, "Expected failure\n");
542 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
543 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
544 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
545 ok(displaynameW[0] == 'A', "Service name changed\n");
547 displaysize = 2;
548 strcpy(displayname, "ABC");
549 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
550 ok(!ret, "Expected failure\n");
551 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
552 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
553 todo_wine ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
554 ok(displayname[0] == 0, "Service name not empty\n");
556 displaysize = 2;
557 lstrcpyW( displaynameW, abcW );
558 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
559 ok(!ret, "Expected failure\n");
560 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
561 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
562 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
563 ok(displaynameW[0] == 0, "Service name not empty\n");
565 /* Check if 'Spooler' exists */
566 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
567 if (!svc_handle)
569 skip("Spooler service doesn't exist\n");
570 CloseServiceHandle(scm_handle);
571 return;
573 CloseServiceHandle(svc_handle);
575 /* Retrieve the needed size for the buffer */
576 SetLastError(0xdeadbeef);
577 displaysize = -1;
578 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
579 ok(!ret, "Expected failure\n");
580 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
581 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
582 tempsize = displaysize;
584 displaysize = 0;
585 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
586 ok(!ret, "Expected failure\n");
587 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
588 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
589 ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
591 /* Buffer is too small */
592 SetLastError(0xdeadbeef);
593 displaysize = (tempsize / 2);
594 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
595 ok(!ret, "Expected failure\n");
596 ok(displaysize == tempsize, "Expected the needed buffersize\n");
597 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
598 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
600 /* First try with a buffer that should be big enough to hold
601 * the ANSI string (and terminating character). This succeeds on Windows
602 * although when asked (see above 2 tests) it will return twice the needed size.
604 SetLastError(0xdeadbeef);
605 displaysize = (tempsize / 2) + 1;
606 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
607 ok(ret, "Expected success, got error %u\n", GetLastError());
608 ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
610 /* Now with the original returned size */
611 SetLastError(0xdeadbeef);
612 displaysize = tempsize;
613 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
614 ok(ret, "Expected success, got error %u\n", GetLastError());
615 ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
617 /* And with a bigger than needed buffer */
618 SetLastError(0xdeadbeef);
619 displaysize = tempsize * 2;
620 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
621 ok(ret, "Expected success, got error %u\n", GetLastError());
622 /* Test that shows that if the buffersize is enough, it's not changed */
623 ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
624 ok(strlen(displayname) == tempsize/2,
625 "Expected the buffer to be twice the length of the string\n") ;
627 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
628 SetLastError(0xdeadbeef);
629 displaysize = -1;
630 ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
631 ok(!ret, "Expected failure\n");
632 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
633 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
635 /* Buffer is too small */
636 SetLastError(0xdeadbeef);
637 tempsizeW = displaysize;
638 displaysize = tempsizeW / 2;
639 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
640 ok(!ret, "Expected failure\n");
641 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
642 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
643 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
645 /* Now with the original returned size */
646 SetLastError(0xdeadbeef);
647 displaysize = tempsizeW;
648 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
649 ok(!ret, "Expected failure\n");
650 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
651 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
652 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
654 /* And with a bigger than needed buffer */
655 SetLastError(0xdeadbeef);
656 displaysize = tempsizeW + 1; /* This caters for the null terminating character */
657 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
658 ok(ret, "Expected success, got error %u\n", GetLastError());
659 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
660 ok(lstrlenW(displaynameW) == displaysize,
661 "Expected the buffer to be the length of the string\n") ;
662 ok(tempsize / 2 == tempsizeW,
663 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
665 CloseServiceHandle(scm_handle);
667 /* Test for a service without a displayname (which is valid). This should return
668 * the servicename itself.
670 SetLastError(0xdeadbeef);
671 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
672 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
674 skip("Not enough rights to get a handle to the manager\n");
675 return;
678 SetLastError(0xdeadbeef);
679 svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
680 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
681 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
682 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
683 if (!svc_handle)
685 CloseServiceHandle(scm_handle);
686 return;
689 /* Retrieve the needed size for the buffer */
690 SetLastError(0xdeadbeef);
691 displaysize = -1;
692 ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
693 ok(!ret, "Expected failure\n");
694 ok(displaysize == strlen(servicename) * 2,
695 "Expected the displaysize to be twice the size of the servicename\n");
696 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
697 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
699 /* Buffer is too small */
700 SetLastError(0xdeadbeef);
701 tempsize = displaysize;
702 displaysize = (tempsize / 2);
703 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
704 ok(!ret, "Expected failure\n");
705 ok(displaysize == tempsize, "Expected the needed buffersize\n");
706 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
707 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
709 /* Get the displayname */
710 SetLastError(0xdeadbeef);
711 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
712 ok(ret, "Expected success, got error %u\n", GetLastError());
713 ok(!lstrcmpiA(displayname, servicename),
714 "Expected displayname to be %s, got %s\n", servicename, displayname);
716 /* Delete the service */
717 ret = DeleteService(svc_handle);
718 ok(ret, "Expected success (err=%d)\n", GetLastError());
720 CloseServiceHandle(svc_handle);
721 CloseServiceHandle(scm_handle);
723 /* Wait a while. Just in case one of the following tests does a CreateService again */
724 Sleep(1000);
727 static void test_get_servicekeyname(void)
729 SC_HANDLE scm_handle, svc_handle;
730 CHAR servicename[4096];
731 CHAR displayname[4096];
732 WCHAR servicenameW[4096];
733 WCHAR displaynameW[4096];
734 DWORD servicesize, displaysize, tempsize;
735 BOOL ret;
736 static const CHAR deadbeef[] = "Deadbeef";
737 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
738 static const WCHAR abcW[] = {'A','B','C',0};
740 /* Having NULL for the size of the buffer will crash on W2K3 */
742 SetLastError(0xdeadbeef);
743 ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
744 ok(!ret, "Expected failure\n");
745 ok(GetLastError() == ERROR_INVALID_HANDLE,
746 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
748 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
750 servicesize = 200;
751 SetLastError(0xdeadbeef);
752 ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
753 ok(!ret, "Expected failure\n");
754 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
755 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
756 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
757 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
759 /* Valid handle and buffer but no displayname */
760 servicesize = 200;
761 SetLastError(0xdeadbeef);
762 ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
763 ok(!ret, "Expected failure\n");
764 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
765 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
766 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
767 todo_wine ok(servicesize == 200, "Service size expected 1, got %d\n", servicesize);
769 /* Test for nonexistent displayname */
770 SetLastError(0xdeadbeef);
771 ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
772 ok(!ret, "Expected failure\n");
773 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
774 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
775 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
777 servicesize = 15;
778 strcpy(servicename, "ABC");
779 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
780 ok(!ret, "Expected failure\n");
781 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
782 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
783 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
784 ok(servicename[0] == 0, "Service name not empty\n");
786 servicesize = 15;
787 lstrcpyW( servicenameW, abcW );
788 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
789 ok(!ret, "Expected failure\n");
790 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
791 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
792 ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
793 ok(servicenameW[0] == 0, "Service name not empty\n");
795 servicesize = 0;
796 strcpy(servicename, "ABC");
797 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
798 ok(!ret, "Expected failure\n");
799 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
800 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
801 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
802 ok(servicename[0] == 'A', "Service name changed\n");
804 servicesize = 0;
805 lstrcpyW( servicenameW, abcW );
806 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
807 ok(!ret, "Expected failure\n");
808 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
809 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
810 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
811 ok(servicenameW[0] == 'A', "Service name changed\n");
813 servicesize = 1;
814 strcpy(servicename, "ABC");
815 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
816 ok(!ret, "Expected failure\n");
817 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
818 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
819 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
820 ok(servicename[0] == 0, "Service name not empty\n");
822 servicesize = 1;
823 lstrcpyW( servicenameW, abcW );
824 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
825 ok(!ret, "Expected failure\n");
826 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
827 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
828 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
829 ok(servicenameW[0] == 'A', "Service name changed\n");
831 servicesize = 2;
832 strcpy(servicename, "ABC");
833 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
834 ok(!ret, "Expected failure\n");
835 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
836 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
837 todo_wine ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
838 ok(servicename[0] == 0, "Service name not empty\n");
840 servicesize = 2;
841 lstrcpyW( servicenameW, abcW );
842 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
843 ok(!ret, "Expected failure\n");
844 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
845 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
846 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
847 ok(servicenameW[0] == 0, "Service name not empty\n");
849 /* Check if 'Spooler' exists */
850 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
851 if (!svc_handle)
853 skip("Spooler service doesn't exist\n");
854 CloseServiceHandle(scm_handle);
855 return;
857 CloseServiceHandle(svc_handle);
859 /* Get the displayname for the 'Spooler' service */
860 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
861 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
863 /* Retrieve the needed size for the buffer */
864 SetLastError(0xdeadbeef);
865 servicesize = 0;
866 ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
867 ok(!ret, "Expected failure\n");
868 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
869 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
871 /* Valid call with the correct buffersize */
872 SetLastError(0xdeadbeef);
873 tempsize = servicesize;
874 servicesize *= 2;
875 ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
876 ok(ret, "Expected success, got error %u\n", GetLastError());
877 if (ret)
879 ok(strlen(servicename) == tempsize/2,
880 "Expected the buffer to be twice the length of the string\n") ;
881 ok(!lstrcmpiA(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
882 ok(servicesize == (tempsize * 2),
883 "Expected servicesize not to change if buffer not insufficient\n") ;
886 MultiByteToWideChar(CP_ACP, 0, displayname, -1, displaynameW, sizeof(displaynameW)/2);
887 SetLastError(0xdeadbeef);
888 servicesize *= 2;
889 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
890 ok(ret, "Expected success, got error %u\n", GetLastError());
891 if (ret)
893 ok(strlen(servicename) == tempsize/2,
894 "Expected the buffer to be twice the length of the string\n") ;
895 ok(servicesize == lstrlenW(servicenameW),
896 "Expected servicesize not to change if buffer not insufficient\n") ;
899 SetLastError(0xdeadbeef);
900 servicesize = 3;
901 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
902 ok(!ret, "Expected failure\n");
903 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
904 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
905 ok(servicenameW[0] == 0, "Buffer not empty\n");
907 CloseServiceHandle(scm_handle);
910 static void test_query_svc(void)
912 SC_HANDLE scm_handle, svc_handle;
913 BOOL ret;
914 SERVICE_STATUS status;
915 SERVICE_STATUS_PROCESS *statusproc;
916 DWORD bufsize, needed;
918 /* All NULL or wrong */
919 SetLastError(0xdeadbeef);
920 ret = QueryServiceStatus(NULL, NULL);
921 ok(!ret, "Expected failure\n");
922 ok(GetLastError() == ERROR_INVALID_HANDLE,
923 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
925 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
927 /* Check if 'Spooler' exists.
928 * Open with not enough rights to query the status.
930 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
931 if (!svc_handle)
933 skip("Spooler service doesn't exist\n");
934 CloseServiceHandle(scm_handle);
935 return;
938 SetLastError(0xdeadbeef);
939 ret = QueryServiceStatus(svc_handle, NULL);
940 ok(!ret, "Expected failure\n");
941 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
942 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
943 "Unexpected last error %d\n", GetLastError());
945 SetLastError(0xdeadbeef);
946 ret = QueryServiceStatus(svc_handle, &status);
947 ok(!ret, "Expected failure\n");
948 ok(GetLastError() == ERROR_ACCESS_DENIED,
949 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
951 /* Open the service with just enough rights.
952 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
954 CloseServiceHandle(svc_handle);
955 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
957 SetLastError(0xdeadbeef);
958 ret = QueryServiceStatus(svc_handle, &status);
959 ok(ret, "Expected success, got error %u\n", GetLastError());
961 CloseServiceHandle(svc_handle);
963 /* More or less the same tests for QueryServiceStatusEx */
964 if (!pQueryServiceStatusEx)
966 win_skip( "QueryServiceStatusEx not available\n" );
967 CloseServiceHandle(scm_handle);
968 return;
971 /* Open service with not enough rights to query the status */
972 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
974 /* All NULL or wrong, this proves that info level is checked first */
975 SetLastError(0xdeadbeef);
976 ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
977 ok(!ret, "Expected failure\n");
978 ok(GetLastError() == ERROR_INVALID_LEVEL,
979 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
981 /* Passing a NULL parameter for the needed buffer size
982 * will crash on anything but NT4.
985 /* Only info level is correct. It looks like the buffer/size is checked second */
986 SetLastError(0xdeadbeef);
987 ret = pQueryServiceStatusEx(NULL, SC_STATUS_PROCESS_INFO, NULL, 0, &needed);
988 /* NT4 checks the handle first */
989 if (GetLastError() != ERROR_INVALID_HANDLE)
991 ok(!ret, "Expected failure\n");
992 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
993 "Needed buffersize is wrong : %d\n", needed);
994 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
995 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
998 /* Pass a correct buffer and buffersize but a NULL handle */
999 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1000 bufsize = needed;
1001 SetLastError(0xdeadbeef);
1002 ret = pQueryServiceStatusEx(NULL, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
1003 ok(!ret, "Expected failure\n");
1004 ok(GetLastError() == ERROR_INVALID_HANDLE,
1005 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1006 HeapFree(GetProcessHeap(), 0, statusproc);
1008 /* Correct handle and info level */
1009 SetLastError(0xdeadbeef);
1010 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, NULL, 0, &needed);
1011 /* NT4 doesn't return the needed size */
1012 if (GetLastError() != ERROR_INVALID_PARAMETER)
1014 ok(!ret, "Expected failure\n");
1015 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
1016 "Needed buffersize is wrong : %d\n", needed);
1017 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1018 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1021 /* All parameters are OK but we don't have enough rights */
1022 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1023 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1024 SetLastError(0xdeadbeef);
1025 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
1026 ok(!ret, "Expected failure\n");
1027 ok(GetLastError() == ERROR_ACCESS_DENIED,
1028 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1029 HeapFree(GetProcessHeap(), 0, statusproc);
1031 /* Open the service with just enough rights. */
1032 CloseServiceHandle(svc_handle);
1033 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
1035 /* Everything should be fine now. */
1036 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1037 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1038 SetLastError(0xdeadbeef);
1039 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
1040 ok(ret, "Expected success, got error %u\n", GetLastError());
1041 if (statusproc->dwCurrentState == SERVICE_RUNNING)
1042 ok(statusproc->dwProcessId != 0,
1043 "Expect a process id for this running service\n");
1044 else
1045 ok(statusproc->dwProcessId == 0,
1046 "Expect no process id for this stopped service\n");
1048 /* same call with null needed pointer */
1049 SetLastError(0xdeadbeef);
1050 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, NULL);
1051 ok(!ret, "Expected failure\n");
1052 ok(broken(GetLastError() == ERROR_INVALID_PARAMETER) /* NT4 */ ||
1053 GetLastError() == ERROR_INVALID_ADDRESS, "got %d\n", GetLastError());
1055 HeapFree(GetProcessHeap(), 0, statusproc);
1057 CloseServiceHandle(svc_handle);
1058 CloseServiceHandle(scm_handle);
1061 static void test_enum_svc(void)
1063 SC_HANDLE scm_handle;
1064 BOOL ret;
1065 DWORD bufsize, needed, returned, resume;
1066 DWORD neededW, returnedW;
1067 DWORD tempneeded, tempreturned, missing;
1068 DWORD servicecountactive, servicecountinactive;
1069 ENUM_SERVICE_STATUSA *services;
1070 ENUM_SERVICE_STATUSW *servicesW;
1071 ENUM_SERVICE_STATUS_PROCESSA *exservices;
1072 UINT i;
1074 /* All NULL or wrong */
1075 SetLastError(0xdeadbeef);
1076 ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1077 ok(!ret, "Expected failure\n");
1078 ok(GetLastError() == ERROR_INVALID_HANDLE,
1079 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1081 SetLastError(0xdeadbeef);
1082 ret = EnumServicesStatusW(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1083 ok(!ret, "Expected failure\n");
1084 ok(GetLastError() == ERROR_INVALID_HANDLE,
1085 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1087 /* Open the service control manager with not enough rights at first */
1088 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1090 /* Valid handle but rest is still NULL or wrong */
1091 SetLastError(0xdeadbeef);
1092 ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1093 ok(!ret, "Expected failure\n");
1094 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1095 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1096 "Unexpected last error %d\n", GetLastError());
1098 SetLastError(0xdeadbeef);
1099 ret = EnumServicesStatusW(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1100 ok(!ret, "Expected failure\n");
1101 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1102 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1103 "Unexpected last error %d\n", GetLastError());
1105 /* Don't specify the two required pointers */
1106 returned = 0xdeadbeef;
1107 SetLastError(0xdeadbeef);
1108 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1109 ok(!ret, "Expected failure\n");
1110 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1111 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1112 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1113 "Unexpected last error %d\n", GetLastError());
1115 returned = 0xdeadbeef;
1116 SetLastError(0xdeadbeef);
1117 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1118 ok(!ret, "Expected failure\n");
1119 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1120 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1121 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1122 "Unexpected last error %d\n", GetLastError());
1124 /* Don't specify the two required pointers */
1125 needed = 0xdeadbeef;
1126 SetLastError(0xdeadbeef);
1127 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1128 ok(!ret, "Expected failure\n");
1129 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1130 "Expected no change to the needed buffer variable\n");
1131 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1132 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1133 "Unexpected last error %d\n", GetLastError());
1135 needed = 0xdeadbeef;
1136 SetLastError(0xdeadbeef);
1137 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1138 ok(!ret, "Expected failure\n");
1139 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1140 "Expected no change to the needed buffer variable\n");
1141 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1142 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1143 "Unexpected last error %d\n", GetLastError());
1145 /* No valid servicetype and servicestate */
1146 needed = 0xdeadbeef;
1147 returned = 0xdeadbeef;
1148 SetLastError(0xdeadbeef);
1149 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1150 ok(!ret, "Expected failure\n");
1151 ok(needed == 0 || broken(needed != 0), /* nt4 */
1152 "Expected needed buffer size to be set to 0, got %d\n", needed);
1153 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1154 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1155 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1157 needed = 0xdeadbeef;
1158 returned = 0xdeadbeef;
1159 SetLastError(0xdeadbeef);
1160 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1161 ok(!ret, "Expected failure\n");
1162 ok(needed == 0 || broken(needed != 0), /* nt4 */
1163 "Expected needed buffer size to be set to 0, got %d\n", needed);
1164 ok(returned == 0 || broken(returned != 0), /* nt4 */
1165 "Expected number of services to be set to 0, got %d\n", returned);
1166 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1167 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1169 /* No valid servicestate */
1170 needed = 0xdeadbeef;
1171 returned = 0xdeadbeef;
1172 SetLastError(0xdeadbeef);
1173 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1174 ok(!ret, "Expected failure\n");
1175 ok(needed == 0 || broken(needed != 0), /* nt4 */
1176 "Expected needed buffer size to be set to 0, got %d\n", needed);
1177 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1178 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1179 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1181 needed = 0xdeadbeef;
1182 returned = 0xdeadbeef;
1183 SetLastError(0xdeadbeef);
1184 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1185 ok(!ret, "Expected failure\n");
1186 ok(needed == 0 || broken(needed != 0), /* nt4 */
1187 "Expected needed buffer size to be set to 0, got %d\n", needed);
1188 ok(returned == 0 || broken(returned != 0), /* nt4 */
1189 "Expected number of services to be set to 0, got %d\n", returned);
1190 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1191 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1193 /* No valid servicetype */
1194 needed = 0xdeadbeef;
1195 returned = 0xdeadbeef;
1196 SetLastError(0xdeadbeef);
1197 ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1198 ok(!ret, "Expected failure\n");
1199 ok(needed == 0 || broken(needed != 0), /* nt4 */
1200 "Expected needed buffer size to be set to 0, got %d\n", needed);
1201 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1202 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1203 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1205 needed = 0xdeadbeef;
1206 returned = 0xdeadbeef;
1207 SetLastError(0xdeadbeef);
1208 ret = EnumServicesStatusW(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1209 ok(!ret, "Expected failure\n");
1210 ok(needed == 0 || broken(needed != 0), /* nt4 */
1211 "Expected needed buffer size to be set to 0, got %d\n", needed);
1212 ok(returned == 0 || broken(returned != 0), /* nt4 */
1213 "Expected number of services to be set to 0, got %d\n", returned);
1214 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1215 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1217 /* All parameters are correct but our access rights are wrong */
1218 needed = 0xdeadbeef;
1219 returned = 0xdeadbeef;
1220 SetLastError(0xdeadbeef);
1221 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1222 ok(!ret, "Expected failure\n");
1223 ok(needed == 0 || broken(needed != 0), /* nt4 */
1224 "Expected needed buffer size to be set to 0, got %d\n", needed);
1225 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1226 ok(GetLastError() == ERROR_ACCESS_DENIED,
1227 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1229 needed = 0xdeadbeef;
1230 returned = 0xdeadbeef;
1231 SetLastError(0xdeadbeef);
1232 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1233 ok(!ret, "Expected failure\n");
1234 ok(needed == 0 || broken(needed != 0), /* nt4 */
1235 "Expected needed buffer size to be set to 0, got %d\n", needed);
1236 ok(returned == 0 || broken(returned != 0), /* nt4 */
1237 "Expected number of services to be set to 0, got %d\n", returned);
1238 ok(GetLastError() == ERROR_ACCESS_DENIED,
1239 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1241 /* Open the service control manager with the needed rights */
1242 CloseServiceHandle(scm_handle);
1243 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1245 /* All parameters are correct. Request the needed buffer size */
1246 needed = 0xdeadbeef;
1247 returned = 0xdeadbeef;
1248 SetLastError(0xdeadbeef);
1249 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1250 ok(!ret, "Expected failure\n");
1251 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1252 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1253 ok(GetLastError() == ERROR_MORE_DATA,
1254 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1256 /* Test to show we get the same needed buffer size for the W-call */
1257 neededW = 0xdeadbeef;
1258 returnedW = 0xdeadbeef;
1259 SetLastError(0xdeadbeef);
1260 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &neededW, &returnedW, NULL);
1261 ok(!ret, "Expected failure\n");
1262 ok(neededW != 0xdeadbeef && neededW > 0, "Expected the needed buffer size for this one service\n");
1263 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1264 ok(returnedW == 0, "Expected no service returned, got %d\n", returnedW);
1265 ok(GetLastError() == ERROR_MORE_DATA,
1266 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1268 /* Store the needed bytes */
1269 tempneeded = needed;
1271 /* Allocate the correct needed bytes */
1272 services = HeapAlloc(GetProcessHeap(), 0, needed);
1273 bufsize = needed;
1274 needed = 0xdeadbeef;
1275 returned = 0xdeadbeef;
1276 SetLastError(0xdeadbeef);
1277 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1278 services, bufsize, &needed, &returned, NULL);
1279 ok(ret, "Expected success, got error %u\n", GetLastError());
1280 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1281 ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1282 HeapFree(GetProcessHeap(), 0, services);
1284 /* Store the number of returned services */
1285 tempreturned = returned;
1287 servicesW = HeapAlloc(GetProcessHeap(), 0, neededW);
1288 bufsize = neededW;
1289 neededW = 0xdeadbeef;
1290 returnedW = 0xdeadbeef;
1291 SetLastError(0xdeadbeef);
1292 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1293 servicesW, bufsize, &neededW, &returnedW, NULL);
1294 ok(ret, "Expected success, got error %u\n", GetLastError());
1295 ok(neededW == 0, "Expected needed buffer to be 0 as we are done\n");
1296 ok(returnedW != 0xdeadbeef && returnedW > 0, "Expected some returned services\n");
1297 HeapFree(GetProcessHeap(), 0, servicesW);
1299 /* Allocate less than the needed bytes and don't specify a resume handle */
1300 services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1301 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUSA);
1302 needed = 0xdeadbeef;
1303 returned = 0xdeadbeef;
1304 SetLastError(0xdeadbeef);
1305 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1306 services, bufsize, &needed, &returned, NULL);
1307 ok(!ret, "Expected failure\n");
1308 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1309 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1310 ok(GetLastError() == ERROR_MORE_DATA,
1311 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1313 /* Allocate less than the needed bytes, this time with a correct resume handle */
1314 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUSA);
1315 needed = 0xdeadbeef;
1316 returned = 0xdeadbeef;
1317 resume = 0;
1318 SetLastError(0xdeadbeef);
1319 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1320 services, bufsize, &needed, &returned, &resume);
1321 ok(!ret, "Expected failure\n");
1322 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1323 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1324 todo_wine ok(resume, "Expected a resume handle\n");
1325 ok(GetLastError() == ERROR_MORE_DATA,
1326 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1328 /* Fetch the missing services but pass a bigger buffer size */
1329 missing = tempreturned - returned;
1330 bufsize = tempneeded;
1331 needed = 0xdeadbeef;
1332 returned = 0xdeadbeef;
1333 SetLastError(0xdeadbeef);
1334 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1335 services, bufsize, &needed, &returned, &resume);
1336 ok(ret, "Expected success, got error %u\n", GetLastError());
1337 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1338 ok(returned == missing, "Expected %u services to be returned\n", missing);
1339 ok(resume == 0, "Expected the resume handle to be 0\n");
1340 HeapFree(GetProcessHeap(), 0, services);
1342 /* See if things add up */
1344 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1345 * and doesn't count the others as inactive. This means that Vista could
1346 * show a total that is greater than the sum of active and inactive
1347 * drivers.
1348 * The number of active and inactive drivers is greatly influenced by the
1349 * time when tests are run, immediately after boot or later for example.
1351 * Both reasons make calculations for drivers not so useful
1354 /* Get the number of active win32 services */
1355 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1356 &needed, &returned, NULL);
1357 services = HeapAlloc(GetProcessHeap(), 0, needed);
1358 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1359 needed, &needed, &returned, NULL);
1360 HeapFree(GetProcessHeap(), 0, services);
1362 servicecountactive = returned;
1364 /* Get the number of inactive win32 services */
1365 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1366 &needed, &returned, NULL);
1367 services = HeapAlloc(GetProcessHeap(), 0, needed);
1368 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1369 needed, &needed, &returned, NULL);
1370 HeapFree(GetProcessHeap(), 0, services);
1372 servicecountinactive = returned;
1374 /* Get the number of win32 services */
1375 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1376 &needed, &returned, NULL);
1377 services = HeapAlloc(GetProcessHeap(), 0, needed);
1378 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1379 needed, &needed, &returned, NULL);
1380 HeapFree(GetProcessHeap(), 0, services);
1382 /* Check if total is the same as active and inactive win32 services */
1383 ok(returned == (servicecountactive + servicecountinactive),
1384 "Something wrong in the calculation\n");
1386 /* Get all drivers and services
1388 * Fetch the status of the last call as failing could make the following tests crash
1389 * on Wine (we don't return anything yet).
1391 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1392 NULL, 0, &needed, &returned, NULL);
1393 services = HeapAlloc(GetProcessHeap(), 0, needed);
1394 ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1395 services, needed, &needed, &returned, NULL);
1397 /* Loop through all those returned drivers and services */
1398 for (i = 0; ret && i < returned; i++)
1400 SERVICE_STATUS status = services[i].ServiceStatus;
1402 /* lpServiceName and lpDisplayName should always be filled */
1403 ok(services[i].lpServiceName[0], "Expected a service name\n");
1404 ok(services[i].lpDisplayName[0], "Expected a display name\n");
1406 /* Decrement the counters to see if the functions calls return the same
1407 * numbers as the contents of these structures.
1409 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1411 if (status.dwCurrentState == SERVICE_STOPPED)
1412 servicecountinactive--;
1413 else
1414 servicecountactive--;
1417 HeapFree(GetProcessHeap(), 0, services);
1419 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1420 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1422 CloseServiceHandle(scm_handle);
1424 /* More or less the same for EnumServicesStatusExA */
1425 if (!pEnumServicesStatusExA)
1427 win_skip( "EnumServicesStatusExA not available\n" );
1428 return;
1431 /* All NULL or wrong */
1432 SetLastError(0xdeadbeef);
1433 ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1434 ok(!ret, "Expected failure\n");
1435 ok(GetLastError() == ERROR_INVALID_LEVEL,
1436 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1438 /* All NULL or wrong, just the info level is correct */
1439 SetLastError(0xdeadbeef);
1440 ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1441 ok(!ret, "Expected failure\n");
1442 ok(GetLastError() == ERROR_INVALID_HANDLE,
1443 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1445 /* Open the service control manager with not enough rights at first */
1446 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1448 /* Valid handle and info level but rest is still NULL or wrong */
1449 SetLastError(0xdeadbeef);
1450 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1451 ok(!ret, "Expected failure\n");
1452 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1453 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1454 "Unexpected last error %d\n", GetLastError());
1456 /* Don't specify the two required pointers */
1457 needed = 0xdeadbeef;
1458 SetLastError(0xdeadbeef);
1459 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1460 ok(!ret, "Expected failure\n");
1461 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1462 "Expected no change to the needed buffer variable\n");
1463 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1464 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1465 "Unexpected last error %d\n", GetLastError());
1467 /* Don't specify the two required pointers */
1468 returned = 0xdeadbeef;
1469 SetLastError(0xdeadbeef);
1470 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1471 ok(!ret, "Expected failure\n");
1472 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1473 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1474 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1475 "Unexpected last error %d\n", GetLastError());
1477 /* No valid servicetype and servicestate */
1478 needed = 0xdeadbeef;
1479 returned = 0xdeadbeef;
1480 SetLastError(0xdeadbeef);
1481 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1482 ok(!ret, "Expected failure\n");
1483 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1484 ok(needed == 0 || broken(needed != 0), /* nt4 */
1485 "Expected needed buffer size to be set to 0, got %d\n", needed);
1486 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1487 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1489 /* No valid servicestate */
1490 needed = 0xdeadbeef;
1491 returned = 0xdeadbeef;
1492 SetLastError(0xdeadbeef);
1493 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1494 &needed, &returned, NULL, NULL);
1495 ok(!ret, "Expected failure\n");
1496 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1497 ok(needed == 0 || broken(needed != 0), /* nt4 */
1498 "Expected needed buffer size to be set to 0, got %d\n", needed);
1499 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1500 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1502 /* No valid servicetype */
1503 needed = 0xdeadbeef;
1504 returned = 0xdeadbeef;
1505 SetLastError(0xdeadbeef);
1506 ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1507 &needed, &returned, NULL, NULL);
1508 ok(!ret, "Expected failure\n");
1509 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1510 ok(needed == 0 || broken(needed != 0), /* nt4 */
1511 "Expected needed buffer size to be set to 0, got %d\n", needed);
1512 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1513 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1515 /* No valid servicetype and servicestate and unknown service group */
1516 needed = 0xdeadbeef;
1517 returned = 0xdeadbeef;
1518 SetLastError(0xdeadbeef);
1519 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1520 &returned, NULL, "deadbeef_group");
1521 ok(!ret, "Expected failure\n");
1522 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1523 ok(needed == 0 || broken(needed != 0), /* nt4 */
1524 "Expected needed buffer size to be set to 0, got %d\n", needed);
1525 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1526 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1528 /* All parameters are correct but our access rights are wrong */
1529 needed = 0xdeadbeef;
1530 returned = 0xdeadbeef;
1531 SetLastError(0xdeadbeef);
1532 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1533 NULL, 0, &needed, &returned, NULL, NULL);
1534 ok(!ret, "Expected failure\n");
1535 ok(needed == 0 || broken(needed != 0), /* nt4 */
1536 "Expected needed buffer size to be set to 0, got %d\n", needed);
1537 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1538 ok(GetLastError() == ERROR_ACCESS_DENIED,
1539 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1541 /* All parameters are correct, access rights are wrong but the
1542 * group name won't be checked yet.
1544 needed = 0xdeadbeef;
1545 returned = 0xdeadbeef;
1546 SetLastError(0xdeadbeef);
1547 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1548 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1549 ok(!ret, "Expected failure\n");
1550 ok(needed == 0 || broken(needed != 0), /* nt4 */
1551 "Expected needed buffer size to be set to 0, got %d\n", needed);
1552 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1553 ok(GetLastError() == ERROR_ACCESS_DENIED,
1554 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1556 /* Open the service control manager with the needed rights */
1557 CloseServiceHandle(scm_handle);
1558 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1560 /* All parameters are correct and the group will be checked */
1561 needed = 0xdeadbeef;
1562 returned = 0xdeadbeef;
1563 SetLastError(0xdeadbeef);
1564 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1565 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1566 ok(!ret, "Expected failure\n");
1567 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1568 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1569 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1570 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1572 /* TODO: Create a test that makes sure we enumerate all services that don't
1573 * belong to a group. (specifying "").
1576 /* All parameters are correct. Request the needed buffer size */
1577 needed = 0xdeadbeef;
1578 returned = 0xdeadbeef;
1579 SetLastError(0xdeadbeef);
1580 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1581 NULL, 0, &needed, &returned, NULL, NULL);
1582 ok(!ret, "Expected failure\n");
1583 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1584 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1585 ok(GetLastError() == ERROR_MORE_DATA,
1586 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1588 /* Test to show we get the same needed buffer size for the W-call */
1589 neededW = 0xdeadbeef;
1590 ret = pEnumServicesStatusExW(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1591 NULL, 0, &neededW, &returnedW, NULL, NULL);
1592 ok(!ret, "Expected failure\n");
1593 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1595 /* Store the needed bytes */
1596 tempneeded = needed;
1598 /* Allocate the correct needed bytes */
1599 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1600 bufsize = needed;
1601 needed = 0xdeadbeef;
1602 returned = 0xdeadbeef;
1603 SetLastError(0xdeadbeef);
1604 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1605 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1606 ok(ret, "Expected success, got error %u\n", GetLastError());
1607 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1608 ok(returned == tempreturned, "Expected the same number of service from this function\n");
1609 HeapFree(GetProcessHeap(), 0, exservices);
1611 /* Store the number of returned services */
1612 tempreturned = returned;
1614 /* Allocate less than the needed bytes and don't specify a resume handle */
1615 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1616 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUSA);
1617 needed = 0xdeadbeef;
1618 returned = 0xdeadbeef;
1619 SetLastError(0xdeadbeef);
1620 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1621 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1622 ok(!ret, "Expected failure\n");
1623 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1624 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1625 ok(GetLastError() == ERROR_MORE_DATA,
1626 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1628 /* Allocate less than the needed bytes, this time with a correct resume handle */
1629 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUSA);
1630 needed = 0xdeadbeef;
1631 returned = 0xdeadbeef;
1632 resume = 0;
1633 SetLastError(0xdeadbeef);
1634 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1635 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1636 ok(!ret, "Expected failure\n");
1637 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1638 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1639 todo_wine ok(resume, "Expected a resume handle\n");
1640 ok(GetLastError() == ERROR_MORE_DATA,
1641 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1643 /* Fetch that last service but pass a bigger buffer size */
1644 missing = tempreturned - returned;
1645 bufsize = tempneeded;
1646 needed = 0xdeadbeef;
1647 returned = 0xdeadbeef;
1648 SetLastError(0xdeadbeef);
1649 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1650 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1651 ok(ret, "Expected success, got error %u\n", GetLastError());
1652 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1653 ok(returned == missing, "Expected %u services to be returned\n", missing);
1654 ok(resume == 0, "Expected the resume handle to be 0\n");
1655 HeapFree(GetProcessHeap(), 0, exservices);
1657 /* See if things add up */
1659 /* Get the number of active win32 services */
1660 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1661 NULL, 0, &needed, &returned, NULL, NULL);
1662 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1663 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1664 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1665 HeapFree(GetProcessHeap(), 0, exservices);
1667 servicecountactive = returned;
1669 /* Get the number of inactive win32 services */
1670 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1671 NULL, 0, &needed, &returned, NULL, NULL);
1672 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1673 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1674 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1675 HeapFree(GetProcessHeap(), 0, exservices);
1677 servicecountinactive = returned;
1679 /* Get the number of win32 services */
1680 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1681 NULL, 0, &needed, &returned, NULL, NULL);
1682 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1683 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1684 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1685 HeapFree(GetProcessHeap(), 0, exservices);
1687 /* Check if total is the same as active and inactive win32 services */
1688 ok(returned == (servicecountactive + servicecountinactive),
1689 "Something wrong in the calculation\n");
1691 /* Get all drivers and services */
1692 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1693 SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1694 ok(!ret, "Expected failure\n");
1695 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1696 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1697 SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1698 ok(ret, "Expected success %u\n", GetLastError());
1700 /* Loop through all those returned drivers and services */
1701 for (i = 0; i < returned; i++)
1703 SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1705 /* lpServiceName and lpDisplayName should always be filled */
1706 ok(exservices[i].lpServiceName[0], "Expected a service name\n");
1707 ok(exservices[i].lpDisplayName[0], "Expected a display name\n");
1709 /* Decrement the counters to see if the functions calls return the
1710 * same numbers as the contents of these structures.
1711 * Check some process id specifics.
1713 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1715 /* We shouldn't have a process id for drivers */
1716 ok(status.dwProcessId == 0,
1717 "This driver shouldn't have an associated process id\n");
1720 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1722 if (status.dwCurrentState != SERVICE_STOPPED)
1724 /* We expect a process id for every running service */
1725 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1726 exservices[i].lpServiceName);
1728 servicecountactive--;
1730 else
1732 /* We shouldn't have a process id for inactive services */
1733 ok(status.dwProcessId == 0, "Service %s state %u shouldn't have an associated process id\n",
1734 exservices[i].lpServiceName, status.dwCurrentState);
1736 servicecountinactive--;
1740 HeapFree(GetProcessHeap(), 0, exservices);
1742 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1743 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1745 CloseServiceHandle(scm_handle);
1748 static void test_close(void)
1750 SC_HANDLE handle;
1751 BOOL ret;
1753 /* NULL handle */
1754 SetLastError(0xdeadbeef);
1755 ret = CloseServiceHandle(NULL);
1756 ok(!ret, "Expected failure\n");
1757 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1759 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1761 /* Proper call */
1762 handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1763 SetLastError(0xdeadbeef);
1764 ret = CloseServiceHandle(handle);
1765 ok(ret, "Expected success got error %u\n", GetLastError());
1768 static void test_sequence(void)
1770 SC_HANDLE scm_handle, svc_handle;
1771 BOOL ret, is_nt4;
1772 QUERY_SERVICE_CONFIGA *config;
1773 DWORD given, needed;
1774 static const CHAR servicename [] = "Winetest";
1775 static const CHAR displayname [] = "Winetest dummy service";
1776 static const CHAR displayname2[] = "Winetest dummy service (2)";
1777 static const CHAR pathname [] = "we_dont_care.exe";
1778 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1779 static const CHAR password [] = "";
1780 static const CHAR empty [] = "";
1781 static const CHAR localsystem [] = "LocalSystem";
1783 SetLastError(0xdeadbeef);
1784 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1786 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1788 skip("Not enough rights to get a handle to the manager\n");
1789 return;
1791 else
1792 ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1794 if (!scm_handle) return;
1795 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
1796 is_nt4=(svc_handle == NULL && GetLastError() == ERROR_INVALID_PARAMETER);
1797 CloseServiceHandle(svc_handle);
1799 /* Create a dummy service */
1800 SetLastError(0xdeadbeef);
1801 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1802 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1803 pathname, NULL, NULL, dependencies, NULL, password);
1805 if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1807 /* We try and open the service and do the rest of the tests. Some could
1808 * fail if the tests were changed between these runs.
1810 trace("Deletion probably didn't work last time\n");
1811 SetLastError(0xdeadbeef);
1812 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1813 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1815 skip("Not enough rights to open the service\n");
1816 CloseServiceHandle(scm_handle);
1817 return;
1819 ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1821 else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1823 skip("Not enough rights to create the service\n");
1824 CloseServiceHandle(scm_handle);
1825 return;
1827 else
1829 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1830 if ((svc_handle != NULL) && (pGetSecurityInfo != NULL))
1832 PSID sidOwner, sidGroup;
1833 PACL dacl, sacl;
1834 PSECURITY_DESCRIPTOR pSD;
1835 DWORD error, n1, n2;
1836 HRESULT retval;
1837 BOOL bret;
1839 /* Test using GetSecurityInfo to obtain security information */
1840 retval = pGetSecurityInfo(svc_handle, SE_SERVICE, DACL_SECURITY_INFORMATION, &sidOwner,
1841 &sidGroup, &dacl, &sacl, &pSD);
1842 LocalFree(pSD);
1843 ok(retval == ERROR_SUCCESS, "Expected GetSecurityInfo to succeed: result %d\n", retval);
1844 retval = pGetSecurityInfo(svc_handle, SE_SERVICE, DACL_SECURITY_INFORMATION, NULL,
1845 NULL, NULL, NULL, &pSD);
1846 LocalFree(pSD);
1847 ok(retval == ERROR_SUCCESS, "Expected GetSecurityInfo to succeed: result %d\n", retval);
1848 if (!is_nt4)
1850 retval = pGetSecurityInfo(svc_handle, SE_SERVICE, DACL_SECURITY_INFORMATION, NULL,
1851 NULL, &dacl, NULL, &pSD);
1852 ok(retval == ERROR_SUCCESS, "Expected GetSecurityInfo to succeed: result %d\n", retval);
1853 LocalFree(pSD);
1854 SetLastError(0xdeadbeef);
1855 retval = pGetSecurityInfo(svc_handle, SE_SERVICE, DACL_SECURITY_INFORMATION, NULL,
1856 NULL, NULL, NULL, NULL);
1857 error = GetLastError();
1858 ok(retval == ERROR_INVALID_PARAMETER, "Expected GetSecurityInfo to fail: result %d\n", retval);
1859 ok(error == 0xdeadbeef, "Unexpected last error %d\n", error);
1861 else
1862 win_skip("A NULL security descriptor in GetSecurityInfo results in an exception on NT4.\n");
1864 /* Test using QueryServiceObjectSecurity to obtain security information */
1865 SetLastError(0xdeadbeef);
1866 bret = pQueryServiceObjectSecurity(svc_handle, DACL_SECURITY_INFORMATION, NULL, 0, &n1);
1867 error = GetLastError();
1868 ok(!bret, "Expected QueryServiceObjectSecurity to fail: result %d\n", bret);
1869 ok(error == ERROR_INSUFFICIENT_BUFFER ||
1870 broken(error == ERROR_INVALID_ADDRESS) || broken(error == ERROR_INVALID_PARAMETER),
1871 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", error);
1872 if (error != ERROR_INSUFFICIENT_BUFFER) n1 = 1024;
1873 pSD = LocalAlloc(0, n1);
1874 bret = pQueryServiceObjectSecurity(svc_handle, DACL_SECURITY_INFORMATION, pSD, n1, &n2);
1875 ok(bret, "Expected QueryServiceObjectSecurity to succeed: result %d\n", bret);
1876 LocalFree(pSD);
1880 if (!svc_handle) {
1881 CloseServiceHandle(scm_handle);
1882 return;
1885 /* TODO:
1886 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1887 * that the correct keys are used.
1890 /* Request the size for the buffer */
1891 SetLastError(0xdeadbeef);
1892 ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1893 ok(!ret, "Expected failure\n");
1894 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1896 config = HeapAlloc(GetProcessHeap(), 0, needed);
1897 given = needed;
1898 SetLastError(0xdeadbeef);
1899 ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1900 ok(ret, "Expected success, got error %u\n", GetLastError());
1902 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1903 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1904 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1905 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1906 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1907 ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1908 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1909 ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1910 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1911 /* TODO: Show the double 0 terminated string */
1912 todo_wine
1914 ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1916 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1917 ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1919 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2",
1920 NULL, NULL, NULL, NULL, displayname2);
1921 ok(ret, "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1923 QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1924 config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1925 ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1926 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1927 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1928 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1929 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1930 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1931 ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1932 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1933 ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1934 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1935 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1936 ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1938 SetLastError(0xdeadbeef);
1939 ret = DeleteService(svc_handle);
1940 ok(ret, "Expected success, got error %u\n", GetLastError());
1941 CloseServiceHandle(svc_handle);
1943 /* Wait a while. The following test does a CreateService again */
1944 Sleep(1000);
1946 CloseServiceHandle(scm_handle);
1947 HeapFree(GetProcessHeap(), 0, config);
1950 static void test_queryconfig2(void)
1952 SC_HANDLE scm_handle, svc_handle;
1953 BOOL ret;
1954 DWORD expected, needed;
1955 BYTE buffer[MAX_PATH];
1956 LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
1957 SERVICE_PRESHUTDOWN_INFO preshutdown_info;
1958 static const CHAR servicename [] = "Winetest";
1959 static const CHAR displayname [] = "Winetest dummy service";
1960 static const CHAR pathname [] = "we_dont_care.exe";
1961 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1962 static const CHAR password [] = "";
1963 static const CHAR description [] = "Description";
1965 if(!pQueryServiceConfig2A)
1967 win_skip("function QueryServiceConfig2A not present\n");
1968 return;
1971 SetLastError(0xdeadbeef);
1972 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1974 if (!scm_handle)
1976 if(GetLastError() == ERROR_ACCESS_DENIED)
1977 skip("Not enough rights to get a handle to the manager\n");
1978 else
1979 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
1980 return;
1983 /* Create a dummy service */
1984 SetLastError(0xdeadbeef);
1985 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1986 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1987 pathname, NULL, NULL, dependencies, NULL, password);
1989 if (!svc_handle)
1991 if(GetLastError() == ERROR_SERVICE_EXISTS)
1993 /* We try and open the service and do the rest of the tests. Some could
1994 * fail if the tests were changed between these runs.
1996 trace("Deletion probably didn't work last time\n");
1997 SetLastError(0xdeadbeef);
1998 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1999 if (!svc_handle)
2001 if(GetLastError() == ERROR_ACCESS_DENIED)
2002 skip("Not enough rights to open the service\n");
2003 else
2004 ok(FALSE, "Could not open the service : %d\n", GetLastError());
2005 CloseServiceHandle(scm_handle);
2006 return;
2009 if (GetLastError() == ERROR_ACCESS_DENIED)
2011 skip("Not enough rights to create the service\n");
2012 CloseServiceHandle(scm_handle);
2013 return;
2015 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
2016 if (!svc_handle)
2018 CloseServiceHandle(scm_handle);
2019 return;
2022 SetLastError(0xdeadbeef);
2023 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2024 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2025 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
2027 SetLastError(0xdeadbeef);
2028 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
2029 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2030 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
2032 SetLastError(0xdeadbeef);
2033 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
2034 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2035 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
2037 SetLastError(0xdeadbeef);
2038 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
2039 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2040 ok((ERROR_INVALID_ADDRESS == GetLastError()) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()),
2041 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2043 SetLastError(0xdeadbeef);
2044 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),NULL);
2045 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2046 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
2048 needed = 0;
2049 SetLastError(0xdeadbeef);
2050 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA)-1,&needed);
2051 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2052 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2053 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
2055 needed = 0;
2056 pConfig->lpDescription = (LPSTR)0xdeadbeef;
2057 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2058 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2059 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
2060 ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
2062 SetLastError(0xdeadbeef);
2063 needed = 0;
2064 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
2065 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2066 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2067 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
2069 if(!pChangeServiceConfig2A)
2071 win_skip("function ChangeServiceConfig2A not present\n");
2072 goto cleanup;
2075 pConfig->lpDescription = (LPSTR) description;
2076 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
2077 ok(ret, "ChangeServiceConfig2A failed\n");
2078 if (!ret) {
2079 goto cleanup;
2082 SetLastError(0xdeadbeef);
2083 needed = 0;
2084 expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
2085 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2086 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2087 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2088 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2090 SetLastError(0xdeadbeef);
2091 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
2092 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2093 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2095 SetLastError(0xdeadbeef);
2096 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
2097 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2098 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2099 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2101 SetLastError(0xdeadbeef);
2102 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
2103 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2104 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2105 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2107 if(!pQueryServiceConfig2W)
2109 win_skip("function QueryServiceConfig2W not present\n");
2110 goto cleanup;
2112 SetLastError(0xdeadbeef);
2113 needed = 0;
2114 expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
2115 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
2116 ok(!ret, "expected QueryServiceConfig2W to fail\n");
2117 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2118 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2120 SetLastError(0xdeadbeef);
2121 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
2122 ok(ret, "expected QueryServiceConfig2W to succeed\n");
2124 SetLastError(0xdeadbeef);
2125 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_PRESHUTDOWN_INFO,
2126 (LPBYTE)&preshutdown_info, sizeof(preshutdown_info), &needed);
2127 if(!ret && GetLastError()==ERROR_INVALID_LEVEL)
2129 /* Win2k3 and older */
2130 win_skip("SERVICE_CONFIG_PRESHUTDOWN_INFO not supported\n");
2131 goto cleanup;
2133 ok(ret, "expected QueryServiceConfig2W to succeed (%d)\n", GetLastError());
2134 ok(needed == sizeof(preshutdown_info), "needed = %d\n", needed);
2135 ok(preshutdown_info.dwPreshutdownTimeout == 180000, "Default PreshutdownTimeout = %d\n",
2136 preshutdown_info.dwPreshutdownTimeout);
2138 SetLastError(0xdeadbeef);
2139 preshutdown_info.dwPreshutdownTimeout = -1;
2140 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_PRESHUTDOWN_INFO,
2141 (LPVOID)&preshutdown_info);
2142 ok(ret, "expected ChangeServiceConfig2A to succeed (%d)\n", GetLastError());
2144 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_PRESHUTDOWN_INFO,
2145 (LPBYTE)&preshutdown_info, sizeof(preshutdown_info), &needed);
2146 ok(ret, "expected QueryServiceConfig2W to succeed (%d)\n", GetLastError());
2147 ok(needed == sizeof(preshutdown_info), "needed = %d\n", needed);
2148 ok(preshutdown_info.dwPreshutdownTimeout == -1, "New PreshutdownTimeout = %d\n",
2149 preshutdown_info.dwPreshutdownTimeout);
2151 cleanup:
2152 DeleteService(svc_handle);
2154 CloseServiceHandle(svc_handle);
2156 /* Wait a while. The following test does a CreateService again */
2157 Sleep(1000);
2159 CloseServiceHandle(scm_handle);
2162 static DWORD try_start_stop(SC_HANDLE svc_handle, const char* name, DWORD is_nt4)
2164 BOOL ret;
2165 DWORD le1, le2;
2166 SERVICE_STATUS status;
2168 ret = StartServiceA(svc_handle, 0, NULL);
2169 le1 = GetLastError();
2170 ok(!ret, "%s: StartServiceA() should have failed\n", name);
2172 if (pQueryServiceStatusEx)
2174 DWORD needed;
2175 SERVICE_STATUS_PROCESS statusproc;
2177 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)&statusproc, sizeof(statusproc), &needed);
2178 ok(ret, "%s: QueryServiceStatusEx() failed le=%u\n", name, GetLastError());
2179 ok(statusproc.dwCurrentState == SERVICE_STOPPED, "%s: should be stopped state=%x\n", name, statusproc.dwCurrentState);
2180 ok(statusproc.dwProcessId == 0, "%s: ProcessId should be 0 instead of %x\n", name, statusproc.dwProcessId);
2183 ret = StartServiceA(svc_handle, 0, NULL);
2184 le2 = GetLastError();
2185 ok(!ret, "%s: StartServiceA() should have failed\n", name);
2186 ok(le2 == le1, "%s: the second try should yield the same error: %u != %u\n", name, le1, le2);
2188 status.dwCurrentState = 0xdeadbeef;
2189 ret = ControlService(svc_handle, SERVICE_CONTROL_STOP, &status);
2190 le2 = GetLastError();
2191 ok(!ret, "%s: ControlService() should have failed\n", name);
2192 ok(le2 == ERROR_SERVICE_NOT_ACTIVE, "%s: %d != ERROR_SERVICE_NOT_ACTIVE\n", name, le2);
2193 ok(status.dwCurrentState == SERVICE_STOPPED ||
2194 broken(is_nt4), /* NT4 returns a random value */
2195 "%s: should be stopped state=%x\n", name, status.dwCurrentState);
2197 return le1;
2200 struct notify_data {
2201 SERVICE_NOTIFYW notify;
2202 SC_HANDLE svc;
2205 static void CALLBACK cb_stopped(void *user)
2207 struct notify_data *data = user;
2208 BOOL br;
2210 ok(data->notify.dwNotificationStatus == ERROR_SUCCESS,
2211 "Got wrong notification status: %u\n", data->notify.dwNotificationStatus);
2212 ok(data->notify.ServiceStatus.dwCurrentState == SERVICE_STOPPED,
2213 "Got wrong service state: 0x%x\n", data->notify.ServiceStatus.dwCurrentState);
2214 ok(data->notify.dwNotificationTriggered == SERVICE_NOTIFY_STOPPED,
2215 "Got wrong notification triggered: 0x%x\n", data->notify.dwNotificationTriggered);
2217 br = StartServiceA(data->svc, 0, NULL);
2218 ok(br, "StartService failed: %u\n", GetLastError());
2221 static void CALLBACK cb_running(void *user)
2223 struct notify_data *data = user;
2224 BOOL br;
2225 SERVICE_STATUS status;
2227 ok(data->notify.dwNotificationStatus == ERROR_SUCCESS,
2228 "Got wrong notification status: %u\n", data->notify.dwNotificationStatus);
2229 ok(data->notify.ServiceStatus.dwCurrentState == SERVICE_RUNNING,
2230 "Got wrong service state: 0x%x\n", data->notify.ServiceStatus.dwCurrentState);
2231 ok(data->notify.dwNotificationTriggered == SERVICE_NOTIFY_RUNNING,
2232 "Got wrong notification triggered: 0x%x\n", data->notify.dwNotificationTriggered);
2234 br = ControlService(data->svc, SERVICE_CONTROL_STOP, &status);
2235 ok(br, "ControlService failed: %u\n", GetLastError());
2238 static void test_servicenotify(SC_HANDLE svc)
2240 DWORD dr;
2241 struct notify_data data;
2243 if(!pNotifyServiceStatusChangeW){
2244 win_skip("No NotifyServiceStatusChangeW\n");
2245 return;
2248 memset(&data.notify, 0, sizeof(data.notify));
2249 data.notify.dwVersion = SERVICE_NOTIFY_STATUS_CHANGE;
2250 data.notify.pfnNotifyCallback = &cb_stopped;
2251 data.notify.pContext = &data;
2252 data.svc = svc;
2254 dr = pNotifyServiceStatusChangeW(svc, SERVICE_NOTIFY_STOPPED | SERVICE_NOTIFY_RUNNING, &data.notify);
2255 ok(dr == ERROR_SUCCESS, "NotifyServiceStatusChangeW failed: %u\n", dr);
2257 dr = SleepEx(100, TRUE);
2258 ok(dr == WAIT_IO_COMPLETION, "APC wasn't called\n");
2260 data.notify.pfnNotifyCallback = &cb_running;
2262 dr = pNotifyServiceStatusChangeW(svc, SERVICE_NOTIFY_STOPPED | SERVICE_NOTIFY_RUNNING, &data.notify);
2263 ok(dr == ERROR_SUCCESS, "NotifyServiceStatusChangeW failed: %u\n", dr);
2265 dr = SleepEx(100, TRUE);
2266 ok(dr == WAIT_IO_COMPLETION, "APC wasn't called\n");
2269 static void test_start_stop(void)
2271 BOOL ret;
2272 SC_HANDLE scm_handle, svc_handle;
2273 DWORD le, is_nt4;
2274 static const char servicename[] = "Winetest";
2275 char cmd[MAX_PATH+20];
2276 const char* displayname;
2278 SetLastError(0xdeadbeef);
2279 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2280 if (!scm_handle)
2282 if(GetLastError() == ERROR_ACCESS_DENIED)
2283 skip("Not enough rights to get a handle to the manager\n");
2284 else
2285 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
2286 return;
2289 /* Detect NT4 */
2290 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
2291 is_nt4=(svc_handle == NULL && GetLastError() == ERROR_INVALID_PARAMETER);
2293 /* Do some cleanup in case a previous run crashed */
2294 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2295 if (svc_handle)
2297 DeleteService(svc_handle);
2298 CloseServiceHandle(svc_handle);
2301 /* Create a dummy disabled service */
2302 sprintf(cmd, "\"%s\" service exit", selfname);
2303 displayname = "Winetest Disabled Service";
2304 svc_handle = CreateServiceA(scm_handle, servicename, displayname,
2305 GENERIC_ALL, SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS,
2306 SERVICE_DISABLED, SERVICE_ERROR_IGNORE, cmd, NULL,
2307 NULL, NULL, NULL, NULL);
2308 if (!svc_handle)
2310 if(GetLastError() == ERROR_ACCESS_DENIED)
2311 skip("Not enough rights to create the service\n");
2312 else
2313 ok(FALSE, "Could not create the service: %d\n", GetLastError());
2314 goto cleanup;
2316 le = try_start_stop(svc_handle, displayname, is_nt4);
2317 ok(le == ERROR_SERVICE_DISABLED, "%d != ERROR_SERVICE_DISABLED\n", le);
2319 /* Then one with a bad path */
2320 displayname = "Winetest Bad Path";
2321 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, "c:\\no_such_file.exe", NULL, NULL, NULL, NULL, NULL, displayname);
2322 ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2323 try_start_stop(svc_handle, displayname, is_nt4);
2325 if (is_nt4)
2327 /* NT4 does not detect when a service fails to start and uses an
2328 * insanely long timeout: 120s. So skip the rest of the tests.
2330 win_skip("Skip some service start/stop tests on NT4\n");
2331 goto cleanup;
2334 /* Again with a process that exits right away */
2335 displayname = "Winetest Exit Service";
2336 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, cmd, NULL, NULL, NULL, NULL, NULL, displayname);
2337 ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2338 le = try_start_stop(svc_handle, displayname, is_nt4);
2339 ok(le == ERROR_SERVICE_REQUEST_TIMEOUT, "%d != ERROR_SERVICE_REQUEST_TIMEOUT\n", le);
2341 /* create a real service and test notifications */
2342 sprintf(cmd, "%s service serve", selfname);
2343 displayname = "Winetest Service";
2344 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, cmd, NULL, NULL, NULL, NULL, NULL, displayname);
2345 ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2346 test_servicenotify(svc_handle);
2348 cleanup:
2349 if (svc_handle)
2351 DeleteService(svc_handle);
2352 CloseServiceHandle(svc_handle);
2355 /* Wait a while. The following test does a CreateService again */
2356 Sleep(1000);
2358 CloseServiceHandle(scm_handle);
2361 static void test_refcount(void)
2363 SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
2364 static const CHAR servicename [] = "Winetest";
2365 static const CHAR pathname [] = "we_dont_care.exe";
2366 BOOL ret;
2368 /* Get a handle to the Service Control Manager */
2369 SetLastError(0xdeadbeef);
2370 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2371 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
2373 skip("Not enough rights to get a handle to the manager\n");
2374 return;
2377 /* Create a service */
2378 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2379 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2380 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2381 ok(svc_handle1 != NULL, "Expected success, got error %u\n", GetLastError());
2383 /* Get a handle to this new service */
2384 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2385 ok(svc_handle2 != NULL, "Expected success, got error %u\n", GetLastError());
2387 /* Get another handle to this new service */
2388 svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2389 ok(svc_handle3 != NULL, "Expected success, got error %u\n", GetLastError());
2391 /* Check if we can close the handle to the Service Control Manager */
2392 ret = CloseServiceHandle(scm_handle);
2393 ok(ret, "Expected success (err=%d)\n", GetLastError());
2395 /* Get a new handle to the Service Control Manager */
2396 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2397 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
2399 /* Get a handle to this new service */
2400 svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2401 ok(svc_handle4 != NULL, "Expected success, got error %u\n", GetLastError());
2403 /* Delete the service */
2404 ret = DeleteService(svc_handle4);
2405 ok(ret, "Expected success (err=%d)\n", GetLastError());
2407 /* We cannot create the same service again as it's still marked as 'being deleted'.
2408 * The reason is that we still have 4 open handles to this service even though we
2409 * closed the handle to the Service Control Manager in between.
2411 SetLastError(0xdeadbeef);
2412 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2413 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2414 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2415 ok(!svc_handle5, "Expected failure\n");
2416 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
2417 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2419 /* Close all the handles to the service and try again */
2420 ret = CloseServiceHandle(svc_handle4);
2421 ok(ret, "Expected success (err=%d)\n", GetLastError());
2422 ret = CloseServiceHandle(svc_handle3);
2423 ok(ret, "Expected success (err=%d)\n", GetLastError());
2424 ret = CloseServiceHandle(svc_handle2);
2425 ok(ret, "Expected success (err=%d)\n", GetLastError());
2426 ret = CloseServiceHandle(svc_handle1);
2427 ok(ret, "Expected success (err=%d)\n", GetLastError());
2429 /* Wait a while. Doing a CreateService too soon will result again
2430 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2432 Sleep(1000);
2434 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2435 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2436 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2437 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2438 ok(svc_handle5 != NULL, "Expected success, got error %u\n", GetLastError());
2440 /* Delete the service */
2441 ret = DeleteService(svc_handle5);
2442 ok(ret, "Expected success (err=%d)\n", GetLastError());
2444 /* Wait a while. Just in case one of the following tests does a CreateService again */
2445 Sleep(1000);
2447 CloseServiceHandle(svc_handle5);
2448 CloseServiceHandle(scm_handle);
2451 static DWORD WINAPI ctrl_handler(DWORD ctl, DWORD type, void *data, void *user)
2453 HANDLE evt = user;
2455 switch(ctl){
2456 case SERVICE_CONTROL_STOP:
2457 SetEvent(evt);
2458 break;
2459 case SERVICE_CONTROL_INTERROGATE:
2460 return NO_ERROR;
2463 return ERROR_CALL_NOT_IMPLEMENTED;
2466 static void WINAPI service_main(DWORD argc, char **argv)
2468 SERVICE_STATUS_HANDLE st_handle;
2469 SERVICE_STATUS st;
2470 HANDLE evt = CreateEventW(0, FALSE, FALSE, 0);
2472 st_handle = RegisterServiceCtrlHandlerExA("", &ctrl_handler, evt);
2474 st.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
2475 st.dwServiceSpecificExitCode = 0;
2476 st.dwCurrentState = SERVICE_RUNNING;
2477 st.dwWin32ExitCode = NO_ERROR;
2478 st.dwWaitHint = 0;
2479 st.dwControlsAccepted = SERVICE_ACCEPT_STOP;
2480 st.dwCheckPoint = 0;
2482 SetServiceStatus(st_handle, &st);
2484 WaitForSingleObject(evt, 5000);
2486 st.dwCurrentState = SERVICE_STOPPED;
2488 SetServiceStatus(st_handle, &st);
2491 static void run_service(void)
2493 char empty[] = {0};
2494 SERVICE_TABLE_ENTRYA table[] = {
2495 {empty, &service_main },
2496 {0, 0}
2499 StartServiceCtrlDispatcherA(table);
2502 START_TEST(service)
2504 SC_HANDLE scm_handle;
2505 int myARGC;
2506 char** myARGV;
2508 myARGC = winetest_get_mainargs(&myARGV);
2509 GetFullPathNameA(myARGV[0], sizeof(selfname), selfname, NULL);
2510 if (myARGC >= 3)
2512 if (strcmp(myARGV[2], "serve") == 0)
2513 run_service();
2514 return;
2517 /* Bail out if we are on win98 */
2518 SetLastError(0xdeadbeef);
2519 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2521 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2523 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2524 return;
2526 CloseServiceHandle(scm_handle);
2528 init_function_pointers();
2530 /* First some parameter checking */
2531 test_open_scm();
2532 test_open_svc();
2533 test_create_delete_svc();
2534 test_get_displayname();
2535 test_get_servicekeyname();
2536 test_query_svc();
2537 test_enum_svc();
2538 test_close();
2539 /* Test the creation, querying and deletion of a service */
2540 test_sequence();
2541 test_queryconfig2();
2542 test_start_stop();
2543 /* The main reason for this test is to check if any refcounting is used
2544 * and what the rules are
2546 test_refcount();