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
33 #include "wine/test.h"
35 static const CHAR spooler[] = "Spooler"; /* Should be available on all platforms */
37 static BOOL (WINAPI *pChangeServiceConfig2A)(SC_HANDLE,DWORD,LPVOID);
38 static BOOL (WINAPI *pEnumServicesStatusExA)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
39 DWORD, LPBYTE, DWORD, LPDWORD,
40 LPDWORD, LPDWORD, LPCSTR);
41 static BOOL (WINAPI *pEnumServicesStatusExW)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
42 DWORD, LPBYTE, DWORD, LPDWORD,
43 LPDWORD, LPDWORD, LPCWSTR);
44 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
45 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
46 static BOOL (WINAPI *pQueryServiceConfig2A)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
47 static BOOL (WINAPI *pQueryServiceConfig2W)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
48 static BOOL (WINAPI *pQueryServiceStatusEx)(SC_HANDLE, SC_STATUS_TYPE, LPBYTE,
51 static void init_function_pointers(void)
53 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
55 pChangeServiceConfig2A = (void*)GetProcAddress(hadvapi32, "ChangeServiceConfig2A");
56 pEnumServicesStatusExA= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExA");
57 pEnumServicesStatusExW= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExW");
58 pGetSecurityInfo = (void *)GetProcAddress(hadvapi32, "GetSecurityInfo");
59 pQueryServiceConfig2A= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2A");
60 pQueryServiceConfig2W= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2W");
61 pQueryServiceStatusEx= (void*)GetProcAddress(hadvapi32, "QueryServiceStatusEx");
64 static void test_open_scm(void)
68 /* No access rights */
69 SetLastError(0xdeadbeef);
70 scm_handle = OpenSCManagerA(NULL, NULL, 0);
71 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
72 CloseServiceHandle(scm_handle);
74 /* Unknown database name */
75 SetLastError(0xdeadbeef);
76 scm_handle = OpenSCManagerA(NULL, "DoesNotExist", SC_MANAGER_CONNECT);
77 ok(!scm_handle, "Expected failure\n");
78 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
79 CloseServiceHandle(scm_handle); /* Just in case */
81 /* MSDN says only ServiceActive is allowed, or NULL */
82 SetLastError(0xdeadbeef);
83 scm_handle = OpenSCManagerA(NULL, SERVICES_FAILED_DATABASEA, SC_MANAGER_CONNECT);
84 ok(!scm_handle, "Expected failure\n");
85 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
86 CloseServiceHandle(scm_handle); /* Just in case */
88 /* Remote unknown host */
89 SetLastError(0xdeadbeef);
90 scm_handle = OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
93 ok(!scm_handle, "Expected failure\n");
94 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* w2k8 */,
95 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
97 CloseServiceHandle(scm_handle); /* Just in case */
99 /* Proper call with an empty hostname */
100 SetLastError(0xdeadbeef);
101 scm_handle = OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
102 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
103 CloseServiceHandle(scm_handle);
105 /* Again a correct one */
106 SetLastError(0xdeadbeef);
107 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
108 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
109 CloseServiceHandle(scm_handle);
112 static void test_open_svc(void)
114 SC_HANDLE scm_handle, svc_handle;
115 CHAR displayname[4096];
118 /* All NULL (invalid access rights) */
119 SetLastError(0xdeadbeef);
120 svc_handle = OpenServiceA(NULL, NULL, 0);
121 ok(!svc_handle, "Expected failure\n");
122 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
124 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
127 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
128 SetLastError(0xdeadbeef);
129 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
130 ok(!svc_handle, "Expected failure\n");
131 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
132 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
133 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
135 /* Nonexistent service */
136 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
137 SetLastError(0xdeadbeef);
138 svc_handle = OpenServiceA(scm_handle, "deadbeef", GENERIC_READ);
139 ok(!svc_handle, "Expected failure\n");
140 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
141 CloseServiceHandle(scm_handle);
143 /* Proper SCM handle but different access rights */
144 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
145 SetLastError(0xdeadbeef);
146 svc_handle = OpenServiceA(scm_handle, "Spooler", GENERIC_WRITE);
147 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
148 skip("Not enough rights to get a handle to the service\n");
151 ok(svc_handle != NULL, "Expected success, got error %u\n", GetLastError());
152 CloseServiceHandle(svc_handle);
155 /* Test to show we can't open a service with the displayname */
157 /* Retrieve the needed size for the buffer */
159 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
160 /* Get the displayname */
161 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
162 /* Try to open the service with this displayname, unless the displayname equals
163 * the servicename as that would defeat the purpose of this test.
165 if (!lstrcmpi(spooler, displayname))
167 skip("displayname equals servicename\n");
168 CloseServiceHandle(scm_handle);
172 SetLastError(0xdeadbeef);
173 svc_handle = OpenServiceA(scm_handle, displayname, GENERIC_READ);
174 ok(!svc_handle, "Expected failure\n");
175 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
177 CloseServiceHandle(svc_handle);
179 CloseServiceHandle(scm_handle);
182 static void test_create_delete_svc(void)
184 SC_HANDLE scm_handle, svc_handle1;
185 CHAR username[UNLEN + 1], domain[MAX_PATH];
186 DWORD user_size = UNLEN + 1;
187 CHAR account[UNLEN + 3];
188 static const CHAR servicename [] = "Winetest";
189 static const CHAR pathname [] = "we_dont_care.exe";
190 static const CHAR empty [] = "";
191 static const CHAR password [] = "secret";
192 BOOL spooler_exists = FALSE;
195 DWORD display_size = sizeof(display);
197 /* Get the username and turn it into an account to be used in some tests */
198 GetUserNameA(username, &user_size);
199 /* Get the domainname to cater for that situation */
200 if (GetEnvironmentVariableA("USERDOMAIN", domain, MAX_PATH))
201 sprintf(account, "%s\\%s", domain, username);
203 sprintf(account, ".\\%s", username);
206 SetLastError(0xdeadbeef);
207 svc_handle1 = CreateServiceA(NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
208 ok(!svc_handle1, "Expected failure\n");
209 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
211 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
213 /* Only a valid handle to the Service Control Manager */
214 SetLastError(0xdeadbeef);
215 svc_handle1 = CreateServiceA(scm_handle, NULL, 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 /* Now with a servicename */
222 SetLastError(0xdeadbeef);
223 svc_handle1 = CreateServiceA(scm_handle, servicename, 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 /* Or just a binary name */
230 SetLastError(0xdeadbeef);
231 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, pathname, 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 /* Both servicename and binary name (We only have connect rights) */
238 SetLastError(0xdeadbeef);
239 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
240 ok(!svc_handle1, "Expected failure\n");
241 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
243 /* They can even be empty at this stage of parameter checking */
244 SetLastError(0xdeadbeef);
245 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
246 ok(!svc_handle1, "Expected failure\n");
247 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
249 SetLastError(0xdeadbeef);
250 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
251 ok(!svc_handle1, "Expected failure\n");
252 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
254 /* Open the Service Control Manager with minimal rights for creation
255 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
257 CloseServiceHandle(scm_handle);
258 SetLastError(0xdeadbeef);
259 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
260 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
262 skip("Not enough rights to get a handle to the manager\n");
266 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
268 /* Empty strings for servicename and binary name are checked */
269 SetLastError(0xdeadbeef);
270 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
271 ok(!svc_handle1, "Expected failure\n");
272 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
274 SetLastError(0xdeadbeef);
275 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
276 ok(!svc_handle1, "Expected failure\n");
277 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
279 SetLastError(0xdeadbeef);
280 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
281 ok(!svc_handle1, "Expected failure\n");
282 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
284 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
285 * an ERROR_INVALID_PARAMETER)
287 SetLastError(0xdeadbeef);
288 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
289 SERVICE_DISABLED, 0, empty, NULL, NULL, NULL, NULL, NULL);
290 ok(!svc_handle1, "Expected failure\n");
291 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
293 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
295 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
296 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
298 SetLastError(0xdeadbeef);
299 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS,
300 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
301 ok(!svc_handle1, "Expected failure\n");
302 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
304 /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
305 SetLastError(0xdeadbeef);
306 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_FILE_SYSTEM_DRIVER | SERVICE_INTERACTIVE_PROCESS,
307 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
308 ok(!svc_handle1, "Expected failure\n");
309 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
311 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
312 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
314 SetLastError(0xdeadbeef);
315 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
316 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, account, password);
317 ok(!svc_handle1, "Expected failure\n");
318 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INVALID_SERVICE_ACCOUNT,
319 "Expected ERROR_INVALID_PARAMETER or ERROR_INVALID_SERVICE_ACCOUNT, got %d\n", GetLastError());
321 /* Illegal (start-type is not a mask and should only be one of the possibilities)
322 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
323 * it's most likely not the wanted start-type)
325 SetLastError(0xdeadbeef);
326 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS,
327 SERVICE_AUTO_START | SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
328 ok(!svc_handle1, "Expected failure\n");
329 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
331 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
332 SetLastError(0xdeadbeef);
333 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
334 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
335 ok(!svc_handle1, "Expected failure\n");
336 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
338 /* Test if ServiceType can be a combined one for drivers */
339 SetLastError(0xdeadbeef);
340 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER,
341 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
342 ok(!svc_handle1, "Expected failure\n");
343 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
345 /* The service already exists (check first, just in case) */
346 svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
349 spooler_exists = TRUE;
350 CloseServiceHandle(svc_handle1);
351 SetLastError(0xdeadbeef);
352 svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
353 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
354 ok(!svc_handle1, "Expected failure\n");
355 ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
358 skip("Spooler service doesn't exist\n");
360 /* To find an existing displayname we check the 'Spooler' service. Although the registry
361 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
362 * to the servicename and can't be used as well for a new displayname.
366 ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
369 skip("Could not retrieve a displayname for the Spooler service\n");
372 svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
373 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
374 ok(!svc_handle1, "Expected failure\n");
375 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
376 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
380 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
382 /* Windows doesn't care about the access rights for creation (which makes
383 * sense as there is no service yet) as long as there are sufficient
384 * rights to the manager.
386 SetLastError(0xdeadbeef);
387 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
388 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
389 ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
391 /* DeleteService however must have proper rights */
392 SetLastError(0xdeadbeef);
393 ret = DeleteService(svc_handle1);
394 ok(!ret, "Expected failure\n");
395 ok(GetLastError() == ERROR_ACCESS_DENIED,
396 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
398 /* Open the service with minimal rights for deletion.
399 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
401 CloseServiceHandle(svc_handle1);
402 svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
404 /* Now that we have the proper rights, we should be able to delete */
405 SetLastError(0xdeadbeef);
406 ret = DeleteService(svc_handle1);
407 ok(ret, "Expected success, got error %u\n", GetLastError());
409 CloseServiceHandle(svc_handle1);
410 CloseServiceHandle(scm_handle);
412 /* Wait a while. One of the following tests also does a CreateService for the
413 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
414 * error if we do this to quick. Vista seems more picky then the others.
418 /* And a final NULL check */
419 SetLastError(0xdeadbeef);
420 ret = DeleteService(NULL);
421 ok(!ret, "Expected failure\n");
422 ok(GetLastError() == ERROR_INVALID_HANDLE,
423 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
426 static void test_get_displayname(void)
428 SC_HANDLE scm_handle, svc_handle;
430 CHAR displayname[4096];
431 WCHAR displaynameW[2048];
432 DWORD displaysize, tempsize, tempsizeW;
433 static const CHAR deadbeef[] = "Deadbeef";
434 static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
435 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
436 static const WCHAR abcW[] = {'A','B','C',0};
437 static const CHAR servicename[] = "Winetest";
438 static const CHAR pathname[] = "we_dont_care.exe";
440 /* Having NULL for the size of the buffer will crash on W2K3 */
442 SetLastError(0xdeadbeef);
443 ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
444 ok(!ret, "Expected failure\n");
445 ok(GetLastError() == ERROR_INVALID_HANDLE,
446 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
448 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
450 SetLastError(0xdeadbeef);
451 ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
452 ok(!ret, "Expected failure\n");
453 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
454 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
455 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
457 SetLastError(0xdeadbeef);
458 displaysize = sizeof(displayname);
459 ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
460 ok(!ret, "Expected failure\n");
461 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
462 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
463 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
465 /* Test for nonexistent service */
466 SetLastError(0xdeadbeef);
468 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
469 ok(!ret, "Expected failure\n");
470 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
471 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
473 SetLastError(0xdeadbeef);
474 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
475 ok(!ret, "Expected failure\n");
476 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
477 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
478 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
481 strcpy(displayname, "ABC");
482 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
483 ok(!ret, "Expected failure\n");
484 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
485 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
486 todo_wine ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
487 ok(displayname[0] == 0, "Service name not empty\n");
490 lstrcpyW( displaynameW, abcW );
491 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
492 ok(!ret, "Expected failure\n");
493 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
494 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
495 ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
496 ok(displaynameW[0] == 0, "Service name not empty\n");
499 strcpy(displayname, "ABC");
500 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
501 ok(!ret, "Expected failure\n");
502 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
503 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
504 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
505 ok(displayname[0] == 'A', "Service name changed\n");
508 lstrcpyW( displaynameW, abcW );
509 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
510 ok(!ret, "Expected failure\n");
511 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
512 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
513 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
514 ok(displaynameW[0] == 'A', "Service name changed\n");
517 strcpy(displayname, "ABC");
518 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
519 ok(!ret, "Expected failure\n");
520 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
521 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
522 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
523 ok(displayname[0] == 0, "Service name not empty\n");
526 lstrcpyW( displaynameW, abcW );
527 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
528 ok(!ret, "Expected failure\n");
529 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
530 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
531 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
532 ok(displaynameW[0] == 'A', "Service name changed\n");
535 strcpy(displayname, "ABC");
536 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
537 ok(!ret, "Expected failure\n");
538 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
539 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
540 todo_wine ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
541 ok(displayname[0] == 0, "Service name not empty\n");
544 lstrcpyW( displaynameW, abcW );
545 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
546 ok(!ret, "Expected failure\n");
547 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
548 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
549 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
550 ok(displaynameW[0] == 0, "Service name not empty\n");
552 /* Check if 'Spooler' exists */
553 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
556 skip("Spooler service doesn't exist\n");
557 CloseServiceHandle(scm_handle);
560 CloseServiceHandle(svc_handle);
562 /* Retrieve the needed size for the buffer */
563 SetLastError(0xdeadbeef);
565 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
566 ok(!ret, "Expected failure\n");
567 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
568 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
569 tempsize = displaysize;
572 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
573 ok(!ret, "Expected failure\n");
574 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
575 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
576 ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
578 /* Buffer is too small */
579 SetLastError(0xdeadbeef);
580 displaysize = (tempsize / 2);
581 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
582 ok(!ret, "Expected failure\n");
583 ok(displaysize == tempsize, "Expected the needed buffersize\n");
584 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
585 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
587 /* First try with a buffer that should be big enough to hold
588 * the ANSI string (and terminating character). This succeeds on Windows
589 * although when asked (see above 2 tests) it will return twice the needed size.
591 SetLastError(0xdeadbeef);
592 displaysize = (tempsize / 2) + 1;
593 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
594 ok(ret, "Expected success, got error %u\n", GetLastError());
595 ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
597 /* Now with the original returned size */
598 SetLastError(0xdeadbeef);
599 displaysize = tempsize;
600 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
601 ok(ret, "Expected success, got error %u\n", GetLastError());
602 ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
604 /* And with a bigger than needed buffer */
605 SetLastError(0xdeadbeef);
606 displaysize = tempsize * 2;
607 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
608 ok(ret, "Expected success, got error %u\n", GetLastError());
609 /* Test that shows that if the buffersize is enough, it's not changed */
610 ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
611 ok(strlen(displayname) == tempsize/2,
612 "Expected the buffer to be twice the length of the string\n") ;
614 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
615 SetLastError(0xdeadbeef);
617 ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
618 ok(!ret, "Expected failure\n");
619 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
620 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
622 /* Buffer is too small */
623 SetLastError(0xdeadbeef);
624 tempsizeW = displaysize;
625 displaysize = tempsizeW / 2;
626 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
627 ok(!ret, "Expected failure\n");
628 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
629 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
630 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
632 /* Now with the original returned size */
633 SetLastError(0xdeadbeef);
634 displaysize = tempsizeW;
635 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
636 ok(!ret, "Expected failure\n");
637 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
638 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
639 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
641 /* And with a bigger than needed buffer */
642 SetLastError(0xdeadbeef);
643 displaysize = tempsizeW + 1; /* This caters for the null terminating character */
644 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
645 ok(ret, "Expected success, got error %u\n", GetLastError());
646 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
647 ok(lstrlenW(displaynameW) == displaysize,
648 "Expected the buffer to be the length of the string\n") ;
649 ok(tempsize / 2 == tempsizeW,
650 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
652 CloseServiceHandle(scm_handle);
654 /* Test for a service without a displayname (which is valid). This should return
655 * the servicename itself.
657 SetLastError(0xdeadbeef);
658 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
659 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
661 skip("Not enough rights to get a handle to the manager\n");
665 SetLastError(0xdeadbeef);
666 svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
667 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
668 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
669 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
672 CloseServiceHandle(scm_handle);
676 /* Retrieve the needed size for the buffer */
677 SetLastError(0xdeadbeef);
679 ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
680 ok(!ret, "Expected failure\n");
681 ok(displaysize == strlen(servicename) * 2,
682 "Expected the displaysize to be twice the size of the servicename\n");
683 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
684 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
686 /* Buffer is too small */
687 SetLastError(0xdeadbeef);
688 tempsize = displaysize;
689 displaysize = (tempsize / 2);
690 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
691 ok(!ret, "Expected failure\n");
692 ok(displaysize == tempsize, "Expected the needed buffersize\n");
693 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
694 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
696 /* Get the displayname */
697 SetLastError(0xdeadbeef);
698 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
699 ok(ret, "Expected success, got error %u\n", GetLastError());
700 ok(!lstrcmpi(displayname, servicename),
701 "Expected displayname to be %s, got %s\n", servicename, displayname);
703 /* Delete the service */
704 ret = DeleteService(svc_handle);
705 ok(ret, "Expected success (err=%d)\n", GetLastError());
707 CloseServiceHandle(svc_handle);
708 CloseServiceHandle(scm_handle);
710 /* Wait a while. Just in case one of the following tests does a CreateService again */
714 static void test_get_servicekeyname(void)
716 SC_HANDLE scm_handle, svc_handle;
717 CHAR servicename[4096];
718 CHAR displayname[4096];
719 WCHAR servicenameW[4096];
720 WCHAR displaynameW[4096];
721 DWORD servicesize, displaysize, tempsize;
723 static const CHAR deadbeef[] = "Deadbeef";
724 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
725 static const WCHAR abcW[] = {'A','B','C',0};
727 /* Having NULL for the size of the buffer will crash on W2K3 */
729 SetLastError(0xdeadbeef);
730 ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
731 ok(!ret, "Expected failure\n");
732 ok(GetLastError() == ERROR_INVALID_HANDLE,
733 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
735 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
738 SetLastError(0xdeadbeef);
739 ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
740 ok(!ret, "Expected failure\n");
741 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
742 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
743 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
744 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
746 /* Valid handle and buffer but no displayname */
748 SetLastError(0xdeadbeef);
749 ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
750 ok(!ret, "Expected failure\n");
751 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
752 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
753 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
754 todo_wine ok(servicesize == 200, "Service size expected 1, got %d\n", servicesize);
756 /* Test for nonexistent displayname */
757 SetLastError(0xdeadbeef);
758 ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
759 ok(!ret, "Expected failure\n");
760 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
761 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
762 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
765 strcpy(servicename, "ABC");
766 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
767 ok(!ret, "Expected failure\n");
768 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
769 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
770 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
771 ok(servicename[0] == 0, "Service name not empty\n");
774 lstrcpyW( servicenameW, abcW );
775 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
776 ok(!ret, "Expected failure\n");
777 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
778 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
779 ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
780 ok(servicenameW[0] == 0, "Service name not empty\n");
783 strcpy(servicename, "ABC");
784 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
785 ok(!ret, "Expected failure\n");
786 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
787 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
788 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
789 ok(servicename[0] == 'A', "Service name changed\n");
792 lstrcpyW( servicenameW, abcW );
793 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
794 ok(!ret, "Expected failure\n");
795 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
796 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
797 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
798 ok(servicenameW[0] == 'A', "Service name changed\n");
801 strcpy(servicename, "ABC");
802 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
803 ok(!ret, "Expected failure\n");
804 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
805 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
806 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
807 ok(servicename[0] == 0, "Service name not empty\n");
810 lstrcpyW( servicenameW, abcW );
811 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
812 ok(!ret, "Expected failure\n");
813 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
814 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
815 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
816 ok(servicenameW[0] == 'A', "Service name changed\n");
819 strcpy(servicename, "ABC");
820 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
821 ok(!ret, "Expected failure\n");
822 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
823 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
824 todo_wine ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
825 ok(servicename[0] == 0, "Service name not empty\n");
828 lstrcpyW( servicenameW, abcW );
829 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
830 ok(!ret, "Expected failure\n");
831 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
832 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
833 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
834 ok(servicenameW[0] == 0, "Service name not empty\n");
836 /* Check if 'Spooler' exists */
837 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
840 skip("Spooler service doesn't exist\n");
841 CloseServiceHandle(scm_handle);
844 CloseServiceHandle(svc_handle);
846 /* Get the displayname for the 'Spooler' service */
847 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
848 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
850 /* Retrieve the needed size for the buffer */
851 SetLastError(0xdeadbeef);
853 ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
854 ok(!ret, "Expected failure\n");
855 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
856 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
858 /* Valid call with the correct buffersize */
859 SetLastError(0xdeadbeef);
860 tempsize = servicesize;
862 ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
863 ok(ret, "Expected success, got error %u\n", GetLastError());
866 ok(strlen(servicename) == tempsize/2,
867 "Expected the buffer to be twice the length of the string\n") ;
868 ok(!lstrcmpi(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
869 ok(servicesize == (tempsize * 2),
870 "Expected servicesize not to change if buffer not insufficient\n") ;
873 MultiByteToWideChar(CP_ACP, 0, displayname, -1, displaynameW, sizeof(displaynameW)/2);
874 SetLastError(0xdeadbeef);
876 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
877 ok(ret, "Expected success, got error %u\n", GetLastError());
880 ok(strlen(servicename) == tempsize/2,
881 "Expected the buffer to be twice the length of the string\n") ;
882 ok(servicesize == lstrlenW(servicenameW),
883 "Expected servicesize not to change if buffer not insufficient\n") ;
886 SetLastError(0xdeadbeef);
888 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
889 ok(!ret, "Expected failure\n");
890 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
891 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
892 ok(servicenameW[0] == 0, "Buffer not empty\n");
894 CloseServiceHandle(scm_handle);
897 static void test_query_svc(void)
899 SC_HANDLE scm_handle, svc_handle;
901 SERVICE_STATUS status;
902 SERVICE_STATUS_PROCESS *statusproc;
903 DWORD bufsize, needed;
905 /* All NULL or wrong */
906 SetLastError(0xdeadbeef);
907 ret = QueryServiceStatus(NULL, NULL);
908 ok(!ret, "Expected failure\n");
909 ok(GetLastError() == ERROR_INVALID_HANDLE,
910 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
912 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
914 /* Check if 'Spooler' exists.
915 * Open with not enough rights to query the status.
917 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
920 skip("Spooler service doesn't exist\n");
921 CloseServiceHandle(scm_handle);
925 SetLastError(0xdeadbeef);
926 ret = QueryServiceStatus(svc_handle, NULL);
927 ok(!ret, "Expected failure\n");
929 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
930 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
931 "Unexpected last error %d\n", GetLastError());
933 SetLastError(0xdeadbeef);
934 ret = QueryServiceStatus(svc_handle, &status);
935 ok(!ret, "Expected failure\n");
936 ok(GetLastError() == ERROR_ACCESS_DENIED,
937 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
939 /* Open the service with just enough rights.
940 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
942 CloseServiceHandle(svc_handle);
943 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
945 SetLastError(0xdeadbeef);
946 ret = QueryServiceStatus(svc_handle, &status);
947 ok(ret, "Expected success, got error %u\n", GetLastError());
949 CloseServiceHandle(svc_handle);
951 /* More or less the same tests for QueryServiceStatusEx */
953 /* Open service with not enough rights to query the status */
954 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
956 /* All NULL or wrong, this proves that info level is checked first */
957 SetLastError(0xdeadbeef);
958 ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
959 ok(!ret, "Expected failure\n");
961 ok(GetLastError() == ERROR_INVALID_LEVEL,
962 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
964 /* Passing a NULL parameter for the needed buffer size
965 * will crash on anything but NT4.
968 /* Only info level is correct. It looks like the buffer/size is checked second */
969 SetLastError(0xdeadbeef);
970 ret = pQueryServiceStatusEx(NULL, 0, NULL, 0, &needed);
971 /* NT4 and Wine check the handle first */
972 if (GetLastError() != ERROR_INVALID_HANDLE)
974 ok(!ret, "Expected failure\n");
975 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
976 "Needed buffersize is wrong : %d\n", needed);
977 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
978 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
981 /* Pass a correct buffer and buffersize but a NULL handle */
982 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
984 SetLastError(0xdeadbeef);
985 ret = pQueryServiceStatusEx(NULL, 0, (BYTE*)statusproc, bufsize, &needed);
986 ok(!ret, "Expected failure\n");
987 ok(GetLastError() == ERROR_INVALID_HANDLE,
988 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
989 HeapFree(GetProcessHeap(), 0, statusproc);
991 /* Correct handle and info level */
992 SetLastError(0xdeadbeef);
993 ret = pQueryServiceStatusEx(svc_handle, 0, NULL, 0, &needed);
994 /* NT4 doesn't return the needed size */
995 if (GetLastError() != ERROR_INVALID_PARAMETER)
997 ok(!ret, "Expected failure\n");
1000 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
1001 "Needed buffersize is wrong : %d\n", needed);
1002 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1003 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1007 /* All parameters are OK but we don't have enough rights */
1008 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1009 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1010 SetLastError(0xdeadbeef);
1011 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
1012 ok(!ret, "Expected failure\n");
1013 ok(GetLastError() == ERROR_ACCESS_DENIED,
1014 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1015 HeapFree(GetProcessHeap(), 0, statusproc);
1017 /* Open the service with just enough rights. */
1018 CloseServiceHandle(svc_handle);
1019 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
1021 /* Everything should be fine now. */
1022 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1023 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1024 SetLastError(0xdeadbeef);
1025 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
1026 ok(ret, "Expected success, got error %u\n", GetLastError());
1027 if (statusproc->dwCurrentState == SERVICE_RUNNING)
1028 ok(statusproc->dwProcessId != 0,
1029 "Expect a process id for this running service\n");
1031 ok(statusproc->dwProcessId == 0,
1032 "Expect no process id for this stopped service\n");
1033 HeapFree(GetProcessHeap(), 0, statusproc);
1035 CloseServiceHandle(svc_handle);
1036 CloseServiceHandle(scm_handle);
1039 static void test_enum_svc(void)
1041 SC_HANDLE scm_handle;
1043 DWORD bufsize, needed, returned, resume;
1044 DWORD neededW, returnedW;
1045 DWORD tempneeded, tempreturned, missing;
1046 DWORD servicecountactive, servicecountinactive;
1047 ENUM_SERVICE_STATUS *services;
1048 ENUM_SERVICE_STATUSW *servicesW;
1049 ENUM_SERVICE_STATUS_PROCESS *exservices;
1052 /* All NULL or wrong */
1053 SetLastError(0xdeadbeef);
1054 ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1055 ok(!ret, "Expected failure\n");
1056 ok(GetLastError() == ERROR_INVALID_HANDLE,
1057 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1059 SetLastError(0xdeadbeef);
1060 ret = EnumServicesStatusW(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1061 ok(!ret, "Expected failure\n");
1062 ok(GetLastError() == ERROR_INVALID_HANDLE,
1063 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1065 /* Open the service control manager with not enough rights at first */
1066 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1068 /* Valid handle but rest is still NULL or wrong */
1069 SetLastError(0xdeadbeef);
1070 ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1071 ok(!ret, "Expected failure\n");
1072 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1073 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1074 "Unexpected last error %d\n", GetLastError());
1076 SetLastError(0xdeadbeef);
1077 ret = EnumServicesStatusW(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1078 ok(!ret, "Expected failure\n");
1079 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1080 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1081 "Unexpected last error %d\n", GetLastError());
1083 /* Don't specify the two required pointers */
1084 returned = 0xdeadbeef;
1085 SetLastError(0xdeadbeef);
1086 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1087 ok(!ret, "Expected failure\n");
1088 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1089 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1090 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1091 "Unexpected last error %d\n", GetLastError());
1093 returned = 0xdeadbeef;
1094 SetLastError(0xdeadbeef);
1095 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1096 ok(!ret, "Expected failure\n");
1097 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1098 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1099 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1100 "Unexpected last error %d\n", GetLastError());
1102 /* Don't specify the two required pointers */
1103 needed = 0xdeadbeef;
1104 SetLastError(0xdeadbeef);
1105 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1106 ok(!ret, "Expected failure\n");
1107 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1108 "Expected no change to the needed buffer variable\n");
1109 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1110 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1111 "Unexpected last error %d\n", GetLastError());
1113 needed = 0xdeadbeef;
1114 SetLastError(0xdeadbeef);
1115 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1116 ok(!ret, "Expected failure\n");
1117 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1118 "Expected no change to the needed buffer variable\n");
1119 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1120 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1121 "Unexpected last error %d\n", GetLastError());
1123 /* No valid servicetype and servicestate */
1124 needed = 0xdeadbeef;
1125 returned = 0xdeadbeef;
1126 SetLastError(0xdeadbeef);
1127 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1128 ok(!ret, "Expected failure\n");
1129 ok(needed == 0 || broken(needed != 0), /* nt4 */
1130 "Expected needed buffer size to be set to 0, got %d\n", needed);
1131 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1132 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1133 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1135 needed = 0xdeadbeef;
1136 returned = 0xdeadbeef;
1137 SetLastError(0xdeadbeef);
1138 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1139 ok(!ret, "Expected failure\n");
1140 ok(needed == 0 || broken(needed != 0), /* nt4 */
1141 "Expected needed buffer size to be set to 0, got %d\n", needed);
1142 ok(returned == 0 || broken(returned != 0), /* nt4 */
1143 "Expected number of services to be set to 0, got %d\n", returned);
1144 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1145 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1147 /* No valid servicestate */
1148 needed = 0xdeadbeef;
1149 returned = 0xdeadbeef;
1150 SetLastError(0xdeadbeef);
1151 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1152 ok(!ret, "Expected failure\n");
1153 ok(needed == 0 || broken(needed != 0), /* nt4 */
1154 "Expected needed buffer size to be set to 0, got %d\n", needed);
1155 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1156 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1157 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1159 needed = 0xdeadbeef;
1160 returned = 0xdeadbeef;
1161 SetLastError(0xdeadbeef);
1162 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1163 ok(!ret, "Expected failure\n");
1164 ok(needed == 0 || broken(needed != 0), /* nt4 */
1165 "Expected needed buffer size to be set to 0, got %d\n", needed);
1166 ok(returned == 0 || broken(returned != 0), /* nt4 */
1167 "Expected number of services to be set to 0, got %d\n", returned);
1168 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1169 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1171 /* No valid servicetype */
1172 needed = 0xdeadbeef;
1173 returned = 0xdeadbeef;
1174 SetLastError(0xdeadbeef);
1175 ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1176 ok(!ret, "Expected failure\n");
1177 ok(needed == 0 || broken(needed != 0), /* nt4 */
1178 "Expected needed buffer size to be set to 0, got %d\n", needed);
1179 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1180 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1181 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1183 needed = 0xdeadbeef;
1184 returned = 0xdeadbeef;
1185 SetLastError(0xdeadbeef);
1186 ret = EnumServicesStatusW(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1187 ok(!ret, "Expected failure\n");
1188 ok(needed == 0 || broken(needed != 0), /* nt4 */
1189 "Expected needed buffer size to be set to 0, got %d\n", needed);
1190 ok(returned == 0 || broken(returned != 0), /* nt4 */
1191 "Expected number of services to be set to 0, got %d\n", returned);
1192 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1193 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1195 /* All parameters are correct but our access rights are wrong */
1196 needed = 0xdeadbeef;
1197 returned = 0xdeadbeef;
1198 SetLastError(0xdeadbeef);
1199 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1200 ok(!ret, "Expected failure\n");
1201 ok(needed == 0 || broken(needed != 0), /* nt4 */
1202 "Expected needed buffer size to be set to 0, got %d\n", needed);
1203 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1204 ok(GetLastError() == ERROR_ACCESS_DENIED,
1205 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1207 needed = 0xdeadbeef;
1208 returned = 0xdeadbeef;
1209 SetLastError(0xdeadbeef);
1210 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1211 ok(!ret, "Expected failure\n");
1212 ok(needed == 0 || broken(needed != 0), /* nt4 */
1213 "Expected needed buffer size to be set to 0, got %d\n", needed);
1214 ok(returned == 0 || broken(returned != 0), /* nt4 */
1215 "Expected number of services to be set to 0, got %d\n", returned);
1216 ok(GetLastError() == ERROR_ACCESS_DENIED,
1217 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1219 /* Open the service control manager with the needed rights */
1220 CloseServiceHandle(scm_handle);
1221 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1223 /* All parameters are correct. Request the needed buffer size */
1224 needed = 0xdeadbeef;
1225 returned = 0xdeadbeef;
1226 SetLastError(0xdeadbeef);
1227 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1228 ok(!ret, "Expected failure\n");
1229 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1230 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1231 ok(GetLastError() == ERROR_MORE_DATA,
1232 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1234 /* Test to show we get the same needed buffer size for the W-call */
1235 neededW = 0xdeadbeef;
1236 returnedW = 0xdeadbeef;
1237 SetLastError(0xdeadbeef);
1238 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &neededW, &returnedW, NULL);
1239 ok(!ret, "Expected failure\n");
1240 ok(neededW != 0xdeadbeef && neededW > 0, "Expected the needed buffer size for this one service\n");
1241 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1242 ok(returnedW == 0, "Expected no service returned, got %d\n", returnedW);
1243 ok(GetLastError() == ERROR_MORE_DATA,
1244 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1246 /* Store the needed bytes */
1247 tempneeded = needed;
1249 /* Allocate the correct needed bytes */
1250 services = HeapAlloc(GetProcessHeap(), 0, needed);
1252 needed = 0xdeadbeef;
1253 returned = 0xdeadbeef;
1254 SetLastError(0xdeadbeef);
1255 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1256 services, bufsize, &needed, &returned, NULL);
1257 ok(ret, "Expected success, got error %u\n", GetLastError());
1258 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1259 ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1260 HeapFree(GetProcessHeap(), 0, services);
1262 /* Store the number of returned services */
1263 tempreturned = returned;
1265 servicesW = HeapAlloc(GetProcessHeap(), 0, neededW);
1267 neededW = 0xdeadbeef;
1268 returnedW = 0xdeadbeef;
1269 SetLastError(0xdeadbeef);
1270 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1271 servicesW, bufsize, &neededW, &returnedW, NULL);
1272 ok(ret, "Expected success, got error %u\n", GetLastError());
1273 ok(neededW == 0, "Expected needed buffer to be 0 as we are done\n");
1274 ok(returnedW != 0xdeadbeef && returnedW > 0, "Expected some returned services\n");
1275 HeapFree(GetProcessHeap(), 0, servicesW);
1277 /* Allocate less than the needed bytes and don't specify a resume handle */
1278 services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1279 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1280 needed = 0xdeadbeef;
1281 returned = 0xdeadbeef;
1282 SetLastError(0xdeadbeef);
1283 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1284 services, bufsize, &needed, &returned, NULL);
1285 ok(!ret, "Expected failure\n");
1286 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1287 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1288 ok(GetLastError() == ERROR_MORE_DATA,
1289 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1291 /* Allocate less than the needed bytes, this time with a correct resume handle */
1292 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1293 needed = 0xdeadbeef;
1294 returned = 0xdeadbeef;
1296 SetLastError(0xdeadbeef);
1297 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1298 services, bufsize, &needed, &returned, &resume);
1299 ok(!ret, "Expected failure\n");
1300 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1301 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1302 todo_wine ok(resume, "Expected a resume handle\n");
1303 ok(GetLastError() == ERROR_MORE_DATA,
1304 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1306 /* Fetch the missing services but pass a bigger buffer size */
1307 missing = tempreturned - returned;
1308 bufsize = tempneeded;
1309 needed = 0xdeadbeef;
1310 returned = 0xdeadbeef;
1311 SetLastError(0xdeadbeef);
1312 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1313 services, bufsize, &needed, &returned, &resume);
1314 ok(ret, "Expected success, got error %u\n", GetLastError());
1315 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1316 ok(returned == missing, "Expected %u services to be returned\n", missing);
1317 ok(resume == 0, "Expected the resume handle to be 0\n");
1318 HeapFree(GetProcessHeap(), 0, services);
1320 /* See if things add up */
1322 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1323 * and doesn't count the others as inactive. This means that Vista could
1324 * show a total that is greater than the sum of active and inactive
1326 * The number of active and inactive drivers is greatly influenced by the
1327 * time when tests are run, immediately after boot or later for example.
1329 * Both reasons make calculations for drivers not so useful
1332 /* Get the number of active win32 services */
1333 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1334 &needed, &returned, NULL);
1335 services = HeapAlloc(GetProcessHeap(), 0, needed);
1336 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1337 needed, &needed, &returned, NULL);
1338 HeapFree(GetProcessHeap(), 0, services);
1340 servicecountactive = returned;
1342 /* Get the number of inactive win32 services */
1343 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1344 &needed, &returned, NULL);
1345 services = HeapAlloc(GetProcessHeap(), 0, needed);
1346 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1347 needed, &needed, &returned, NULL);
1348 HeapFree(GetProcessHeap(), 0, services);
1350 servicecountinactive = returned;
1352 /* Get the number of win32 services */
1353 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1354 &needed, &returned, NULL);
1355 services = HeapAlloc(GetProcessHeap(), 0, needed);
1356 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1357 needed, &needed, &returned, NULL);
1358 HeapFree(GetProcessHeap(), 0, services);
1360 /* Check if total is the same as active and inactive win32 services */
1361 ok(returned == (servicecountactive + servicecountinactive),
1362 "Something wrong in the calculation\n");
1364 /* Get all drivers and services
1366 * Fetch the status of the last call as failing could make the following tests crash
1367 * on Wine (we don't return anything yet).
1369 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1370 NULL, 0, &needed, &returned, NULL);
1371 services = HeapAlloc(GetProcessHeap(), 0, needed);
1372 ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1373 services, needed, &needed, &returned, NULL);
1375 /* Loop through all those returned drivers and services */
1376 for (i = 0; ret && i < returned; i++)
1378 SERVICE_STATUS status = services[i].ServiceStatus;
1380 /* lpServiceName and lpDisplayName should always be filled */
1381 ok(lstrlenA(services[i].lpServiceName) > 0, "Expected a service name\n");
1382 ok(lstrlenA(services[i].lpDisplayName) > 0, "Expected a display name\n");
1384 /* Decrement the counters to see if the functions calls return the same
1385 * numbers as the contents of these structures.
1387 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1389 if (status.dwCurrentState == SERVICE_STOPPED)
1390 servicecountinactive--;
1392 servicecountactive--;
1395 HeapFree(GetProcessHeap(), 0, services);
1397 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1398 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1400 CloseServiceHandle(scm_handle);
1402 /* More or less the same for EnumServicesStatusExA */
1404 /* All NULL or wrong */
1405 SetLastError(0xdeadbeef);
1406 ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1407 ok(!ret, "Expected failure\n");
1408 ok(GetLastError() == ERROR_INVALID_LEVEL,
1409 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1411 /* All NULL or wrong, just the info level is correct */
1412 SetLastError(0xdeadbeef);
1413 ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1414 ok(!ret, "Expected failure\n");
1415 ok(GetLastError() == ERROR_INVALID_HANDLE,
1416 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1418 /* Open the service control manager with not enough rights at first */
1419 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1421 /* Valid handle and info level but rest is still NULL or wrong */
1422 SetLastError(0xdeadbeef);
1423 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1424 ok(!ret, "Expected failure\n");
1425 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1426 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1427 "Unexpected last error %d\n", GetLastError());
1429 /* Don't specify the two required pointers */
1430 needed = 0xdeadbeef;
1431 SetLastError(0xdeadbeef);
1432 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1433 ok(!ret, "Expected failure\n");
1434 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1435 "Expected no change to the needed buffer variable\n");
1436 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1437 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1438 "Unexpected last error %d\n", GetLastError());
1440 /* Don't specify the two required pointers */
1441 returned = 0xdeadbeef;
1442 SetLastError(0xdeadbeef);
1443 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1444 ok(!ret, "Expected failure\n");
1445 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1446 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1447 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1448 "Unexpected last error %d\n", GetLastError());
1450 /* No valid servicetype and servicestate */
1451 needed = 0xdeadbeef;
1452 returned = 0xdeadbeef;
1453 SetLastError(0xdeadbeef);
1454 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1455 ok(!ret, "Expected failure\n");
1456 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1457 ok(needed == 0 || broken(needed != 0), /* nt4 */
1458 "Expected needed buffer size to be set to 0, got %d\n", needed);
1459 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1460 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1462 /* No valid servicestate */
1463 needed = 0xdeadbeef;
1464 returned = 0xdeadbeef;
1465 SetLastError(0xdeadbeef);
1466 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1467 &needed, &returned, NULL, NULL);
1468 ok(!ret, "Expected failure\n");
1469 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1470 ok(needed == 0 || broken(needed != 0), /* nt4 */
1471 "Expected needed buffer size to be set to 0, got %d\n", needed);
1472 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1473 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1475 /* No valid servicetype */
1476 needed = 0xdeadbeef;
1477 returned = 0xdeadbeef;
1478 SetLastError(0xdeadbeef);
1479 ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1480 &needed, &returned, NULL, NULL);
1481 ok(!ret, "Expected failure\n");
1482 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1483 ok(needed == 0 || broken(needed != 0), /* nt4 */
1484 "Expected needed buffer size to be set to 0, got %d\n", needed);
1485 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1486 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1488 /* No valid servicetype and servicestate and unknown service group */
1489 needed = 0xdeadbeef;
1490 returned = 0xdeadbeef;
1491 SetLastError(0xdeadbeef);
1492 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1493 &returned, NULL, "deadbeef_group");
1494 ok(!ret, "Expected failure\n");
1495 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1496 ok(needed == 0 || broken(needed != 0), /* nt4 */
1497 "Expected needed buffer size to be set to 0, got %d\n", needed);
1498 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1499 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1501 /* All parameters are correct but our access rights are wrong */
1502 needed = 0xdeadbeef;
1503 returned = 0xdeadbeef;
1504 SetLastError(0xdeadbeef);
1505 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1506 NULL, 0, &needed, &returned, NULL, NULL);
1507 ok(!ret, "Expected failure\n");
1508 ok(needed == 0 || broken(needed != 0), /* nt4 */
1509 "Expected needed buffer size to be set to 0, got %d\n", needed);
1510 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1511 ok(GetLastError() == ERROR_ACCESS_DENIED,
1512 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1514 /* All parameters are correct, access rights are wrong but the
1515 * group name won't be checked yet.
1517 needed = 0xdeadbeef;
1518 returned = 0xdeadbeef;
1519 SetLastError(0xdeadbeef);
1520 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1521 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1522 ok(!ret, "Expected failure\n");
1523 ok(needed == 0 || broken(needed != 0), /* nt4 */
1524 "Expected needed buffer size to be set to 0, got %d\n", needed);
1525 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1526 ok(GetLastError() == ERROR_ACCESS_DENIED,
1527 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1529 /* Open the service control manager with the needed rights */
1530 CloseServiceHandle(scm_handle);
1531 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1533 /* All parameters are correct and the group will be checked */
1534 needed = 0xdeadbeef;
1535 returned = 0xdeadbeef;
1536 SetLastError(0xdeadbeef);
1537 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1538 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1539 ok(!ret, "Expected failure\n");
1540 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1541 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1542 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1543 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1545 /* TODO: Create a test that makes sure we enumerate all services that don't
1546 * belong to a group. (specifying "").
1549 /* All parameters are correct. Request the needed buffer size */
1550 needed = 0xdeadbeef;
1551 returned = 0xdeadbeef;
1552 SetLastError(0xdeadbeef);
1553 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1554 NULL, 0, &needed, &returned, NULL, NULL);
1555 ok(!ret, "Expected failure\n");
1556 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1557 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1558 ok(GetLastError() == ERROR_MORE_DATA,
1559 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1561 /* Test to show we get the same needed buffer size for the W-call */
1562 neededW = 0xdeadbeef;
1563 ret = pEnumServicesStatusExW(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1564 NULL, 0, &neededW, &returnedW, NULL, NULL);
1565 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1567 /* Store the needed bytes */
1568 tempneeded = needed;
1570 /* Allocate the correct needed bytes */
1571 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1573 needed = 0xdeadbeef;
1574 returned = 0xdeadbeef;
1575 SetLastError(0xdeadbeef);
1576 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1577 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1578 ok(ret, "Expected success, got error %u\n", GetLastError());
1579 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1580 ok(returned == tempreturned, "Expected the same number of service from this function\n");
1581 HeapFree(GetProcessHeap(), 0, exservices);
1583 /* Store the number of returned services */
1584 tempreturned = returned;
1586 /* Allocate less than the needed bytes and don't specify a resume handle */
1587 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1588 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1589 needed = 0xdeadbeef;
1590 returned = 0xdeadbeef;
1591 SetLastError(0xdeadbeef);
1592 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1593 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1594 ok(!ret, "Expected failure\n");
1595 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1596 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1597 ok(GetLastError() == ERROR_MORE_DATA,
1598 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1600 /* Allocate less than the needed bytes, this time with a correct resume handle */
1601 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1602 needed = 0xdeadbeef;
1603 returned = 0xdeadbeef;
1605 SetLastError(0xdeadbeef);
1606 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1607 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1608 ok(!ret, "Expected failure\n");
1609 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1610 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1611 todo_wine ok(resume, "Expected a resume handle\n");
1612 ok(GetLastError() == ERROR_MORE_DATA,
1613 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1615 /* Fetch that last service but pass a bigger buffer size */
1616 missing = tempreturned - returned;
1617 bufsize = tempneeded;
1618 needed = 0xdeadbeef;
1619 returned = 0xdeadbeef;
1620 SetLastError(0xdeadbeef);
1621 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1622 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1623 ok(ret, "Expected success, got error %u\n", GetLastError());
1624 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1625 ok(returned == missing, "Expected %u services to be returned\n", missing);
1626 ok(resume == 0, "Expected the resume handle to be 0\n");
1627 HeapFree(GetProcessHeap(), 0, exservices);
1629 /* See if things add up */
1631 /* Get the number of active win32 services */
1632 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1633 NULL, 0, &needed, &returned, NULL, NULL);
1634 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1635 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1636 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1637 HeapFree(GetProcessHeap(), 0, exservices);
1639 servicecountactive = returned;
1641 /* Get the number of inactive win32 services */
1642 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1643 NULL, 0, &needed, &returned, NULL, NULL);
1644 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1645 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1646 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1647 HeapFree(GetProcessHeap(), 0, exservices);
1649 servicecountinactive = returned;
1651 /* Get the number of win32 services */
1652 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1653 NULL, 0, &needed, &returned, NULL, NULL);
1654 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1655 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1656 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1657 HeapFree(GetProcessHeap(), 0, exservices);
1659 /* Check if total is the same as active and inactive win32 services */
1660 ok(returned == (servicecountactive + servicecountinactive),
1661 "Something wrong in the calculation\n");
1663 /* Get all drivers and services */
1664 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1665 SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1666 ok(!ret, "Expected failure\n");
1667 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1668 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1669 SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1670 ok(ret, "Expected success %u\n", GetLastError());
1672 /* Loop through all those returned drivers and services */
1673 for (i = 0; i < returned; i++)
1675 SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1677 /* lpServiceName and lpDisplayName should always be filled */
1678 ok(lstrlenA(exservices[i].lpServiceName) > 0, "Expected a service name\n");
1679 ok(lstrlenA(exservices[i].lpDisplayName) > 0, "Expected a display name\n");
1681 /* Decrement the counters to see if the functions calls return the
1682 * same numbers as the contents of these structures.
1683 * Check some process id specifics.
1685 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1687 /* We shouldn't have a process id for drivers */
1688 ok(status.dwProcessId == 0,
1689 "This driver shouldn't have an associated process id\n");
1692 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1694 if (status.dwCurrentState != SERVICE_STOPPED)
1696 /* We expect a process id for every running service */
1697 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1698 exservices[i].lpServiceName);
1700 servicecountactive--;
1704 /* We shouldn't have a process id for inactive services */
1705 ok(status.dwProcessId == 0, "Service %s state %u shouldn't have an associated process id\n",
1706 exservices[i].lpServiceName, status.dwCurrentState);
1708 servicecountinactive--;
1712 HeapFree(GetProcessHeap(), 0, exservices);
1714 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1715 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1717 CloseServiceHandle(scm_handle);
1720 static void test_close(void)
1726 SetLastError(0xdeadbeef);
1727 ret = CloseServiceHandle(NULL);
1728 ok(!ret, "Expected failure\n");
1729 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1731 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1734 handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1735 SetLastError(0xdeadbeef);
1736 ret = CloseServiceHandle(handle);
1737 ok(ret, "Expected success got error %u\n", GetLastError());
1740 static void test_sequence(void)
1742 SC_HANDLE scm_handle, svc_handle;
1744 QUERY_SERVICE_CONFIGA *config;
1745 DWORD given, needed;
1746 static const CHAR servicename [] = "Winetest";
1747 static const CHAR displayname [] = "Winetest dummy service";
1748 static const CHAR displayname2[] = "Winetest dummy service (2)";
1749 static const CHAR pathname [] = "we_dont_care.exe";
1750 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1751 static const CHAR password [] = "";
1752 static const CHAR empty [] = "";
1753 static const CHAR localsystem [] = "LocalSystem";
1755 SetLastError(0xdeadbeef);
1756 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1758 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1760 skip("Not enough rights to get a handle to the manager\n");
1764 ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1766 if (!scm_handle) return;
1768 /* Create a dummy service */
1769 SetLastError(0xdeadbeef);
1770 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1771 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1772 pathname, NULL, NULL, dependencies, NULL, password);
1774 if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1776 /* We try and open the service and do the rest of the tests. Some could
1777 * fail if the tests were changed between these runs.
1779 trace("Deletion probably didn't work last time\n");
1780 SetLastError(0xdeadbeef);
1781 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1782 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1784 skip("Not enough rights to open the service\n");
1785 CloseServiceHandle(scm_handle);
1788 ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1790 else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1792 skip("Not enough rights to create the service\n");
1793 CloseServiceHandle(scm_handle);
1798 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1799 if ((svc_handle != NULL) && (pGetSecurityInfo != NULL))
1801 PSID sidOwner, sidGroup;
1803 PSECURITY_DESCRIPTOR pSD;
1804 HRESULT retval = pGetSecurityInfo(svc_handle,SE_SERVICE,DACL_SECURITY_INFORMATION,&sidOwner,&sidGroup,&dacl,&sacl,&pSD);
1805 todo_wine ok(ERROR_SUCCESS == retval, "Expected GetSecurityInfo to succeed: result %d\n",retval);
1809 if (!svc_handle) return;
1812 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1813 * that the correct keys are used.
1816 /* Request the size for the buffer */
1817 SetLastError(0xdeadbeef);
1818 ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1819 ok(!ret, "Expected failure\n");
1820 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1822 config = HeapAlloc(GetProcessHeap(), 0, needed);
1824 SetLastError(0xdeadbeef);
1825 ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1826 ok(ret, "Expected success, got error %u\n", GetLastError());
1828 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1829 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1830 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1831 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1832 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1833 ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1834 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1835 ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1836 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1837 /* TODO: Show the double 0 terminated string */
1840 ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1842 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1843 ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1845 ok(ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2", NULL, NULL, NULL, NULL, displayname2),
1846 "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1848 QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1849 config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1850 ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1851 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1852 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1853 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1854 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1855 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1856 ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1857 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1858 ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1859 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1860 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1861 ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1863 SetLastError(0xdeadbeef);
1864 ret = DeleteService(svc_handle);
1865 ok(ret, "Expected success, got error %u\n", GetLastError());
1866 CloseServiceHandle(svc_handle);
1868 /* Wait a while. The following test does a CreateService again */
1871 CloseServiceHandle(scm_handle);
1872 HeapFree(GetProcessHeap(), 0, config);
1875 static void test_queryconfig2(void)
1877 SC_HANDLE scm_handle, svc_handle;
1879 DWORD expected, needed;
1880 BYTE buffer[MAX_PATH];
1881 LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
1882 static const CHAR servicename [] = "Winetest";
1883 static const CHAR displayname [] = "Winetest dummy service";
1884 static const CHAR pathname [] = "we_dont_care.exe";
1885 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1886 static const CHAR password [] = "";
1887 static const CHAR description [] = "Description";
1889 if(!pQueryServiceConfig2A)
1891 win_skip("function QueryServiceConfig2A not present\n");
1895 SetLastError(0xdeadbeef);
1896 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1900 if(GetLastError() == ERROR_ACCESS_DENIED)
1901 skip("Not enough rights to get a handle to the manager\n");
1903 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
1907 /* Create a dummy service */
1908 SetLastError(0xdeadbeef);
1909 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1910 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1911 pathname, NULL, NULL, dependencies, NULL, password);
1915 if(GetLastError() == ERROR_SERVICE_EXISTS)
1917 /* We try and open the service and do the rest of the tests. Some could
1918 * fail if the tests were changed between these runs.
1920 trace("Deletion probably didn't work last time\n");
1921 SetLastError(0xdeadbeef);
1922 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1925 if(GetLastError() == ERROR_ACCESS_DENIED)
1926 skip("Not enough rights to open the service\n");
1928 ok(FALSE, "Could not open the service : %d\n", GetLastError());
1929 CloseServiceHandle(scm_handle);
1933 if (GetLastError() == ERROR_ACCESS_DENIED)
1935 skip("Not enough rights to create the service\n");
1936 CloseServiceHandle(scm_handle);
1939 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1942 CloseServiceHandle(scm_handle);
1946 SetLastError(0xdeadbeef);
1947 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1948 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1949 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1951 SetLastError(0xdeadbeef);
1952 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1953 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1954 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1956 SetLastError(0xdeadbeef);
1957 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1958 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1959 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1961 SetLastError(0xdeadbeef);
1962 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
1963 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1964 ok((ERROR_INVALID_ADDRESS == GetLastError()) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()),
1965 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1967 SetLastError(0xdeadbeef);
1968 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),NULL);
1969 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1970 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1973 SetLastError(0xdeadbeef);
1974 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA)-1,&needed);
1975 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1976 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1977 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1980 pConfig->lpDescription = (LPSTR)0xdeadbeef;
1981 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1982 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1983 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1984 ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
1986 SetLastError(0xdeadbeef);
1988 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1989 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1990 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1991 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1993 if(!pChangeServiceConfig2A)
1995 win_skip("function ChangeServiceConfig2A not present\n");
1999 pConfig->lpDescription = (LPSTR) description;
2000 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
2001 ok(ret, "ChangeServiceConfig2A failed\n");
2006 SetLastError(0xdeadbeef);
2008 expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
2009 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2010 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2011 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2012 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2014 SetLastError(0xdeadbeef);
2015 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
2016 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2017 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2019 SetLastError(0xdeadbeef);
2020 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
2021 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2022 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2023 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2025 SetLastError(0xdeadbeef);
2026 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
2027 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2028 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2029 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2031 if(!pQueryServiceConfig2W)
2033 win_skip("function QueryServiceConfig2W not present\n");
2036 SetLastError(0xdeadbeef);
2038 expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
2039 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
2040 ok(!ret, "expected QueryServiceConfig2W to fail\n");
2041 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2042 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2044 SetLastError(0xdeadbeef);
2045 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
2046 ok(ret, "expected QueryServiceConfig2W to succeed\n");
2049 DeleteService(svc_handle);
2051 CloseServiceHandle(svc_handle);
2053 /* Wait a while. The following test does a CreateService again */
2056 CloseServiceHandle(scm_handle);
2059 static void test_refcount(void)
2061 SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
2062 static const CHAR servicename [] = "Winetest";
2063 static const CHAR pathname [] = "we_dont_care.exe";
2066 /* Get a handle to the Service Control Manager */
2067 SetLastError(0xdeadbeef);
2068 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2069 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
2071 skip("Not enough rights to get a handle to the manager\n");
2075 /* Create a service */
2076 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2077 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2078 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2079 ok(svc_handle1 != NULL, "Expected success, got error %u\n", GetLastError());
2081 /* Get a handle to this new service */
2082 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2083 ok(svc_handle2 != NULL, "Expected success, got error %u\n", GetLastError());
2085 /* Get another handle to this new service */
2086 svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2087 ok(svc_handle3 != NULL, "Expected success, got error %u\n", GetLastError());
2089 /* Check if we can close the handle to the Service Control Manager */
2090 ret = CloseServiceHandle(scm_handle);
2091 ok(ret, "Expected success (err=%d)\n", GetLastError());
2093 /* Get a new handle to the Service Control Manager */
2094 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2095 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
2097 /* Get a handle to this new service */
2098 svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2099 ok(svc_handle4 != NULL, "Expected success, got error %u\n", GetLastError());
2101 /* Delete the service */
2102 ret = DeleteService(svc_handle4);
2103 ok(ret, "Expected success (err=%d)\n", GetLastError());
2105 /* We cannot create the same service again as it's still marked as 'being deleted'.
2106 * The reason is that we still have 4 open handles to this service even though we
2107 * closed the handle to the Service Control Manager in between.
2109 SetLastError(0xdeadbeef);
2110 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2111 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2112 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2115 ok(!svc_handle5, "Expected failure\n");
2116 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
2117 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2120 /* FIXME: Remove this when Wine is fixed */
2123 DeleteService(svc_handle5);
2124 CloseServiceHandle(svc_handle5);
2127 /* Close all the handles to the service and try again */
2128 ret = CloseServiceHandle(svc_handle4);
2129 ok(ret, "Expected success (err=%d)\n", GetLastError());
2130 ret = CloseServiceHandle(svc_handle3);
2131 ok(ret, "Expected success (err=%d)\n", GetLastError());
2132 ret = CloseServiceHandle(svc_handle2);
2133 ok(ret, "Expected success (err=%d)\n", GetLastError());
2134 ret = CloseServiceHandle(svc_handle1);
2135 ok(ret, "Expected success (err=%d)\n", GetLastError());
2137 /* Wait a while. Doing a CreateService too soon will result again
2138 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2142 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2143 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2144 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2145 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2146 ok(svc_handle5 != NULL, "Expected success, got error %u\n", GetLastError());
2148 /* Delete the service */
2149 ret = DeleteService(svc_handle5);
2150 ok(ret, "Expected success (err=%d)\n", GetLastError());
2152 /* Wait a while. Just in case one of the following tests does a CreateService again */
2155 CloseServiceHandle(svc_handle5);
2156 CloseServiceHandle(scm_handle);
2161 SC_HANDLE scm_handle;
2163 /* Bail out if we are on win98 */
2164 SetLastError(0xdeadbeef);
2165 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2167 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2169 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2172 CloseServiceHandle(scm_handle);
2174 init_function_pointers();
2176 /* First some parameter checking */
2179 test_create_delete_svc();
2180 test_get_displayname();
2181 test_get_servicekeyname();
2185 /* Test the creation, querying and deletion of a service */
2187 test_queryconfig2();
2188 /* The main reason for this test is to check if any refcounting is used
2189 * and what the rules are