ReactOS  0.4.13-dev-101-g0ca4b50
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 
10 static 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);
45  ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyName, &hKey);
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 
60  RegCloseKey(hKey);
61 
62  return 0;
63 }
64 
65 static 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 
80 static 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 
146 cleanup:
147 
148  DestroyService(hService2);
149  DestroyService(hService1);
150 
151  if (hScm)
152  CloseServiceHandle(hScm);
153 }
154 
156 {
158 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define SERVICE_ERROR_IGNORE
Definition: cmtypes.h:979
#define TRUE
Definition: types.h:120
#define ERROR_SUCCESS
Definition: deptool.c:10
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:808
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
Definition: ecma_167.h:138
static void Test_CreateService(void)
Definition: CreateService.c:80
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:916
#define ok_err(error)
Definition: FindFiles.c:39
#define CreateService
Definition: winsvc.h:569
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
long LONG
Definition: pedump.c:60
LONG WINAPI RegOpenKeyW(HKEY hKey, LPCWSTR lpSubKey, PHKEY phkResult)
Definition: reg.c:3311
#define ok(value,...)
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
smooth NULL
Definition: ftsmooth.c:416
STRSAFEAPI StringCbPrintfW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:557
#define SERVICE_BOOT_START
Definition: cmtypes.h:973
#define SC_MANAGER_CREATE_SERVICE
Definition: winsvc.h:15
char serviceName[]
Definition: tftpd.cpp:34
static void DestroyService(SC_HANDLE hService)
Definition: CreateService.c:65
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4134
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
int ret
static const WCHAR L[]
Definition: oid.c:1250
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2011
START_TEST(CreateService)
#define DNS_ERROR_RCODE_NXRRSET
Definition: winerror.h:1855
#define skip(...)
char * cleanup(char *str)
Definition: wpickclick.c:99
static int MakeService(SC_HANDLE hScm, const wchar_t *serviceName, SC_HANDLE *hService, DWORD *tag)
Definition: CreateService.c:10
#define REG_DWORD
Definition: sdbapi.c:596
BYTE * PBYTE
Definition: pedump.c:66
#define DELETE
Definition: nt_native.h:57
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define SERVICE_KERNEL_DRIVER
Definition: cmtypes.h:951