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