Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / advapi32 / tests / service.c
blob5a4fabdd8927c6ba8d59c8830dbc9fac2e2ad213
1 /*
2 * Unit tests for service functions
4 * Copyright (c) 2007 Paul Vriens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "winsvc.h"
29 #include "winnls.h"
30 #include "lmcons.h"
31 #include "aclapi.h"
33 #include "wine/test.h"
35 static const CHAR spooler[] = "Spooler"; /* Should be available on all platforms */
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,
49 DWORD, LPDWORD);
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)
66 SC_HANDLE scm_handle;
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);
91 todo_wine
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];
116 DWORD displaysize;
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 */
126 /* NULL service */
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");
149 else
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 */
158 displaysize = 0;
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);
169 return;
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());
176 /* Just in case */
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;
193 BOOL ret;
194 CHAR display[4096];
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);
202 else
203 sprintf(account, ".\\%s", username);
205 /* All NULL */
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");
263 return;
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 /* The service already exists (check first, just in case) */
339 svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
340 if (svc_handle1)
342 spooler_exists = TRUE;
343 CloseServiceHandle(svc_handle1);
344 SetLastError(0xdeadbeef);
345 svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
346 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
347 ok(!svc_handle1, "Expected failure\n");
348 ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
350 else
351 skip("Spooler service doesn't exist\n");
353 /* To find an existing displayname we check the 'Spooler' service. Although the registry
354 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
355 * to the servicename and can't be used as well for a new displayname.
357 if (spooler_exists)
359 ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
361 if (!ret)
362 skip("Could not retrieve a displayname for the Spooler service\n");
363 else
365 svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
366 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
367 ok(!svc_handle1, "Expected failure\n");
368 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
369 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
372 else
373 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
375 /* Windows doesn't care about the access rights for creation (which makes
376 * sense as there is no service yet) as long as there are sufficient
377 * rights to the manager.
379 SetLastError(0xdeadbeef);
380 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
381 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
382 ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
384 /* DeleteService however must have proper rights */
385 SetLastError(0xdeadbeef);
386 ret = DeleteService(svc_handle1);
387 ok(!ret, "Expected failure\n");
388 ok(GetLastError() == ERROR_ACCESS_DENIED,
389 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
391 /* Open the service with minimal rights for deletion.
392 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
394 CloseServiceHandle(svc_handle1);
395 svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
397 /* Now that we have the proper rights, we should be able to delete */
398 SetLastError(0xdeadbeef);
399 ret = DeleteService(svc_handle1);
400 ok(ret, "Expected success, got error %u\n", GetLastError());
402 CloseServiceHandle(svc_handle1);
403 CloseServiceHandle(scm_handle);
405 /* Wait a while. One of the following tests also does a CreateService for the
406 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
407 * error if we do this to quick. Vista seems more picky then the others.
409 Sleep(1000);
411 /* And a final NULL check */
412 SetLastError(0xdeadbeef);
413 ret = DeleteService(NULL);
414 ok(!ret, "Expected failure\n");
415 ok(GetLastError() == ERROR_INVALID_HANDLE,
416 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
419 static void test_get_displayname(void)
421 SC_HANDLE scm_handle, svc_handle;
422 BOOL ret;
423 CHAR displayname[4096];
424 WCHAR displaynameW[2048];
425 DWORD displaysize, tempsize, tempsizeW;
426 static const CHAR deadbeef[] = "Deadbeef";
427 static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
428 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
429 static const WCHAR abcW[] = {'A','B','C',0};
430 static const CHAR servicename[] = "Winetest";
431 static const CHAR pathname[] = "we_dont_care.exe";
433 /* Having NULL for the size of the buffer will crash on W2K3 */
435 SetLastError(0xdeadbeef);
436 ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
437 ok(!ret, "Expected failure\n");
438 ok(GetLastError() == ERROR_INVALID_HANDLE,
439 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
441 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
443 SetLastError(0xdeadbeef);
444 ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
445 ok(!ret, "Expected failure\n");
446 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
447 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
448 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
450 SetLastError(0xdeadbeef);
451 displaysize = sizeof(displayname);
452 ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
453 ok(!ret, "Expected failure\n");
454 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
455 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
456 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
458 /* Test for nonexistent service */
459 SetLastError(0xdeadbeef);
460 displaysize = -1;
461 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
462 ok(!ret, "Expected failure\n");
463 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
464 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
466 SetLastError(0xdeadbeef);
467 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
468 ok(!ret, "Expected failure\n");
469 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
470 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
471 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
473 displaysize = 15;
474 strcpy(displayname, "ABC");
475 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
476 ok(!ret, "Expected failure\n");
477 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
478 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
479 todo_wine ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
480 ok(displayname[0] == 0, "Service name not empty\n");
482 displaysize = 15;
483 lstrcpyW( displaynameW, abcW );
484 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
485 ok(!ret, "Expected failure\n");
486 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
487 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
488 ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
489 ok(displaynameW[0] == 0, "Service name not empty\n");
491 displaysize = 0;
492 strcpy(displayname, "ABC");
493 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
494 ok(!ret, "Expected failure\n");
495 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
496 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
497 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
498 ok(displayname[0] == 'A', "Service name changed\n");
500 displaysize = 0;
501 lstrcpyW( displaynameW, abcW );
502 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
503 ok(!ret, "Expected failure\n");
504 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
505 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
506 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
507 ok(displaynameW[0] == 'A', "Service name changed\n");
509 displaysize = 1;
510 strcpy(displayname, "ABC");
511 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
512 ok(!ret, "Expected failure\n");
513 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
514 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
515 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
516 ok(displayname[0] == 0, "Service name not empty\n");
518 displaysize = 1;
519 lstrcpyW( displaynameW, abcW );
520 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
521 ok(!ret, "Expected failure\n");
522 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
523 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
524 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
525 ok(displaynameW[0] == 'A', "Service name changed\n");
527 displaysize = 2;
528 strcpy(displayname, "ABC");
529 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
530 ok(!ret, "Expected failure\n");
531 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
532 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
533 todo_wine ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
534 ok(displayname[0] == 0, "Service name not empty\n");
536 displaysize = 2;
537 lstrcpyW( displaynameW, abcW );
538 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
539 ok(!ret, "Expected failure\n");
540 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
541 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
542 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
543 ok(displaynameW[0] == 0, "Service name not empty\n");
545 /* Check if 'Spooler' exists */
546 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
547 if (!svc_handle)
549 skip("Spooler service doesn't exist\n");
550 CloseServiceHandle(scm_handle);
551 return;
553 CloseServiceHandle(svc_handle);
555 /* Retrieve the needed size for the buffer */
556 SetLastError(0xdeadbeef);
557 displaysize = -1;
558 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
559 ok(!ret, "Expected failure\n");
560 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
561 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
562 tempsize = displaysize;
564 displaysize = 0;
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 ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
571 /* Buffer is too small */
572 SetLastError(0xdeadbeef);
573 displaysize = (tempsize / 2);
574 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
575 ok(!ret, "Expected failure\n");
576 ok(displaysize == tempsize, "Expected the needed buffersize\n");
577 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
578 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
580 /* First try with a buffer that should be big enough to hold
581 * the ANSI string (and terminating character). This succeeds on Windows
582 * although when asked (see above 2 tests) it will return twice the needed size.
584 SetLastError(0xdeadbeef);
585 displaysize = (tempsize / 2) + 1;
586 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
587 ok(ret, "Expected success, got error %u\n", GetLastError());
588 ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
590 /* Now with the original returned size */
591 SetLastError(0xdeadbeef);
592 displaysize = tempsize;
593 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
594 ok(ret, "Expected success, got error %u\n", GetLastError());
595 ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
597 /* And with a bigger than needed buffer */
598 SetLastError(0xdeadbeef);
599 displaysize = tempsize * 2;
600 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
601 ok(ret, "Expected success, got error %u\n", GetLastError());
602 /* Test that shows that if the buffersize is enough, it's not changed */
603 ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
604 ok(strlen(displayname) == tempsize/2,
605 "Expected the buffer to be twice the length of the string\n") ;
607 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
608 SetLastError(0xdeadbeef);
609 displaysize = -1;
610 ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
611 ok(!ret, "Expected failure\n");
612 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
613 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
615 /* Buffer is too small */
616 SetLastError(0xdeadbeef);
617 tempsizeW = displaysize;
618 displaysize = tempsizeW / 2;
619 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
620 ok(!ret, "Expected failure\n");
621 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
622 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
623 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
625 /* Now with the original returned size */
626 SetLastError(0xdeadbeef);
627 displaysize = tempsizeW;
628 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
629 ok(!ret, "Expected failure\n");
630 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
631 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
632 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
634 /* And with a bigger than needed buffer */
635 SetLastError(0xdeadbeef);
636 displaysize = tempsizeW + 1; /* This caters for the null terminating character */
637 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
638 ok(ret, "Expected success, got error %u\n", GetLastError());
639 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
640 ok(lstrlenW(displaynameW) == displaysize,
641 "Expected the buffer to be the length of the string\n") ;
642 ok(tempsize / 2 == tempsizeW,
643 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
645 CloseServiceHandle(scm_handle);
647 /* Test for a service without a displayname (which is valid). This should return
648 * the servicename itself.
650 SetLastError(0xdeadbeef);
651 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
652 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
654 skip("Not enough rights to get a handle to the manager\n");
655 return;
658 SetLastError(0xdeadbeef);
659 svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
660 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
661 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
662 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
663 if (!svc_handle)
665 CloseServiceHandle(scm_handle);
666 return;
669 /* Retrieve the needed size for the buffer */
670 SetLastError(0xdeadbeef);
671 displaysize = -1;
672 ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
673 ok(!ret, "Expected failure\n");
674 ok(displaysize == strlen(servicename) * 2,
675 "Expected the displaysize to be twice the size of the servicename\n");
676 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
677 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
679 /* Buffer is too small */
680 SetLastError(0xdeadbeef);
681 tempsize = displaysize;
682 displaysize = (tempsize / 2);
683 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
684 ok(!ret, "Expected failure\n");
685 ok(displaysize == tempsize, "Expected the needed buffersize\n");
686 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
687 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
689 /* Get the displayname */
690 SetLastError(0xdeadbeef);
691 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
692 ok(ret, "Expected success, got error %u\n", GetLastError());
693 ok(!lstrcmpi(displayname, servicename),
694 "Expected displayname to be %s, got %s\n", servicename, displayname);
696 /* Delete the service */
697 ret = DeleteService(svc_handle);
698 ok(ret, "Expected success (err=%d)\n", GetLastError());
700 CloseServiceHandle(svc_handle);
701 CloseServiceHandle(scm_handle);
703 /* Wait a while. Just in case one of the following tests does a CreateService again */
704 Sleep(1000);
707 static void test_get_servicekeyname(void)
709 SC_HANDLE scm_handle, svc_handle;
710 CHAR servicename[4096];
711 CHAR displayname[4096];
712 WCHAR servicenameW[4096];
713 WCHAR displaynameW[4096];
714 DWORD servicesize, displaysize, tempsize;
715 BOOL ret;
716 static const CHAR deadbeef[] = "Deadbeef";
717 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
718 static const WCHAR abcW[] = {'A','B','C',0};
720 /* Having NULL for the size of the buffer will crash on W2K3 */
722 SetLastError(0xdeadbeef);
723 ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
724 ok(!ret, "Expected failure\n");
725 ok(GetLastError() == ERROR_INVALID_HANDLE,
726 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
728 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
730 servicesize = 200;
731 SetLastError(0xdeadbeef);
732 ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
733 ok(!ret, "Expected failure\n");
734 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
735 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
736 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
737 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
739 /* Valid handle and buffer but no displayname */
740 servicesize = 200;
741 SetLastError(0xdeadbeef);
742 ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
743 ok(!ret, "Expected failure\n");
744 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
745 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
746 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
747 todo_wine ok(servicesize == 200, "Service size expected 1, got %d\n", servicesize);
749 /* Test for nonexistent displayname */
750 SetLastError(0xdeadbeef);
751 ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
752 ok(!ret, "Expected failure\n");
753 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
754 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
755 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
757 servicesize = 15;
758 strcpy(servicename, "ABC");
759 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
760 ok(!ret, "Expected failure\n");
761 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
762 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
763 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
764 ok(servicename[0] == 0, "Service name not empty\n");
766 servicesize = 15;
767 lstrcpyW( servicenameW, abcW );
768 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
769 ok(!ret, "Expected failure\n");
770 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
771 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
772 ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
773 ok(servicenameW[0] == 0, "Service name not empty\n");
775 servicesize = 0;
776 strcpy(servicename, "ABC");
777 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
778 ok(!ret, "Expected failure\n");
779 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
780 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
781 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
782 ok(servicename[0] == 'A', "Service name changed\n");
784 servicesize = 0;
785 lstrcpyW( servicenameW, abcW );
786 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
787 ok(!ret, "Expected failure\n");
788 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
789 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
790 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
791 ok(servicenameW[0] == 'A', "Service name changed\n");
793 servicesize = 1;
794 strcpy(servicename, "ABC");
795 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
796 ok(!ret, "Expected failure\n");
797 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
798 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
799 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
800 ok(servicename[0] == 0, "Service name not empty\n");
802 servicesize = 1;
803 lstrcpyW( servicenameW, abcW );
804 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
805 ok(!ret, "Expected failure\n");
806 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
807 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
808 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
809 ok(servicenameW[0] == 'A', "Service name changed\n");
811 servicesize = 2;
812 strcpy(servicename, "ABC");
813 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
814 ok(!ret, "Expected failure\n");
815 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
816 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
817 todo_wine ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
818 ok(servicename[0] == 0, "Service name not empty\n");
820 servicesize = 2;
821 lstrcpyW( servicenameW, abcW );
822 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
823 ok(!ret, "Expected failure\n");
824 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
825 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
826 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
827 ok(servicenameW[0] == 0, "Service name not empty\n");
829 /* Check if 'Spooler' exists */
830 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
831 if (!svc_handle)
833 skip("Spooler service doesn't exist\n");
834 CloseServiceHandle(scm_handle);
835 return;
837 CloseServiceHandle(svc_handle);
839 /* Get the displayname for the 'Spooler' service */
840 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
841 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
843 /* Retrieve the needed size for the buffer */
844 SetLastError(0xdeadbeef);
845 servicesize = 0;
846 ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
847 ok(!ret, "Expected failure\n");
848 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
849 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
851 /* Valid call with the correct buffersize */
852 SetLastError(0xdeadbeef);
853 tempsize = servicesize;
854 servicesize *= 2;
855 ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
856 ok(ret, "Expected success, got error %u\n", GetLastError());
857 if (ret)
859 ok(strlen(servicename) == tempsize/2,
860 "Expected the buffer to be twice the length of the string\n") ;
861 ok(!lstrcmpi(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
862 ok(servicesize == (tempsize * 2),
863 "Expected servicesize not to change if buffer not insufficient\n") ;
866 MultiByteToWideChar(CP_ACP, 0, displayname, -1, displaynameW, sizeof(displaynameW)/2);
867 SetLastError(0xdeadbeef);
868 servicesize *= 2;
869 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
870 ok(ret, "Expected success, got error %u\n", GetLastError());
871 if (ret)
873 ok(strlen(servicename) == tempsize/2,
874 "Expected the buffer to be twice the length of the string\n") ;
875 ok(servicesize == lstrlenW(servicenameW),
876 "Expected servicesize not to change if buffer not insufficient\n") ;
879 SetLastError(0xdeadbeef);
880 servicesize = 3;
881 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
882 ok(!ret, "Expected failure\n");
883 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
884 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
885 ok(servicenameW[0] == 0, "Buffer not empty\n");
887 CloseServiceHandle(scm_handle);
890 static void test_query_svc(void)
892 SC_HANDLE scm_handle, svc_handle;
893 BOOL ret;
894 SERVICE_STATUS status;
895 SERVICE_STATUS_PROCESS *statusproc;
896 DWORD bufsize, needed;
898 /* All NULL or wrong */
899 SetLastError(0xdeadbeef);
900 ret = QueryServiceStatus(NULL, NULL);
901 ok(!ret, "Expected failure\n");
902 ok(GetLastError() == ERROR_INVALID_HANDLE,
903 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
905 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
907 /* Check if 'Spooler' exists.
908 * Open with not enough rights to query the status.
910 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
911 if (!svc_handle)
913 skip("Spooler service doesn't exist\n");
914 CloseServiceHandle(scm_handle);
915 return;
918 SetLastError(0xdeadbeef);
919 ret = QueryServiceStatus(svc_handle, NULL);
920 ok(!ret, "Expected failure\n");
921 todo_wine
922 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
923 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
924 "Unexpected last error %d\n", GetLastError());
926 SetLastError(0xdeadbeef);
927 ret = QueryServiceStatus(svc_handle, &status);
928 ok(!ret, "Expected failure\n");
929 ok(GetLastError() == ERROR_ACCESS_DENIED,
930 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
932 /* Open the service with just enough rights.
933 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
935 CloseServiceHandle(svc_handle);
936 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
938 SetLastError(0xdeadbeef);
939 ret = QueryServiceStatus(svc_handle, &status);
940 ok(ret, "Expected success, got error %u\n", GetLastError());
942 CloseServiceHandle(svc_handle);
944 /* More or less the same tests for QueryServiceStatusEx */
946 /* Open service with not enough rights to query the status */
947 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
949 /* All NULL or wrong, this proves that info level is checked first */
950 SetLastError(0xdeadbeef);
951 ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
952 ok(!ret, "Expected failure\n");
953 todo_wine
954 ok(GetLastError() == ERROR_INVALID_LEVEL,
955 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
957 /* Passing a NULL parameter for the needed buffer size
958 * will crash on anything but NT4.
961 /* Only info level is correct. It looks like the buffer/size is checked second */
962 SetLastError(0xdeadbeef);
963 ret = pQueryServiceStatusEx(NULL, 0, NULL, 0, &needed);
964 /* NT4 and Wine check the handle first */
965 if (GetLastError() != ERROR_INVALID_HANDLE)
967 ok(!ret, "Expected failure\n");
968 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
969 "Needed buffersize is wrong : %d\n", needed);
970 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
971 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
974 /* Pass a correct buffer and buffersize but a NULL handle */
975 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
976 bufsize = needed;
977 SetLastError(0xdeadbeef);
978 ret = pQueryServiceStatusEx(NULL, 0, (BYTE*)statusproc, bufsize, &needed);
979 ok(!ret, "Expected failure\n");
980 ok(GetLastError() == ERROR_INVALID_HANDLE,
981 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
982 HeapFree(GetProcessHeap(), 0, statusproc);
984 /* Correct handle and info level */
985 SetLastError(0xdeadbeef);
986 ret = pQueryServiceStatusEx(svc_handle, 0, NULL, 0, &needed);
987 /* NT4 doesn't return the needed size */
988 if (GetLastError() != ERROR_INVALID_PARAMETER)
990 ok(!ret, "Expected failure\n");
991 todo_wine
993 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
994 "Needed buffersize is wrong : %d\n", needed);
995 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
996 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1000 /* All parameters are OK but we don't have enough rights */
1001 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1002 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1003 SetLastError(0xdeadbeef);
1004 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
1005 ok(!ret, "Expected failure\n");
1006 ok(GetLastError() == ERROR_ACCESS_DENIED,
1007 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1008 HeapFree(GetProcessHeap(), 0, statusproc);
1010 /* Open the service with just enough rights. */
1011 CloseServiceHandle(svc_handle);
1012 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
1014 /* Everything should be fine now. */
1015 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1016 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1017 SetLastError(0xdeadbeef);
1018 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
1019 ok(ret, "Expected success, got error %u\n", GetLastError());
1020 if (statusproc->dwCurrentState == SERVICE_RUNNING)
1021 ok(statusproc->dwProcessId != 0,
1022 "Expect a process id for this running service\n");
1023 else
1024 ok(statusproc->dwProcessId == 0,
1025 "Expect no process id for this stopped service\n");
1026 HeapFree(GetProcessHeap(), 0, statusproc);
1028 CloseServiceHandle(svc_handle);
1029 CloseServiceHandle(scm_handle);
1032 static void test_enum_svc(void)
1034 SC_HANDLE scm_handle;
1035 BOOL ret;
1036 DWORD bufsize, needed, returned, resume;
1037 DWORD neededW, returnedW;
1038 DWORD tempneeded, tempreturned, missing;
1039 DWORD servicecountactive, servicecountinactive;
1040 ENUM_SERVICE_STATUS *services;
1041 ENUM_SERVICE_STATUSW *servicesW;
1042 ENUM_SERVICE_STATUS_PROCESS *exservices;
1043 UINT i;
1045 /* All NULL or wrong */
1046 SetLastError(0xdeadbeef);
1047 ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1048 ok(!ret, "Expected failure\n");
1049 ok(GetLastError() == ERROR_INVALID_HANDLE,
1050 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1052 SetLastError(0xdeadbeef);
1053 ret = EnumServicesStatusW(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1054 ok(!ret, "Expected failure\n");
1055 ok(GetLastError() == ERROR_INVALID_HANDLE,
1056 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1058 /* Open the service control manager with not enough rights at first */
1059 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1061 /* Valid handle but rest is still NULL or wrong */
1062 SetLastError(0xdeadbeef);
1063 ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1064 ok(!ret, "Expected failure\n");
1065 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1066 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1067 "Unexpected last error %d\n", GetLastError());
1069 SetLastError(0xdeadbeef);
1070 ret = EnumServicesStatusW(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 /* Don't specify the two required pointers */
1077 returned = 0xdeadbeef;
1078 SetLastError(0xdeadbeef);
1079 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1080 ok(!ret, "Expected failure\n");
1081 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1082 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1083 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1084 "Unexpected last error %d\n", GetLastError());
1086 returned = 0xdeadbeef;
1087 SetLastError(0xdeadbeef);
1088 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1089 ok(!ret, "Expected failure\n");
1090 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1091 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1092 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1093 "Unexpected last error %d\n", GetLastError());
1095 /* Don't specify the two required pointers */
1096 needed = 0xdeadbeef;
1097 SetLastError(0xdeadbeef);
1098 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1099 ok(!ret, "Expected failure\n");
1100 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1101 "Expected no change to the needed buffer variable\n");
1102 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1103 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1104 "Unexpected last error %d\n", GetLastError());
1106 needed = 0xdeadbeef;
1107 SetLastError(0xdeadbeef);
1108 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1109 ok(!ret, "Expected failure\n");
1110 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1111 "Expected no change to the needed buffer variable\n");
1112 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1113 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1114 "Unexpected last error %d\n", GetLastError());
1116 /* No valid servicetype and servicestate */
1117 needed = 0xdeadbeef;
1118 returned = 0xdeadbeef;
1119 SetLastError(0xdeadbeef);
1120 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1121 ok(!ret, "Expected failure\n");
1122 ok(needed == 0 || broken(needed != 0), /* nt4 */
1123 "Expected needed buffer size to be set to 0, got %d\n", needed);
1124 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1125 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1126 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1128 needed = 0xdeadbeef;
1129 returned = 0xdeadbeef;
1130 SetLastError(0xdeadbeef);
1131 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1132 ok(!ret, "Expected failure\n");
1133 ok(needed == 0 || broken(needed != 0), /* nt4 */
1134 "Expected needed buffer size to be set to 0, got %d\n", needed);
1135 ok(returned == 0 || broken(returned != 0), /* nt4 */
1136 "Expected number of services to be set to 0, got %d\n", returned);
1137 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1138 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1140 /* No valid servicestate */
1141 needed = 0xdeadbeef;
1142 returned = 0xdeadbeef;
1143 SetLastError(0xdeadbeef);
1144 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1145 ok(!ret, "Expected failure\n");
1146 ok(needed == 0 || broken(needed != 0), /* nt4 */
1147 "Expected needed buffer size to be set to 0, got %d\n", needed);
1148 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1149 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1150 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1152 needed = 0xdeadbeef;
1153 returned = 0xdeadbeef;
1154 SetLastError(0xdeadbeef);
1155 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1156 ok(!ret, "Expected failure\n");
1157 ok(needed == 0 || broken(needed != 0), /* nt4 */
1158 "Expected needed buffer size to be set to 0, got %d\n", needed);
1159 ok(returned == 0 || broken(returned != 0), /* nt4 */
1160 "Expected number of services to be set to 0, got %d\n", returned);
1161 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1162 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1164 /* No valid servicetype */
1165 needed = 0xdeadbeef;
1166 returned = 0xdeadbeef;
1167 SetLastError(0xdeadbeef);
1168 ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1169 ok(!ret, "Expected failure\n");
1170 ok(needed == 0 || broken(needed != 0), /* nt4 */
1171 "Expected needed buffer size to be set to 0, got %d\n", needed);
1172 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1173 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1174 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1176 needed = 0xdeadbeef;
1177 returned = 0xdeadbeef;
1178 SetLastError(0xdeadbeef);
1179 ret = EnumServicesStatusW(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1180 ok(!ret, "Expected failure\n");
1181 ok(needed == 0 || broken(needed != 0), /* nt4 */
1182 "Expected needed buffer size to be set to 0, got %d\n", needed);
1183 ok(returned == 0 || broken(returned != 0), /* nt4 */
1184 "Expected number of services to be set to 0, got %d\n", returned);
1185 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1186 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1188 /* All parameters are correct but our access rights are wrong */
1189 needed = 0xdeadbeef;
1190 returned = 0xdeadbeef;
1191 SetLastError(0xdeadbeef);
1192 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1193 ok(!ret, "Expected failure\n");
1194 ok(needed == 0 || broken(needed != 0), /* nt4 */
1195 "Expected needed buffer size to be set to 0, got %d\n", needed);
1196 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1197 ok(GetLastError() == ERROR_ACCESS_DENIED,
1198 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1200 needed = 0xdeadbeef;
1201 returned = 0xdeadbeef;
1202 SetLastError(0xdeadbeef);
1203 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1204 ok(!ret, "Expected failure\n");
1205 ok(needed == 0 || broken(needed != 0), /* nt4 */
1206 "Expected needed buffer size to be set to 0, got %d\n", needed);
1207 ok(returned == 0 || broken(returned != 0), /* nt4 */
1208 "Expected number of services to be set to 0, got %d\n", returned);
1209 ok(GetLastError() == ERROR_ACCESS_DENIED,
1210 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1212 /* Open the service control manager with the needed rights */
1213 CloseServiceHandle(scm_handle);
1214 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1216 /* All parameters are correct. Request the needed buffer size */
1217 needed = 0xdeadbeef;
1218 returned = 0xdeadbeef;
1219 SetLastError(0xdeadbeef);
1220 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1221 ok(!ret, "Expected failure\n");
1222 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1223 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1224 ok(GetLastError() == ERROR_MORE_DATA,
1225 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1227 /* Test to show we get the same needed buffer size for the W-call */
1228 neededW = 0xdeadbeef;
1229 returnedW = 0xdeadbeef;
1230 SetLastError(0xdeadbeef);
1231 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &neededW, &returnedW, NULL);
1232 ok(!ret, "Expected failure\n");
1233 ok(neededW != 0xdeadbeef && neededW > 0, "Expected the needed buffer size for this one service\n");
1234 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1235 ok(returnedW == 0, "Expected no service returned, got %d\n", returnedW);
1236 ok(GetLastError() == ERROR_MORE_DATA,
1237 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1239 /* Store the needed bytes */
1240 tempneeded = needed;
1242 /* Allocate the correct needed bytes */
1243 services = HeapAlloc(GetProcessHeap(), 0, needed);
1244 bufsize = needed;
1245 needed = 0xdeadbeef;
1246 returned = 0xdeadbeef;
1247 SetLastError(0xdeadbeef);
1248 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1249 services, bufsize, &needed, &returned, NULL);
1250 ok(ret, "Expected success, got error %u\n", GetLastError());
1251 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1252 ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1253 HeapFree(GetProcessHeap(), 0, services);
1255 /* Store the number of returned services */
1256 tempreturned = returned;
1258 servicesW = HeapAlloc(GetProcessHeap(), 0, neededW);
1259 bufsize = neededW;
1260 neededW = 0xdeadbeef;
1261 returnedW = 0xdeadbeef;
1262 SetLastError(0xdeadbeef);
1263 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1264 servicesW, bufsize, &neededW, &returnedW, NULL);
1265 ok(ret, "Expected success, got error %u\n", GetLastError());
1266 ok(neededW == 0, "Expected needed buffer to be 0 as we are done\n");
1267 ok(returnedW != 0xdeadbeef && returnedW > 0, "Expected some returned services\n");
1268 HeapFree(GetProcessHeap(), 0, servicesW);
1270 /* Allocate less than the needed bytes and don't specify a resume handle */
1271 services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1272 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1273 needed = 0xdeadbeef;
1274 returned = 0xdeadbeef;
1275 SetLastError(0xdeadbeef);
1276 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1277 services, bufsize, &needed, &returned, NULL);
1278 ok(!ret, "Expected failure\n");
1279 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1280 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1281 ok(GetLastError() == ERROR_MORE_DATA,
1282 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1284 /* Allocate less than the needed bytes, this time with a correct resume handle */
1285 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1286 needed = 0xdeadbeef;
1287 returned = 0xdeadbeef;
1288 resume = 0;
1289 SetLastError(0xdeadbeef);
1290 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1291 services, bufsize, &needed, &returned, &resume);
1292 ok(!ret, "Expected failure\n");
1293 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1294 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1295 todo_wine ok(resume, "Expected a resume handle\n");
1296 ok(GetLastError() == ERROR_MORE_DATA,
1297 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1299 /* Fetch the missing services but pass a bigger buffer size */
1300 missing = tempreturned - returned;
1301 bufsize = tempneeded;
1302 needed = 0xdeadbeef;
1303 returned = 0xdeadbeef;
1304 SetLastError(0xdeadbeef);
1305 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1306 services, bufsize, &needed, &returned, &resume);
1307 ok(ret, "Expected success, got error %u\n", GetLastError());
1308 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1309 ok(returned == missing, "Expected %u services to be returned\n", missing);
1310 ok(resume == 0, "Expected the resume handle to be 0\n");
1311 HeapFree(GetProcessHeap(), 0, services);
1313 /* See if things add up */
1315 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1316 * and doesn't count the others as inactive. This means that Vista could
1317 * show a total that is greater than the sum of active and inactive
1318 * drivers.
1319 * The number of active and inactive drivers is greatly influenced by the
1320 * time when tests are run, immediately after boot or later for example.
1322 * Both reasons make calculations for drivers not so useful
1325 /* Get the number of active win32 services */
1326 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1327 &needed, &returned, NULL);
1328 services = HeapAlloc(GetProcessHeap(), 0, needed);
1329 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1330 needed, &needed, &returned, NULL);
1331 HeapFree(GetProcessHeap(), 0, services);
1333 servicecountactive = returned;
1335 /* Get the number of inactive win32 services */
1336 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1337 &needed, &returned, NULL);
1338 services = HeapAlloc(GetProcessHeap(), 0, needed);
1339 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1340 needed, &needed, &returned, NULL);
1341 HeapFree(GetProcessHeap(), 0, services);
1343 servicecountinactive = returned;
1345 /* Get the number of win32 services */
1346 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1347 &needed, &returned, NULL);
1348 services = HeapAlloc(GetProcessHeap(), 0, needed);
1349 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1350 needed, &needed, &returned, NULL);
1351 HeapFree(GetProcessHeap(), 0, services);
1353 /* Check if total is the same as active and inactive win32 services */
1354 ok(returned == (servicecountactive + servicecountinactive),
1355 "Something wrong in the calculation\n");
1357 /* Get all drivers and services
1359 * Fetch the status of the last call as failing could make the following tests crash
1360 * on Wine (we don't return anything yet).
1362 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1363 NULL, 0, &needed, &returned, NULL);
1364 services = HeapAlloc(GetProcessHeap(), 0, needed);
1365 ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1366 services, needed, &needed, &returned, NULL);
1368 /* Loop through all those returned drivers and services */
1369 for (i = 0; ret && i < returned; i++)
1371 SERVICE_STATUS status = services[i].ServiceStatus;
1373 /* lpServiceName and lpDisplayName should always be filled */
1374 ok(lstrlenA(services[i].lpServiceName) > 0, "Expected a service name\n");
1375 ok(lstrlenA(services[i].lpDisplayName) > 0, "Expected a display name\n");
1377 /* Decrement the counters to see if the functions calls return the same
1378 * numbers as the contents of these structures.
1380 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1382 if (status.dwCurrentState == SERVICE_STOPPED)
1383 servicecountinactive--;
1384 else
1385 servicecountactive--;
1388 HeapFree(GetProcessHeap(), 0, services);
1390 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1391 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1393 CloseServiceHandle(scm_handle);
1395 /* More or less the same for EnumServicesStatusExA */
1397 /* All NULL or wrong */
1398 SetLastError(0xdeadbeef);
1399 ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1400 ok(!ret, "Expected failure\n");
1401 ok(GetLastError() == ERROR_INVALID_LEVEL,
1402 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1404 /* All NULL or wrong, just the info level is correct */
1405 SetLastError(0xdeadbeef);
1406 ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1407 ok(!ret, "Expected failure\n");
1408 ok(GetLastError() == ERROR_INVALID_HANDLE,
1409 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1411 /* Open the service control manager with not enough rights at first */
1412 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1414 /* Valid handle and info level but rest is still NULL or wrong */
1415 SetLastError(0xdeadbeef);
1416 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1417 ok(!ret, "Expected failure\n");
1418 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1419 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1420 "Unexpected last error %d\n", GetLastError());
1422 /* Don't specify the two required pointers */
1423 needed = 0xdeadbeef;
1424 SetLastError(0xdeadbeef);
1425 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1426 ok(!ret, "Expected failure\n");
1427 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1428 "Expected no change to the needed buffer variable\n");
1429 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1430 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1431 "Unexpected last error %d\n", GetLastError());
1433 /* Don't specify the two required pointers */
1434 returned = 0xdeadbeef;
1435 SetLastError(0xdeadbeef);
1436 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1437 ok(!ret, "Expected failure\n");
1438 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1439 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1440 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1441 "Unexpected last error %d\n", GetLastError());
1443 /* No valid servicetype and servicestate */
1444 needed = 0xdeadbeef;
1445 returned = 0xdeadbeef;
1446 SetLastError(0xdeadbeef);
1447 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1448 ok(!ret, "Expected failure\n");
1449 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1450 ok(needed == 0 || broken(needed != 0), /* nt4 */
1451 "Expected needed buffer size to be set to 0, got %d\n", needed);
1452 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1453 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1455 /* No valid servicestate */
1456 needed = 0xdeadbeef;
1457 returned = 0xdeadbeef;
1458 SetLastError(0xdeadbeef);
1459 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1460 &needed, &returned, NULL, NULL);
1461 ok(!ret, "Expected failure\n");
1462 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1463 ok(needed == 0 || broken(needed != 0), /* nt4 */
1464 "Expected needed buffer size to be set to 0, got %d\n", needed);
1465 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1466 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1468 /* No valid servicetype */
1469 needed = 0xdeadbeef;
1470 returned = 0xdeadbeef;
1471 SetLastError(0xdeadbeef);
1472 ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1473 &needed, &returned, NULL, NULL);
1474 ok(!ret, "Expected failure\n");
1475 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1476 ok(needed == 0 || broken(needed != 0), /* nt4 */
1477 "Expected needed buffer size to be set to 0, got %d\n", needed);
1478 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1479 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1481 /* No valid servicetype and servicestate and unknown service group */
1482 needed = 0xdeadbeef;
1483 returned = 0xdeadbeef;
1484 SetLastError(0xdeadbeef);
1485 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1486 &returned, NULL, "deadbeef_group");
1487 ok(!ret, "Expected failure\n");
1488 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1489 ok(needed == 0 || broken(needed != 0), /* nt4 */
1490 "Expected needed buffer size to be set to 0, got %d\n", needed);
1491 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1492 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1494 /* All parameters are correct but our access rights are wrong */
1495 needed = 0xdeadbeef;
1496 returned = 0xdeadbeef;
1497 SetLastError(0xdeadbeef);
1498 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1499 NULL, 0, &needed, &returned, NULL, NULL);
1500 ok(!ret, "Expected failure\n");
1501 ok(needed == 0 || broken(needed != 0), /* nt4 */
1502 "Expected needed buffer size to be set to 0, got %d\n", needed);
1503 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1504 ok(GetLastError() == ERROR_ACCESS_DENIED,
1505 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1507 /* All parameters are correct, access rights are wrong but the
1508 * group name won't be checked yet.
1510 needed = 0xdeadbeef;
1511 returned = 0xdeadbeef;
1512 SetLastError(0xdeadbeef);
1513 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1514 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1515 ok(!ret, "Expected failure\n");
1516 ok(needed == 0 || broken(needed != 0), /* nt4 */
1517 "Expected needed buffer size to be set to 0, got %d\n", needed);
1518 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1519 ok(GetLastError() == ERROR_ACCESS_DENIED,
1520 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1522 /* Open the service control manager with the needed rights */
1523 CloseServiceHandle(scm_handle);
1524 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1526 /* All parameters are correct and the group will be checked */
1527 needed = 0xdeadbeef;
1528 returned = 0xdeadbeef;
1529 SetLastError(0xdeadbeef);
1530 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1531 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1532 ok(!ret, "Expected failure\n");
1533 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1534 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1535 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1536 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1538 /* TODO: Create a test that makes sure we enumerate all services that don't
1539 * belong to a group. (specifying "").
1542 /* All parameters are correct. Request the needed buffer size */
1543 needed = 0xdeadbeef;
1544 returned = 0xdeadbeef;
1545 SetLastError(0xdeadbeef);
1546 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1547 NULL, 0, &needed, &returned, NULL, NULL);
1548 ok(!ret, "Expected failure\n");
1549 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1550 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1551 ok(GetLastError() == ERROR_MORE_DATA,
1552 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1554 /* Test to show we get the same needed buffer size for the W-call */
1555 neededW = 0xdeadbeef;
1556 ret = pEnumServicesStatusExW(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1557 NULL, 0, &neededW, &returnedW, NULL, NULL);
1558 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1560 /* Store the needed bytes */
1561 tempneeded = needed;
1563 /* Allocate the correct needed bytes */
1564 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1565 bufsize = needed;
1566 needed = 0xdeadbeef;
1567 returned = 0xdeadbeef;
1568 SetLastError(0xdeadbeef);
1569 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1570 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1571 ok(ret, "Expected success, got error %u\n", GetLastError());
1572 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1573 ok(returned == tempreturned, "Expected the same number of service from this function\n");
1574 HeapFree(GetProcessHeap(), 0, exservices);
1576 /* Store the number of returned services */
1577 tempreturned = returned;
1579 /* Allocate less than the needed bytes and don't specify a resume handle */
1580 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1581 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1582 needed = 0xdeadbeef;
1583 returned = 0xdeadbeef;
1584 SetLastError(0xdeadbeef);
1585 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1586 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1587 ok(!ret, "Expected failure\n");
1588 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1589 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1590 ok(GetLastError() == ERROR_MORE_DATA,
1591 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1593 /* Allocate less than the needed bytes, this time with a correct resume handle */
1594 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1595 needed = 0xdeadbeef;
1596 returned = 0xdeadbeef;
1597 resume = 0;
1598 SetLastError(0xdeadbeef);
1599 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1600 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1601 ok(!ret, "Expected failure\n");
1602 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1603 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1604 todo_wine ok(resume, "Expected a resume handle\n");
1605 ok(GetLastError() == ERROR_MORE_DATA,
1606 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1608 /* Fetch that last service but pass a bigger buffer size */
1609 missing = tempreturned - returned;
1610 bufsize = tempneeded;
1611 needed = 0xdeadbeef;
1612 returned = 0xdeadbeef;
1613 SetLastError(0xdeadbeef);
1614 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1615 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1616 ok(ret, "Expected success, got error %u\n", GetLastError());
1617 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1618 ok(returned == missing, "Expected %u services to be returned\n", missing);
1619 ok(resume == 0, "Expected the resume handle to be 0\n");
1620 HeapFree(GetProcessHeap(), 0, exservices);
1622 /* See if things add up */
1624 /* Get the number of active win32 services */
1625 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1626 NULL, 0, &needed, &returned, NULL, NULL);
1627 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1628 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1629 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1630 HeapFree(GetProcessHeap(), 0, exservices);
1632 servicecountactive = returned;
1634 /* Get the number of inactive win32 services */
1635 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1636 NULL, 0, &needed, &returned, NULL, NULL);
1637 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1638 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1639 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1640 HeapFree(GetProcessHeap(), 0, exservices);
1642 servicecountinactive = returned;
1644 /* Get the number of win32 services */
1645 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1646 NULL, 0, &needed, &returned, NULL, NULL);
1647 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1648 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1649 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1650 HeapFree(GetProcessHeap(), 0, exservices);
1652 /* Check if total is the same as active and inactive win32 services */
1653 ok(returned == (servicecountactive + servicecountinactive),
1654 "Something wrong in the calculation\n");
1656 /* Get all drivers and services */
1657 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1658 SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1659 ok(!ret, "Expected failure\n");
1660 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1661 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1662 SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1663 ok(ret, "Expected success %u\n", GetLastError());
1665 /* Loop through all those returned drivers and services */
1666 for (i = 0; i < returned; i++)
1668 SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1670 /* lpServiceName and lpDisplayName should always be filled */
1671 ok(lstrlenA(exservices[i].lpServiceName) > 0, "Expected a service name\n");
1672 ok(lstrlenA(exservices[i].lpDisplayName) > 0, "Expected a display name\n");
1674 /* Decrement the counters to see if the functions calls return the
1675 * same numbers as the contents of these structures.
1676 * Check some process id specifics.
1678 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1680 /* We shouldn't have a process id for drivers */
1681 ok(status.dwProcessId == 0,
1682 "This driver shouldn't have an associated process id\n");
1685 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1687 if (status.dwCurrentState != SERVICE_STOPPED)
1689 /* We expect a process id for every running service */
1690 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1691 exservices[i].lpServiceName);
1693 servicecountactive--;
1695 else
1697 /* We shouldn't have a process id for inactive services */
1698 ok(status.dwProcessId == 0, "Service %s state %u shouldn't have an associated process id\n",
1699 exservices[i].lpServiceName, status.dwCurrentState);
1701 servicecountinactive--;
1705 HeapFree(GetProcessHeap(), 0, exservices);
1707 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1708 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1710 CloseServiceHandle(scm_handle);
1713 static void test_close(void)
1715 SC_HANDLE handle;
1716 BOOL ret;
1718 /* NULL handle */
1719 SetLastError(0xdeadbeef);
1720 ret = CloseServiceHandle(NULL);
1721 ok(!ret, "Expected failure\n");
1722 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1724 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1726 /* Proper call */
1727 handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1728 SetLastError(0xdeadbeef);
1729 ret = CloseServiceHandle(handle);
1730 ok(ret, "Expected success got error %u\n", GetLastError());
1733 static void test_sequence(void)
1735 SC_HANDLE scm_handle, svc_handle;
1736 BOOL ret;
1737 QUERY_SERVICE_CONFIGA *config;
1738 DWORD given, needed;
1739 static const CHAR servicename [] = "Winetest";
1740 static const CHAR displayname [] = "Winetest dummy service";
1741 static const CHAR displayname2[] = "Winetest dummy service (2)";
1742 static const CHAR pathname [] = "we_dont_care.exe";
1743 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1744 static const CHAR password [] = "";
1745 static const CHAR empty [] = "";
1746 static const CHAR localsystem [] = "LocalSystem";
1748 SetLastError(0xdeadbeef);
1749 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1751 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1753 skip("Not enough rights to get a handle to the manager\n");
1754 return;
1756 else
1757 ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1759 if (!scm_handle) return;
1761 /* Create a dummy service */
1762 SetLastError(0xdeadbeef);
1763 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1764 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1765 pathname, NULL, NULL, dependencies, NULL, password);
1767 if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1769 /* We try and open the service and do the rest of the tests. Some could
1770 * fail if the tests were changed between these runs.
1772 trace("Deletion probably didn't work last time\n");
1773 SetLastError(0xdeadbeef);
1774 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1775 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1777 skip("Not enough rights to open the service\n");
1778 CloseServiceHandle(scm_handle);
1779 return;
1781 ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1783 else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1785 skip("Not enough rights to create the service\n");
1786 CloseServiceHandle(scm_handle);
1787 return;
1789 else
1791 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1792 if ((svc_handle != NULL) && (pGetSecurityInfo != NULL))
1794 PSID sidOwner, sidGroup;
1795 PACL dacl, sacl;
1796 PSECURITY_DESCRIPTOR pSD;
1797 HRESULT retval = pGetSecurityInfo(svc_handle,SE_SERVICE,DACL_SECURITY_INFORMATION,&sidOwner,&sidGroup,&dacl,&sacl,&pSD);
1798 todo_wine ok(ERROR_SUCCESS == retval, "Expected GetSecurityInfo to succeed: result %d\n",retval);
1802 if (!svc_handle) return;
1804 /* TODO:
1805 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1806 * that the correct keys are used.
1809 /* Request the size for the buffer */
1810 SetLastError(0xdeadbeef);
1811 ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1812 ok(!ret, "Expected failure\n");
1813 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1815 config = HeapAlloc(GetProcessHeap(), 0, needed);
1816 given = needed;
1817 SetLastError(0xdeadbeef);
1818 ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1819 ok(ret, "Expected success, got error %u\n", GetLastError());
1821 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1822 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1823 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1824 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1825 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1826 ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1827 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1828 ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1829 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1830 /* TODO: Show the double 0 terminated string */
1831 todo_wine
1833 ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1835 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1836 ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1838 ok(ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2", NULL, NULL, NULL, NULL, displayname2),
1839 "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1841 QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1842 config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1843 ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1844 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1845 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1846 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1847 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1848 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1849 ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1850 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1851 ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1852 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1853 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1854 ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1856 SetLastError(0xdeadbeef);
1857 ret = DeleteService(svc_handle);
1858 ok(ret, "Expected success, got error %u\n", GetLastError());
1859 CloseServiceHandle(svc_handle);
1861 /* Wait a while. The following test does a CreateService again */
1862 Sleep(1000);
1864 CloseServiceHandle(scm_handle);
1865 HeapFree(GetProcessHeap(), 0, config);
1868 static void test_queryconfig2(void)
1870 SC_HANDLE scm_handle, svc_handle;
1871 BOOL ret;
1872 DWORD expected, needed;
1873 BYTE buffer[MAX_PATH];
1874 LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
1875 static const CHAR servicename [] = "Winetest";
1876 static const CHAR displayname [] = "Winetest dummy service";
1877 static const CHAR pathname [] = "we_dont_care.exe";
1878 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1879 static const CHAR password [] = "";
1880 static const CHAR description [] = "Description";
1882 if(!pQueryServiceConfig2A)
1884 win_skip("function QueryServiceConfig2A not present\n");
1885 return;
1888 SetLastError(0xdeadbeef);
1889 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1891 if (!scm_handle)
1893 if(GetLastError() == ERROR_ACCESS_DENIED)
1894 skip("Not enough rights to get a handle to the manager\n");
1895 else
1896 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
1897 return;
1900 /* Create a dummy service */
1901 SetLastError(0xdeadbeef);
1902 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1903 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1904 pathname, NULL, NULL, dependencies, NULL, password);
1906 if (!svc_handle)
1908 if(GetLastError() == ERROR_SERVICE_EXISTS)
1910 /* We try and open the service and do the rest of the tests. Some could
1911 * fail if the tests were changed between these runs.
1913 trace("Deletion probably didn't work last time\n");
1914 SetLastError(0xdeadbeef);
1915 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1916 if (!svc_handle)
1918 if(GetLastError() == ERROR_ACCESS_DENIED)
1919 skip("Not enough rights to open the service\n");
1920 else
1921 ok(FALSE, "Could not open the service : %d\n", GetLastError());
1922 CloseServiceHandle(scm_handle);
1923 return;
1926 if (GetLastError() == ERROR_ACCESS_DENIED)
1928 skip("Not enough rights to create the service\n");
1929 CloseServiceHandle(scm_handle);
1930 return;
1932 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1933 if (!svc_handle)
1935 CloseServiceHandle(scm_handle);
1936 return;
1939 SetLastError(0xdeadbeef);
1940 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1941 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1942 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1944 SetLastError(0xdeadbeef);
1945 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1946 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1947 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1949 SetLastError(0xdeadbeef);
1950 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1951 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1952 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1954 SetLastError(0xdeadbeef);
1955 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
1956 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1957 ok((ERROR_INVALID_ADDRESS == GetLastError()) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()),
1958 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1960 SetLastError(0xdeadbeef);
1961 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),NULL);
1962 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1963 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1965 needed = 0;
1966 SetLastError(0xdeadbeef);
1967 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA)-1,&needed);
1968 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1969 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1970 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1972 needed = 0;
1973 pConfig->lpDescription = (LPSTR)0xdeadbeef;
1974 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1975 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1976 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1977 ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
1979 SetLastError(0xdeadbeef);
1980 needed = 0;
1981 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1982 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1983 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1984 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1986 if(!pChangeServiceConfig2A)
1988 win_skip("function ChangeServiceConfig2A not present\n");
1989 goto cleanup;
1992 pConfig->lpDescription = (LPSTR) description;
1993 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
1994 ok(ret, "ChangeServiceConfig2A failed\n");
1995 if (!ret) {
1996 goto cleanup;
1999 SetLastError(0xdeadbeef);
2000 needed = 0;
2001 expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
2002 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2003 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2004 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2005 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2007 SetLastError(0xdeadbeef);
2008 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
2009 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2010 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2012 SetLastError(0xdeadbeef);
2013 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
2014 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2015 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2016 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2018 SetLastError(0xdeadbeef);
2019 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
2020 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2021 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2022 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2024 if(!pQueryServiceConfig2W)
2026 win_skip("function QueryServiceConfig2W not present\n");
2027 goto cleanup;
2029 SetLastError(0xdeadbeef);
2030 needed = 0;
2031 expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
2032 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
2033 ok(!ret, "expected QueryServiceConfig2W to fail\n");
2034 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2035 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2037 SetLastError(0xdeadbeef);
2038 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
2039 ok(ret, "expected QueryServiceConfig2W to succeed\n");
2041 cleanup:
2042 DeleteService(svc_handle);
2044 CloseServiceHandle(svc_handle);
2046 /* Wait a while. The following test does a CreateService again */
2047 Sleep(1000);
2049 CloseServiceHandle(scm_handle);
2052 static void test_refcount(void)
2054 SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
2055 static const CHAR servicename [] = "Winetest";
2056 static const CHAR pathname [] = "we_dont_care.exe";
2057 BOOL ret;
2059 /* Get a handle to the Service Control Manager */
2060 SetLastError(0xdeadbeef);
2061 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2062 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
2064 skip("Not enough rights to get a handle to the manager\n");
2065 return;
2068 /* Create a service */
2069 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2070 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2071 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2072 ok(svc_handle1 != NULL, "Expected success, got error %u\n", GetLastError());
2074 /* Get a handle to this new service */
2075 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2076 ok(svc_handle2 != NULL, "Expected success, got error %u\n", GetLastError());
2078 /* Get another handle to this new service */
2079 svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2080 ok(svc_handle3 != NULL, "Expected success, got error %u\n", GetLastError());
2082 /* Check if we can close the handle to the Service Control Manager */
2083 ret = CloseServiceHandle(scm_handle);
2084 ok(ret, "Expected success (err=%d)\n", GetLastError());
2086 /* Get a new handle to the Service Control Manager */
2087 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2088 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
2090 /* Get a handle to this new service */
2091 svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2092 ok(svc_handle4 != NULL, "Expected success, got error %u\n", GetLastError());
2094 /* Delete the service */
2095 ret = DeleteService(svc_handle4);
2096 ok(ret, "Expected success (err=%d)\n", GetLastError());
2098 /* We cannot create the same service again as it's still marked as 'being deleted'.
2099 * The reason is that we still have 4 open handles to this service even though we
2100 * closed the handle to the Service Control Manager in between.
2102 SetLastError(0xdeadbeef);
2103 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2104 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2105 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2106 todo_wine
2108 ok(!svc_handle5, "Expected failure\n");
2109 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
2110 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2113 /* FIXME: Remove this when Wine is fixed */
2114 if (svc_handle5)
2116 DeleteService(svc_handle5);
2117 CloseServiceHandle(svc_handle5);
2120 /* Close all the handles to the service and try again */
2121 ret = CloseServiceHandle(svc_handle4);
2122 ok(ret, "Expected success (err=%d)\n", GetLastError());
2123 ret = CloseServiceHandle(svc_handle3);
2124 ok(ret, "Expected success (err=%d)\n", GetLastError());
2125 ret = CloseServiceHandle(svc_handle2);
2126 ok(ret, "Expected success (err=%d)\n", GetLastError());
2127 ret = CloseServiceHandle(svc_handle1);
2128 ok(ret, "Expected success (err=%d)\n", GetLastError());
2130 /* Wait a while. Doing a CreateService too soon will result again
2131 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2133 Sleep(1000);
2135 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2136 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2137 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2138 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2139 ok(svc_handle5 != NULL, "Expected success, got error %u\n", GetLastError());
2141 /* Delete the service */
2142 ret = DeleteService(svc_handle5);
2143 ok(ret, "Expected success (err=%d)\n", GetLastError());
2145 /* Wait a while. Just in case one of the following tests does a CreateService again */
2146 Sleep(1000);
2148 CloseServiceHandle(svc_handle5);
2149 CloseServiceHandle(scm_handle);
2152 START_TEST(service)
2154 SC_HANDLE scm_handle;
2156 /* Bail out if we are on win98 */
2157 SetLastError(0xdeadbeef);
2158 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2160 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2162 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2163 return;
2165 CloseServiceHandle(scm_handle);
2167 init_function_pointers();
2169 /* First some parameter checking */
2170 test_open_scm();
2171 test_open_svc();
2172 test_create_delete_svc();
2173 test_get_displayname();
2174 test_get_servicekeyname();
2175 test_query_svc();
2176 test_enum_svc();
2177 test_close();
2178 /* Test the creation, querying and deletion of a service */
2179 test_sequence();
2180 test_queryconfig2();
2181 /* The main reason for this test is to check if any refcounting is used
2182 * and what the rules are
2184 test_refcount();