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
32 #include "wine/test.h"
34 static const CHAR spooler[] = "Spooler"; /* Should be available on all platforms */
36 static BOOL (WINAPI *pChangeServiceConfig2A)(SC_HANDLE,DWORD,LPVOID);
37 static BOOL (WINAPI *pEnumServicesStatusExA)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
38 DWORD, LPBYTE, DWORD, LPDWORD,
39 LPDWORD, LPDWORD, LPCSTR);
40 static BOOL (WINAPI *pQueryServiceConfig2A)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
41 static BOOL (WINAPI *pQueryServiceConfig2W)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
42 static BOOL (WINAPI *pQueryServiceStatusEx)(SC_HANDLE, SC_STATUS_TYPE, LPBYTE,
45 static void init_function_pointers(void)
47 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
49 pChangeServiceConfig2A = (void*)GetProcAddress(hadvapi32, "ChangeServiceConfig2A");
50 pEnumServicesStatusExA= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExA");
51 pQueryServiceConfig2A= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2A");
52 pQueryServiceConfig2W= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2W");
53 pQueryServiceStatusEx= (void*)GetProcAddress(hadvapi32, "QueryServiceStatusEx");
56 static void test_open_scm(void)
60 /* No access rights */
61 SetLastError(0xdeadbeef);
62 scm_handle = OpenSCManagerA(NULL, NULL, 0);
63 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
64 CloseServiceHandle(scm_handle);
66 /* Unknown database name */
67 SetLastError(0xdeadbeef);
68 scm_handle = OpenSCManagerA(NULL, "DoesNotExist", SC_MANAGER_CONNECT);
69 ok(!scm_handle, "Expected failure\n");
70 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
71 CloseServiceHandle(scm_handle); /* Just in case */
73 /* MSDN says only ServiceActive is allowed, or NULL */
74 SetLastError(0xdeadbeef);
75 scm_handle = OpenSCManagerA(NULL, SERVICES_FAILED_DATABASEA, SC_MANAGER_CONNECT);
76 ok(!scm_handle, "Expected failure\n");
77 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
78 CloseServiceHandle(scm_handle); /* Just in case */
80 /* Remote unknown host */
81 SetLastError(0xdeadbeef);
82 scm_handle = OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
85 ok(!scm_handle, "Expected failure\n");
86 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* w2k8 */,
87 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
89 CloseServiceHandle(scm_handle); /* Just in case */
91 /* Proper call with an empty hostname */
92 SetLastError(0xdeadbeef);
93 scm_handle = OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
94 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
95 CloseServiceHandle(scm_handle);
97 /* Again a correct one */
98 SetLastError(0xdeadbeef);
99 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
100 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
101 CloseServiceHandle(scm_handle);
104 static void test_open_svc(void)
106 SC_HANDLE scm_handle, svc_handle;
107 CHAR displayname[4096];
110 /* All NULL (invalid access rights) */
111 SetLastError(0xdeadbeef);
112 svc_handle = OpenServiceA(NULL, NULL, 0);
113 ok(!svc_handle, "Expected failure\n");
114 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
116 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
119 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
120 SetLastError(0xdeadbeef);
121 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
122 ok(!svc_handle, "Expected failure\n");
123 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
124 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
125 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
127 /* Nonexistent service */
128 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
129 SetLastError(0xdeadbeef);
130 svc_handle = OpenServiceA(scm_handle, "deadbeef", GENERIC_READ);
131 ok(!svc_handle, "Expected failure\n");
132 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
133 CloseServiceHandle(scm_handle);
135 /* Proper SCM handle but different access rights */
136 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
137 SetLastError(0xdeadbeef);
138 svc_handle = OpenServiceA(scm_handle, "Spooler", GENERIC_WRITE);
139 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
140 skip("Not enough rights to get a handle to the service\n");
143 ok(svc_handle != NULL, "Expected success, got error %u\n", GetLastError());
144 CloseServiceHandle(svc_handle);
147 /* Test to show we can't open a service with the displayname */
149 /* Retrieve the needed size for the buffer */
151 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
152 /* Get the displayname */
153 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
154 /* Try to open the service with this displayname, unless the displayname equals
155 * the servicename as that would defeat the purpose of this test.
157 if (!lstrcmpi(spooler, displayname))
159 skip("displayname equals servicename\n");
160 CloseServiceHandle(scm_handle);
164 SetLastError(0xdeadbeef);
165 svc_handle = OpenServiceA(scm_handle, displayname, GENERIC_READ);
166 ok(!svc_handle, "Expected failure\n");
167 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
169 CloseServiceHandle(svc_handle);
171 CloseServiceHandle(scm_handle);
174 static void test_create_delete_svc(void)
176 SC_HANDLE scm_handle, svc_handle1;
177 CHAR username[UNLEN + 1], domain[MAX_PATH];
178 DWORD user_size = UNLEN + 1;
179 CHAR account[UNLEN + 3];
180 static const CHAR servicename [] = "Winetest";
181 static const CHAR pathname [] = "we_dont_care.exe";
182 static const CHAR empty [] = "";
183 static const CHAR password [] = "secret";
184 BOOL spooler_exists = FALSE;
187 DWORD display_size = sizeof(display);
189 /* Get the username and turn it into an account to be used in some tests */
190 GetUserNameA(username, &user_size);
191 /* Get the domainname to cater for that situation */
192 if (GetEnvironmentVariableA("USERDOMAIN", domain, MAX_PATH))
193 sprintf(account, "%s\\%s", domain, username);
195 sprintf(account, ".\\%s", username);
198 SetLastError(0xdeadbeef);
199 svc_handle1 = CreateServiceA(NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
200 ok(!svc_handle1, "Expected failure\n");
201 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
203 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
205 /* Only a valid handle to the Service Control Manager */
206 SetLastError(0xdeadbeef);
207 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
208 ok(!svc_handle1, "Expected failure\n");
209 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
210 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
211 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
213 /* Now with a servicename */
214 SetLastError(0xdeadbeef);
215 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
216 ok(!svc_handle1, "Expected failure\n");
217 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
218 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
219 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
221 /* Or just a binary name */
222 SetLastError(0xdeadbeef);
223 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, pathname, 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 /* Both servicename and binary name (We only have connect rights) */
230 SetLastError(0xdeadbeef);
231 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
232 ok(!svc_handle1, "Expected failure\n");
233 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
235 /* They can even be empty at this stage of parameter checking */
236 SetLastError(0xdeadbeef);
237 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
238 ok(!svc_handle1, "Expected failure\n");
239 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
241 SetLastError(0xdeadbeef);
242 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
243 ok(!svc_handle1, "Expected failure\n");
244 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
246 /* Open the Service Control Manager with minimal rights for creation
247 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
249 CloseServiceHandle(scm_handle);
250 SetLastError(0xdeadbeef);
251 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
252 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
254 skip("Not enough rights to get a handle to the manager\n");
258 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
260 /* Empty strings for servicename and binary name are checked */
261 SetLastError(0xdeadbeef);
262 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
263 ok(!svc_handle1, "Expected failure\n");
264 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
266 SetLastError(0xdeadbeef);
267 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
268 ok(!svc_handle1, "Expected failure\n");
269 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
271 SetLastError(0xdeadbeef);
272 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
273 ok(!svc_handle1, "Expected failure\n");
274 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
276 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
277 * an ERROR_INVALID_PARAMETER)
279 SetLastError(0xdeadbeef);
280 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
281 SERVICE_DISABLED, 0, empty, NULL, NULL, NULL, NULL, NULL);
282 ok(!svc_handle1, "Expected failure\n");
283 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
285 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
287 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
288 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
290 SetLastError(0xdeadbeef);
291 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS,
292 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
293 ok(!svc_handle1, "Expected failure\n");
294 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
296 /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
297 SetLastError(0xdeadbeef);
298 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_FILE_SYSTEM_DRIVER | SERVICE_INTERACTIVE_PROCESS,
299 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
300 ok(!svc_handle1, "Expected failure\n");
301 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
303 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
304 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
306 SetLastError(0xdeadbeef);
307 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
308 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, account, password);
309 ok(!svc_handle1, "Expected failure\n");
310 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
312 /* Illegal (start-type is not a mask and should only be one of the possibilities)
313 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
314 * it's most likely not the wanted start-type)
316 SetLastError(0xdeadbeef);
317 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS,
318 SERVICE_AUTO_START | SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
319 ok(!svc_handle1, "Expected failure\n");
320 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
322 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
323 SetLastError(0xdeadbeef);
324 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
325 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
326 ok(!svc_handle1, "Expected failure\n");
327 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
329 /* The service already exists (check first, just in case) */
330 svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
333 spooler_exists = TRUE;
334 CloseServiceHandle(svc_handle1);
335 SetLastError(0xdeadbeef);
336 svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
337 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
338 ok(!svc_handle1, "Expected failure\n");
339 ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
342 skip("Spooler service doesn't exist\n");
344 /* To find an existing displayname we check the 'Spooler' service. Although the registry
345 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
346 * to the servicename and can't be used as well for a new displayname.
350 ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
353 skip("Could not retrieve a displayname for the Spooler service\n");
356 svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
357 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
358 ok(!svc_handle1, "Expected failure\n");
359 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
360 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
364 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
366 /* Windows doesn't care about the access rights for creation (which makes
367 * sense as there is no service yet) as long as there are sufficient
368 * rights to the manager.
370 SetLastError(0xdeadbeef);
371 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
372 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
373 ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
375 /* DeleteService however must have proper rights */
376 SetLastError(0xdeadbeef);
377 ret = DeleteService(svc_handle1);
378 ok(!ret, "Expected failure\n");
379 ok(GetLastError() == ERROR_ACCESS_DENIED,
380 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
382 /* Open the service with minimal rights for deletion.
383 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
385 CloseServiceHandle(svc_handle1);
386 svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
388 /* Now that we have the proper rights, we should be able to delete */
389 SetLastError(0xdeadbeef);
390 ret = DeleteService(svc_handle1);
391 ok(ret, "Expected success, got error %u\n", GetLastError());
393 CloseServiceHandle(svc_handle1);
394 CloseServiceHandle(scm_handle);
396 /* Wait a while. One of the following tests also does a CreateService for the
397 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
398 * error if we do this to quick. Vista seems more picky then the others.
402 /* And a final NULL check */
403 SetLastError(0xdeadbeef);
404 ret = DeleteService(NULL);
405 ok(!ret, "Expected failure\n");
406 ok(GetLastError() == ERROR_INVALID_HANDLE,
407 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
410 static void test_get_displayname(void)
412 SC_HANDLE scm_handle, svc_handle;
414 CHAR displayname[4096];
415 WCHAR displaynameW[2048];
416 DWORD displaysize, tempsize, tempsizeW;
417 static const CHAR deadbeef[] = "Deadbeef";
418 static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
419 static const CHAR servicename[] = "Winetest";
420 static const CHAR pathname[] = "we_dont_care.exe";
422 /* Having NULL for the size of the buffer will crash on W2K3 */
424 SetLastError(0xdeadbeef);
425 ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
426 ok(!ret, "Expected failure\n");
427 ok(GetLastError() == ERROR_INVALID_HANDLE,
428 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
430 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
432 SetLastError(0xdeadbeef);
433 ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
434 ok(!ret, "Expected failure\n");
435 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
436 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
437 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
439 SetLastError(0xdeadbeef);
440 displaysize = sizeof(displayname);
441 ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
442 ok(!ret, "Expected failure\n");
443 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
444 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
445 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
447 /* Test for nonexistent service */
448 SetLastError(0xdeadbeef);
450 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
451 ok(!ret, "Expected failure\n");
452 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
453 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
455 /* Check if 'Spooler' exists */
456 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
459 skip("Spooler service doesn't exist\n");
460 CloseServiceHandle(scm_handle);
463 CloseServiceHandle(svc_handle);
465 /* Retrieve the needed size for the buffer */
466 SetLastError(0xdeadbeef);
468 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
469 ok(!ret, "Expected failure\n");
470 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
471 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
472 tempsize = displaysize;
475 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
476 ok(!ret, "Expected failure\n");
477 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
478 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
479 ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
481 /* Buffer is too small */
482 SetLastError(0xdeadbeef);
483 displaysize = (tempsize / 2);
484 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
485 ok(!ret, "Expected failure\n");
486 ok(displaysize == tempsize, "Expected the needed buffersize\n");
487 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
488 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
490 /* First try with a buffer that should be big enough to hold
491 * the ANSI string (and terminating character). This succeeds on Windows
492 * although when asked (see above 2 tests) it will return twice the needed size.
494 SetLastError(0xdeadbeef);
495 displaysize = (tempsize / 2) + 1;
496 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
497 ok(ret, "Expected success, got error %u\n", GetLastError());
498 ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
500 /* Now with the original returned size */
501 SetLastError(0xdeadbeef);
502 displaysize = tempsize;
503 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
504 ok(ret, "Expected success, got error %u\n", GetLastError());
505 ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
507 /* And with a bigger than needed buffer */
508 SetLastError(0xdeadbeef);
509 displaysize = tempsize * 2;
510 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
511 ok(ret, "Expected success, got error %u\n", GetLastError());
512 /* Test that shows that if the buffersize is enough, it's not changed */
513 ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
514 ok(lstrlen(displayname) == tempsize/2,
515 "Expected the buffer to be twice the length of the string\n") ;
517 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
518 SetLastError(0xdeadbeef);
520 ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
521 ok(!ret, "Expected failure\n");
522 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
523 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
525 /* Buffer is too small */
526 SetLastError(0xdeadbeef);
527 tempsizeW = displaysize;
528 displaysize = tempsizeW / 2;
529 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
530 ok(!ret, "Expected failure\n");
531 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
532 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
533 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
535 /* Now with the original returned size */
536 SetLastError(0xdeadbeef);
537 displaysize = tempsizeW;
538 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
539 ok(!ret, "Expected failure\n");
540 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
541 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
542 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
544 /* And with a bigger than needed buffer */
545 SetLastError(0xdeadbeef);
546 displaysize = tempsizeW + 1; /* This caters for the null terminating character */
547 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
548 ok(ret, "Expected success, got error %u\n", GetLastError());
549 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
550 ok(lstrlenW(displaynameW) == displaysize,
551 "Expected the buffer to be the length of the string\n") ;
552 ok(tempsize / 2 == tempsizeW,
553 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
555 CloseServiceHandle(scm_handle);
557 /* Test for a service without a displayname (which is valid). This should return
558 * the servicename itself.
560 SetLastError(0xdeadbeef);
561 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
562 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
564 skip("Not enough rights to get a handle to the manager\n");
568 SetLastError(0xdeadbeef);
569 svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
570 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
571 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
572 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
575 CloseServiceHandle(scm_handle);
579 /* Retrieve the needed size for the buffer */
580 SetLastError(0xdeadbeef);
582 ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
583 ok(!ret, "Expected failure\n");
584 ok(displaysize == lstrlen(servicename) * 2,
585 "Expected the displaysize to be twice the size of the servicename\n");
586 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
587 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
589 /* Buffer is too small */
590 SetLastError(0xdeadbeef);
591 tempsize = displaysize;
592 displaysize = (tempsize / 2);
593 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
594 ok(!ret, "Expected failure\n");
595 ok(displaysize == tempsize, "Expected the needed buffersize\n");
596 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
597 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
599 /* Get the displayname */
600 SetLastError(0xdeadbeef);
601 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
602 ok(ret, "Expected success, got error %u\n", GetLastError());
603 ok(!lstrcmpi(displayname, servicename),
604 "Expected displayname to be %s, got %s\n", servicename, displayname);
606 /* Delete the service */
607 ret = DeleteService(svc_handle);
608 ok(ret, "Expected success (err=%d)\n", GetLastError());
610 CloseServiceHandle(svc_handle);
611 CloseServiceHandle(scm_handle);
613 /* Wait a while. Just in case one of the following tests does a CreateService again */
617 static void test_get_servicekeyname(void)
619 SC_HANDLE scm_handle, svc_handle;
620 CHAR servicename[4096];
621 CHAR displayname[4096];
622 WCHAR servicenameW[4096];
623 WCHAR displaynameW[4096];
624 DWORD servicesize, displaysize, tempsize;
626 static const CHAR deadbeef[] = "Deadbeef";
627 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
629 /* Having NULL for the size of the buffer will crash on W2K3 */
631 SetLastError(0xdeadbeef);
632 ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
633 ok(!ret, "Expected failure\n");
634 ok(GetLastError() == ERROR_INVALID_HANDLE,
635 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
637 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
640 SetLastError(0xdeadbeef);
641 ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
642 ok(!ret, "Expected failure\n");
643 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
644 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
645 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
646 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
648 /* Valid handle and buffer but no displayname */
650 SetLastError(0xdeadbeef);
651 ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
652 ok(!ret, "Expected failure\n");
653 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
654 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
655 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
656 todo_wine ok(servicesize == 200, "Service size expected 1, got %d\n", servicesize);
658 /* Test for nonexistent displayname */
659 SetLastError(0xdeadbeef);
660 ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
661 ok(!ret, "Expected failure\n");
662 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
663 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
664 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
667 strcpy(servicename, "ABC");
668 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
669 ok(!ret, "Expected failure\n");
670 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
671 ok(servicename[0] == 0, "Service name not empty\n");
674 servicenameW[0] = 'A';
675 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
676 ok(!ret, "Expected failure\n");
677 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
678 ok(servicenameW[0] == 0, "Service name not empty\n");
681 strcpy(servicename, "ABC");
682 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
683 ok(!ret, "Expected failure\n");
684 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
685 ok(servicename[0] == 'A', "Service name changed\n");
688 servicenameW[0] = 'A';
689 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
690 ok(!ret, "Expected failure\n");
691 todo_wine ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
692 ok(servicenameW[0] == 'A', "Service name changed\n");
694 /* Check if 'Spooler' exists */
695 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
698 skip("Spooler service doesn't exist\n");
699 CloseServiceHandle(scm_handle);
702 CloseServiceHandle(svc_handle);
704 /* Get the displayname for the 'Spooler' service */
705 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
706 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
708 /* Retrieve the needed size for the buffer */
709 SetLastError(0xdeadbeef);
711 ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
712 ok(!ret, "Expected failure\n");
713 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
714 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
716 /* Valid call with the correct buffersize */
717 SetLastError(0xdeadbeef);
718 tempsize = servicesize;
720 ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
721 ok(ret, "Expected success, got error %u\n", GetLastError());
724 ok(lstrlen(servicename) == tempsize/2,
725 "Expected the buffer to be twice the length of the string\n") ;
726 ok(!lstrcmpi(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
727 ok(servicesize == (tempsize * 2),
728 "Expected servicesize not to change if buffer not insufficient\n") ;
731 MultiByteToWideChar(CP_ACP, 0, displayname, -1, displaynameW, sizeof(displaynameW)/2);
732 SetLastError(0xdeadbeef);
734 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
735 ok(ret, "Expected success, got error %u\n", GetLastError());
738 ok(lstrlen(servicename) == tempsize/2,
739 "Expected the buffer to be twice the length of the string\n") ;
740 ok(servicesize == lstrlenW(servicenameW),
741 "Expected servicesize not to change if buffer not insufficient\n") ;
744 SetLastError(0xdeadbeef);
746 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
747 ok(!ret, "Expected failure\n");
748 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
749 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
750 ok(servicenameW[0] == 0, "Buffer not empty\n");
752 CloseServiceHandle(scm_handle);
755 static void test_query_svc(void)
757 SC_HANDLE scm_handle, svc_handle;
759 SERVICE_STATUS status;
760 SERVICE_STATUS_PROCESS *statusproc;
761 DWORD bufsize, needed;
763 /* All NULL or wrong */
764 SetLastError(0xdeadbeef);
765 ret = QueryServiceStatus(NULL, NULL);
766 ok(!ret, "Expected failure\n");
767 ok(GetLastError() == ERROR_INVALID_HANDLE,
768 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
770 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
772 /* Check if 'Spooler' exists.
773 * Open with not enough rights to query the status.
775 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
778 skip("Spooler service doesn't exist\n");
779 CloseServiceHandle(scm_handle);
783 SetLastError(0xdeadbeef);
784 ret = QueryServiceStatus(svc_handle, NULL);
785 ok(!ret, "Expected failure\n");
787 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
788 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
789 "Unexpected last error %d\n", GetLastError());
791 SetLastError(0xdeadbeef);
792 ret = QueryServiceStatus(svc_handle, &status);
793 ok(!ret, "Expected failure\n");
794 ok(GetLastError() == ERROR_ACCESS_DENIED,
795 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
797 /* Open the service with just enough rights.
798 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
800 CloseServiceHandle(svc_handle);
801 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
803 SetLastError(0xdeadbeef);
804 ret = QueryServiceStatus(svc_handle, &status);
805 ok(ret, "Expected success, got error %u\n", GetLastError());
807 CloseServiceHandle(svc_handle);
809 /* More or less the same tests for QueryServiceStatusEx */
811 /* Open service with not enough rights to query the status */
812 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
814 /* All NULL or wrong, this proves that info level is checked first */
815 SetLastError(0xdeadbeef);
816 ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
817 ok(!ret, "Expected failure\n");
819 ok(GetLastError() == ERROR_INVALID_LEVEL,
820 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
822 /* Passing a NULL parameter for the needed buffer size
823 * will crash on anything but NT4.
826 /* Only info level is correct. It looks like the buffer/size is checked second */
827 SetLastError(0xdeadbeef);
828 ret = pQueryServiceStatusEx(NULL, 0, NULL, 0, &needed);
829 /* NT4 and Wine check the handle first */
830 if (GetLastError() != ERROR_INVALID_HANDLE)
832 ok(!ret, "Expected failure\n");
833 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
834 "Needed buffersize is wrong : %d\n", needed);
835 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
836 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
839 /* Pass a correct buffer and buffersize but a NULL handle */
840 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
842 SetLastError(0xdeadbeef);
843 ret = pQueryServiceStatusEx(NULL, 0, (BYTE*)statusproc, bufsize, &needed);
844 ok(!ret, "Expected failure\n");
845 ok(GetLastError() == ERROR_INVALID_HANDLE,
846 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
847 HeapFree(GetProcessHeap(), 0, statusproc);
849 /* Correct handle and info level */
850 SetLastError(0xdeadbeef);
851 ret = pQueryServiceStatusEx(svc_handle, 0, NULL, 0, &needed);
852 /* NT4 doesn't return the needed size */
853 if (GetLastError() != ERROR_INVALID_PARAMETER)
855 ok(!ret, "Expected failure\n");
858 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
859 "Needed buffersize is wrong : %d\n", needed);
860 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
861 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
865 /* All parameters are OK but we don't have enough rights */
866 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
867 bufsize = sizeof(SERVICE_STATUS_PROCESS);
868 SetLastError(0xdeadbeef);
869 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
870 ok(!ret, "Expected failure\n");
871 ok(GetLastError() == ERROR_ACCESS_DENIED,
872 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
873 HeapFree(GetProcessHeap(), 0, statusproc);
875 /* Open the service with just enough rights. */
876 CloseServiceHandle(svc_handle);
877 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
879 /* Everything should be fine now. */
880 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
881 bufsize = sizeof(SERVICE_STATUS_PROCESS);
882 SetLastError(0xdeadbeef);
883 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
884 ok(ret, "Expected success, got error %u\n", GetLastError());
885 if (statusproc->dwCurrentState == SERVICE_RUNNING)
886 ok(statusproc->dwProcessId != 0,
887 "Expect a process id for this running service\n");
889 ok(statusproc->dwProcessId == 0,
890 "Expect no process id for this stopped service\n");
891 HeapFree(GetProcessHeap(), 0, statusproc);
893 CloseServiceHandle(svc_handle);
894 CloseServiceHandle(scm_handle);
897 static void test_enum_svc(void)
899 SC_HANDLE scm_handle;
901 DWORD bufsize, needed, returned, resume;
902 DWORD tempneeded, tempreturned, missing;
903 DWORD servicecountactive, servicecountinactive;
904 ENUM_SERVICE_STATUS *services;
905 ENUM_SERVICE_STATUS_PROCESS *exservices;
908 /* All NULL or wrong */
909 SetLastError(0xdeadbeef);
910 ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
911 ok(!ret, "Expected failure\n");
913 ok(GetLastError() == ERROR_INVALID_HANDLE,
914 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
916 /* Open the service control manager with not enough rights at first */
917 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
919 /* Valid handle but rest is still NULL or wrong */
920 SetLastError(0xdeadbeef);
921 ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
922 ok(!ret, "Expected failure\n");
924 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
925 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
926 "Unexpected last error %d\n", GetLastError());
928 /* Don't specify the two required pointers */
929 returned = 0xdeadbeef;
930 SetLastError(0xdeadbeef);
931 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
932 ok(!ret, "Expected failure\n");
933 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
935 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
936 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
937 "Unexpected last error %d\n", GetLastError());
939 /* Don't specify the two required pointers */
941 SetLastError(0xdeadbeef);
942 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
943 ok(!ret, "Expected failure\n");
944 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
945 "Expected no change to the needed buffer variable\n");
947 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
948 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
949 "Unexpected last error %d\n", GetLastError());
951 /* No valid servicetype and servicestate */
953 returned = 0xdeadbeef;
954 SetLastError(0xdeadbeef);
955 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
956 ok(!ret, "Expected failure\n");
959 ok(needed == 0 || broken(needed != 0), /* nt4 */
960 "Expected needed buffer size to be set to 0, got %d\n", needed);
961 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
962 ok(GetLastError() == ERROR_INVALID_PARAMETER,
963 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
966 /* No valid servicetype and servicestate */
968 returned = 0xdeadbeef;
969 SetLastError(0xdeadbeef);
970 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
971 ok(!ret, "Expected failure\n");
974 ok(needed == 0 || broken(needed != 0), /* nt4 */
975 "Expected needed buffer size to be set to 0, got %d\n", needed);
976 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
977 ok(GetLastError() == ERROR_INVALID_PARAMETER,
978 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
981 /* No valid servicetype and servicestate */
983 returned = 0xdeadbeef;
984 SetLastError(0xdeadbeef);
985 ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0,
986 &needed, &returned, NULL);
987 ok(!ret, "Expected failure\n");
990 ok(needed == 0 || broken(needed != 0), /* nt4 */
991 "Expected needed buffer size to be set to 0, got %d\n", needed);
992 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
993 ok(GetLastError() == ERROR_INVALID_PARAMETER,
994 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
997 /* All parameters are correct but our access rights are wrong */
999 returned = 0xdeadbeef;
1000 SetLastError(0xdeadbeef);
1001 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1002 &needed, &returned, NULL);
1003 ok(!ret, "Expected failure\n");
1006 ok(needed == 0 || broken(needed != 0), /* nt4 */
1007 "Expected needed buffer size to be set to 0, got %d\n", needed);
1008 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1010 ok(GetLastError() == ERROR_ACCESS_DENIED,
1011 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1013 /* Open the service control manager with the needed rights */
1014 CloseServiceHandle(scm_handle);
1015 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1017 /* All parameters are correct. Request the needed buffer size */
1018 needed = 0xdeadbeef;
1019 returned = 0xdeadbeef;
1020 SetLastError(0xdeadbeef);
1021 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1022 &needed, &returned, NULL);
1023 ok(!ret, "Expected failure\n");
1026 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1027 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1028 ok(GetLastError() == ERROR_MORE_DATA,
1029 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1032 /* Store the needed bytes */
1033 tempneeded = needed;
1035 /* Allocate the correct needed bytes */
1036 services = HeapAlloc(GetProcessHeap(), 0, needed);
1038 needed = 0xdeadbeef;
1039 returned = 0xdeadbeef;
1040 SetLastError(0xdeadbeef);
1041 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1042 services, bufsize, &needed, &returned, NULL);
1045 ok(ret, "Expected success, got error %u\n", GetLastError());
1046 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1047 ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1049 HeapFree(GetProcessHeap(), 0, services);
1051 /* Store the number of returned services */
1052 tempreturned = returned;
1054 /* Allocate less than the needed bytes and don't specify a resume handle */
1055 services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1056 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1057 needed = 0xdeadbeef;
1058 returned = 0xdeadbeef;
1059 SetLastError(0xdeadbeef);
1060 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1061 services, bufsize, &needed, &returned, NULL);
1062 ok(!ret, "Expected failure\n");
1065 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1066 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1067 ok(GetLastError() == ERROR_MORE_DATA,
1068 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1071 /* Allocate less than the needed bytes, this time with a correct resume handle */
1072 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1073 needed = 0xdeadbeef;
1074 returned = 0xdeadbeef;
1076 SetLastError(0xdeadbeef);
1077 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1078 services, bufsize, &needed, &returned, &resume);
1079 ok(!ret, "Expected failure\n");
1082 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1083 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1084 ok(resume, "Expected a resume handle\n");
1085 ok(GetLastError() == ERROR_MORE_DATA,
1086 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1089 /* Fetch the missing services but pass a bigger buffer size */
1090 missing = tempreturned - returned;
1091 bufsize = tempneeded;
1092 needed = 0xdeadbeef;
1093 returned = 0xdeadbeef;
1094 SetLastError(0xdeadbeef);
1095 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1096 services, bufsize, &needed, &returned, &resume);
1099 ok(ret, "Expected success, got error %u\n", GetLastError());
1100 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1101 ok(returned == missing, "Expected %u services to be returned\n", missing);
1103 ok(resume == 0, "Expected the resume handle to be 0\n");
1104 HeapFree(GetProcessHeap(), 0, services);
1106 /* See if things add up */
1108 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1109 * and doesn't count the others as inactive. This means that Vista could
1110 * show a total that is greater than the sum of active and inactive
1112 * The number of active and inactive drivers is greatly influenced by the
1113 * time when tests are run, immediately after boot or later for example.
1115 * Both reasons make calculations for drivers not so useful
1118 /* Get the number of active win32 services */
1119 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1120 &needed, &returned, NULL);
1121 services = HeapAlloc(GetProcessHeap(), 0, needed);
1122 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1123 needed, &needed, &returned, NULL);
1124 HeapFree(GetProcessHeap(), 0, services);
1126 servicecountactive = returned;
1128 /* Get the number of inactive win32 services */
1129 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1130 &needed, &returned, NULL);
1131 services = HeapAlloc(GetProcessHeap(), 0, needed);
1132 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1133 needed, &needed, &returned, NULL);
1134 HeapFree(GetProcessHeap(), 0, services);
1136 servicecountinactive = returned;
1138 /* Get the number of win32 services */
1139 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1140 &needed, &returned, NULL);
1141 services = HeapAlloc(GetProcessHeap(), 0, needed);
1142 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1143 needed, &needed, &returned, NULL);
1144 HeapFree(GetProcessHeap(), 0, services);
1146 /* Check if total is the same as active and inactive win32 services */
1148 ok(returned == (servicecountactive + servicecountinactive),
1149 "Something wrong in the calculation\n");
1151 /* Get all drivers and services
1153 * Fetch the status of the last call as failing could make the following tests crash
1154 * on Wine (we don't return anything yet).
1156 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1157 NULL, 0, &needed, &returned, NULL);
1158 services = HeapAlloc(GetProcessHeap(), 0, needed);
1159 ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1160 services, needed, &needed, &returned, NULL);
1162 /* Loop through all those returned drivers and services */
1163 for (i = 0; ret && i < returned; i++)
1165 SERVICE_STATUS status = services[i].ServiceStatus;
1167 /* lpServiceName and lpDisplayName should always be filled */
1168 ok(lstrlenA(services[i].lpServiceName) > 0, "Expected a service name\n");
1169 ok(lstrlenA(services[i].lpDisplayName) > 0, "Expected a display name\n");
1171 /* Decrement the counters to see if the functions calls return the same
1172 * numbers as the contents of these structures.
1174 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1176 if (status.dwCurrentState == SERVICE_RUNNING)
1177 servicecountactive--;
1179 servicecountinactive--;
1182 HeapFree(GetProcessHeap(), 0, services);
1186 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1187 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1190 CloseServiceHandle(scm_handle);
1192 /* More or less the same for EnumServicesStatusExA */
1194 /* All NULL or wrong */
1195 SetLastError(0xdeadbeef);
1196 ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1197 ok(!ret, "Expected failure\n");
1199 ok(GetLastError() == ERROR_INVALID_LEVEL,
1200 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1202 /* All NULL or wrong, just the info level is correct */
1203 SetLastError(0xdeadbeef);
1204 ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1205 ok(!ret, "Expected failure\n");
1207 ok(GetLastError() == ERROR_INVALID_HANDLE,
1208 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1210 /* Open the service control manager with not enough rights at first */
1211 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1213 /* Valid handle and info level but rest is still NULL or wrong */
1214 SetLastError(0xdeadbeef);
1215 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1216 ok(!ret, "Expected failure\n");
1218 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1219 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1220 "Unexpected last error %d\n", GetLastError());
1222 /* Don't specify the two required pointers */
1223 needed = 0xdeadbeef;
1224 SetLastError(0xdeadbeef);
1225 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1226 ok(!ret, "Expected failure\n");
1227 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1228 "Expected no change to the needed buffer variable\n");
1230 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1231 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1232 "Unexpected last error %d\n", GetLastError());
1234 /* Don't specify the two required pointers */
1235 returned = 0xdeadbeef;
1236 SetLastError(0xdeadbeef);
1237 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1238 ok(!ret, "Expected failure\n");
1241 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1242 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1243 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1244 "Unexpected last error %d\n", GetLastError());
1247 /* No valid servicetype and servicestate */
1248 needed = 0xdeadbeef;
1249 returned = 0xdeadbeef;
1250 SetLastError(0xdeadbeef);
1251 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1252 ok(!ret, "Expected failure\n");
1253 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1256 ok(needed == 0 || broken(needed != 0), /* nt4 */
1257 "Expected needed buffer size to be set to 0, got %d\n", needed);
1258 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1259 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1262 /* No valid servicestate */
1263 needed = 0xdeadbeef;
1264 returned = 0xdeadbeef;
1265 SetLastError(0xdeadbeef);
1266 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1267 &needed, &returned, NULL, NULL);
1268 ok(!ret, "Expected failure\n");
1269 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1272 ok(needed == 0 || broken(needed != 0), /* nt4 */
1273 "Expected needed buffer size to be set to 0, got %d\n", needed);
1274 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1275 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1278 /* No valid servicetype */
1279 needed = 0xdeadbeef;
1280 returned = 0xdeadbeef;
1281 SetLastError(0xdeadbeef);
1282 ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1283 &needed, &returned, NULL, NULL);
1284 ok(!ret, "Expected failure\n");
1285 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1288 ok(needed == 0 || broken(needed != 0), /* nt4 */
1289 "Expected needed buffer size to be set to 0, got %d\n", needed);
1290 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1291 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1294 /* No valid servicetype and servicestate and unknown service group */
1295 needed = 0xdeadbeef;
1296 returned = 0xdeadbeef;
1297 SetLastError(0xdeadbeef);
1298 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1299 &returned, NULL, "deadbeef_group");
1300 ok(!ret, "Expected failure\n");
1301 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1304 ok(needed == 0 || broken(needed != 0), /* nt4 */
1305 "Expected needed buffer size to be set to 0, got %d\n", needed);
1306 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1307 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1310 /* All parameters are correct but our access rights are wrong */
1311 needed = 0xdeadbeef;
1312 returned = 0xdeadbeef;
1313 SetLastError(0xdeadbeef);
1314 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1315 NULL, 0, &needed, &returned, NULL, NULL);
1316 ok(!ret, "Expected failure\n");
1318 ok(needed == 0 || broken(needed != 0), /* nt4 */
1319 "Expected needed buffer size to be set to 0, got %d\n", needed);
1320 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1321 ok(GetLastError() == ERROR_ACCESS_DENIED,
1322 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1324 /* All parameters are correct, access rights are wrong but the
1325 * group name won't be checked yet.
1327 needed = 0xdeadbeef;
1328 returned = 0xdeadbeef;
1329 SetLastError(0xdeadbeef);
1330 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1331 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1332 ok(!ret, "Expected failure\n");
1334 ok(needed == 0 || broken(needed != 0), /* nt4 */
1335 "Expected needed buffer size to be set to 0, got %d\n", needed);
1336 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1337 ok(GetLastError() == ERROR_ACCESS_DENIED,
1338 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1340 /* Open the service control manager with the needed rights */
1341 CloseServiceHandle(scm_handle);
1342 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1344 /* All parameters are correct and the group will be checked */
1345 needed = 0xdeadbeef;
1346 returned = 0xdeadbeef;
1347 SetLastError(0xdeadbeef);
1348 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1349 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1350 ok(!ret, "Expected failure\n");
1351 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1354 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1355 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1356 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1359 /* TODO: Create a test that makes sure we enumerate all services that don't
1360 * belong to a group. (specifying "").
1363 /* All parameters are correct. Request the needed buffer size */
1364 needed = 0xdeadbeef;
1365 returned = 0xdeadbeef;
1366 SetLastError(0xdeadbeef);
1367 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1368 NULL, 0, &needed, &returned, NULL, NULL);
1369 ok(!ret, "Expected failure\n");
1370 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1373 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1374 ok(GetLastError() == ERROR_MORE_DATA,
1375 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1378 /* Store the needed bytes */
1379 tempneeded = needed;
1381 /* Allocate the correct needed bytes */
1382 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1384 needed = 0xdeadbeef;
1385 returned = 0xdeadbeef;
1386 SetLastError(0xdeadbeef);
1387 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1388 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1391 ok(ret, "Expected success, got error %u\n", GetLastError());
1392 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1393 ok(returned == tempreturned, "Expected the same number of service from this function\n");
1395 HeapFree(GetProcessHeap(), 0, exservices);
1397 /* Store the number of returned services */
1398 tempreturned = returned;
1400 /* Allocate less than the needed bytes and don't specify a resume handle */
1401 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1402 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1403 needed = 0xdeadbeef;
1404 returned = 0xdeadbeef;
1405 SetLastError(0xdeadbeef);
1406 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1407 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1408 ok(!ret, "Expected failure\n");
1411 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1412 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1413 ok(GetLastError() == ERROR_MORE_DATA,
1414 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1417 /* Allocate less than the needed bytes, this time with a correct resume handle */
1418 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1419 needed = 0xdeadbeef;
1420 returned = 0xdeadbeef;
1422 SetLastError(0xdeadbeef);
1423 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1424 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1425 ok(!ret, "Expected failure\n");
1428 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1429 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1430 ok(resume, "Expected a resume handle\n");
1431 ok(GetLastError() == ERROR_MORE_DATA,
1432 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1435 /* Fetch that last service but pass a bigger buffer size */
1436 missing = tempreturned - returned;
1437 bufsize = tempneeded;
1438 needed = 0xdeadbeef;
1439 returned = 0xdeadbeef;
1440 SetLastError(0xdeadbeef);
1441 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1442 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1445 ok(ret, "Expected success, got error %u\n", GetLastError());
1446 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1448 ok(returned == missing, "Expected %u services to be returned\n", missing);
1449 ok(resume == 0, "Expected the resume handle to be 0\n");
1450 HeapFree(GetProcessHeap(), 0, exservices);
1452 /* See if things add up */
1454 /* Get the number of active win32 services */
1455 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1456 NULL, 0, &needed, &returned, NULL, NULL);
1457 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1458 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1459 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1460 HeapFree(GetProcessHeap(), 0, exservices);
1462 servicecountactive = returned;
1464 /* Get the number of inactive win32 services */
1465 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1466 NULL, 0, &needed, &returned, NULL, NULL);
1467 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1468 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1469 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1470 HeapFree(GetProcessHeap(), 0, exservices);
1472 servicecountinactive = returned;
1474 /* Get the number of win32 services */
1475 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1476 NULL, 0, &needed, &returned, NULL, NULL);
1477 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1478 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1479 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1480 HeapFree(GetProcessHeap(), 0, exservices);
1482 /* Check if total is the same as active and inactive win32 services */
1483 ok(returned == (servicecountactive + servicecountinactive),
1484 "Something wrong in the calculation\n");
1486 /* Get all drivers and services */
1487 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1488 SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1489 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1490 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1491 SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1493 /* Loop through all those returned drivers and services */
1494 for (i = 0; i < returned; i++)
1496 SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1499 /* lpServiceName and lpDisplayName should always be filled */
1500 ok(lstrlenA(exservices[i].lpServiceName) > 0, "Expected a service name\n");
1501 ok(lstrlenA(exservices[i].lpDisplayName) > 0, "Expected a display name\n");
1503 /* Decrement the counters to see if the functions calls return the
1504 * same numbers as the contents of these structures.
1505 * Check some process id specifics.
1507 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1509 /* We shouldn't have a process id for drivers */
1510 ok(status.dwProcessId == 0,
1511 "This driver shouldn't have an associated process id\n");
1514 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1516 if (status.dwCurrentState == SERVICE_RUNNING)
1518 /* We expect a process id for every running service */
1519 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1520 exservices[i].lpServiceName);
1522 servicecountactive--;
1526 /* We shouldn't have a process id for inactive services */
1527 ok(status.dwProcessId == 0, "Service %s state %u shouldn't have an associated process id\n",
1528 exservices[i].lpServiceName, status.dwCurrentState);
1530 servicecountinactive--;
1534 HeapFree(GetProcessHeap(), 0, exservices);
1536 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1537 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1539 CloseServiceHandle(scm_handle);
1542 static void test_close(void)
1548 SetLastError(0xdeadbeef);
1549 ret = CloseServiceHandle(NULL);
1550 ok(!ret, "Expected failure\n");
1551 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1553 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1556 handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1557 SetLastError(0xdeadbeef);
1558 ret = CloseServiceHandle(handle);
1559 ok(ret, "Expected success got error %u\n", GetLastError());
1562 static void test_sequence(void)
1564 SC_HANDLE scm_handle, svc_handle;
1566 QUERY_SERVICE_CONFIGA *config;
1567 DWORD given, needed;
1568 static const CHAR servicename [] = "Winetest";
1569 static const CHAR displayname [] = "Winetest dummy service";
1570 static const CHAR displayname2[] = "Winetest dummy service (2)";
1571 static const CHAR pathname [] = "we_dont_care.exe";
1572 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1573 static const CHAR password [] = "";
1574 static const CHAR empty [] = "";
1575 static const CHAR localsystem [] = "LocalSystem";
1577 SetLastError(0xdeadbeef);
1578 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1580 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1582 skip("Not enough rights to get a handle to the manager\n");
1586 ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1588 if (!scm_handle) return;
1590 /* Create a dummy service */
1591 SetLastError(0xdeadbeef);
1592 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1593 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1594 pathname, NULL, NULL, dependencies, NULL, password);
1596 if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1598 /* We try and open the service and do the rest of the tests. Some could
1599 * fail if the tests were changed between these runs.
1601 trace("Deletion probably didn't work last time\n");
1602 SetLastError(0xdeadbeef);
1603 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1604 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1606 skip("Not enough rights to open the service\n");
1607 CloseServiceHandle(scm_handle);
1610 ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1612 else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1614 skip("Not enough rights to create the service\n");
1615 CloseServiceHandle(scm_handle);
1619 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1621 if (!svc_handle) return;
1624 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1625 * that the correct keys are used.
1628 /* Request the size for the buffer */
1629 SetLastError(0xdeadbeef);
1630 ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1631 ok(!ret, "Expected failure\n");
1632 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1634 config = HeapAlloc(GetProcessHeap(), 0, needed);
1636 SetLastError(0xdeadbeef);
1637 ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1638 ok(ret, "Expected success, got error %u\n", GetLastError());
1641 ok(given == needed, "Expected the given (%d) and needed (%d) buffersizes to be equal\n", given, needed);
1643 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1644 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1645 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1646 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1647 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1648 ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1649 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1650 ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1651 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1652 /* TODO: Show the double 0 terminated string */
1655 ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1657 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1658 ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1660 ok(ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2", NULL, NULL, NULL, NULL, displayname2),
1661 "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1663 QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1664 config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1665 ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1666 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1667 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1668 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1669 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1670 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1671 ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1672 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1673 ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1674 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1675 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1676 ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1678 SetLastError(0xdeadbeef);
1679 ret = DeleteService(svc_handle);
1680 ok(ret, "Expected success, got error %u\n", GetLastError());
1681 CloseServiceHandle(svc_handle);
1683 /* Wait a while. The following test does a CreateService again */
1686 CloseServiceHandle(scm_handle);
1687 HeapFree(GetProcessHeap(), 0, config);
1690 static void test_queryconfig2(void)
1692 SC_HANDLE scm_handle, svc_handle;
1694 DWORD expected, needed;
1695 BYTE buffer[MAX_PATH];
1696 LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
1697 static const CHAR servicename [] = "Winetest";
1698 static const CHAR displayname [] = "Winetest dummy service";
1699 static const CHAR pathname [] = "we_dont_care.exe";
1700 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1701 static const CHAR password [] = "";
1702 static const CHAR description [] = "Description";
1704 if(!pQueryServiceConfig2A)
1706 skip("function QueryServiceConfig2A not present\n");
1710 SetLastError(0xdeadbeef);
1711 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1715 if(GetLastError() == ERROR_ACCESS_DENIED)
1716 skip("Not enough rights to get a handle to the manager\n");
1718 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
1722 /* Create a dummy service */
1723 SetLastError(0xdeadbeef);
1724 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1725 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1726 pathname, NULL, NULL, dependencies, NULL, password);
1730 if(GetLastError() == ERROR_SERVICE_EXISTS)
1732 /* We try and open the service and do the rest of the tests. Some could
1733 * fail if the tests were changed between these runs.
1735 trace("Deletion probably didn't work last time\n");
1736 SetLastError(0xdeadbeef);
1737 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1740 if(GetLastError() == ERROR_ACCESS_DENIED)
1741 skip("Not enough rights to open the service\n");
1743 ok(FALSE, "Could not open the service : %d\n", GetLastError());
1744 CloseServiceHandle(scm_handle);
1748 if (GetLastError() == ERROR_ACCESS_DENIED)
1750 skip("Not enough rights to create the service\n");
1751 CloseServiceHandle(scm_handle);
1754 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1757 CloseServiceHandle(scm_handle);
1761 SetLastError(0xdeadbeef);
1762 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1763 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1764 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1766 SetLastError(0xdeadbeef);
1767 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1768 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1769 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1771 SetLastError(0xdeadbeef);
1772 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1773 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1774 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1776 SetLastError(0xdeadbeef);
1777 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
1778 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1779 ok((ERROR_INVALID_ADDRESS == GetLastError()) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()),
1780 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1782 SetLastError(0xdeadbeef);
1783 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),NULL);
1784 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1785 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1788 SetLastError(0xdeadbeef);
1789 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA)-1,&needed);
1790 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1791 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1792 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1795 pConfig->lpDescription = (LPSTR)0xdeadbeef;
1796 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1797 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1798 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1799 ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
1801 SetLastError(0xdeadbeef);
1803 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1804 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1805 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1806 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1808 if(!pChangeServiceConfig2A)
1810 skip("function ChangeServiceConfig2A not present\n");
1814 pConfig->lpDescription = (LPSTR) description;
1815 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
1816 ok(ret, "ChangeServiceConfig2A failed\n");
1821 SetLastError(0xdeadbeef);
1823 expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
1824 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1825 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1826 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1827 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
1829 SetLastError(0xdeadbeef);
1830 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
1831 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1832 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1834 SetLastError(0xdeadbeef);
1835 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
1836 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1837 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
1838 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
1840 SetLastError(0xdeadbeef);
1841 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
1842 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1843 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
1844 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
1846 if(!pQueryServiceConfig2W)
1848 skip("function QueryServiceConfig2W not present\n");
1851 SetLastError(0xdeadbeef);
1853 expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
1854 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1855 ok(!ret, "expected QueryServiceConfig2W to fail\n");
1856 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1857 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
1859 SetLastError(0xdeadbeef);
1860 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
1861 ok(ret, "expected QueryServiceConfig2W to succeed\n");
1864 DeleteService(svc_handle);
1866 CloseServiceHandle(svc_handle);
1868 /* Wait a while. The following test does a CreateService again */
1871 CloseServiceHandle(scm_handle);
1874 static void test_refcount(void)
1876 SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
1877 static const CHAR servicename [] = "Winetest";
1878 static const CHAR pathname [] = "we_dont_care.exe";
1881 /* Get a handle to the Service Control Manager */
1882 SetLastError(0xdeadbeef);
1883 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1884 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1886 skip("Not enough rights to get a handle to the manager\n");
1890 /* Create a service */
1891 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
1892 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
1893 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
1894 ok(svc_handle1 != NULL, "Expected success, got error %u\n", GetLastError());
1896 /* Get a handle to this new service */
1897 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
1898 ok(svc_handle2 != NULL, "Expected success, got error %u\n", GetLastError());
1900 /* Get another handle to this new service */
1901 svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
1902 ok(svc_handle3 != NULL, "Expected success, got error %u\n", GetLastError());
1904 /* Check if we can close the handle to the Service Control Manager */
1905 ret = CloseServiceHandle(scm_handle);
1906 ok(ret, "Expected success (err=%d)\n", GetLastError());
1908 /* Get a new handle to the Service Control Manager */
1909 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1910 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
1912 /* Get a handle to this new service */
1913 svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1914 ok(svc_handle4 != NULL, "Expected success, got error %u\n", GetLastError());
1916 /* Delete the service */
1917 ret = DeleteService(svc_handle4);
1918 ok(ret, "Expected success (err=%d)\n", GetLastError());
1920 /* We cannot create the same service again as it's still marked as 'being deleted'.
1921 * The reason is that we still have 4 open handles to this service even though we
1922 * closed the handle to the Service Control Manager in between.
1924 SetLastError(0xdeadbeef);
1925 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
1926 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
1927 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
1930 ok(!svc_handle5, "Expected failure\n");
1931 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
1932 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
1935 /* FIXME: Remove this when Wine is fixed */
1938 DeleteService(svc_handle5);
1939 CloseServiceHandle(svc_handle5);
1942 /* Close all the handles to the service and try again */
1943 ret = CloseServiceHandle(svc_handle4);
1944 ok(ret, "Expected success (err=%d)\n", GetLastError());
1945 ret = CloseServiceHandle(svc_handle3);
1946 ok(ret, "Expected success (err=%d)\n", GetLastError());
1947 ret = CloseServiceHandle(svc_handle2);
1948 ok(ret, "Expected success (err=%d)\n", GetLastError());
1949 ret = CloseServiceHandle(svc_handle1);
1950 ok(ret, "Expected success (err=%d)\n", GetLastError());
1952 /* Wait a while. Doing a CreateService too soon will result again
1953 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
1957 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
1958 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
1959 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
1960 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
1961 ok(svc_handle5 != NULL, "Expected success, got error %u\n", GetLastError());
1963 /* Delete the service */
1964 ret = DeleteService(svc_handle5);
1965 ok(ret, "Expected success (err=%d)\n", GetLastError());
1967 /* Wait a while. Just in case one of the following tests does a CreateService again */
1970 CloseServiceHandle(svc_handle5);
1971 CloseServiceHandle(scm_handle);
1976 SC_HANDLE scm_handle;
1978 /* Bail out if we are on win98 */
1979 SetLastError(0xdeadbeef);
1980 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1982 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1984 skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
1987 CloseServiceHandle(scm_handle);
1989 init_function_pointers();
1991 /* First some parameter checking */
1994 test_create_delete_svc();
1995 test_get_displayname();
1996 test_get_servicekeyname();
2000 /* Test the creation, querying and deletion of a service */
2002 test_queryconfig2();
2003 /* The main reason for this test is to check if any refcounting is used
2004 * and what the rules are