2 * Unit tests for service functions
4 * Copyright (c) 2007 Paul Vriens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/test.h"
35 static const CHAR spooler
[] = "Spooler"; /* Should be available on all platforms */
37 static BOOL (WINAPI
*pChangeServiceConfig2A
)(SC_HANDLE
,DWORD
,LPVOID
);
38 static BOOL (WINAPI
*pEnumServicesStatusExA
)(SC_HANDLE
, SC_ENUM_TYPE
, DWORD
,
39 DWORD
, LPBYTE
, DWORD
, LPDWORD
,
40 LPDWORD
, LPDWORD
, LPCSTR
);
41 static BOOL (WINAPI
*pEnumServicesStatusExW
)(SC_HANDLE
, SC_ENUM_TYPE
, DWORD
,
42 DWORD
, LPBYTE
, DWORD
, LPDWORD
,
43 LPDWORD
, LPDWORD
, LPCWSTR
);
44 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
45 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
46 static BOOL (WINAPI
*pQueryServiceConfig2A
)(SC_HANDLE
,DWORD
,LPBYTE
,DWORD
,LPDWORD
);
47 static BOOL (WINAPI
*pQueryServiceConfig2W
)(SC_HANDLE
,DWORD
,LPBYTE
,DWORD
,LPDWORD
);
48 static BOOL (WINAPI
*pQueryServiceStatusEx
)(SC_HANDLE
, SC_STATUS_TYPE
, LPBYTE
,
51 static void init_function_pointers(void)
53 HMODULE hadvapi32
= GetModuleHandleA("advapi32.dll");
55 pChangeServiceConfig2A
= (void*)GetProcAddress(hadvapi32
, "ChangeServiceConfig2A");
56 pEnumServicesStatusExA
= (void*)GetProcAddress(hadvapi32
, "EnumServicesStatusExA");
57 pEnumServicesStatusExW
= (void*)GetProcAddress(hadvapi32
, "EnumServicesStatusExW");
58 pGetSecurityInfo
= (void *)GetProcAddress(hadvapi32
, "GetSecurityInfo");
59 pQueryServiceConfig2A
= (void*)GetProcAddress(hadvapi32
, "QueryServiceConfig2A");
60 pQueryServiceConfig2W
= (void*)GetProcAddress(hadvapi32
, "QueryServiceConfig2W");
61 pQueryServiceStatusEx
= (void*)GetProcAddress(hadvapi32
, "QueryServiceStatusEx");
64 static void test_open_scm(void)
68 /* No access rights */
69 SetLastError(0xdeadbeef);
70 scm_handle
= OpenSCManagerA(NULL
, NULL
, 0);
71 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
72 CloseServiceHandle(scm_handle
);
74 /* Unknown database name */
75 SetLastError(0xdeadbeef);
76 scm_handle
= OpenSCManagerA(NULL
, "DoesNotExist", SC_MANAGER_CONNECT
);
77 ok(!scm_handle
, "Expected failure\n");
78 ok(GetLastError() == ERROR_INVALID_NAME
, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
79 CloseServiceHandle(scm_handle
); /* Just in case */
81 /* MSDN says only ServiceActive is allowed, or NULL */
82 SetLastError(0xdeadbeef);
83 scm_handle
= OpenSCManagerA(NULL
, SERVICES_FAILED_DATABASEA
, SC_MANAGER_CONNECT
);
84 ok(!scm_handle
, "Expected failure\n");
85 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST
, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
86 CloseServiceHandle(scm_handle
); /* Just in case */
88 /* Remote unknown host */
89 SetLastError(0xdeadbeef);
90 scm_handle
= OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA
, SC_MANAGER_CONNECT
);
93 ok(!scm_handle
, "Expected failure\n");
94 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* w2k8 */,
95 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
97 CloseServiceHandle(scm_handle
); /* Just in case */
99 /* Proper call with an empty hostname */
100 SetLastError(0xdeadbeef);
101 scm_handle
= OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA
, SC_MANAGER_CONNECT
);
102 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
103 CloseServiceHandle(scm_handle
);
105 /* Again a correct one */
106 SetLastError(0xdeadbeef);
107 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
108 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
109 CloseServiceHandle(scm_handle
);
112 static void test_open_svc(void)
114 SC_HANDLE scm_handle
, svc_handle
;
115 CHAR displayname
[4096];
118 /* All NULL (invalid access rights) */
119 SetLastError(0xdeadbeef);
120 svc_handle
= OpenServiceA(NULL
, NULL
, 0);
121 ok(!svc_handle
, "Expected failure\n");
122 ok(GetLastError() == ERROR_INVALID_HANDLE
, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
124 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
127 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
128 SetLastError(0xdeadbeef);
129 svc_handle
= OpenServiceA(scm_handle
, NULL
, GENERIC_READ
);
130 ok(!svc_handle
, "Expected failure\n");
131 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
132 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
133 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
135 /* Nonexistent service */
136 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
137 SetLastError(0xdeadbeef);
138 svc_handle
= OpenServiceA(scm_handle
, "deadbeef", GENERIC_READ
);
139 ok(!svc_handle
, "Expected failure\n");
140 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
141 CloseServiceHandle(scm_handle
);
143 /* Proper SCM handle but different access rights */
144 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
145 SetLastError(0xdeadbeef);
146 svc_handle
= OpenServiceA(scm_handle
, "Spooler", GENERIC_WRITE
);
147 if (!svc_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
148 skip("Not enough rights to get a handle to the service\n");
151 ok(svc_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
152 CloseServiceHandle(svc_handle
);
155 /* Test to show we can't open a service with the displayname */
157 /* Retrieve the needed size for the buffer */
159 GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
160 /* Get the displayname */
161 GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
162 /* Try to open the service with this displayname, unless the displayname equals
163 * the servicename as that would defeat the purpose of this test.
165 if (!lstrcmpi(spooler
, displayname
))
167 skip("displayname equals servicename\n");
168 CloseServiceHandle(scm_handle
);
172 SetLastError(0xdeadbeef);
173 svc_handle
= OpenServiceA(scm_handle
, displayname
, GENERIC_READ
);
174 ok(!svc_handle
, "Expected failure\n");
175 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
177 CloseServiceHandle(svc_handle
);
179 CloseServiceHandle(scm_handle
);
182 static void test_create_delete_svc(void)
184 SC_HANDLE scm_handle
, svc_handle1
;
185 CHAR username
[UNLEN
+ 1], domain
[MAX_PATH
];
186 DWORD user_size
= UNLEN
+ 1;
187 CHAR account
[UNLEN
+ 3];
188 static const CHAR servicename
[] = "Winetest";
189 static const CHAR pathname
[] = "we_dont_care.exe";
190 static const CHAR empty
[] = "";
191 static const CHAR password
[] = "secret";
192 BOOL spooler_exists
= FALSE
;
195 DWORD display_size
= sizeof(display
);
197 /* Get the username and turn it into an account to be used in some tests */
198 GetUserNameA(username
, &user_size
);
199 /* Get the domainname to cater for that situation */
200 if (GetEnvironmentVariableA("USERDOMAIN", domain
, MAX_PATH
))
201 sprintf(account
, "%s\\%s", domain
, username
);
203 sprintf(account
, ".\\%s", username
);
206 SetLastError(0xdeadbeef);
207 svc_handle1
= CreateServiceA(NULL
, NULL
, NULL
, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
208 ok(!svc_handle1
, "Expected failure\n");
209 ok(GetLastError() == ERROR_INVALID_HANDLE
, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
211 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
213 /* Only a valid handle to the Service Control Manager */
214 SetLastError(0xdeadbeef);
215 svc_handle1
= CreateServiceA(scm_handle
, NULL
, NULL
, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
216 ok(!svc_handle1
, "Expected failure\n");
217 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, W2K3, XP, Vista */ ||
218 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
219 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
221 /* Now with a servicename */
222 SetLastError(0xdeadbeef);
223 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
224 ok(!svc_handle1
, "Expected failure\n");
225 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, W2K3, XP, Vista */ ||
226 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
227 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
229 /* Or just a binary name */
230 SetLastError(0xdeadbeef);
231 svc_handle1
= CreateServiceA(scm_handle
, NULL
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
232 ok(!svc_handle1
, "Expected failure\n");
233 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, W2K3, XP, Vista */ ||
234 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
235 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
237 /* Both servicename and binary name (We only have connect rights) */
238 SetLastError(0xdeadbeef);
239 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
240 ok(!svc_handle1
, "Expected failure\n");
241 ok(GetLastError() == ERROR_ACCESS_DENIED
, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
243 /* They can even be empty at this stage of parameter checking */
244 SetLastError(0xdeadbeef);
245 svc_handle1
= CreateServiceA(scm_handle
, empty
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
246 ok(!svc_handle1
, "Expected failure\n");
247 ok(GetLastError() == ERROR_ACCESS_DENIED
, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
249 SetLastError(0xdeadbeef);
250 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
251 ok(!svc_handle1
, "Expected failure\n");
252 ok(GetLastError() == ERROR_ACCESS_DENIED
, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
254 /* Open the Service Control Manager with minimal rights for creation
255 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
257 CloseServiceHandle(scm_handle
);
258 SetLastError(0xdeadbeef);
259 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
);
260 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
262 skip("Not enough rights to get a handle to the manager\n");
266 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
268 /* Empty strings for servicename and binary name are checked */
269 SetLastError(0xdeadbeef);
270 svc_handle1
= CreateServiceA(scm_handle
, empty
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
271 ok(!svc_handle1
, "Expected failure\n");
272 ok(GetLastError() == ERROR_INVALID_NAME
, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
274 SetLastError(0xdeadbeef);
275 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
276 ok(!svc_handle1
, "Expected failure\n");
277 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
279 SetLastError(0xdeadbeef);
280 svc_handle1
= CreateServiceA(scm_handle
, empty
, NULL
, 0, 0, 0, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
281 ok(!svc_handle1
, "Expected failure\n");
282 ok(GetLastError() == ERROR_INVALID_NAME
, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
284 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
285 * an ERROR_INVALID_PARAMETER)
287 SetLastError(0xdeadbeef);
288 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
,
289 SERVICE_DISABLED
, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
290 ok(!svc_handle1
, "Expected failure\n");
291 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
293 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
295 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
296 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
298 SetLastError(0xdeadbeef);
299 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_WIN32_OWN_PROCESS
| SERVICE_WIN32_SHARE_PROCESS
,
300 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
301 ok(!svc_handle1
, "Expected failure\n");
302 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
304 /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
305 SetLastError(0xdeadbeef);
306 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_FILE_SYSTEM_DRIVER
| SERVICE_INTERACTIVE_PROCESS
,
307 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
308 ok(!svc_handle1
, "Expected failure\n");
309 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
311 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
312 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
314 SetLastError(0xdeadbeef);
315 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
316 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, account
, password
);
317 ok(!svc_handle1
, "Expected failure\n");
318 ok(GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_INVALID_SERVICE_ACCOUNT
,
319 "Expected ERROR_INVALID_PARAMETER or ERROR_INVALID_SERVICE_ACCOUNT, got %d\n", GetLastError());
321 /* Illegal (start-type is not a mask and should only be one of the possibilities)
322 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
323 * it's most likely not the wanted start-type)
325 SetLastError(0xdeadbeef);
326 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_WIN32_OWN_PROCESS
,
327 SERVICE_AUTO_START
| SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
328 ok(!svc_handle1
, "Expected failure\n");
329 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
331 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
332 SetLastError(0xdeadbeef);
333 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
,
334 SERVICE_BOOT_START
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
335 ok(!svc_handle1
, "Expected failure\n");
336 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
338 /* The service already exists (check first, just in case) */
339 svc_handle1
= OpenServiceA(scm_handle
, spooler
, GENERIC_READ
);
342 spooler_exists
= TRUE
;
343 CloseServiceHandle(svc_handle1
);
344 SetLastError(0xdeadbeef);
345 svc_handle1
= CreateServiceA(scm_handle
, spooler
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
,
346 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
347 ok(!svc_handle1
, "Expected failure\n");
348 ok(GetLastError() == ERROR_SERVICE_EXISTS
, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
351 skip("Spooler service doesn't exist\n");
353 /* To find an existing displayname we check the 'Spooler' service. Although the registry
354 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
355 * to the servicename and can't be used as well for a new displayname.
359 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, display
, &display_size
);
362 skip("Could not retrieve a displayname for the Spooler service\n");
365 svc_handle1
= CreateServiceA(scm_handle
, servicename
, display
, 0, SERVICE_WIN32_OWN_PROCESS
,
366 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
367 ok(!svc_handle1
, "Expected failure\n");
368 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME
,
369 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
373 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
375 /* Windows doesn't care about the access rights for creation (which makes
376 * sense as there is no service yet) as long as there are sufficient
377 * rights to the manager.
379 SetLastError(0xdeadbeef);
380 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
381 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
382 ok(svc_handle1
!= NULL
, "Could not create the service : %d\n", GetLastError());
384 /* DeleteService however must have proper rights */
385 SetLastError(0xdeadbeef);
386 ret
= DeleteService(svc_handle1
);
387 ok(!ret
, "Expected failure\n");
388 ok(GetLastError() == ERROR_ACCESS_DENIED
,
389 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
391 /* Open the service with minimal rights for deletion.
392 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
394 CloseServiceHandle(svc_handle1
);
395 svc_handle1
= OpenServiceA(scm_handle
, servicename
, DELETE
);
397 /* Now that we have the proper rights, we should be able to delete */
398 SetLastError(0xdeadbeef);
399 ret
= DeleteService(svc_handle1
);
400 ok(ret
, "Expected success, got error %u\n", GetLastError());
402 CloseServiceHandle(svc_handle1
);
403 CloseServiceHandle(scm_handle
);
405 /* Wait a while. One of the following tests also does a CreateService for the
406 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
407 * error if we do this to quick. Vista seems more picky then the others.
411 /* And a final NULL check */
412 SetLastError(0xdeadbeef);
413 ret
= DeleteService(NULL
);
414 ok(!ret
, "Expected failure\n");
415 ok(GetLastError() == ERROR_INVALID_HANDLE
,
416 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
419 static void test_get_displayname(void)
421 SC_HANDLE scm_handle
, svc_handle
;
423 CHAR displayname
[4096];
424 WCHAR displaynameW
[2048];
425 DWORD displaysize
, tempsize
, tempsizeW
;
426 static const CHAR deadbeef
[] = "Deadbeef";
427 static const WCHAR spoolerW
[] = {'S','p','o','o','l','e','r',0};
428 static const WCHAR deadbeefW
[] = {'D','e','a','d','b','e','e','f',0};
429 static const WCHAR abcW
[] = {'A','B','C',0};
430 static const CHAR servicename
[] = "Winetest";
431 static const CHAR pathname
[] = "we_dont_care.exe";
433 /* Having NULL for the size of the buffer will crash on W2K3 */
435 SetLastError(0xdeadbeef);
436 ret
= GetServiceDisplayNameA(NULL
, NULL
, NULL
, &displaysize
);
437 ok(!ret
, "Expected failure\n");
438 ok(GetLastError() == ERROR_INVALID_HANDLE
,
439 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
441 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
443 SetLastError(0xdeadbeef);
444 ret
= GetServiceDisplayNameA(scm_handle
, NULL
, NULL
, &displaysize
);
445 ok(!ret
, "Expected failure\n");
446 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
447 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
448 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
450 SetLastError(0xdeadbeef);
451 displaysize
= sizeof(displayname
);
452 ret
= GetServiceDisplayNameA(scm_handle
, NULL
, displayname
, &displaysize
);
453 ok(!ret
, "Expected failure\n");
454 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
455 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
456 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
458 /* Test for nonexistent service */
459 SetLastError(0xdeadbeef);
461 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, NULL
, &displaysize
);
462 ok(!ret
, "Expected failure\n");
463 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
464 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
466 SetLastError(0xdeadbeef);
467 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, NULL
, &displaysize
);
468 ok(!ret
, "Expected failure\n");
469 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
470 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
471 todo_wine
ok(displaysize
== 1, "Service size expected 1, got %d\n", displaysize
);
474 strcpy(displayname
, "ABC");
475 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
476 ok(!ret
, "Expected failure\n");
477 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
478 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
479 todo_wine
ok(displaysize
== 15, "Service size expected 15, got %d\n", displaysize
);
480 ok(displayname
[0] == 0, "Service name not empty\n");
483 lstrcpyW( displaynameW
, abcW
);
484 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
485 ok(!ret
, "Expected failure\n");
486 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
487 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
488 ok(displaysize
== 15, "Service size expected 15, got %d\n", displaysize
);
489 ok(displaynameW
[0] == 0, "Service name not empty\n");
492 strcpy(displayname
, "ABC");
493 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
494 ok(!ret
, "Expected failure\n");
495 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
496 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
497 todo_wine
ok(displaysize
== 1, "Service size expected 1, got %d\n", displaysize
);
498 ok(displayname
[0] == 'A', "Service name changed\n");
501 lstrcpyW( displaynameW
, abcW
);
502 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
503 ok(!ret
, "Expected failure\n");
504 ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
505 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
506 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
507 ok(displaynameW
[0] == 'A', "Service name changed\n");
510 strcpy(displayname
, "ABC");
511 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
512 ok(!ret
, "Expected failure\n");
513 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
514 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
515 todo_wine
ok(displaysize
== 1, "Service size expected 1, got %d\n", displaysize
);
516 ok(displayname
[0] == 0, "Service name not empty\n");
519 lstrcpyW( displaynameW
, abcW
);
520 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
521 ok(!ret
, "Expected failure\n");
522 ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
523 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
524 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
525 ok(displaynameW
[0] == 'A', "Service name changed\n");
528 strcpy(displayname
, "ABC");
529 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
530 ok(!ret
, "Expected failure\n");
531 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
532 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
533 todo_wine
ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
534 ok(displayname
[0] == 0, "Service name not empty\n");
537 lstrcpyW( displaynameW
, abcW
);
538 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
539 ok(!ret
, "Expected failure\n");
540 ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
541 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
542 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
543 ok(displaynameW
[0] == 0, "Service name not empty\n");
545 /* Check if 'Spooler' exists */
546 svc_handle
= OpenServiceA(scm_handle
, spooler
, GENERIC_READ
);
549 skip("Spooler service doesn't exist\n");
550 CloseServiceHandle(scm_handle
);
553 CloseServiceHandle(svc_handle
);
555 /* Retrieve the needed size for the buffer */
556 SetLastError(0xdeadbeef);
558 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
559 ok(!ret
, "Expected failure\n");
560 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
561 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
562 tempsize
= displaysize
;
565 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
566 ok(!ret
, "Expected failure\n");
567 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
568 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
569 ok(displaysize
== tempsize
, "Buffer size mismatch (%d vs %d)\n", tempsize
, displaysize
);
571 /* Buffer is too small */
572 SetLastError(0xdeadbeef);
573 displaysize
= (tempsize
/ 2);
574 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
575 ok(!ret
, "Expected failure\n");
576 ok(displaysize
== tempsize
, "Expected the needed buffersize\n");
577 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
578 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
580 /* First try with a buffer that should be big enough to hold
581 * the ANSI string (and terminating character). This succeeds on Windows
582 * although when asked (see above 2 tests) it will return twice the needed size.
584 SetLastError(0xdeadbeef);
585 displaysize
= (tempsize
/ 2) + 1;
586 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
587 ok(ret
, "Expected success, got error %u\n", GetLastError());
588 ok(displaysize
== ((tempsize
/ 2) + 1), "Expected no change for the needed buffer size\n");
590 /* Now with the original returned size */
591 SetLastError(0xdeadbeef);
592 displaysize
= tempsize
;
593 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
594 ok(ret
, "Expected success, got error %u\n", GetLastError());
595 ok(displaysize
== tempsize
, "Expected no change for the needed buffer size\n");
597 /* And with a bigger than needed buffer */
598 SetLastError(0xdeadbeef);
599 displaysize
= tempsize
* 2;
600 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
601 ok(ret
, "Expected success, got error %u\n", GetLastError());
602 /* Test that shows that if the buffersize is enough, it's not changed */
603 ok(displaysize
== tempsize
* 2, "Expected no change for the needed buffer size\n");
604 ok(lstrlen(displayname
) == tempsize
/2,
605 "Expected the buffer to be twice the length of the string\n") ;
607 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
608 SetLastError(0xdeadbeef);
610 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, NULL
, &displaysize
);
611 ok(!ret
, "Expected failure\n");
612 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
613 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
615 /* Buffer is too small */
616 SetLastError(0xdeadbeef);
617 tempsizeW
= displaysize
;
618 displaysize
= tempsizeW
/ 2;
619 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, displaynameW
, &displaysize
);
620 ok(!ret
, "Expected failure\n");
621 ok(displaysize
== tempsizeW
, "Expected the needed buffersize\n");
622 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
623 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
625 /* Now with the original returned size */
626 SetLastError(0xdeadbeef);
627 displaysize
= tempsizeW
;
628 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, displaynameW
, &displaysize
);
629 ok(!ret
, "Expected failure\n");
630 ok(displaysize
== tempsizeW
, "Expected the needed buffersize\n");
631 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
632 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
634 /* And with a bigger than needed buffer */
635 SetLastError(0xdeadbeef);
636 displaysize
= tempsizeW
+ 1; /* This caters for the null terminating character */
637 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, displaynameW
, &displaysize
);
638 ok(ret
, "Expected success, got error %u\n", GetLastError());
639 ok(displaysize
== tempsizeW
, "Expected the needed buffersize\n");
640 ok(lstrlenW(displaynameW
) == displaysize
,
641 "Expected the buffer to be the length of the string\n") ;
642 ok(tempsize
/ 2 == tempsizeW
,
643 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
645 CloseServiceHandle(scm_handle
);
647 /* Test for a service without a displayname (which is valid). This should return
648 * the servicename itself.
650 SetLastError(0xdeadbeef);
651 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
);
652 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
654 skip("Not enough rights to get a handle to the manager\n");
658 SetLastError(0xdeadbeef);
659 svc_handle
= CreateServiceA(scm_handle
, servicename
, NULL
, DELETE
,
660 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
661 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
662 ok(svc_handle
!= NULL
, "Could not create the service : %d\n", GetLastError());
665 CloseServiceHandle(scm_handle
);
669 /* Retrieve the needed size for the buffer */
670 SetLastError(0xdeadbeef);
672 ret
= GetServiceDisplayNameA(scm_handle
, servicename
, NULL
, &displaysize
);
673 ok(!ret
, "Expected failure\n");
674 ok(displaysize
== lstrlen(servicename
) * 2,
675 "Expected the displaysize to be twice the size of the servicename\n");
676 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
677 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
679 /* Buffer is too small */
680 SetLastError(0xdeadbeef);
681 tempsize
= displaysize
;
682 displaysize
= (tempsize
/ 2);
683 ret
= GetServiceDisplayNameA(scm_handle
, servicename
, displayname
, &displaysize
);
684 ok(!ret
, "Expected failure\n");
685 ok(displaysize
== tempsize
, "Expected the needed buffersize\n");
686 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
687 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
689 /* Get the displayname */
690 SetLastError(0xdeadbeef);
691 ret
= GetServiceDisplayNameA(scm_handle
, servicename
, displayname
, &displaysize
);
692 ok(ret
, "Expected success, got error %u\n", GetLastError());
693 ok(!lstrcmpi(displayname
, servicename
),
694 "Expected displayname to be %s, got %s\n", servicename
, displayname
);
696 /* Delete the service */
697 ret
= DeleteService(svc_handle
);
698 ok(ret
, "Expected success (err=%d)\n", GetLastError());
700 CloseServiceHandle(svc_handle
);
701 CloseServiceHandle(scm_handle
);
703 /* Wait a while. Just in case one of the following tests does a CreateService again */
707 static void test_get_servicekeyname(void)
709 SC_HANDLE scm_handle
, svc_handle
;
710 CHAR servicename
[4096];
711 CHAR displayname
[4096];
712 WCHAR servicenameW
[4096];
713 WCHAR displaynameW
[4096];
714 DWORD servicesize
, displaysize
, tempsize
;
716 static const CHAR deadbeef
[] = "Deadbeef";
717 static const WCHAR deadbeefW
[] = {'D','e','a','d','b','e','e','f',0};
718 static const WCHAR abcW
[] = {'A','B','C',0};
720 /* Having NULL for the size of the buffer will crash on W2K3 */
722 SetLastError(0xdeadbeef);
723 ret
= GetServiceKeyNameA(NULL
, NULL
, NULL
, &servicesize
);
724 ok(!ret
, "Expected failure\n");
725 ok(GetLastError() == ERROR_INVALID_HANDLE
,
726 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
728 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
731 SetLastError(0xdeadbeef);
732 ret
= GetServiceKeyNameA(scm_handle
, NULL
, NULL
, &servicesize
);
733 ok(!ret
, "Expected failure\n");
734 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
735 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
736 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
737 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
739 /* Valid handle and buffer but no displayname */
741 SetLastError(0xdeadbeef);
742 ret
= GetServiceKeyNameA(scm_handle
, NULL
, servicename
, &servicesize
);
743 ok(!ret
, "Expected failure\n");
744 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
745 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
746 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
747 todo_wine
ok(servicesize
== 200, "Service size expected 1, got %d\n", servicesize
);
749 /* Test for nonexistent displayname */
750 SetLastError(0xdeadbeef);
751 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, NULL
, &servicesize
);
752 ok(!ret
, "Expected failure\n");
753 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
754 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
755 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
758 strcpy(servicename
, "ABC");
759 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
760 ok(!ret
, "Expected failure\n");
761 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
762 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
763 todo_wine
ok(servicesize
== 15, "Service size expected 15, got %d\n", servicesize
);
764 ok(servicename
[0] == 0, "Service name not empty\n");
767 lstrcpyW( servicenameW
, abcW
);
768 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
769 ok(!ret
, "Expected failure\n");
770 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
771 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
772 ok(servicesize
== 15, "Service size expected 15, got %d\n", servicesize
);
773 ok(servicenameW
[0] == 0, "Service name not empty\n");
776 strcpy(servicename
, "ABC");
777 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
778 ok(!ret
, "Expected failure\n");
779 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
780 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
781 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
782 ok(servicename
[0] == 'A', "Service name changed\n");
785 lstrcpyW( servicenameW
, abcW
);
786 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
787 ok(!ret
, "Expected failure\n");
788 ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
789 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
790 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
791 ok(servicenameW
[0] == 'A', "Service name changed\n");
794 strcpy(servicename
, "ABC");
795 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
796 ok(!ret
, "Expected failure\n");
797 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
798 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
799 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
800 ok(servicename
[0] == 0, "Service name not empty\n");
803 lstrcpyW( servicenameW
, abcW
);
804 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
805 ok(!ret
, "Expected failure\n");
806 ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
807 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
808 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
809 ok(servicenameW
[0] == 'A', "Service name changed\n");
812 strcpy(servicename
, "ABC");
813 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
814 ok(!ret
, "Expected failure\n");
815 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
816 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
817 todo_wine
ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
818 ok(servicename
[0] == 0, "Service name not empty\n");
821 lstrcpyW( servicenameW
, abcW
);
822 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
823 ok(!ret
, "Expected failure\n");
824 ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
825 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
826 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
827 ok(servicenameW
[0] == 0, "Service name not empty\n");
829 /* Check if 'Spooler' exists */
830 svc_handle
= OpenServiceA(scm_handle
, spooler
, GENERIC_READ
);
833 skip("Spooler service doesn't exist\n");
834 CloseServiceHandle(scm_handle
);
837 CloseServiceHandle(svc_handle
);
839 /* Get the displayname for the 'Spooler' service */
840 GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
841 GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
843 /* Retrieve the needed size for the buffer */
844 SetLastError(0xdeadbeef);
846 ret
= GetServiceKeyNameA(scm_handle
, displayname
, NULL
, &servicesize
);
847 ok(!ret
, "Expected failure\n");
848 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
849 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
851 /* Valid call with the correct buffersize */
852 SetLastError(0xdeadbeef);
853 tempsize
= servicesize
;
855 ret
= GetServiceKeyNameA(scm_handle
, displayname
, servicename
, &servicesize
);
856 ok(ret
, "Expected success, got error %u\n", GetLastError());
859 ok(lstrlen(servicename
) == tempsize
/2,
860 "Expected the buffer to be twice the length of the string\n") ;
861 ok(!lstrcmpi(servicename
, spooler
), "Expected %s, got %s\n", spooler
, servicename
);
862 ok(servicesize
== (tempsize
* 2),
863 "Expected servicesize not to change if buffer not insufficient\n") ;
866 MultiByteToWideChar(CP_ACP
, 0, displayname
, -1, displaynameW
, sizeof(displaynameW
)/2);
867 SetLastError(0xdeadbeef);
869 ret
= GetServiceKeyNameW(scm_handle
, displaynameW
, servicenameW
, &servicesize
);
870 ok(ret
, "Expected success, got error %u\n", GetLastError());
873 ok(lstrlen(servicename
) == tempsize
/2,
874 "Expected the buffer to be twice the length of the string\n") ;
875 ok(servicesize
== lstrlenW(servicenameW
),
876 "Expected servicesize not to change if buffer not insufficient\n") ;
879 SetLastError(0xdeadbeef);
881 ret
= GetServiceKeyNameW(scm_handle
, displaynameW
, servicenameW
, &servicesize
);
882 ok(!ret
, "Expected failure\n");
883 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
884 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
885 ok(servicenameW
[0] == 0, "Buffer not empty\n");
887 CloseServiceHandle(scm_handle
);
890 static void test_query_svc(void)
892 SC_HANDLE scm_handle
, svc_handle
;
894 SERVICE_STATUS status
;
895 SERVICE_STATUS_PROCESS
*statusproc
;
896 DWORD bufsize
, needed
;
898 /* All NULL or wrong */
899 SetLastError(0xdeadbeef);
900 ret
= QueryServiceStatus(NULL
, NULL
);
901 ok(!ret
, "Expected failure\n");
902 ok(GetLastError() == ERROR_INVALID_HANDLE
,
903 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
905 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
907 /* Check if 'Spooler' exists.
908 * Open with not enough rights to query the status.
910 svc_handle
= OpenServiceA(scm_handle
, spooler
, STANDARD_RIGHTS_READ
);
913 skip("Spooler service doesn't exist\n");
914 CloseServiceHandle(scm_handle
);
918 SetLastError(0xdeadbeef);
919 ret
= QueryServiceStatus(svc_handle
, NULL
);
920 ok(!ret
, "Expected failure\n");
922 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
923 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
924 "Unexpected last error %d\n", GetLastError());
926 SetLastError(0xdeadbeef);
927 ret
= QueryServiceStatus(svc_handle
, &status
);
928 ok(!ret
, "Expected failure\n");
929 ok(GetLastError() == ERROR_ACCESS_DENIED
,
930 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
932 /* Open the service with just enough rights.
933 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
935 CloseServiceHandle(svc_handle
);
936 svc_handle
= OpenServiceA(scm_handle
, spooler
, SERVICE_QUERY_STATUS
);
938 SetLastError(0xdeadbeef);
939 ret
= QueryServiceStatus(svc_handle
, &status
);
940 ok(ret
, "Expected success, got error %u\n", GetLastError());
942 CloseServiceHandle(svc_handle
);
944 /* More or less the same tests for QueryServiceStatusEx */
946 /* Open service with not enough rights to query the status */
947 svc_handle
= OpenServiceA(scm_handle
, spooler
, STANDARD_RIGHTS_READ
);
949 /* All NULL or wrong, this proves that info level is checked first */
950 SetLastError(0xdeadbeef);
951 ret
= pQueryServiceStatusEx(NULL
, 1, NULL
, 0, NULL
);
952 ok(!ret
, "Expected failure\n");
954 ok(GetLastError() == ERROR_INVALID_LEVEL
,
955 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
957 /* Passing a NULL parameter for the needed buffer size
958 * will crash on anything but NT4.
961 /* Only info level is correct. It looks like the buffer/size is checked second */
962 SetLastError(0xdeadbeef);
963 ret
= pQueryServiceStatusEx(NULL
, 0, NULL
, 0, &needed
);
964 /* NT4 and Wine check the handle first */
965 if (GetLastError() != ERROR_INVALID_HANDLE
)
967 ok(!ret
, "Expected failure\n");
968 ok(needed
== sizeof(SERVICE_STATUS_PROCESS
),
969 "Needed buffersize is wrong : %d\n", needed
);
970 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
971 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
974 /* Pass a correct buffer and buffersize but a NULL handle */
975 statusproc
= HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS
));
977 SetLastError(0xdeadbeef);
978 ret
= pQueryServiceStatusEx(NULL
, 0, (BYTE
*)statusproc
, bufsize
, &needed
);
979 ok(!ret
, "Expected failure\n");
980 ok(GetLastError() == ERROR_INVALID_HANDLE
,
981 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
982 HeapFree(GetProcessHeap(), 0, statusproc
);
984 /* Correct handle and info level */
985 SetLastError(0xdeadbeef);
986 ret
= pQueryServiceStatusEx(svc_handle
, 0, NULL
, 0, &needed
);
987 /* NT4 doesn't return the needed size */
988 if (GetLastError() != ERROR_INVALID_PARAMETER
)
990 ok(!ret
, "Expected failure\n");
993 ok(needed
== sizeof(SERVICE_STATUS_PROCESS
),
994 "Needed buffersize is wrong : %d\n", needed
);
995 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
996 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1000 /* All parameters are OK but we don't have enough rights */
1001 statusproc
= HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS
));
1002 bufsize
= sizeof(SERVICE_STATUS_PROCESS
);
1003 SetLastError(0xdeadbeef);
1004 ret
= pQueryServiceStatusEx(svc_handle
, 0, (BYTE
*)statusproc
, bufsize
, &needed
);
1005 ok(!ret
, "Expected failure\n");
1006 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1007 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1008 HeapFree(GetProcessHeap(), 0, statusproc
);
1010 /* Open the service with just enough rights. */
1011 CloseServiceHandle(svc_handle
);
1012 svc_handle
= OpenServiceA(scm_handle
, spooler
, SERVICE_QUERY_STATUS
);
1014 /* Everything should be fine now. */
1015 statusproc
= HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS
));
1016 bufsize
= sizeof(SERVICE_STATUS_PROCESS
);
1017 SetLastError(0xdeadbeef);
1018 ret
= pQueryServiceStatusEx(svc_handle
, 0, (BYTE
*)statusproc
, bufsize
, &needed
);
1019 ok(ret
, "Expected success, got error %u\n", GetLastError());
1020 if (statusproc
->dwCurrentState
== SERVICE_RUNNING
)
1021 ok(statusproc
->dwProcessId
!= 0,
1022 "Expect a process id for this running service\n");
1024 ok(statusproc
->dwProcessId
== 0,
1025 "Expect no process id for this stopped service\n");
1026 HeapFree(GetProcessHeap(), 0, statusproc
);
1028 CloseServiceHandle(svc_handle
);
1029 CloseServiceHandle(scm_handle
);
1032 static void test_enum_svc(void)
1034 SC_HANDLE scm_handle
;
1036 DWORD bufsize
, needed
, returned
, resume
;
1037 DWORD neededW
, returnedW
;
1038 DWORD tempneeded
, tempreturned
, missing
;
1039 DWORD servicecountactive
, servicecountinactive
;
1040 ENUM_SERVICE_STATUS
*services
;
1041 ENUM_SERVICE_STATUS_PROCESS
*exservices
;
1044 /* All NULL or wrong */
1045 SetLastError(0xdeadbeef);
1046 ret
= EnumServicesStatusA(NULL
, 1, 0, NULL
, 0, NULL
, NULL
, NULL
);
1047 ok(!ret
, "Expected failure\n");
1049 ok(GetLastError() == ERROR_INVALID_HANDLE
,
1050 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1052 /* Open the service control manager with not enough rights at first */
1053 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
1055 /* Valid handle but rest is still NULL or wrong */
1056 SetLastError(0xdeadbeef);
1057 ret
= EnumServicesStatusA(scm_handle
, 1, 0, NULL
, 0, NULL
, NULL
, NULL
);
1058 ok(!ret
, "Expected failure\n");
1060 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1061 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1062 "Unexpected last error %d\n", GetLastError());
1064 /* Don't specify the two required pointers */
1065 returned
= 0xdeadbeef;
1066 SetLastError(0xdeadbeef);
1067 ret
= EnumServicesStatusA(scm_handle
, 0, 0, NULL
, 0, NULL
, &returned
, NULL
);
1068 ok(!ret
, "Expected failure\n");
1069 ok(returned
== 0xdeadbeef, "Expected no change to the number of services variable\n");
1071 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1072 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1073 "Unexpected last error %d\n", GetLastError());
1075 /* Don't specify the two required pointers */
1076 needed
= 0xdeadbeef;
1077 SetLastError(0xdeadbeef);
1078 ret
= EnumServicesStatusA(scm_handle
, 0, 0, NULL
, 0, &needed
, NULL
, NULL
);
1079 ok(!ret
, "Expected failure\n");
1080 ok(needed
== 0xdeadbeef || broken(needed
!= 0xdeadbeef), /* nt4 */
1081 "Expected no change to the needed buffer variable\n");
1083 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1084 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1085 "Unexpected last error %d\n", GetLastError());
1087 /* No valid servicetype and servicestate */
1088 needed
= 0xdeadbeef;
1089 returned
= 0xdeadbeef;
1090 SetLastError(0xdeadbeef);
1091 ret
= EnumServicesStatusA(scm_handle
, 0, 0, NULL
, 0, &needed
, &returned
, NULL
);
1092 ok(!ret
, "Expected failure\n");
1095 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1096 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1097 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1098 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1099 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1102 /* No valid servicetype and servicestate */
1103 needed
= 0xdeadbeef;
1104 returned
= 0xdeadbeef;
1105 SetLastError(0xdeadbeef);
1106 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, 0, NULL
, 0, &needed
, &returned
, NULL
);
1107 ok(!ret
, "Expected failure\n");
1110 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1111 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1112 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1113 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1114 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1117 /* No valid servicetype and servicestate */
1118 needed
= 0xdeadbeef;
1119 returned
= 0xdeadbeef;
1120 SetLastError(0xdeadbeef);
1121 ret
= EnumServicesStatusA(scm_handle
, 0, SERVICE_STATE_ALL
, NULL
, 0,
1122 &needed
, &returned
, NULL
);
1123 ok(!ret
, "Expected failure\n");
1126 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1127 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1128 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1129 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1130 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1133 /* All parameters are correct but our access rights are wrong */
1134 needed
= 0xdeadbeef;
1135 returned
= 0xdeadbeef;
1136 SetLastError(0xdeadbeef);
1137 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0,
1138 &needed
, &returned
, NULL
);
1139 ok(!ret
, "Expected failure\n");
1142 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1143 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1144 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1146 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1147 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1149 /* Open the service control manager with the needed rights */
1150 CloseServiceHandle(scm_handle
);
1151 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
1153 /* All parameters are correct. Request the needed buffer size */
1154 needed
= 0xdeadbeef;
1155 returned
= 0xdeadbeef;
1156 SetLastError(0xdeadbeef);
1157 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0,
1158 &needed
, &returned
, NULL
);
1159 ok(!ret
, "Expected failure\n");
1162 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size for this one service\n");
1163 ok(returned
== 0, "Expected no service returned, got %d\n", returned
);
1164 ok(GetLastError() == ERROR_MORE_DATA
,
1165 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1168 /* Test to show we get the same needed buffer size for the W-call */
1169 neededW
= 0xdeadbeef;
1170 ret
= EnumServicesStatusW(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0,
1171 &neededW
, &returnedW
, NULL
);
1172 ok(neededW
== needed
, "Expected needed buffersize to be the same for A- and W-calls\n");
1174 /* Store the needed bytes */
1175 tempneeded
= needed
;
1177 /* Allocate the correct needed bytes */
1178 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1180 needed
= 0xdeadbeef;
1181 returned
= 0xdeadbeef;
1182 SetLastError(0xdeadbeef);
1183 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1184 services
, bufsize
, &needed
, &returned
, NULL
);
1187 ok(ret
, "Expected success, got error %u\n", GetLastError());
1188 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1189 ok(returned
!= 0xdeadbeef && returned
> 0, "Expected some returned services\n");
1191 HeapFree(GetProcessHeap(), 0, services
);
1193 /* Store the number of returned services */
1194 tempreturned
= returned
;
1196 /* Allocate less than the needed bytes and don't specify a resume handle */
1197 services
= HeapAlloc(GetProcessHeap(), 0, tempneeded
);
1198 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUS
);
1199 needed
= 0xdeadbeef;
1200 returned
= 0xdeadbeef;
1201 SetLastError(0xdeadbeef);
1202 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1203 services
, bufsize
, &needed
, &returned
, NULL
);
1204 ok(!ret
, "Expected failure\n");
1207 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size for this one service\n");
1208 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1209 ok(GetLastError() == ERROR_MORE_DATA
,
1210 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1213 /* Allocate less than the needed bytes, this time with a correct resume handle */
1214 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUS
);
1215 needed
= 0xdeadbeef;
1216 returned
= 0xdeadbeef;
1218 SetLastError(0xdeadbeef);
1219 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1220 services
, bufsize
, &needed
, &returned
, &resume
);
1221 ok(!ret
, "Expected failure\n");
1224 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size for this one service\n");
1225 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1226 ok(resume
, "Expected a resume handle\n");
1227 ok(GetLastError() == ERROR_MORE_DATA
,
1228 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1231 /* Fetch the missing services but pass a bigger buffer size */
1232 missing
= tempreturned
- returned
;
1233 bufsize
= tempneeded
;
1234 needed
= 0xdeadbeef;
1235 returned
= 0xdeadbeef;
1236 SetLastError(0xdeadbeef);
1237 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1238 services
, bufsize
, &needed
, &returned
, &resume
);
1241 ok(ret
, "Expected success, got error %u\n", GetLastError());
1242 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1243 ok(returned
== missing
, "Expected %u services to be returned\n", missing
);
1245 ok(resume
== 0, "Expected the resume handle to be 0\n");
1246 HeapFree(GetProcessHeap(), 0, services
);
1248 /* See if things add up */
1250 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1251 * and doesn't count the others as inactive. This means that Vista could
1252 * show a total that is greater than the sum of active and inactive
1254 * The number of active and inactive drivers is greatly influenced by the
1255 * time when tests are run, immediately after boot or later for example.
1257 * Both reasons make calculations for drivers not so useful
1260 /* Get the number of active win32 services */
1261 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_ACTIVE
, NULL
, 0,
1262 &needed
, &returned
, NULL
);
1263 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1264 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_ACTIVE
, services
,
1265 needed
, &needed
, &returned
, NULL
);
1266 HeapFree(GetProcessHeap(), 0, services
);
1268 servicecountactive
= returned
;
1270 /* Get the number of inactive win32 services */
1271 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_INACTIVE
, NULL
, 0,
1272 &needed
, &returned
, NULL
);
1273 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1274 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_INACTIVE
, services
,
1275 needed
, &needed
, &returned
, NULL
);
1276 HeapFree(GetProcessHeap(), 0, services
);
1278 servicecountinactive
= returned
;
1280 /* Get the number of win32 services */
1281 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0,
1282 &needed
, &returned
, NULL
);
1283 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1284 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, services
,
1285 needed
, &needed
, &returned
, NULL
);
1286 HeapFree(GetProcessHeap(), 0, services
);
1288 /* Check if total is the same as active and inactive win32 services */
1290 ok(returned
== (servicecountactive
+ servicecountinactive
),
1291 "Something wrong in the calculation\n");
1293 /* Get all drivers and services
1295 * Fetch the status of the last call as failing could make the following tests crash
1296 * on Wine (we don't return anything yet).
1298 EnumServicesStatusA(scm_handle
, SERVICE_DRIVER
| SERVICE_WIN32
, SERVICE_STATE_ALL
,
1299 NULL
, 0, &needed
, &returned
, NULL
);
1300 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1301 ret
= EnumServicesStatusA(scm_handle
, SERVICE_DRIVER
| SERVICE_WIN32
, SERVICE_STATE_ALL
,
1302 services
, needed
, &needed
, &returned
, NULL
);
1304 /* Loop through all those returned drivers and services */
1305 for (i
= 0; ret
&& i
< returned
; i
++)
1307 SERVICE_STATUS status
= services
[i
].ServiceStatus
;
1309 /* lpServiceName and lpDisplayName should always be filled */
1310 ok(lstrlenA(services
[i
].lpServiceName
) > 0, "Expected a service name\n");
1311 ok(lstrlenA(services
[i
].lpDisplayName
) > 0, "Expected a display name\n");
1313 /* Decrement the counters to see if the functions calls return the same
1314 * numbers as the contents of these structures.
1316 if (status
.dwServiceType
& (SERVICE_WIN32_OWN_PROCESS
| SERVICE_WIN32_SHARE_PROCESS
))
1318 if (status
.dwCurrentState
== SERVICE_RUNNING
)
1319 servicecountactive
--;
1321 servicecountinactive
--;
1324 HeapFree(GetProcessHeap(), 0, services
);
1328 ok(servicecountactive
== 0, "Active services mismatch %u\n", servicecountactive
);
1329 ok(servicecountinactive
== 0, "Inactive services mismatch %u\n", servicecountinactive
);
1332 CloseServiceHandle(scm_handle
);
1334 /* More or less the same for EnumServicesStatusExA */
1336 /* All NULL or wrong */
1337 SetLastError(0xdeadbeef);
1338 ret
= pEnumServicesStatusExA(NULL
, 1, 0, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1339 ok(!ret
, "Expected failure\n");
1341 ok(GetLastError() == ERROR_INVALID_LEVEL
,
1342 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1344 /* All NULL or wrong, just the info level is correct */
1345 SetLastError(0xdeadbeef);
1346 ret
= pEnumServicesStatusExA(NULL
, 0, 0, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1347 ok(!ret
, "Expected failure\n");
1349 ok(GetLastError() == ERROR_INVALID_HANDLE
,
1350 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1352 /* Open the service control manager with not enough rights at first */
1353 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
1355 /* Valid handle and info level but rest is still NULL or wrong */
1356 SetLastError(0xdeadbeef);
1357 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1358 ok(!ret
, "Expected failure\n");
1360 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1361 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1362 "Unexpected last error %d\n", GetLastError());
1364 /* Don't specify the two required pointers */
1365 needed
= 0xdeadbeef;
1366 SetLastError(0xdeadbeef);
1367 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, &needed
, NULL
, NULL
, NULL
);
1368 ok(!ret
, "Expected failure\n");
1369 ok(needed
== 0xdeadbeef || broken(needed
!= 0xdeadbeef), /* nt4 */
1370 "Expected no change to the needed buffer variable\n");
1372 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1373 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1374 "Unexpected last error %d\n", GetLastError());
1376 /* Don't specify the two required pointers */
1377 returned
= 0xdeadbeef;
1378 SetLastError(0xdeadbeef);
1379 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, NULL
, &returned
, NULL
, NULL
);
1380 ok(!ret
, "Expected failure\n");
1383 ok(returned
== 0xdeadbeef, "Expected no change to the number of services variable\n");
1384 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1385 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1386 "Unexpected last error %d\n", GetLastError());
1389 /* No valid servicetype and servicestate */
1390 needed
= 0xdeadbeef;
1391 returned
= 0xdeadbeef;
1392 SetLastError(0xdeadbeef);
1393 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1394 ok(!ret
, "Expected failure\n");
1395 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1398 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1399 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1400 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1401 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1404 /* No valid servicestate */
1405 needed
= 0xdeadbeef;
1406 returned
= 0xdeadbeef;
1407 SetLastError(0xdeadbeef);
1408 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, 0, NULL
, 0,
1409 &needed
, &returned
, NULL
, NULL
);
1410 ok(!ret
, "Expected failure\n");
1411 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1414 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1415 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1416 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1417 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1420 /* No valid servicetype */
1421 needed
= 0xdeadbeef;
1422 returned
= 0xdeadbeef;
1423 SetLastError(0xdeadbeef);
1424 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, SERVICE_STATE_ALL
, NULL
, 0,
1425 &needed
, &returned
, NULL
, NULL
);
1426 ok(!ret
, "Expected failure\n");
1427 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1430 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1431 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1432 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1433 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1436 /* No valid servicetype and servicestate and unknown service group */
1437 needed
= 0xdeadbeef;
1438 returned
= 0xdeadbeef;
1439 SetLastError(0xdeadbeef);
1440 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, &needed
,
1441 &returned
, NULL
, "deadbeef_group");
1442 ok(!ret
, "Expected failure\n");
1443 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1446 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1447 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1448 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1449 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1452 /* All parameters are correct but our access rights are wrong */
1453 needed
= 0xdeadbeef;
1454 returned
= 0xdeadbeef;
1455 SetLastError(0xdeadbeef);
1456 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1457 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1458 ok(!ret
, "Expected failure\n");
1460 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1461 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1462 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1463 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1464 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1466 /* All parameters are correct, access rights are wrong but the
1467 * group name won't be checked yet.
1469 needed
= 0xdeadbeef;
1470 returned
= 0xdeadbeef;
1471 SetLastError(0xdeadbeef);
1472 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1473 NULL
, 0, &needed
, &returned
, NULL
, "deadbeef_group");
1474 ok(!ret
, "Expected failure\n");
1476 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1477 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1478 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1479 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1480 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1482 /* Open the service control manager with the needed rights */
1483 CloseServiceHandle(scm_handle
);
1484 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
1486 /* All parameters are correct and the group will be checked */
1487 needed
= 0xdeadbeef;
1488 returned
= 0xdeadbeef;
1489 SetLastError(0xdeadbeef);
1490 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1491 NULL
, 0, &needed
, &returned
, NULL
, "deadbeef_group");
1492 ok(!ret
, "Expected failure\n");
1493 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1496 ok(needed
== 0, "Expected needed buffer size to be set to 0, got %d\n", needed
);
1497 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
1498 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1501 /* TODO: Create a test that makes sure we enumerate all services that don't
1502 * belong to a group. (specifying "").
1505 /* All parameters are correct. Request the needed buffer size */
1506 needed
= 0xdeadbeef;
1507 returned
= 0xdeadbeef;
1508 SetLastError(0xdeadbeef);
1509 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1510 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1511 ok(!ret
, "Expected failure\n");
1512 ok(returned
== 0, "Expected no service returned, got %d\n", returned
);
1515 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size\n");
1516 ok(GetLastError() == ERROR_MORE_DATA
,
1517 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1520 /* Test to show we get the same needed buffer size for the W-call */
1521 neededW
= 0xdeadbeef;
1522 ret
= pEnumServicesStatusExW(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1523 NULL
, 0, &neededW
, &returnedW
, NULL
, NULL
);
1524 ok(neededW
== needed
, "Expected needed buffersize to be the same for A- and W-calls\n");
1526 /* Store the needed bytes */
1527 tempneeded
= needed
;
1529 /* Allocate the correct needed bytes */
1530 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1532 needed
= 0xdeadbeef;
1533 returned
= 0xdeadbeef;
1534 SetLastError(0xdeadbeef);
1535 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1536 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, NULL
, NULL
);
1539 ok(ret
, "Expected success, got error %u\n", GetLastError());
1540 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1541 ok(returned
== tempreturned
, "Expected the same number of service from this function\n");
1543 HeapFree(GetProcessHeap(), 0, exservices
);
1545 /* Store the number of returned services */
1546 tempreturned
= returned
;
1548 /* Allocate less than the needed bytes and don't specify a resume handle */
1549 exservices
= HeapAlloc(GetProcessHeap(), 0, tempneeded
);
1550 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUS
);
1551 needed
= 0xdeadbeef;
1552 returned
= 0xdeadbeef;
1553 SetLastError(0xdeadbeef);
1554 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1555 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, NULL
, NULL
);
1556 ok(!ret
, "Expected failure\n");
1559 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size\n");
1560 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1561 ok(GetLastError() == ERROR_MORE_DATA
,
1562 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1565 /* Allocate less than the needed bytes, this time with a correct resume handle */
1566 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUS
);
1567 needed
= 0xdeadbeef;
1568 returned
= 0xdeadbeef;
1570 SetLastError(0xdeadbeef);
1571 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1572 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, &resume
, NULL
);
1573 ok(!ret
, "Expected failure\n");
1576 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size\n");
1577 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1578 ok(resume
, "Expected a resume handle\n");
1579 ok(GetLastError() == ERROR_MORE_DATA
,
1580 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1583 /* Fetch that last service but pass a bigger buffer size */
1584 missing
= tempreturned
- returned
;
1585 bufsize
= tempneeded
;
1586 needed
= 0xdeadbeef;
1587 returned
= 0xdeadbeef;
1588 SetLastError(0xdeadbeef);
1589 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1590 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, &resume
, NULL
);
1593 ok(ret
, "Expected success, got error %u\n", GetLastError());
1594 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1596 ok(returned
== missing
, "Expected %u services to be returned\n", missing
);
1597 ok(resume
== 0, "Expected the resume handle to be 0\n");
1598 HeapFree(GetProcessHeap(), 0, exservices
);
1600 /* See if things add up */
1602 /* Get the number of active win32 services */
1603 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_ACTIVE
,
1604 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1605 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1606 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_ACTIVE
,
1607 (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1608 HeapFree(GetProcessHeap(), 0, exservices
);
1610 servicecountactive
= returned
;
1612 /* Get the number of inactive win32 services */
1613 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_INACTIVE
,
1614 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1615 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1616 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_INACTIVE
,
1617 (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1618 HeapFree(GetProcessHeap(), 0, exservices
);
1620 servicecountinactive
= returned
;
1622 /* Get the number of win32 services */
1623 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1624 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1625 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1626 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1627 (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1628 HeapFree(GetProcessHeap(), 0, exservices
);
1630 /* Check if total is the same as active and inactive win32 services */
1631 ok(returned
== (servicecountactive
+ servicecountinactive
),
1632 "Something wrong in the calculation\n");
1634 /* Get all drivers and services */
1635 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
| SERVICE_DRIVER
,
1636 SERVICE_STATE_ALL
, NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1637 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1638 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
| SERVICE_DRIVER
,
1639 SERVICE_STATE_ALL
, (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1641 /* Loop through all those returned drivers and services */
1642 for (i
= 0; i
< returned
; i
++)
1644 SERVICE_STATUS_PROCESS status
= exservices
[i
].ServiceStatusProcess
;
1647 /* lpServiceName and lpDisplayName should always be filled */
1648 ok(lstrlenA(exservices
[i
].lpServiceName
) > 0, "Expected a service name\n");
1649 ok(lstrlenA(exservices
[i
].lpDisplayName
) > 0, "Expected a display name\n");
1651 /* Decrement the counters to see if the functions calls return the
1652 * same numbers as the contents of these structures.
1653 * Check some process id specifics.
1655 if (status
.dwServiceType
& (SERVICE_FILE_SYSTEM_DRIVER
| SERVICE_KERNEL_DRIVER
))
1657 /* We shouldn't have a process id for drivers */
1658 ok(status
.dwProcessId
== 0,
1659 "This driver shouldn't have an associated process id\n");
1662 if (status
.dwServiceType
& (SERVICE_WIN32_OWN_PROCESS
| SERVICE_WIN32_SHARE_PROCESS
))
1664 if (status
.dwCurrentState
== SERVICE_RUNNING
)
1666 /* We expect a process id for every running service */
1667 ok(status
.dwProcessId
> 0, "Expected a process id for this running service (%s)\n",
1668 exservices
[i
].lpServiceName
);
1670 servicecountactive
--;
1674 /* We shouldn't have a process id for inactive services */
1675 ok(status
.dwProcessId
== 0, "Service %s state %u shouldn't have an associated process id\n",
1676 exservices
[i
].lpServiceName
, status
.dwCurrentState
);
1678 servicecountinactive
--;
1682 HeapFree(GetProcessHeap(), 0, exservices
);
1684 ok(servicecountactive
== 0, "Active services mismatch %u\n", servicecountactive
);
1685 ok(servicecountinactive
== 0, "Inactive services mismatch %u\n", servicecountinactive
);
1687 CloseServiceHandle(scm_handle
);
1690 static void test_close(void)
1696 SetLastError(0xdeadbeef);
1697 ret
= CloseServiceHandle(NULL
);
1698 ok(!ret
, "Expected failure\n");
1699 ok(GetLastError() == ERROR_INVALID_HANDLE
, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1701 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1704 handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
1705 SetLastError(0xdeadbeef);
1706 ret
= CloseServiceHandle(handle
);
1707 ok(ret
, "Expected success got error %u\n", GetLastError());
1710 static void test_sequence(void)
1712 SC_HANDLE scm_handle
, svc_handle
;
1714 QUERY_SERVICE_CONFIGA
*config
;
1715 DWORD given
, needed
;
1716 static const CHAR servicename
[] = "Winetest";
1717 static const CHAR displayname
[] = "Winetest dummy service";
1718 static const CHAR displayname2
[] = "Winetest dummy service (2)";
1719 static const CHAR pathname
[] = "we_dont_care.exe";
1720 static const CHAR dependencies
[] = "Master1\0Master2\0+MasterGroup1\0";
1721 static const CHAR password
[] = "";
1722 static const CHAR empty
[] = "";
1723 static const CHAR localsystem
[] = "LocalSystem";
1725 SetLastError(0xdeadbeef);
1726 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
1728 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
1730 skip("Not enough rights to get a handle to the manager\n");
1734 ok(scm_handle
!= NULL
, "Could not get a handle to the manager: %d\n", GetLastError());
1736 if (!scm_handle
) return;
1738 /* Create a dummy service */
1739 SetLastError(0xdeadbeef);
1740 svc_handle
= CreateServiceA(scm_handle
, servicename
, displayname
, GENERIC_ALL
,
1741 SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
, SERVICE_DISABLED
, SERVICE_ERROR_IGNORE
,
1742 pathname
, NULL
, NULL
, dependencies
, NULL
, password
);
1744 if (!svc_handle
&& (GetLastError() == ERROR_SERVICE_EXISTS
))
1746 /* We try and open the service and do the rest of the tests. Some could
1747 * fail if the tests were changed between these runs.
1749 trace("Deletion probably didn't work last time\n");
1750 SetLastError(0xdeadbeef);
1751 svc_handle
= OpenServiceA(scm_handle
, servicename
, GENERIC_ALL
);
1752 if (!svc_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
1754 skip("Not enough rights to open the service\n");
1755 CloseServiceHandle(scm_handle
);
1758 ok(svc_handle
!= NULL
, "Could not open the service : %d\n", GetLastError());
1760 else if (!svc_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
1762 skip("Not enough rights to create the service\n");
1763 CloseServiceHandle(scm_handle
);
1768 ok(svc_handle
!= NULL
, "Could not create the service : %d\n", GetLastError());
1769 if ((svc_handle
!= NULL
) && (pGetSecurityInfo
!= NULL
))
1771 PSID sidOwner
, sidGroup
;
1773 PSECURITY_DESCRIPTOR pSD
;
1774 HRESULT retval
= pGetSecurityInfo(svc_handle
,SE_SERVICE
,DACL_SECURITY_INFORMATION
,&sidOwner
,&sidGroup
,&dacl
,&sacl
,&pSD
);
1775 todo_wine
ok(ERROR_SUCCESS
== retval
, "Expected GetSecurityInfo to succeed: result %d\n",retval
);
1779 if (!svc_handle
) return;
1782 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1783 * that the correct keys are used.
1786 /* Request the size for the buffer */
1787 SetLastError(0xdeadbeef);
1788 ret
= QueryServiceConfigA(svc_handle
, NULL
, 0, &needed
);
1789 ok(!ret
, "Expected failure\n");
1790 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1792 config
= HeapAlloc(GetProcessHeap(), 0, needed
);
1794 SetLastError(0xdeadbeef);
1795 ret
= QueryServiceConfigA(svc_handle
, config
, given
, &needed
);
1796 ok(ret
, "Expected success, got error %u\n", GetLastError());
1798 ok(config
->lpBinaryPathName
&& config
->lpLoadOrderGroup
&& config
->lpDependencies
&& config
->lpServiceStartName
&&
1799 config
->lpDisplayName
, "Expected all string struct members to be non-NULL\n");
1800 ok(config
->dwServiceType
== (SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
),
1801 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config
->dwServiceType
);
1802 ok(config
->dwStartType
== SERVICE_DISABLED
, "Expected SERVICE_DISABLED, got %d\n", config
->dwStartType
);
1803 ok(config
->dwErrorControl
== SERVICE_ERROR_IGNORE
, "Expected SERVICE_ERROR_IGNORE, got %d\n", config
->dwErrorControl
);
1804 ok(!strcmp(config
->lpBinaryPathName
, pathname
), "Expected '%s', got '%s'\n", pathname
, config
->lpBinaryPathName
);
1805 ok(!strcmp(config
->lpLoadOrderGroup
, empty
), "Expected an empty string, got '%s'\n", config
->lpLoadOrderGroup
);
1806 ok(config
->dwTagId
== 0, "Expected 0, got %d\n", config
->dwTagId
);
1807 /* TODO: Show the double 0 terminated string */
1810 ok(!memcmp(config
->lpDependencies
, dependencies
, sizeof(dependencies
)), "Wrong string\n");
1812 ok(!strcmp(config
->lpServiceStartName
, localsystem
), "Expected 'LocalSystem', got '%s'\n", config
->lpServiceStartName
);
1813 ok(!strcmp(config
->lpDisplayName
, displayname
), "Expected '%s', got '%s'\n", displayname
, config
->lpDisplayName
);
1815 ok(ChangeServiceConfigA(svc_handle
, SERVICE_NO_CHANGE
, SERVICE_NO_CHANGE
, SERVICE_ERROR_NORMAL
, NULL
, "TestGroup2", NULL
, NULL
, NULL
, NULL
, displayname2
),
1816 "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1818 QueryServiceConfigA(svc_handle
, NULL
, 0, &needed
);
1819 config
= HeapReAlloc(GetProcessHeap(), 0, config
, needed
);
1820 ok(QueryServiceConfigA(svc_handle
, config
, needed
, &needed
), "QueryServiceConfig failed\n");
1821 ok(config
->lpBinaryPathName
&& config
->lpLoadOrderGroup
&& config
->lpDependencies
&& config
->lpServiceStartName
&&
1822 config
->lpDisplayName
, "Expected all string struct members to be non-NULL\n");
1823 ok(config
->dwServiceType
== (SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
),
1824 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config
->dwServiceType
);
1825 ok(config
->dwStartType
== SERVICE_DISABLED
, "Expected SERVICE_DISABLED, got %d\n", config
->dwStartType
);
1826 ok(config
->dwErrorControl
== SERVICE_ERROR_NORMAL
, "Expected SERVICE_ERROR_NORMAL, got %d\n", config
->dwErrorControl
);
1827 ok(!strcmp(config
->lpBinaryPathName
, pathname
), "Expected '%s', got '%s'\n", pathname
, config
->lpBinaryPathName
);
1828 ok(!strcmp(config
->lpLoadOrderGroup
, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config
->lpLoadOrderGroup
);
1829 ok(config
->dwTagId
== 0, "Expected 0, got %d\n", config
->dwTagId
);
1830 ok(!strcmp(config
->lpServiceStartName
, localsystem
), "Expected 'LocalSystem', got '%s'\n", config
->lpServiceStartName
);
1831 ok(!strcmp(config
->lpDisplayName
, displayname2
), "Expected '%s', got '%s'\n", displayname2
, config
->lpDisplayName
);
1833 SetLastError(0xdeadbeef);
1834 ret
= DeleteService(svc_handle
);
1835 ok(ret
, "Expected success, got error %u\n", GetLastError());
1836 CloseServiceHandle(svc_handle
);
1838 /* Wait a while. The following test does a CreateService again */
1841 CloseServiceHandle(scm_handle
);
1842 HeapFree(GetProcessHeap(), 0, config
);
1845 static void test_queryconfig2(void)
1847 SC_HANDLE scm_handle
, svc_handle
;
1849 DWORD expected
, needed
;
1850 BYTE buffer
[MAX_PATH
];
1851 LPSERVICE_DESCRIPTIONA pConfig
= (LPSERVICE_DESCRIPTIONA
)buffer
;
1852 static const CHAR servicename
[] = "Winetest";
1853 static const CHAR displayname
[] = "Winetest dummy service";
1854 static const CHAR pathname
[] = "we_dont_care.exe";
1855 static const CHAR dependencies
[] = "Master1\0Master2\0+MasterGroup1\0";
1856 static const CHAR password
[] = "";
1857 static const CHAR description
[] = "Description";
1859 if(!pQueryServiceConfig2A
)
1861 win_skip("function QueryServiceConfig2A not present\n");
1865 SetLastError(0xdeadbeef);
1866 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
1870 if(GetLastError() == ERROR_ACCESS_DENIED
)
1871 skip("Not enough rights to get a handle to the manager\n");
1873 ok(FALSE
, "Could not get a handle to the manager: %d\n", GetLastError());
1877 /* Create a dummy service */
1878 SetLastError(0xdeadbeef);
1879 svc_handle
= CreateServiceA(scm_handle
, servicename
, displayname
, GENERIC_ALL
,
1880 SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
, SERVICE_DISABLED
, SERVICE_ERROR_IGNORE
,
1881 pathname
, NULL
, NULL
, dependencies
, NULL
, password
);
1885 if(GetLastError() == ERROR_SERVICE_EXISTS
)
1887 /* We try and open the service and do the rest of the tests. Some could
1888 * fail if the tests were changed between these runs.
1890 trace("Deletion probably didn't work last time\n");
1891 SetLastError(0xdeadbeef);
1892 svc_handle
= OpenServiceA(scm_handle
, servicename
, GENERIC_ALL
);
1895 if(GetLastError() == ERROR_ACCESS_DENIED
)
1896 skip("Not enough rights to open the service\n");
1898 ok(FALSE
, "Could not open the service : %d\n", GetLastError());
1899 CloseServiceHandle(scm_handle
);
1903 if (GetLastError() == ERROR_ACCESS_DENIED
)
1905 skip("Not enough rights to create the service\n");
1906 CloseServiceHandle(scm_handle
);
1909 ok(svc_handle
!= NULL
, "Could not create the service : %d\n", GetLastError());
1912 CloseServiceHandle(scm_handle
);
1916 SetLastError(0xdeadbeef);
1917 ret
= pQueryServiceConfig2A(svc_handle
,0xfff0,buffer
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
1918 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1919 ok(ERROR_INVALID_LEVEL
== GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1921 SetLastError(0xdeadbeef);
1922 ret
= pQueryServiceConfig2A(svc_handle
,0xfff0,buffer
,sizeof(SERVICE_DESCRIPTIONA
),NULL
);
1923 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1924 ok(ERROR_INVALID_LEVEL
== GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1926 SetLastError(0xdeadbeef);
1927 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
),NULL
);
1928 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1929 ok(ERROR_INVALID_ADDRESS
== GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1931 SetLastError(0xdeadbeef);
1932 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
1933 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1934 ok((ERROR_INVALID_ADDRESS
== GetLastError()) || (ERROR_INSUFFICIENT_BUFFER
== GetLastError()),
1935 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1937 SetLastError(0xdeadbeef);
1938 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,sizeof(SERVICE_DESCRIPTIONA
),NULL
);
1939 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1940 ok(ERROR_INVALID_ADDRESS
== GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1943 SetLastError(0xdeadbeef);
1944 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
)-1,&needed
);
1945 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1946 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1947 ok(needed
== sizeof(SERVICE_DESCRIPTIONA
), "got %d\n", needed
);
1950 pConfig
->lpDescription
= (LPSTR
)0xdeadbeef;
1951 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
1952 ok(ret
, "expected QueryServiceConfig2A to succeed\n");
1953 ok(needed
== sizeof(SERVICE_DESCRIPTIONA
), "got %d\n", needed
);
1954 ok(!pConfig
->lpDescription
, "expected lpDescription to be NULL, got %p\n", pConfig
->lpDescription
);
1956 SetLastError(0xdeadbeef);
1958 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,0,&needed
);
1959 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1960 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1961 ok(needed
== sizeof(SERVICE_DESCRIPTIONA
), "got %d\n", needed
);
1963 if(!pChangeServiceConfig2A
)
1965 win_skip("function ChangeServiceConfig2A not present\n");
1969 pConfig
->lpDescription
= (LPSTR
) description
;
1970 ret
= pChangeServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
);
1971 ok(ret
, "ChangeServiceConfig2A failed\n");
1976 SetLastError(0xdeadbeef);
1978 expected
= sizeof(SERVICE_DESCRIPTIONA
) + sizeof(description
) * sizeof(WCHAR
); /* !! */
1979 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
1980 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1981 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1982 ok(needed
== expected
, "expected needed to be %d, got %d\n", expected
, needed
);
1984 SetLastError(0xdeadbeef);
1985 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,needed
-1,&needed
);
1986 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
1987 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1989 SetLastError(0xdeadbeef);
1990 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,needed
,&needed
);
1991 ok(ret
, "expected QueryServiceConfig2A to succeed\n");
1992 ok(pConfig
->lpDescription
&& !strcmp(description
,pConfig
->lpDescription
),
1993 "expected lpDescription to be %s, got %s\n",description
,pConfig
->lpDescription
);
1995 SetLastError(0xdeadbeef);
1996 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
, needed
+ 1,&needed
);
1997 ok(ret
, "expected QueryServiceConfig2A to succeed\n");
1998 ok(pConfig
->lpDescription
&& !strcmp(description
,pConfig
->lpDescription
),
1999 "expected lpDescription to be %s, got %s\n",description
,pConfig
->lpDescription
);
2001 if(!pQueryServiceConfig2W
)
2003 win_skip("function QueryServiceConfig2W not present\n");
2006 SetLastError(0xdeadbeef);
2008 expected
= sizeof(SERVICE_DESCRIPTIONW
) + sizeof(WCHAR
) * sizeof(description
);
2009 ret
= pQueryServiceConfig2W(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,0,&needed
);
2010 ok(!ret
, "expected QueryServiceConfig2W to fail\n");
2011 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2012 ok(needed
== expected
, "expected needed to be %d, got %d\n", expected
, needed
);
2014 SetLastError(0xdeadbeef);
2015 ret
= pQueryServiceConfig2W(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
, needed
,&needed
);
2016 ok(ret
, "expected QueryServiceConfig2W to succeed\n");
2019 DeleteService(svc_handle
);
2021 CloseServiceHandle(svc_handle
);
2023 /* Wait a while. The following test does a CreateService again */
2026 CloseServiceHandle(scm_handle
);
2029 static void test_refcount(void)
2031 SC_HANDLE scm_handle
, svc_handle1
, svc_handle2
, svc_handle3
, svc_handle4
, svc_handle5
;
2032 static const CHAR servicename
[] = "Winetest";
2033 static const CHAR pathname
[] = "we_dont_care.exe";
2036 /* Get a handle to the Service Control Manager */
2037 SetLastError(0xdeadbeef);
2038 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
2039 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
2041 skip("Not enough rights to get a handle to the manager\n");
2045 /* Create a service */
2046 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
,
2047 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
2048 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
2049 ok(svc_handle1
!= NULL
, "Expected success, got error %u\n", GetLastError());
2051 /* Get a handle to this new service */
2052 svc_handle2
= OpenServiceA(scm_handle
, servicename
, GENERIC_READ
);
2053 ok(svc_handle2
!= NULL
, "Expected success, got error %u\n", GetLastError());
2055 /* Get another handle to this new service */
2056 svc_handle3
= OpenServiceA(scm_handle
, servicename
, GENERIC_READ
);
2057 ok(svc_handle3
!= NULL
, "Expected success, got error %u\n", GetLastError());
2059 /* Check if we can close the handle to the Service Control Manager */
2060 ret
= CloseServiceHandle(scm_handle
);
2061 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2063 /* Get a new handle to the Service Control Manager */
2064 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
2065 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
2067 /* Get a handle to this new service */
2068 svc_handle4
= OpenServiceA(scm_handle
, servicename
, GENERIC_ALL
);
2069 ok(svc_handle4
!= NULL
, "Expected success, got error %u\n", GetLastError());
2071 /* Delete the service */
2072 ret
= DeleteService(svc_handle4
);
2073 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2075 /* We cannot create the same service again as it's still marked as 'being deleted'.
2076 * The reason is that we still have 4 open handles to this service even though we
2077 * closed the handle to the Service Control Manager in between.
2079 SetLastError(0xdeadbeef);
2080 svc_handle5
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
,
2081 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
2082 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
2085 ok(!svc_handle5
, "Expected failure\n");
2086 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE
,
2087 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2090 /* FIXME: Remove this when Wine is fixed */
2093 DeleteService(svc_handle5
);
2094 CloseServiceHandle(svc_handle5
);
2097 /* Close all the handles to the service and try again */
2098 ret
= CloseServiceHandle(svc_handle4
);
2099 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2100 ret
= CloseServiceHandle(svc_handle3
);
2101 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2102 ret
= CloseServiceHandle(svc_handle2
);
2103 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2104 ret
= CloseServiceHandle(svc_handle1
);
2105 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2107 /* Wait a while. Doing a CreateService too soon will result again
2108 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2112 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2113 svc_handle5
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
,
2114 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
2115 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
2116 ok(svc_handle5
!= NULL
, "Expected success, got error %u\n", GetLastError());
2118 /* Delete the service */
2119 ret
= DeleteService(svc_handle5
);
2120 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2122 /* Wait a while. Just in case one of the following tests does a CreateService again */
2125 CloseServiceHandle(svc_handle5
);
2126 CloseServiceHandle(scm_handle
);
2131 SC_HANDLE scm_handle
;
2133 /* Bail out if we are on win98 */
2134 SetLastError(0xdeadbeef);
2135 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
2137 if (!scm_handle
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2139 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2142 CloseServiceHandle(scm_handle
);
2144 init_function_pointers();
2146 /* First some parameter checking */
2149 test_create_delete_svc();
2150 test_get_displayname();
2151 test_get_servicekeyname();
2155 /* Test the creation, querying and deletion of a service */
2157 test_queryconfig2();
2158 /* The main reason for this test is to check if any refcounting is used
2159 * and what the rules are