urlmon: Don't create stgmed_obj for binding to object.
[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 "lmcons.h"
30
31 #include "wine/test.h"
32
33 static const CHAR spooler[] = "Spooler"; /* Should be available on all platforms */
34
35 static void test_open_scm(void)
36 {
37     SC_HANDLE scm_handle;
38
39     /* No access rights */
40     SetLastError(0xdeadbeef);
41     scm_handle = OpenSCManagerA(NULL, NULL, 0);
42     ok(scm_handle != NULL, "Expected success\n");
43     ok(GetLastError() == ERROR_SUCCESS    /* W2K3, Vista */ ||
44        GetLastError() == 0xdeadbeef       /* NT4, XP */ ||
45        GetLastError() == ERROR_IO_PENDING /* W2K */,
46        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
47     CloseServiceHandle(scm_handle);
48
49     /* Unknown database name */
50     SetLastError(0xdeadbeef);
51     scm_handle = OpenSCManagerA(NULL, "DoesNotExist", SC_MANAGER_CONNECT);
52     ok(!scm_handle, "Expected failure\n");
53     ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
54     CloseServiceHandle(scm_handle); /* Just in case */
55
56     /* MSDN says only ServiceActive is allowed, or NULL */
57     SetLastError(0xdeadbeef);
58     scm_handle = OpenSCManagerA(NULL, SERVICES_FAILED_DATABASEA, SC_MANAGER_CONNECT);
59     ok(!scm_handle, "Expected failure\n");
60     ok(GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST, "Expected ERROR_DATABASE_DOES_NOT_EXIST, got %d\n", GetLastError());
61     CloseServiceHandle(scm_handle); /* Just in case */
62
63     /* Remote unknown host */
64     SetLastError(0xdeadbeef);
65     scm_handle = OpenSCManagerA("DOESNOTEXIST", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
66     ok(!scm_handle, "Expected failure\n");
67     todo_wine
68     ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE, "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
69     CloseServiceHandle(scm_handle); /* Just in case */
70
71     /* Proper call with an empty hostname */
72     SetLastError(0xdeadbeef);
73     scm_handle = OpenSCManagerA("", SERVICES_ACTIVE_DATABASEA, SC_MANAGER_CONNECT);
74     ok(scm_handle != NULL, "Expected success\n");
75     ok(GetLastError() == ERROR_SUCCESS          /* W2K3, Vista */ ||
76        GetLastError() == ERROR_ENVVAR_NOT_FOUND /* NT4 */ ||
77        GetLastError() == 0xdeadbeef             /* XP */ ||
78        GetLastError() == ERROR_IO_PENDING       /* W2K */,
79        "Expected ERROR_SUCCESS, ERROR_IO_PENDING, ERROR_ENVVAR_NOT_FOUND or 0xdeadbeef, got %d\n", GetLastError());
80     CloseServiceHandle(scm_handle);
81
82     /* Again a correct one */
83     SetLastError(0xdeadbeef);
84     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
85     ok(scm_handle != NULL, "Expected success\n");
86     ok(GetLastError() == ERROR_SUCCESS    /* W2K3, Vista */ ||
87        GetLastError() == 0xdeadbeef       /* NT4, XP */ ||
88        GetLastError() == ERROR_IO_PENDING /* W2K */,
89        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
90     CloseServiceHandle(scm_handle);
91 }
92
93 static void test_open_svc(void)
94 {
95     SC_HANDLE scm_handle, svc_handle;
96
97     /* All NULL (invalid access rights) */
98     SetLastError(0xdeadbeef);
99     svc_handle = OpenServiceA(NULL, NULL, 0);
100     ok(!svc_handle, "Expected failure\n");
101     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
102
103     /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
104
105     /* NULL service */
106     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
107     SetLastError(0xdeadbeef);
108     svc_handle = OpenServiceA(scm_handle, NULL, GENERIC_READ);
109     ok(!svc_handle, "Expected failure\n");
110     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, XP, W2K3, Vista */ ||
111        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
112        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
113
114     /* Nonexistent service */
115     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
116     SetLastError(0xdeadbeef);
117     svc_handle = OpenServiceA(scm_handle, "deadbeef", GENERIC_READ);
118     ok(!svc_handle, "Expected failure\n");
119     ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST, "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
120     CloseServiceHandle(scm_handle);
121
122     /* Proper SCM handle but different access rights */
123     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
124     SetLastError(0xdeadbeef);
125     svc_handle = OpenServiceA(scm_handle, "Spooler", GENERIC_WRITE);
126     if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
127         skip("Not enough rights to get a handle to the service\n");
128     else
129     {
130         ok(svc_handle != NULL, "Expected success\n");
131         ok(GetLastError() == ERROR_SUCCESS    /* W2K3, Vista */ ||
132            GetLastError() == ERROR_IO_PENDING /* W2K */ ||
133            GetLastError() == 0xdeadbeef       /* XP, NT4 */,
134            "Expected ERROR_SUCCESS or 0xdeadbeef, got %d\n", GetLastError());
135         CloseServiceHandle(svc_handle);
136     }
137     CloseServiceHandle(scm_handle);
138 }
139
140 static void test_create_delete_svc(void)
141 {
142     SC_HANDLE scm_handle, svc_handle1;
143     CHAR username[UNLEN + 1], domain[MAX_PATH];
144     DWORD user_size = UNLEN + 1;
145     CHAR account[UNLEN + 3];
146     static const CHAR servicename         [] = "Winetest";
147     static const CHAR pathname            [] = "we_dont_care.exe";
148     static const CHAR empty               [] = "";
149     static const CHAR password            [] = "secret";
150     BOOL spooler_exists = FALSE;
151     BOOL ret;
152     CHAR display[4096];
153     DWORD display_size = sizeof(display);
154
155     /* Get the username and turn it into an account to be used in some tests */
156     GetUserNameA(username, &user_size);
157     /* Get the domainname to cater for that situation */
158     if (GetEnvironmentVariableA("USERDOMAIN", domain, MAX_PATH))
159         sprintf(account, "%s\\%s", domain, username);
160     else
161         sprintf(account, ".\\%s", username);
162
163     /* All NULL */
164     SetLastError(0xdeadbeef);
165     svc_handle1 = CreateServiceA(NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
166     ok(!svc_handle1, "Expected failure\n");
167     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
168
169     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
170
171     /* Only a valid handle to the Service Control Manager */
172     SetLastError(0xdeadbeef);
173     svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
174     ok(!svc_handle1, "Expected failure\n");
175     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, W2K3, XP, Vista */ ||
176        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
177        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
178
179     /* Now with a servicename */
180     SetLastError(0xdeadbeef);
181     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
182     ok(!svc_handle1, "Expected failure\n");
183     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, W2K3, XP, Vista */ ||
184        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
185        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
186
187     /* Or just a binary name */
188     SetLastError(0xdeadbeef);
189     svc_handle1 = CreateServiceA(scm_handle, NULL, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
190     ok(!svc_handle1, "Expected failure\n");
191     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, W2K3, XP, Vista */ ||
192        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
193        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
194
195     /* Both servicename and binary name (We only have connect rights) */
196     SetLastError(0xdeadbeef);
197     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
198     ok(!svc_handle1, "Expected failure\n");
199     ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
200
201     /* They can even be empty at this stage of parameter checking */
202     SetLastError(0xdeadbeef);
203     svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
204     ok(!svc_handle1, "Expected failure\n");
205     ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
206
207     SetLastError(0xdeadbeef);
208     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
209     ok(!svc_handle1, "Expected failure\n");
210     ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
211
212     /* Open the Service Control Manager with minimal rights for creation
213      * (Verified with 'SC_MANAGER_ALL_ACCESS &~ SC_MANAGER_CREATE_SERVICE')
214      */
215     CloseServiceHandle(scm_handle);
216     SetLastError(0xdeadbeef);
217     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
218     if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
219     {
220         skip("Not enough rights to get a handle to the manager\n");
221         return;
222     }
223
224     /* TODO: It looks like account (ServiceStartName) and (maybe) password are checked at this place */
225
226     /* Empty strings for servicename and binary name are checked */
227     SetLastError(0xdeadbeef);
228     svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, pathname, NULL, NULL, NULL, NULL, NULL);
229     ok(!svc_handle1, "Expected failure\n");
230     ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
231
232     SetLastError(0xdeadbeef);
233     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
234     ok(!svc_handle1, "Expected failure\n");
235     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
236
237     SetLastError(0xdeadbeef);
238     svc_handle1 = CreateServiceA(scm_handle, empty, NULL, 0, 0, 0, 0, empty, NULL, NULL, NULL, NULL, NULL);
239     ok(!svc_handle1, "Expected failure\n");
240     ok(GetLastError() == ERROR_INVALID_NAME, "Expected ERROR_INVALID_NAME, got %d\n", GetLastError());
241
242     /* Valid call (as we will see later) except for the empty binary name (to proof it's indeed
243      * an ERROR_INVALID_PARAMETER)
244      */
245     SetLastError(0xdeadbeef);
246     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
247                                  SERVICE_DISABLED, 0, empty, NULL, NULL, NULL, NULL, NULL);
248     ok(!svc_handle1, "Expected failure\n");
249     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
250
251     /* Windows checks if the 'service type', 'access type' and the combination of them are valid, so let's test that */
252
253     /* Illegal (service-type, which is used as a mask can't have a mix. Except the one with
254      * SERVICE_INTERACTIVE_PROCESS which will be tested below in a valid call)
255      */
256     SetLastError(0xdeadbeef);
257     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS,
258                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
259     ok(!svc_handle1, "Expected failure\n");
260     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
261
262     /* Illegal (SERVICE_INTERACTIVE_PROCESS is only allowed with SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS) */
263     SetLastError(0xdeadbeef);
264     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_FILE_SYSTEM_DRIVER | SERVICE_INTERACTIVE_PROCESS,
265                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
266     ok(!svc_handle1, "Expected failure\n");
267     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
268
269     /* Illegal (this combination is only allowed when the LocalSystem account (ServiceStartName) is used)
270      * Not having a correct account would have resulted in an ERROR_INVALID_SERVICE_ACCOUNT.
271      */
272     SetLastError(0xdeadbeef);
273     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
274                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, account, password);
275     ok(!svc_handle1, "Expected failure\n");
276     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
277
278     /* Illegal (start-type is not a mask and should only be one of the possibilities)
279      * Remark : 'OR'-ing them could result in a valid possibility (but doesn't make sense as
280      * it's most likely not the wanted start-type)
281      */
282     SetLastError(0xdeadbeef);
283     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL, SERVICE_WIN32_OWN_PROCESS,
284                                  SERVICE_AUTO_START | SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
285     ok(!svc_handle1, "Expected failure\n");
286     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
287
288     /* Illegal (SERVICE_BOOT_START and SERVICE_SYSTEM_START are only allowed for driver services) */
289     SetLastError(0xdeadbeef);
290     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
291                                  SERVICE_BOOT_START, 0, pathname, NULL, NULL, NULL, NULL, NULL);
292     ok(!svc_handle1, "Expected failure\n");
293     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
294
295     /* The service already exists (check first, just in case) */
296     svc_handle1 = OpenServiceA(scm_handle, spooler, GENERIC_READ);
297     if (svc_handle1)
298     {
299         spooler_exists = TRUE;
300         CloseServiceHandle(svc_handle1);
301         SetLastError(0xdeadbeef);
302         svc_handle1 = CreateServiceA(scm_handle, spooler, NULL, 0, SERVICE_WIN32_OWN_PROCESS,
303                                      SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
304         ok(!svc_handle1, "Expected failure\n");
305         ok(GetLastError() == ERROR_SERVICE_EXISTS, "Expected ERROR_SERVICE_EXISTS, got %d\n", GetLastError());
306     }
307     else
308         skip("Spooler service doesn't exist\n");
309
310     /* To find an existing displayname we check the 'Spooler' service. Although the registry
311      * doesn't show DisplayName on NT4, this call will return a displayname which is equal
312      * to the servicename and can't be used as well for a new displayname.
313      */
314     if (spooler_exists)
315     {
316         ret = GetServiceDisplayNameA(scm_handle, spooler, display, &display_size);
317
318         if (!ret)
319             skip("Could not retrieve a displayname for the Spooler service\n");
320         else
321         {
322             svc_handle1 = CreateServiceA(scm_handle, servicename, display, 0, SERVICE_WIN32_OWN_PROCESS,
323                                          SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
324             ok(!svc_handle1, "Expected failure\n");
325             ok(GetLastError() == ERROR_DUPLICATE_SERVICE_NAME,
326                "Expected ERROR_DUPLICATE_SERVICE_NAME, got %d\n", GetLastError());
327         }
328     }
329     else
330         skip("Could not retrieve a displayname (Spooler service doesn't exist)\n");
331
332     /* Windows doesn't care about the access rights for creation (which makes
333      * sense as there is no service yet) as long as there are sufficient
334      * rights to the manager.
335      */
336     SetLastError(0xdeadbeef);
337     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, 0, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
338                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
339     ok(svc_handle1 != NULL, "Could not create the service : %d\n", GetLastError());
340     ok(GetLastError() == ERROR_SUCCESS    /* W2K3, Vista */ ||
341        GetLastError() == 0xdeadbeef       /* NT4, XP */ ||
342        GetLastError() == ERROR_IO_PENDING /* W2K */,
343        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
344
345     /* DeleteService however must have proper rights */
346     SetLastError(0xdeadbeef);
347     ret = DeleteService(svc_handle1);
348     ok(!ret, "Expected failure\n");
349     ok(GetLastError() == ERROR_ACCESS_DENIED,
350        "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
351
352     /* Open the service with minimal rights for deletion.
353      * (Verified with 'SERVICE_ALL_ACCESS &~ DELETE')
354      */
355     CloseServiceHandle(svc_handle1);
356     svc_handle1 = OpenServiceA(scm_handle, servicename, DELETE);
357
358     /* Now that we have the proper rights, we should be able to delete */
359     SetLastError(0xdeadbeef);
360     ret = DeleteService(svc_handle1);
361     ok(ret, "Expected success\n");
362     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
363        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */ ||
364        GetLastError() == ERROR_IO_PENDING /* W2K */,
365        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
366
367     CloseServiceHandle(svc_handle1);
368
369     CloseServiceHandle(scm_handle);
370
371     /* Wait a while. One of the following tests also does a CreateService for the
372      * same servicename and this would result in an ERROR_SERVICE_MARKED_FOR_DELETE
373      * error if we do this to quick. Vista seems more picky then the others.
374      */
375     Sleep(1000);
376
377     /* And a final NULL check */
378     SetLastError(0xdeadbeef);
379     ret = DeleteService(NULL);
380     ok(!ret, "Expected failure\n");
381     ok(GetLastError() == ERROR_INVALID_HANDLE,
382         "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
383 }
384
385 static void test_get_displayname(void)
386 {
387     SC_HANDLE scm_handle, svc_handle;
388     BOOL ret;
389     CHAR displayname[4096];
390     WCHAR displaynameW[2048];
391     DWORD displaysize, tempsize, tempsizeW;
392     static const CHAR deadbeef[] = "Deadbeef";
393     static const WCHAR spoolerW[] = {'S','p','o','o','l','e','r',0};
394     static const CHAR servicename[] = "Winetest";
395     static const CHAR pathname[] = "we_dont_care.exe";
396
397     /* Having NULL for the size of the buffer will crash on W2K3 */
398
399     SetLastError(0xdeadbeef);
400     ret = GetServiceDisplayNameA(NULL, NULL, NULL, &displaysize);
401     ok(!ret, "Expected failure\n");
402     ok(GetLastError() == ERROR_INVALID_HANDLE,
403        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
404
405     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
406
407     SetLastError(0xdeadbeef);
408     ret = GetServiceDisplayNameA(scm_handle, NULL, NULL, &displaysize);
409     ok(!ret, "Expected failure\n");
410     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, XP, W2K3, Vista */ ||
411        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
412        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
413
414     SetLastError(0xdeadbeef);
415     displaysize = sizeof(displayname);
416     ret = GetServiceDisplayNameA(scm_handle, NULL, displayname, &displaysize);
417     ok(!ret, "Expected failure\n");
418     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, XP, W2K3, Vista */ ||
419        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
420        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
421
422     /* Test for nonexistent service */
423     SetLastError(0xdeadbeef);
424     displaysize = -1;
425     ret = GetServiceDisplayNameA(scm_handle, deadbeef, NULL, &displaysize);
426     ok(!ret, "Expected failure\n");
427     ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
428        "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
429
430     /* Check if 'Spooler' exists */
431     svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
432     if (!svc_handle)
433     {
434         skip("Spooler service doesn't exist\n");
435         CloseServiceHandle(scm_handle);
436         return;
437     }
438     CloseServiceHandle(svc_handle);
439
440     /* Retrieve the needed size for the buffer */
441     SetLastError(0xdeadbeef);
442     displaysize = -1;
443     ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
444     ok(!ret, "Expected failure\n");
445     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
446        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
447     tempsize = displaysize;
448
449     displaysize = 0;
450     ret = GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
451     ok(!ret, "Expected failure\n");
452     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
453        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
454     ok(displaysize == tempsize, "Buffer size mismatch (%d vs %d)\n", tempsize, displaysize);
455
456     /* Buffer is too small */
457     SetLastError(0xdeadbeef);
458     displaysize = (tempsize / 2);
459     ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
460     ok(!ret, "Expected failure\n");
461     ok(displaysize == tempsize, "Expected the needed buffersize\n");
462     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
463        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
464
465     /* First try with a buffer that should be big enough to hold
466      * the ANSI string (and terminating character). This succeeds on Windows
467      *  although when asked (see above 2 tests) it will return twice the needed size.
468      */
469     SetLastError(0xdeadbeef);
470     displaysize = (tempsize / 2) + 1;
471     ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
472     ok(ret, "Expected success\n");
473     ok(displaysize == ((tempsize / 2) + 1), "Expected no change for the needed buffer size\n");
474     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
475        GetLastError() == ERROR_IO_PENDING /* W2K */ ||
476        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */,
477        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
478
479     /* Now with the original returned size */
480     SetLastError(0xdeadbeef);
481     displaysize = tempsize;
482     ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
483     ok(ret, "Expected success\n");
484     ok(displaysize == tempsize, "Expected no change for the needed buffer size\n");
485     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
486        GetLastError() == ERROR_IO_PENDING /* W2K */ ||
487        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */,
488        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
489
490     /* And with a bigger than needed buffer */
491     SetLastError(0xdeadbeef);
492     displaysize = tempsize * 2;
493     ret = GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
494     ok(ret, "Expected success\n");
495     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
496        GetLastError() == ERROR_IO_PENDING /* W2K */ ||
497        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */,
498        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
499     /* Test that shows that if the buffersize is enough, it's not changed */
500     ok(displaysize == tempsize * 2, "Expected no change for the needed buffer size\n");
501     ok(lstrlen(displayname) == tempsize/2,
502        "Expected the buffer to be twice the length of the string\n") ;
503
504     /* Do the buffer(size) tests also for GetServiceDisplayNameW */
505     SetLastError(0xdeadbeef);
506     displaysize = -1;
507     ret = GetServiceDisplayNameW(scm_handle, spoolerW, NULL, &displaysize);
508     ok(!ret, "Expected failure\n");
509     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
510        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
511
512     /* Buffer is too small */
513     SetLastError(0xdeadbeef);
514     tempsizeW = displaysize;
515     displaysize = tempsizeW / 2;
516     ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
517     ok(!ret, "Expected failure\n");
518     ok(displaysize = tempsizeW, "Expected the needed buffersize\n");
519     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
520        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
521
522     /* Now with the original returned size */
523     SetLastError(0xdeadbeef);
524     displaysize = tempsizeW;
525     ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
526     ok(!ret, "Expected failure\n");
527     ok(displaysize = tempsizeW, "Expected the needed buffersize\n");
528     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
529        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
530
531     /* And with a bigger than needed buffer */
532     SetLastError(0xdeadbeef);
533     displaysize = tempsizeW + 1; /* This caters for the null terminating character */
534     ret = GetServiceDisplayNameW(scm_handle, spoolerW, displaynameW, &displaysize);
535     ok(ret, "Expected success\n");
536     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
537        GetLastError() == ERROR_IO_PENDING /* W2K */ ||
538        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */,
539        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
540     ok(displaysize == tempsizeW, "Expected the needed buffersize\n");
541     ok(lstrlenW(displaynameW) == displaysize,
542        "Expected the buffer to be the length of the string\n") ;
543     ok(tempsize / 2 == tempsizeW,
544        "Expected the needed buffersize (in bytes) to be the same for the A and W call\n");
545
546     CloseServiceHandle(scm_handle);
547
548     /* Test for a service without a displayname (which is valid). This should return
549      * the servicename itself.
550      */
551     SetLastError(0xdeadbeef);
552     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
553     if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
554     {
555         skip("Not enough rights to get a handle to the manager\n");
556         return;
557     }
558
559     SetLastError(0xdeadbeef);
560     svc_handle = CreateServiceA(scm_handle, servicename, NULL, DELETE,
561                                 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
562                                 SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
563     ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
564     if (!svc_handle)
565     {
566         CloseServiceHandle(scm_handle);
567         return;
568     }
569
570     /* Retrieve the needed size for the buffer */
571     SetLastError(0xdeadbeef);
572     displaysize = -1;
573     ret = GetServiceDisplayNameA(scm_handle, servicename, NULL, &displaysize);
574     ok(!ret, "Expected failure\n");
575     ok(displaysize == lstrlen(servicename) * 2,
576        "Expected the displaysize to be twice the size of the servicename\n");
577     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
578        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
579
580     /* Buffer is too small */
581     SetLastError(0xdeadbeef);
582     tempsize = displaysize;
583     displaysize = (tempsize / 2);
584     ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
585     ok(!ret, "Expected failure\n");
586     ok(displaysize == tempsize, "Expected the needed buffersize\n");
587     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
588        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
589
590     /* Get the displayname */
591     SetLastError(0xdeadbeef);
592     ret = GetServiceDisplayNameA(scm_handle, servicename, displayname, &displaysize);
593     ok(ret, "Expected success\n");
594     ok(!lstrcmpi(displayname, servicename),
595        "Expected displayname to be %s, got %s\n", servicename, displayname);
596     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
597        GetLastError() == ERROR_IO_PENDING /* W2K */ ||
598        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */,
599        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
600
601     /* Delete the service */
602     ret = DeleteService(svc_handle);
603     ok(ret, "Expected success\n");
604
605     CloseServiceHandle(svc_handle);
606     CloseServiceHandle(scm_handle);
607
608     /* Wait a while. Just in case one of the following tests does a CreateService again */
609     Sleep(1000);
610 }
611
612 static void test_get_servicekeyname(void)
613 {
614     SC_HANDLE scm_handle, svc_handle;
615     CHAR servicename[4096];
616     CHAR displayname[4096];
617     DWORD servicesize, displaysize, tempsize;
618     BOOL ret;
619     static const CHAR deadbeef[] = "Deadbeef";
620
621     /* Having NULL for the size of the buffer will crash on W2K3 */
622
623     SetLastError(0xdeadbeef);
624     ret = GetServiceKeyNameA(NULL, NULL, NULL, &servicesize);
625     ok(!ret, "Expected failure\n");
626     todo_wine
627     ok(GetLastError() == ERROR_INVALID_HANDLE,
628        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
629
630     scm_handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
631
632     SetLastError(0xdeadbeef);
633     ret = GetServiceKeyNameA(scm_handle, NULL, NULL, &servicesize);
634     ok(!ret, "Expected failure\n");
635     todo_wine
636     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, XP, W2K3, Vista */ ||
637        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
638        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
639
640     /* Valid handle and buffer but no displayname */
641     SetLastError(0xdeadbeef);
642     ret = GetServiceKeyNameA(scm_handle, NULL, servicename, &servicesize);
643     ok(!ret, "Expected failure\n");
644     todo_wine
645     ok(GetLastError() == ERROR_INVALID_ADDRESS   /* W2K, XP, W2K3, Vista */ ||
646        GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
647        "Expected ERROR_INVALID_ADDRESS or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
648
649     /* Test for nonexistent displayname */
650     SetLastError(0xdeadbeef);
651     ret = GetServiceKeyNameA(scm_handle, deadbeef, NULL, &servicesize);
652     ok(!ret, "Expected failure\n");
653     todo_wine
654     ok(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST,
655        "Expected ERROR_SERVICE_DOES_NOT_EXIST, got %d\n", GetLastError());
656
657     /* Check if 'Spooler' exists */
658     svc_handle = OpenServiceA(scm_handle, spooler, GENERIC_READ);
659     if (!svc_handle)
660     {
661         skip("Spooler service doesn't exist\n");
662         CloseServiceHandle(scm_handle);
663         return;
664     }
665     CloseServiceHandle(svc_handle);
666
667     /* Get the displayname for the 'Spooler' service */
668     GetServiceDisplayNameA(scm_handle, spooler, NULL, &displaysize);
669     GetServiceDisplayNameA(scm_handle, spooler, displayname, &displaysize);
670
671     /* Retrieve the needed size for the buffer */
672     SetLastError(0xdeadbeef);
673     servicesize = 0;
674     ret = GetServiceKeyNameA(scm_handle, displayname, NULL, &servicesize);
675     ok(!ret, "Expected failure\n");
676     todo_wine
677     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
678        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
679
680     /* Valid call with the correct buffersize */
681     SetLastError(0xdeadbeef);
682     tempsize = servicesize;
683     servicesize *= 2;
684     ret = GetServiceKeyNameA(scm_handle, displayname, servicename, &servicesize);
685     todo_wine
686     ok(ret, "Expected success\n");
687     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
688        GetLastError() == ERROR_IO_PENDING /* W2K */ ||
689        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */,
690        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
691     if (ret)
692     {
693         ok(lstrlen(servicename) == tempsize/2,
694            "Expected the buffer to be twice the length of the string\n") ;
695         ok(!lstrcmpi(servicename, spooler), "Expected %s, got %s\n", spooler, servicename);
696     }
697
698     CloseServiceHandle(scm_handle);
699 }
700
701 static void test_close(void)
702 {
703     SC_HANDLE handle;
704     BOOL ret;
705
706     /* NULL handle */
707     SetLastError(0xdeadbeef);
708     ret = CloseServiceHandle(NULL);
709     todo_wine
710     {
711     ok(!ret, "Expected failure\n");
712     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
713     }
714
715     /* TODO: Add some tests with invalid handles. These produce errors on Windows but crash on Wine */
716
717     /* Proper call */
718     handle = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
719     SetLastError(0xdeadbeef);
720     ret = CloseServiceHandle(handle);
721     ok(ret, "Expected success\n");
722     ok(GetLastError() == ERROR_IO_PENDING /* W2K */ ||
723        GetLastError() == ERROR_SUCCESS    /* W2K3 */ ||
724        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */,
725        "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
726 }
727
728 static void test_sequence(void)
729 {
730     SC_HANDLE scm_handle, svc_handle;
731     BOOL ret;
732     QUERY_SERVICE_CONFIGA *config;
733     DWORD given, needed;
734     static const CHAR servicename [] = "Winetest";
735     static const CHAR displayname [] = "Winetest dummy service";
736     static const CHAR pathname    [] = "we_dont_care.exe";
737     static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
738     static const CHAR password    [] = "";
739     static const CHAR empty       [] = "";
740     static const CHAR localsystem [] = "LocalSystem";
741
742     SetLastError(0xdeadbeef);
743     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
744
745     if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
746     {
747         skip("Not enough rights to get a handle to the manager\n");
748         return;
749     }
750     else
751         ok(scm_handle != NULL, "Could not get a handle to the manager: %d\n", GetLastError());
752
753     if (!scm_handle) return;
754
755     /* Create a dummy service */
756     SetLastError(0xdeadbeef);
757     svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
758         SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
759         pathname, NULL, NULL, dependencies, NULL, password);
760
761     if (!svc_handle && (GetLastError() == ERROR_SERVICE_EXISTS))
762     {
763         /* We try and open the service and do the rest of the tests. Some could
764          * fail if the tests were changed between these runs.
765          */
766         trace("Deletion probably didn't work last time\n");
767         SetLastError(0xdeadbeef);
768         svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
769         if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
770         {
771             skip("Not enough rights to open the service\n");
772             CloseServiceHandle(scm_handle);        
773             return;
774         }
775         ok(svc_handle != NULL, "Could not open the service : %d\n", GetLastError());
776     }
777     else if (!svc_handle && (GetLastError() == ERROR_ACCESS_DENIED))
778     {
779         skip("Not enough rights to create the service\n");
780         CloseServiceHandle(scm_handle);        
781         return;
782     }
783     else
784         ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
785
786     if (!svc_handle) return;
787
788     /* TODO:
789      * Before we do a QueryServiceConfig we should check the registry. This will make sure
790      * that the correct keys are used.
791      */
792
793     /* Request the size for the buffer */
794     SetLastError(0xdeadbeef);
795     ret = QueryServiceConfigA(svc_handle, NULL, 0, &needed);
796     ok(!ret, "Expected failure\n");
797     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
798
799     config = HeapAlloc(GetProcessHeap(), 0, needed);
800     given = needed;
801     SetLastError(0xdeadbeef);
802     ret = QueryServiceConfigA(svc_handle, config, given, &needed);
803     ok(ret, "Expected success\n");
804     todo_wine
805     {
806     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */||
807        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */ ||
808        GetLastError() == ERROR_IO_PENDING /* W2K */,
809         "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
810     ok(given == needed, "Expected the given (%d) and needed (%d) buffersizes to be equal\n", given, needed);
811     }
812     ok(config->lpBinaryPathName && config->lpLoadOrderGroup && config->lpDependencies && config->lpServiceStartName &&
813         config->lpDisplayName, "Expected all string struct members to be non-NULL\n");
814     ok(config->dwServiceType == (SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS),
815         "Expected SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, got %d\n", config->dwServiceType);
816     ok(config->dwStartType == SERVICE_DISABLED, "Expected SERVICE_DISABLED, got %d\n", config->dwStartType);
817     ok(config->dwErrorControl == SERVICE_ERROR_IGNORE, "Expected SERVICE_ERROR_IGNORE, got %d\n", config->dwErrorControl);
818     ok(!strcmp(config->lpBinaryPathName, pathname), "Expected '%s', got '%s'\n", pathname, config->lpBinaryPathName);
819     ok(!strcmp(config->lpLoadOrderGroup, empty), "Expected an empty string, got '%s'\n", config->lpLoadOrderGroup);
820     ok(config->dwTagId == 0, "Expected 0, got %d\n", config->dwTagId);
821     /* TODO: Show the double 0 terminated string */
822     todo_wine
823     {
824     ok(!memcmp(config->lpDependencies, dependencies, sizeof(dependencies)), "Wrong string\n");
825     }
826     ok(!strcmp(config->lpServiceStartName, localsystem), "Expected 'LocalSystem', got '%s'\n", config->lpServiceStartName);
827     ok(!strcmp(config->lpDisplayName, displayname), "Expected '%s', got '%s'\n", displayname, config->lpDisplayName);
828     
829     SetLastError(0xdeadbeef);
830     ret = DeleteService(svc_handle);
831     ok(ret, "Expected success\n");
832     ok(GetLastError() == ERROR_SUCCESS    /* W2K3 */||
833        GetLastError() == 0xdeadbeef       /* NT4, XP, Vista */ ||
834        GetLastError() == ERROR_IO_PENDING /* W2K */,
835         "Expected ERROR_SUCCESS, ERROR_IO_PENDING or 0xdeadbeef, got %d\n", GetLastError());
836     
837     CloseServiceHandle(svc_handle);
838
839     /* Wait a while. The following test does a CreateService again */
840     Sleep(1000);
841
842     CloseServiceHandle(scm_handle);
843     HeapFree(GetProcessHeap(), 0, config);
844 }
845
846 static void test_queryconfig2(void)
847 {
848     SC_HANDLE scm_handle, svc_handle;
849     BOOL ret;
850     DWORD expected, needed;
851     BYTE buffer[MAX_PATH];
852     LPSERVICE_DESCRIPTIONA pConfig = (LPSERVICE_DESCRIPTIONA)buffer;
853     static const CHAR servicename [] = "Winetest";
854     static const CHAR displayname [] = "Winetest dummy service";
855     static const CHAR pathname    [] = "we_dont_care.exe";
856     static const CHAR dependencies[] = "Master1\0Master2\0+MasterGroup1\0";
857     static const CHAR password    [] = "";
858     static const CHAR description [] = "Description";
859     HMODULE dllhandle = GetModuleHandleA("advapi32.dll");
860     BOOL (WINAPI *pChangeServiceConfig2A)(SC_HANDLE,DWORD,LPVOID)
861             = (void*)GetProcAddress(dllhandle, "ChangeServiceConfig2A");
862     BOOL (WINAPI *pQueryServiceConfig2A)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD)
863             = (void*)GetProcAddress(dllhandle, "QueryServiceConfig2A");
864     BOOL (WINAPI *pQueryServiceConfig2W)(SC_HANDLE,DWORD,LPBYTE,DWORD,LPDWORD)
865             = (void*)GetProcAddress(dllhandle, "QueryServiceConfig2W");
866     if(!pQueryServiceConfig2A)
867     {
868         skip("function QueryServiceConfig2A not present\n");
869         return;
870     }
871
872     SetLastError(0xdeadbeef);
873     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
874
875     if (!scm_handle)
876     {
877         if(GetLastError() == ERROR_ACCESS_DENIED)
878             skip("Not enough rights to get a handle to the manager\n");
879         else
880             ok(FALSE, "Could not get a handle to the manager: %d\n", GetLastError());
881         return;
882     }
883
884     /* Create a dummy service */
885     SetLastError(0xdeadbeef);
886     svc_handle = CreateServiceA(scm_handle, servicename, displayname, GENERIC_ALL,
887         SERVICE_INTERACTIVE_PROCESS | SERVICE_WIN32_OWN_PROCESS, SERVICE_DISABLED, SERVICE_ERROR_IGNORE,
888         pathname, NULL, NULL, dependencies, NULL, password);
889
890     if (!svc_handle)
891     {
892         if(GetLastError() == ERROR_SERVICE_EXISTS)
893         {
894             /* We try and open the service and do the rest of the tests. Some could
895              * fail if the tests were changed between these runs.
896              */
897             trace("Deletion probably didn't work last time\n");
898             SetLastError(0xdeadbeef);
899             svc_handle = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
900             if (!svc_handle)
901             {
902                 if(GetLastError() == ERROR_ACCESS_DENIED)
903                     skip("Not enough rights to open the service\n");
904                 else
905                     ok(FALSE, "Could not open the service : %d\n", GetLastError());
906                 CloseServiceHandle(scm_handle);
907                 return;
908             }
909         }
910         if (GetLastError() == ERROR_ACCESS_DENIED)
911         {
912             skip("Not enough rights to create the service\n");
913             CloseServiceHandle(scm_handle);
914             return;
915         }
916         ok(svc_handle != NULL, "Could not create the service : %d\n", GetLastError());
917         if (!svc_handle)
918         {
919             CloseServiceHandle(scm_handle);
920             return;
921         }
922     }
923     SetLastError(0xdeadbeef);
924     ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
925     ok(!ret, "expected QueryServiceConfig2A to fail\n");
926     ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
927
928     SetLastError(0xdeadbeef);
929     ret = pQueryServiceConfig2A(svc_handle,0xfff0,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
930     ok(!ret, "expected QueryServiceConfig2A to fail\n");
931     ok(ERROR_INVALID_LEVEL == GetLastError(), "expected error ERROR_INVALID_LEVEL, got %d\n", GetLastError());
932
933     SetLastError(0xdeadbeef);
934     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),NULL);
935     ok(!ret, "expected QueryServiceConfig2A to fail\n");
936     ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
937
938     SetLastError(0xdeadbeef);
939     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),&needed);
940     ok(!ret, "expected QueryServiceConfig2A to fail\n");
941     ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
942
943     SetLastError(0xdeadbeef);
944     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,sizeof(SERVICE_DESCRIPTIONA),NULL);
945     ok(!ret, "expected QueryServiceConfig2A to fail\n");
946     ok(ERROR_INVALID_ADDRESS == GetLastError(), "expected error ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
947
948     needed = 0;
949     pConfig->lpDescription = (LPSTR)0xdeadbeef;
950     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
951     ok(ret, "expected QueryServiceConfig2A to succeed\n");
952     ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
953     ok(!pConfig->lpDescription, "expected lpDescription to be NULL, got %p\n", pConfig->lpDescription);
954
955     SetLastError(0xdeadbeef);
956     needed = 0;
957     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
958     ok(!ret, "expected QueryServiceConfig2A to fail\n");
959     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
960     ok(needed == sizeof(SERVICE_DESCRIPTIONA), "got %d\n", needed);
961
962     if(!pChangeServiceConfig2A)
963     {
964         skip("function ChangeServiceConfig2A not present\n");
965         goto cleanup;
966     }
967
968     pConfig->lpDescription = (LPSTR) description;
969     ret = pChangeServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer);
970     ok(ret, "ChangeServiceConfig2A failed\n");
971     if (!ret) {
972         goto cleanup;
973     }
974
975     SetLastError(0xdeadbeef);
976     needed = 0;
977     expected = sizeof(SERVICE_DESCRIPTIONA) + sizeof(description) * sizeof(WCHAR); /* !! */
978     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,sizeof(SERVICE_DESCRIPTIONA),&needed);
979     ok(!ret, "expected QueryServiceConfig2A to fail\n");
980     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
981     ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
982
983     SetLastError(0xdeadbeef);
984     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed-1,&needed);
985     ok(!ret, "expected QueryServiceConfig2A to fail\n");
986     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
987
988     SetLastError(0xdeadbeef);
989     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer,needed,&needed);
990     ok(ret, "expected QueryServiceConfig2A to succeed\n");
991     ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
992         "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
993
994     SetLastError(0xdeadbeef);
995     ret = pQueryServiceConfig2A(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed + 1,&needed);
996     ok(ret, "expected QueryServiceConfig2A to succeed\n");
997     ok(pConfig->lpDescription && !strcmp(description,pConfig->lpDescription),
998         "expected lpDescription to be %s, got %s\n",description ,pConfig->lpDescription);
999
1000     if(!pQueryServiceConfig2W)
1001     {
1002         skip("function QueryServiceConfig2W not present\n");
1003         goto cleanup;
1004     }
1005     SetLastError(0xdeadbeef);
1006     needed = 0;
1007     expected = sizeof(SERVICE_DESCRIPTIONW) + sizeof(WCHAR) * sizeof(description);
1008     ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,NULL,0,&needed);
1009     ok(!ret, "expected QueryServiceConfig2W to fail\n");
1010     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(), "expected error ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
1011     ok(needed == expected, "expected needed to be %d, got %d\n", expected, needed);
1012
1013     SetLastError(0xdeadbeef);
1014     ret = pQueryServiceConfig2W(svc_handle, SERVICE_CONFIG_DESCRIPTION,buffer, needed,&needed);
1015     ok(ret, "expected QueryServiceConfig2W to succeed\n");
1016
1017 cleanup:
1018     DeleteService(svc_handle);
1019
1020     CloseServiceHandle(svc_handle);
1021
1022     /* Wait a while. The following test does a CreateService again */
1023     Sleep(1000);
1024
1025     CloseServiceHandle(scm_handle);
1026 }
1027
1028 static void test_refcount(void)
1029 {
1030     SC_HANDLE scm_handle, svc_handle1, svc_handle2, svc_handle3, svc_handle4, svc_handle5;
1031     static const CHAR servicename         [] = "Winetest";
1032     static const CHAR pathname            [] = "we_dont_care.exe";
1033     BOOL ret;
1034
1035     /* Get a handle to the Service Control Manager */
1036     SetLastError(0xdeadbeef);
1037     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1038     if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
1039     {
1040         skip("Not enough rights to get a handle to the manager\n");
1041         return;
1042     }
1043
1044     /* Create a service */
1045     svc_handle1 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
1046                                  SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
1047                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
1048     ok(svc_handle1 != NULL, "Expected success\n");
1049
1050     /* Get a handle to this new service */
1051     svc_handle2 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
1052     ok(svc_handle2 != NULL, "Expected success\n");
1053
1054     /* Get another handle to this new service */
1055     svc_handle3 = OpenServiceA(scm_handle, servicename, GENERIC_READ);
1056     ok(svc_handle3 != NULL, "Expected success\n");
1057
1058     /* Check if we can close the handle to the Service Control Manager */
1059     ret = CloseServiceHandle(scm_handle);
1060     ok(ret, "Expected success\n");
1061
1062     /* Get a new handle to the Service Control Manager */
1063     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1064     ok(scm_handle != NULL, "Expected success\n");
1065
1066     /* Get a handle to this new service */
1067     svc_handle4 = OpenServiceA(scm_handle, servicename, GENERIC_ALL);
1068     ok(svc_handle4 != NULL, "Expected success\n");
1069
1070     /* Delete the service */
1071     ret = DeleteService(svc_handle4);
1072     ok(ret, "Expected success\n");
1073
1074     /* We cannot create the same service again as it's still marked as 'being deleted'.
1075      * The reason is that we still have 4 open handles to this service even though we
1076      * closed the handle to the Service Control Manager in between.
1077      */
1078     SetLastError(0xdeadbeef);
1079     svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
1080                                  SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
1081                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
1082     todo_wine
1083     {
1084     ok(!svc_handle5, "Expected failure\n");
1085     ok(GetLastError() == ERROR_SERVICE_MARKED_FOR_DELETE,
1086        "Expected ERROR_SERVICE_MARKED_FOR_DELETE, got %d\n", GetLastError());
1087     }
1088
1089     /* FIXME: Remove this when Wine is fixed */
1090     if (svc_handle5)
1091     {
1092         DeleteService(svc_handle5);
1093         CloseServiceHandle(svc_handle5);
1094     }
1095
1096     /* Close all the handles to the service and try again */
1097     ret = CloseServiceHandle(svc_handle4);
1098     ok(ret, "Expected success\n");
1099     ret = CloseServiceHandle(svc_handle3);
1100     ok(ret, "Expected success\n");
1101     ret = CloseServiceHandle(svc_handle2);
1102     ok(ret, "Expected success\n");
1103     ret = CloseServiceHandle(svc_handle1);
1104     ok(ret, "Expected success\n");
1105
1106     /* Wait a while. Doing a CreateService too soon will result again
1107      * in an ERROR_SERVICE_MARKED_FOR_DELETE error.
1108      */
1109     Sleep(1000);
1110
1111     /* We succeed now as all handles are closed (tested this also with a long SLeep() */
1112     svc_handle5 = CreateServiceA(scm_handle, servicename, NULL, GENERIC_ALL,
1113                                  SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
1114                                  SERVICE_DISABLED, 0, pathname, NULL, NULL, NULL, NULL, NULL);
1115     ok(svc_handle5 != NULL, "Expected success\n");
1116
1117     /* Delete the service */
1118     ret = DeleteService(svc_handle5);
1119     ok(ret, "Expected success\n");
1120
1121     /* Wait a while. Just in case one of the following tests does a CreateService again */
1122     Sleep(1000);
1123
1124     CloseServiceHandle(svc_handle5);
1125     CloseServiceHandle(scm_handle);
1126 }
1127
1128 START_TEST(service)
1129 {
1130     SC_HANDLE scm_handle;
1131
1132     /* Bail out if we are on win98 */
1133     SetLastError(0xdeadbeef);
1134     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
1135
1136     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
1137     {
1138         skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
1139         return;
1140     }
1141     CloseServiceHandle(scm_handle);
1142
1143     /* First some parameter checking */
1144     test_open_scm();
1145     test_open_svc();
1146     test_create_delete_svc();
1147     test_get_displayname();
1148     test_get_servicekeyname();
1149     test_close();
1150     /* Test the creation, querying and deletion of a service */
1151     test_sequence();
1152     test_queryconfig2();
1153     /* The main reason for this test is to check if any refcounting is used
1154      * and what the rules are
1155      */
1156     test_refcount();
1157 }