mshtml: Populate dynamic properties table in get_dynamic_data.
[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
953     /* Open service with not enough rights to query the status */
954     svc_handle = OpenServiceA(scm_handle, spooler, STANDARD_RIGHTS_READ);
955
956     /* All NULL or wrong, this proves that info level is checked first */
957     SetLastError(0xdeadbeef);
958     ret = pQueryServiceStatusEx(NULL, 1, NULL, 0, NULL);
959     ok(!ret, "Expected failure\n");
960     ok(GetLastError() == ERROR_INVALID_LEVEL,
961        "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
962
963     /* Passing a NULL parameter for the needed buffer size
964      * will crash on anything but NT4.
965      */
966
967     /* Only info level is correct. It looks like the buffer/size is checked second */
968     SetLastError(0xdeadbeef);
969     ret = pQueryServiceStatusEx(NULL, SC_STATUS_PROCESS_INFO, NULL, 0, &needed);
970     /* NT4 checks the handle first */
971     if (GetLastError() != ERROR_INVALID_HANDLE)
972     {
973         ok(!ret, "Expected failure\n");
974         ok(needed == sizeof(SERVICE_STATUS_PROCESS),
975            "Needed buffersize is wrong : %d\n", needed);
976         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
977            "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
978     }
979
980     /* Pass a correct buffer and buffersize but a NULL handle */
981     statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
982     bufsize = needed;
983     SetLastError(0xdeadbeef);
984     ret = pQueryServiceStatusEx(NULL, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
985     ok(!ret, "Expected failure\n");
986     ok(GetLastError() == ERROR_INVALID_HANDLE,
987        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
988     HeapFree(GetProcessHeap(), 0, statusproc);
989
990     /* Correct handle and info level */
991     SetLastError(0xdeadbeef);
992     ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, NULL, 0, &needed);
993     /* NT4 doesn't return the needed size */
994     if (GetLastError() != ERROR_INVALID_PARAMETER)
995     {
996         ok(!ret, "Expected failure\n");
997         ok(needed == sizeof(SERVICE_STATUS_PROCESS),
998            "Needed buffersize is wrong : %d\n", needed);
999         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1000            "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1001     }
1002
1003     /* All parameters are OK but we don't have enough rights */
1004     statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1005     bufsize = sizeof(SERVICE_STATUS_PROCESS);
1006     SetLastError(0xdeadbeef);
1007     ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
1008     ok(!ret, "Expected failure\n");
1009     ok(GetLastError() == ERROR_ACCESS_DENIED,
1010        "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1011     HeapFree(GetProcessHeap(), 0, statusproc);
1012
1013     /* Open the service with just enough rights. */
1014     CloseServiceHandle(svc_handle);
1015     svc_handle = OpenServiceA(scm_handle, spooler, SERVICE_QUERY_STATUS);
1016
1017     /* Everything should be fine now. */
1018     statusproc = HeapAlloc(GetProcessHeap(), 0, sizeof(SERVICE_STATUS_PROCESS));
1019     bufsize = sizeof(SERVICE_STATUS_PROCESS);
1020     SetLastError(0xdeadbeef);
1021     ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)statusproc, bufsize, &needed);
1022     ok(ret, "Expected success, got error %u\n", GetLastError());
1023     if (statusproc->dwCurrentState == SERVICE_RUNNING)
1024         ok(statusproc->dwProcessId != 0,
1025            "Expect a process id for this running service\n");
1026     else
1027         ok(statusproc->dwProcessId == 0,
1028            "Expect no process id for this stopped service\n");
1029     HeapFree(GetProcessHeap(), 0, statusproc);
1030
1031     CloseServiceHandle(svc_handle);
1032     CloseServiceHandle(scm_handle);
1033 }
1034
1035 static void test_enum_svc(void)
1036 {
1037     SC_HANDLE scm_handle;
1038     BOOL ret;
1039     DWORD bufsize, needed, returned, resume;
1040     DWORD neededW, returnedW;
1041     DWORD tempneeded, tempreturned, missing;
1042     DWORD servicecountactive, servicecountinactive;
1043     ENUM_SERVICE_STATUS *services;
1044     ENUM_SERVICE_STATUSW *servicesW;
1045     ENUM_SERVICE_STATUS_PROCESS *exservices;
1046     UINT i;
1047
1048     /* All NULL or wrong  */
1049     SetLastError(0xdeadbeef);
1050     ret = EnumServicesStatusA(NULL, 1, 0, NULL, 0, NULL, NULL, NULL);
1051     ok(!ret, "Expected failure\n");
1052     ok(GetLastError() == ERROR_INVALID_HANDLE,
1053        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1054
1055     SetLastError(0xdeadbeef);
1056     ret = EnumServicesStatusW(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     /* Open the service control manager with not enough rights at first */
1062     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1063
1064     /* Valid handle but rest is still NULL or wrong  */
1065     SetLastError(0xdeadbeef);
1066     ret = EnumServicesStatusA(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1067     ok(!ret, "Expected failure\n");
1068     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1069        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1070        "Unexpected last error %d\n", GetLastError());
1071
1072     SetLastError(0xdeadbeef);
1073     ret = EnumServicesStatusW(scm_handle, 1, 0, NULL, 0, NULL, NULL, NULL);
1074     ok(!ret, "Expected failure\n");
1075     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1076        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1077        "Unexpected last error %d\n", GetLastError());
1078
1079     /* Don't specify the two required pointers */
1080     returned = 0xdeadbeef;
1081     SetLastError(0xdeadbeef);
1082     ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1083     ok(!ret, "Expected failure\n");
1084     ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1085     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1086        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1087        "Unexpected last error %d\n", GetLastError());
1088
1089     returned = 0xdeadbeef;
1090     SetLastError(0xdeadbeef);
1091     ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, NULL, &returned, NULL);
1092     ok(!ret, "Expected failure\n");
1093     ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1094     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1095        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1096        "Unexpected last error %d\n", GetLastError());
1097
1098     /* Don't specify the two required pointers */
1099     needed = 0xdeadbeef;
1100     SetLastError(0xdeadbeef);
1101     ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1102     ok(!ret, "Expected failure\n");
1103     ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1104        "Expected no change to the needed buffer variable\n");
1105     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1106        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1107        "Unexpected last error %d\n", GetLastError());
1108
1109     needed = 0xdeadbeef;
1110     SetLastError(0xdeadbeef);
1111     ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, NULL, NULL);
1112     ok(!ret, "Expected failure\n");
1113     ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1114        "Expected no change to the needed buffer variable\n");
1115     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1116        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1117        "Unexpected last error %d\n", GetLastError());
1118
1119     /* No valid servicetype and servicestate */
1120     needed = 0xdeadbeef;
1121     returned = 0xdeadbeef;
1122     SetLastError(0xdeadbeef);
1123     ret = EnumServicesStatusA(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1124     ok(!ret, "Expected failure\n");
1125     ok(needed == 0 || broken(needed != 0), /* nt4 */
1126        "Expected needed buffer size to be set to 0, got %d\n", needed);
1127     ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1128     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1129        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1130
1131     needed = 0xdeadbeef;
1132     returned = 0xdeadbeef;
1133     SetLastError(0xdeadbeef);
1134     ret = EnumServicesStatusW(scm_handle, 0, 0, NULL, 0, &needed, &returned, NULL);
1135     ok(!ret, "Expected failure\n");
1136     ok(needed == 0 || broken(needed != 0), /* nt4 */
1137        "Expected needed buffer size to be set to 0, got %d\n", needed);
1138     ok(returned == 0 || broken(returned != 0), /* nt4 */
1139        "Expected number of services to be set to 0, got %d\n", returned);
1140     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1141        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1142
1143     /* No valid servicestate */
1144     needed = 0xdeadbeef;
1145     returned = 0xdeadbeef;
1146     SetLastError(0xdeadbeef);
1147     ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1148     ok(!ret, "Expected failure\n");
1149     ok(needed == 0 || broken(needed != 0), /* nt4 */
1150        "Expected needed buffer size to be set to 0, got %d\n", needed);
1151     ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1152     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1153        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1154
1155     needed = 0xdeadbeef;
1156     returned = 0xdeadbeef;
1157     SetLastError(0xdeadbeef);
1158     ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, 0, NULL, 0, &needed, &returned, NULL);
1159     ok(!ret, "Expected failure\n");
1160     ok(needed == 0 || broken(needed != 0), /* nt4 */
1161        "Expected needed buffer size to be set to 0, got %d\n", needed);
1162     ok(returned == 0 || broken(returned != 0), /* nt4 */
1163        "Expected number of services to be set to 0, got %d\n", returned);
1164     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1165        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1166
1167     /* No valid servicetype */
1168     needed = 0xdeadbeef;
1169     returned = 0xdeadbeef;
1170     SetLastError(0xdeadbeef);
1171     ret = EnumServicesStatusA(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1172     ok(!ret, "Expected failure\n");
1173     ok(needed == 0 || broken(needed != 0), /* nt4 */
1174        "Expected needed buffer size to be set to 0, got %d\n", needed);
1175     ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1176     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1177        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1178
1179     needed = 0xdeadbeef;
1180     returned = 0xdeadbeef;
1181     SetLastError(0xdeadbeef);
1182     ret = EnumServicesStatusW(scm_handle, 0, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1183     ok(!ret, "Expected failure\n");
1184     ok(needed == 0 || broken(needed != 0), /* nt4 */
1185        "Expected needed buffer size to be set to 0, got %d\n", needed);
1186     ok(returned == 0 || broken(returned != 0), /* nt4 */
1187        "Expected number of services to be set to 0, got %d\n", returned);
1188     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1189        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1190
1191     /* All parameters are correct but our access rights are wrong */
1192     needed = 0xdeadbeef;
1193     returned = 0xdeadbeef;
1194     SetLastError(0xdeadbeef);
1195     ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1196     ok(!ret, "Expected failure\n");
1197     ok(needed == 0 || broken(needed != 0), /* nt4 */
1198        "Expected needed buffer size to be set to 0, got %d\n", needed);
1199     ok(returned == 0, "Expected number of services to be set to 0, got %d\n", returned);
1200     ok(GetLastError() == ERROR_ACCESS_DENIED,
1201        "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1202
1203     needed = 0xdeadbeef;
1204     returned = 0xdeadbeef;
1205     SetLastError(0xdeadbeef);
1206     ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1207     ok(!ret, "Expected failure\n");
1208     ok(needed == 0 || broken(needed != 0), /* nt4 */
1209        "Expected needed buffer size to be set to 0, got %d\n", needed);
1210     ok(returned == 0 || broken(returned != 0), /* nt4 */
1211        "Expected number of services to be set to 0, got %d\n", returned);
1212     ok(GetLastError() == ERROR_ACCESS_DENIED,
1213        "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1214
1215     /* Open the service control manager with the needed rights */
1216     CloseServiceHandle(scm_handle);
1217     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1218
1219     /* All parameters are correct. Request the needed buffer size */
1220     needed = 0xdeadbeef;
1221     returned = 0xdeadbeef;
1222     SetLastError(0xdeadbeef);
1223     ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL);
1224     ok(!ret, "Expected failure\n");
1225     ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1226     ok(returned == 0, "Expected no service returned, got %d\n", returned);
1227     ok(GetLastError() == ERROR_MORE_DATA,
1228        "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1229
1230     /* Test to show we get the same needed buffer size for the W-call */
1231     neededW = 0xdeadbeef;
1232     returnedW = 0xdeadbeef;
1233     SetLastError(0xdeadbeef);
1234     ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0, &neededW, &returnedW, NULL);
1235     ok(!ret, "Expected failure\n");
1236     ok(neededW != 0xdeadbeef && neededW > 0, "Expected the needed buffer size for this one service\n");
1237     ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1238     ok(returnedW == 0, "Expected no service returned, got %d\n", returnedW);
1239     ok(GetLastError() == ERROR_MORE_DATA,
1240        "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1241
1242     /* Store the needed bytes */
1243     tempneeded = needed;
1244
1245     /* Allocate the correct needed bytes */
1246     services = HeapAlloc(GetProcessHeap(), 0, needed);
1247     bufsize = needed;
1248     needed = 0xdeadbeef;
1249     returned = 0xdeadbeef;
1250     SetLastError(0xdeadbeef);
1251     ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1252                               services, bufsize, &needed, &returned, NULL);
1253     ok(ret, "Expected success, got error %u\n", GetLastError());
1254     ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1255     ok(returned != 0xdeadbeef && returned > 0, "Expected some returned services\n");
1256     HeapFree(GetProcessHeap(), 0, services);
1257
1258     /* Store the number of returned services */
1259     tempreturned = returned;
1260
1261     servicesW = HeapAlloc(GetProcessHeap(), 0, neededW);
1262     bufsize = neededW;
1263     neededW = 0xdeadbeef;
1264     returnedW = 0xdeadbeef;
1265     SetLastError(0xdeadbeef);
1266     ret = EnumServicesStatusW(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1267                               servicesW, bufsize, &neededW, &returnedW, NULL);
1268     ok(ret, "Expected success, got error %u\n", GetLastError());
1269     ok(neededW == 0, "Expected needed buffer to be 0 as we are done\n");
1270     ok(returnedW != 0xdeadbeef && returnedW > 0, "Expected some returned services\n");
1271     HeapFree(GetProcessHeap(), 0, servicesW);
1272
1273     /* Allocate less than the needed bytes and don't specify a resume handle */
1274     services = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1275     bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1276     needed = 0xdeadbeef;
1277     returned = 0xdeadbeef;
1278     SetLastError(0xdeadbeef);
1279     ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1280                               services, bufsize, &needed, &returned, NULL);
1281     ok(!ret, "Expected failure\n");
1282     ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1283     ok(returned < tempreturned, "Expected fewer services to be returned\n");
1284     ok(GetLastError() == ERROR_MORE_DATA,
1285        "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1286
1287     /* Allocate less than the needed bytes, this time with a correct resume handle */
1288     bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1289     needed = 0xdeadbeef;
1290     returned = 0xdeadbeef;
1291     resume = 0;
1292     SetLastError(0xdeadbeef);
1293     ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1294                               services, bufsize, &needed, &returned, &resume);
1295     ok(!ret, "Expected failure\n");
1296     ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size for this one service\n");
1297     ok(returned < tempreturned, "Expected fewer services to be returned\n");
1298     todo_wine ok(resume, "Expected a resume handle\n");
1299     ok(GetLastError() == ERROR_MORE_DATA,
1300        "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1301
1302     /* Fetch the missing services but pass a bigger buffer size */
1303     missing = tempreturned - returned;
1304     bufsize = tempneeded;
1305     needed = 0xdeadbeef;
1306     returned = 0xdeadbeef;
1307     SetLastError(0xdeadbeef);
1308     ret = EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL,
1309                               services, bufsize, &needed, &returned, &resume);
1310     ok(ret, "Expected success, got error %u\n", GetLastError());
1311     ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1312     ok(returned == missing, "Expected %u services to be returned\n", missing);
1313     ok(resume == 0, "Expected the resume handle to be 0\n");
1314     HeapFree(GetProcessHeap(), 0, services);
1315
1316     /* See if things add up */
1317
1318     /* Vista only shows the drivers with a state of SERVICE_RUNNING as active
1319      * and doesn't count the others as inactive. This means that Vista could
1320      * show a total that is greater than the sum of active and inactive
1321      * drivers.
1322      * The number of active and inactive drivers is greatly influenced by the
1323      * time when tests are run, immediately after boot or later for example.
1324      *
1325      * Both reasons make calculations for drivers not so useful
1326      */
1327
1328     /* Get the number of active win32 services */
1329     EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0,
1330                         &needed, &returned, NULL);
1331     services = HeapAlloc(GetProcessHeap(), 0, needed);
1332     EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_ACTIVE, services,
1333                         needed, &needed, &returned, NULL);
1334     HeapFree(GetProcessHeap(), 0, services);
1335
1336     servicecountactive = returned;
1337
1338     /* Get the number of inactive win32 services */
1339     EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, NULL, 0,
1340                         &needed, &returned, NULL);
1341     services = HeapAlloc(GetProcessHeap(), 0, needed);
1342     EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_INACTIVE, services,
1343                         needed, &needed, &returned, NULL);
1344     HeapFree(GetProcessHeap(), 0, services);
1345
1346     servicecountinactive = returned;
1347
1348     /* Get the number of win32 services */
1349     EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, 0,
1350                         &needed, &returned, NULL);
1351     services = HeapAlloc(GetProcessHeap(), 0, needed);
1352     EnumServicesStatusA(scm_handle, SERVICE_WIN32, SERVICE_STATE_ALL, services,
1353                         needed, &needed, &returned, NULL);
1354     HeapFree(GetProcessHeap(), 0, services);
1355
1356     /* Check if total is the same as active and inactive win32 services */
1357     ok(returned == (servicecountactive + servicecountinactive),
1358        "Something wrong in the calculation\n");
1359
1360     /* Get all drivers and services
1361      *
1362      * Fetch the status of the last call as failing could make the following tests crash
1363      * on Wine (we don't return anything yet).
1364      */
1365     EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1366                         NULL, 0, &needed, &returned, NULL);
1367     services = HeapAlloc(GetProcessHeap(), 0, needed);
1368     ret = EnumServicesStatusA(scm_handle, SERVICE_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL,
1369                               services, needed, &needed, &returned, NULL);
1370
1371     /* Loop through all those returned drivers and services */
1372     for (i = 0; ret && i < returned; i++)
1373     {
1374         SERVICE_STATUS status = services[i].ServiceStatus;
1375
1376         /* lpServiceName and lpDisplayName should always be filled */
1377         ok(lstrlenA(services[i].lpServiceName) > 0, "Expected a service name\n");
1378         ok(lstrlenA(services[i].lpDisplayName) > 0, "Expected a display name\n");
1379
1380         /* Decrement the counters to see if the functions calls return the same
1381          * numbers as the contents of these structures.
1382          */
1383         if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1384         {
1385             if (status.dwCurrentState == SERVICE_STOPPED)
1386                 servicecountinactive--;
1387             else
1388                 servicecountactive--;
1389         }
1390     }
1391     HeapFree(GetProcessHeap(), 0, services);
1392
1393     ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1394     ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1395
1396     CloseServiceHandle(scm_handle);
1397
1398     /* More or less the same for EnumServicesStatusExA */
1399
1400     /* All NULL or wrong */
1401     SetLastError(0xdeadbeef);
1402     ret = pEnumServicesStatusExA(NULL, 1, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1403     ok(!ret, "Expected failure\n");
1404     ok(GetLastError() == ERROR_INVALID_LEVEL,
1405        "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1406
1407     /* All NULL or wrong, just the info level is correct */
1408     SetLastError(0xdeadbeef);
1409     ret = pEnumServicesStatusExA(NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1410     ok(!ret, "Expected failure\n");
1411     ok(GetLastError() == ERROR_INVALID_HANDLE,
1412        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1413
1414     /* Open the service control manager with not enough rights at first */
1415     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1416
1417     /* Valid handle and info level but rest is still NULL or wrong */
1418     SetLastError(0xdeadbeef);
1419     ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
1420     ok(!ret, "Expected failure\n");
1421     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1422        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1423        "Unexpected last error %d\n", GetLastError());
1424
1425     /* Don't specify the two required pointers */
1426     needed = 0xdeadbeef;
1427     SetLastError(0xdeadbeef);
1428     ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, NULL, NULL, NULL);
1429     ok(!ret, "Expected failure\n");
1430     ok(needed == 0xdeadbeef || broken(needed != 0xdeadbeef), /* nt4 */
1431        "Expected no change to the needed buffer variable\n");
1432     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     returned = 0xdeadbeef;
1438     SetLastError(0xdeadbeef);
1439     ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, NULL, &returned, NULL, NULL);
1440     ok(!ret, "Expected failure\n");
1441     ok(returned == 0xdeadbeef, "Expected no change to the number of services variable\n");
1442     ok(GetLastError() == ERROR_INVALID_ADDRESS ||
1443        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
1444        "Unexpected last error %d\n", GetLastError());
1445
1446     /* No valid servicetype and servicestate */
1447     needed = 0xdeadbeef;
1448     returned = 0xdeadbeef;
1449     SetLastError(0xdeadbeef);
1450     ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed, &returned, NULL, NULL);
1451     ok(!ret, "Expected failure\n");
1452     ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1453     ok(needed == 0 || broken(needed != 0), /* nt4 */
1454        "Expected needed buffer size to be set to 0, got %d\n", needed);
1455     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1456        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1457
1458     /* No valid servicestate */
1459     needed = 0xdeadbeef;
1460     returned = 0xdeadbeef;
1461     SetLastError(0xdeadbeef);
1462     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, 0, NULL, 0,
1463                                  &needed, &returned, NULL, NULL);
1464     ok(!ret, "Expected failure\n");
1465     ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1466     ok(needed == 0 || broken(needed != 0), /* nt4 */
1467        "Expected needed buffer size to be set to 0, got %d\n", needed);
1468     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1469        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1470
1471     /* No valid servicetype */
1472     needed = 0xdeadbeef;
1473     returned = 0xdeadbeef;
1474     SetLastError(0xdeadbeef);
1475     ret = pEnumServicesStatusExA(scm_handle, 0, 0, SERVICE_STATE_ALL, NULL, 0,
1476                                  &needed, &returned, NULL, NULL);
1477     ok(!ret, "Expected failure\n");
1478     ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1479     ok(needed == 0 || broken(needed != 0), /* nt4 */
1480        "Expected needed buffer size to be set to 0, got %d\n", needed);
1481     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1482        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1483
1484     /* No valid servicetype and servicestate and unknown service group */
1485     needed = 0xdeadbeef;
1486     returned = 0xdeadbeef;
1487     SetLastError(0xdeadbeef);
1488     ret = pEnumServicesStatusExA(scm_handle, 0, 0, 0, NULL, 0, &needed,
1489                                  &returned, NULL, "deadbeef_group");
1490     ok(!ret, "Expected failure\n");
1491     ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1492     ok(needed == 0 || broken(needed != 0), /* nt4 */
1493        "Expected needed buffer size to be set to 0, got %d\n", needed);
1494     ok(GetLastError() == ERROR_INVALID_PARAMETER,
1495        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1496
1497     /* All parameters are correct but our access rights are wrong */
1498     needed = 0xdeadbeef;
1499     returned = 0xdeadbeef;
1500     SetLastError(0xdeadbeef);
1501     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1502                                  NULL, 0, &needed, &returned, NULL, NULL);
1503     ok(!ret, "Expected failure\n");
1504     ok(needed == 0 || broken(needed != 0), /* nt4 */
1505        "Expected needed buffer size to be set to 0, got %d\n", needed);
1506     ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1507     ok(GetLastError() == ERROR_ACCESS_DENIED,
1508        "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1509
1510     /* All parameters are correct, access rights are wrong but the
1511      * group name won't be checked yet.
1512      */
1513     needed = 0xdeadbeef;
1514     returned = 0xdeadbeef;
1515     SetLastError(0xdeadbeef);
1516     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1517                                  NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1518     ok(!ret, "Expected failure\n");
1519     ok(needed == 0 || broken(needed != 0), /* nt4 */
1520        "Expected needed buffer size to be set to 0, got %d\n", needed);
1521     ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1522     ok(GetLastError() == ERROR_ACCESS_DENIED,
1523        "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1524
1525     /* Open the service control manager with the needed rights */
1526     CloseServiceHandle(scm_handle);
1527     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
1528
1529     /* All parameters are correct and the group will be checked */
1530     needed = 0xdeadbeef;
1531     returned = 0xdeadbeef;
1532     SetLastError(0xdeadbeef);
1533     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1534                                  NULL, 0, &needed, &returned, NULL, "deadbeef_group");
1535     ok(!ret, "Expected failure\n");
1536     ok(returned == 0, "Expected number of service to be set to 0, got %d\n", returned);
1537     ok(needed == 0, "Expected needed buffer size to be set to 0, got %d\n", needed);
1538     ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
1539        "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
1540
1541     /* TODO: Create a test that makes sure we enumerate all services that don't
1542      * belong to a group. (specifying "").
1543      */
1544
1545     /* All parameters are correct. Request the needed buffer size */
1546     needed = 0xdeadbeef;
1547     returned = 0xdeadbeef;
1548     SetLastError(0xdeadbeef);
1549     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1550                                  NULL, 0, &needed, &returned, NULL, NULL);
1551     ok(!ret, "Expected failure\n");
1552     ok(returned == 0, "Expected no service returned, got %d\n", returned);
1553     ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1554     ok(GetLastError() == ERROR_MORE_DATA,
1555        "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1556
1557     /* Test to show we get the same needed buffer size for the W-call */
1558     neededW = 0xdeadbeef;
1559     ret = pEnumServicesStatusExW(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1560                                  NULL, 0, &neededW, &returnedW, NULL, NULL);
1561     ok(!ret, "Expected failure\n");
1562     ok(neededW == needed, "Expected needed buffersize to be the same for A- and W-calls\n");
1563
1564     /* Store the needed bytes */
1565     tempneeded = needed;
1566
1567     /* Allocate the correct needed bytes */
1568     exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1569     bufsize = needed;
1570     needed = 0xdeadbeef;
1571     returned = 0xdeadbeef;
1572     SetLastError(0xdeadbeef);
1573     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1574                                  (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1575     ok(ret, "Expected success, got error %u\n", GetLastError());
1576     ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1577     ok(returned == tempreturned, "Expected the same number of service from this function\n");
1578     HeapFree(GetProcessHeap(), 0, exservices);
1579
1580     /* Store the number of returned services */
1581     tempreturned = returned;
1582
1583     /* Allocate less than the needed bytes and don't specify a resume handle */
1584     exservices = HeapAlloc(GetProcessHeap(), 0, tempneeded);
1585     bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1586     needed = 0xdeadbeef;
1587     returned = 0xdeadbeef;
1588     SetLastError(0xdeadbeef);
1589     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1590                                  (BYTE*)exservices, bufsize, &needed, &returned, NULL, NULL);
1591     ok(!ret, "Expected failure\n");
1592     ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1593     ok(returned < tempreturned, "Expected fewer services to be returned\n");
1594     ok(GetLastError() == ERROR_MORE_DATA,
1595        "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1596
1597     /* Allocate less than the needed bytes, this time with a correct resume handle */
1598     bufsize = (tempreturned - 1) * sizeof(ENUM_SERVICE_STATUS);
1599     needed = 0xdeadbeef;
1600     returned = 0xdeadbeef;
1601     resume = 0;
1602     SetLastError(0xdeadbeef);
1603     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1604                                  (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1605     ok(!ret, "Expected failure\n");
1606     ok(needed != 0xdeadbeef && needed > 0, "Expected the needed buffer size\n");
1607     ok(returned < tempreturned, "Expected fewer services to be returned\n");
1608     todo_wine ok(resume, "Expected a resume handle\n");
1609     ok(GetLastError() == ERROR_MORE_DATA,
1610        "Expected ERROR_MORE_DATA, got %d\n", GetLastError());
1611
1612     /* Fetch that last service but pass a bigger buffer size */
1613     missing = tempreturned - returned;
1614     bufsize = tempneeded;
1615     needed = 0xdeadbeef;
1616     returned = 0xdeadbeef;
1617     SetLastError(0xdeadbeef);
1618     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1619                                  (BYTE*)exservices, bufsize, &needed, &returned, &resume, NULL);
1620     ok(ret, "Expected success, got error %u\n", GetLastError());
1621     ok(needed == 0, "Expected needed buffer to be 0 as we are done\n");
1622     ok(returned == missing, "Expected %u services to be returned\n", missing);
1623     ok(resume == 0, "Expected the resume handle to be 0\n");
1624     HeapFree(GetProcessHeap(), 0, exservices);
1625
1626     /* See if things add up */
1627
1628     /* Get the number of active win32 services */
1629     pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1630                            NULL, 0, &needed, &returned, NULL, NULL);
1631     exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1632     pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_ACTIVE,
1633                            (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1634     HeapFree(GetProcessHeap(), 0, exservices);
1635
1636     servicecountactive = returned;
1637
1638     /* Get the number of inactive win32 services */
1639     pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1640                            NULL, 0, &needed, &returned, NULL, NULL);
1641     exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1642     pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_INACTIVE,
1643                            (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1644     HeapFree(GetProcessHeap(), 0, exservices);
1645
1646     servicecountinactive = returned;
1647
1648     /* Get the number of win32 services */
1649     pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1650                            NULL, 0, &needed, &returned, NULL, NULL);
1651     exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1652     pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32, SERVICE_STATE_ALL,
1653                            (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1654     HeapFree(GetProcessHeap(), 0, exservices);
1655
1656     /* Check if total is the same as active and inactive win32 services */
1657     ok(returned == (servicecountactive + servicecountinactive),
1658        "Something wrong in the calculation\n");
1659
1660     /* Get all drivers and services */
1661     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1662                                  SERVICE_STATE_ALL, NULL, 0, &needed, &returned, NULL, NULL);
1663     ok(!ret, "Expected failure\n");
1664     exservices = HeapAlloc(GetProcessHeap(), 0, needed);
1665     ret = pEnumServicesStatusExA(scm_handle, 0, SERVICE_WIN32 | SERVICE_DRIVER,
1666                                  SERVICE_STATE_ALL, (BYTE*)exservices, needed, &needed, &returned, NULL, NULL);
1667     ok(ret, "Expected success %u\n", GetLastError());
1668
1669     /* Loop through all those returned drivers and services */
1670     for (i = 0; i < returned; i++)
1671     {
1672         SERVICE_STATUS_PROCESS status = exservices[i].ServiceStatusProcess;
1673
1674         /* lpServiceName and lpDisplayName should always be filled */
1675         ok(lstrlenA(exservices[i].lpServiceName) > 0, "Expected a service name\n");
1676         ok(lstrlenA(exservices[i].lpDisplayName) > 0, "Expected a display name\n");
1677
1678         /* Decrement the counters to see if the functions calls return the
1679          * same numbers as the contents of these structures.
1680          * Check some process id specifics.
1681          */
1682         if (status.dwServiceType & (SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER))
1683         {
1684             /* We shouldn't have a process id for drivers */
1685             ok(status.dwProcessId == 0,
1686                "This driver shouldn't have an associated process id\n");
1687         }
1688
1689         if (status.dwServiceType & (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS))
1690         {
1691             if (status.dwCurrentState != SERVICE_STOPPED)
1692             {
1693                 /* We expect a process id for every running service */
1694                 ok(status.dwProcessId > 0, "Expected a process id for this running service (%s)\n",
1695                    exservices[i].lpServiceName);
1696
1697                 servicecountactive--;
1698             }
1699             else
1700             {
1701                 /* We shouldn't have a process id for inactive services */
1702                 ok(status.dwProcessId == 0, "Service %s state %u shouldn't have an associated process id\n",
1703                    exservices[i].lpServiceName, status.dwCurrentState);
1704
1705                 servicecountinactive--;
1706             }
1707         }
1708     }
1709     HeapFree(GetProcessHeap(), 0, exservices);
1710
1711     ok(servicecountactive == 0, "Active services mismatch %u\n", servicecountactive);
1712     ok(servicecountinactive == 0, "Inactive services mismatch %u\n", servicecountinactive);
1713
1714     CloseServiceHandle(scm_handle);
1715 }
1716
1717 static void test_close(void)
1718 {
1719     SC_HANDLE handle;
1720     BOOL ret;
1721
1722     /* NULL handle */
1723     SetLastError(0xdeadbeef);
1724     ret = CloseServiceHandle(NULL);
1725     ok(!ret, "Expected failure\n");
1726     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1727
1728     /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
1729
1730     /* Proper call */
1731     handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
1732     SetLastError(0xdeadbeef);
1733     ret = CloseServiceHandle(handle);
1734     ok(ret, "Expected success got error %u\n", GetLastError());
1735 }
1736
1737 static void test_sequence(void)
1738 {
1739     SC_HANDLE scm_handle, svc_handle;
1740     BOOL ret;
1741     QUERY_SERVICE_CONFIGA *config;
1742     DWORD given, needed;
1743     static const CHAR servicename [] = "Winetest";
1744     static const CHAR displayname [] = "Winetest dummy service";
1745     static const CHAR displayname2[] = "Winetest dummy service (2)";
1746     static const CHAR pathname    [] = "we_dont_care.exe";
1747     static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1748     static const CHAR password    [] = "";
1749     static const CHAR empty       [] = "";
1750     static const CHAR localsystem [] = "LocalSystem";
1751
1752     SetLastError(0xdeadbeef);
1753     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1754
1755     if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1756     {
1757         skip("Not enough rights to get a handle to the manager\n");
1758         return;
1759     }
1760     else
1761         ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
1762
1763     if (!scm_handle) return;
1764
1765     /* Create a dummy service */
1766     SetLastError(0xdeadbeef);
1767     svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1768         SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1769         pathname, NULL, NULL, dependencies, NULL, password);
1770
1771     if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
1772     {
1773         /* We try and open the service and do the rest of the tests. Some could
1774          * fail if the tests were changed between these runs.
1775          */
1776         trace("Deletion probably didn't work last time\n");
1777         SetLastError(0xdeadbeef);
1778         svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1779         if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1780         {
1781             skip("Not enough rights to open the service\n");
1782             CloseServiceHandle(scm_handle);        
1783             return;
1784         }
1785         ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
1786     }
1787     else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1788     {
1789         skip("Not enough rights to create the service\n");
1790         CloseServiceHandle(scm_handle);        
1791         return;
1792     }
1793     else
1794     {
1795         ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1796         if ((svc_handle != NULL) && (pGetSecurityInfo != NULL))
1797         {
1798             PSID sidOwner, sidGroup;
1799             PACL dacl, sacl;
1800             PSECURITY_DESCRIPTOR pSD;
1801             HRESULT retval = pGetSecurityInfo(svc_handle,SE_SERVICE,DACL_SECURITY_INFORMATION,&sidOwner,&sidGroup,&dacl,&sacl,&pSD);
1802             todo_wine ok(ERROR_SUCCESS == retval, "Expected GetSecurityInfo to succeed: result %d\n",retval);
1803         }
1804     }
1805
1806     if (!svc_handle) return;
1807
1808     /* TODO:
1809      * Before we do a QueryServiceConfig we should check the registry. This will make sure
1810      * that the correct keys are used.
1811      */
1812
1813     /* Request the size for the buffer */
1814     SetLastError(0xdeadbeef);
1815     ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1816     ok(!ret, "Expected failure\n");
1817     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1818
1819     config = HeapAlloc(GetProcessHeap(), 0, needed);
1820     given = needed;
1821     SetLastError(0xdeadbeef);
1822     ret = QueryServiceConfigA(svc_handle, config, given, &needed);
1823     ok(ret, "Expected success, got error %u\n", GetLastError());
1824
1825     ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1826         config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1827     ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1828         "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1829     ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1830     ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
1831     ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1832     ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
1833     ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1834     /* TODO: Show the double 0 terminated string */
1835     todo_wine
1836     {
1837     ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
1838     }
1839     ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1840     ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
1841
1842     ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_ERROR_NORMAL, NULL, "TestGroup2",
1843                                NULL, NULL, NULL, NULL, displayname2);
1844     ok(ret, "ChangeServiceConfig failed (err=%d)\n", GetLastError());
1845
1846     QueryServiceConfigA(svc_handle, NULL, 0, &needed);
1847     config = HeapReAlloc(GetProcessHeap(), 0, config, needed);
1848     ok(QueryServiceConfigA(svc_handle, config, needed, &needed), "QueryServiceConfig failed\n");
1849     ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
1850         config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
1851     ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
1852         "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
1853     ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
1854     ok(config->dwErrorControl == SERVICE_ERROR_NORMAL, "Expected SERVICE_ERROR_NORMAL, got %d\n", config->dwErrorControl);
1855     ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
1856     ok(!strcmp(config->lpLoadOrderGroup, "TestGroup2"), "Expected 'TestGroup2', got '%s'\n", config->lpLoadOrderGroup);
1857     ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
1858     ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
1859     ok(!strcmp(config->lpDisplayName, displayname2), "Expected '%s', got '%s'\n", displayname2, config->lpDisplayName);
1860
1861     SetLastError(0xdeadbeef);
1862     ret = DeleteService(svc_handle);
1863     ok(ret, "Expected success, got error %u\n", GetLastError());
1864     CloseServiceHandle(svc_handle);
1865
1866     /* Wait a while. The following test does a CreateService again */
1867     Sleep(1000);
1868
1869     CloseServiceHandle(scm_handle);
1870     HeapFree(GetProcessHeap(), 0, config);
1871 }
1872
1873 static void test_queryconfig2(void)
1874 {
1875     SC_HANDLE scm_handle, svc_handle;
1876     BOOL ret;
1877     DWORD expected, needed;
1878     BYTE buffer[MAX_PATH];
1879     LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
1880     static const CHAR servicename [] = "Winetest";
1881     static const CHAR displayname [] = "Winetest dummy service";
1882     static const CHAR pathname    [] = "we_dont_care.exe";
1883     static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
1884     static const CHAR password    [] = "";
1885     static const CHAR description [] = "Description";
1886
1887     if(!pQueryServiceConfig2A)
1888     {
1889         win_skip("function QueryServiceConfig2A not present\n");
1890         return;
1891     }
1892
1893     SetLastError(0xdeadbeef);
1894     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1895
1896     if (!scm_handle)
1897     {
1898         if(GetLastError() == ERROR_ACCESS_DENIED)
1899             skip("Not enough rights to get a handle to the manager\n");
1900         else
1901             ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
1902         return;
1903     }
1904
1905     /* Create a dummy service */
1906     SetLastError(0xdeadbeef);
1907     svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
1908         SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
1909         pathname, NULL, NULL, dependencies, NULL, password);
1910
1911     if (!svc_handle)
1912     {
1913         if(GetLastError() == ERROR_SERVICE_EXISTS)
1914         {
1915             /* We try and open the service and do the rest of the tests. Some could
1916              * fail if the tests were changed between these runs.
1917              */
1918             trace("Deletion probably didn't work last time\n");
1919             SetLastError(0xdeadbeef);
1920             svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1921             if (!svc_handle)
1922             {
1923                 if(GetLastError() == ERROR_ACCESS_DENIED)
1924                     skip("Not enough rights to open the service\n");
1925                 else
1926                     ok(FALSE, "Could not open the service : %d\n", GetLastError());
1927                 CloseServiceHandle(scm_handle);
1928                 return;
1929             }
1930         }
1931         if (GetLastError() == ERROR_ACCESS_DENIED)
1932         {
1933             skip("Not enough rights to create the service\n");
1934             CloseServiceHandle(scm_handle);
1935             return;
1936         }
1937         ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
1938         if (!svc_handle)
1939         {
1940             CloseServiceHandle(scm_handle);
1941             return;
1942         }
1943     }
1944     SetLastError(0xdeadbeef);
1945     ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1946     ok(!ret, "expected QueryServiceConfig2A to fail\n");
1947     ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1948
1949     SetLastError(0xdeadbeef);
1950     ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1951     ok(!ret, "expected QueryServiceConfig2A to fail\n");
1952     ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
1953
1954     SetLastError(0xdeadbeef);
1955     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
1956     ok(!ret, "expected QueryServiceConfig2A to fail\n");
1957     ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1958
1959     SetLastError(0xdeadbeef);
1960     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
1961     ok(!ret, "expected QueryServiceConfig2A to fail\n");
1962     ok((ERROR_INVALID_ADDRESS == GetLastError()) || (ERROR_INSUFFICIENT_BUFFER == GetLastError()),
1963        "expected error ERROR_INVALID_ADDRESS or ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1964
1965     SetLastError(0xdeadbeef);
1966     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,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     needed = 0;
1971     SetLastError(0xdeadbeef);
1972     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA)-1,&needed);
1973     ok(!ret, "expected QueryServiceConfig2A to fail\n");
1974     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1975     ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1976
1977     needed = 0;
1978     pConfig->lpDescription = (LPSTR)0xdeadbeef;
1979     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
1980     ok(ret, "expected QueryServiceConfig2A to succeed\n");
1981     ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1982     ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
1983
1984     SetLastError(0xdeadbeef);
1985     needed = 0;
1986     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1987     ok(!ret, "expected QueryServiceConfig2A to fail\n");
1988     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1989     ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
1990
1991     if(!pChangeServiceConfig2A)
1992     {
1993         win_skip("function ChangeServiceConfig2A not present\n");
1994         goto cleanup;
1995     }
1996
1997     pConfig->lpDescription = (LPSTR) description;
1998     ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
1999     ok(ret, "ChangeServiceConfig2A failed\n");
2000     if (!ret) {
2001         goto cleanup;
2002     }
2003
2004     SetLastError(0xdeadbeef);
2005     needed = 0;
2006     expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
2007     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
2008     ok(!ret, "expected QueryServiceConfig2A to fail\n");
2009     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2010     ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2011
2012     SetLastError(0xdeadbeef);
2013     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
2014     ok(!ret, "expected QueryServiceConfig2A to fail\n");
2015     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2016
2017     SetLastError(0xdeadbeef);
2018     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
2019     ok(ret, "expected QueryServiceConfig2A to succeed\n");
2020     ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2021         "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2022
2023     SetLastError(0xdeadbeef);
2024     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
2025     ok(ret, "expected QueryServiceConfig2A to succeed\n");
2026     ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
2027         "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
2028
2029     if(!pQueryServiceConfig2W)
2030     {
2031         win_skip("function QueryServiceConfig2W not present\n");
2032         goto cleanup;
2033     }
2034     SetLastError(0xdeadbeef);
2035     needed = 0;
2036     expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
2037     ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
2038     ok(!ret, "expected QueryServiceConfig2W to fail\n");
2039     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
2040     ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
2041
2042     SetLastError(0xdeadbeef);
2043     ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
2044     ok(ret, "expected QueryServiceConfig2W to succeed\n");
2045
2046 cleanup:
2047     DeleteService(svc_handle);
2048
2049     CloseServiceHandle(svc_handle);
2050
2051     /* Wait a while. The following test does a CreateService again */
2052     Sleep(1000);
2053
2054     CloseServiceHandle(scm_handle);
2055 }
2056
2057 static DWORD try_start_stop(SC_HANDLE svc_handle, const char* name, int todo)
2058 {
2059     BOOL ret;
2060     DWORD needed, le1, le2;
2061     SERVICE_STATUS status;
2062     SERVICE_STATUS_PROCESS statusproc;
2063
2064     ret = StartServiceA(svc_handle, 0, NULL);
2065     le1 = GetLastError();
2066     ok(!ret, "%s: StartServiceA() should have failed\n", name);
2067
2068     ret = pQueryServiceStatusEx(svc_handle, SC_STATUS_PROCESS_INFO, (BYTE*)&statusproc, sizeof(statusproc), &needed);
2069     ok(ret, "%s: QueryServiceStatusEx() failed le=%u\n", name, GetLastError());
2070     todo_wine ok(statusproc.dwCurrentState == SERVICE_STOPPED, "%s: should be stopped state=%x\n", name, statusproc.dwCurrentState);
2071     todo_wine ok(statusproc.dwProcessId == 0, "%s: ProcessId should be 0 instead of %x\n", name, statusproc.dwProcessId);
2072
2073     ret = StartServiceA(svc_handle, 0, NULL);
2074     le2 = GetLastError();
2075     ok(!ret, "%s: StartServiceA() should have failed\n", name);
2076     if (todo)
2077         todo_wine ok(le2 == le1, "%s: the second try should yield the same error: %u != %u\n", name, le1, le2);
2078     else
2079         ok(le2 == le1, "%s: the second try should yield the same error: %u != %u\n", name, le1, le2);
2080
2081     ret = ControlService(svc_handle, SERVICE_CONTROL_STOP, &status);
2082     le2 = GetLastError();
2083     ok(!ret, "%s: ControlService() should have failed\n", name);
2084     todo_wine ok(le2 == ERROR_SERVICE_NOT_ACTIVE, "%s: %d != ERROR_SERVICE_NOT_ACTIVE\n", name, le2);
2085     todo_wine ok(status.dwCurrentState == SERVICE_STOPPED, "%s: should be stopped state=%x\n", name, status.dwCurrentState);
2086
2087     return le1;
2088 }
2089
2090 static void test_start_stop(void)
2091 {
2092     BOOL ret;
2093     SC_HANDLE scm_handle, svc_handle;
2094     DWORD le;
2095     static const char servicename[] = "Winetest";
2096     char cmd[MAX_PATH+20];
2097     const char* displayname;
2098
2099     SetLastError(0xdeadbeef);
2100     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2101     if (!scm_handle)
2102     {
2103         if(GetLastError() == ERROR_ACCESS_DENIED)
2104             skip("Not enough rights to get a handle to the manager\n");
2105         else
2106             ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
2107         return;
2108     }
2109
2110     /* Do some cleanup in case a previous run crashed */
2111     svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2112     if (svc_handle)
2113     {
2114         DeleteService(svc_handle);
2115         CloseServiceHandle(svc_handle);
2116     }
2117
2118     /* Create a dummy disabled service */
2119     sprintf(cmd, "\"%s\" service exit", selfname);
2120     displayname = "Winetest Disabled Service";
2121     svc_handle = CreateServiceA(scm_handle, servicename, displayname,
2122         GENERIC_ALL, SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS,
2123         SERVICE_DISABLED, SERVICE_ERROR_IGNORE, cmd, NULL,
2124         NULL, NULL, NULL, NULL);
2125     if (!svc_handle)
2126     {
2127         if(GetLastError() == ERROR_ACCESS_DENIED)
2128             skip("Not enough rights to create the service\n");
2129         else
2130             ok(FALSE, "Could not create the service: %d\n", GetLastError());
2131         goto cleanup;
2132     }
2133     le = try_start_stop(svc_handle, displayname, 1);
2134     todo_wine ok(le == ERROR_SERVICE_DISABLED, "%d != ERROR_SERVICE_DISABLED\n", le);
2135
2136     /* Then with a process that exits right away */
2137     displayname = "Winetest Exit Service";
2138     ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, displayname);
2139     ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2140     le = try_start_stop(svc_handle, displayname, 0);
2141     todo_wine ok(le == ERROR_SERVICE_REQUEST_TIMEOUT, "%d != ERROR_SERVICE_REQUEST_TIMEOUT\n", le);
2142
2143     /* Again with a bad path */
2144     displayname = "Winetest Bad Path";
2145     ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, "no_such_file.exe", NULL, NULL, NULL, NULL, NULL, displayname);
2146     ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2147     try_start_stop(svc_handle, displayname, 0);
2148
2149     /* And finally with a service that plays dead, forcing a timeout.
2150      * This time we will put no quotes. That should work too, even if there are
2151      * spaces in the path.
2152      */
2153     sprintf(cmd, "%s service sleep", selfname);
2154     displayname = "Winetest Sleep Service";
2155     ret = ChangeServiceConfigA(svc_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, cmd, NULL, NULL, NULL, NULL, NULL, displayname);
2156     ok(ret, "ChangeServiceConfig() failed le=%u\n", GetLastError());
2157
2158     le = try_start_stop(svc_handle, displayname, 0);
2159     todo_wine ok(le == ERROR_SERVICE_REQUEST_TIMEOUT, "%d != ERROR_SERVICE_REQUEST_TIMEOUT\n", le);
2160
2161 cleanup:
2162     if (svc_handle)
2163     {
2164         DeleteService(svc_handle);
2165         CloseServiceHandle(svc_handle);
2166     }
2167
2168     /* Wait a while. The following test does a CreateService again */
2169     Sleep(1000);
2170
2171     CloseServiceHandle(scm_handle);
2172 }
2173
2174 static void test_refcount(void)
2175 {
2176     SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
2177     static const CHAR servicename         [] = "Winetest";
2178     static const CHAR pathname            [] = "we_dont_care.exe";
2179     BOOL ret;
2180
2181     /* Get a handle to the Service Control Manager */
2182     SetLastError(0xdeadbeef);
2183     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2184     if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
2185     {
2186         skip("Not enough rights to get a handle to the manager\n");
2187         return;
2188     }
2189
2190     /* Create a service */
2191     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2192                                  SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2193                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2194     ok(svc_handle1 != NULL, "Expected success, got error %u\n", GetLastError());
2195
2196     /* Get a handle to this new service */
2197     svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2198     ok(svc_handle2 != NULL, "Expected success, got error %u\n", GetLastError());
2199
2200     /* Get another handle to this new service */
2201     svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
2202     ok(svc_handle3 != NULL, "Expected success, got error %u\n", GetLastError());
2203
2204     /* Check if we can close the handle to the Service Control Manager */
2205     ret = CloseServiceHandle(scm_handle);
2206     ok(ret, "Expected success (err=%d)\n", GetLastError());
2207
2208     /* Get a new handle to the Service Control Manager */
2209     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2210     ok(scm_handle != NULL, "Expected success, got error %u\n", GetLastError());
2211
2212     /* Get a handle to this new service */
2213     svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
2214     ok(svc_handle4 != NULL, "Expected success, got error %u\n", GetLastError());
2215
2216     /* Delete the service */
2217     ret = DeleteService(svc_handle4);
2218     ok(ret, "Expected success (err=%d)\n", GetLastError());
2219
2220     /* We cannot create the same service again as it's still marked as 'being deleted'.
2221      * The reason is that we still have 4 open handles to this service even though we
2222      * closed the handle to the Service Control Manager in between.
2223      */
2224     SetLastError(0xdeadbeef);
2225     svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2226                                  SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2227                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2228     todo_wine
2229     {
2230     ok(!svc_handle5, "Expected failure\n");
2231     ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
2232        "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
2233     }
2234
2235     /* FIXME: Remove this when Wine is fixed */
2236     if (svc_handle5)
2237     {
2238         DeleteService(svc_handle5);
2239         CloseServiceHandle(svc_handle5);
2240     }
2241
2242     /* Close all the handles to the service and try again */
2243     ret = CloseServiceHandle(svc_handle4);
2244     ok(ret, "Expected success (err=%d)\n", GetLastError());
2245     ret = CloseServiceHandle(svc_handle3);
2246     ok(ret, "Expected success (err=%d)\n", GetLastError());
2247     ret = CloseServiceHandle(svc_handle2);
2248     ok(ret, "Expected success (err=%d)\n", GetLastError());
2249     ret = CloseServiceHandle(svc_handle1);
2250     ok(ret, "Expected success (err=%d)\n", GetLastError());
2251
2252     /* Wait a while. Doing a CreateService too soon will result again
2253      * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
2254      */
2255     Sleep(1000);
2256
2257     /* We succeed now as all handles are closed (tested this also with a long SLeep() */
2258     svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
2259                                  SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
2260                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
2261     ok(svc_handle5 != NULL, "Expected success, got error %u\n", GetLastError());
2262
2263     /* Delete the service */
2264     ret = DeleteService(svc_handle5);
2265     ok(ret, "Expected success (err=%d)\n", GetLastError());
2266
2267     /* Wait a while. Just in case one of the following tests does a CreateService again */
2268     Sleep(1000);
2269
2270     CloseServiceHandle(svc_handle5);
2271     CloseServiceHandle(scm_handle);
2272 }
2273
2274 START_TEST(service)
2275 {
2276     SC_HANDLE scm_handle;
2277     int myARGC;
2278     char** myARGV;
2279
2280     myARGC = winetest_get_mainargs(&myARGV);
2281     selfname = myARGV[0];
2282     if (myARGC >= 3)
2283     {
2284         if (strcmp(myARGV[2], "sleep") == 0)
2285             /* Cause a service startup timeout */
2286             Sleep(90000);
2287         /* then, or if myARGV[2] == "exit", just exit */
2288         return;
2289     }
2290
2291     /* Bail out if we are on win98 */
2292     SetLastError(0xdeadbeef);
2293     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
2294
2295     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
2296     {
2297         win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
2298         return;
2299     }
2300     CloseServiceHandle(scm_handle);
2301
2302     init_function_pointers();
2303
2304     /* First some parameter checking */
2305     test_open_scm();
2306     test_open_svc();
2307     test_create_delete_svc();
2308     test_get_displayname();
2309     test_get_servicekeyname();
2310     test_query_svc();
2311     test_enum_svc();
2312     test_close();
2313     /* Test the creation, querying and deletion of a service */
2314     test_sequence();
2315     test_queryconfig2();
2316     test_start_stop();
2317     /* The main reason for this test is to check if any refcounting is used
2318      * and what the rules are
2319      */
2320     test_refcount();
2321 }