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