ReactOS 0.4.15-dev-7711-g5627da4
CreateService.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for CreateService
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8#include "precomp.h"
9
10static int MakeService(SC_HANDLE hScm, const wchar_t *serviceName, SC_HANDLE *hService, DWORD *tag)
11{
12 DWORD ret;
13 HKEY hKey = NULL;
14 DWORD type = 0, tagData = 0, tagSize;
15 wchar_t keyName[256];
16
18 *hService = CreateServiceW(
19 hScm,
21 NULL,
22 DELETE,
26 L"%systemroot%\\drivers\\win32k.sys",
27 L"advapi32_apitest_CreateService_Test_Group",
28 tag,
29 NULL,
30 NULL,
31 NULL);
32
33 ok(*hService != NULL, "Failed to create service, error=0x%08lx\n", GetLastError());
34 if (!*hService)
35 {
36 skip("No service; cannot proceed with CreateService test\n");
37 return 1;
38 }
39
41
42 ok(*tag != 0, "tag is zero, expected nonzero\n");
43
44 StringCbPrintfW(keyName, sizeof keyName, L"System\\CurrentControlSet\\Services\\%ls", serviceName);
46 ok(ret == ERROR_SUCCESS, "RegOpenKeyW failed with 0x%08lx\n", ret);
47 if (ret)
48 {
49 skip("No regkey; cannot proceed with CreateService test\n");
50 return 2;
51 }
52
53 tagSize = sizeof tagData;
54 ret = RegQueryValueExW(hKey, L"Tag", NULL, &type, (PBYTE)&tagData, &tagSize);
55 ok(ret == ERROR_SUCCESS, "RegQueryValueExW returned 0x%08lx\n", ret);
56 ok(type == REG_DWORD, "type=%lu, expected REG_DWORD\n", type);
57 ok(tagSize == sizeof tagData, "tagSize=%lu, expected 4\n", tagSize);
58 ok(tagData == *tag, "tag=%lu, but registry says %lu\n", *tag, tagData);
59
61
62 return 0;
63}
64
65static void DestroyService(SC_HANDLE hService)
66{
67 LONG ret;
68
69 if (!hService)
70 return;
72 ret = DeleteService(hService);
73 ok(ret == TRUE, "DeleteService returned %ld, expected 1\n", ret);
75 || GetLastError() == ERROR_SUCCESS /* win2k3 */, "DeleteService GetLastError()=0x%08lx\n", GetLastError());
76
77 CloseServiceHandle(hService);
78}
79
80static void Test_CreateService(void)
81{
82 SC_HANDLE hScm = NULL;
83 SC_HANDLE hService1 = NULL, hService2 = NULL;
84 SC_HANDLE hService3 = NULL;
85 DWORD tag1 = 0, tag2 = 0;
86 DWORD tag3 = 785;
87
90 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
91 if (!hScm)
92 {
93 skip("No service control manager; cannot proceed with CreateService test\n");
94 goto cleanup;
95 }
96
98
99 if (MakeService(hScm, L"advapi32_apitest_CreateService_Test_Service1", &hService1, &tag1))
100 goto cleanup;
101
102 if (MakeService(hScm, L"advapi32_apitest_CreateService_Test_Service2", &hService2, &tag2))
103 goto cleanup;
104
105 ok(tag1 != tag2, "tag1=%lu, tag2=%lu\n", tag1, tag2);
106
107 /* ask for a tag, but don't have a group */
108 hService3 = CreateServiceW(
109 hScm,
110 L"advapi32_apitest_CreateService_Test_Service2",
111 NULL,
112 DELETE,
116 L"%systemroot%\\drivers\\win32k.sys",
117 NULL,
118 &tag3,
119 NULL,
120 NULL,
121 NULL);
122 ok(hService3 == NULL, "hService3=%p\n", hService3);
123 ok(GetLastError() == ERROR_INVALID_PARAMETER, "error=%lu\n", GetLastError());
124 ok(tag3 == 785, "tag3=%lu\n", tag3);
125 DestroyService(hService3);
126
127 hService3 = CreateServiceW(
128 hScm,
129 L"advapi32_apitest_CreateService_Test_Service2",
130 NULL,
131 DELETE,
135 L"%systemroot%\\drivers\\win32k.sys",
136 L"",
137 &tag3,
138 NULL,
139 NULL,
140 NULL);
141 ok(hService3 == NULL, "hService3=%p\n", hService3);
142 ok(GetLastError() == ERROR_INVALID_PARAMETER, "error=%lu\n", GetLastError());
143 ok(tag3 == 785, "tag3=%lu\n", tag3);
144 DestroyService(hService3);
145
146cleanup:
147
148 DestroyService(hService2);
149 DestroyService(hService1);
150
151 if (hScm)
152 CloseServiceHandle(hScm);
153}
154
156{
158}
static void DestroyService(SC_HANDLE hService)
Definition: CreateService.c:65
static int MakeService(SC_HANDLE hScm, const wchar_t *serviceName, SC_HANDLE *hService, DWORD *tag)
Definition: CreateService.c:10
static void Test_CreateService(void)
Definition: CreateService.c:80
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define ok_err(error)
Definition: atltest.h:124
#define START_TEST(x)
Definition: atltest.h:75
#define RegCloseKey(hKey)
Definition: registry.h:49
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
LONG WINAPI RegOpenKeyW(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:3297
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4132
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define SetLastError(x)
Definition: compat.h:752
static void cleanup(void)
Definition: main.c:1335
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
#define DELETE
Definition: nt_native.h:57
#define L(x)
Definition: ntvdm.h:50
BYTE * PBYTE
Definition: pedump.c:66
long LONG
Definition: pedump.c:60
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:921
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2068
SC_HANDLE WINAPI CreateServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, LPCWSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCWSTR lpBinaryPathName, LPCWSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCWSTR lpDependencies, LPCWSTR lpServiceStartName, LPCWSTR lpPassword)
Definition: scm.c:812
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
#define REG_DWORD
Definition: sdbapi.c:596
STRSAFEAPI StringCbPrintfW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:557
Definition: ecma_167.h:138
char serviceName[]
Definition: tftpd.cpp:34
int ret
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define DNS_ERROR_RCODE_NXRRSET
Definition: winerror.h:1855
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define CreateService
Definition: winsvc.h:569
#define SC_MANAGER_CREATE_SERVICE
Definition: winsvc.h:15
#define SERVICE_KERNEL_DRIVER
Definition: cmtypes.h:953
#define SERVICE_BOOT_START
Definition: cmtypes.h:975
#define SERVICE_ERROR_IGNORE
Definition: cmtypes.h:981