[ADVAPI32_WINETEST]: Sync with Wine 1.5.19.
[reactos.git] / rostests / winetests / advapi32 / service.c
1 /*
2 * Unit tests for service functions
3 *
4 * Copyright (c) 2007 Paul Vriens
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "winsvc.h"
29 #include "winnls.h"
30 #include "lmcons.h"
31 #include "aclapi.h"
32
33 #include "wine/test.h"
34
35 static const CHAR spooler[] = "Spooler"; /* Should be available on all platforms */
36 static CHAR selfname[MAX_PATH];
37
38 static BOOL (WINAPI *pChangeServiceConfig2A)(SC_HANDLE,DWORD,LPVOID);
39 static BOOL (WINAPI *pEnumServicesStatusExA)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
40 DWORD, LPBYTE, DWORD, LPDWORD,
41 LPDWORD, LPDWORD, LPCSTR);
42 static BOOL (WINAPI *pEnumServicesStatusExW)(SC_HANDLE, SC_ENUM_TYPE, DWORD,
43 DWORD, LPBYTE, DWORD, LPDWORD,
44 LPDWORD, LPDWORD, LPCWSTR);
45 static DWORD (WINAPI *pGetSecurityInfo)(HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
46 PSID*, PSID*, PACL*, PACL*, PSECURITY_DESCRIPTOR*);
47 static BOOL (WINAPI *pQueryServiceConfig2A)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
48 static BOOL (WINAPI *pQueryServiceConfig2W)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD);
49 static BOOL (WINAPI *pQueryServiceStatusEx)(SC_HANDLE, SC_STATUS_TYPE, LPBYTE,
50 DWORD, LPDWORD);
51
52 static void init_function_pointers(void)
53 {
54 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
55
56 pChangeServiceConfig2A = (void*)GetProcAddress(hadvapi32, "ChangeServiceConfig2A");
57 pEnumServicesStatusExA= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExA");
58 pEnumServicesStatusExW= (void*)GetProcAddress(hadvapi32, "EnumServicesStatusExW");
59 pGetSecurityInfo = (void *)GetProcAddress(hadvapi32, "GetSecurityInfo");
60 pQueryServiceConfig2A= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2A");
61 pQueryServiceConfig2W= (void*)GetProcAddress(hadvapi32, "QueryServiceConfig2W");
62 pQueryServiceStatusEx= (void*)GetProcAddress(hadvapi32, "QueryServiceStatusEx");
63 }
64
65 static void test_open_scm(void)
66 {
67 SC_HANDLE scm_handle;
68
69 /* No access rights */
70 SetLastError(0xdeadbeef);
71 scm_handle = OpenSCManagerA(NULL, NULL, 0);
72 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
73 CloseServiceHandle(scm_handle);
74
75 /* Unknown database name */
76 SetLastError(0xdeadbeef);
77 scm_handle = OpenSCManagerA(NULL, "DoesNotExist", SC_MANAGER_CONNECT);
78 ok(!scm_handle, "Expected failure\n");
79 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
80 CloseServiceHandle(scm_handle); /* Just in case */
81
82 /* MSDN says only ServiceActive is allowed, or NULL */
83 SetLastError(0xdeadbeef);
84 scm_handle = OpenSCManagerA(NULL, SERVICES_FAILED_DATABASEA, SC_MANAGER_CONNECT);
85 ok(!scm_handle, "Expected failure\n");
86 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
87 CloseServiceHandle(scm_handle); /* Just in case */
88
89 /* Remote unknown host */
90 SetLastError(0xdeadbeef);
91 scm_handle = OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
92 todo_wine
93 {
94 ok(!scm_handle, "Expected failure\n");
95 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE || GetLastError() == RPC_S_INVALID_NET_ADDR /* w2k8 */,
96 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
97 }
98 CloseServiceHandle(scm_handle); /* Just in case */
99
100 /* Proper call with an empty hostname */
101 SetLastError(0xdeadbeef);
102 scm_handle = OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
103 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
104 CloseServiceHandle(scm_handle);
105
106 /* Again a correct one */
107 SetLastError(0xdeadbeef);
108 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
109 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
110 CloseServiceHandle(scm_handle);
111 }
112
113 static void test_open_svc(void)
114 {
115 SC_HANDLE scm_handle, svc_handle;
116 CHAR displayname[4096];
117 DWORD displaysize;
118
119 /* All NULL (invalid access rights) */
120 SetLastError(0xdeadbeef);
121 svc_handle = OpenServiceA(NULL, NULL, 0);
122 ok(!svc_handle, "Expected failure\n");
123 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
124
125 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
126
127 /* NULL service */
128 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
129 SetLastError(0xdeadbeef);
130 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
131 ok(!svc_handle, "Expected failure\n");
132 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
133 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
134 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
135
136 /* Nonexistent service */
137 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
138 SetLastError(0xdeadbeef);
139 svc_handle = OpenServiceA(scm_handle, "deadbeef", GENERIC_READ);
140 ok(!svc_handle, "Expected failure\n");
141 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
142 CloseServiceHandle(scm_handle);
143
144 /* Proper SCM handle but different access rights */
145 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
146 SetLastError(0xdeadbeef);
147 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_WRITE);
148 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
149 skip("Not enough rights to get a handle to the service\n");
150 else
151 {
152 ok(svc_handle != NULL, "Expected success, got error %u\n", GetLastError());
153 CloseServiceHandle(svc_handle);
154 }
155
156 /* Test to show we can't open a service with the displayname */
157
158 /* Retrieve the needed size for the buffer */
159 displaysize = 0;
160 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
161 /* Get the displayname */
162 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
163 /* Try to open the service with this displayname, unless the displayname equals
164 * the servicename as that would defeat the purpose of this test.
165 */
166 if (!lstrcmpi(spooler, displayname))
167 {
168 skip("displayname equals servicename\n");
169 CloseServiceHandle(scm_handle);
170 return;
171 }
172
173 SetLastError(0xdeadbeef);
174 svc_handle = OpenServiceA(scm_handle, displayname, GENERIC_READ);
175 ok(!svc_handle, "Expected failure\n");
176 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
177 /* Just in case */
178 CloseServiceHandle(svc_handle);
179
180 CloseServiceHandle(scm_handle);
181 }
182
183 static void test_create_delete_svc(void)
184 {
185 SC_HANDLE scm_handle, svc_handle1;
186 CHAR username[UNLEN + 1], domain[MAX_PATH];
187 DWORD user_size = UNLEN + 1;
188 CHAR account[UNLEN + 3];
189 static const CHAR servicename [] = "Winetest";
190 static const CHAR pathname [] = "we_dont_care.exe";
191 static const CHAR empty [] = "";
192 static const CHAR password [] = "secret";
193 BOOL spooler_exists = FALSE;
194 BOOL ret;
195 CHAR display[4096];
196 DWORD display_size = sizeof(display);
197
198 /* Get the username and turn it into an account to be used in some tests */
199 GetUserNameA(username, &user_size);
200 /* Get the domainname to cater for that situation */
201 if (GetEnvironmentVariableA("USERDOMAIN", domain, MAX_PATH))
202 sprintf(account, "%s\\%s", domain, username);
203 else
204 sprintf(account, ".\\%s", username);
205
206 /* All NULL */
207 SetLastError(0xdeadbeef);
208 svc_handle1 = CreateServiceA(NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
209 ok(!svc_handle1, "Expected failure\n");
210 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
211
212 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
213
214 /* Only a valid handle to the Service Control Manager */
215 SetLastError(0xdeadbeef);
216 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
217 ok(!svc_handle1, "Expected failure\n");
218 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
219 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
220 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
221
222 /* Now with a servicename */
223 SetLastError(0xdeadbeef);
224 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
225 ok(!svc_handle1, "Expected failure\n");
226 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
227 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
228 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
229
230 /* Or just a binary name */
231 SetLastError(0xdeadbeef);
232 svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
233 ok(!svc_handle1, "Expected failure\n");
234 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, W2K3, XP, Vista */ ||
235 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
236 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
237
238 /* Both servicename and binary name (We only have connect rights) */
239 SetLastError(0xdeadbeef);
240 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
241 ok(!svc_handle1, "Expected failure\n");
242 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
243
244 /* They can even be empty at this stage of parameter checking */
245 SetLastError(0xdeadbeef);
246 svc_handle1 = CreateServiceA(scm_handle, empty, 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());
249
250 SetLastError(0xdeadbeef);
251 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
252 ok(!svc_handle1, "Expected failure\n");
253 ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
254
255 /* Open the Service Control Manager with minimal rights for creation
256 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
257 */
258 CloseServiceHandle(scm_handle);
259 SetLastError(0xdeadbeef);
260 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
261 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
262 {
263 skip("Not enough rights to get a handle to the manager\n");
264 return;
265 }
266
267 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
268
269 /* Empty strings for servicename and binary name are checked */
270 SetLastError(0xdeadbeef);
271 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
272 ok(!svc_handle1, "Expected failure\n");
273 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
274
275 SetLastError(0xdeadbeef);
276 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
277 ok(!svc_handle1, "Expected failure\n");
278 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
279
280 SetLastError(0xdeadbeef);
281 svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
282 ok(!svc_handle1, "Expected failure\n");
283 ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
284
285 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
286 * an ERROR_INVALID_PARAMETER)
287 */
288 SetLastError(0xdeadbeef);
289 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
290 SERVICE_DISABLED, 0, empty, NULL, NULL, NULL, NULL, NULL);
291 ok(!svc_handle1, "Expected failure\n");
292 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
293
294 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
295
296 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
297 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
298 */
299 SetLastError(0xdeadbeef);
300 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS,
301 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
302 ok(!svc_handle1, "Expected failure\n");
303 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
304
305 /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
306 SetLastError(0xdeadbeef);
307 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_FILE_SYSTEM_DRIVER | SERVICE_INTERACTIVE_PROCESS,
308 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
309 ok(!svc_handle1, "Expected failure\n");
310 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
311
312 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
313 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
314 */
315 SetLastError(0xdeadbeef);
316 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
317 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, account, password);
318 ok(!svc_handle1, "Expected failure\n");
319 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INVALID_SERVICE_ACCOUNT,
320 "Expected ERROR_INVALID_PARAMETER or ERROR_INVALID_SERVICE_ACCOUNT, got %d\n", GetLastError());
321
322 /* Illegal (start-type is not a mask and should only be one of the possibilities)
323 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
324 * it's most likely not the wanted start-type)
325 */
326 SetLastError(0xdeadbeef);
327 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS,
328 SERVICE_AUTO_START | SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
329 ok(!svc_handle1, "Expected failure\n");
330 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
331
332 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
333 SetLastError(0xdeadbeef);
334 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
335 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
336 ok(!svc_handle1, "Expected failure\n");
337 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
338
339 /* Test if ServiceType can be a combined one for drivers */
340 SetLastError(0xdeadbeef);
341 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER,
342 SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
343 ok(!svc_handle1, "Expected failure\n");
344 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
345
346 /* The service already exists (check first, just in case) */
347 svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
348 if (svc_handle1)
349 {
350 spooler_exists = TRUE;
351 CloseServiceHandle(svc_handle1);
352 SetLastError(0xdeadbeef);
353 svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
354 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
355 ok(!svc_handle1, "Expected failure\n");
356 ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
357 }
358 else
359 skip("Spooler service doesn't exist\n");
360
361 /* To find an existing displayname we check the 'Spooler' service. Although the registry
362 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
363 * to the servicename and can't be used as well for a new displayname.
364 */
365 if (spooler_exists)
366 {
367 ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
368
369 if (!ret)
370 skip("Could not retrieve a displayname for the Spooler service\n");
371 else
372 {
373 svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
374 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
375 ok(!svc_handle1, "Expected failure\n");
376 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
377 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
378 }
379 }
380 else
381 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
382
383 /* Windows doesn't care about the access rights for creation (which makes
384 * sense as there is no service yet) as long as there are sufficient
385 * rights to the manager.
386 */
387 SetLastError(0xdeadbeef);
388 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
389 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
390 ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
391
392 /* DeleteService however must have proper rights */
393 SetLastError(0xdeadbeef);
394 ret = DeleteService(svc_handle1);
395 ok(!ret, "Expected failure\n");
396 ok(GetLastError() == ERROR_ACCESS_DENIED,
397 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
398
399 /* Open the service with minimal rights for deletion.
400 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
401 */
402 CloseServiceHandle(svc_handle1);
403 svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
404
405 /* Now that we have the proper rights, we should be able to delete */
406 SetLastError(0xdeadbeef);
407 ret = DeleteService(svc_handle1);
408 ok(ret, "Expected success, got error %u\n", GetLastError());
409
410 CloseServiceHandle(svc_handle1);
411 CloseServiceHandle(scm_handle);
412
413 /* Wait a while. One of the following tests also does a CreateService for the
414 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
415 * error if we do this to quick. Vista seems more picky then the others.
416 */
417 Sleep(1000);
418
419 /* And a final NULL check */
420 SetLastError(0xdeadbeef);
421 ret = DeleteService(NULL);
422 ok(!ret, "Expected failure\n");
423 ok(GetLastError() == ERROR_INVALID_HANDLE,
424 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
425 }
426
427 static void test_get_displayname(void)
428 {
429 SC_HANDLE scm_handle, svc_handle;
430 BOOL ret;
431 CHAR displayname[4096];
432 WCHAR displaynameW[2048];
433 DWORD displaysize, tempsize, tempsizeW;
434 static const CHAR deadbeef[] = "Deadbeef";
435 static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
436 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
437 static const WCHAR abcW[] = {'A','B','C',0};
438 static const CHAR servicename[] = "Winetest";
439 static const CHAR pathname[] = "we_dont_care.exe";
440
441 /* Having NULL for the size of the buffer will crash on W2K3 */
442
443 SetLastError(0xdeadbeef);
444 ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
445 ok(!ret, "Expected failure\n");
446 ok(GetLastError() == ERROR_INVALID_HANDLE,
447 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
448
449 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
450
451 SetLastError(0xdeadbeef);
452 ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
453 ok(!ret, "Expected failure\n");
454 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
455 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
456 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
457
458 SetLastError(0xdeadbeef);
459 displaysize = sizeof(displayname);
460 ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
461 ok(!ret, "Expected failure\n");
462 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
463 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
464 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
465
466 /* Test for nonexistent service */
467 SetLastError(0xdeadbeef);
468 displaysize = -1;
469 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
470 ok(!ret, "Expected failure\n");
471 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
472 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
473
474 SetLastError(0xdeadbeef);
475 ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
476 ok(!ret, "Expected failure\n");
477 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
478 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
479 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
480
481 displaysize = 15;
482 strcpy(displayname, "ABC");
483 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
484 ok(!ret, "Expected failure\n");
485 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
486 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
487 todo_wine ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
488 ok(displayname[0] == 0, "Service name not empty\n");
489
490 displaysize = 15;
491 lstrcpyW( displaynameW, abcW );
492 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
493 ok(!ret, "Expected failure\n");
494 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
495 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
496 ok(displaysize == 15, "Service size expected 15, got %d\n", displaysize);
497 ok(displaynameW[0] == 0, "Service name not empty\n");
498
499 displaysize = 0;
500 strcpy(displayname, "ABC");
501 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
502 ok(!ret, "Expected failure\n");
503 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
504 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
505 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
506 ok(displayname[0] == 'A', "Service name changed\n");
507
508 displaysize = 0;
509 lstrcpyW( displaynameW, abcW );
510 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
511 ok(!ret, "Expected failure\n");
512 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
513 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
514 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
515 ok(displaynameW[0] == 'A', "Service name changed\n");
516
517 displaysize = 1;
518 strcpy(displayname, "ABC");
519 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
520 ok(!ret, "Expected failure\n");
521 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
522 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
523 todo_wine ok(displaysize == 1, "Service size expected 1, got %d\n", displaysize);
524 ok(displayname[0] == 0, "Service name not empty\n");
525
526 displaysize = 1;
527 lstrcpyW( displaynameW, abcW );
528 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
529 ok(!ret, "Expected failure\n");
530 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
531 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
532 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
533 ok(displaynameW[0] == 'A', "Service name changed\n");
534
535 displaysize = 2;
536 strcpy(displayname, "ABC");
537 ret = GetServiceDisplayNameA(scm_handle, deadbeef, displayname, &displaysize);
538 ok(!ret, "Expected failure\n");
539 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
540 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
541 todo_wine ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
542 ok(displayname[0] == 0, "Service name not empty\n");
543
544 displaysize = 2;
545 lstrcpyW( displaynameW, abcW );
546 ret = GetServiceDisplayNameW(scm_handle, deadbeefW, displaynameW, &displaysize);
547 ok(!ret, "Expected failure\n");
548 ok(displaysize == 2, "Service size expected 2, got %d\n", displaysize);
549 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
550 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
551 ok(displaynameW[0] == 0, "Service name not empty\n");
552
553 /* Check if 'Spooler' exists */
554 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
555 if (!svc_handle)
556 {
557 skip("Spooler service doesn't exist\n");
558 CloseServiceHandle(scm_handle);
559 return;
560 }
561 CloseServiceHandle(svc_handle);
562
563 /* Retrieve the needed size for the buffer */
564 SetLastError(0xdeadbeef);
565 displaysize = -1;
566 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
567 ok(!ret, "Expected failure\n");
568 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
569 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
570 tempsize = displaysize;
571
572 displaysize = 0;
573 ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
574 ok(!ret, "Expected failure\n");
575 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
576 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
577 ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
578
579 /* Buffer is too small */
580 SetLastError(0xdeadbeef);
581 displaysize = (tempsize / 2);
582 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
583 ok(!ret, "Expected failure\n");
584 ok(displaysize == tempsize, "Expected the needed buffersize\n");
585 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
586 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
587
588 /* First try with a buffer that should be big enough to hold
589 * the ANSI string (and terminating character). This succeeds on Windows
590 * although when asked (see above 2 tests) it will return twice the needed size.
591 */
592 SetLastError(0xdeadbeef);
593 displaysize = (tempsize / 2) + 1;
594 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
595 ok(ret, "Expected success, got error %u\n", GetLastError());
596 ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
597
598 /* Now with the original returned size */
599 SetLastError(0xdeadbeef);
600 displaysize = tempsize;
601 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
602 ok(ret, "Expected success, got error %u\n", GetLastError());
603 ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
604
605 /* And with a bigger than needed buffer */
606 SetLastError(0xdeadbeef);
607 displaysize = tempsize * 2;
608 ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
609 ok(ret, "Expected success, got error %u\n", GetLastError());
610 /* Test that shows that if the buffersize is enough, it's not changed */
611 ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
612 ok(strlen(displayname) == tempsize/2,
613 "Expected the buffer to be twice the length of the string\n") ;
614
615 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
616 SetLastError(0xdeadbeef);
617 displaysize = -1;
618 ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
619 ok(!ret, "Expected failure\n");
620 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
621 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
622
623 /* Buffer is too small */
624 SetLastError(0xdeadbeef);
625 tempsizeW = displaysize;
626 displaysize = tempsizeW / 2;
627 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
628 ok(!ret, "Expected failure\n");
629 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
630 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
631 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
632
633 /* Now with the original returned size */
634 SetLastError(0xdeadbeef);
635 displaysize = tempsizeW;
636 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
637 ok(!ret, "Expected failure\n");
638 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
639 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
640 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
641
642 /* And with a bigger than needed buffer */
643 SetLastError(0xdeadbeef);
644 displaysize = tempsizeW + 1; /* This caters for the null terminating character */
645 ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
646 ok(ret, "Expected success, got error %u\n", GetLastError());
647 ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
648 ok(lstrlenW(displaynameW) == displaysize,
649 "Expected the buffer to be the length of the string\n") ;
650 ok(tempsize / 2 == tempsizeW,
651 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
652
653 CloseServiceHandle(scm_handle);
654
655 /* Test for a service without a displayname (which is valid). This should return
656 * the servicename itself.
657 */
658 SetLastError(0xdeadbeef);
659 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
660 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
661 {
662 skip("Not enough rights to get a handle to the manager\n");
663 return;
664 }
665
666 SetLastError(0xdeadbeef);
667 svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
668 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
669 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
670 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
671 if (!svc_handle)
672 {
673 CloseServiceHandle(scm_handle);
674 return;
675 }
676
677 /* Retrieve the needed size for the buffer */
678 SetLastError(0xdeadbeef);
679 displaysize = -1;
680 ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
681 ok(!ret, "Expected failure\n");
682 ok(displaysize == strlen(servicename) * 2,
683 "Expected the displaysize to be twice the size of the servicename\n");
684 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
685 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
686
687 /* Buffer is too small */
688 SetLastError(0xdeadbeef);
689 tempsize = displaysize;
690 displaysize = (tempsize / 2);
691 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
692 ok(!ret, "Expected failure\n");
693 ok(displaysize == tempsize, "Expected the needed buffersize\n");
694 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
695 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
696
697 /* Get the displayname */
698 SetLastError(0xdeadbeef);
699 ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
700 ok(ret, "Expected success, got error %u\n", GetLastError());
701 ok(!lstrcmpi(displayname, servicename),
702 "Expected displayname to be %s, got %s\n", servicename, displayname);
703
704 /* Delete the service */
705 ret = DeleteService(svc_handle);
706 ok(ret, "Expected success (err=%d)\n", GetLastError());
707
708 CloseServiceHandle(svc_handle);
709 CloseServiceHandle(scm_handle);
710
711 /* Wait a while. Just in case one of the following tests does a CreateService again */
712 Sleep(1000);
713 }
714
715 static void test_get_servicekeyname(void)
716 {
717 SC_HANDLE scm_handle, svc_handle;
718 CHAR servicename[4096];
719 CHAR displayname[4096];
720 WCHAR servicenameW[4096];
721 WCHAR displaynameW[4096];
722 DWORD servicesize, displaysize, tempsize;
723 BOOL ret;
724 static const CHAR deadbeef[] = "Deadbeef";
725 static const WCHAR deadbeefW[] = {'D','e','a','d','b','e','e','f',0};
726 static const WCHAR abcW[] = {'A','B','C',0};
727
728 /* Having NULL for the size of the buffer will crash on W2K3 */
729
730 SetLastError(0xdeadbeef);
731 ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
732 ok(!ret, "Expected failure\n");
733 ok(GetLastError() == ERROR_INVALID_HANDLE,
734 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
735
736 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
737
738 servicesize = 200;
739 SetLastError(0xdeadbeef);
740 ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
741 ok(!ret, "Expected failure\n");
742 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
743 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
744 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
745 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
746
747 /* Valid handle and buffer but no displayname */
748 servicesize = 200;
749 SetLastError(0xdeadbeef);
750 ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
751 ok(!ret, "Expected failure\n");
752 ok(GetLastError() == ERROR_INVALID_ADDRESS /* W2K, XP, W2K3, Vista */ ||
753 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
754 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
755 todo_wine ok(servicesize == 200, "Service size expected 1, got %d\n", servicesize);
756
757 /* Test for nonexistent displayname */
758 SetLastError(0xdeadbeef);
759 ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
760 ok(!ret, "Expected failure\n");
761 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
762 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
763 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
764
765 servicesize = 15;
766 strcpy(servicename, "ABC");
767 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
768 ok(!ret, "Expected failure\n");
769 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
770 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
771 todo_wine ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
772 ok(servicename[0] == 0, "Service name not empty\n");
773
774 servicesize = 15;
775 lstrcpyW( servicenameW, abcW );
776 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
777 ok(!ret, "Expected failure\n");
778 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
779 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
780 ok(servicesize == 15, "Service size expected 15, got %d\n", servicesize);
781 ok(servicenameW[0] == 0, "Service name not empty\n");
782
783 servicesize = 0;
784 strcpy(servicename, "ABC");
785 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
786 ok(!ret, "Expected failure\n");
787 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
788 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
789 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
790 ok(servicename[0] == 'A', "Service name changed\n");
791
792 servicesize = 0;
793 lstrcpyW( servicenameW, abcW );
794 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
795 ok(!ret, "Expected failure\n");
796 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
797 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
798 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
799 ok(servicenameW[0] == 'A', "Service name changed\n");
800
801 servicesize = 1;
802 strcpy(servicename, "ABC");
803 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
804 ok(!ret, "Expected failure\n");
805 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
806 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
807 todo_wine ok(servicesize == 1, "Service size expected 1, got %d\n", servicesize);
808 ok(servicename[0] == 0, "Service name not empty\n");
809
810 servicesize = 1;
811 lstrcpyW( servicenameW, abcW );
812 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
813 ok(!ret, "Expected failure\n");
814 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
815 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
816 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
817 ok(servicenameW[0] == 'A', "Service name changed\n");
818
819 servicesize = 2;
820 strcpy(servicename, "ABC");
821 ret = GetServiceKeyNameA(scm_handle, deadbeef, servicename, &servicesize);
822 ok(!ret, "Expected failure\n");
823 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
824 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
825 todo_wine ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
826 ok(servicename[0] == 0, "Service name not empty\n");
827
828 servicesize = 2;
829 lstrcpyW( servicenameW, abcW );
830 ret = GetServiceKeyNameW(scm_handle, deadbeefW, servicenameW, &servicesize);
831 ok(!ret, "Expected failure\n");
832 ok(servicesize == 2, "Service size expected 2, got %d\n", servicesize);
833 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
834 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
835 ok(servicenameW[0] == 0, "Service name not empty\n");
836
837 /* Check if 'Spooler' exists */
838 svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
839 if (!svc_handle)
840 {
841 skip("Spooler service doesn't exist\n");
842 CloseServiceHandle(scm_handle);
843 return;
844 }
845 CloseServiceHandle(svc_handle);
846
847 /* Get the displayname for the 'Spooler' service */
848 GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
849 GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
850
851 /* Retrieve the needed size for the buffer */
852 SetLastError(0xdeadbeef);
853 servicesize = 0;
854 ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
855 ok(!ret, "Expected failure\n");
856 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
857 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
858
859 /* Valid call with the correct buffersize */
860 SetLastError(0xdeadbeef);
861 tempsize = servicesize;
862 servicesize *= 2;
863 ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
864 ok(ret, "Expected success, got error %u\n", GetLastError());
865 if (ret)
866 {
867 ok(strlen(servicename) == tempsize/2,
868 "Expected the buffer to be twice the length of the string\n") ;
869 ok(!lstrcmpi(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
870 ok(servicesize == (tempsize * 2),
871 "Expected servicesize not to change if buffer not insufficient\n") ;
872 }
873
874 MultiByteToWideChar(CP_ACP, 0, displayname, -1, displaynameW, sizeof(displaynameW)/2);
875 SetLastError(0xdeadbeef);
876 servicesize *= 2;
877 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
878 ok(ret, "Expected success, got error %u\n", GetLastError());
879 if (ret)
880 {
881 ok(strlen(servicename) == tempsize/2,
882 "Expected the buffer to be twice the length of the string\n") ;
883 ok(servicesize == lstrlenW(servicenameW),
884 "Expected servicesize not to change if buffer not insufficient\n") ;
885 }
886
887 SetLastError(0xdeadbeef);
888 servicesize = 3;
889 ret = GetServiceKeyNameW(scm_handle, displaynameW, servicenameW, &servicesize);
890 ok(!ret, "Expected failure\n");
891 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
892 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
893 ok(servicenameW[0] == 0, "Buffer not empty\n");
894
895 CloseServiceHandle(scm_handle);
896 }
897
898 static void test_query_svc(void)
899 {
900 SC_HANDLE scm_handle, svc_handle;
901 BOOL ret;
902 SERVICE_STATUS status;
903 SERVICE_STATUS_PROCESS *statusproc;
904 DWORD bufsize, needed;
905
906 /* All NULL or wrong */
907 SetLastError(0xdeadbeef);
908 ret = QueryServiceStatus(NULL, NULL);
909 ok(!ret, "Expected failure\n");
910 ok(GetLastError() == ERROR_INVALID_HANDLE,
911 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
912
913 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
914
915 /* Check if 'Spooler' exists.
916 * Open with not enough rights to query the status.
917 */
918 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
919 if (!svc_handle)
920 {
921 skip("Spooler service doesn't exist\n");
922 CloseServiceHandle(scm_handle);
923 return;
924 }
925
926 SetLastError(0xdeadbeef);
927 ret = QueryServiceStatus(svc_handle, NULL);
928 ok(!ret, "Expected failure\n");
929 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
930 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
931 "Unexpected last error %d\n", GetLastError());
932
933 SetLastError(0xdeadbeef);
934 ret = QueryServiceStatus(svc_handle, &status);
935 ok(!ret, "Expected failure\n");
936 ok(GetLastError() == ERROR_ACCESS_DENIED,
937 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
938
939 /* Open the service with just enough rights.
940 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
941 */
942 CloseServiceHandle(svc_handle);
943 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
944
945 SetLastError(0xdeadbeef);
946 ret = QueryServiceStatus(svc_handle, &status);
947 ok(ret, "Expected success, got error %u\n", GetLastError());
948
949 CloseServiceHandle(svc_handle);
950
951 /* More or less the same tests for QueryServiceStatusEx */
952 if (!pQueryServiceStatusEx)
953 {
954 win_skip( "QueryServiceStatusEx not available\n" );
955 CloseServiceHandle(scm_handle);
956 return;
957 }
958
959 /* Open service with not enough rights to query the status */
960 svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
961
962 /* All NULL or wrong, this proves that info level is checked first */
963 SetLastError(0xdeadbeef);
964 ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
965 ok(!ret, "Expected failure\n");
966 ok(GetLastError() == ERROR_INVALID_LEVEL,
967 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
968
969 /* Passing a NULL parameter for the needed buffer size
970 * will crash on anything but NT4.
971 */
972
973 /* Only info level is correct. It looks like the buffer/size is checked second */
974 SetLastError(0xdeadbeef);
975 ret = pQueryServiceStatusEx(NULL, SC_STATUS_PROCESS_INFO, NULL, 0, &needed);
976 /* NT4 checks the handle first */
977 if (GetLastError() != ERROR_INVALID_HANDLE)
978 {
979 ok(!ret, "Expected failure\n");
980 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
981 "Needed buffersize is wrong : %d\n", needed);
982 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
983 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
984 }
985
986 /* Pass a correct buffer and buffersize but a NULL handle */
987 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
988 bufsize = needed;
989 SetLastError(0xdeadbeef);
990 ret = pQueryServiceStatusEx(NULL, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
991 ok(!ret, "Expected failure\n");
992 ok(GetLastError() == ERROR_INVALID_HANDLE,
993 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
994 HeapFree(GetProcessHeap(), 0, statusproc);
995
996 /* Correct handle and info level */
997 SetLastError(0xdeadbeef);
998 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, NULL, 0, &needed);
999 /* NT4 doesn't return the needed size */
1000 if (GetLastError() != ERROR_INVALID_PARAMETER)
1001 {
1002 ok(!ret, "Expected failure\n");
1003 ok(needed == sizeof(SERVICE_STATUS_PROCESS),
1004 "Needed buffersize is wrong : %d\n", needed);
1005 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1006 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1007 }
1008
1009 /* All parameters are OK but we don't have enough rights */
1010 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1011 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1012 SetLastError(0xdeadbeef);
1013 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
1014 ok(!ret, "Expected failure\n");
1015 ok(GetLastError() == ERROR_ACCESS_DENIED,
1016 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1017 HeapFree(GetProcessHeap(), 0, statusproc);
1018
1019 /* Open the service with just enough rights. */
1020 CloseServiceHandle(svc_handle);
1021 svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
1022
1023 /* Everything should be fine now. */
1024 statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1025 bufsize = sizeof(SERVICE_STATUS_PROCESS);
1026 SetLastError(0xdeadbeef);
1027 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
1028 ok(ret, "Expected success, got error %u\n", GetLastError());
1029 if (statusproc->dwCurrentState == SERVICE_RUNNING)
1030 ok(statusproc->dwProcessId != 0,
1031 "Expect a process id for this running service\n");
1032 else
1033 ok(statusproc->dwProcessId == 0,
1034 "Expect no process id for this stopped service\n");
1035
1036 /* same call with null needed pointer */
1037 SetLastError(0xdeadbeef);
1038 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, NULL);
1039 ok(!ret, "Expected failure\n");
1040 ok(broken(GetLastError() == ERROR_INVALID_PARAMETER) /* NT4 */ ||
1041 GetLastError() == ERROR_INVALID_ADDRESS, "got %d\n", GetLastError());
1042
1043 HeapFree(GetProcessHeap(), 0, statusproc);
1044
1045 CloseServiceHandle(svc_handle);
1046 CloseServiceHandle(scm_handle);
1047 }
1048
1049 static void test_enum_svc(void)
1050 {
1051 SC_HANDLE scm_handle;
1052 BOOL ret;
1053 DWORD bufsize, needed, returned, resume;
1054 DWORD neededW, returnedW;
1055 DWORD tempneeded, tempreturned, missing;
1056 DWORD servicecountactive, servicecountinactive;
1057 ENUM_SERVICE_STATUS *services;
1058 ENUM_SERVICE_STATUSW *servicesW;
1059 ENUM_SERVICE_STATUS_PROCESS *exservices;
1060 UINT i;
1061
1062 /* All NULL or wrong */
1063 SetLastError(0xdeadbeef);
1064 ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1065 ok(!ret, "Expected failure\n");
1066 ok(GetLastError() == ERROR_INVALID_HANDLE,
1067 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1068
1069 SetLastError(0xdeadbeef);
1070 ret = EnumServicesStatusW(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1071 ok(!ret, "Expected failure\n");
1072 ok(GetLastError() == ERROR_INVALID_HANDLE,
1073 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1074
1075 /* Open the service control manager with not enough rights at first */
1076 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1077
1078 /* Valid handle but rest is still NULL or wrong */
1079 SetLastError(0xdeadbeef);
1080 ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1081 ok(!ret, "Expected failure\n");
1082 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1083 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1084 "Unexpected last error %d\n", GetLastError());
1085
1086 SetLastError(0xdeadbeef);
1087 ret = EnumServicesStatusW(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1088 ok(!ret, "Expected failure\n");
1089 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1090 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1091 "Unexpected last error %d\n", GetLastError());
1092
1093 /* Don't specify the two required pointers */
1094 returned = 0xdeadbeef;
1095 SetLastError(0xdeadbeef);
1096 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1097 ok(!ret, "Expected failure\n");
1098 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1099 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1100 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1101 "Unexpected last error %d\n", GetLastError());
1102
1103 returned = 0xdeadbeef;
1104 SetLastError(0xdeadbeef);
1105 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1106 ok(!ret, "Expected failure\n");
1107 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1108 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1109 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1110 "Unexpected last error %d\n", GetLastError());
1111
1112 /* Don't specify the two required pointers */
1113 needed = 0xdeadbeef;
1114 SetLastError(0xdeadbeef);
1115 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1116 ok(!ret, "Expected failure\n");
1117 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1118 "Expected no change to the needed buffer variable\n");
1119 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1120 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1121 "Unexpected last error %d\n", GetLastError());
1122
1123 needed = 0xdeadbeef;
1124 SetLastError(0xdeadbeef);
1125 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1126 ok(!ret, "Expected failure\n");
1127 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1128 "Expected no change to the needed buffer variable\n");
1129 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1130 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1131 "Unexpected last error %d\n", GetLastError());
1132
1133 /* No valid servicetype and servicestate */
1134 needed = 0xdeadbeef;
1135 returned = 0xdeadbeef;
1136 SetLastError(0xdeadbeef);
1137 ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1138 ok(!ret, "Expected failure\n");
1139 ok(needed == 0 || broken(needed != 0), /* nt4 */
1140 "Expected needed buffer size to be set to 0, got %d\n", needed);
1141 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1142 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1143 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1144
1145 needed = 0xdeadbeef;
1146 returned = 0xdeadbeef;
1147 SetLastError(0xdeadbeef);
1148 ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1149 ok(!ret, "Expected failure\n");
1150 ok(needed == 0 || broken(needed != 0), /* nt4 */
1151 "Expected needed buffer size to be set to 0, got %d\n", needed);
1152 ok(returned == 0 || broken(returned != 0), /* nt4 */
1153 "Expected number of services to be set to 0, got %d\n", returned);
1154 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1155 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1156
1157 /* No valid servicestate */
1158 needed = 0xdeadbeef;
1159 returned = 0xdeadbeef;
1160 SetLastError(0xdeadbeef);
1161 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1162 ok(!ret, "Expected failure\n");
1163 ok(needed == 0 || broken(needed != 0), /* nt4 */
1164 "Expected needed buffer size to be set to 0, got %d\n", needed);
1165 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1166 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1167 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1168
1169 needed = 0xdeadbeef;
1170 returned = 0xdeadbeef;
1171 SetLastError(0xdeadbeef);
1172 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1173 ok(!ret, "Expected failure\n");
1174 ok(needed == 0 || broken(needed != 0), /* nt4 */
1175 "Expected needed buffer size to be set to 0, got %d\n", needed);
1176 ok(returned == 0 || broken(returned != 0), /* nt4 */
1177 "Expected number of services to be set to 0, got %d\n", returned);
1178 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1179 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1180
1181 /* No valid servicetype */
1182 needed = 0xdeadbeef;
1183 returned = 0xdeadbeef;
1184 SetLastError(0xdeadbeef);
1185 ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1186 ok(!ret, "Expected failure\n");
1187 ok(needed == 0 || broken(needed != 0), /* nt4 */
1188 "Expected needed buffer size to be set to 0, got %d\n", needed);
1189 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1190 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1191 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1192
1193 needed = 0xdeadbeef;
1194 returned = 0xdeadbeef;
1195 SetLastError(0xdeadbeef);
1196 ret = EnumServicesStatusW(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1197 ok(!ret, "Expected failure\n");
1198 ok(needed == 0 || broken(needed != 0), /* nt4 */
1199 "Expected needed buffer size to be set to 0, got %d\n", needed);
1200 ok(returned == 0 || broken(returned != 0), /* nt4 */
1201 "Expected number of services to be set to 0, got %d\n", returned);
1202 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1203 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1204
1205 /* All parameters are correct but our access rights are wrong */
1206 needed = 0xdeadbeef;
1207 returned = 0xdeadbeef;
1208 SetLastError(0xdeadbeef);
1209 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1210 ok(!ret, "Expected failure\n");
1211 ok(needed == 0 || broken(needed != 0), /* nt4 */
1212 "Expected needed buffer size to be set to 0, got %d\n", needed);
1213 ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1214 ok(GetLastError() == ERROR_ACCESS_DENIED,
1215 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1216
1217 needed = 0xdeadbeef;
1218 returned = 0xdeadbeef;
1219 SetLastError(0xdeadbeef);
1220 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1221 ok(!ret, "Expected failure\n");
1222 ok(needed == 0 || broken(needed != 0), /* nt4 */
1223 "Expected needed buffer size to be set to 0, got %d\n", needed);
1224 ok(returned == 0 || broken(returned != 0), /* nt4 */
1225 "Expected number of services to be set to 0, got %d\n", returned);
1226 ok(GetLastError() == ERROR_ACCESS_DENIED,
1227 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1228
1229 /* Open the service control manager with the needed rights */
1230 CloseServiceHandle(scm_handle);
1231 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1232
1233 /* All parameters are correct. Request the needed buffer size */
1234 needed = 0xdeadbeef;
1235 returned = 0xdeadbeef;
1236 SetLastError(0xdeadbeef);
1237 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1238 ok(!ret, "Expected failure\n");
1239 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1240 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1241 ok(GetLastError() == ERROR_MORE_DATA,
1242 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1243
1244 /* Test to show we get the same needed buffer size for the W-call */
1245 neededW = 0xdeadbeef;
1246 returnedW = 0xdeadbeef;
1247 SetLastError(0xdeadbeef);
1248 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &neededW, &returnedW, NULL);
1249 ok(!ret, "Expected failure\n");
1250 ok(neededW != 0xdeadbeef && neededW > 0, "Expected the needed buffer size for this one service\n");
1251 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1252 ok(returnedW == 0, "Expected no service returned, got %d\n", returnedW);
1253 ok(GetLastError() == ERROR_MORE_DATA,
1254 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1255
1256 /* Store the needed bytes */
1257 tempneeded = needed;
1258
1259 /* Allocate the correct needed bytes */
1260 services = HeapAlloc(GetProcessHeap(), 0, needed);
1261 bufsize = needed;
1262 needed = 0xdeadbeef;
1263 returned = 0xdeadbeef;
1264 SetLastError(0xdeadbeef);
1265 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1266 services, bufsize, &needed, &returned, NULL);
1267 ok(ret, "Expected success, got error %u\n", GetLastError());
1268 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1269 ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1270 HeapFree(GetProcessHeap(), 0, services);
1271
1272 /* Store the number of returned services */
1273 tempreturned = returned;
1274
1275 servicesW = HeapAlloc(GetProcessHeap(), 0, neededW);
1276 bufsize = neededW;
1277 neededW = 0xdeadbeef;
1278 returnedW = 0xdeadbeef;
1279 SetLastError(0xdeadbeef);
1280 ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1281 servicesW, bufsize, &neededW, &returnedW, NULL);
1282 ok(ret, "Expected success, got error %u\n", GetLastError());
1283 ok(neededW == 0, "Expected needed buffer to be 0 as we are done\n");
1284 ok(returnedW != 0xdeadbeef && returnedW > 0, "Expected some returned services\n");
1285 HeapFree(GetProcessHeap(), 0, servicesW);
1286
1287 /* Allocate less than the needed bytes and don't specify a resume handle */
1288 services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1289 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1290 needed = 0xdeadbeef;
1291 returned = 0xdeadbeef;
1292 SetLastError(0xdeadbeef);
1293 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1294 services, bufsize, &needed, &returned, NULL);
1295 ok(!ret, "Expected failure\n");
1296 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1297 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1298 ok(GetLastError() == ERROR_MORE_DATA,
1299 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1300
1301 /* Allocate less than the needed bytes, this time with a correct resume handle */
1302 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1303 needed = 0xdeadbeef;
1304 returned = 0xdeadbeef;
1305 resume = 0;
1306 SetLastError(0xdeadbeef);
1307 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1308 services, bufsize, &needed, &returned, &resume);
1309 ok(!ret, "Expected failure\n");
1310 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1311 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1312 todo_wine ok(resume, "Expected a resume handle\n");
1313 ok(GetLastError() == ERROR_MORE_DATA,
1314 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1315
1316 /* Fetch the missing services but pass a bigger buffer size */
1317 missing = tempreturned - returned;
1318 bufsize = tempneeded;
1319 needed = 0xdeadbeef;
1320 returned = 0xdeadbeef;
1321 SetLastError(0xdeadbeef);
1322 ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1323 services, bufsize, &needed, &returned, &resume);
1324 ok(ret, "Expected success, got error %u\n", GetLastError());
1325 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1326 ok(returned == missing, "Expected %u services to be returned\n", missing);
1327 ok(resume == 0, "Expected the resume handle to be 0\n");
1328 HeapFree(GetProcessHeap(), 0, services);
1329
1330 /* See if things add up */
1331
1332 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1333 * and doesn't count the others as inactive. This means that Vista could
1334 * show a total that is greater than the sum of active and inactive
1335 * drivers.
1336 * The number of active and inactive drivers is greatly influenced by the
1337 * time when tests are run, immediately after boot or later for example.
1338 *
1339 * Both reasons make calculations for drivers not so useful
1340 */
1341
1342 /* Get the number of active win32 services */
1343 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1344 &needed, &returned, NULL);
1345 services = HeapAlloc(GetProcessHeap(), 0, needed);
1346 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1347 needed, &needed, &returned, NULL);
1348 HeapFree(GetProcessHeap(), 0, services);
1349
1350 servicecountactive = returned;
1351
1352 /* Get the number of inactive win32 services */
1353 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1354 &needed, &returned, NULL);
1355 services = HeapAlloc(GetProcessHeap(), 0, needed);
1356 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1357 needed, &needed, &returned, NULL);
1358 HeapFree(GetProcessHeap(), 0, services);
1359
1360 servicecountinactive = returned;
1361
1362 /* Get the number of win32 services */
1363 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1364 &needed, &returned, NULL);
1365 services = HeapAlloc(GetProcessHeap(), 0, needed);
1366 EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1367 needed, &needed, &returned, NULL);
1368 HeapFree(GetProcessHeap(), 0, services);
1369
1370 /* Check if total is the same as active and inactive win32 services */
1371 ok(returned == (servicecountactive + servicecountinactive),
1372 "Something wrong in the calculation\n");
1373
1374 /* Get all drivers and services
1375 *
1376 * Fetch the status of the last call as failing could make the following tests crash
1377 * on Wine (we don't return anything yet).
1378 */
1379 EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1380 NULL, 0, &needed, &returned, NULL);
1381 services = HeapAlloc(GetProcessHeap(), 0, needed);
1382 ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1383 services, needed, &needed, &returned, NULL);
1384
1385 /* Loop through all those returned drivers and services */
1386 for (i = 0; ret && i < returned; i++)
1387 {
1388 SERVICE_STATUS status = services[i].ServiceStatus;
1389
1390 /* lpServiceName and lpDisplayName should always be filled */
1391 ok(lstrlenA(services[i].lpServiceName) > 0, "Expected a service name\n");
1392 ok(lstrlenA(services[i].lpDisplayName) > 0, "Expected a display name\n");
1393
1394 /* Decrement the counters to see if the functions calls return the same
1395 * numbers as the contents of these structures.
1396 */
1397 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1398 {
1399 if (status.dwCurrentState == SERVICE_STOPPED)
1400 servicecountinactive--;
1401 else
1402 servicecountactive--;
1403 }
1404 }
1405 HeapFree(GetProcessHeap(), 0, services);
1406
1407 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1408 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1409
1410 CloseServiceHandle(scm_handle);
1411
1412 /* More or less the same for EnumServicesStatusExA */
1413 if (!pEnumServicesStatusExA)
1414 {
1415 win_skip( "EnumServicesStatusExA not available\n" );
1416 return;
1417 }
1418
1419 /* All NULL or wrong */
1420 SetLastError(0xdeadbeef);
1421 ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1422 ok(!ret, "Expected failure\n");
1423 ok(GetLastError() == ERROR_INVALID_LEVEL,
1424 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1425
1426 /* All NULL or wrong, just the info level is correct */
1427 SetLastError(0xdeadbeef);
1428 ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1429 ok(!ret, "Expected failure\n");
1430 ok(GetLastError() == ERROR_INVALID_HANDLE,
1431 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1432
1433 /* Open the service control manager with not enough rights at first */
1434 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1435
1436 /* Valid handle and info level but rest is still NULL or wrong */
1437 SetLastError(0xdeadbeef);
1438 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1439 ok(!ret, "Expected failure\n");
1440 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1441 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1442 "Unexpected last error %d\n", GetLastError());
1443
1444 /* Don't specify the two required pointers */
1445 needed = 0xdeadbeef;
1446 SetLastError(0xdeadbeef);
1447 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1448 ok(!ret, "Expected failure\n");
1449 ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1450 "Expected no change to the needed buffer variable\n");
1451 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1452 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1453 "Unexpected last error %d\n", GetLastError());
1454
1455 /* Don't specify the two required pointers */
1456 returned = 0xdeadbeef;
1457 SetLastError(0xdeadbeef);
1458 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1459 ok(!ret, "Expected failure\n");
1460 ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1461 ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1462 GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1463 "Unexpected last error %d\n", GetLastError());
1464
1465 /* No valid servicetype and servicestate */
1466 needed = 0xdeadbeef;
1467 returned = 0xdeadbeef;
1468 SetLastError(0xdeadbeef);
1469 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1470 ok(!ret, "Expected failure\n");
1471 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1472 ok(needed == 0 || broken(needed != 0), /* nt4 */
1473 "Expected needed buffer size to be set to 0, got %d\n", needed);
1474 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1475 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1476
1477 /* No valid servicestate */
1478 needed = 0xdeadbeef;
1479 returned = 0xdeadbeef;
1480 SetLastError(0xdeadbeef);
1481 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1482 &needed, &returned, NULL, NULL);
1483 ok(!ret, "Expected failure\n");
1484 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1485 ok(needed == 0 || broken(needed != 0), /* nt4 */
1486 "Expected needed buffer size to be set to 0, got %d\n", needed);
1487 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1488 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1489
1490 /* No valid servicetype */
1491 needed = 0xdeadbeef;
1492 returned = 0xdeadbeef;
1493 SetLastError(0xdeadbeef);
1494 ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1495 &needed, &returned, NULL, NULL);
1496 ok(!ret, "Expected failure\n");
1497 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1498 ok(needed == 0 || broken(needed != 0), /* nt4 */
1499 "Expected needed buffer size to be set to 0, got %d\n", needed);
1500 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1501 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1502
1503 /* No valid servicetype and servicestate and unknown service group */
1504 needed = 0xdeadbeef;
1505 returned = 0xdeadbeef;
1506 SetLastError(0xdeadbeef);
1507 ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1508 &returned, NULL, "deadbeef_group");
1509 ok(!ret, "Expected failure\n");
1510 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1511 ok(needed == 0 || broken(needed != 0), /* nt4 */
1512 "Expected needed buffer size to be set to 0, got %d\n", needed);
1513 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1514 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1515
1516 /* All parameters are correct but our access rights are wrong */
1517 needed = 0xdeadbeef;
1518 returned = 0xdeadbeef;
1519 SetLastError(0xdeadbeef);
1520 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1521 NULL, 0, &needed, &returned, NULL, NULL);
1522 ok(!ret, "Expected failure\n");
1523 ok(needed == 0 || broken(needed != 0), /* nt4 */
1524 "Expected needed buffer size to be set to 0, got %d\n", needed);
1525 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1526 ok(GetLastError() == ERROR_ACCESS_DENIED,
1527 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1528
1529 /* All parameters are correct, access rights are wrong but the
1530 * group name won't be checked yet.
1531 */
1532 needed = 0xdeadbeef;
1533 returned = 0xdeadbeef;
1534 SetLastError(0xdeadbeef);
1535 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1536 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1537 ok(!ret, "Expected failure\n");
1538 ok(needed == 0 || broken(needed != 0), /* nt4 */
1539 "Expected needed buffer size to be set to 0, got %d\n", needed);
1540 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1541 ok(GetLastError() == ERROR_ACCESS_DENIED,
1542 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1543
1544 /* Open the service control manager with the needed rights */
1545 CloseServiceHandle(scm_handle);
1546 scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1547
1548 /* All parameters are correct and the group will be checked */
1549 needed = 0xdeadbeef;
1550 returned = 0xdeadbeef;
1551 SetLastError(0xdeadbeef);
1552 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1553 NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1554 ok(!ret, "Expected failure\n");
1555 ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1556 ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1557 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1558 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1559
1560 /* TODO: Create a test that makes sure we enumerate all services that don't
1561 * belong to a group. (specifying "").
1562 */
1563
1564 /* All parameters are correct. Request the needed buffer size */
1565 needed = 0xdeadbeef;
1566 returned = 0xdeadbeef;
1567 SetLastError(0xdeadbeef);
1568 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1569 NULL, 0, &needed, &returned, NULL, NULL);
1570 ok(!ret, "Expected failure\n");
1571 ok(returned == 0, "Expected no service returned, got %d\n", returned);
1572 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1573 ok(GetLastError() == ERROR_MORE_DATA,
1574 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1575
1576 /* Test to show we get the same needed buffer size for the W-call */
1577 neededW = 0xdeadbeef;
1578 ret = pEnumServicesStatusExW(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1579 NULL, 0, &neededW, &returnedW, NULL, NULL);
1580 ok(!ret, "Expected failure\n");
1581 ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1582
1583 /* Store the needed bytes */
1584 tempneeded = needed;
1585
1586 /* Allocate the correct needed bytes */
1587 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1588 bufsize = needed;
1589 needed = 0xdeadbeef;
1590 returned = 0xdeadbeef;
1591 SetLastError(0xdeadbeef);
1592 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1593 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1594 ok(ret, "Expected success, got error %u\n", GetLastError());
1595 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1596 ok(returned == tempreturned, "Expected the same number of service from this function\n");
1597 HeapFree(GetProcessHeap(), 0, exservices);
1598
1599 /* Store the number of returned services */
1600 tempreturned = returned;
1601
1602 /* Allocate less than the needed bytes and don't specify a resume handle */
1603 exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1604 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1605 needed = 0xdeadbeef;
1606 returned = 0xdeadbeef;
1607 SetLastError(0xdeadbeef);
1608 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1609 (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1610 ok(!ret, "Expected failure\n");
1611 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1612 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1613 ok(GetLastError() == ERROR_MORE_DATA,
1614 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1615
1616 /* Allocate less than the needed bytes, this time with a correct resume handle */
1617 bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1618 needed = 0xdeadbeef;
1619 returned = 0xdeadbeef;
1620 resume = 0;
1621 SetLastError(0xdeadbeef);
1622 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1623 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1624 ok(!ret, "Expected failure\n");
1625 ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1626 ok(returned < tempreturned, "Expected fewer services to be returned\n");
1627 todo_wine ok(resume, "Expected a resume handle\n");
1628 ok(GetLastError() == ERROR_MORE_DATA,
1629 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1630
1631 /* Fetch that last service but pass a bigger buffer size */
1632 missing = tempreturned - returned;
1633 bufsize = tempneeded;
1634 needed = 0xdeadbeef;
1635 returned = 0xdeadbeef;
1636 SetLastError(0xdeadbeef);
1637 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1638 (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1639 ok(ret, "Expected success, got error %u\n", GetLastError());
1640 ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1641 ok(returned == missing, "Expected %u services to be returned\n", missing);
1642 ok(resume == 0, "Expected the resume handle to be 0\n");
1643 HeapFree(GetProcessHeap(), 0, exservices);
1644
1645 /* See if things add up */
1646
1647 /* Get the number of active win32 services */
1648 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1649 NULL, 0, &needed, &returned, NULL, NULL);
1650 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1651 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1652 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1653 HeapFree(GetProcessHeap(), 0, exservices);
1654
1655 servicecountactive = returned;
1656
1657 /* Get the number of inactive win32 services */
1658 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1659 NULL, 0, &needed, &returned, NULL, NULL);
1660 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1661 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1662 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1663 HeapFree(GetProcessHeap(), 0, exservices);
1664
1665 servicecountinactive = returned;
1666
1667 /* Get the number of win32 services */
1668 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1669 NULL, 0, &needed, &returned, NULL, NULL);
1670 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1671 pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1672 (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1673 HeapFree(GetProcessHeap(), 0, exservices);
1674
1675 /* Check if total is the same as active and inactive win32 services */
1676 ok(returned == (servicecountactive + servicecountinactive),
1677 "Something wrong in the calculation\n");
1678
1679 /* Get all drivers and services */
1680 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1681 SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1682 ok(!ret, "Expected failure\n");
1683 exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1684 ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1685 SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1686 ok(ret, "Expected success %u\n", GetLastError());
1687
1688 /* Loop through all those returned drivers and services */
1689 for (i = 0; i < returned; i++)
1690 {
1691 SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1692
1693 /* lpServiceName and lpDisplayName should always be filled */
1694 ok(lstrlenA(exservices[i].lpServiceName) > 0, "Expected a service name\n");
1695 ok(lstrlenA(exservices[i].lpDisplayName) > 0, "Expected a display name\n");
1696
1697 /* Decrement the counters to see if the functions calls return the
1698 * same numbers as the contents of these structures.
1699 * Check some process id specifics.
1700 */
1701 if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1702 {
1703 /* We shouldn't have a process id for drivers */
1704 ok(status.dwProcessId == 0,
1705 "This driver shouldn't have an associated process id\n");
1706 }
1707
1708 if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1709 {
1710 if (status.dwCurrentState != SERVICE_STOPPED)
1711 {
1712 /* We expect a process id for every running service */
1713 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1714 exservices[i].lpServiceName);
1715
1716 servicecountactive--;
1717 }
1718 else
1719 {
1720 /* We shouldn't have a process id for inactive services */
1721 ok(status.dwProcessId == 0, "Service %s state %u shouldn't have an associated process id\n",
1722 exservices[i].lpServiceName, status.dwCurrentState);
1723
1724 servicecountinactive--;
1725 }
1726 }
1727 }
1728 HeapFree(GetProcessHeap(), 0, exservices);
1729
1730 ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1731 ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1732
1733 CloseServiceHandle(scm_handle);
1734 }
1735
1736 static void test_close(void)
1737 {
1738 SC_HANDLE handle;
1739 BOOL ret;
1740
1741 /* NULL handle */
1742 SetLastError(0xdeadbeef);
1743 ret = CloseServiceHandle(NULL);
1744 ok(!ret, "Expected failure\n");
1745 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1746
1747 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1748
1749 /* Proper call */
1750 handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1751 SetLastError(0xdeadbeef);
1752 ret = CloseServiceHandle(handle);
1753 ok(ret, "Expected success got error %u\n", GetLastError());
1754 }
1755
1756 static void test_sequence(void)
1757 {
1758 SC_HANDLE scm_handle, svc_handle;
1759 BOOL ret;
1760 QUERY_SERVICE_CONFIGA *config;
1761 DWORD given, needed;
1762 static const CHAR servicename [] = "Winetest";
1763 static const CHAR displayname [] = "Winetest dummy service";
1764 static const CHAR displayname2[] = "Winetest dummy service (2)";
1765 static const CHAR pathname [] = "we_dont_care.exe";
1766 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1767 static const CHAR password [] = "";
1768 static const CHAR empty [] = "";
1769 static const CHAR localsystem [] = "LocalSystem";
1770
1771 SetLastError(0xdeadbeef);
1772 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1773
1774 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1775 {
1776 skip("Not enough rights to get a handle to the manager\n");
1777 return;
1778 }
1779 else
1780 ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1781
1782 if (!scm_handle) return;
1783
1784 /* Create a dummy service */
1785 SetLastError(0xdeadbeef);
1786 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1787 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1788 pathname, NULL, NULL, dependencies, NULL, password);
1789
1790 if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1791 {
1792 /* We try and open the service and do the rest of the tests. Some could
1793 * fail if the tests were changed between these runs.
1794 */
1795 trace("Deletion probably didn't work last time\n");
1796 SetLastError(0xdeadbeef);
1797 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1798 if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1799 {
1800 skip("Not enough rights to open the service\n");
1801 CloseServiceHandle(scm_handle);
1802 return;
1803 }
1804 ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1805 }
1806 else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1807 {
1808 skip("Not enough rights to create the service\n");
1809 CloseServiceHandle(scm_handle);
1810 return;
1811 }
1812 else
1813 {
1814 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1815 if ((svc_handle != NULL) && (pGetSecurityInfo != NULL))
1816 {
1817 PSID sidOwner, sidGroup;
1818 PACL dacl, sacl;
1819 PSECURITY_DESCRIPTOR pSD;
1820 HRESULT retval = pGetSecurityInfo(svc_handle,SE_SERVICE,DACL_SECURITY_INFORMATION,&sidOwner,&sidGroup,&dacl,&sacl,&pSD);
1821 todo_wine ok(ERROR_SUCCESS == retval, "Expected GetSecurityInfo to succeed: result %d\n",retval);
1822 }
1823 }
1824
1825 if (!svc_handle) return;
1826
1827 /* TODO:
1828 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1829 * that the correct keys are used.
1830 */
1831
1832 /* Request the size for the buffer */
1833 SetLastError(0xdeadbeef);
1834 ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1835 ok(!ret, "Expected failure\n");
1836 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1837
1838 config = HeapAlloc(GetProcessHeap(), 0, needed);
1839 given = needed;
1840 SetLastError(0xdeadbeef);
1841 ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1842 ok(ret, "Expected success, got error %u\n", GetLastError());
1843
1844 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1845 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1846 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1847 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1848 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1849 ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1850 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1851 ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1852 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1853 /* TODO: Show the double 0 terminated string */
1854 todo_wine
1855 {
1856 ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1857 }
1858 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1859 ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1860
1861 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2",
1862 NULL, NULL, NULL, NULL, displayname2);
1863 ok(ret, "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1864
1865 QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1866 config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1867 ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1868 ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1869 config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1870 ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1871 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1872 ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1873 ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1874 ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1875 ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1876 ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1877 ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1878 ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1879
1880 SetLastError(0xdeadbeef);
1881 ret = DeleteService(svc_handle);
1882 ok(ret, "Expected success, got error %u\n", GetLastError());
1883 CloseServiceHandle(svc_handle);
1884
1885 /* Wait a while. The following test does a CreateService again */
1886 Sleep(1000);
1887
1888 CloseServiceHandle(scm_handle);
1889 HeapFree(GetProcessHeap(), 0, config);
1890 }
1891
1892 static void test_queryconfig2(void)
1893 {
1894 SC_HANDLE scm_handle, svc_handle;
1895 BOOL ret;
1896 DWORD expected, needed;
1897 BYTE buffer[MAX_PATH];
1898 LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
1899 SERVICE_PRESHUTDOWN_INFO preshutdown_info;
1900 static const CHAR servicename [] = "Winetest";
1901 static const CHAR displayname [] = "Winetest dummy service";
1902 static const CHAR pathname [] = "we_dont_care.exe";
1903 static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1904 static const CHAR password [] = "";
1905 static const CHAR description [] = "Description";
1906
1907 if(!pQueryServiceConfig2A)
1908 {
1909 win_skip("function QueryServiceConfig2A not present\n");
1910 return;
1911 }
1912
1913 SetLastError(0xdeadbeef);
1914 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1915
1916 if (!scm_handle)
1917 {
1918 if(GetLastError() == ERROR_ACCESS_DENIED)
1919 skip("Not enough rights to get a handle to the manager\n");
1920 else
1921 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
1922 return;
1923 }
1924
1925 /* Create a dummy service */
1926 SetLastError(0xdeadbeef);
1927 svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1928 SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1929 pathname, NULL, NULL, dependencies, NULL, password);
1930
1931 if (!svc_handle)
1932 {
1933 if(GetLastError() == ERROR_SERVICE_EXISTS)
1934 {
1935 /* We try and open the service and do the rest of the tests. Some could
1936 * fail if the tests were changed between these runs.
1937 */
1938 trace("Deletion probably didn't work last time\n");
1939 SetLastError(0xdeadbeef);
1940 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1941 if (!svc_handle)
1942 {
1943 if(GetLastError() == ERROR_ACCESS_DENIED)
1944 skip("Not enough rights to open the service\n");
1945 else
1946 ok(FALSE, "Could not open the service : %d\n", GetLastError());
1947 CloseServiceHandle(scm_handle);
1948 return;
1949 }
1950 }
1951 if (GetLastError() == ERROR_ACCESS_DENIED)
1952 {
1953 skip("Not enough rights to create the service\n");
1954 CloseServiceHandle(scm_handle);
1955 return;
1956 }
1957 ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1958 if (!svc_handle)
1959 {
1960 CloseServiceHandle(scm_handle);
1961 return;
1962 }
1963 }
1964 SetLastError(0xdeadbeef);
1965 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1966 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1967 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1968
1969 SetLastError(0xdeadbeef);
1970 ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1971 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1972 ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1973
1974 SetLastError(0xdeadbeef);
1975 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1976 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1977 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1978
1979 SetLastError(0xdeadbeef);
1980 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
1981 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1982 ok((ERROR_INVALID_ADDRESS == GetLastError()) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()),
1983 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1984
1985 SetLastError(0xdeadbeef);
1986 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),NULL);
1987 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1988 ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1989
1990 needed = 0;
1991 SetLastError(0xdeadbeef);
1992 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA)-1,&needed);
1993 ok(!ret, "expected QueryServiceConfig2A to fail\n");
1994 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1995 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1996
1997 needed = 0;
1998 pConfig->lpDescription = (LPSTR)0xdeadbeef;
1999 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2000 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2001 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
2002 ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
2003
2004 SetLastError(0xdeadbeef);
2005 needed = 0;
2006 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
2007 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2008 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2009 ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
2010
2011 if(!pChangeServiceConfig2A)
2012 {
2013 win_skip("function ChangeServiceConfig2A not present\n");
2014 goto cleanup;
2015 }
2016
2017 pConfig->lpDescription = (LPSTR) description;
2018 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
2019 ok(ret, "ChangeServiceConfig2A failed\n");
2020 if (!ret) {
2021 goto cleanup;
2022 }
2023
2024 SetLastError(0xdeadbeef);
2025 needed = 0;
2026 expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
2027 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2028 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2029 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2030 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2031
2032 SetLastError(0xdeadbeef);
2033 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
2034 ok(!ret, "expected QueryServiceConfig2A to fail\n");
2035 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2036
2037 SetLastError(0xdeadbeef);
2038 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
2039 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2040 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2041 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2042
2043 SetLastError(0xdeadbeef);
2044 ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
2045 ok(ret, "expected QueryServiceConfig2A to succeed\n");
2046 ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2047 "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2048
2049 if(!pQueryServiceConfig2W)
2050 {
2051 win_skip("function QueryServiceConfig2W not present\n");
2052 goto cleanup;
2053 }
2054 SetLastError(0xdeadbeef);
2055 needed = 0;
2056 expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
2057 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
2058 ok(!ret, "expected QueryServiceConfig2W to fail\n");
2059 ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2060 ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2061
2062 SetLastError(0xdeadbeef);
2063 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
2064 ok(ret, "expected QueryServiceConfig2W to succeed\n");
2065
2066 SetLastError(0xdeadbeef);
2067 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_PRESHUTDOWN_INFO,
2068 (LPBYTE)&preshutdown_info, sizeof(preshutdown_info), &needed);
2069 if(!ret && GetLastError()==ERROR_INVALID_LEVEL)
2070 {
2071 /* Win2k3 and older */
2072 win_skip("SERVICE_CONFIG_PRESHUTDOWN_INFO not supported\n");
2073 goto cleanup;
2074 }
2075 ok(ret, "expected QueryServiceConfig2W to succeed (%d)\n", GetLastError());
2076 ok(needed == sizeof(preshutdown_info), "needed = %d\n", needed);
2077 ok(preshutdown_info.dwPreshutdownTimeout == 180000, "Default PreshutdownTimeout = %d\n",
2078 preshutdown_info.dwPreshutdownTimeout);
2079
2080 SetLastError(0xdeadbeef);
2081 preshutdown_info.dwPreshutdownTimeout = -1;
2082 ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_PRESHUTDOWN_INFO,
2083 (LPVOID)&preshutdown_info);
2084 ok(ret, "expected ChangeServiceConfig2A to succeed (%d)\n", GetLastError());
2085
2086 ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_PRESHUTDOWN_INFO,
2087 (LPBYTE)&preshutdown_info, sizeof(preshutdown_info), &needed);
2088 ok(ret, "expected QueryServiceConfig2W to succeed (%d)\n", GetLastError());
2089 ok(needed == sizeof(preshutdown_info), "needed = %d\n", needed);
2090 ok(preshutdown_info.dwPreshutdownTimeout == -1, "New PreshutdownTimeout = %d\n",
2091 preshutdown_info.dwPreshutdownTimeout);
2092
2093 cleanup:
2094 DeleteService(svc_handle);
2095
2096 CloseServiceHandle(svc_handle);
2097
2098 /* Wait a while. The following test does a CreateService again */
2099 Sleep(1000);
2100
2101 CloseServiceHandle(scm_handle);
2102 }
2103
2104 static DWORD try_start_stop(SC_HANDLE svc_handle, const char* name, DWORD is_nt4)
2105 {
2106 BOOL ret;
2107 DWORD le1, le2;
2108 SERVICE_STATUS status;
2109
2110 ret = StartServiceA(svc_handle, 0, NULL);
2111 le1 = GetLastError();
2112 ok(!ret, "%s: StartServiceA() should have failed\n", name);
2113
2114 if (pQueryServiceStatusEx)
2115 {
2116 DWORD needed;
2117 SERVICE_STATUS_PROCESS statusproc;
2118
2119 ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)&statusproc, sizeof(statusproc), &needed);
2120 ok(ret, "%s: QueryServiceStatusEx() failed le=%u\n", name, GetLastError());
2121 ok(statusproc.dwCurrentState == SERVICE_STOPPED, "%s: should be stopped state=%x\n", name, statusproc.dwCurrentState);
2122 ok(statusproc.dwProcessId == 0, "%s: ProcessId should be 0 instead of %x\n", name, statusproc.dwProcessId);
2123 }
2124
2125 ret = StartServiceA(svc_handle, 0, NULL);
2126 le2 = GetLastError();
2127 ok(!ret, "%s: StartServiceA() should have failed\n", name);
2128 ok(le2 == le1, "%s: the second try should yield the same error: %u != %u\n", name, le1, le2);
2129
2130 status.dwCurrentState = 0xdeadbeef;
2131 ret = ControlService(svc_handle, SERVICE_CONTROL_STOP, &status);
2132 le2 = GetLastError();
2133 ok(!ret, "%s: ControlService() should have failed\n", name);
2134 ok(le2 == ERROR_SERVICE_NOT_ACTIVE, "%s: %d != ERROR_SERVICE_NOT_ACTIVE\n", name, le2);
2135 ok(status.dwCurrentState == SERVICE_STOPPED ||
2136 broken(is_nt4), /* NT4 returns a random value */
2137 "%s: should be stopped state=%x\n", name, status.dwCurrentState);
2138
2139 return le1;
2140 }
2141
2142 static void test_start_stop(void)
2143 {
2144 BOOL ret;
2145 SC_HANDLE scm_handle, svc_handle;
2146 DWORD le, is_nt4;
2147 static const char servicename[] = "Winetest";
2148 char cmd[MAX_PATH+20];
2149 const char* displayname;
2150
2151 SetLastError(0xdeadbeef);
2152 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2153 if (!scm_handle)
2154 {
2155 if(GetLastError() == ERROR_ACCESS_DENIED)
2156 skip("Not enough rights to get a handle to the manager\n");
2157 else
2158 ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
2159 return;
2160 }
2161
2162 /* Detect NT4 */
2163 svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
2164 is_nt4=(svc_handle == NULL && GetLastError() == ERROR_INVALID_PARAMETER);
2165
2166 /* Do some cleanup in case a previous run crashed */
2167 svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2168 if (svc_handle)
2169 {
2170 DeleteService(svc_handle);
2171 CloseServiceHandle(svc_handle);
2172 }
2173
2174 /* Create a dummy disabled service */
2175 sprintf(cmd, "\"%s\" service exit", selfname);
2176 displayname = "Winetest Disabled Service";
2177 svc_handle = CreateServiceA(scm_handle, servicename, displayname,
2178 GENERIC_ALL, SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS,
2179 SERVICE_DISABLED, SERVICE_ERROR_IGNORE, cmd, NULL,
2180 NULL, NULL, NULL, NULL);
2181 if (!svc_handle)
2182 {
2183 if(GetLastError() == ERROR_ACCESS_DENIED)
2184 skip("Not enough rights to create the service\n");
2185 else
2186 ok(FALSE, "Could not create the service: %d\n", GetLastError());
2187 goto cleanup;
2188 }
2189 le = try_start_stop(svc_handle, displayname, is_nt4);
2190 ok(le == ERROR_SERVICE_DISABLED, "%d != ERROR_SERVICE_DISABLED\n", le);
2191
2192 /* Then one with a bad path */
2193 displayname = "Winetest Bad Path";
2194 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, "c:\\no_such_file.exe", NULL, NULL, NULL, NULL, NULL, displayname);
2195 ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2196 try_start_stop(svc_handle, displayname, is_nt4);
2197
2198 if (is_nt4)
2199 {
2200 /* NT4 does not detect when a service fails to start and uses an
2201 * insanely long timeout: 120s. So skip the rest of the tests.
2202 */
2203 win_skip("Skip some service start/stop tests on NT4\n");
2204 goto cleanup;
2205 }
2206
2207 if (!winetest_interactive)
2208 {
2209 skip("ROSTESTS-56: Skipping service start timeout tests!\n");
2210 goto cleanup;
2211 }
2212
2213 /* Again with a process that exits right away */
2214 displayname = "Winetest Exit Service";
2215 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, cmd, NULL, NULL, NULL, NULL, NULL, displayname);
2216 ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2217 le = try_start_stop(svc_handle, displayname, is_nt4);
2218 ok(le == ERROR_SERVICE_REQUEST_TIMEOUT, "%d != ERROR_SERVICE_REQUEST_TIMEOUT\n", le);
2219
2220 /* And finally with a service that plays dead, forcing a timeout.
2221 * This time we will put no quotes. That should work too, even if there are
2222 * spaces in the path.
2223 */
2224 sprintf(cmd, "%s service sleep", selfname);
2225 displayname = "Winetest Sleep Service";
2226 ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, cmd, NULL, NULL, NULL, NULL, NULL, displayname);
2227 ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2228
2229 le = try_start_stop(svc_handle, displayname, is_nt4);
2230 ok(le == ERROR_SERVICE_REQUEST_TIMEOUT, "%d != ERROR_SERVICE_REQUEST_TIMEOUT\n", le);
2231
2232 cleanup:
2233 if (svc_handle)
2234 {
2235 DeleteService(svc_handle);
2236 CloseServiceHandle(svc_handle);
2237 }
2238
2239 /* Wait a while. The following test does a CreateService again */
2240 Sleep(1000);
2241
2242 CloseServiceHandle(scm_handle);
2243 }
2244
2245 static void test_refcount(void)
2246 {
2247 SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
2248 static const CHAR servicename [] = "Winetest";
2249 static const CHAR pathname [] = "we_dont_care.exe";
2250 BOOL ret;
2251
2252 /* Get a handle to the Service Control Manager */
2253 SetLastError(0xdeadbeef);
2254 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2255 if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
2256 {
2257 skip("Not enough rights to get a handle to the manager\n");
2258 return;
2259 }
2260
2261 /* Create a service */
2262 svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2263 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2264 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2265 ok(svc_handle1 != NULL, "Expected success, got error %u\n", GetLastError());
2266
2267 /* Get a handle to this new service */
2268 svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2269 ok(svc_handle2 != NULL, "Expected success, got error %u\n", GetLastError());
2270
2271 /* Get another handle to this new service */
2272 svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2273 ok(svc_handle3 != NULL, "Expected success, got error %u\n", GetLastError());
2274
2275 /* Check if we can close the handle to the Service Control Manager */
2276 ret = CloseServiceHandle(scm_handle);
2277 ok(ret, "Expected success (err=%d)\n", GetLastError());
2278
2279 /* Get a new handle to the Service Control Manager */
2280 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2281 ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
2282
2283 /* Get a handle to this new service */
2284 svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2285 ok(svc_handle4 != NULL, "Expected success, got error %u\n", GetLastError());
2286
2287 /* Delete the service */
2288 ret = DeleteService(svc_handle4);
2289 ok(ret, "Expected success (err=%d)\n", GetLastError());
2290
2291 /* We cannot create the same service again as it's still marked as 'being deleted'.
2292 * The reason is that we still have 4 open handles to this service even though we
2293 * closed the handle to the Service Control Manager in between.
2294 */
2295 SetLastError(0xdeadbeef);
2296 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2297 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2298 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2299 todo_wine
2300 {
2301 ok(!svc_handle5, "Expected failure\n");
2302 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
2303 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2304 }
2305
2306 /* FIXME: Remove this when Wine is fixed */
2307 if (svc_handle5)
2308 {
2309 DeleteService(svc_handle5);
2310 CloseServiceHandle(svc_handle5);
2311 }
2312
2313 /* Close all the handles to the service and try again */
2314 ret = CloseServiceHandle(svc_handle4);
2315 ok(ret, "Expected success (err=%d)\n", GetLastError());
2316 ret = CloseServiceHandle(svc_handle3);
2317 ok(ret, "Expected success (err=%d)\n", GetLastError());
2318 ret = CloseServiceHandle(svc_handle2);
2319 ok(ret, "Expected success (err=%d)\n", GetLastError());
2320 ret = CloseServiceHandle(svc_handle1);
2321 ok(ret, "Expected success (err=%d)\n", GetLastError());
2322
2323 /* Wait a while. Doing a CreateService too soon will result again
2324 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2325 */
2326 Sleep(1000);
2327
2328 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2329 svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2330 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2331 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2332 ok(svc_handle5 != NULL, "Expected success, got error %u\n", GetLastError());
2333
2334 /* Delete the service */
2335 ret = DeleteService(svc_handle5);
2336 ok(ret, "Expected success (err=%d)\n", GetLastError());
2337
2338 /* Wait a while. Just in case one of the following tests does a CreateService again */
2339 Sleep(1000);
2340
2341 CloseServiceHandle(svc_handle5);
2342 CloseServiceHandle(scm_handle);
2343 }
2344
2345 START_TEST(service)
2346 {
2347 SC_HANDLE scm_handle;
2348 int myARGC;
2349 char** myARGV;
2350
2351 myARGC = winetest_get_mainargs(&myARGV);
2352 GetFullPathNameA(myARGV[0], sizeof(selfname), selfname, NULL);
2353 if (myARGC >= 3)
2354 {
2355 if (strcmp(myARGV[2], "sleep") == 0)
2356 /* Cause a service startup timeout */
2357 Sleep(90000);
2358 /* then, or if myARGV[2] == "exit", just exit */
2359 return;
2360 }
2361
2362 /* Bail out if we are on win98 */
2363 SetLastError(0xdeadbeef);
2364 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2365
2366 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2367 {
2368 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2369 return;
2370 }
2371 CloseServiceHandle(scm_handle);
2372
2373 init_function_pointers();
2374
2375 /* First some parameter checking */
2376 test_open_scm();
2377 test_open_svc();
2378 test_create_delete_svc();
2379 test_get_displayname();
2380 test_get_servicekeyname();
2381 test_query_svc();
2382 test_enum_svc();
2383 test_close();
2384 /* Test the creation, querying and deletion of a service */
2385 test_sequence();
2386 test_queryconfig2();
2387 test_start_stop();
2388 /* The main reason for this test is to check if any refcounting is used
2389 * and what the rules are
2390 */
2391 test_refcount();
2392 }