push a9ea98eed33411fd355fd4e8b78f7880f1dd09a3
[wine/hacks.git] / dlls / advapi32 / tests / service.c
blob4225e350e3135eb4c25e47a87de6fc5a832db9c6
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\n");
64 ok(GetLastError() == ERROR_SUCCESS /* W2K3, Vista */ ||
65 GetLastError() == 0xdeadbeef /* NT4, XP */ ||
66 GetLastError() == ERROR_IO_PENDING /* W2K */,
67 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
68 CloseServiceHandle(scm_handle);
70 /* Unknown database name */
71 SetLastError(0xdeadbeef);
72 scm_handle = OpenSCManagerA(NULL, "DoesNotExist", SC_MANAGER_CONNECT);
73 ok(!scm_handle, "Expected failure\n");
74 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
75 CloseServiceHandle(scm_handle); /* Just in case */
77 /* MSDN says only ServiceActive is allowed, or NULL */
78 SetLastError(0xdeadbeef);
79 scm_handle = OpenSCManagerA(NULL, SERVICES_FAILED_DATABASEA, SC_MANAGER_CONNECT);
80 ok(!scm_handle, "Expected failure\n");
81 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
82 CloseServiceHandle(scm_handle); /* Just in case */
84 /* Remote unknown host */
85 SetLastError(0xdeadbeef);
86 scm_handle = OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
87 ok(!scm_handle, "Expected failure\n");
88 todo_wine
89 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* w2k8 */,
90 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
91 CloseServiceHandle(scm_handle); /* Just in case */
93 /* Proper call with an empty hostname */
94 SetLastError(0xdeadbeef);
95 scm_handle = OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
96 ok(scm_handle != NULL, "Expected success\n");
97 ok(GetLastError() == ERROR_SUCCESS /* W2K3, Vista */ ||
98 GetLastError() == ERROR_ENVVAR_NOT_FOUND /* NT4 */ ||
99 GetLastError() == 0xdeadbeef /* XP */ ||
100 GetLastError() == ERROR_IO_PENDING /* W2K */,
101 "Expected ERROR_SUCCESS, ERROR_IO_PENDING, ERROR_ENVVAR_NOT_FOUND or 0xdeadbeef, got %d\n", GetLastError());
102 CloseServiceHandle(scm_handle);
104 /* Again a correct one */
105 SetLastError(0xdeadbeef);
106 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
107 ok(scm_handle != NULL, "Expected success\n");
108 ok(GetLastError() == ERROR_SUCCESS /* W2K3, Vista */ ||
109 GetLastError() == 0xdeadbeef /* NT4, XP */ ||
110 GetLastError() == ERROR_IO_PENDING /* W2K */,
111 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
112 CloseServiceHandle(scm_handle);
115 static void test_open_svc(void)
117 SC_HANDLE scm_handle, svc_handle;
118 CHAR displayname[4096];
119 DWORD displaysize;
121 /* All NULL (invalid access rights) */
122 SetLastError(0xdeadbeef);
123 svc_handle = OpenServiceA(NULL, NULL, 0);
124 ok(!svc_handle, "Expected failure\n");
125 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
127 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
129 /* NULL service */
130 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
131 SetLastError(0xdeadbeef);
132 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
133 ok(!svc_handle, "Expected failure\n");
134 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
135 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
136 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
138 /* Nonexistent service */
139 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
140 SetLastError(0xdeadbeef);
141 svc_handle = OpenServiceA(scm_handle, "deadbeef", GENERIC_READ);
142 ok(!svc_handle, "Expected failure\n");
143 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
144 CloseServiceHandle(scm_handle);
146 /* Proper SCM handle but different access rights */
147 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
148 SetLastError(0xdeadbeef);
149 svc_handle = OpenServiceA(scm_handle, "Spooler", GENERIC_WRITE);
150 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
151 skip("Not enough rights to get a handle to the service\n");
152 else
154 ok(svc_handle != NULL, "Expected success\n");
155 ok(GetLastError() == ERROR_SUCCESS /* W2K3, Vista */ ||
156 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
157 GetLastError() == 0xdeadbeef /* XP, NT4 */,
158 "Expected ERROR_SUCCESS or 0xdeadbeef, got %d\n", GetLastError());
159 CloseServiceHandle(svc_handle);
162 /* Test to show we can't open a service with the displayname */
164 /* Retrieve the needed size for the buffer */
165 displaysize = 0;
166 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
167 /* Get the displayname */
168 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
169 /* Try to open the service with this displayname, unless the displayname equals
170 * the servicename as that would defeat the purpose of this test.
172 if (!lstrcmpi(spooler, displayname))
174 skip("displayname equals servicename\n");
175 CloseServiceHandle(scm_handle);
176 return;
179 SetLastError(0xdeadbeef);
180 svc_handle = OpenServiceA(scm_handle, displayname, GENERIC_READ);
181 ok(!svc_handle, "Expected failure\n");
182 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
183 /* Just in case */
184 CloseServiceHandle(svc_handle);
186 CloseServiceHandle(scm_handle);
189 static void test_create_delete_svc(void)
191 SC_HANDLE scm_handle, svc_handle1;
192 CHAR username[UNLEN + 1], domain[MAX_PATH];
193 DWORD user_size = UNLEN + 1;
194 CHAR account[UNLEN + 3];
195 static const CHAR servicename [] = "Winetest";
196 static const CHAR pathname [] = "we_dont_care.exe";
197 static const CHAR empty [] = "";
198 static const CHAR password [] = "secret";
199 BOOL spooler_exists = FALSE;
200 BOOL ret;
201 CHAR display[4096];
202 DWORD display_size = sizeof(display);
204 /* Get the username and turn it into an account to be used in some tests */
205 GetUserNameA(username, &user_size);
206 /* Get the domainname to cater for that situation */
207 if (GetEnvironmentVariableA("USERDOMAIN", domain, MAX_PATH))
208 sprintf(account, "%s\\%s", domain, username);
209 else
210 sprintf(account, ".\\%s", username);
212 /* All NULL */
213 SetLastError(0xdeadbeef);
214 svc_handle1 = CreateServiceA(NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
215 ok(!svc_handle1, "Expected failure\n");
216 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
218 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
220 /* Only a valid handle to the Service Control Manager */
221 SetLastError(0xdeadbeef);
222 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
223 ok(!svc_handle1, "Expected failure\n");
224 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
225 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
226 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
228 /* Now with a servicename */
229 SetLastError(0xdeadbeef);
230 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
231 ok(!svc_handle1, "Expected failure\n");
232 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
233 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
234 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
236 /* Or just a binary name */
237 SetLastError(0xdeadbeef);
238 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
239 ok(!svc_handle1, "Expected failure\n");
240 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
241 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
242 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
244 /* Both servicename and binary name (We only have connect rights) */
245 SetLastError(0xdeadbeef);
246 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
247 ok(!svc_handle1, "Expected failure\n");
248 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
250 /* They can even be empty at this stage of parameter checking */
251 SetLastError(0xdeadbeef);
252 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
253 ok(!svc_handle1, "Expected failure\n");
254 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
256 SetLastError(0xdeadbeef);
257 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
258 ok(!svc_handle1, "Expected failure\n");
259 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
261 /* Open the Service Control Manager with minimal rights for creation
262 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
264 CloseServiceHandle(scm_handle);
265 SetLastError(0xdeadbeef);
266 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
267 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
269 skip("Not enough rights to get a handle to the manager\n");
270 return;
273 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
275 /* Empty strings for servicename and binary name are checked */
276 SetLastError(0xdeadbeef);
277 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
278 ok(!svc_handle1, "Expected failure\n");
279 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
281 SetLastError(0xdeadbeef);
282 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
283 ok(!svc_handle1, "Expected failure\n");
284 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
286 SetLastError(0xdeadbeef);
287 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
288 ok(!svc_handle1, "Expected failure\n");
289 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
291 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
292 * an ERROR_INVALID_PARAMETER)
294 SetLastError(0xdeadbeef);
295 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
296 SERVICE_DISABLED, 0, empty, NULL, NULL, NULL, NULL, NULL);
297 ok(!svc_handle1, "Expected failure\n");
298 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
300 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
302 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
303 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
305 SetLastError(0xdeadbeef);
306 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_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 (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
312 SetLastError(0xdeadbeef);
313 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_FILE_SYSTEM_DRIVER | SERVICE_INTERACTIVE_PROCESS,
314 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
315 ok(!svc_handle1, "Expected failure\n");
316 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
318 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
319 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
321 SetLastError(0xdeadbeef);
322 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
323 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, account, password);
324 ok(!svc_handle1, "Expected failure\n");
325 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
327 /* Illegal (start-type is not a mask and should only be one of the possibilities)
328 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
329 * it's most likely not the wanted start-type)
331 SetLastError(0xdeadbeef);
332 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS,
333 SERVICE_AUTO_START | SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
334 ok(!svc_handle1, "Expected failure\n");
335 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
337 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
338 SetLastError(0xdeadbeef);
339 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
340 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
341 ok(!svc_handle1, "Expected failure\n");
342 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
344 /* The service already exists (check first, just in case) */
345 svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
346 if (svc_handle1)
348 spooler_exists = TRUE;
349 CloseServiceHandle(svc_handle1);
350 SetLastError(0xdeadbeef);
351 svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
352 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
353 ok(!svc_handle1, "Expected failure\n");
354 ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
356 else
357 skip("Spooler service doesn't exist\n");
359 /* To find an existing displayname we check the 'Spooler' service. Although the registry
360 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
361 * to the servicename and can't be used as well for a new displayname.
363 if (spooler_exists)
365 ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
367 if (!ret)
368 skip("Could not retrieve a displayname for the Spooler service\n");
369 else
371 svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
372 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
373 ok(!svc_handle1, "Expected failure\n");
374 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
375 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
378 else
379 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
381 /* Windows doesn't care about the access rights for creation (which makes
382 * sense as there is no service yet) as long as there are sufficient
383 * rights to the manager.
385 SetLastError(0xdeadbeef);
386 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
387 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
388 ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
389 ok(GetLastError() == ERROR_SUCCESS /* W2K3, Vista */ ||
390 GetLastError() == 0xdeadbeef /* NT4, XP */ ||
391 GetLastError() == ERROR_IO_PENDING /* W2K */,
392 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
394 /* DeleteService however must have proper rights */
395 SetLastError(0xdeadbeef);
396 ret = DeleteService(svc_handle1);
397 ok(!ret, "Expected failure\n");
398 ok(GetLastError() == ERROR_ACCESS_DENIED,
399 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
401 /* Open the service with minimal rights for deletion.
402 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
404 CloseServiceHandle(svc_handle1);
405 svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
407 /* Now that we have the proper rights, we should be able to delete */
408 SetLastError(0xdeadbeef);
409 ret = DeleteService(svc_handle1);
410 ok(ret, "Expected success\n");
411 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
412 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */ ||
413 GetLastError() == ERROR_IO_PENDING /* W2K */,
414 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
416 CloseServiceHandle(svc_handle1);
418 CloseServiceHandle(scm_handle);
420 /* Wait a while. One of the following tests also does a CreateService for the
421 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
422 * error if we do this to quick. Vista seems more picky then the others.
424 Sleep(1000);
426 /* And a final NULL check */
427 SetLastError(0xdeadbeef);
428 ret = DeleteService(NULL);
429 ok(!ret, "Expected failure\n");
430 ok(GetLastError() == ERROR_INVALID_HANDLE,
431 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
434 static void test_get_displayname(void)
436 SC_HANDLE scm_handle, svc_handle;
437 BOOL ret;
438 CHAR displayname[4096];
439 WCHAR displaynameW[2048];
440 DWORD displaysize, tempsize, tempsizeW;
441 static const CHAR deadbeef[] = "Deadbeef";
442 static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
443 static const CHAR servicename[] = "Winetest";
444 static const CHAR pathname[] = "we_dont_care.exe";
446 /* Having NULL for the size of the buffer will crash on W2K3 */
448 SetLastError(0xdeadbeef);
449 ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
450 ok(!ret, "Expected failure\n");
451 ok(GetLastError() == ERROR_INVALID_HANDLE,
452 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
454 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
456 SetLastError(0xdeadbeef);
457 ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
458 ok(!ret, "Expected failure\n");
459 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
460 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
461 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
463 SetLastError(0xdeadbeef);
464 displaysize = sizeof(displayname);
465 ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
466 ok(!ret, "Expected failure\n");
467 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
468 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
469 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
471 /* Test for nonexistent service */
472 SetLastError(0xdeadbeef);
473 displaysize = -1;
474 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
475 ok(!ret, "Expected failure\n");
476 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
477 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
479 /* Check if 'Spooler' exists */
480 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
481 if (!svc_handle)
483 skip("Spooler service doesn't exist\n");
484 CloseServiceHandle(scm_handle);
485 return;
487 CloseServiceHandle(svc_handle);
489 /* Retrieve the needed size for the buffer */
490 SetLastError(0xdeadbeef);
491 displaysize = -1;
492 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
493 ok(!ret, "Expected failure\n");
494 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
495 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
496 tempsize = displaysize;
498 displaysize = 0;
499 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
500 ok(!ret, "Expected failure\n");
501 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
502 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
503 ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
505 /* Buffer is too small */
506 SetLastError(0xdeadbeef);
507 displaysize = (tempsize / 2);
508 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
509 ok(!ret, "Expected failure\n");
510 ok(displaysize == tempsize, "Expected the needed buffersize\n");
511 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
512 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
514 /* First try with a buffer that should be big enough to hold
515 * the ANSI string (and terminating character). This succeeds on Windows
516 * although when asked (see above 2 tests) it will return twice the needed size.
518 SetLastError(0xdeadbeef);
519 displaysize = (tempsize / 2) + 1;
520 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
521 ok(ret, "Expected success\n");
522 ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
523 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
524 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
525 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
526 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
528 /* Now with the original returned size */
529 SetLastError(0xdeadbeef);
530 displaysize = tempsize;
531 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
532 ok(ret, "Expected success\n");
533 ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
534 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
535 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
536 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
537 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
539 /* And with a bigger than needed buffer */
540 SetLastError(0xdeadbeef);
541 displaysize = tempsize * 2;
542 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
543 ok(ret, "Expected success\n");
544 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
545 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
546 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
547 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
548 /* Test that shows that if the buffersize is enough, it's not changed */
549 ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
550 ok(lstrlen(displayname) == tempsize/2,
551 "Expected the buffer to be twice the length of the string\n") ;
553 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
554 SetLastError(0xdeadbeef);
555 displaysize = -1;
556 ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
557 ok(!ret, "Expected failure\n");
558 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
559 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
561 /* Buffer is too small */
562 SetLastError(0xdeadbeef);
563 tempsizeW = displaysize;
564 displaysize = tempsizeW / 2;
565 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
566 ok(!ret, "Expected failure\n");
567 ok(displaysize = tempsizeW, "Expected the needed buffersize\n");
568 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
569 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
571 /* Now with the original returned size */
572 SetLastError(0xdeadbeef);
573 displaysize = tempsizeW;
574 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
575 ok(!ret, "Expected failure\n");
576 ok(displaysize = tempsizeW, "Expected the needed buffersize\n");
577 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
578 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
580 /* And with a bigger than needed buffer */
581 SetLastError(0xdeadbeef);
582 displaysize = tempsizeW + 1; /* This caters for the null terminating character */
583 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
584 ok(ret, "Expected success\n");
585 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
586 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
587 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
588 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
589 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
590 ok(lstrlenW(displaynameW) == displaysize,
591 "Expected the buffer to be the length of the string\n") ;
592 ok(tempsize / 2 == tempsizeW,
593 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
595 CloseServiceHandle(scm_handle);
597 /* Test for a service without a displayname (which is valid). This should return
598 * the servicename itself.
600 SetLastError(0xdeadbeef);
601 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
602 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
604 skip("Not enough rights to get a handle to the manager\n");
605 return;
608 SetLastError(0xdeadbeef);
609 svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
610 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
611 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
612 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
613 if (!svc_handle)
615 CloseServiceHandle(scm_handle);
616 return;
619 /* Retrieve the needed size for the buffer */
620 SetLastError(0xdeadbeef);
621 displaysize = -1;
622 ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
623 ok(!ret, "Expected failure\n");
624 ok(displaysize == lstrlen(servicename) * 2,
625 "Expected the displaysize to be twice the size of the servicename\n");
626 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
627 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
629 /* Buffer is too small */
630 SetLastError(0xdeadbeef);
631 tempsize = displaysize;
632 displaysize = (tempsize / 2);
633 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
634 ok(!ret, "Expected failure\n");
635 ok(displaysize == tempsize, "Expected the needed buffersize\n");
636 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
637 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
639 /* Get the displayname */
640 SetLastError(0xdeadbeef);
641 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
642 ok(ret, "Expected success\n");
643 ok(!lstrcmpi(displayname, servicename),
644 "Expected displayname to be %s, got %s\n", servicename, displayname);
645 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
646 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
647 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
648 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
650 /* Delete the service */
651 ret = DeleteService(svc_handle);
652 ok(ret, "Expected success\n");
654 CloseServiceHandle(svc_handle);
655 CloseServiceHandle(scm_handle);
657 /* Wait a while. Just in case one of the following tests does a CreateService again */
658 Sleep(1000);
661 static void test_get_servicekeyname(void)
663 SC_HANDLE scm_handle, svc_handle;
664 CHAR servicename[4096];
665 CHAR displayname[4096];
666 WCHAR servicenameW[4096];
667 WCHAR displaynameW[4096];
668 DWORD servicesize, displaysize, tempsize;
669 BOOL ret;
670 static const CHAR deadbeef[] = "Deadbeef";
671 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
673 /* Having NULL for the size of the buffer will crash on W2K3 */
675 SetLastError(0xdeadbeef);
676 ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
677 ok(!ret, "Expected failure\n");
678 ok(GetLastError() == ERROR_INVALID_HANDLE,
679 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
681 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
683 servicesize = 200;
684 SetLastError(0xdeadbeef);
685 ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
686 ok(!ret, "Expected failure\n");
687 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
688 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
689 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
690 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
692 /* Valid handle and buffer but no displayname */
693 servicesize = 200;
694 SetLastError(0xdeadbeef);
695 ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
696 ok(!ret, "Expected failure\n");
697 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
698 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
699 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
700 todo_wine ok(servicesize == 200, "Service size expected 1, got %d\n", servicesize);
702 /* Test for nonexistent displayname */
703 SetLastError(0xdeadbeef);
704 ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
705 ok(!ret, "Expected failure\n");
706 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
707 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
708 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
710 servicesize = 15;
711 strcpy(servicename, "ABC");
712 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
713 ok(!ret, "Expected failure\n");
714 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
715 ok(servicename[0] == 0, "Service name not empty\n");
717 servicesize = 15;
718 servicenameW[0] = 'A';
719 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
720 ok(!ret, "Expected failure\n");
721 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
722 ok(servicenameW[0] == 0, "Service name not empty\n");
724 servicesize = 0;
725 strcpy(servicename, "ABC");
726 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
727 ok(!ret, "Expected failure\n");
728 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
729 ok(servicename[0] == 'A', "Service name changed\n");
731 servicesize = 0;
732 servicenameW[0] = 'A';
733 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
734 ok(!ret, "Expected failure\n");
735 todo_wine ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
736 ok(servicenameW[0] == 'A', "Service name changed\n");
738 /* Check if 'Spooler' exists */
739 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
740 if (!svc_handle)
742 skip("Spooler service doesn't exist\n");
743 CloseServiceHandle(scm_handle);
744 return;
746 CloseServiceHandle(svc_handle);
748 /* Get the displayname for the 'Spooler' service */
749 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
750 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
752 /* Retrieve the needed size for the buffer */
753 SetLastError(0xdeadbeef);
754 servicesize = 0;
755 ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
756 ok(!ret, "Expected failure\n");
757 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
758 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
760 /* Valid call with the correct buffersize */
761 SetLastError(0xdeadbeef);
762 tempsize = servicesize;
763 servicesize *= 2;
764 ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
765 ok(ret, "Expected success\n");
766 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
767 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
768 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
769 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
770 if (ret)
772 ok(lstrlen(servicename) == tempsize/2,
773 "Expected the buffer to be twice the length of the string\n") ;
774 ok(!lstrcmpi(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
775 ok(servicesize == (tempsize * 2),
776 "Expected servicesize not to change if buffer not insufficient\n") ;
779 MultiByteToWideChar(CP_ACP, 0, displayname, -1, displaynameW, sizeof(displaynameW)/2);
780 SetLastError(0xdeadbeef);
781 servicesize *= 2;
782 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
783 ok(ret, "Expected success\n");
784 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
785 GetLastError() == ERROR_IO_PENDING /* W2K */ ||
786 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
787 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
788 if (ret)
790 ok(lstrlen(servicename) == tempsize/2,
791 "Expected the buffer to be twice the length of the string\n") ;
792 ok(servicesize == lstrlenW(servicenameW),
793 "Expected servicesize not to change if buffer not insufficient\n") ;
796 SetLastError(0xdeadbeef);
797 servicesize = 3;
798 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
799 ok(!ret, "Expected failure\n");
800 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
801 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
802 ok(servicenameW[0] == 0, "Buffer not empty\n");
804 CloseServiceHandle(scm_handle);
807 static void test_query_svc(void)
809 SC_HANDLE scm_handle, svc_handle;
810 BOOL ret;
811 SERVICE_STATUS status;
812 SERVICE_STATUS_PROCESS *statusproc;
813 DWORD bufsize, needed;
815 /* All NULL or wrong */
816 SetLastError(0xdeadbeef);
817 ret = QueryServiceStatus(NULL, NULL);
818 ok(!ret, "Expected failure\n");
819 ok(GetLastError() == ERROR_INVALID_HANDLE,
820 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
822 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
824 /* Check if 'Spooler' exists.
825 * Open with not enough rights to query the status.
827 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
828 if (!svc_handle)
830 skip("Spooler service doesn't exist\n");
831 CloseServiceHandle(scm_handle);
832 return;
835 SetLastError(0xdeadbeef);
836 ret = QueryServiceStatus(svc_handle, NULL);
837 ok(!ret, "Expected failure\n");
838 todo_wine
839 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
840 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
841 "Unexpected last error %d\n", GetLastError());
843 SetLastError(0xdeadbeef);
844 ret = QueryServiceStatus(svc_handle, &status);
845 ok(!ret, "Expected failure\n");
846 ok(GetLastError() == ERROR_ACCESS_DENIED,
847 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
849 /* Open the service with just enough rights.
850 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
852 CloseServiceHandle(svc_handle);
853 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
855 SetLastError(0xdeadbeef);
856 ret = QueryServiceStatus(svc_handle, &status);
857 ok(ret, "Expected success\n");
858 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
859 GetLastError() == 0xdeadbeef /* NT4, XP and Vista */ ||
860 GetLastError() == ERROR_IO_PENDING /* W2K */,
861 "Unexpected last error %d\n", GetLastError());
863 CloseServiceHandle(svc_handle);
865 /* More or less the same tests for QueryServiceStatusEx */
867 /* Open service with not enough rights to query the status */
868 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
870 /* All NULL or wrong, this proves that info level is checked first */
871 SetLastError(0xdeadbeef);
872 ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
873 ok(!ret, "Expected failure\n");
874 todo_wine
875 ok(GetLastError() == ERROR_INVALID_LEVEL,
876 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
878 /* Passing a NULL parameter for the needed buffer size
879 * will crash on anything but NT4.
882 /* Only info level is correct. It looks like the buffer/size is checked second */
883 SetLastError(0xdeadbeef);
884 ret = pQueryServiceStatusEx(NULL, 0, NULL, 0, &needed);
885 /* NT4 checks the handle first */
886 if (GetLastError() != ERROR_INVALID_HANDLE)
888 ok(!ret, "Expected failure\n");
889 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
890 "Needed buffersize is wrong : %d\n", needed);
891 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
892 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
895 /* Pass a correct buffer and buffersize but a NULL handle */
896 statusproc = HeapAlloc(GetProcessHeap(), 0, needed);
897 bufsize = needed;
898 SetLastError(0xdeadbeef);
899 ret = pQueryServiceStatusEx(NULL, 0, (BYTE*)statusproc, bufsize, &needed);
900 ok(!ret, "Expected failure\n");
901 ok(GetLastError() == ERROR_INVALID_HANDLE,
902 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
903 HeapFree(GetProcessHeap(), 0, statusproc);
905 /* Correct handle and info level */
906 SetLastError(0xdeadbeef);
907 ret = pQueryServiceStatusEx(svc_handle, 0, NULL, 0, &needed);
908 /* NT4 doesn't return the needed size */
909 if (GetLastError() != ERROR_INVALID_PARAMETER)
911 ok(!ret, "Expected failure\n");
912 todo_wine
914 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
915 "Needed buffersize is wrong : %d\n", needed);
916 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
917 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
921 /* All parameters are OK but we don't have enough rights */
922 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
923 bufsize = sizeof(SERVICE_STATUS_PROCESS);
924 SetLastError(0xdeadbeef);
925 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
926 ok(!ret, "Expected failure\n");
927 ok(GetLastError() == ERROR_ACCESS_DENIED,
928 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
929 HeapFree(GetProcessHeap(), 0, statusproc);
931 /* Open the service with just enough rights. */
932 CloseServiceHandle(svc_handle);
933 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
935 /* Everything should be fine now. */
936 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
937 bufsize = sizeof(SERVICE_STATUS_PROCESS);
938 SetLastError(0xdeadbeef);
939 ret = pQueryServiceStatusEx(svc_handle, 0, (BYTE*)statusproc, bufsize, &needed);
940 ok(ret, "Expected success\n");
941 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
942 GetLastError() == 0xdeadbeef /* NT4, XP and Vista */ ||
943 GetLastError() == ERROR_IO_PENDING /* W2K */,
944 "Unexpected last error %d\n", GetLastError());
945 if (statusproc->dwCurrentState == SERVICE_RUNNING)
946 ok(statusproc->dwProcessId != 0,
947 "Expect a process id for this running service\n");
948 else
949 ok(statusproc->dwProcessId == 0,
950 "Expect no process id for this stopped service\n");
951 HeapFree(GetProcessHeap(), 0, statusproc);
953 CloseServiceHandle(svc_handle);
954 CloseServiceHandle(scm_handle);
957 static void test_enum_svc(void)
959 SC_HANDLE scm_handle;
960 BOOL ret;
961 DWORD bufsize, needed, returned, resume;
962 DWORD tempneeded, tempreturned;
963 DWORD drivercountactive, servicecountactive;
964 DWORD drivercountinactive, servicecountinactive;
965 ENUM_SERVICE_STATUS *services;
966 ENUM_SERVICE_STATUS_PROCESS *exservices;
967 INT i;
969 /* All NULL or wrong */
970 SetLastError(0xdeadbeef);
971 ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
972 ok(!ret, "Expected failure\n");
973 todo_wine
974 ok(GetLastError() == ERROR_INVALID_HANDLE,
975 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
977 /* Open the service control manager with not enough rights at first */
978 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
980 /* Valid handle but rest is still NULL or wrong */
981 SetLastError(0xdeadbeef);
982 ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
983 ok(!ret, "Expected failure\n");
984 todo_wine
985 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
986 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
987 "Unexpected last error %d\n", GetLastError());
989 /* Don't specify the two required pointers */
990 returned = 0xdeadbeef;
991 SetLastError(0xdeadbeef);
992 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
993 ok(!ret, "Expected failure\n");
994 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
995 todo_wine
996 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
997 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
998 "Unexpected last error %d\n", GetLastError());
1000 /* Don't specify the two required pointers */
1001 needed = 0xdeadbeef;
1002 SetLastError(0xdeadbeef);
1003 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1004 ok(!ret, "Expected failure\n");
1005 ok(needed == 0xdeadbeef, "Expected no change to the needed buffer variable\n");
1006 todo_wine
1007 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1008 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1009 "Unexpected last error %d\n", GetLastError());
1011 /* No valid servicetype and servicestate */
1012 needed = 0xdeadbeef;
1013 returned = 0xdeadbeef;
1014 SetLastError(0xdeadbeef);
1015 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1016 ok(!ret, "Expected failure\n");
1017 todo_wine
1019 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1020 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1021 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1022 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1025 /* No valid servicetype and servicestate */
1026 needed = 0xdeadbeef;
1027 returned = 0xdeadbeef;
1028 SetLastError(0xdeadbeef);
1029 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1030 ok(!ret, "Expected failure\n");
1031 todo_wine
1033 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1034 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1035 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1036 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1039 /* No valid servicetype and servicestate */
1040 needed = 0xdeadbeef;
1041 returned = 0xdeadbeef;
1042 SetLastError(0xdeadbeef);
1043 ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0,
1044 &needed, &returned, NULL);
1045 ok(!ret, "Expected failure\n");
1046 todo_wine
1048 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1049 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1050 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1051 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1054 /* All parameters are correct but our access rights are wrong */
1055 needed = 0xdeadbeef;
1056 returned = 0xdeadbeef;
1057 SetLastError(0xdeadbeef);
1058 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1059 &needed, &returned, NULL);
1060 ok(!ret, "Expected failure\n");
1061 todo_wine
1063 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1064 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1066 ok(GetLastError() == ERROR_ACCESS_DENIED,
1067 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1069 /* Open the service control manager with the needed rights */
1070 CloseServiceHandle(scm_handle);
1071 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
1073 /* All parameters are correct. Request the needed buffer size */
1074 needed = 0xdeadbeef;
1075 returned = 0xdeadbeef;
1076 SetLastError(0xdeadbeef);
1077 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1078 &needed, &returned, NULL);
1079 ok(!ret, "Expected failure\n");
1080 todo_wine
1082 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1083 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1084 ok(GetLastError() == ERROR_MORE_DATA,
1085 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1088 /* Store the needed bytes */
1089 tempneeded = needed;
1091 /* Allocate the correct needed bytes */
1092 services = HeapAlloc(GetProcessHeap(), 0, needed);
1093 bufsize = needed;
1094 needed = 0xdeadbeef;
1095 returned = 0xdeadbeef;
1096 SetLastError(0xdeadbeef);
1097 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1098 services, bufsize, &needed, &returned, NULL);
1099 todo_wine
1101 ok(ret, "Expected success\n");
1102 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1103 ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1104 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
1105 GetLastError() == 0xdeadbeef /* NT4, XP and Vista */ ||
1106 GetLastError() == ERROR_IO_PENDING /* W2K */,
1107 "Unexpected last error %d\n", GetLastError());
1109 HeapFree(GetProcessHeap(), 0, services);
1111 /* Store the number of returned services */
1112 tempreturned = returned;
1114 /* Allocate less then the needed bytes and don't specify a resume handle */
1115 services = HeapAlloc(GetProcessHeap(), 0, tempneeded - 1);
1116 bufsize = tempneeded - 1;
1117 needed = 0xdeadbeef;
1118 returned = 0xdeadbeef;
1119 SetLastError(0xdeadbeef);
1120 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1121 services, bufsize, &needed, &returned, NULL);
1122 ok(!ret, "Expected failure\n");
1123 todo_wine
1125 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1126 ok(returned == (tempreturned - 1), "Expected one service less to be returned\n");
1127 ok(GetLastError() == ERROR_MORE_DATA,
1128 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1130 HeapFree(GetProcessHeap(), 0, services);
1132 /* Allocate less then the needed bytes, this time with a correct resume handle */
1133 services = HeapAlloc(GetProcessHeap(), 0, tempneeded - 1);
1134 bufsize = tempneeded - 1;
1135 needed = 0xdeadbeef;
1136 returned = 0xdeadbeef;
1137 resume = 0;
1138 SetLastError(0xdeadbeef);
1139 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1140 services, bufsize, &needed, &returned, &resume);
1141 ok(!ret, "Expected failure\n");
1142 todo_wine
1144 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1145 ok(returned == (tempreturned - 1), "Expected one service less to be returned\n");
1146 ok(resume, "Expected a resume handle\n");
1147 ok(GetLastError() == ERROR_MORE_DATA,
1148 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1150 HeapFree(GetProcessHeap(), 0, services);
1152 /* Fetch that last service but pass a bigger buffer size */
1153 services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1154 bufsize = tempneeded;
1155 needed = 0xdeadbeef;
1156 returned = 0xdeadbeef;
1157 SetLastError(0xdeadbeef);
1158 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1159 services, bufsize, &needed, &returned, &resume);
1160 todo_wine
1162 ok(ret, "Expected success\n");
1163 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1164 ok(returned == 1, "Expected only 1 service to be returned\n");
1165 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
1166 GetLastError() == 0xdeadbeef /* NT4, XP and Vista */ ||
1167 GetLastError() == ERROR_IO_PENDING /* W2K */,
1168 "Unexpected last error %d\n", GetLastError());
1170 ok(resume == 0, "Expected the resume handle to be 0\n");
1171 HeapFree(GetProcessHeap(), 0, services);
1173 /* See if things add up */
1175 /* Get the number of active driver services */
1176 EnumServicesStatusA(scm_handle, SERVICE_DRIVER, SERVICE_ACTIVE, NULL, 0,
1177 &needed, &returned, NULL);
1178 services = HeapAlloc(GetProcessHeap(), 0, needed);
1179 EnumServicesStatusA(scm_handle, SERVICE_DRIVER, SERVICE_ACTIVE, services,
1180 needed, &needed, &returned, NULL);
1181 HeapFree(GetProcessHeap(), 0, services);
1183 /* Store the number of active driver services */
1184 drivercountactive = returned;
1186 /* Get the number of inactive driver services */
1187 EnumServicesStatusA(scm_handle, SERVICE_DRIVER, SERVICE_INACTIVE, NULL, 0,
1188 &needed, &returned, NULL);
1189 services = HeapAlloc(GetProcessHeap(), 0, needed);
1190 EnumServicesStatusA(scm_handle, SERVICE_DRIVER, SERVICE_INACTIVE, services,
1191 needed, &needed, &returned, NULL);
1192 HeapFree(GetProcessHeap(), 0, services);
1194 drivercountinactive = returned;
1196 /* Get the number of driver services */
1197 EnumServicesStatusA(scm_handle, SERVICE_DRIVER, SERVICE_STATE_ALL, NULL, 0,
1198 &needed, &returned, NULL);
1199 services = HeapAlloc(GetProcessHeap(), 0, needed);
1200 EnumServicesStatusA(scm_handle, SERVICE_DRIVER, SERVICE_STATE_ALL, services,
1201 needed, &needed, &returned, NULL);
1202 HeapFree(GetProcessHeap(), 0, services);
1204 /* Check if total is the same as active and inactive driver services */
1205 todo_wine
1206 ok(returned == (drivercountactive + drivercountinactive),
1207 "Something wrong in the calculation\n");
1209 /* Get the number of active win32 services */
1210 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1211 &needed, &returned, NULL);
1212 services = HeapAlloc(GetProcessHeap(), 0, needed);
1213 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1214 needed, &needed, &returned, NULL);
1215 HeapFree(GetProcessHeap(), 0, services);
1217 servicecountactive = returned;
1219 /* Get the number of inactive win32 services */
1220 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1221 &needed, &returned, NULL);
1222 services = HeapAlloc(GetProcessHeap(), 0, needed);
1223 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1224 needed, &needed, &returned, NULL);
1225 HeapFree(GetProcessHeap(), 0, services);
1227 servicecountinactive = returned;
1229 /* Get the number of win32 services */
1230 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1231 &needed, &returned, NULL);
1232 services = HeapAlloc(GetProcessHeap(), 0, needed);
1233 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1234 needed, &needed, &returned, NULL);
1235 HeapFree(GetProcessHeap(), 0, services);
1237 /* Check if total is the same as active and inactive win32 services */
1238 todo_wine
1239 ok(returned == (servicecountactive + servicecountinactive),
1240 "Something wrong in the calculation\n");
1242 /* Get the number of all services */
1243 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1244 NULL, 0, &needed, &returned, NULL);
1245 services = HeapAlloc(GetProcessHeap(), 0, needed);
1246 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1247 services, needed, &needed, &returned, NULL);
1249 /* Check if total is the same as active and inactive win32 services */
1250 todo_wine
1251 ok(returned == (servicecountactive + servicecountinactive),
1252 "Something wrong in the calculation\n");
1254 /* Get the number of all services.
1255 * Fetch the status of the last call as failing could make the following tests crash
1256 * on Wine (we don't return anything yet).
1258 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1259 NULL, 0, &needed, &returned, NULL);
1260 services = HeapAlloc(GetProcessHeap(), 0, needed);
1261 ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1262 services, needed, &needed, &returned, NULL);
1264 /* Check if total is the same as all those single calls */
1265 todo_wine
1266 ok(returned == (drivercountactive + drivercountinactive + servicecountactive + servicecountinactive),
1267 "Something wrong in the calculation\n");
1269 /* Loop through all those returned services */
1270 for (i = 0; ret && i < returned; i++)
1272 SERVICE_STATUS status = services[i].ServiceStatus;
1274 /* lpServiceName and lpDisplayName should always be filled */
1275 ok(lstrlenA(services[i].lpServiceName) > 0, "Expected a service name\n");
1276 ok(lstrlenA(services[i].lpDisplayName) > 0, "Expected a display name\n");
1278 /* Decrement the counters to see if the functions calls return the same
1279 * numbers as the contents of these structures.
1281 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1283 /* FIXME: should be probably more then just SERVICE_RUNNING */
1284 if (status.dwCurrentState == SERVICE_RUNNING)
1285 drivercountactive--;
1286 else
1287 drivercountinactive--;
1290 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1292 if (status.dwCurrentState == SERVICE_RUNNING)
1293 servicecountactive--;
1294 else
1295 servicecountinactive--;
1298 HeapFree(GetProcessHeap(), 0, services);
1300 todo_wine
1302 ok(drivercountactive == 0, "Active driver mismatch\n");
1303 ok(drivercountinactive == 0, "Inactive driver mismatch\n");
1304 ok(servicecountactive == 0, "Active services mismatch\n");
1305 ok(servicecountinactive == 0, "Inactive services mismatch\n");
1308 CloseServiceHandle(scm_handle);
1310 /* More or less the same for EnumServicesStatusExA */
1312 /* All NULL or wrong */
1313 SetLastError(0xdeadbeef);
1314 ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1315 ok(!ret, "Expected failure\n");
1316 todo_wine
1317 ok(GetLastError() == ERROR_INVALID_LEVEL,
1318 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1320 /* All NULL or wrong, just the info level is correct */
1321 SetLastError(0xdeadbeef);
1322 ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1323 ok(!ret, "Expected failure\n");
1324 todo_wine
1325 ok(GetLastError() == ERROR_INVALID_HANDLE,
1326 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1328 /* Open the service control manager with not enough rights at first */
1329 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1331 /* Valid handle and info level but rest is still NULL or wrong */
1332 SetLastError(0xdeadbeef);
1333 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1334 ok(!ret, "Expected failure\n");
1335 todo_wine
1336 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1337 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1338 "Unexpected last error %d\n", GetLastError());
1340 /* Don't specify the two required pointers */
1341 needed = 0xdeadbeef;
1342 SetLastError(0xdeadbeef);
1343 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1344 ok(!ret, "Expected failure\n");
1345 ok(needed == 0xdeadbeef, "Expected no change to the needed buffer variable\n");
1346 todo_wine
1347 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1348 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1349 "Unexpected last error %d\n", GetLastError());
1351 /* Don't specify the two required pointers */
1352 returned = 0xdeadbeef;
1353 SetLastError(0xdeadbeef);
1354 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1355 ok(!ret, "Expected failure\n");
1356 todo_wine
1358 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1359 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1360 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1361 "Unexpected last error %d\n", GetLastError());
1364 /* No valid servicetype and servicestate */
1365 needed = 0xdeadbeef;
1366 returned = 0xdeadbeef;
1367 SetLastError(0xdeadbeef);
1368 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1369 ok(!ret, "Expected failure\n");
1370 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1371 todo_wine
1373 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1374 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1375 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1378 /* No valid servicestate */
1379 needed = 0xdeadbeef;
1380 returned = 0xdeadbeef;
1381 SetLastError(0xdeadbeef);
1382 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1383 &needed, &returned, NULL, NULL);
1384 ok(!ret, "Expected failure\n");
1385 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1386 todo_wine
1388 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1389 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1390 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1393 /* No valid servicetype */
1394 needed = 0xdeadbeef;
1395 returned = 0xdeadbeef;
1396 SetLastError(0xdeadbeef);
1397 ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1398 &needed, &returned, NULL, NULL);
1399 ok(!ret, "Expected failure\n");
1400 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1401 todo_wine
1403 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1404 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1405 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1408 /* No valid servicetype and servicestate and unknown service group */
1409 needed = 0xdeadbeef;
1410 returned = 0xdeadbeef;
1411 SetLastError(0xdeadbeef);
1412 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1413 &returned, NULL, "deadbeef_group");
1414 ok(!ret, "Expected failure\n");
1415 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1416 todo_wine
1418 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1419 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1420 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1423 /* All parameters are correct but our access rights are wrong */
1424 needed = 0xdeadbeef;
1425 returned = 0xdeadbeef;
1426 SetLastError(0xdeadbeef);
1427 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1428 NULL, 0, &needed, &returned, NULL, NULL);
1429 ok(!ret, "Expected failure\n");
1430 todo_wine
1431 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1432 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1433 ok(GetLastError() == ERROR_ACCESS_DENIED,
1434 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1436 /* All parameters are correct, access rights are wrong but the
1437 * group name won't be checked yet.
1439 needed = 0xdeadbeef;
1440 returned = 0xdeadbeef;
1441 SetLastError(0xdeadbeef);
1442 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1443 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1444 ok(!ret, "Expected failure\n");
1445 todo_wine
1446 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1447 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1448 ok(GetLastError() == ERROR_ACCESS_DENIED,
1449 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1451 /* Open the service control manager with the needed rights */
1452 CloseServiceHandle(scm_handle);
1453 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
1455 /* All parameters are correct and the group will be checked */
1456 needed = 0xdeadbeef;
1457 returned = 0xdeadbeef;
1458 SetLastError(0xdeadbeef);
1459 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1460 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1461 ok(!ret, "Expected failure\n");
1462 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1463 todo_wine
1465 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1466 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1467 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1470 /* TODO: Create a test that makes sure we enumerate all services that don't
1471 * belong to a group. (specifying "").
1474 /* All parameters are correct. Request the needed buffer size */
1475 needed = 0xdeadbeef;
1476 returned = 0xdeadbeef;
1477 SetLastError(0xdeadbeef);
1478 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1479 NULL, 0, &needed, &returned, NULL, NULL);
1480 ok(!ret, "Expected failure\n");
1481 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1482 todo_wine
1484 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1485 ok(GetLastError() == ERROR_MORE_DATA,
1486 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1489 /* Store the needed bytes */
1490 tempneeded = needed;
1492 /* Allocate the correct needed bytes */
1493 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1494 bufsize = needed;
1495 needed = 0xdeadbeef;
1496 returned = 0xdeadbeef;
1497 SetLastError(0xdeadbeef);
1498 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1499 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1500 todo_wine
1502 ok(ret, "Expected success\n");
1503 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1504 ok(returned == tempreturned, "Expected the same number of service from this function\n");
1505 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
1506 GetLastError() == 0xdeadbeef /* NT4, XP and Vista */ ||
1507 GetLastError() == ERROR_IO_PENDING /* W2K */,
1508 "Unexpected last error %d\n", GetLastError());
1510 HeapFree(GetProcessHeap(), 0, exservices);
1512 /* Store the number of returned services */
1513 tempreturned = returned;
1515 /* Allocate less then the needed bytes and don't specify a resume handle */
1516 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded - 1);
1517 bufsize = tempneeded - 1;
1518 needed = 0xdeadbeef;
1519 returned = 0xdeadbeef;
1520 SetLastError(0xdeadbeef);
1521 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1522 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1523 ok(!ret, "Expected failure\n");
1524 todo_wine
1526 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1527 ok(returned == (tempreturned - 1), "Expected one service less to be returned\n");
1528 ok(GetLastError() == ERROR_MORE_DATA,
1529 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1531 HeapFree(GetProcessHeap(), 0, exservices);
1533 /* Allocate less then the needed bytes, this time with a correct resume handle */
1534 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded - 1);
1535 bufsize = tempneeded - 1;
1536 needed = 0xdeadbeef;
1537 returned = 0xdeadbeef;
1538 resume = 0;
1539 SetLastError(0xdeadbeef);
1540 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1541 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1542 ok(!ret, "Expected failure\n");
1543 todo_wine
1545 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1546 ok(returned == (tempreturned - 1), "Expected one service less to be returned\n");
1547 ok(resume, "Expected a resume handle\n");
1548 ok(GetLastError() == ERROR_MORE_DATA,
1549 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1551 HeapFree(GetProcessHeap(), 0, exservices);
1553 /* Fetch that last service but pass a bigger buffer size */
1554 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1555 bufsize = tempneeded;
1556 needed = 0xdeadbeef;
1557 returned = 0xdeadbeef;
1558 SetLastError(0xdeadbeef);
1559 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1560 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1561 todo_wine
1563 ok(ret, "Expected success\n");
1564 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1565 ok(returned == 1, "Expected only 1 service to be returned\n");
1566 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
1567 GetLastError() == 0xdeadbeef /* NT4, XP and Vista */ ||
1568 GetLastError() == ERROR_IO_PENDING /* W2K */,
1569 "Unexpected last error %d\n", GetLastError());
1571 ok(resume == 0, "Expected the resume handle to be 0\n");
1572 HeapFree(GetProcessHeap(), 0, exservices);
1574 /* See if things add up */
1576 /* Get the number of active driver services */
1577 pEnumServicesStatusExA(scm_handle, 0, SERVICE_DRIVER, SERVICE_ACTIVE,
1578 NULL, 0, &needed, &returned, NULL, NULL);
1579 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1580 pEnumServicesStatusExA(scm_handle, 0, SERVICE_DRIVER, SERVICE_ACTIVE,
1581 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1582 HeapFree(GetProcessHeap(), 0, exservices);
1584 /* Store the number of active driver services */
1585 drivercountactive = returned;
1587 /* Get the number of inactive driver services */
1588 pEnumServicesStatusExA(scm_handle, 0, SERVICE_DRIVER, SERVICE_INACTIVE,
1589 NULL, 0, &needed, &returned, NULL, NULL);
1590 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1591 pEnumServicesStatusExA(scm_handle, 0, SERVICE_DRIVER, SERVICE_INACTIVE,
1592 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1593 HeapFree(GetProcessHeap(), 0, exservices);
1595 drivercountinactive = returned;
1597 /* Get the number of driver services */
1598 pEnumServicesStatusExA(scm_handle, 0, SERVICE_DRIVER, SERVICE_STATE_ALL,
1599 NULL, 0, &needed, &returned, NULL, NULL);
1600 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1601 pEnumServicesStatusExA(scm_handle, 0, SERVICE_DRIVER, SERVICE_STATE_ALL,
1602 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1603 HeapFree(GetProcessHeap(), 0, exservices);
1605 /* Check if total is the same as active and inactive driver services */
1606 ok(returned == (drivercountactive + drivercountinactive),
1607 "Something wrong in the calculation\n");
1609 /* Get the number of active win32 services */
1610 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1611 NULL, 0, &needed, &returned, NULL, NULL);
1612 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1613 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1614 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1615 HeapFree(GetProcessHeap(), 0, exservices);
1617 servicecountactive = returned;
1619 /* Get the number of inactive win32 services */
1620 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1621 NULL, 0, &needed, &returned, NULL, NULL);
1622 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1623 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1624 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1625 HeapFree(GetProcessHeap(), 0, exservices);
1627 servicecountinactive = returned;
1629 /* Get the number of win32 services */
1630 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1631 NULL, 0, &needed, &returned, NULL, NULL);
1632 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1633 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1634 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1635 HeapFree(GetProcessHeap(), 0, exservices);
1637 /* Check if total is the same as active and inactive win32 services */
1638 ok(returned == (servicecountactive + servicecountinactive),
1639 "Something wrong in the calculation\n");
1641 /* Get the number of all services */
1642 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1643 SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1644 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1645 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1646 SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1648 /* Check if total is the same as all those single calls */
1649 ok(returned == (drivercountactive + drivercountinactive + servicecountactive + servicecountinactive),
1650 "Something wrong in the calculation\n");
1652 /* Loop through all those returned services */
1653 for (i = 0; i < returned; i++)
1655 SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1658 /* lpServiceName and lpDisplayName should always be filled */
1659 ok(lstrlenA(exservices[i].lpServiceName) > 0, "Expected a service name\n");
1660 ok(lstrlenA(exservices[i].lpDisplayName) > 0, "Expected a display name\n");
1662 /* Decrement the counters to see if the functions calls return the
1663 * same numbers as the contents of these structures.
1664 * Check some process id specifics.
1666 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1668 if (status.dwCurrentState == SERVICE_RUNNING)
1669 drivercountactive--;
1670 else
1671 drivercountinactive--;
1673 /* We shouldn't have a process id for drivers */
1674 ok(status.dwProcessId == 0,
1675 "This driver shouldn't have an associated process id\n");
1678 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1680 if (status.dwCurrentState == SERVICE_RUNNING)
1682 /* We expect a process id for every running service */
1683 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1684 exservices[i].lpServiceName);
1686 servicecountactive--;
1688 else
1690 /* We shouldn't have a process id for inactive services */
1691 ok(status.dwProcessId == 0, "This service shouldn't have an associated process id\n");
1693 servicecountinactive--;
1697 HeapFree(GetProcessHeap(), 0, exservices);
1699 ok(drivercountactive == 0, "Active driver mismatch\n");
1700 ok(drivercountinactive == 0, "Inactive driver mismatch\n");
1701 ok(servicecountactive == 0, "Active services mismatch\n");
1702 ok(servicecountinactive == 0, "Inactive services mismatch\n");
1704 CloseServiceHandle(scm_handle);
1707 static void test_close(void)
1709 SC_HANDLE handle;
1710 BOOL ret;
1712 /* NULL handle */
1713 SetLastError(0xdeadbeef);
1714 ret = CloseServiceHandle(NULL);
1715 ok(!ret, "Expected failure\n");
1716 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1718 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1720 /* Proper call */
1721 handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1722 SetLastError(0xdeadbeef);
1723 ret = CloseServiceHandle(handle);
1724 ok(ret, "Expected success\n");
1725 ok(GetLastError() == ERROR_IO_PENDING /* W2K */ ||
1726 GetLastError() == ERROR_SUCCESS /* W2K3 */ ||
1727 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */,
1728 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
1731 static void test_sequence(void)
1733 SC_HANDLE scm_handle, svc_handle;
1734 BOOL ret;
1735 QUERY_SERVICE_CONFIGA *config;
1736 DWORD given, needed;
1737 static const CHAR servicename [] = "Winetest";
1738 static const CHAR displayname [] = "Winetest dummy service";
1739 static const CHAR displayname2[] = "Winetest dummy service (2)";
1740 static const CHAR pathname [] = "we_dont_care.exe";
1741 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1742 static const CHAR password [] = "";
1743 static const CHAR empty [] = "";
1744 static const CHAR localsystem [] = "LocalSystem";
1746 SetLastError(0xdeadbeef);
1747 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1749 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1751 skip("Not enough rights to get a handle to the manager\n");
1752 return;
1754 else
1755 ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1757 if (!scm_handle) return;
1759 /* Create a dummy service */
1760 SetLastError(0xdeadbeef);
1761 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1762 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1763 pathname, NULL, NULL, dependencies, NULL, password);
1765 if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1767 /* We try and open the service and do the rest of the tests. Some could
1768 * fail if the tests were changed between these runs.
1770 trace("Deletion probably didn't work last time\n");
1771 SetLastError(0xdeadbeef);
1772 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1773 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1775 skip("Not enough rights to open the service\n");
1776 CloseServiceHandle(scm_handle);
1777 return;
1779 ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1781 else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1783 skip("Not enough rights to create the service\n");
1784 CloseServiceHandle(scm_handle);
1785 return;
1787 else
1788 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1790 if (!svc_handle) return;
1792 /* TODO:
1793 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1794 * that the correct keys are used.
1797 /* Request the size for the buffer */
1798 SetLastError(0xdeadbeef);
1799 ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1800 ok(!ret, "Expected failure\n");
1801 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1803 config = HeapAlloc(GetProcessHeap(), 0, needed);
1804 given = needed;
1805 SetLastError(0xdeadbeef);
1806 ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1807 ok(ret, "Expected success\n");
1808 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */||
1809 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */ ||
1810 GetLastError() == ERROR_IO_PENDING /* W2K */,
1811 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
1812 todo_wine
1814 ok(given == needed, "Expected the given (%d) and needed (%d) buffersizes to be equal\n", given, needed);
1816 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1817 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1818 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1819 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1820 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1821 ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1822 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1823 ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1824 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1825 /* TODO: Show the double 0 terminated string */
1826 todo_wine
1828 ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1830 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1831 ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1833 ok(ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2", NULL, NULL, NULL, NULL, displayname2),
1834 "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1836 QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1837 config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1838 ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1839 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1840 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1841 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1842 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1843 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1844 ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1845 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1846 ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1847 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1848 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1849 ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1851 SetLastError(0xdeadbeef);
1852 ret = DeleteService(svc_handle);
1853 ok(ret, "Expected success\n");
1854 ok(GetLastError() == ERROR_SUCCESS /* W2K3 */||
1855 GetLastError() == 0xdeadbeef /* NT4, XP, Vista */ ||
1856 GetLastError() == ERROR_IO_PENDING /* W2K */,
1857 "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\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 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 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 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\n");
2074 /* Get a handle to this new service */
2075 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2076 ok(svc_handle2 != NULL, "Expected success\n");
2078 /* Get another handle to this new service */
2079 svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2080 ok(svc_handle3 != NULL, "Expected success\n");
2082 /* Check if we can close the handle to the Service Control Manager */
2083 ret = CloseServiceHandle(scm_handle);
2084 ok(ret, "Expected success\n");
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\n");
2090 /* Get a handle to this new service */
2091 svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2092 ok(svc_handle4 != NULL, "Expected success\n");
2094 /* Delete the service */
2095 ret = DeleteService(svc_handle4);
2096 ok(ret, "Expected success\n");
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\n");
2123 ret = CloseServiceHandle(svc_handle3);
2124 ok(ret, "Expected success\n");
2125 ret = CloseServiceHandle(svc_handle2);
2126 ok(ret, "Expected success\n");
2127 ret = CloseServiceHandle(svc_handle1);
2128 ok(ret, "Expected success\n");
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\n");
2141 /* Delete the service */
2142 ret = DeleteService(svc_handle5);
2143 ok(ret, "Expected success\n");
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 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();