advapi32: Fix GetServiceDisplayNameW behavior to pass the tests.
[wine.git] / dlls / advapi32 / tests / service.c
blob451a21be0871d399daea1eeebf6f600ccbf7e06c
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"
32 #include "wine/test.h"
34 static const CHAR spooler[] = "Spooler"; /* Should be available on all platforms */
36 static BOOL (WINAPI *pChangeServiceConfig2A)(SC_HANDLE,DWORD,LPVOID);
37 static BOOL (WINAPI *pEnumServicesStatusExA)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
38 DWORD, LPBYTE, DWORD, LPDWORD,
39 LPDWORD, LPDWORD, LPCSTR);
40 static BOOL (WINAPI *pQueryServiceConfig2A)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
41 static BOOL (WINAPI *pQueryServiceConfig2W)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
42 static BOOL (WINAPI *pQueryServiceStatusEx)(SC_HANDLE, SC_STATUS_TYPE, LPBYTE,
43 DWORD, LPDWORD);
45 static void init_function_pointers(void)
47 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
49 pChangeServiceConfig2A = (void*)GetProcAddress(hadvapi32, "ChangeServiceConfig2A");
50 pEnumServicesStatusExA= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExA");
51 pQueryServiceConfig2A= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2A");
52 pQueryServiceConfig2W= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2W");
53 pQueryServiceStatusEx= (void*)GetProcAddress(hadvapi32, "QueryServiceStatusEx");
56 static void test_open_scm(void)
58 SC_HANDLE scm_handle;
60 /* No access rights */
61 SetLastError(0xdeadbeef);
62 scm_handle = OpenSCManagerA(NULL, NULL, 0);
63 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
64 CloseServiceHandle(scm_handle);
66 /* Unknown database name */
67 SetLastError(0xdeadbeef);
68 scm_handle = OpenSCManagerA(NULL, "DoesNotExist", SC_MANAGER_CONNECT);
69 ok(!scm_handle, "Expected failure\n");
70 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
71 CloseServiceHandle(scm_handle); /* Just in case */
73 /* MSDN says only ServiceActive is allowed, or NULL */
74 SetLastError(0xdeadbeef);
75 scm_handle = OpenSCManagerA(NULL, SERVICES_FAILED_DATABASEA, SC_MANAGER_CONNECT);
76 ok(!scm_handle, "Expected failure\n");
77 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
78 CloseServiceHandle(scm_handle); /* Just in case */
80 /* Remote unknown host */
81 SetLastError(0xdeadbeef);
82 scm_handle = OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
83 todo_wine
85 ok(!scm_handle, "Expected failure\n");
86 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* w2k8 */,
87 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
89 CloseServiceHandle(scm_handle); /* Just in case */
91 /* Proper call with an empty hostname */
92 SetLastError(0xdeadbeef);
93 scm_handle = OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
94 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
95 CloseServiceHandle(scm_handle);
97 /* Again a correct one */
98 SetLastError(0xdeadbeef);
99 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
100 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
101 CloseServiceHandle(scm_handle);
104 static void test_open_svc(void)
106 SC_HANDLE scm_handle, svc_handle;
107 CHAR displayname[4096];
108 DWORD displaysize;
110 /* All NULL (invalid access rights) */
111 SetLastError(0xdeadbeef);
112 svc_handle = OpenServiceA(NULL, NULL, 0);
113 ok(!svc_handle, "Expected failure\n");
114 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
116 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
118 /* NULL service */
119 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
120 SetLastError(0xdeadbeef);
121 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
122 ok(!svc_handle, "Expected failure\n");
123 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
124 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
125 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
127 /* Nonexistent service */
128 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
129 SetLastError(0xdeadbeef);
130 svc_handle = OpenServiceA(scm_handle, "deadbeef", GENERIC_READ);
131 ok(!svc_handle, "Expected failure\n");
132 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
133 CloseServiceHandle(scm_handle);
135 /* Proper SCM handle but different access rights */
136 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
137 SetLastError(0xdeadbeef);
138 svc_handle = OpenServiceA(scm_handle, "Spooler", GENERIC_WRITE);
139 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
140 skip("Not enough rights to get a handle to the service\n");
141 else
143 ok(svc_handle != NULL, "Expected success, got error %u\n", GetLastError());
144 CloseServiceHandle(svc_handle);
147 /* Test to show we can't open a service with the displayname */
149 /* Retrieve the needed size for the buffer */
150 displaysize = 0;
151 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
152 /* Get the displayname */
153 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
154 /* Try to open the service with this displayname, unless the displayname equals
155 * the servicename as that would defeat the purpose of this test.
157 if (!lstrcmpi(spooler, displayname))
159 skip("displayname equals servicename\n");
160 CloseServiceHandle(scm_handle);
161 return;
164 SetLastError(0xdeadbeef);
165 svc_handle = OpenServiceA(scm_handle, displayname, GENERIC_READ);
166 ok(!svc_handle, "Expected failure\n");
167 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
168 /* Just in case */
169 CloseServiceHandle(svc_handle);
171 CloseServiceHandle(scm_handle);
174 static void test_create_delete_svc(void)
176 SC_HANDLE scm_handle, svc_handle1;
177 CHAR username[UNLEN + 1], domain[MAX_PATH];
178 DWORD user_size = UNLEN + 1;
179 CHAR account[UNLEN + 3];
180 static const CHAR servicename [] = "Winetest";
181 static const CHAR pathname [] = "we_dont_care.exe";
182 static const CHAR empty [] = "";
183 static const CHAR password [] = "secret";
184 BOOL spooler_exists = FALSE;
185 BOOL ret;
186 CHAR display[4096];
187 DWORD display_size = sizeof(display);
189 /* Get the username and turn it into an account to be used in some tests */
190 GetUserNameA(username, &user_size);
191 /* Get the domainname to cater for that situation */
192 if (GetEnvironmentVariableA("USERDOMAIN", domain, MAX_PATH))
193 sprintf(account, "%s\\%s", domain, username);
194 else
195 sprintf(account, ".\\%s", username);
197 /* All NULL */
198 SetLastError(0xdeadbeef);
199 svc_handle1 = CreateServiceA(NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
200 ok(!svc_handle1, "Expected failure\n");
201 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
203 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
205 /* Only a valid handle to the Service Control Manager */
206 SetLastError(0xdeadbeef);
207 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
208 ok(!svc_handle1, "Expected failure\n");
209 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
210 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
211 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
213 /* Now with a servicename */
214 SetLastError(0xdeadbeef);
215 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
216 ok(!svc_handle1, "Expected failure\n");
217 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
218 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
219 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
221 /* Or just a binary name */
222 SetLastError(0xdeadbeef);
223 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
224 ok(!svc_handle1, "Expected failure\n");
225 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
226 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
227 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
229 /* Both servicename and binary name (We only have connect rights) */
230 SetLastError(0xdeadbeef);
231 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
232 ok(!svc_handle1, "Expected failure\n");
233 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
235 /* They can even be empty at this stage of parameter checking */
236 SetLastError(0xdeadbeef);
237 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
238 ok(!svc_handle1, "Expected failure\n");
239 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
241 SetLastError(0xdeadbeef);
242 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
243 ok(!svc_handle1, "Expected failure\n");
244 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
246 /* Open the Service Control Manager with minimal rights for creation
247 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
249 CloseServiceHandle(scm_handle);
250 SetLastError(0xdeadbeef);
251 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
252 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
254 skip("Not enough rights to get a handle to the manager\n");
255 return;
258 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
260 /* Empty strings for servicename and binary name are checked */
261 SetLastError(0xdeadbeef);
262 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
263 ok(!svc_handle1, "Expected failure\n");
264 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
266 SetLastError(0xdeadbeef);
267 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
268 ok(!svc_handle1, "Expected failure\n");
269 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
271 SetLastError(0xdeadbeef);
272 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
273 ok(!svc_handle1, "Expected failure\n");
274 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
276 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
277 * an ERROR_INVALID_PARAMETER)
279 SetLastError(0xdeadbeef);
280 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
281 SERVICE_DISABLED, 0, empty, NULL, NULL, NULL, NULL, NULL);
282 ok(!svc_handle1, "Expected failure\n");
283 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
285 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
287 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
288 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
290 SetLastError(0xdeadbeef);
291 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS,
292 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
293 ok(!svc_handle1, "Expected failure\n");
294 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
296 /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
297 SetLastError(0xdeadbeef);
298 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_FILE_SYSTEM_DRIVER | SERVICE_INTERACTIVE_PROCESS,
299 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
300 ok(!svc_handle1, "Expected failure\n");
301 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
303 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
304 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
306 SetLastError(0xdeadbeef);
307 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
308 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, account, password);
309 ok(!svc_handle1, "Expected failure\n");
310 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
312 /* Illegal (start-type is not a mask and should only be one of the possibilities)
313 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
314 * it's most likely not the wanted start-type)
316 SetLastError(0xdeadbeef);
317 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS,
318 SERVICE_AUTO_START | SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
319 ok(!svc_handle1, "Expected failure\n");
320 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
322 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
323 SetLastError(0xdeadbeef);
324 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
325 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
326 ok(!svc_handle1, "Expected failure\n");
327 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
329 /* The service already exists (check first, just in case) */
330 svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
331 if (svc_handle1)
333 spooler_exists = TRUE;
334 CloseServiceHandle(svc_handle1);
335 SetLastError(0xdeadbeef);
336 svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
337 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
338 ok(!svc_handle1, "Expected failure\n");
339 ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
341 else
342 skip("Spooler service doesn't exist\n");
344 /* To find an existing displayname we check the 'Spooler' service. Although the registry
345 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
346 * to the servicename and can't be used as well for a new displayname.
348 if (spooler_exists)
350 ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
352 if (!ret)
353 skip("Could not retrieve a displayname for the Spooler service\n");
354 else
356 svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
357 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
358 ok(!svc_handle1, "Expected failure\n");
359 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
360 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
363 else
364 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
366 /* Windows doesn't care about the access rights for creation (which makes
367 * sense as there is no service yet) as long as there are sufficient
368 * rights to the manager.
370 SetLastError(0xdeadbeef);
371 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
372 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
373 ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
375 /* DeleteService however must have proper rights */
376 SetLastError(0xdeadbeef);
377 ret = DeleteService(svc_handle1);
378 ok(!ret, "Expected failure\n");
379 ok(GetLastError() == ERROR_ACCESS_DENIED,
380 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
382 /* Open the service with minimal rights for deletion.
383 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
385 CloseServiceHandle(svc_handle1);
386 svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
388 /* Now that we have the proper rights, we should be able to delete */
389 SetLastError(0xdeadbeef);
390 ret = DeleteService(svc_handle1);
391 ok(ret, "Expected success, got error %u\n", GetLastError());
393 CloseServiceHandle(svc_handle1);
394 CloseServiceHandle(scm_handle);
396 /* Wait a while. One of the following tests also does a CreateService for the
397 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
398 * error if we do this to quick. Vista seems more picky then the others.
400 Sleep(1000);
402 /* And a final NULL check */
403 SetLastError(0xdeadbeef);
404 ret = DeleteService(NULL);
405 ok(!ret, "Expected failure\n");
406 ok(GetLastError() == ERROR_INVALID_HANDLE,
407 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
410 static void test_get_displayname(void)
412 SC_HANDLE scm_handle, svc_handle;
413 BOOL ret;
414 CHAR displayname[4096];
415 WCHAR displaynameW[2048];
416 DWORD displaysize, tempsize, tempsizeW;
417 static const CHAR deadbeef[] = "Deadbeef";
418 static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
419 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
420 static const WCHAR abcW[] = {'A','B','C',0};
421 static const CHAR servicename[] = "Winetest";
422 static const CHAR pathname[] = "we_dont_care.exe";
424 /* Having NULL for the size of the buffer will crash on W2K3 */
426 SetLastError(0xdeadbeef);
427 ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
428 ok(!ret, "Expected failure\n");
429 ok(GetLastError() == ERROR_INVALID_HANDLE,
430 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
432 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
434 SetLastError(0xdeadbeef);
435 ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
436 ok(!ret, "Expected failure\n");
437 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
438 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
439 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
441 SetLastError(0xdeadbeef);
442 displaysize = sizeof(displayname);
443 ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
444 ok(!ret, "Expected failure\n");
445 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
446 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
447 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
449 /* Test for nonexistent service */
450 SetLastError(0xdeadbeef);
451 displaysize = -1;
452 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
453 ok(!ret, "Expected failure\n");
454 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
455 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
457 SetLastError(0xdeadbeef);
458 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
459 ok(!ret, "Expected failure\n");
460 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
461 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
462 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
464 displaysize = 15;
465 strcpy(displayname, "ABC");
466 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
467 ok(!ret, "Expected failure\n");
468 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
469 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
470 todo_wine ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
471 ok(displayname[0] == 0, "Service name not empty\n");
473 displaysize = 15;
474 lstrcpyW( displaynameW, abcW );
475 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &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 ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
480 ok(displaynameW[0] == 0, "Service name not empty\n");
482 displaysize = 0;
483 strcpy(displayname, "ABC");
484 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &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 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
489 ok(displayname[0] == 'A', "Service name changed\n");
491 displaysize = 0;
492 lstrcpyW( displaynameW, abcW );
493 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
494 ok(!ret, "Expected failure\n");
495 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
496 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
497 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
498 ok(displaynameW[0] == 'A', "Service name changed\n");
500 displaysize = 1;
501 strcpy(displayname, "ABC");
502 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
503 ok(!ret, "Expected failure\n");
504 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
505 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
506 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
507 ok(displayname[0] == 0, "Service name not empty\n");
509 displaysize = 1;
510 lstrcpyW( displaynameW, abcW );
511 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
512 ok(!ret, "Expected failure\n");
513 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
514 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
515 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
516 ok(displaynameW[0] == 'A', "Service name changed\n");
518 displaysize = 2;
519 strcpy(displayname, "ABC");
520 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
521 ok(!ret, "Expected failure\n");
522 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
523 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
524 todo_wine ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
525 ok(displayname[0] == 0, "Service name not empty\n");
527 displaysize = 2;
528 lstrcpyW( displaynameW, abcW );
529 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
530 ok(!ret, "Expected failure\n");
531 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
532 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
533 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
534 ok(displaynameW[0] == 0, "Service name not empty\n");
536 /* Check if 'Spooler' exists */
537 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
538 if (!svc_handle)
540 skip("Spooler service doesn't exist\n");
541 CloseServiceHandle(scm_handle);
542 return;
544 CloseServiceHandle(svc_handle);
546 /* Retrieve the needed size for the buffer */
547 SetLastError(0xdeadbeef);
548 displaysize = -1;
549 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
550 ok(!ret, "Expected failure\n");
551 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
552 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
553 tempsize = displaysize;
555 displaysize = 0;
556 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
557 ok(!ret, "Expected failure\n");
558 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
559 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
560 ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
562 /* Buffer is too small */
563 SetLastError(0xdeadbeef);
564 displaysize = (tempsize / 2);
565 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
566 ok(!ret, "Expected failure\n");
567 ok(displaysize == tempsize, "Expected the needed buffersize\n");
568 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
569 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
571 /* First try with a buffer that should be big enough to hold
572 * the ANSI string (and terminating character). This succeeds on Windows
573 * although when asked (see above 2 tests) it will return twice the needed size.
575 SetLastError(0xdeadbeef);
576 displaysize = (tempsize / 2) + 1;
577 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
578 ok(ret, "Expected success, got error %u\n", GetLastError());
579 ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
581 /* Now with the original returned size */
582 SetLastError(0xdeadbeef);
583 displaysize = tempsize;
584 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
585 ok(ret, "Expected success, got error %u\n", GetLastError());
586 ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
588 /* And with a bigger than needed buffer */
589 SetLastError(0xdeadbeef);
590 displaysize = tempsize * 2;
591 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
592 ok(ret, "Expected success, got error %u\n", GetLastError());
593 /* Test that shows that if the buffersize is enough, it's not changed */
594 ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
595 ok(lstrlen(displayname) == tempsize/2,
596 "Expected the buffer to be twice the length of the string\n") ;
598 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
599 SetLastError(0xdeadbeef);
600 displaysize = -1;
601 ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
602 ok(!ret, "Expected failure\n");
603 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
604 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
606 /* Buffer is too small */
607 SetLastError(0xdeadbeef);
608 tempsizeW = displaysize;
609 displaysize = tempsizeW / 2;
610 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
611 ok(!ret, "Expected failure\n");
612 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
613 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
614 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
616 /* Now with the original returned size */
617 SetLastError(0xdeadbeef);
618 displaysize = tempsizeW;
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 /* And with a bigger than needed buffer */
626 SetLastError(0xdeadbeef);
627 displaysize = tempsizeW + 1; /* This caters for the null terminating character */
628 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
629 ok(ret, "Expected success, got error %u\n", GetLastError());
630 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
631 ok(lstrlenW(displaynameW) == displaysize,
632 "Expected the buffer to be the length of the string\n") ;
633 ok(tempsize / 2 == tempsizeW,
634 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
636 CloseServiceHandle(scm_handle);
638 /* Test for a service without a displayname (which is valid). This should return
639 * the servicename itself.
641 SetLastError(0xdeadbeef);
642 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
643 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
645 skip("Not enough rights to get a handle to the manager\n");
646 return;
649 SetLastError(0xdeadbeef);
650 svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
651 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
652 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
653 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
654 if (!svc_handle)
656 CloseServiceHandle(scm_handle);
657 return;
660 /* Retrieve the needed size for the buffer */
661 SetLastError(0xdeadbeef);
662 displaysize = -1;
663 ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
664 ok(!ret, "Expected failure\n");
665 ok(displaysize == lstrlen(servicename) * 2,
666 "Expected the displaysize to be twice the size of the servicename\n");
667 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
668 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
670 /* Buffer is too small */
671 SetLastError(0xdeadbeef);
672 tempsize = displaysize;
673 displaysize = (tempsize / 2);
674 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
675 ok(!ret, "Expected failure\n");
676 ok(displaysize == tempsize, "Expected the needed buffersize\n");
677 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
678 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
680 /* Get the displayname */
681 SetLastError(0xdeadbeef);
682 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
683 ok(ret, "Expected success, got error %u\n", GetLastError());
684 ok(!lstrcmpi(displayname, servicename),
685 "Expected displayname to be %s, got %s\n", servicename, displayname);
687 /* Delete the service */
688 ret = DeleteService(svc_handle);
689 ok(ret, "Expected success (err=%d)\n", GetLastError());
691 CloseServiceHandle(svc_handle);
692 CloseServiceHandle(scm_handle);
694 /* Wait a while. Just in case one of the following tests does a CreateService again */
695 Sleep(1000);
698 static void test_get_servicekeyname(void)
700 SC_HANDLE scm_handle, svc_handle;
701 CHAR servicename[4096];
702 CHAR displayname[4096];
703 WCHAR servicenameW[4096];
704 WCHAR displaynameW[4096];
705 DWORD servicesize, displaysize, tempsize;
706 BOOL ret;
707 static const CHAR deadbeef[] = "Deadbeef";
708 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
709 static const WCHAR abcW[] = {'A','B','C',0};
711 /* Having NULL for the size of the buffer will crash on W2K3 */
713 SetLastError(0xdeadbeef);
714 ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
715 ok(!ret, "Expected failure\n");
716 ok(GetLastError() == ERROR_INVALID_HANDLE,
717 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
719 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
721 servicesize = 200;
722 SetLastError(0xdeadbeef);
723 ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
724 ok(!ret, "Expected failure\n");
725 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
726 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
727 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
728 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
730 /* Valid handle and buffer but no displayname */
731 servicesize = 200;
732 SetLastError(0xdeadbeef);
733 ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
734 ok(!ret, "Expected failure\n");
735 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
736 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
737 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
738 todo_wine ok(servicesize == 200, "Service size expected 1, got %d\n", servicesize);
740 /* Test for nonexistent displayname */
741 SetLastError(0xdeadbeef);
742 ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
743 ok(!ret, "Expected failure\n");
744 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
745 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
746 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
748 servicesize = 15;
749 strcpy(servicename, "ABC");
750 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
751 ok(!ret, "Expected failure\n");
752 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
753 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
754 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
755 ok(servicename[0] == 0, "Service name not empty\n");
757 servicesize = 15;
758 lstrcpyW( servicenameW, abcW );
759 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &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 ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
764 ok(servicenameW[0] == 0, "Service name not empty\n");
766 servicesize = 0;
767 strcpy(servicename, "ABC");
768 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &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 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
773 ok(servicename[0] == 'A', "Service name changed\n");
775 servicesize = 0;
776 lstrcpyW( servicenameW, abcW );
777 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
778 ok(!ret, "Expected failure\n");
779 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
780 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
781 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
782 ok(servicenameW[0] == 'A', "Service name changed\n");
784 servicesize = 1;
785 strcpy(servicename, "ABC");
786 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
787 ok(!ret, "Expected failure\n");
788 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
789 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
790 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
791 ok(servicename[0] == 0, "Service name not empty\n");
793 servicesize = 1;
794 lstrcpyW( servicenameW, abcW );
795 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
796 ok(!ret, "Expected failure\n");
797 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
798 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
799 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
800 ok(servicenameW[0] == 'A', "Service name changed\n");
802 servicesize = 2;
803 strcpy(servicename, "ABC");
804 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
805 ok(!ret, "Expected failure\n");
806 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
807 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
808 todo_wine ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
809 ok(servicename[0] == 0, "Service name not empty\n");
811 servicesize = 2;
812 lstrcpyW( servicenameW, abcW );
813 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
814 ok(!ret, "Expected failure\n");
815 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
816 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
817 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
818 ok(servicenameW[0] == 0, "Service name not empty\n");
820 /* Check if 'Spooler' exists */
821 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
822 if (!svc_handle)
824 skip("Spooler service doesn't exist\n");
825 CloseServiceHandle(scm_handle);
826 return;
828 CloseServiceHandle(svc_handle);
830 /* Get the displayname for the 'Spooler' service */
831 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
832 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
834 /* Retrieve the needed size for the buffer */
835 SetLastError(0xdeadbeef);
836 servicesize = 0;
837 ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
838 ok(!ret, "Expected failure\n");
839 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
840 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
842 /* Valid call with the correct buffersize */
843 SetLastError(0xdeadbeef);
844 tempsize = servicesize;
845 servicesize *= 2;
846 ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
847 ok(ret, "Expected success, got error %u\n", GetLastError());
848 if (ret)
850 ok(lstrlen(servicename) == tempsize/2,
851 "Expected the buffer to be twice the length of the string\n") ;
852 ok(!lstrcmpi(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
853 ok(servicesize == (tempsize * 2),
854 "Expected servicesize not to change if buffer not insufficient\n") ;
857 MultiByteToWideChar(CP_ACP, 0, displayname, -1, displaynameW, sizeof(displaynameW)/2);
858 SetLastError(0xdeadbeef);
859 servicesize *= 2;
860 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
861 ok(ret, "Expected success, got error %u\n", GetLastError());
862 if (ret)
864 ok(lstrlen(servicename) == tempsize/2,
865 "Expected the buffer to be twice the length of the string\n") ;
866 ok(servicesize == lstrlenW(servicenameW),
867 "Expected servicesize not to change if buffer not insufficient\n") ;
870 SetLastError(0xdeadbeef);
871 servicesize = 3;
872 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
873 ok(!ret, "Expected failure\n");
874 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
875 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
876 ok(servicenameW[0] == 0, "Buffer not empty\n");
878 CloseServiceHandle(scm_handle);
881 static void test_query_svc(void)
883 SC_HANDLE scm_handle, svc_handle;
884 BOOL ret;
885 SERVICE_STATUS status;
886 SERVICE_STATUS_PROCESS *statusproc;
887 DWORD bufsize, needed;
889 /* All NULL or wrong */
890 SetLastError(0xdeadbeef);
891 ret = QueryServiceStatus(NULL, NULL);
892 ok(!ret, "Expected failure\n");
893 ok(GetLastError() == ERROR_INVALID_HANDLE,
894 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
896 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
898 /* Check if 'Spooler' exists.
899 * Open with not enough rights to query the status.
901 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
902 if (!svc_handle)
904 skip("Spooler service doesn't exist\n");
905 CloseServiceHandle(scm_handle);
906 return;
909 SetLastError(0xdeadbeef);
910 ret = QueryServiceStatus(svc_handle, NULL);
911 ok(!ret, "Expected failure\n");
912 todo_wine
913 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
914 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
915 "Unexpected last error %d\n", GetLastError());
917 SetLastError(0xdeadbeef);
918 ret = QueryServiceStatus(svc_handle, &status);
919 ok(!ret, "Expected failure\n");
920 ok(GetLastError() == ERROR_ACCESS_DENIED,
921 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
923 /* Open the service with just enough rights.
924 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
926 CloseServiceHandle(svc_handle);
927 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
929 SetLastError(0xdeadbeef);
930 ret = QueryServiceStatus(svc_handle, &status);
931 ok(ret, "Expected success, got error %u\n", GetLastError());
933 CloseServiceHandle(svc_handle);
935 /* More or less the same tests for QueryServiceStatusEx */
937 /* Open service with not enough rights to query the status */
938 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
940 /* All NULL or wrong, this proves that info level is checked first */
941 SetLastError(0xdeadbeef);
942 ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
943 ok(!ret, "Expected failure\n");
944 todo_wine
945 ok(GetLastError() == ERROR_INVALID_LEVEL,
946 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
948 /* Passing a NULL parameter for the needed buffer size
949 * will crash on anything but NT4.
952 /* Only info level is correct. It looks like the buffer/size is checked second */
953 SetLastError(0xdeadbeef);
954 ret = pQueryServiceStatusEx(NULL, 0, NULL, 0, &needed);
955 /* NT4 and Wine check the handle first */
956 if (GetLastError() != ERROR_INVALID_HANDLE)
958 ok(!ret, "Expected failure\n");
959 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
960 "Needed buffersize is wrong : %d\n", needed);
961 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
962 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
965 /* Pass a correct buffer and buffersize but a NULL handle */
966 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
967 bufsize = needed;
968 SetLastError(0xdeadbeef);
969 ret = pQueryServiceStatusEx(NULL, 0, (BYTE*)statusproc, bufsize, &needed);
970 ok(!ret, "Expected failure\n");
971 ok(GetLastError() == ERROR_INVALID_HANDLE,
972 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
973 HeapFree(GetProcessHeap(), 0, statusproc);
975 /* Correct handle and info level */
976 SetLastError(0xdeadbeef);
977 ret = pQueryServiceStatusEx(svc_handle, 0, NULL, 0, &needed);
978 /* NT4 doesn't return the needed size */
979 if (GetLastError() != ERROR_INVALID_PARAMETER)
981 ok(!ret, "Expected failure\n");
982 todo_wine
984 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
985 "Needed buffersize is wrong : %d\n", needed);
986 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
987 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
991 /* All parameters are OK but we don't have enough rights */
992 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
993 bufsize = sizeof(SERVICE_STATUS_PROCESS);
994 SetLastError(0xdeadbeef);
995 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
996 ok(!ret, "Expected failure\n");
997 ok(GetLastError() == ERROR_ACCESS_DENIED,
998 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
999 HeapFree(GetProcessHeap(), 0, statusproc);
1001 /* Open the service with just enough rights. */
1002 CloseServiceHandle(svc_handle);
1003 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
1005 /* Everything should be fine now. */
1006 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1007 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1008 SetLastError(0xdeadbeef);
1009 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
1010 ok(ret, "Expected success, got error %u\n", GetLastError());
1011 if (statusproc->dwCurrentState == SERVICE_RUNNING)
1012 ok(statusproc->dwProcessId != 0,
1013 "Expect a process id for this running service\n");
1014 else
1015 ok(statusproc->dwProcessId == 0,
1016 "Expect no process id for this stopped service\n");
1017 HeapFree(GetProcessHeap(), 0, statusproc);
1019 CloseServiceHandle(svc_handle);
1020 CloseServiceHandle(scm_handle);
1023 static void test_enum_svc(void)
1025 SC_HANDLE scm_handle;
1026 BOOL ret;
1027 DWORD bufsize, needed, returned, resume;
1028 DWORD tempneeded, tempreturned, missing;
1029 DWORD servicecountactive, servicecountinactive;
1030 ENUM_SERVICE_STATUS *services;
1031 ENUM_SERVICE_STATUS_PROCESS *exservices;
1032 INT i;
1034 /* All NULL or wrong */
1035 SetLastError(0xdeadbeef);
1036 ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1037 ok(!ret, "Expected failure\n");
1038 todo_wine
1039 ok(GetLastError() == ERROR_INVALID_HANDLE,
1040 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1042 /* Open the service control manager with not enough rights at first */
1043 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1045 /* Valid handle but rest is still NULL or wrong */
1046 SetLastError(0xdeadbeef);
1047 ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1048 ok(!ret, "Expected failure\n");
1049 todo_wine
1050 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1051 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1052 "Unexpected last error %d\n", GetLastError());
1054 /* Don't specify the two required pointers */
1055 returned = 0xdeadbeef;
1056 SetLastError(0xdeadbeef);
1057 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1058 ok(!ret, "Expected failure\n");
1059 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1060 todo_wine
1061 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1062 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1063 "Unexpected last error %d\n", GetLastError());
1065 /* Don't specify the two required pointers */
1066 needed = 0xdeadbeef;
1067 SetLastError(0xdeadbeef);
1068 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1069 ok(!ret, "Expected failure\n");
1070 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1071 "Expected no change to the needed buffer variable\n");
1072 todo_wine
1073 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1074 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1075 "Unexpected last error %d\n", GetLastError());
1077 /* No valid servicetype and servicestate */
1078 needed = 0xdeadbeef;
1079 returned = 0xdeadbeef;
1080 SetLastError(0xdeadbeef);
1081 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1082 ok(!ret, "Expected failure\n");
1083 todo_wine
1085 ok(needed == 0 || broken(needed != 0), /* nt4 */
1086 "Expected needed buffer size to be set to 0, got %d\n", needed);
1087 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1088 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1089 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1092 /* No valid servicetype and servicestate */
1093 needed = 0xdeadbeef;
1094 returned = 0xdeadbeef;
1095 SetLastError(0xdeadbeef);
1096 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1097 ok(!ret, "Expected failure\n");
1098 todo_wine
1100 ok(needed == 0 || broken(needed != 0), /* nt4 */
1101 "Expected needed buffer size to be set to 0, got %d\n", needed);
1102 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1103 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1104 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1107 /* No valid servicetype and servicestate */
1108 needed = 0xdeadbeef;
1109 returned = 0xdeadbeef;
1110 SetLastError(0xdeadbeef);
1111 ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0,
1112 &needed, &returned, NULL);
1113 ok(!ret, "Expected failure\n");
1114 todo_wine
1116 ok(needed == 0 || broken(needed != 0), /* nt4 */
1117 "Expected needed buffer size to be set to 0, got %d\n", needed);
1118 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1119 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1120 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1123 /* All parameters are correct but our access rights are wrong */
1124 needed = 0xdeadbeef;
1125 returned = 0xdeadbeef;
1126 SetLastError(0xdeadbeef);
1127 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1128 &needed, &returned, NULL);
1129 ok(!ret, "Expected failure\n");
1130 todo_wine
1132 ok(needed == 0 || broken(needed != 0), /* nt4 */
1133 "Expected needed buffer size to be set to 0, got %d\n", needed);
1134 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1136 ok(GetLastError() == ERROR_ACCESS_DENIED,
1137 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1139 /* Open the service control manager with the needed rights */
1140 CloseServiceHandle(scm_handle);
1141 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1143 /* All parameters are correct. Request the needed buffer size */
1144 needed = 0xdeadbeef;
1145 returned = 0xdeadbeef;
1146 SetLastError(0xdeadbeef);
1147 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1148 &needed, &returned, NULL);
1149 ok(!ret, "Expected failure\n");
1150 todo_wine
1152 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1153 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1154 ok(GetLastError() == ERROR_MORE_DATA,
1155 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1158 /* Store the needed bytes */
1159 tempneeded = needed;
1161 /* Allocate the correct needed bytes */
1162 services = HeapAlloc(GetProcessHeap(), 0, needed);
1163 bufsize = needed;
1164 needed = 0xdeadbeef;
1165 returned = 0xdeadbeef;
1166 SetLastError(0xdeadbeef);
1167 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1168 services, bufsize, &needed, &returned, NULL);
1169 todo_wine
1171 ok(ret, "Expected success, got error %u\n", GetLastError());
1172 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1173 ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1175 HeapFree(GetProcessHeap(), 0, services);
1177 /* Store the number of returned services */
1178 tempreturned = returned;
1180 /* Allocate less than the needed bytes and don't specify a resume handle */
1181 services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1182 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1183 needed = 0xdeadbeef;
1184 returned = 0xdeadbeef;
1185 SetLastError(0xdeadbeef);
1186 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1187 services, bufsize, &needed, &returned, NULL);
1188 ok(!ret, "Expected failure\n");
1189 todo_wine
1191 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1192 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1193 ok(GetLastError() == ERROR_MORE_DATA,
1194 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1197 /* Allocate less than the needed bytes, this time with a correct resume handle */
1198 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1199 needed = 0xdeadbeef;
1200 returned = 0xdeadbeef;
1201 resume = 0;
1202 SetLastError(0xdeadbeef);
1203 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1204 services, bufsize, &needed, &returned, &resume);
1205 ok(!ret, "Expected failure\n");
1206 todo_wine
1208 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1209 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1210 ok(resume, "Expected a resume handle\n");
1211 ok(GetLastError() == ERROR_MORE_DATA,
1212 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1215 /* Fetch the missing services but pass a bigger buffer size */
1216 missing = tempreturned - returned;
1217 bufsize = tempneeded;
1218 needed = 0xdeadbeef;
1219 returned = 0xdeadbeef;
1220 SetLastError(0xdeadbeef);
1221 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1222 services, bufsize, &needed, &returned, &resume);
1223 todo_wine
1225 ok(ret, "Expected success, got error %u\n", GetLastError());
1226 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1227 ok(returned == missing, "Expected %u services to be returned\n", missing);
1229 ok(resume == 0, "Expected the resume handle to be 0\n");
1230 HeapFree(GetProcessHeap(), 0, services);
1232 /* See if things add up */
1234 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1235 * and doesn't count the others as inactive. This means that Vista could
1236 * show a total that is greater than the sum of active and inactive
1237 * drivers.
1238 * The number of active and inactive drivers is greatly influenced by the
1239 * time when tests are run, immediately after boot or later for example.
1241 * Both reasons make calculations for drivers not so useful
1244 /* Get the number of active win32 services */
1245 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1246 &needed, &returned, NULL);
1247 services = HeapAlloc(GetProcessHeap(), 0, needed);
1248 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1249 needed, &needed, &returned, NULL);
1250 HeapFree(GetProcessHeap(), 0, services);
1252 servicecountactive = returned;
1254 /* Get the number of inactive win32 services */
1255 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1256 &needed, &returned, NULL);
1257 services = HeapAlloc(GetProcessHeap(), 0, needed);
1258 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1259 needed, &needed, &returned, NULL);
1260 HeapFree(GetProcessHeap(), 0, services);
1262 servicecountinactive = returned;
1264 /* Get the number of win32 services */
1265 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1266 &needed, &returned, NULL);
1267 services = HeapAlloc(GetProcessHeap(), 0, needed);
1268 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1269 needed, &needed, &returned, NULL);
1270 HeapFree(GetProcessHeap(), 0, services);
1272 /* Check if total is the same as active and inactive win32 services */
1273 todo_wine
1274 ok(returned == (servicecountactive + servicecountinactive),
1275 "Something wrong in the calculation\n");
1277 /* Get all drivers and services
1279 * Fetch the status of the last call as failing could make the following tests crash
1280 * on Wine (we don't return anything yet).
1282 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1283 NULL, 0, &needed, &returned, NULL);
1284 services = HeapAlloc(GetProcessHeap(), 0, needed);
1285 ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1286 services, needed, &needed, &returned, NULL);
1288 /* Loop through all those returned drivers and services */
1289 for (i = 0; ret && i < returned; i++)
1291 SERVICE_STATUS status = services[i].ServiceStatus;
1293 /* lpServiceName and lpDisplayName should always be filled */
1294 ok(lstrlenA(services[i].lpServiceName) > 0, "Expected a service name\n");
1295 ok(lstrlenA(services[i].lpDisplayName) > 0, "Expected a display name\n");
1297 /* Decrement the counters to see if the functions calls return the same
1298 * numbers as the contents of these structures.
1300 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1302 if (status.dwCurrentState == SERVICE_RUNNING)
1303 servicecountactive--;
1304 else
1305 servicecountinactive--;
1308 HeapFree(GetProcessHeap(), 0, services);
1310 todo_wine
1312 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1313 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1316 CloseServiceHandle(scm_handle);
1318 /* More or less the same for EnumServicesStatusExA */
1320 /* All NULL or wrong */
1321 SetLastError(0xdeadbeef);
1322 ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1323 ok(!ret, "Expected failure\n");
1324 todo_wine
1325 ok(GetLastError() == ERROR_INVALID_LEVEL,
1326 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1328 /* All NULL or wrong, just the info level is correct */
1329 SetLastError(0xdeadbeef);
1330 ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1331 ok(!ret, "Expected failure\n");
1332 todo_wine
1333 ok(GetLastError() == ERROR_INVALID_HANDLE,
1334 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1336 /* Open the service control manager with not enough rights at first */
1337 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1339 /* Valid handle and info level but rest is still NULL or wrong */
1340 SetLastError(0xdeadbeef);
1341 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1342 ok(!ret, "Expected failure\n");
1343 todo_wine
1344 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1345 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1346 "Unexpected last error %d\n", GetLastError());
1348 /* Don't specify the two required pointers */
1349 needed = 0xdeadbeef;
1350 SetLastError(0xdeadbeef);
1351 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1352 ok(!ret, "Expected failure\n");
1353 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1354 "Expected no change to the needed buffer variable\n");
1355 todo_wine
1356 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1357 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1358 "Unexpected last error %d\n", GetLastError());
1360 /* Don't specify the two required pointers */
1361 returned = 0xdeadbeef;
1362 SetLastError(0xdeadbeef);
1363 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1364 ok(!ret, "Expected failure\n");
1365 todo_wine
1367 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1368 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1369 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1370 "Unexpected last error %d\n", GetLastError());
1373 /* No valid servicetype and servicestate */
1374 needed = 0xdeadbeef;
1375 returned = 0xdeadbeef;
1376 SetLastError(0xdeadbeef);
1377 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1378 ok(!ret, "Expected failure\n");
1379 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1380 todo_wine
1382 ok(needed == 0 || broken(needed != 0), /* nt4 */
1383 "Expected needed buffer size to be set to 0, got %d\n", needed);
1384 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1385 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1388 /* No valid servicestate */
1389 needed = 0xdeadbeef;
1390 returned = 0xdeadbeef;
1391 SetLastError(0xdeadbeef);
1392 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1393 &needed, &returned, NULL, NULL);
1394 ok(!ret, "Expected failure\n");
1395 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1396 todo_wine
1398 ok(needed == 0 || broken(needed != 0), /* nt4 */
1399 "Expected needed buffer size to be set to 0, got %d\n", needed);
1400 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1401 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1404 /* No valid servicetype */
1405 needed = 0xdeadbeef;
1406 returned = 0xdeadbeef;
1407 SetLastError(0xdeadbeef);
1408 ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1409 &needed, &returned, NULL, NULL);
1410 ok(!ret, "Expected failure\n");
1411 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1412 todo_wine
1414 ok(needed == 0 || broken(needed != 0), /* nt4 */
1415 "Expected needed buffer size to be set to 0, got %d\n", needed);
1416 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1417 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1420 /* No valid servicetype and servicestate and unknown service group */
1421 needed = 0xdeadbeef;
1422 returned = 0xdeadbeef;
1423 SetLastError(0xdeadbeef);
1424 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1425 &returned, NULL, "deadbeef_group");
1426 ok(!ret, "Expected failure\n");
1427 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1428 todo_wine
1430 ok(needed == 0 || broken(needed != 0), /* nt4 */
1431 "Expected needed buffer size to be set to 0, got %d\n", needed);
1432 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1433 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1436 /* All parameters are correct but our access rights are wrong */
1437 needed = 0xdeadbeef;
1438 returned = 0xdeadbeef;
1439 SetLastError(0xdeadbeef);
1440 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1441 NULL, 0, &needed, &returned, NULL, NULL);
1442 ok(!ret, "Expected failure\n");
1443 todo_wine
1444 ok(needed == 0 || broken(needed != 0), /* nt4 */
1445 "Expected needed buffer size to be set to 0, got %d\n", needed);
1446 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1447 ok(GetLastError() == ERROR_ACCESS_DENIED,
1448 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1450 /* All parameters are correct, access rights are wrong but the
1451 * group name won't be checked yet.
1453 needed = 0xdeadbeef;
1454 returned = 0xdeadbeef;
1455 SetLastError(0xdeadbeef);
1456 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1457 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1458 ok(!ret, "Expected failure\n");
1459 todo_wine
1460 ok(needed == 0 || broken(needed != 0), /* nt4 */
1461 "Expected needed buffer size to be set to 0, got %d\n", needed);
1462 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1463 ok(GetLastError() == ERROR_ACCESS_DENIED,
1464 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1466 /* Open the service control manager with the needed rights */
1467 CloseServiceHandle(scm_handle);
1468 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1470 /* All parameters are correct and the group will be checked */
1471 needed = 0xdeadbeef;
1472 returned = 0xdeadbeef;
1473 SetLastError(0xdeadbeef);
1474 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1475 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1476 ok(!ret, "Expected failure\n");
1477 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1478 todo_wine
1480 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1481 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1482 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1485 /* TODO: Create a test that makes sure we enumerate all services that don't
1486 * belong to a group. (specifying "").
1489 /* All parameters are correct. Request the needed buffer size */
1490 needed = 0xdeadbeef;
1491 returned = 0xdeadbeef;
1492 SetLastError(0xdeadbeef);
1493 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1494 NULL, 0, &needed, &returned, NULL, NULL);
1495 ok(!ret, "Expected failure\n");
1496 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1497 todo_wine
1499 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1500 ok(GetLastError() == ERROR_MORE_DATA,
1501 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1504 /* Store the needed bytes */
1505 tempneeded = needed;
1507 /* Allocate the correct needed bytes */
1508 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1509 bufsize = needed;
1510 needed = 0xdeadbeef;
1511 returned = 0xdeadbeef;
1512 SetLastError(0xdeadbeef);
1513 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1514 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1515 todo_wine
1517 ok(ret, "Expected success, got error %u\n", GetLastError());
1518 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1519 ok(returned == tempreturned, "Expected the same number of service from this function\n");
1521 HeapFree(GetProcessHeap(), 0, exservices);
1523 /* Store the number of returned services */
1524 tempreturned = returned;
1526 /* Allocate less than the needed bytes and don't specify a resume handle */
1527 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1528 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1529 needed = 0xdeadbeef;
1530 returned = 0xdeadbeef;
1531 SetLastError(0xdeadbeef);
1532 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1533 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1534 ok(!ret, "Expected failure\n");
1535 todo_wine
1537 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1538 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1539 ok(GetLastError() == ERROR_MORE_DATA,
1540 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1543 /* Allocate less than the needed bytes, this time with a correct resume handle */
1544 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1545 needed = 0xdeadbeef;
1546 returned = 0xdeadbeef;
1547 resume = 0;
1548 SetLastError(0xdeadbeef);
1549 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1550 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1551 ok(!ret, "Expected failure\n");
1552 todo_wine
1554 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1555 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1556 ok(resume, "Expected a resume handle\n");
1557 ok(GetLastError() == ERROR_MORE_DATA,
1558 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1561 /* Fetch that last service but pass a bigger buffer size */
1562 missing = tempreturned - returned;
1563 bufsize = tempneeded;
1564 needed = 0xdeadbeef;
1565 returned = 0xdeadbeef;
1566 SetLastError(0xdeadbeef);
1567 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1568 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1569 todo_wine
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");
1574 ok(returned == missing, "Expected %u services to be returned\n", missing);
1575 ok(resume == 0, "Expected the resume handle to be 0\n");
1576 HeapFree(GetProcessHeap(), 0, exservices);
1578 /* See if things add up */
1580 /* Get the number of active win32 services */
1581 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1582 NULL, 0, &needed, &returned, NULL, NULL);
1583 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1584 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1585 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1586 HeapFree(GetProcessHeap(), 0, exservices);
1588 servicecountactive = returned;
1590 /* Get the number of inactive win32 services */
1591 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1592 NULL, 0, &needed, &returned, NULL, NULL);
1593 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1594 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1595 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1596 HeapFree(GetProcessHeap(), 0, exservices);
1598 servicecountinactive = returned;
1600 /* Get the number of win32 services */
1601 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1602 NULL, 0, &needed, &returned, NULL, NULL);
1603 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1604 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1605 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1606 HeapFree(GetProcessHeap(), 0, exservices);
1608 /* Check if total is the same as active and inactive win32 services */
1609 ok(returned == (servicecountactive + servicecountinactive),
1610 "Something wrong in the calculation\n");
1612 /* Get all drivers and services */
1613 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1614 SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1615 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1616 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1617 SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1619 /* Loop through all those returned drivers and services */
1620 for (i = 0; i < returned; i++)
1622 SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1625 /* lpServiceName and lpDisplayName should always be filled */
1626 ok(lstrlenA(exservices[i].lpServiceName) > 0, "Expected a service name\n");
1627 ok(lstrlenA(exservices[i].lpDisplayName) > 0, "Expected a display name\n");
1629 /* Decrement the counters to see if the functions calls return the
1630 * same numbers as the contents of these structures.
1631 * Check some process id specifics.
1633 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1635 /* We shouldn't have a process id for drivers */
1636 ok(status.dwProcessId == 0,
1637 "This driver shouldn't have an associated process id\n");
1640 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1642 if (status.dwCurrentState == SERVICE_RUNNING)
1644 /* We expect a process id for every running service */
1645 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1646 exservices[i].lpServiceName);
1648 servicecountactive--;
1650 else
1652 /* We shouldn't have a process id for inactive services */
1653 ok(status.dwProcessId == 0, "Service %s state %u shouldn't have an associated process id\n",
1654 exservices[i].lpServiceName, status.dwCurrentState);
1656 servicecountinactive--;
1660 HeapFree(GetProcessHeap(), 0, exservices);
1662 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1663 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1665 CloseServiceHandle(scm_handle);
1668 static void test_close(void)
1670 SC_HANDLE handle;
1671 BOOL ret;
1673 /* NULL handle */
1674 SetLastError(0xdeadbeef);
1675 ret = CloseServiceHandle(NULL);
1676 ok(!ret, "Expected failure\n");
1677 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1679 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1681 /* Proper call */
1682 handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1683 SetLastError(0xdeadbeef);
1684 ret = CloseServiceHandle(handle);
1685 ok(ret, "Expected success got error %u\n", GetLastError());
1688 static void test_sequence(void)
1690 SC_HANDLE scm_handle, svc_handle;
1691 BOOL ret;
1692 QUERY_SERVICE_CONFIGA *config;
1693 DWORD given, needed;
1694 static const CHAR servicename [] = "Winetest";
1695 static const CHAR displayname [] = "Winetest dummy service";
1696 static const CHAR displayname2[] = "Winetest dummy service (2)";
1697 static const CHAR pathname [] = "we_dont_care.exe";
1698 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1699 static const CHAR password [] = "";
1700 static const CHAR empty [] = "";
1701 static const CHAR localsystem [] = "LocalSystem";
1703 SetLastError(0xdeadbeef);
1704 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1706 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1708 skip("Not enough rights to get a handle to the manager\n");
1709 return;
1711 else
1712 ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1714 if (!scm_handle) return;
1716 /* Create a dummy service */
1717 SetLastError(0xdeadbeef);
1718 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1719 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1720 pathname, NULL, NULL, dependencies, NULL, password);
1722 if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1724 /* We try and open the service and do the rest of the tests. Some could
1725 * fail if the tests were changed between these runs.
1727 trace("Deletion probably didn't work last time\n");
1728 SetLastError(0xdeadbeef);
1729 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1730 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1732 skip("Not enough rights to open the service\n");
1733 CloseServiceHandle(scm_handle);
1734 return;
1736 ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1738 else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1740 skip("Not enough rights to create the service\n");
1741 CloseServiceHandle(scm_handle);
1742 return;
1744 else
1745 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1747 if (!svc_handle) return;
1749 /* TODO:
1750 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1751 * that the correct keys are used.
1754 /* Request the size for the buffer */
1755 SetLastError(0xdeadbeef);
1756 ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1757 ok(!ret, "Expected failure\n");
1758 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1760 config = HeapAlloc(GetProcessHeap(), 0, needed);
1761 given = needed;
1762 SetLastError(0xdeadbeef);
1763 ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1764 ok(ret, "Expected success, got error %u\n", GetLastError());
1765 todo_wine
1767 ok(given == needed, "Expected the given (%d) and needed (%d) buffersizes to be equal\n", given, needed);
1769 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1770 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1771 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1772 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1773 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1774 ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1775 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1776 ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1777 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1778 /* TODO: Show the double 0 terminated string */
1779 todo_wine
1781 ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1783 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1784 ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1786 ok(ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2", NULL, NULL, NULL, NULL, displayname2),
1787 "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1789 QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1790 config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1791 ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1792 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1793 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1794 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1795 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1796 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1797 ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1798 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1799 ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1800 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1801 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1802 ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1804 SetLastError(0xdeadbeef);
1805 ret = DeleteService(svc_handle);
1806 ok(ret, "Expected success, got error %u\n", GetLastError());
1807 CloseServiceHandle(svc_handle);
1809 /* Wait a while. The following test does a CreateService again */
1810 Sleep(1000);
1812 CloseServiceHandle(scm_handle);
1813 HeapFree(GetProcessHeap(), 0, config);
1816 static void test_queryconfig2(void)
1818 SC_HANDLE scm_handle, svc_handle;
1819 BOOL ret;
1820 DWORD expected, needed;
1821 BYTE buffer[MAX_PATH];
1822 LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
1823 static const CHAR servicename [] = "Winetest";
1824 static const CHAR displayname [] = "Winetest dummy service";
1825 static const CHAR pathname [] = "we_dont_care.exe";
1826 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1827 static const CHAR password [] = "";
1828 static const CHAR description [] = "Description";
1830 if(!pQueryServiceConfig2A)
1832 skip("function QueryServiceConfig2A not present\n");
1833 return;
1836 SetLastError(0xdeadbeef);
1837 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1839 if (!scm_handle)
1841 if(GetLastError() == ERROR_ACCESS_DENIED)
1842 skip("Not enough rights to get a handle to the manager\n");
1843 else
1844 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
1845 return;
1848 /* Create a dummy service */
1849 SetLastError(0xdeadbeef);
1850 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1851 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1852 pathname, NULL, NULL, dependencies, NULL, password);
1854 if (!svc_handle)
1856 if(GetLastError() == ERROR_SERVICE_EXISTS)
1858 /* We try and open the service and do the rest of the tests. Some could
1859 * fail if the tests were changed between these runs.
1861 trace("Deletion probably didn't work last time\n");
1862 SetLastError(0xdeadbeef);
1863 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1864 if (!svc_handle)
1866 if(GetLastError() == ERROR_ACCESS_DENIED)
1867 skip("Not enough rights to open the service\n");
1868 else
1869 ok(FALSE, "Could not open the service : %d\n", GetLastError());
1870 CloseServiceHandle(scm_handle);
1871 return;
1874 if (GetLastError() == ERROR_ACCESS_DENIED)
1876 skip("Not enough rights to create the service\n");
1877 CloseServiceHandle(scm_handle);
1878 return;
1880 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1881 if (!svc_handle)
1883 CloseServiceHandle(scm_handle);
1884 return;
1887 SetLastError(0xdeadbeef);
1888 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1889 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1890 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1892 SetLastError(0xdeadbeef);
1893 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1894 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1895 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1897 SetLastError(0xdeadbeef);
1898 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1899 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1900 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1902 SetLastError(0xdeadbeef);
1903 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
1904 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1905 ok((ERROR_INVALID_ADDRESS == GetLastError()) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()),
1906 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1908 SetLastError(0xdeadbeef);
1909 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),NULL);
1910 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1911 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1913 needed = 0;
1914 SetLastError(0xdeadbeef);
1915 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA)-1,&needed);
1916 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1917 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1918 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1920 needed = 0;
1921 pConfig->lpDescription = (LPSTR)0xdeadbeef;
1922 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1923 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1924 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1925 ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
1927 SetLastError(0xdeadbeef);
1928 needed = 0;
1929 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1930 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1931 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1932 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1934 if(!pChangeServiceConfig2A)
1936 skip("function ChangeServiceConfig2A not present\n");
1937 goto cleanup;
1940 pConfig->lpDescription = (LPSTR) description;
1941 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
1942 ok(ret, "ChangeServiceConfig2A failed\n");
1943 if (!ret) {
1944 goto cleanup;
1947 SetLastError(0xdeadbeef);
1948 needed = 0;
1949 expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
1950 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1951 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1952 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1953 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
1955 SetLastError(0xdeadbeef);
1956 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
1957 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1958 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1960 SetLastError(0xdeadbeef);
1961 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
1962 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1963 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
1964 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
1966 SetLastError(0xdeadbeef);
1967 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
1968 ok(ret, "expected QueryServiceConfig2A to succeed\n");
1969 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
1970 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
1972 if(!pQueryServiceConfig2W)
1974 skip("function QueryServiceConfig2W not present\n");
1975 goto cleanup;
1977 SetLastError(0xdeadbeef);
1978 needed = 0;
1979 expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
1980 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1981 ok(!ret, "expected QueryServiceConfig2W to fail\n");
1982 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1983 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
1985 SetLastError(0xdeadbeef);
1986 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
1987 ok(ret, "expected QueryServiceConfig2W to succeed\n");
1989 cleanup:
1990 DeleteService(svc_handle);
1992 CloseServiceHandle(svc_handle);
1994 /* Wait a while. The following test does a CreateService again */
1995 Sleep(1000);
1997 CloseServiceHandle(scm_handle);
2000 static void test_refcount(void)
2002 SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
2003 static const CHAR servicename [] = "Winetest";
2004 static const CHAR pathname [] = "we_dont_care.exe";
2005 BOOL ret;
2007 /* Get a handle to the Service Control Manager */
2008 SetLastError(0xdeadbeef);
2009 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2010 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
2012 skip("Not enough rights to get a handle to the manager\n");
2013 return;
2016 /* Create a service */
2017 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2018 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2019 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2020 ok(svc_handle1 != NULL, "Expected success, got error %u\n", GetLastError());
2022 /* Get a handle to this new service */
2023 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2024 ok(svc_handle2 != NULL, "Expected success, got error %u\n", GetLastError());
2026 /* Get another handle to this new service */
2027 svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2028 ok(svc_handle3 != NULL, "Expected success, got error %u\n", GetLastError());
2030 /* Check if we can close the handle to the Service Control Manager */
2031 ret = CloseServiceHandle(scm_handle);
2032 ok(ret, "Expected success (err=%d)\n", GetLastError());
2034 /* Get a new handle to the Service Control Manager */
2035 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2036 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
2038 /* Get a handle to this new service */
2039 svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2040 ok(svc_handle4 != NULL, "Expected success, got error %u\n", GetLastError());
2042 /* Delete the service */
2043 ret = DeleteService(svc_handle4);
2044 ok(ret, "Expected success (err=%d)\n", GetLastError());
2046 /* We cannot create the same service again as it's still marked as 'being deleted'.
2047 * The reason is that we still have 4 open handles to this service even though we
2048 * closed the handle to the Service Control Manager in between.
2050 SetLastError(0xdeadbeef);
2051 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2052 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2053 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2054 todo_wine
2056 ok(!svc_handle5, "Expected failure\n");
2057 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
2058 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2061 /* FIXME: Remove this when Wine is fixed */
2062 if (svc_handle5)
2064 DeleteService(svc_handle5);
2065 CloseServiceHandle(svc_handle5);
2068 /* Close all the handles to the service and try again */
2069 ret = CloseServiceHandle(svc_handle4);
2070 ok(ret, "Expected success (err=%d)\n", GetLastError());
2071 ret = CloseServiceHandle(svc_handle3);
2072 ok(ret, "Expected success (err=%d)\n", GetLastError());
2073 ret = CloseServiceHandle(svc_handle2);
2074 ok(ret, "Expected success (err=%d)\n", GetLastError());
2075 ret = CloseServiceHandle(svc_handle1);
2076 ok(ret, "Expected success (err=%d)\n", GetLastError());
2078 /* Wait a while. Doing a CreateService too soon will result again
2079 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2081 Sleep(1000);
2083 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2084 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2085 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2086 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2087 ok(svc_handle5 != NULL, "Expected success, got error %u\n", GetLastError());
2089 /* Delete the service */
2090 ret = DeleteService(svc_handle5);
2091 ok(ret, "Expected success (err=%d)\n", GetLastError());
2093 /* Wait a while. Just in case one of the following tests does a CreateService again */
2094 Sleep(1000);
2096 CloseServiceHandle(svc_handle5);
2097 CloseServiceHandle(scm_handle);
2100 START_TEST(service)
2102 SC_HANDLE scm_handle;
2104 /* Bail out if we are on win98 */
2105 SetLastError(0xdeadbeef);
2106 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2108 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2110 skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2111 return;
2113 CloseServiceHandle(scm_handle);
2115 init_function_pointers();
2117 /* First some parameter checking */
2118 test_open_scm();
2119 test_open_svc();
2120 test_create_delete_svc();
2121 test_get_displayname();
2122 test_get_servicekeyname();
2123 test_query_svc();
2124 test_enum_svc();
2125 test_close();
2126 /* Test the creation, querying and deletion of a service */
2127 test_sequence();
2128 test_queryconfig2();
2129 /* The main reason for this test is to check if any refcounting is used
2130 * and what the rules are
2132 test_refcount();