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
23 static const CHAR spooler
[] = "Spooler"; /* Should be available on all platforms */
24 static CHAR selfname
[MAX_PATH
];
26 static BOOL (WINAPI
*pChangeServiceConfig2A
)(SC_HANDLE
,DWORD
,LPVOID
);
27 static BOOL (WINAPI
*pEnumServicesStatusExA
)(SC_HANDLE
, SC_ENUM_TYPE
, DWORD
,
28 DWORD
, LPBYTE
, DWORD
, LPDWORD
,
29 LPDWORD
, LPDWORD
, LPCSTR
);
30 static BOOL (WINAPI
*pEnumServicesStatusExW
)(SC_HANDLE
, SC_ENUM_TYPE
, DWORD
,
31 DWORD
, LPBYTE
, DWORD
, LPDWORD
,
32 LPDWORD
, LPDWORD
, LPCWSTR
);
33 static DWORD (WINAPI
*pGetSecurityInfo
)(HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
34 PSID
*, PSID
*, PACL
*, PACL
*, PSECURITY_DESCRIPTOR
*);
35 static BOOL (WINAPI
*pQueryServiceConfig2A
)(SC_HANDLE
,DWORD
,LPBYTE
,DWORD
,LPDWORD
);
36 static BOOL (WINAPI
*pQueryServiceConfig2W
)(SC_HANDLE
,DWORD
,LPBYTE
,DWORD
,LPDWORD
);
37 static BOOL (WINAPI
*pQueryServiceStatusEx
)(SC_HANDLE
, SC_STATUS_TYPE
, LPBYTE
,
39 static BOOL (WINAPI
*pQueryServiceObjectSecurity
)(SC_HANDLE
, SECURITY_INFORMATION
,
40 PSECURITY_DESCRIPTOR
, DWORD
, LPDWORD
);
41 static DWORD (WINAPI
*pNotifyServiceStatusChangeW
)(SC_HANDLE
,DWORD
,SERVICE_NOTIFYW
*);
43 static void init_function_pointers(void)
45 HMODULE hadvapi32
= GetModuleHandleA("advapi32.dll");
47 pChangeServiceConfig2A
= (void*)GetProcAddress(hadvapi32
, "ChangeServiceConfig2A");
48 pEnumServicesStatusExA
= (void*)GetProcAddress(hadvapi32
, "EnumServicesStatusExA");
49 pEnumServicesStatusExW
= (void*)GetProcAddress(hadvapi32
, "EnumServicesStatusExW");
50 pGetSecurityInfo
= (void *)GetProcAddress(hadvapi32
, "GetSecurityInfo");
51 pQueryServiceConfig2A
= (void*)GetProcAddress(hadvapi32
, "QueryServiceConfig2A");
52 pQueryServiceConfig2W
= (void*)GetProcAddress(hadvapi32
, "QueryServiceConfig2W");
53 pQueryServiceStatusEx
= (void*)GetProcAddress(hadvapi32
, "QueryServiceStatusEx");
54 pQueryServiceObjectSecurity
= (void*)GetProcAddress(hadvapi32
, "QueryServiceObjectSecurity");
55 pNotifyServiceStatusChangeW
= (void*)GetProcAddress(hadvapi32
, "NotifyServiceStatusChangeW");
58 static void test_open_scm(void)
62 /* No access rights */
63 SetLastError(0xdeadbeef);
64 scm_handle
= OpenSCManagerA(NULL
, NULL
, 0);
65 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
66 CloseServiceHandle(scm_handle
);
68 /* Unknown database name */
69 SetLastError(0xdeadbeef);
70 scm_handle
= OpenSCManagerA(NULL
, "DoesNotExist", SC_MANAGER_CONNECT
);
71 ok(!scm_handle
, "Expected failure\n");
72 ok(GetLastError() == ERROR_INVALID_NAME
, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
73 CloseServiceHandle(scm_handle
); /* Just in case */
75 /* MSDN says only ServiceActive is allowed, or NULL */
76 SetLastError(0xdeadbeef);
77 scm_handle
= OpenSCManagerA(NULL
, SERVICES_FAILED_DATABASEA
, SC_MANAGER_CONNECT
);
78 ok(!scm_handle
, "Expected failure\n");
79 ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST
, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
80 CloseServiceHandle(scm_handle
); /* Just in case */
82 /* Remote unknown host */
83 SetLastError(0xdeadbeef);
84 scm_handle
= OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA
, SC_MANAGER_CONNECT
);
87 ok(!scm_handle
, "Expected failure\n");
88 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE
|| GetLastError() == RPC_S_INVALID_NET_ADDR
/* w2k8 */,
89 "Expected RPC_S_SERVER_UNAVAILABLE or RPC_S_INVALID_NET_ADDR, got %d\n", GetLastError());
91 CloseServiceHandle(scm_handle
); /* Just in case */
93 /* Proper call with an empty hostname */
94 scm_handle
= OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA
, SC_MANAGER_CONNECT
);
95 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
96 CloseServiceHandle(scm_handle
);
98 /* Again a correct one */
99 SetLastError(0xdeadbeef);
100 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
101 ok(GetLastError() == ERROR_SUCCESS
|| broken(GetLastError() == ERROR_IO_PENDING
) /* win2k */,
102 "Expected ERROR_SUCCESS, got %u\n", GetLastError());
103 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
104 CloseServiceHandle(scm_handle
);
107 static void test_open_svc(void)
109 SC_HANDLE scm_handle
, svc_handle
;
110 CHAR displayname
[4096];
113 /* All NULL (invalid access rights) */
114 SetLastError(0xdeadbeef);
115 svc_handle
= OpenServiceA(NULL
, NULL
, 0);
116 ok(!svc_handle
, "Expected failure\n");
117 ok(GetLastError() == ERROR_INVALID_HANDLE
, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
119 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
122 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
123 SetLastError(0xdeadbeef);
124 svc_handle
= OpenServiceA(scm_handle
, NULL
, GENERIC_READ
);
125 ok(!svc_handle
, "Expected failure\n");
126 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
127 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
128 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
129 CloseServiceHandle(scm_handle
);
131 /* Nonexistent service */
132 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
133 SetLastError(0xdeadbeef);
134 svc_handle
= OpenServiceA(scm_handle
, "deadbeef", GENERIC_READ
);
135 ok(!svc_handle
, "Expected failure\n");
136 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
137 CloseServiceHandle(scm_handle
);
139 /* Proper SCM handle but different access rights */
140 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
141 SetLastError(0xdeadbeef);
142 svc_handle
= OpenServiceA(scm_handle
, spooler
, GENERIC_WRITE
);
143 if (!svc_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
144 skip("Not enough rights to get a handle to the service\n");
147 ok(svc_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
148 CloseServiceHandle(svc_handle
);
151 /* Test to show we can't open a service with the displayname */
153 /* Retrieve the needed size for the buffer */
155 GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
156 /* Get the displayname */
157 GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
158 /* Try to open the service with this displayname, unless the displayname equals
159 * the servicename as that would defeat the purpose of this test.
161 if (!lstrcmpiA(spooler
, displayname
))
163 skip("displayname equals servicename\n");
164 CloseServiceHandle(scm_handle
);
168 SetLastError(0xdeadbeef);
169 svc_handle
= OpenServiceA(scm_handle
, displayname
, GENERIC_READ
);
170 ok(!svc_handle
, "Expected failure\n");
171 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
173 CloseServiceHandle(svc_handle
);
175 CloseServiceHandle(scm_handle
);
178 static void test_create_delete_svc(void)
180 SC_HANDLE scm_handle
, svc_handle1
, svc_handle2
;
181 CHAR username
[UNLEN
+ 1], domain
[MAX_PATH
];
182 DWORD user_size
= UNLEN
+ 1;
183 CHAR account
[UNLEN
+ 3];
184 static const CHAR servicename
[] = "Winetest";
185 static const CHAR pathname
[] = "we_dont_care.exe";
186 static const CHAR empty
[] = "";
187 static const CHAR password
[] = "secret";
188 BOOL spooler_exists
= FALSE
;
191 DWORD display_size
= sizeof(display
);
193 /* Get the username and turn it into an account to be used in some tests */
194 GetUserNameA(username
, &user_size
);
195 /* Get the domainname to cater for that situation */
196 if (GetEnvironmentVariableA("USERDOMAIN", domain
, MAX_PATH
))
197 sprintf(account
, "%s\\%s", domain
, username
);
199 sprintf(account
, ".\\%s", username
);
202 SetLastError(0xdeadbeef);
203 svc_handle1
= CreateServiceA(NULL
, NULL
, NULL
, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
204 ok(!svc_handle1
, "Expected failure\n");
205 ok(GetLastError() == ERROR_INVALID_HANDLE
, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
207 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
209 /* Only a valid handle to the Service Control Manager */
210 SetLastError(0xdeadbeef);
211 svc_handle1
= CreateServiceA(scm_handle
, NULL
, NULL
, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
212 ok(!svc_handle1
, "Expected failure\n");
213 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, W2K3, XP, Vista */ ||
214 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
215 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
217 /* Now with a servicename */
218 SetLastError(0xdeadbeef);
219 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
220 ok(!svc_handle1
, "Expected failure\n");
221 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, W2K3, XP, Vista */ ||
222 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
223 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
225 /* Or just a binary name */
226 SetLastError(0xdeadbeef);
227 svc_handle1
= CreateServiceA(scm_handle
, NULL
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
228 ok(!svc_handle1
, "Expected failure\n");
229 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, W2K3, XP, Vista */ ||
230 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
231 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
233 /* Both servicename and binary name (We only have connect rights) */
234 SetLastError(0xdeadbeef);
235 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
236 ok(!svc_handle1
, "Expected failure\n");
237 ok(GetLastError() == ERROR_ACCESS_DENIED
, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
239 /* They can even be empty at this stage of parameter checking */
240 SetLastError(0xdeadbeef);
241 svc_handle1
= CreateServiceA(scm_handle
, empty
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
242 ok(!svc_handle1
, "Expected failure\n");
243 ok(GetLastError() == ERROR_ACCESS_DENIED
, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
245 SetLastError(0xdeadbeef);
246 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
247 ok(!svc_handle1
, "Expected failure\n");
248 ok(GetLastError() == ERROR_ACCESS_DENIED
, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
250 /* Open the Service Control Manager with minimal rights for creation
251 * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
253 CloseServiceHandle(scm_handle
);
254 SetLastError(0xdeadbeef);
255 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
);
256 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
258 skip("Not enough rights to get a handle to the manager\n");
262 /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
264 /* Empty strings for servicename and binary name are checked */
265 SetLastError(0xdeadbeef);
266 svc_handle1
= CreateServiceA(scm_handle
, empty
, NULL
, 0, 0, 0, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
267 ok(!svc_handle1
, "Expected failure\n");
268 ok(GetLastError() == ERROR_INVALID_NAME
, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
270 SetLastError(0xdeadbeef);
271 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, 0, 0, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
272 ok(!svc_handle1
, "Expected failure\n");
273 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
275 SetLastError(0xdeadbeef);
276 svc_handle1
= CreateServiceA(scm_handle
, empty
, NULL
, 0, 0, 0, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
277 ok(!svc_handle1
, "Expected failure\n");
278 ok(GetLastError() == ERROR_INVALID_NAME
, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
280 /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
281 * an ERROR_INVALID_PARAMETER)
283 SetLastError(0xdeadbeef);
284 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
,
285 SERVICE_DISABLED
, 0, empty
, NULL
, NULL
, NULL
, NULL
, NULL
);
286 ok(!svc_handle1
, "Expected failure\n");
287 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
289 /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
291 /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
292 * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
294 SetLastError(0xdeadbeef);
295 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_WIN32_OWN_PROCESS
| SERVICE_WIN32_SHARE_PROCESS
,
296 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
297 ok(!svc_handle1
, "Expected failure\n");
298 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
300 /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
301 SetLastError(0xdeadbeef);
302 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_FILE_SYSTEM_DRIVER
| SERVICE_INTERACTIVE_PROCESS
,
303 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
304 ok(!svc_handle1
, "Expected failure\n");
305 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
307 /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
308 * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
310 SetLastError(0xdeadbeef);
311 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
312 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, account
, password
);
313 ok(!svc_handle1
, "Expected failure\n");
314 ok(GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_INVALID_SERVICE_ACCOUNT
,
315 "Expected ERROR_INVALID_PARAMETER or ERROR_INVALID_SERVICE_ACCOUNT, got %d\n", GetLastError());
317 /* Illegal (start-type is not a mask and should only be one of the possibilities)
318 * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
319 * it's most likely not the wanted start-type)
321 SetLastError(0xdeadbeef);
322 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
, SERVICE_WIN32_OWN_PROCESS
,
323 SERVICE_AUTO_START
| SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
324 ok(!svc_handle1
, "Expected failure\n");
325 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
327 /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
328 SetLastError(0xdeadbeef);
329 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
,
330 SERVICE_BOOT_START
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
331 ok(!svc_handle1
, "Expected failure\n");
332 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
334 /* Test if ServiceType can be a combined one for drivers */
335 SetLastError(0xdeadbeef);
336 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, SERVICE_KERNEL_DRIVER
| SERVICE_FILE_SYSTEM_DRIVER
,
337 SERVICE_BOOT_START
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
338 ok(!svc_handle1
, "Expected failure\n");
339 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
341 /* The service already exists (check first, just in case) */
342 svc_handle1
= OpenServiceA(scm_handle
, spooler
, GENERIC_READ
);
345 spooler_exists
= TRUE
;
346 CloseServiceHandle(svc_handle1
);
347 SetLastError(0xdeadbeef);
348 svc_handle1
= CreateServiceA(scm_handle
, spooler
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
,
349 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
350 ok(!svc_handle1
, "Expected failure\n");
351 ok(GetLastError() == ERROR_SERVICE_EXISTS
, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
354 skip("Spooler service doesn't exist\n");
356 /* To find an existing displayname we check the 'Spooler' service. Although the registry
357 * doesn't show DisplayName on NT4, this call will return a displayname which is equal
358 * to the servicename and can't be used as well for a new displayname.
362 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, display
, &display_size
);
365 skip("Could not retrieve a displayname for the Spooler service\n");
368 svc_handle1
= CreateServiceA(scm_handle
, servicename
, display
, 0, SERVICE_WIN32_OWN_PROCESS
,
369 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
370 ok(!svc_handle1
, "Expected failure for display name '%s'\n", display
);
371 ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME
,
372 "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
376 skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
378 /* Windows doesn't care about the access rights for creation (which makes
379 * sense as there is no service yet) as long as there are sufficient
380 * rights to the manager.
382 SetLastError(0xdeadbeef);
383 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, 0, SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
384 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
385 ok(svc_handle1
!= NULL
, "Could not create the service : %d\n", GetLastError());
387 /* DeleteService however must have proper rights */
388 SetLastError(0xdeadbeef);
389 ret
= DeleteService(svc_handle1
);
390 ok(!ret
, "Expected failure\n");
391 ok(GetLastError() == ERROR_ACCESS_DENIED
,
392 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
394 /* Open the service with minimal rights for deletion.
395 * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
397 CloseServiceHandle(svc_handle1
);
398 svc_handle1
= OpenServiceA(scm_handle
, servicename
, DELETE
);
400 /* Now that we have the proper rights, we should be able to delete */
401 SetLastError(0xdeadbeef);
402 ret
= DeleteService(svc_handle1
);
403 ok(ret
, "Expected success, got error %u\n", GetLastError());
405 /* Service is marked for delete, but handle is still open. Try to open service again. */
406 svc_handle2
= OpenServiceA(scm_handle
, servicename
, GENERIC_READ
);
407 ok(svc_handle2
!= NULL
, "got %p, error %u\n", svc_handle2
, GetLastError());
408 CloseServiceHandle(svc_handle2
);
410 CloseServiceHandle(svc_handle1
);
411 CloseServiceHandle(scm_handle
);
413 /* Wait a while. One of the following tests also does a CreateService for the
414 * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
415 * error if we do this too quickly. Vista seems more picky than the others.
419 /* And a final NULL check */
420 SetLastError(0xdeadbeef);
421 ret
= DeleteService(NULL
);
422 ok(!ret
, "Expected failure\n");
423 ok(GetLastError() == ERROR_INVALID_HANDLE
,
424 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
427 static void test_get_displayname(void)
429 SC_HANDLE scm_handle
, svc_handle
;
431 CHAR displayname
[4096];
432 WCHAR displaynameW
[2048];
433 DWORD displaysize
, tempsize
, tempsizeW
;
434 static const CHAR deadbeef
[] = "Deadbeef";
435 static const WCHAR spoolerW
[] = {'S','p','o','o','l','e','r',0};
436 static const WCHAR deadbeefW
[] = {'D','e','a','d','b','e','e','f',0};
437 static const WCHAR abcW
[] = {'A','B','C',0};
438 static const CHAR servicename
[] = "Winetest";
439 static const CHAR pathname
[] = "we_dont_care.exe";
441 /* Having NULL for the size of the buffer will crash on W2K3 */
443 SetLastError(0xdeadbeef);
444 ret
= GetServiceDisplayNameA(NULL
, NULL
, NULL
, &displaysize
);
445 ok(!ret
, "Expected failure\n");
446 ok(GetLastError() == ERROR_INVALID_HANDLE
,
447 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
449 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
451 SetLastError(0xdeadbeef);
452 ret
= GetServiceDisplayNameA(scm_handle
, NULL
, NULL
, &displaysize
);
453 ok(!ret
, "Expected failure\n");
454 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
455 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
456 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
458 SetLastError(0xdeadbeef);
459 displaysize
= sizeof(displayname
);
460 ret
= GetServiceDisplayNameA(scm_handle
, NULL
, displayname
, &displaysize
);
461 ok(!ret
, "Expected failure\n");
462 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
463 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
464 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
466 /* Test for nonexistent service */
467 SetLastError(0xdeadbeef);
469 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, NULL
, &displaysize
);
470 ok(!ret
, "Expected failure\n");
471 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
472 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
474 SetLastError(0xdeadbeef);
475 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, NULL
, &displaysize
);
476 ok(!ret
, "Expected failure\n");
477 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
478 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
479 todo_wine
ok(displaysize
== 1, "Service size expected 1, got %d\n", displaysize
);
482 strcpy(displayname
, "ABC");
483 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
484 ok(!ret
, "Expected failure\n");
485 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
486 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
487 todo_wine
ok(displaysize
== 15, "Service size expected 15, got %d\n", displaysize
);
488 ok(displayname
[0] == 0, "Service name not empty\n");
491 lstrcpyW( displaynameW
, abcW
);
492 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
493 ok(!ret
, "Expected failure\n");
494 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
495 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
496 ok(displaysize
== 15, "Service size expected 15, got %d\n", displaysize
);
497 ok(displaynameW
[0] == 0, "Service name not empty\n");
500 strcpy(displayname
, "ABC");
501 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
502 ok(!ret
, "Expected failure\n");
503 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
504 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
505 todo_wine
ok(displaysize
== 1, "Service size expected 1, got %d\n", displaysize
);
506 ok(displayname
[0] == 'A', "Service name changed\n");
509 lstrcpyW( displaynameW
, abcW
);
510 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
511 ok(!ret
, "Expected failure\n");
512 ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
513 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
514 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
515 ok(displaynameW
[0] == 'A', "Service name changed\n");
518 strcpy(displayname
, "ABC");
519 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
520 ok(!ret
, "Expected failure\n");
521 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
522 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
523 todo_wine
ok(displaysize
== 1, "Service size expected 1, got %d\n", displaysize
);
524 ok(displayname
[0] == 0, "Service name not empty\n");
527 lstrcpyW( displaynameW
, abcW
);
528 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
529 ok(!ret
, "Expected failure\n");
530 ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
531 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
532 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
533 ok(displaynameW
[0] == 'A', "Service name changed\n");
536 strcpy(displayname
, "ABC");
537 ret
= GetServiceDisplayNameA(scm_handle
, deadbeef
, displayname
, &displaysize
);
538 ok(!ret
, "Expected failure\n");
539 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
540 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
541 todo_wine
ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
542 ok(displayname
[0] == 0, "Service name not empty\n");
545 lstrcpyW( displaynameW
, abcW
);
546 ret
= GetServiceDisplayNameW(scm_handle
, deadbeefW
, displaynameW
, &displaysize
);
547 ok(!ret
, "Expected failure\n");
548 ok(displaysize
== 2, "Service size expected 2, got %d\n", displaysize
);
549 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
550 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
551 ok(displaynameW
[0] == 0, "Service name not empty\n");
553 /* Check if 'Spooler' exists */
554 svc_handle
= OpenServiceA(scm_handle
, spooler
, GENERIC_READ
);
557 skip("Spooler service doesn't exist\n");
558 CloseServiceHandle(scm_handle
);
561 CloseServiceHandle(svc_handle
);
563 /* Retrieve the needed size for the buffer */
564 SetLastError(0xdeadbeef);
566 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
567 ok(!ret
, "Expected failure\n");
568 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
569 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
570 tempsize
= displaysize
;
573 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
574 ok(!ret
, "Expected failure\n");
575 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
576 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
577 ok(displaysize
== tempsize
, "Buffer size mismatch (%d vs %d)\n", tempsize
, displaysize
);
579 /* Buffer is too small */
580 SetLastError(0xdeadbeef);
581 displaysize
= (tempsize
/ 2);
582 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
583 ok(!ret
, "Expected failure\n");
584 ok(displaysize
== tempsize
, "Expected the needed buffersize\n");
585 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
586 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
588 /* First try with a buffer that should be big enough to hold
589 * the ANSI string (and terminating character). This succeeds on Windows
590 * although when asked (see above 2 tests) it will return twice the needed size.
592 SetLastError(0xdeadbeef);
593 displaysize
= (tempsize
/ 2) + 1;
594 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
595 ok(ret
, "Expected success, got error %u\n", GetLastError());
596 ok(displaysize
== ((tempsize
/ 2) + 1), "Expected no change for the needed buffer size\n");
598 /* Now with the original returned size */
599 SetLastError(0xdeadbeef);
600 displaysize
= tempsize
;
601 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
602 ok(ret
, "Expected success, got error %u\n", GetLastError());
603 ok(displaysize
== tempsize
, "Expected no change for the needed buffer size\n");
605 /* And with a bigger than needed buffer */
606 SetLastError(0xdeadbeef);
607 displaysize
= tempsize
* 2;
608 ret
= GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
609 ok(ret
, "Expected success, got error %u\n", GetLastError());
610 /* Test that shows that if the buffersize is enough, it's not changed */
611 ok(displaysize
== tempsize
* 2, "Expected no change for the needed buffer size\n");
612 ok(strlen(displayname
) == tempsize
/2,
613 "Expected the buffer to be twice the length of the string\n") ;
615 /* Do the buffer(size) tests also for GetServiceDisplayNameW */
616 SetLastError(0xdeadbeef);
618 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, NULL
, &displaysize
);
619 ok(!ret
, "Expected failure\n");
620 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
621 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
623 /* Buffer is too small */
624 SetLastError(0xdeadbeef);
625 tempsizeW
= displaysize
;
626 displaysize
= tempsizeW
/ 2;
627 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, displaynameW
, &displaysize
);
628 ok(!ret
, "Expected failure\n");
629 ok(displaysize
== tempsizeW
, "Expected the needed buffersize\n");
630 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
631 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
633 /* Now with the original returned size */
634 SetLastError(0xdeadbeef);
635 displaysize
= tempsizeW
;
636 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, displaynameW
, &displaysize
);
637 ok(!ret
, "Expected failure\n");
638 ok(displaysize
== tempsizeW
, "Expected the needed buffersize\n");
639 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
640 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
642 /* And with a bigger than needed buffer */
643 SetLastError(0xdeadbeef);
644 displaysize
= tempsizeW
+ 1; /* This caters for the null terminating character */
645 ret
= GetServiceDisplayNameW(scm_handle
, spoolerW
, displaynameW
, &displaysize
);
646 ok(ret
, "Expected success, got error %u\n", GetLastError());
647 ok(displaysize
== tempsizeW
, "Expected the needed buffersize\n");
648 ok(lstrlenW(displaynameW
) == displaysize
,
649 "Expected the buffer to be the length of the string\n") ;
650 ok(tempsize
/ 2 == tempsizeW
,
651 "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
653 CloseServiceHandle(scm_handle
);
655 /* Test for a service without a displayname (which is valid). This should return
656 * the servicename itself.
658 SetLastError(0xdeadbeef);
659 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
);
660 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
662 skip("Not enough rights to get a handle to the manager\n");
666 SetLastError(0xdeadbeef);
667 svc_handle
= CreateServiceA(scm_handle
, servicename
, NULL
, DELETE
,
668 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
669 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
670 ok(svc_handle
!= NULL
, "Could not create the service : %d\n", GetLastError());
673 CloseServiceHandle(scm_handle
);
677 /* Retrieve the needed size for the buffer */
678 SetLastError(0xdeadbeef);
680 ret
= GetServiceDisplayNameA(scm_handle
, servicename
, NULL
, &displaysize
);
681 ok(!ret
, "Expected failure\n");
682 ok(displaysize
== strlen(servicename
) * 2,
683 "Expected the displaysize to be twice the size of the servicename\n");
684 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
685 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
687 /* Buffer is too small */
688 SetLastError(0xdeadbeef);
689 tempsize
= displaysize
;
690 displaysize
= (tempsize
/ 2);
691 ret
= GetServiceDisplayNameA(scm_handle
, servicename
, displayname
, &displaysize
);
692 ok(!ret
, "Expected failure\n");
693 ok(displaysize
== tempsize
, "Expected the needed buffersize\n");
694 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
695 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
697 /* Get the displayname */
698 SetLastError(0xdeadbeef);
699 ret
= GetServiceDisplayNameA(scm_handle
, servicename
, displayname
, &displaysize
);
700 ok(ret
, "Expected success, got error %u\n", GetLastError());
701 ok(!lstrcmpiA(displayname
, servicename
),
702 "Expected displayname to be %s, got %s\n", servicename
, displayname
);
704 /* Delete the service */
705 ret
= DeleteService(svc_handle
);
706 ok(ret
, "Expected success (err=%d)\n", GetLastError());
708 CloseServiceHandle(svc_handle
);
709 CloseServiceHandle(scm_handle
);
711 /* Wait a while. Just in case one of the following tests does a CreateService again */
715 static void test_get_servicekeyname(void)
717 SC_HANDLE scm_handle
, svc_handle
;
718 CHAR servicename
[4096];
719 CHAR displayname
[4096];
720 WCHAR servicenameW
[4096];
721 WCHAR displaynameW
[4096];
722 DWORD servicesize
, displaysize
, tempsize
;
724 static const CHAR deadbeef
[] = "Deadbeef";
725 static const WCHAR deadbeefW
[] = {'D','e','a','d','b','e','e','f',0};
726 static const WCHAR abcW
[] = {'A','B','C',0};
728 /* Having NULL for the size of the buffer will crash on W2K3 */
730 SetLastError(0xdeadbeef);
731 ret
= GetServiceKeyNameA(NULL
, NULL
, NULL
, &servicesize
);
732 ok(!ret
, "Expected failure\n");
733 ok(GetLastError() == ERROR_INVALID_HANDLE
,
734 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
736 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
739 SetLastError(0xdeadbeef);
740 ret
= GetServiceKeyNameA(scm_handle
, NULL
, NULL
, &servicesize
);
741 ok(!ret
, "Expected failure\n");
742 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
743 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
744 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
745 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
747 /* Valid handle and buffer but no displayname */
749 SetLastError(0xdeadbeef);
750 ret
= GetServiceKeyNameA(scm_handle
, NULL
, servicename
, &servicesize
);
751 ok(!ret
, "Expected failure\n");
752 ok(GetLastError() == ERROR_INVALID_ADDRESS
/* W2K, XP, W2K3, Vista */ ||
753 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
754 "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
755 todo_wine
ok(servicesize
== 200, "Service size expected 1, got %d\n", servicesize
);
757 /* Test for nonexistent displayname */
758 SetLastError(0xdeadbeef);
759 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, NULL
, &servicesize
);
760 ok(!ret
, "Expected failure\n");
761 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
762 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
763 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
766 strcpy(servicename
, "ABC");
767 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
768 ok(!ret
, "Expected failure\n");
769 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
770 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
771 todo_wine
ok(servicesize
== 15, "Service size expected 15, got %d\n", servicesize
);
772 ok(servicename
[0] == 0, "Service name not empty\n");
775 lstrcpyW( servicenameW
, abcW
);
776 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
777 ok(!ret
, "Expected failure\n");
778 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
779 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
780 ok(servicesize
== 15, "Service size expected 15, got %d\n", servicesize
);
781 ok(servicenameW
[0] == 0, "Service name not empty\n");
784 strcpy(servicename
, "ABC");
785 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
786 ok(!ret
, "Expected failure\n");
787 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
788 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
789 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
790 ok(servicename
[0] == 'A', "Service name changed\n");
793 lstrcpyW( servicenameW
, abcW
);
794 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
795 ok(!ret
, "Expected failure\n");
796 ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
797 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
798 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
799 ok(servicenameW
[0] == 'A', "Service name changed\n");
802 strcpy(servicename
, "ABC");
803 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
804 ok(!ret
, "Expected failure\n");
805 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
806 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
807 todo_wine
ok(servicesize
== 1, "Service size expected 1, got %d\n", servicesize
);
808 ok(servicename
[0] == 0, "Service name not empty\n");
811 lstrcpyW( servicenameW
, abcW
);
812 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
813 ok(!ret
, "Expected failure\n");
814 ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
815 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
816 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
817 ok(servicenameW
[0] == 'A', "Service name changed\n");
820 strcpy(servicename
, "ABC");
821 ret
= GetServiceKeyNameA(scm_handle
, deadbeef
, servicename
, &servicesize
);
822 ok(!ret
, "Expected failure\n");
823 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
824 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
825 todo_wine
ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
826 ok(servicename
[0] == 0, "Service name not empty\n");
829 lstrcpyW( servicenameW
, abcW
);
830 ret
= GetServiceKeyNameW(scm_handle
, deadbeefW
, servicenameW
, &servicesize
);
831 ok(!ret
, "Expected failure\n");
832 ok(servicesize
== 2, "Service size expected 2, got %d\n", servicesize
);
833 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
834 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
835 ok(servicenameW
[0] == 0, "Service name not empty\n");
837 /* Check if 'Spooler' exists */
838 svc_handle
= OpenServiceA(scm_handle
, spooler
, GENERIC_READ
);
841 skip("Spooler service doesn't exist\n");
842 CloseServiceHandle(scm_handle
);
845 CloseServiceHandle(svc_handle
);
847 /* Get the displayname for the 'Spooler' service */
848 GetServiceDisplayNameA(scm_handle
, spooler
, NULL
, &displaysize
);
849 GetServiceDisplayNameA(scm_handle
, spooler
, displayname
, &displaysize
);
851 /* Retrieve the needed size for the buffer */
852 SetLastError(0xdeadbeef);
854 ret
= GetServiceKeyNameA(scm_handle
, displayname
, NULL
, &servicesize
);
855 ok(!ret
, "Expected failure\n");
856 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
857 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
859 /* Valid call with the correct buffersize */
860 SetLastError(0xdeadbeef);
861 tempsize
= servicesize
;
863 ret
= GetServiceKeyNameA(scm_handle
, displayname
, servicename
, &servicesize
);
864 ok(ret
, "Expected success, got error %u\n", GetLastError());
867 ok(strlen(servicename
) == tempsize
/2,
868 "Expected the buffer to be twice the length of the string\n") ;
869 ok(!lstrcmpiA(servicename
, spooler
), "Expected %s, got %s\n", spooler
, servicename
);
870 ok(servicesize
== (tempsize
* 2),
871 "Expected servicesize not to change if buffer not insufficient\n") ;
874 MultiByteToWideChar(CP_ACP
, 0, displayname
, -1, displaynameW
, sizeof(displaynameW
)/2);
875 SetLastError(0xdeadbeef);
877 ret
= GetServiceKeyNameW(scm_handle
, displaynameW
, servicenameW
, &servicesize
);
878 ok(ret
, "Expected success, got error %u\n", GetLastError());
881 ok(strlen(servicename
) == tempsize
/2,
882 "Expected the buffer to be twice the length of the string\n") ;
883 ok(servicesize
== lstrlenW(servicenameW
),
884 "Expected servicesize not to change if buffer not insufficient\n") ;
887 SetLastError(0xdeadbeef);
889 ret
= GetServiceKeyNameW(scm_handle
, displaynameW
, servicenameW
, &servicesize
);
890 ok(!ret
, "Expected failure\n");
891 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
892 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
893 ok(servicenameW
[0] == 0, "Buffer not empty\n");
895 CloseServiceHandle(scm_handle
);
898 static void test_query_svc(void)
900 SC_HANDLE scm_handle
, svc_handle
;
902 SERVICE_STATUS status
;
903 SERVICE_STATUS_PROCESS
*statusproc
;
904 DWORD bufsize
, needed
;
906 /* All NULL or wrong */
907 SetLastError(0xdeadbeef);
908 ret
= QueryServiceStatus(NULL
, NULL
);
909 ok(!ret
, "Expected failure\n");
910 ok(GetLastError() == ERROR_INVALID_HANDLE
,
911 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
913 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
915 /* Check if 'Spooler' exists.
916 * Open with not enough rights to query the status.
918 svc_handle
= OpenServiceA(scm_handle
, spooler
, STANDARD_RIGHTS_READ
);
921 skip("Spooler service doesn't exist\n");
922 CloseServiceHandle(scm_handle
);
926 SetLastError(0xdeadbeef);
927 ret
= QueryServiceStatus(svc_handle
, NULL
);
928 ok(!ret
, "Expected failure\n");
929 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
930 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
931 "Unexpected last error %d\n", GetLastError());
933 SetLastError(0xdeadbeef);
934 ret
= QueryServiceStatus(svc_handle
, &status
);
935 ok(!ret
, "Expected failure\n");
936 ok(GetLastError() == ERROR_ACCESS_DENIED
,
937 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
939 /* Open the service with just enough rights.
940 * (Verified with 'SERVICE_ALL_ACCESS &~ SERVICE_QUERY_STATUS')
942 CloseServiceHandle(svc_handle
);
943 svc_handle
= OpenServiceA(scm_handle
, spooler
, SERVICE_QUERY_STATUS
);
945 SetLastError(0xdeadbeef);
946 ret
= QueryServiceStatus(svc_handle
, &status
);
947 ok(ret
, "Expected success, got error %u\n", GetLastError());
949 CloseServiceHandle(svc_handle
);
951 /* More or less the same tests for QueryServiceStatusEx */
952 if (!pQueryServiceStatusEx
)
954 win_skip( "QueryServiceStatusEx not available\n" );
955 CloseServiceHandle(scm_handle
);
959 /* Open service with not enough rights to query the status */
960 svc_handle
= OpenServiceA(scm_handle
, spooler
, STANDARD_RIGHTS_READ
);
962 /* All NULL or wrong, this proves that info level is checked first */
963 SetLastError(0xdeadbeef);
964 ret
= pQueryServiceStatusEx(NULL
, 1, NULL
, 0, NULL
);
965 ok(!ret
, "Expected failure\n");
966 ok(GetLastError() == ERROR_INVALID_LEVEL
,
967 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
969 /* Passing a NULL parameter for the needed buffer size
970 * will crash on anything but NT4.
973 /* Only info level is correct. It looks like the buffer/size is checked second */
974 SetLastError(0xdeadbeef);
975 ret
= pQueryServiceStatusEx(NULL
, SC_STATUS_PROCESS_INFO
, NULL
, 0, &needed
);
976 /* NT4 checks the handle first */
977 if (GetLastError() != ERROR_INVALID_HANDLE
)
979 ok(!ret
, "Expected failure\n");
980 ok(needed
== sizeof(SERVICE_STATUS_PROCESS
),
981 "Needed buffersize is wrong : %d\n", needed
);
982 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
983 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
986 /* Pass a correct buffer and buffersize but a NULL handle */
987 statusproc
= HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS
));
989 SetLastError(0xdeadbeef);
990 ret
= pQueryServiceStatusEx(NULL
, SC_STATUS_PROCESS_INFO
, (BYTE
*)statusproc
, bufsize
, &needed
);
991 ok(!ret
, "Expected failure\n");
992 ok(GetLastError() == ERROR_INVALID_HANDLE
,
993 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
994 HeapFree(GetProcessHeap(), 0, statusproc
);
996 /* Correct handle and info level */
997 SetLastError(0xdeadbeef);
998 ret
= pQueryServiceStatusEx(svc_handle
, SC_STATUS_PROCESS_INFO
, NULL
, 0, &needed
);
999 /* NT4 doesn't return the needed size */
1000 if (GetLastError() != ERROR_INVALID_PARAMETER
)
1002 ok(!ret
, "Expected failure\n");
1003 ok(needed
== sizeof(SERVICE_STATUS_PROCESS
),
1004 "Needed buffersize is wrong : %d\n", needed
);
1005 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
1006 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1009 /* All parameters are OK but we don't have enough rights */
1010 statusproc
= HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS
));
1011 bufsize
= sizeof(SERVICE_STATUS_PROCESS
);
1012 SetLastError(0xdeadbeef);
1013 ret
= pQueryServiceStatusEx(svc_handle
, SC_STATUS_PROCESS_INFO
, (BYTE
*)statusproc
, bufsize
, &needed
);
1014 ok(!ret
, "Expected failure\n");
1015 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1016 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1017 HeapFree(GetProcessHeap(), 0, statusproc
);
1019 /* Open the service with just enough rights. */
1020 CloseServiceHandle(svc_handle
);
1021 svc_handle
= OpenServiceA(scm_handle
, spooler
, SERVICE_QUERY_STATUS
);
1023 /* Everything should be fine now. */
1024 statusproc
= HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS
));
1025 bufsize
= sizeof(SERVICE_STATUS_PROCESS
);
1026 SetLastError(0xdeadbeef);
1027 ret
= pQueryServiceStatusEx(svc_handle
, SC_STATUS_PROCESS_INFO
, (BYTE
*)statusproc
, bufsize
, &needed
);
1028 ok(ret
, "Expected success, got error %u\n", GetLastError());
1029 if (statusproc
->dwCurrentState
== SERVICE_RUNNING
)
1030 ok(statusproc
->dwProcessId
!= 0,
1031 "Expect a process id for this running service\n");
1033 ok(statusproc
->dwProcessId
== 0,
1034 "Expect no process id for this stopped service\n");
1036 /* same call with null needed pointer */
1037 SetLastError(0xdeadbeef);
1038 ret
= pQueryServiceStatusEx(svc_handle
, SC_STATUS_PROCESS_INFO
, (BYTE
*)statusproc
, bufsize
, NULL
);
1039 ok(!ret
, "Expected failure\n");
1040 ok(broken(GetLastError() == ERROR_INVALID_PARAMETER
) /* NT4 */ ||
1041 GetLastError() == ERROR_INVALID_ADDRESS
, "got %d\n", GetLastError());
1043 HeapFree(GetProcessHeap(), 0, statusproc
);
1045 CloseServiceHandle(svc_handle
);
1046 CloseServiceHandle(scm_handle
);
1049 static void test_enum_svc(void)
1051 SC_HANDLE scm_handle
;
1053 DWORD bufsize
, needed
, returned
, resume
;
1054 DWORD neededW
, returnedW
;
1055 DWORD tempneeded
, tempreturned
, missing
;
1056 DWORD servicecountactive
, servicecountinactive
;
1057 ENUM_SERVICE_STATUSA
*services
;
1058 ENUM_SERVICE_STATUSW
*servicesW
;
1059 ENUM_SERVICE_STATUS_PROCESSA
*exservices
;
1062 /* All NULL or wrong */
1063 SetLastError(0xdeadbeef);
1064 ret
= EnumServicesStatusA(NULL
, 1, 0, NULL
, 0, NULL
, NULL
, NULL
);
1065 ok(!ret
, "Expected failure\n");
1066 ok(GetLastError() == ERROR_INVALID_HANDLE
,
1067 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1069 SetLastError(0xdeadbeef);
1070 ret
= EnumServicesStatusW(NULL
, 1, 0, NULL
, 0, NULL
, NULL
, NULL
);
1071 ok(!ret
, "Expected failure\n");
1072 ok(GetLastError() == ERROR_INVALID_HANDLE
,
1073 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1075 /* Open the service control manager with not enough rights at first */
1076 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
1078 /* Valid handle but rest is still NULL or wrong */
1079 SetLastError(0xdeadbeef);
1080 ret
= EnumServicesStatusA(scm_handle
, 1, 0, NULL
, 0, NULL
, NULL
, NULL
);
1081 ok(!ret
, "Expected failure\n");
1082 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1083 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1084 "Unexpected last error %d\n", GetLastError());
1086 SetLastError(0xdeadbeef);
1087 ret
= EnumServicesStatusW(scm_handle
, 1, 0, NULL
, 0, NULL
, NULL
, NULL
);
1088 ok(!ret
, "Expected failure\n");
1089 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1090 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1091 "Unexpected last error %d\n", GetLastError());
1093 /* Don't specify the two required pointers */
1094 returned
= 0xdeadbeef;
1095 SetLastError(0xdeadbeef);
1096 ret
= EnumServicesStatusA(scm_handle
, 0, 0, NULL
, 0, NULL
, &returned
, NULL
);
1097 ok(!ret
, "Expected failure\n");
1098 ok(returned
== 0xdeadbeef, "Expected no change to the number of services variable\n");
1099 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1100 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1101 "Unexpected last error %d\n", GetLastError());
1103 returned
= 0xdeadbeef;
1104 SetLastError(0xdeadbeef);
1105 ret
= EnumServicesStatusW(scm_handle
, 0, 0, NULL
, 0, NULL
, &returned
, NULL
);
1106 ok(!ret
, "Expected failure\n");
1107 ok(returned
== 0xdeadbeef, "Expected no change to the number of services variable\n");
1108 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1109 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1110 "Unexpected last error %d\n", GetLastError());
1112 /* Don't specify the two required pointers */
1113 needed
= 0xdeadbeef;
1114 SetLastError(0xdeadbeef);
1115 ret
= EnumServicesStatusA(scm_handle
, 0, 0, NULL
, 0, &needed
, NULL
, NULL
);
1116 ok(!ret
, "Expected failure\n");
1117 ok(needed
== 0xdeadbeef || broken(needed
!= 0xdeadbeef), /* nt4 */
1118 "Expected no change to the needed buffer variable\n");
1119 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1120 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1121 "Unexpected last error %d\n", GetLastError());
1123 needed
= 0xdeadbeef;
1124 SetLastError(0xdeadbeef);
1125 ret
= EnumServicesStatusW(scm_handle
, 0, 0, NULL
, 0, &needed
, NULL
, NULL
);
1126 ok(!ret
, "Expected failure\n");
1127 ok(needed
== 0xdeadbeef || broken(needed
!= 0xdeadbeef), /* nt4 */
1128 "Expected no change to the needed buffer variable\n");
1129 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1130 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1131 "Unexpected last error %d\n", GetLastError());
1133 /* No valid servicetype and servicestate */
1134 needed
= 0xdeadbeef;
1135 returned
= 0xdeadbeef;
1136 SetLastError(0xdeadbeef);
1137 ret
= EnumServicesStatusA(scm_handle
, 0, 0, NULL
, 0, &needed
, &returned
, NULL
);
1138 ok(!ret
, "Expected failure\n");
1139 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1140 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1141 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1142 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1143 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1145 needed
= 0xdeadbeef;
1146 returned
= 0xdeadbeef;
1147 SetLastError(0xdeadbeef);
1148 ret
= EnumServicesStatusW(scm_handle
, 0, 0, NULL
, 0, &needed
, &returned
, NULL
);
1149 ok(!ret
, "Expected failure\n");
1150 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1151 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1152 ok(returned
== 0 || broken(returned
!= 0), /* nt4 */
1153 "Expected number of services to be set to 0, got %d\n", returned
);
1154 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1155 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1157 /* No valid servicestate */
1158 needed
= 0xdeadbeef;
1159 returned
= 0xdeadbeef;
1160 SetLastError(0xdeadbeef);
1161 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, 0, NULL
, 0, &needed
, &returned
, NULL
);
1162 ok(!ret
, "Expected failure\n");
1163 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1164 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1165 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1166 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1167 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1169 needed
= 0xdeadbeef;
1170 returned
= 0xdeadbeef;
1171 SetLastError(0xdeadbeef);
1172 ret
= EnumServicesStatusW(scm_handle
, SERVICE_WIN32
, 0, NULL
, 0, &needed
, &returned
, NULL
);
1173 ok(!ret
, "Expected failure\n");
1174 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1175 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1176 ok(returned
== 0 || broken(returned
!= 0), /* nt4 */
1177 "Expected number of services to be set to 0, got %d\n", returned
);
1178 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1179 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1181 /* No valid servicetype */
1182 needed
= 0xdeadbeef;
1183 returned
= 0xdeadbeef;
1184 SetLastError(0xdeadbeef);
1185 ret
= EnumServicesStatusA(scm_handle
, 0, SERVICE_STATE_ALL
, NULL
, 0, &needed
, &returned
, NULL
);
1186 ok(!ret
, "Expected failure\n");
1187 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1188 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1189 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1190 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1191 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1193 needed
= 0xdeadbeef;
1194 returned
= 0xdeadbeef;
1195 SetLastError(0xdeadbeef);
1196 ret
= EnumServicesStatusW(scm_handle
, 0, SERVICE_STATE_ALL
, NULL
, 0, &needed
, &returned
, NULL
);
1197 ok(!ret
, "Expected failure\n");
1198 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1199 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1200 ok(returned
== 0 || broken(returned
!= 0), /* nt4 */
1201 "Expected number of services to be set to 0, got %d\n", returned
);
1202 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1203 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1205 /* All parameters are correct but our access rights are wrong */
1206 needed
= 0xdeadbeef;
1207 returned
= 0xdeadbeef;
1208 SetLastError(0xdeadbeef);
1209 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0, &needed
, &returned
, NULL
);
1210 ok(!ret
, "Expected failure\n");
1211 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1212 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1213 ok(returned
== 0, "Expected number of services to be set to 0, got %d\n", returned
);
1214 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1215 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1217 needed
= 0xdeadbeef;
1218 returned
= 0xdeadbeef;
1219 SetLastError(0xdeadbeef);
1220 ret
= EnumServicesStatusW(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0, &needed
, &returned
, NULL
);
1221 ok(!ret
, "Expected failure\n");
1222 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1223 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1224 ok(returned
== 0 || broken(returned
!= 0), /* nt4 */
1225 "Expected number of services to be set to 0, got %d\n", returned
);
1226 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1227 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1229 /* Open the service control manager with the needed rights */
1230 CloseServiceHandle(scm_handle
);
1231 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
1233 /* All parameters are correct. Request the needed buffer size */
1234 needed
= 0xdeadbeef;
1235 returned
= 0xdeadbeef;
1236 SetLastError(0xdeadbeef);
1237 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0, &needed
, &returned
, NULL
);
1238 ok(!ret
, "Expected failure\n");
1239 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size for this one service\n");
1240 ok(returned
== 0, "Expected no service returned, got %d\n", returned
);
1241 ok(GetLastError() == ERROR_MORE_DATA
,
1242 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1244 /* Test to show we get the same needed buffer size for the W-call */
1245 neededW
= 0xdeadbeef;
1246 returnedW
= 0xdeadbeef;
1247 SetLastError(0xdeadbeef);
1248 ret
= EnumServicesStatusW(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0, &neededW
, &returnedW
, NULL
);
1249 ok(!ret
, "Expected failure\n");
1250 ok(neededW
!= 0xdeadbeef && neededW
> 0, "Expected the needed buffer size for this one service\n");
1251 ok(neededW
== needed
, "Expected needed buffersize to be the same for A- and W-calls\n");
1252 ok(returnedW
== 0, "Expected no service returned, got %d\n", returnedW
);
1253 ok(GetLastError() == ERROR_MORE_DATA
,
1254 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1256 /* Store the needed bytes */
1257 tempneeded
= needed
;
1259 /* Allocate the correct needed bytes */
1260 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1262 needed
= 0xdeadbeef;
1263 returned
= 0xdeadbeef;
1264 SetLastError(0xdeadbeef);
1265 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1266 services
, bufsize
, &needed
, &returned
, NULL
);
1267 ok(ret
, "Expected success, got error %u\n", GetLastError());
1268 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1269 ok(returned
!= 0xdeadbeef && returned
> 0, "Expected some returned services\n");
1270 HeapFree(GetProcessHeap(), 0, services
);
1272 /* Store the number of returned services */
1273 tempreturned
= returned
;
1275 servicesW
= HeapAlloc(GetProcessHeap(), 0, neededW
);
1277 neededW
= 0xdeadbeef;
1278 returnedW
= 0xdeadbeef;
1279 SetLastError(0xdeadbeef);
1280 ret
= EnumServicesStatusW(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1281 servicesW
, bufsize
, &neededW
, &returnedW
, NULL
);
1282 ok(ret
, "Expected success, got error %u\n", GetLastError());
1283 ok(neededW
== 0, "Expected needed buffer to be 0 as we are done\n");
1284 ok(returnedW
!= 0xdeadbeef && returnedW
> 0, "Expected some returned services\n");
1285 HeapFree(GetProcessHeap(), 0, servicesW
);
1287 /* Allocate less than the needed bytes and don't specify a resume handle */
1288 services
= HeapAlloc(GetProcessHeap(), 0, tempneeded
);
1289 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUSA
);
1290 needed
= 0xdeadbeef;
1291 returned
= 0xdeadbeef;
1292 SetLastError(0xdeadbeef);
1293 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1294 services
, bufsize
, &needed
, &returned
, NULL
);
1295 ok(!ret
, "Expected failure\n");
1296 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size for this one service\n");
1297 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1298 ok(GetLastError() == ERROR_MORE_DATA
,
1299 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1301 /* Allocate less than the needed bytes, this time with a correct resume handle */
1302 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUSA
);
1303 needed
= 0xdeadbeef;
1304 returned
= 0xdeadbeef;
1306 SetLastError(0xdeadbeef);
1307 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1308 services
, bufsize
, &needed
, &returned
, &resume
);
1309 ok(!ret
, "Expected failure\n");
1310 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size for this one service\n");
1311 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1312 todo_wine
ok(resume
, "Expected a resume handle\n");
1313 ok(GetLastError() == ERROR_MORE_DATA
,
1314 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1316 /* Fetch the missing services but pass a bigger buffer size */
1317 missing
= tempreturned
- returned
;
1318 bufsize
= tempneeded
;
1319 needed
= 0xdeadbeef;
1320 returned
= 0xdeadbeef;
1321 SetLastError(0xdeadbeef);
1322 ret
= EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1323 services
, bufsize
, &needed
, &returned
, &resume
);
1324 ok(ret
, "Expected success, got error %u\n", GetLastError());
1325 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1326 ok(returned
== missing
, "Expected %u services to be returned\n", missing
);
1327 ok(resume
== 0, "Expected the resume handle to be 0\n");
1328 HeapFree(GetProcessHeap(), 0, services
);
1330 /* See if things add up */
1332 /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1333 * and doesn't count the others as inactive. This means that Vista could
1334 * show a total that is greater than the sum of active and inactive
1336 * The number of active and inactive drivers is greatly influenced by the
1337 * time when tests are run, immediately after boot or later for example.
1339 * Both reasons make calculations for drivers not so useful
1342 /* Get the number of active win32 services */
1343 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_ACTIVE
, NULL
, 0,
1344 &needed
, &returned
, NULL
);
1345 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1346 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_ACTIVE
, services
,
1347 needed
, &needed
, &returned
, NULL
);
1348 HeapFree(GetProcessHeap(), 0, services
);
1350 servicecountactive
= returned
;
1352 /* Get the number of inactive win32 services */
1353 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_INACTIVE
, NULL
, 0,
1354 &needed
, &returned
, NULL
);
1355 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1356 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_INACTIVE
, services
,
1357 needed
, &needed
, &returned
, NULL
);
1358 HeapFree(GetProcessHeap(), 0, services
);
1360 servicecountinactive
= returned
;
1362 /* Get the number of win32 services */
1363 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, NULL
, 0,
1364 &needed
, &returned
, NULL
);
1365 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1366 EnumServicesStatusA(scm_handle
, SERVICE_WIN32
, SERVICE_STATE_ALL
, services
,
1367 needed
, &needed
, &returned
, NULL
);
1368 HeapFree(GetProcessHeap(), 0, services
);
1370 /* Check if total is the same as active and inactive win32 services */
1371 ok(returned
== (servicecountactive
+ servicecountinactive
),
1372 "Something wrong in the calculation\n");
1374 /* Get all drivers and services
1376 * Fetch the status of the last call as failing could make the following tests crash
1377 * on Wine (we don't return anything yet).
1379 EnumServicesStatusA(scm_handle
, SERVICE_DRIVER
| SERVICE_WIN32
, SERVICE_STATE_ALL
,
1380 NULL
, 0, &needed
, &returned
, NULL
);
1381 services
= HeapAlloc(GetProcessHeap(), 0, needed
);
1382 ret
= EnumServicesStatusA(scm_handle
, SERVICE_DRIVER
| SERVICE_WIN32
, SERVICE_STATE_ALL
,
1383 services
, needed
, &needed
, &returned
, NULL
);
1385 /* Loop through all those returned drivers and services */
1386 for (i
= 0; ret
&& i
< returned
; i
++)
1388 SERVICE_STATUS status
= services
[i
].ServiceStatus
;
1390 /* lpServiceName and lpDisplayName should always be filled */
1391 ok(services
[i
].lpServiceName
[0], "Expected a service name\n");
1392 ok(services
[i
].lpDisplayName
&& services
[i
].lpDisplayName
[0], "Expected a display name\n");
1394 /* Decrement the counters to see if the functions calls return the same
1395 * numbers as the contents of these structures.
1397 if (status
.dwServiceType
& (SERVICE_WIN32_OWN_PROCESS
| SERVICE_WIN32_SHARE_PROCESS
))
1399 if (status
.dwCurrentState
== SERVICE_STOPPED
)
1400 servicecountinactive
--;
1402 servicecountactive
--;
1405 HeapFree(GetProcessHeap(), 0, services
);
1407 ok(servicecountactive
== 0, "Active services mismatch %u\n", servicecountactive
);
1408 ok(servicecountinactive
== 0, "Inactive services mismatch %u\n", servicecountinactive
);
1410 CloseServiceHandle(scm_handle
);
1412 /* More or less the same for EnumServicesStatusExA */
1413 if (!pEnumServicesStatusExA
)
1415 win_skip( "EnumServicesStatusExA not available\n" );
1419 /* All NULL or wrong */
1420 SetLastError(0xdeadbeef);
1421 ret
= pEnumServicesStatusExA(NULL
, 1, 0, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1422 ok(!ret
, "Expected failure\n");
1423 ok(GetLastError() == ERROR_INVALID_LEVEL
,
1424 "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1426 /* All NULL or wrong, just the info level is correct */
1427 SetLastError(0xdeadbeef);
1428 ret
= pEnumServicesStatusExA(NULL
, 0, 0, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1429 ok(!ret
, "Expected failure\n");
1430 ok(GetLastError() == ERROR_INVALID_HANDLE
,
1431 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1433 /* Open the service control manager with not enough rights at first */
1434 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
1436 /* Valid handle and info level but rest is still NULL or wrong */
1437 SetLastError(0xdeadbeef);
1438 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1439 ok(!ret
, "Expected failure\n");
1440 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1441 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1442 "Unexpected last error %d\n", GetLastError());
1444 /* Don't specify the two required pointers */
1445 needed
= 0xdeadbeef;
1446 SetLastError(0xdeadbeef);
1447 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, &needed
, NULL
, NULL
, NULL
);
1448 ok(!ret
, "Expected failure\n");
1449 ok(needed
== 0xdeadbeef || broken(needed
!= 0xdeadbeef), /* nt4 */
1450 "Expected no change to the needed buffer variable\n");
1451 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1452 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1453 "Unexpected last error %d\n", GetLastError());
1455 /* Don't specify the two required pointers */
1456 returned
= 0xdeadbeef;
1457 SetLastError(0xdeadbeef);
1458 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, NULL
, &returned
, NULL
, NULL
);
1459 ok(!ret
, "Expected failure\n");
1460 ok(returned
== 0xdeadbeef, "Expected no change to the number of services variable\n");
1461 ok(GetLastError() == ERROR_INVALID_ADDRESS
||
1462 GetLastError() == ERROR_INVALID_PARAMETER
/* NT4 */,
1463 "Unexpected last error %d\n", GetLastError());
1465 /* No valid servicetype and servicestate */
1466 needed
= 0xdeadbeef;
1467 returned
= 0xdeadbeef;
1468 SetLastError(0xdeadbeef);
1469 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1470 ok(!ret
, "Expected failure\n");
1471 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1472 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1473 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1474 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1475 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1477 /* No valid servicestate */
1478 needed
= 0xdeadbeef;
1479 returned
= 0xdeadbeef;
1480 SetLastError(0xdeadbeef);
1481 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, 0, NULL
, 0,
1482 &needed
, &returned
, NULL
, NULL
);
1483 ok(!ret
, "Expected failure\n");
1484 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1485 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1486 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1487 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1488 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1490 /* No valid servicetype */
1491 needed
= 0xdeadbeef;
1492 returned
= 0xdeadbeef;
1493 SetLastError(0xdeadbeef);
1494 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, SERVICE_STATE_ALL
, NULL
, 0,
1495 &needed
, &returned
, NULL
, NULL
);
1496 ok(!ret
, "Expected failure\n");
1497 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1498 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1499 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1500 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1501 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1503 /* No valid servicetype and servicestate and unknown service group */
1504 needed
= 0xdeadbeef;
1505 returned
= 0xdeadbeef;
1506 SetLastError(0xdeadbeef);
1507 ret
= pEnumServicesStatusExA(scm_handle
, 0, 0, 0, NULL
, 0, &needed
,
1508 &returned
, NULL
, "deadbeef_group");
1509 ok(!ret
, "Expected failure\n");
1510 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1511 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1512 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1513 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
1514 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1516 /* All parameters are correct but our access rights are wrong */
1517 needed
= 0xdeadbeef;
1518 returned
= 0xdeadbeef;
1519 SetLastError(0xdeadbeef);
1520 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1521 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1522 ok(!ret
, "Expected failure\n");
1523 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1524 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1525 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1526 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1527 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1529 /* All parameters are correct, access rights are wrong but the
1530 * group name won't be checked yet.
1532 needed
= 0xdeadbeef;
1533 returned
= 0xdeadbeef;
1534 SetLastError(0xdeadbeef);
1535 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1536 NULL
, 0, &needed
, &returned
, NULL
, "deadbeef_group");
1537 ok(!ret
, "Expected failure\n");
1538 ok(needed
== 0 || broken(needed
!= 0), /* nt4 */
1539 "Expected needed buffer size to be set to 0, got %d\n", needed
);
1540 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1541 ok(GetLastError() == ERROR_ACCESS_DENIED
,
1542 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1544 /* Open the service control manager with the needed rights */
1545 CloseServiceHandle(scm_handle
);
1546 scm_handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
1548 /* All parameters are correct and the group will be checked */
1549 needed
= 0xdeadbeef;
1550 returned
= 0xdeadbeef;
1551 SetLastError(0xdeadbeef);
1552 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1553 NULL
, 0, &needed
, &returned
, NULL
, "deadbeef_group");
1554 ok(!ret
, "Expected failure\n");
1555 ok(returned
== 0, "Expected number of service to be set to 0, got %d\n", returned
);
1556 ok(needed
== 0, "Expected needed buffer size to be set to 0, got %d\n", needed
);
1557 ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
,
1558 "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1560 /* TODO: Create a test that makes sure we enumerate all services that don't
1561 * belong to a group. (specifying "").
1564 /* All parameters are correct. Request the needed buffer size */
1565 needed
= 0xdeadbeef;
1566 returned
= 0xdeadbeef;
1567 SetLastError(0xdeadbeef);
1568 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1569 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1570 ok(!ret
, "Expected failure\n");
1571 ok(returned
== 0, "Expected no service returned, got %d\n", returned
);
1572 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size\n");
1573 ok(GetLastError() == ERROR_MORE_DATA
,
1574 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1576 /* Test to show we get the same needed buffer size for the W-call */
1577 neededW
= 0xdeadbeef;
1578 ret
= pEnumServicesStatusExW(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1579 NULL
, 0, &neededW
, &returnedW
, NULL
, NULL
);
1580 ok(!ret
, "Expected failure\n");
1581 ok(neededW
== needed
, "Expected needed buffersize to be the same for A- and W-calls\n");
1583 /* Store the needed bytes */
1584 tempneeded
= needed
;
1586 /* Allocate the correct needed bytes */
1587 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1589 needed
= 0xdeadbeef;
1590 returned
= 0xdeadbeef;
1591 SetLastError(0xdeadbeef);
1592 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1593 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, NULL
, NULL
);
1594 ok(ret
, "Expected success, got error %u\n", GetLastError());
1595 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1596 ok(returned
== tempreturned
, "Expected the same number of service from this function\n");
1597 HeapFree(GetProcessHeap(), 0, exservices
);
1599 /* Store the number of returned services */
1600 tempreturned
= returned
;
1602 /* Allocate less than the needed bytes and don't specify a resume handle */
1603 exservices
= HeapAlloc(GetProcessHeap(), 0, tempneeded
);
1604 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUSA
);
1605 needed
= 0xdeadbeef;
1606 returned
= 0xdeadbeef;
1607 SetLastError(0xdeadbeef);
1608 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1609 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, NULL
, NULL
);
1610 ok(!ret
, "Expected failure\n");
1611 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size\n");
1612 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1613 ok(GetLastError() == ERROR_MORE_DATA
,
1614 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1616 /* Allocate less than the needed bytes, this time with a correct resume handle */
1617 bufsize
= (tempreturned
- 1) * sizeof(ENUM_SERVICE_STATUSA
);
1618 needed
= 0xdeadbeef;
1619 returned
= 0xdeadbeef;
1621 SetLastError(0xdeadbeef);
1622 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1623 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, &resume
, NULL
);
1624 ok(!ret
, "Expected failure\n");
1625 ok(needed
!= 0xdeadbeef && needed
> 0, "Expected the needed buffer size\n");
1626 ok(returned
< tempreturned
, "Expected fewer services to be returned\n");
1627 todo_wine
ok(resume
, "Expected a resume handle\n");
1628 ok(GetLastError() == ERROR_MORE_DATA
,
1629 "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1631 /* Fetch that last service but pass a bigger buffer size */
1632 missing
= tempreturned
- returned
;
1633 bufsize
= tempneeded
;
1634 needed
= 0xdeadbeef;
1635 returned
= 0xdeadbeef;
1636 SetLastError(0xdeadbeef);
1637 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1638 (BYTE
*)exservices
, bufsize
, &needed
, &returned
, &resume
, NULL
);
1639 ok(ret
, "Expected success, got error %u\n", GetLastError());
1640 ok(needed
== 0, "Expected needed buffer to be 0 as we are done\n");
1641 ok(returned
== missing
, "Expected %u services to be returned\n", missing
);
1642 ok(resume
== 0, "Expected the resume handle to be 0\n");
1643 HeapFree(GetProcessHeap(), 0, exservices
);
1645 /* See if things add up */
1647 /* Get the number of active win32 services */
1648 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_ACTIVE
,
1649 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1650 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1651 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_ACTIVE
,
1652 (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1653 HeapFree(GetProcessHeap(), 0, exservices
);
1655 servicecountactive
= returned
;
1657 /* Get the number of inactive win32 services */
1658 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_INACTIVE
,
1659 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1660 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1661 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_INACTIVE
,
1662 (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1663 HeapFree(GetProcessHeap(), 0, exservices
);
1665 servicecountinactive
= returned
;
1667 /* Get the number of win32 services */
1668 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1669 NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1670 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1671 pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
, SERVICE_STATE_ALL
,
1672 (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1673 HeapFree(GetProcessHeap(), 0, exservices
);
1675 /* Check if total is the same as active and inactive win32 services */
1676 ok(returned
== (servicecountactive
+ servicecountinactive
),
1677 "Something wrong in the calculation\n");
1679 /* Get all drivers and services */
1680 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
| SERVICE_DRIVER
,
1681 SERVICE_STATE_ALL
, NULL
, 0, &needed
, &returned
, NULL
, NULL
);
1682 ok(!ret
, "Expected failure\n");
1683 exservices
= HeapAlloc(GetProcessHeap(), 0, needed
);
1684 ret
= pEnumServicesStatusExA(scm_handle
, 0, SERVICE_WIN32
| SERVICE_DRIVER
,
1685 SERVICE_STATE_ALL
, (BYTE
*)exservices
, needed
, &needed
, &returned
, NULL
, NULL
);
1686 ok(ret
, "Expected success %u\n", GetLastError());
1688 /* Loop through all those returned drivers and services */
1689 for (i
= 0; i
< returned
; i
++)
1691 SERVICE_STATUS_PROCESS status
= exservices
[i
].ServiceStatusProcess
;
1693 /* lpServiceName and lpDisplayName should always be filled */
1694 ok(exservices
[i
].lpServiceName
[0], "Expected a service name\n");
1695 ok(exservices
[i
].lpDisplayName
&& exservices
[i
].lpDisplayName
[0], "Expected a display name\n");
1697 /* Decrement the counters to see if the functions calls return the
1698 * same numbers as the contents of these structures.
1699 * Check some process id specifics.
1701 if (status
.dwServiceType
& (SERVICE_FILE_SYSTEM_DRIVER
| SERVICE_KERNEL_DRIVER
))
1703 /* We shouldn't have a process id for drivers */
1704 ok(status
.dwProcessId
== 0,
1705 "This driver shouldn't have an associated process id\n");
1708 if (status
.dwServiceType
& (SERVICE_WIN32_OWN_PROCESS
| SERVICE_WIN32_SHARE_PROCESS
))
1710 if (status
.dwCurrentState
!= SERVICE_STOPPED
)
1712 /* We expect a process id for every running service */
1713 ok(status
.dwProcessId
> 0, "Expected a process id for this running service (%s)\n",
1714 exservices
[i
].lpServiceName
);
1716 servicecountactive
--;
1720 /* We shouldn't have a process id for inactive services */
1721 ok(status
.dwProcessId
== 0, "Service %s state %u shouldn't have an associated process id\n",
1722 exservices
[i
].lpServiceName
, status
.dwCurrentState
);
1724 servicecountinactive
--;
1728 HeapFree(GetProcessHeap(), 0, exservices
);
1730 ok(servicecountactive
== 0, "Active services mismatch %u\n", servicecountactive
);
1731 ok(servicecountinactive
== 0, "Inactive services mismatch %u\n", servicecountinactive
);
1733 CloseServiceHandle(scm_handle
);
1736 static void test_close(void)
1742 SetLastError(0xdeadbeef);
1743 ret
= CloseServiceHandle(NULL
);
1744 ok(!ret
, "Expected failure\n");
1745 ok(GetLastError() == ERROR_INVALID_HANDLE
, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1747 /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1750 handle
= OpenSCManagerA(NULL
, NULL
, SC_MANAGER_CONNECT
);
1751 SetLastError(0xdeadbeef);
1752 ret
= CloseServiceHandle(handle
);
1753 ok(ret
, "Expected success got error %u\n", GetLastError());
1756 static void test_sequence(void)
1758 SC_HANDLE scm_handle
, svc_handle
;
1760 QUERY_SERVICE_CONFIGA
*config
;
1761 DWORD given
, needed
;
1762 static const CHAR servicename
[] = "Winetest";
1763 static const CHAR displayname
[] = "Winetest dummy service";
1764 static const CHAR displayname2
[] = "Winetest dummy service (2)";
1765 static const CHAR pathname
[] = "we_dont_care.exe";
1766 static const CHAR dependencies
[] = "Master1\0Master2\0+MasterGroup1\0";
1767 static const CHAR password
[] = "";
1768 static const CHAR empty
[] = "";
1769 static const CHAR localsystem
[] = "LocalSystem";
1771 SetLastError(0xdeadbeef);
1772 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
1774 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
1776 skip("Not enough rights to get a handle to the manager\n");
1780 ok(scm_handle
!= NULL
, "Could not get a handle to the manager: %d\n", GetLastError());
1782 if (!scm_handle
) return;
1783 svc_handle
= OpenServiceA(scm_handle
, NULL
, GENERIC_READ
);
1784 is_nt4
=(svc_handle
== NULL
&& GetLastError() == ERROR_INVALID_PARAMETER
);
1785 CloseServiceHandle(svc_handle
);
1787 /* Create a dummy service */
1788 SetLastError(0xdeadbeef);
1789 svc_handle
= CreateServiceA(scm_handle
, servicename
, displayname
, GENERIC_ALL
,
1790 SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
, SERVICE_DISABLED
, SERVICE_ERROR_IGNORE
,
1791 pathname
, NULL
, NULL
, dependencies
, NULL
, password
);
1793 if (!svc_handle
&& (GetLastError() == ERROR_SERVICE_EXISTS
))
1795 /* We try and open the service and do the rest of the tests. Some could
1796 * fail if the tests were changed between these runs.
1798 trace("Deletion probably didn't work last time\n");
1799 SetLastError(0xdeadbeef);
1800 svc_handle
= OpenServiceA(scm_handle
, servicename
, GENERIC_ALL
);
1801 if (!svc_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
1803 skip("Not enough rights to open the service\n");
1804 CloseServiceHandle(scm_handle
);
1807 ok(svc_handle
!= NULL
, "Could not open the service : %d\n", GetLastError());
1809 else if (!svc_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
1811 skip("Not enough rights to create the service\n");
1812 CloseServiceHandle(scm_handle
);
1817 ok(svc_handle
!= NULL
, "Could not create the service : %d\n", GetLastError());
1818 if ((svc_handle
!= NULL
) && (pGetSecurityInfo
!= NULL
))
1820 PSID sidOwner
, sidGroup
;
1822 PSECURITY_DESCRIPTOR pSD
;
1823 DWORD error
, n1
, n2
;
1827 /* Test using GetSecurityInfo to obtain security information */
1828 retval
= pGetSecurityInfo(svc_handle
, SE_SERVICE
, DACL_SECURITY_INFORMATION
, &sidOwner
,
1829 &sidGroup
, &dacl
, &sacl
, &pSD
);
1831 ok(retval
== ERROR_SUCCESS
, "Expected GetSecurityInfo to succeed: result %d\n", retval
);
1832 retval
= pGetSecurityInfo(svc_handle
, SE_SERVICE
, DACL_SECURITY_INFORMATION
, NULL
,
1833 NULL
, NULL
, NULL
, &pSD
);
1835 ok(retval
== ERROR_SUCCESS
, "Expected GetSecurityInfo to succeed: result %d\n", retval
);
1838 retval
= pGetSecurityInfo(svc_handle
, SE_SERVICE
, DACL_SECURITY_INFORMATION
, NULL
,
1839 NULL
, &dacl
, NULL
, &pSD
);
1840 ok(retval
== ERROR_SUCCESS
, "Expected GetSecurityInfo to succeed: result %d\n", retval
);
1842 SetLastError(0xdeadbeef);
1843 retval
= pGetSecurityInfo(svc_handle
, SE_SERVICE
, DACL_SECURITY_INFORMATION
, NULL
,
1844 NULL
, NULL
, NULL
, NULL
);
1845 error
= GetLastError();
1846 ok(retval
== ERROR_INVALID_PARAMETER
, "Expected GetSecurityInfo to fail: result %d\n", retval
);
1847 ok(error
== 0xdeadbeef, "Unexpected last error %d\n", error
);
1850 win_skip("A NULL security descriptor in GetSecurityInfo results in an exception on NT4.\n");
1852 /* Test using QueryServiceObjectSecurity to obtain security information */
1853 SetLastError(0xdeadbeef);
1854 bret
= pQueryServiceObjectSecurity(svc_handle
, DACL_SECURITY_INFORMATION
, NULL
, 0, &n1
);
1855 error
= GetLastError();
1856 ok(!bret
, "Expected QueryServiceObjectSecurity to fail: result %d\n", bret
);
1857 ok(error
== ERROR_INSUFFICIENT_BUFFER
||
1858 broken(error
== ERROR_INVALID_ADDRESS
) || broken(error
== ERROR_INVALID_PARAMETER
),
1859 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", error
);
1860 if (error
!= ERROR_INSUFFICIENT_BUFFER
) n1
= 1024;
1861 pSD
= LocalAlloc(0, n1
);
1862 bret
= pQueryServiceObjectSecurity(svc_handle
, DACL_SECURITY_INFORMATION
, pSD
, n1
, &n2
);
1863 ok(bret
, "Expected QueryServiceObjectSecurity to succeed: result %d\n", bret
);
1869 CloseServiceHandle(scm_handle
);
1874 * Before we do a QueryServiceConfig we should check the registry. This will make sure
1875 * that the correct keys are used.
1878 /* Request the size for the buffer */
1879 SetLastError(0xdeadbeef);
1880 ret
= QueryServiceConfigA(svc_handle
, NULL
, 0, &needed
);
1881 ok(!ret
, "Expected failure\n");
1882 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1884 config
= HeapAlloc(GetProcessHeap(), 0, needed
);
1886 SetLastError(0xdeadbeef);
1887 ret
= QueryServiceConfigA(svc_handle
, config
, given
, &needed
);
1888 ok(ret
, "Expected success, got error %u\n", GetLastError());
1890 ok(config
->lpBinaryPathName
&& config
->lpLoadOrderGroup
&& config
->lpDependencies
&& config
->lpServiceStartName
&&
1891 config
->lpDisplayName
, "Expected all string struct members to be non-NULL\n");
1892 ok(config
->dwServiceType
== (SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
),
1893 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config
->dwServiceType
);
1894 ok(config
->dwStartType
== SERVICE_DISABLED
, "Expected SERVICE_DISABLED, got %d\n", config
->dwStartType
);
1895 ok(config
->dwErrorControl
== SERVICE_ERROR_IGNORE
, "Expected SERVICE_ERROR_IGNORE, got %d\n", config
->dwErrorControl
);
1896 ok(!strcmp(config
->lpBinaryPathName
, pathname
), "Expected '%s', got '%s'\n", pathname
, config
->lpBinaryPathName
);
1897 ok(!strcmp(config
->lpLoadOrderGroup
, empty
), "Expected an empty string, got '%s'\n", config
->lpLoadOrderGroup
);
1898 ok(config
->dwTagId
== 0, "Expected 0, got %d\n", config
->dwTagId
);
1899 /* TODO: Show the double 0 terminated string */
1902 ok(!memcmp(config
->lpDependencies
, dependencies
, sizeof(dependencies
)), "Wrong string\n");
1904 ok(!strcmp(config
->lpServiceStartName
, localsystem
), "Expected 'LocalSystem', got '%s'\n", config
->lpServiceStartName
);
1905 ok(!strcmp(config
->lpDisplayName
, displayname
), "Expected '%s', got '%s'\n", displayname
, config
->lpDisplayName
);
1907 ret
= ChangeServiceConfigA(svc_handle
, SERVICE_NO_CHANGE
, SERVICE_NO_CHANGE
, SERVICE_ERROR_NORMAL
, NULL
, "TestGroup2",
1908 NULL
, NULL
, NULL
, NULL
, displayname2
);
1909 ok(ret
, "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1911 QueryServiceConfigA(svc_handle
, NULL
, 0, &needed
);
1912 config
= HeapReAlloc(GetProcessHeap(), 0, config
, needed
);
1913 ok(QueryServiceConfigA(svc_handle
, config
, needed
, &needed
), "QueryServiceConfig failed\n");
1914 ok(config
->lpBinaryPathName
&& config
->lpLoadOrderGroup
&& config
->lpDependencies
&& config
->lpServiceStartName
&&
1915 config
->lpDisplayName
, "Expected all string struct members to be non-NULL\n");
1916 ok(config
->dwServiceType
== (SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
),
1917 "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config
->dwServiceType
);
1918 ok(config
->dwStartType
== SERVICE_DISABLED
, "Expected SERVICE_DISABLED, got %d\n", config
->dwStartType
);
1919 ok(config
->dwErrorControl
== SERVICE_ERROR_NORMAL
, "Expected SERVICE_ERROR_NORMAL, got %d\n", config
->dwErrorControl
);
1920 ok(!strcmp(config
->lpBinaryPathName
, pathname
), "Expected '%s', got '%s'\n", pathname
, config
->lpBinaryPathName
);
1921 ok(!strcmp(config
->lpLoadOrderGroup
, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config
->lpLoadOrderGroup
);
1922 ok(config
->dwTagId
== 0, "Expected 0, got %d\n", config
->dwTagId
);
1923 ok(!strcmp(config
->lpServiceStartName
, localsystem
), "Expected 'LocalSystem', got '%s'\n", config
->lpServiceStartName
);
1924 ok(!strcmp(config
->lpDisplayName
, displayname2
), "Expected '%s', got '%s'\n", displayname2
, config
->lpDisplayName
);
1926 SetLastError(0xdeadbeef);
1927 ret
= DeleteService(svc_handle
);
1928 ok(ret
, "Expected success, got error %u\n", GetLastError());
1929 CloseServiceHandle(svc_handle
);
1931 /* Wait a while. The following test does a CreateService again */
1934 CloseServiceHandle(scm_handle
);
1935 HeapFree(GetProcessHeap(), 0, config
);
1938 static void test_queryconfig2(void)
1940 SC_HANDLE scm_handle
, svc_handle
;
1942 DWORD expected
, needed
;
1943 BYTE buffer
[MAX_PATH
];
1944 LPSERVICE_DESCRIPTIONA pConfig
= (LPSERVICE_DESCRIPTIONA
)buffer
;
1945 SERVICE_PRESHUTDOWN_INFO preshutdown_info
;
1946 static const CHAR servicename
[] = "Winetest";
1947 static const CHAR displayname
[] = "Winetest dummy service";
1948 static const CHAR pathname
[] = "we_dont_care.exe";
1949 static const CHAR dependencies
[] = "Master1\0Master2\0+MasterGroup1\0";
1950 static const CHAR password
[] = "";
1951 static const CHAR description
[] = "Description";
1953 if(!pQueryServiceConfig2A
)
1955 win_skip("function QueryServiceConfig2A not present\n");
1959 SetLastError(0xdeadbeef);
1960 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
1964 if(GetLastError() == ERROR_ACCESS_DENIED
)
1965 skip("Not enough rights to get a handle to the manager\n");
1967 ok(FALSE
, "Could not get a handle to the manager: %d\n", GetLastError());
1971 /* Create a dummy service */
1972 SetLastError(0xdeadbeef);
1973 svc_handle
= CreateServiceA(scm_handle
, servicename
, displayname
, GENERIC_ALL
,
1974 SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
, SERVICE_DISABLED
, SERVICE_ERROR_IGNORE
,
1975 pathname
, NULL
, NULL
, dependencies
, NULL
, password
);
1979 if(GetLastError() == ERROR_SERVICE_EXISTS
)
1981 /* We try and open the service and do the rest of the tests. Some could
1982 * fail if the tests were changed between these runs.
1984 trace("Deletion probably didn't work last time\n");
1985 SetLastError(0xdeadbeef);
1986 svc_handle
= OpenServiceA(scm_handle
, servicename
, GENERIC_ALL
);
1989 if(GetLastError() == ERROR_ACCESS_DENIED
)
1990 skip("Not enough rights to open the service\n");
1992 ok(FALSE
, "Could not open the service : %d\n", GetLastError());
1993 CloseServiceHandle(scm_handle
);
1997 if (GetLastError() == ERROR_ACCESS_DENIED
)
1999 skip("Not enough rights to create the service\n");
2000 CloseServiceHandle(scm_handle
);
2003 ok(svc_handle
!= NULL
, "Could not create the service : %d\n", GetLastError());
2006 CloseServiceHandle(scm_handle
);
2010 SetLastError(0xdeadbeef);
2011 ret
= pQueryServiceConfig2A(svc_handle
,0xfff0,buffer
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
2012 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2013 ok(ERROR_INVALID_LEVEL
== GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
2015 SetLastError(0xdeadbeef);
2016 ret
= pQueryServiceConfig2A(svc_handle
,0xfff0,buffer
,sizeof(SERVICE_DESCRIPTIONA
),NULL
);
2017 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2018 ok(ERROR_INVALID_LEVEL
== GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
2020 SetLastError(0xdeadbeef);
2021 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
),NULL
);
2022 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2023 ok(ERROR_INVALID_ADDRESS
== GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
2025 SetLastError(0xdeadbeef);
2026 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
2027 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2028 ok((ERROR_INVALID_ADDRESS
== GetLastError()) || (ERROR_INSUFFICIENT_BUFFER
== GetLastError()),
2029 "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2031 SetLastError(0xdeadbeef);
2032 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,sizeof(SERVICE_DESCRIPTIONA
),NULL
);
2033 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2034 ok(ERROR_INVALID_ADDRESS
== GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
2037 SetLastError(0xdeadbeef);
2038 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
)-1,&needed
);
2039 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2040 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2041 ok(needed
== sizeof(SERVICE_DESCRIPTIONA
), "got %d\n", needed
);
2044 pConfig
->lpDescription
= (LPSTR
)0xdeadbeef;
2045 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
2046 ok(ret
, "expected QueryServiceConfig2A to succeed\n");
2047 ok(needed
== sizeof(SERVICE_DESCRIPTIONA
), "got %d\n", needed
);
2048 ok(!pConfig
->lpDescription
, "expected lpDescription to be NULL, got %p\n", pConfig
->lpDescription
);
2050 SetLastError(0xdeadbeef);
2052 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,0,&needed
);
2053 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2054 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2055 ok(needed
== sizeof(SERVICE_DESCRIPTIONA
), "got %d\n", needed
);
2057 if(!pChangeServiceConfig2A
)
2059 win_skip("function ChangeServiceConfig2A not present\n");
2063 pConfig
->lpDescription
= (LPSTR
) description
;
2064 ret
= pChangeServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
);
2065 ok(ret
, "ChangeServiceConfig2A failed\n");
2070 SetLastError(0xdeadbeef);
2072 expected
= sizeof(SERVICE_DESCRIPTIONA
) + sizeof(description
) * sizeof(WCHAR
); /* !! */
2073 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,sizeof(SERVICE_DESCRIPTIONA
),&needed
);
2074 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2075 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2076 ok(needed
== expected
, "expected needed to be %d, got %d\n", expected
, needed
);
2078 SetLastError(0xdeadbeef);
2079 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,needed
-1,&needed
);
2080 ok(!ret
, "expected QueryServiceConfig2A to fail\n");
2081 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2083 SetLastError(0xdeadbeef);
2084 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
,needed
,&needed
);
2085 ok(ret
, "expected QueryServiceConfig2A to succeed\n");
2086 ok(pConfig
->lpDescription
&& !strcmp(description
,pConfig
->lpDescription
),
2087 "expected lpDescription to be %s, got %s\n",description
,pConfig
->lpDescription
);
2089 SetLastError(0xdeadbeef);
2090 ret
= pQueryServiceConfig2A(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
, needed
+ 1,&needed
);
2091 ok(ret
, "expected QueryServiceConfig2A to succeed\n");
2092 ok(pConfig
->lpDescription
&& !strcmp(description
,pConfig
->lpDescription
),
2093 "expected lpDescription to be %s, got %s\n",description
,pConfig
->lpDescription
);
2095 if(!pQueryServiceConfig2W
)
2097 win_skip("function QueryServiceConfig2W not present\n");
2100 SetLastError(0xdeadbeef);
2102 expected
= sizeof(SERVICE_DESCRIPTIONW
) + sizeof(WCHAR
) * sizeof(description
);
2103 ret
= pQueryServiceConfig2W(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,NULL
,0,&needed
);
2104 ok(!ret
, "expected QueryServiceConfig2W to fail\n");
2105 ok(ERROR_INSUFFICIENT_BUFFER
== GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2106 ok(needed
== expected
, "expected needed to be %d, got %d\n", expected
, needed
);
2108 SetLastError(0xdeadbeef);
2109 ret
= pQueryServiceConfig2W(svc_handle
, SERVICE_CONFIG_DESCRIPTION
,buffer
, needed
,&needed
);
2110 ok(ret
, "expected QueryServiceConfig2W to succeed\n");
2112 SetLastError(0xdeadbeef);
2113 ret
= pQueryServiceConfig2W(svc_handle
, SERVICE_CONFIG_PRESHUTDOWN_INFO
,
2114 (LPBYTE
)&preshutdown_info
, sizeof(preshutdown_info
), &needed
);
2115 if(!ret
&& GetLastError()==ERROR_INVALID_LEVEL
)
2117 /* Win2k3 and older */
2118 win_skip("SERVICE_CONFIG_PRESHUTDOWN_INFO not supported\n");
2121 ok(ret
, "expected QueryServiceConfig2W to succeed (%d)\n", GetLastError());
2122 ok(needed
== sizeof(preshutdown_info
), "needed = %d\n", needed
);
2123 ok(preshutdown_info
.dwPreshutdownTimeout
== 180000, "Default PreshutdownTimeout = %d\n",
2124 preshutdown_info
.dwPreshutdownTimeout
);
2126 SetLastError(0xdeadbeef);
2127 preshutdown_info
.dwPreshutdownTimeout
= -1;
2128 ret
= pChangeServiceConfig2A(svc_handle
, SERVICE_CONFIG_PRESHUTDOWN_INFO
,
2129 (LPVOID
)&preshutdown_info
);
2130 ok(ret
, "expected ChangeServiceConfig2A to succeed (%d)\n", GetLastError());
2132 ret
= pQueryServiceConfig2W(svc_handle
, SERVICE_CONFIG_PRESHUTDOWN_INFO
,
2133 (LPBYTE
)&preshutdown_info
, sizeof(preshutdown_info
), &needed
);
2134 ok(ret
, "expected QueryServiceConfig2W to succeed (%d)\n", GetLastError());
2135 ok(needed
== sizeof(preshutdown_info
), "needed = %d\n", needed
);
2136 ok(preshutdown_info
.dwPreshutdownTimeout
== -1, "New PreshutdownTimeout = %d\n",
2137 preshutdown_info
.dwPreshutdownTimeout
);
2140 DeleteService(svc_handle
);
2142 CloseServiceHandle(svc_handle
);
2144 /* Wait a while. The following test does a CreateService again */
2147 CloseServiceHandle(scm_handle
);
2150 static DWORD
try_start_stop(SC_HANDLE svc_handle
, const char* name
, DWORD is_nt4
)
2154 SERVICE_STATUS status
;
2157 fprintf(stdout
, "ROSTESTS-56: Hello sysreg, I am awake\n");
2159 ret
= StartServiceA(svc_handle
, 0, NULL
);
2160 le1
= GetLastError();
2161 ok(!ret
, "%s: StartServiceA() should have failed\n", name
);
2163 if (pQueryServiceStatusEx
)
2166 SERVICE_STATUS_PROCESS statusproc
;
2168 ret
= pQueryServiceStatusEx(svc_handle
, SC_STATUS_PROCESS_INFO
, (BYTE
*)&statusproc
, sizeof(statusproc
), &needed
);
2169 ok(ret
, "%s: QueryServiceStatusEx() failed le=%u\n", name
, GetLastError());
2170 ok(statusproc
.dwCurrentState
== SERVICE_STOPPED
, "%s: should be stopped state=%x\n", name
, statusproc
.dwCurrentState
);
2171 ok(statusproc
.dwProcessId
== 0, "%s: ProcessId should be 0 instead of %x\n", name
, statusproc
.dwProcessId
);
2174 ret
= StartServiceA(svc_handle
, 0, NULL
);
2175 le2
= GetLastError();
2176 ok(!ret
, "%s: StartServiceA() should have failed\n", name
);
2177 ok(le2
== le1
, "%s: the second try should yield the same error: %u != %u\n", name
, le1
, le2
);
2179 status
.dwCurrentState
= 0xdeadbeef;
2180 ret
= ControlService(svc_handle
, SERVICE_CONTROL_STOP
, &status
);
2181 le2
= GetLastError();
2182 ok(!ret
, "%s: ControlService() should have failed\n", name
);
2183 ok(le2
== ERROR_SERVICE_NOT_ACTIVE
, "%s: %d != ERROR_SERVICE_NOT_ACTIVE\n", name
, le2
);
2184 ok(status
.dwCurrentState
== SERVICE_STOPPED
||
2185 broken(is_nt4
), /* NT4 returns a random value */
2186 "%s: should be stopped state=%x\n", name
, status
.dwCurrentState
);
2191 struct notify_data
{
2192 SERVICE_NOTIFYW notify
;
2196 static void CALLBACK
cb_stopped(void *user
)
2198 struct notify_data
*data
= user
;
2201 ok(data
->notify
.dwNotificationStatus
== ERROR_SUCCESS
,
2202 "Got wrong notification status: %u\n", data
->notify
.dwNotificationStatus
);
2203 ok(data
->notify
.ServiceStatus
.dwCurrentState
== SERVICE_STOPPED
,
2204 "Got wrong service state: 0x%x\n", data
->notify
.ServiceStatus
.dwCurrentState
);
2205 ok(data
->notify
.dwNotificationTriggered
== SERVICE_NOTIFY_STOPPED
,
2206 "Got wrong notification triggered: 0x%x\n", data
->notify
.dwNotificationTriggered
);
2208 br
= StartServiceA(data
->svc
, 0, NULL
);
2209 ok(br
, "StartService failed: %u\n", GetLastError());
2212 static void CALLBACK
cb_running(void *user
)
2214 struct notify_data
*data
= user
;
2216 SERVICE_STATUS status
;
2218 ok(data
->notify
.dwNotificationStatus
== ERROR_SUCCESS
,
2219 "Got wrong notification status: %u\n", data
->notify
.dwNotificationStatus
);
2220 ok(data
->notify
.ServiceStatus
.dwCurrentState
== SERVICE_RUNNING
,
2221 "Got wrong service state: 0x%x\n", data
->notify
.ServiceStatus
.dwCurrentState
);
2222 ok(data
->notify
.dwNotificationTriggered
== SERVICE_NOTIFY_RUNNING
,
2223 "Got wrong notification triggered: 0x%x\n", data
->notify
.dwNotificationTriggered
);
2225 br
= ControlService(data
->svc
, SERVICE_CONTROL_STOP
, &status
);
2226 ok(br
, "ControlService failed: %u\n", GetLastError());
2229 static void test_servicenotify(SC_HANDLE svc
)
2232 struct notify_data data
;
2234 if(!pNotifyServiceStatusChangeW
){
2235 win_skip("No NotifyServiceStatusChangeW\n");
2239 memset(&data
.notify
, 0, sizeof(data
.notify
));
2240 data
.notify
.dwVersion
= SERVICE_NOTIFY_STATUS_CHANGE
;
2241 data
.notify
.pfnNotifyCallback
= &cb_stopped
;
2242 data
.notify
.pContext
= &data
;
2245 dr
= pNotifyServiceStatusChangeW(svc
, SERVICE_NOTIFY_STOPPED
| SERVICE_NOTIFY_RUNNING
, &data
.notify
);
2246 ok(dr
== ERROR_SUCCESS
, "NotifyServiceStatusChangeW failed: %u\n", dr
);
2248 dr
= SleepEx(100, TRUE
);
2249 ok(dr
== WAIT_IO_COMPLETION
, "APC wasn't called\n");
2251 data
.notify
.pfnNotifyCallback
= &cb_running
;
2253 dr
= pNotifyServiceStatusChangeW(svc
, SERVICE_NOTIFY_STOPPED
| SERVICE_NOTIFY_RUNNING
, &data
.notify
);
2254 ok(dr
== ERROR_SUCCESS
, "NotifyServiceStatusChangeW failed: %u\n", dr
);
2256 dr
= SleepEx(100, TRUE
);
2257 ok(dr
== WAIT_IO_COMPLETION
, "APC wasn't called\n");
2260 static void test_start_stop(void)
2263 SC_HANDLE scm_handle
, svc_handle
;
2265 static const char servicename
[] = "Winetest";
2266 char cmd
[MAX_PATH
+20];
2267 const char* displayname
;
2269 SetLastError(0xdeadbeef);
2270 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
2273 if(GetLastError() == ERROR_ACCESS_DENIED
)
2274 skip("Not enough rights to get a handle to the manager\n");
2276 ok(FALSE
, "Could not get a handle to the manager: %d\n", GetLastError());
2281 svc_handle
= OpenServiceA(scm_handle
, NULL
, GENERIC_READ
);
2282 is_nt4
=(svc_handle
== NULL
&& GetLastError() == ERROR_INVALID_PARAMETER
);
2284 /* Do some cleanup in case a previous run crashed */
2285 svc_handle
= OpenServiceA(scm_handle
, servicename
, GENERIC_ALL
);
2288 DeleteService(svc_handle
);
2289 CloseServiceHandle(svc_handle
);
2292 /* Create a dummy disabled service */
2293 sprintf(cmd
, "\"%s\" service exit", selfname
);
2294 displayname
= "Winetest Disabled Service";
2295 svc_handle
= CreateServiceA(scm_handle
, servicename
, displayname
,
2296 GENERIC_ALL
, SERVICE_INTERACTIVE_PROCESS
| SERVICE_WIN32_OWN_PROCESS
,
2297 SERVICE_DISABLED
, SERVICE_ERROR_IGNORE
, cmd
, NULL
,
2298 NULL
, NULL
, NULL
, NULL
);
2301 if(GetLastError() == ERROR_ACCESS_DENIED
)
2302 skip("Not enough rights to create the service\n");
2304 ok(FALSE
, "Could not create the service: %d\n", GetLastError());
2307 le
= try_start_stop(svc_handle
, displayname
, is_nt4
);
2308 ok(le
== ERROR_SERVICE_DISABLED
, "%d != ERROR_SERVICE_DISABLED\n", le
);
2310 /* Then one with a bad path */
2311 displayname
= "Winetest Bad Path";
2312 ret
= ChangeServiceConfigA(svc_handle
, SERVICE_NO_CHANGE
, SERVICE_DEMAND_START
, SERVICE_NO_CHANGE
, "c:\\no_such_file.exe", NULL
, NULL
, NULL
, NULL
, NULL
, displayname
);
2313 ok(ret
, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2314 try_start_stop(svc_handle
, displayname
, is_nt4
);
2318 /* NT4 does not detect when a service fails to start and uses an
2319 * insanely long timeout: 120s. So skip the rest of the tests.
2321 win_skip("Skip some service start/stop tests on NT4\n");
2325 if (!winetest_interactive
)
2327 skip("ROSTESTS-151: Skipping service start timeout tests because they take too long. This is not a bug!\n");
2331 /* Again with a process that exits right away */
2332 displayname
= "Winetest Exit Service";
2333 ret
= ChangeServiceConfigA(svc_handle
, SERVICE_NO_CHANGE
, SERVICE_NO_CHANGE
, SERVICE_NO_CHANGE
, cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, displayname
);
2334 ok(ret
, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2335 le
= try_start_stop(svc_handle
, displayname
, is_nt4
);
2336 ok(le
== ERROR_SERVICE_REQUEST_TIMEOUT
, "%d != ERROR_SERVICE_REQUEST_TIMEOUT\n", le
);
2338 /* create a real service and test notifications */
2339 sprintf(cmd
, "%s service serve", selfname
);
2340 displayname
= "Winetest Service";
2341 ret
= ChangeServiceConfigA(svc_handle
, SERVICE_NO_CHANGE
, SERVICE_NO_CHANGE
, SERVICE_NO_CHANGE
, cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, displayname
);
2342 ok(ret
, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2343 test_servicenotify(svc_handle
);
2348 DeleteService(svc_handle
);
2349 CloseServiceHandle(svc_handle
);
2352 /* Wait a while. The following test does a CreateService again */
2355 CloseServiceHandle(scm_handle
);
2358 static void test_refcount(void)
2360 SC_HANDLE scm_handle
, svc_handle1
, svc_handle2
, svc_handle3
, svc_handle4
, svc_handle5
;
2361 static const CHAR servicename
[] = "Winetest";
2362 static const CHAR pathname
[] = "we_dont_care.exe";
2365 /* Get a handle to the Service Control Manager */
2366 SetLastError(0xdeadbeef);
2367 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
2368 if (!scm_handle
&& (GetLastError() == ERROR_ACCESS_DENIED
))
2370 skip("Not enough rights to get a handle to the manager\n");
2374 /* Create a service */
2375 svc_handle1
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
,
2376 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
2377 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
2378 ok(svc_handle1
!= NULL
, "Expected success, got error %u\n", GetLastError());
2380 /* Get a handle to this new service */
2381 svc_handle2
= OpenServiceA(scm_handle
, servicename
, GENERIC_READ
);
2382 ok(svc_handle2
!= NULL
, "Expected success, got error %u\n", GetLastError());
2384 /* Get another handle to this new service */
2385 svc_handle3
= OpenServiceA(scm_handle
, servicename
, GENERIC_READ
);
2386 ok(svc_handle3
!= NULL
, "Expected success, got error %u\n", GetLastError());
2388 /* Check if we can close the handle to the Service Control Manager */
2389 ret
= CloseServiceHandle(scm_handle
);
2390 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2392 /* Get a new handle to the Service Control Manager */
2393 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
2394 ok(scm_handle
!= NULL
, "Expected success, got error %u\n", GetLastError());
2396 /* Get a handle to this new service */
2397 svc_handle4
= OpenServiceA(scm_handle
, servicename
, GENERIC_ALL
);
2398 ok(svc_handle4
!= NULL
, "Expected success, got error %u\n", GetLastError());
2400 /* Delete the service */
2401 ret
= DeleteService(svc_handle4
);
2402 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2404 /* We cannot create the same service again as it's still marked as 'being deleted'.
2405 * The reason is that we still have 4 open handles to this service even though we
2406 * closed the handle to the Service Control Manager in between.
2408 SetLastError(0xdeadbeef);
2409 svc_handle5
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
,
2410 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
2411 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
2412 ok(!svc_handle5
, "Expected failure\n");
2413 ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE
,
2414 "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2416 /* Close all the handles to the service and try again */
2417 ret
= CloseServiceHandle(svc_handle4
);
2418 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2419 ret
= CloseServiceHandle(svc_handle3
);
2420 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2421 ret
= CloseServiceHandle(svc_handle2
);
2422 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2423 ret
= CloseServiceHandle(svc_handle1
);
2424 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2426 /* Wait a while. Doing a CreateService too soon will result again
2427 * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2431 /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2432 svc_handle5
= CreateServiceA(scm_handle
, servicename
, NULL
, GENERIC_ALL
,
2433 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
2434 SERVICE_DISABLED
, 0, pathname
, NULL
, NULL
, NULL
, NULL
, NULL
);
2435 ok(svc_handle5
!= NULL
, "Expected success, got error %u\n", GetLastError());
2437 /* Delete the service */
2438 ret
= DeleteService(svc_handle5
);
2439 ok(ret
, "Expected success (err=%d)\n", GetLastError());
2441 /* Wait a while. Just in case one of the following tests does a CreateService again */
2444 CloseServiceHandle(svc_handle5
);
2445 CloseServiceHandle(scm_handle
);
2448 static DWORD WINAPI
ctrl_handler(DWORD ctl
, DWORD type
, void *data
, void *user
)
2453 case SERVICE_CONTROL_STOP
:
2456 case SERVICE_CONTROL_INTERROGATE
:
2460 return ERROR_CALL_NOT_IMPLEMENTED
;
2463 static void WINAPI
service_main(DWORD argc
, char **argv
)
2465 SERVICE_STATUS_HANDLE st_handle
;
2467 HANDLE evt
= CreateEventW(0, FALSE
, FALSE
, 0);
2469 st_handle
= RegisterServiceCtrlHandlerExA("", &ctrl_handler
, evt
);
2471 st
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
2472 st
.dwServiceSpecificExitCode
= 0;
2473 st
.dwCurrentState
= SERVICE_RUNNING
;
2474 st
.dwWin32ExitCode
= NO_ERROR
;
2476 st
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
2477 st
.dwCheckPoint
= 0;
2479 SetServiceStatus(st_handle
, &st
);
2481 WaitForSingleObject(evt
, 5000);
2483 st
.dwCurrentState
= SERVICE_STOPPED
;
2485 SetServiceStatus(st_handle
, &st
);
2488 static void run_service(void)
2491 SERVICE_TABLE_ENTRYA table
[] = {
2492 {empty
, &service_main
},
2496 StartServiceCtrlDispatcherA(table
);
2501 SC_HANDLE scm_handle
;
2505 myARGC
= winetest_get_mainargs(&myARGV
);
2506 GetFullPathNameA(myARGV
[0], sizeof(selfname
), selfname
, NULL
);
2509 if (strcmp(myARGV
[2], "serve") == 0)
2514 /* Bail out if we are on win98 */
2515 SetLastError(0xdeadbeef);
2516 scm_handle
= OpenSCManagerA(NULL
, NULL
, GENERIC_ALL
);
2518 if (!scm_handle
&& (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
2520 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2523 CloseServiceHandle(scm_handle
);
2525 init_function_pointers();
2527 /* First some parameter checking */
2530 test_create_delete_svc();
2531 test_get_displayname();
2532 test_get_servicekeyname();
2536 /* Test the creation, querying and deletion of a service */
2538 test_queryconfig2();
2540 /* The main reason for this test is to check if any refcounting is used
2541 * and what the rules are