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