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