1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
| #include <iostream>
#include <Windows.h>
#include <conio.h>
#include <tchar.h>
#include <wchar.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
SERVICE_STATUS serviceStatus;
SERVICE_STATUS_HANDLE serviceStatusHandle;
#define serviceName TEXT("SimpleService")
int addLogMessage(const char* text)
{
return printf(text);
}
void ControlHandler(DWORD request)
{
switch (request)
{
case SERVICE_CONTROL_STOP:
addLogMessage("Stopped.");
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
addLogMessage("Shutdown.");
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
serviceStatus.dwWaitHint = 10000;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
default:
break;
}
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
}
void ServiceMain(int, char**)
{
int i = 0;
serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
serviceStatusHandle = RegisterServiceCtrlHandler(serviceName, (LPHANDLER_FUNCTION)ControlHandler);
if (serviceStatusHandle == (SERVICE_STATUS_HANDLE)0) {
return;
}
serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
while (serviceStatus.dwCurrentState == SERVICE_RUNNING)
{
char buffer[255];
sprintf_s(buffer, "%u", i);
int result = addLogMessage(buffer);
if (result) {
serviceStatus.dwCurrentState = SERVICE_STOPPED;
serviceStatus.dwWin32ExitCode = -1;
SetServiceStatus(serviceStatusHandle, &serviceStatus);
return;
}
i++;
}
return;
}
int InstallService() {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (!hSCManager) {
addLogMessage("Error: Can't open Service Control Manager");
return -1;
}
TCHAR szPath[MAX_PATH];
if (!GetModuleFileName(NULL, szPath, MAX_PATH))
{
printf("Cannot install service (%d)\n", GetLastError());
return -1;
}
SC_HANDLE hService = CreateService(
hSCManager,
serviceName,
serviceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
szPath,
NULL, NULL, NULL, NULL, NULL
);
if (!hService) {
int err = GetLastError();
switch (err) {
case ERROR_ACCESS_DENIED:
addLogMessage("Error: ERROR_ACCESS_DENIED");
break;
case ERROR_CIRCULAR_DEPENDENCY:
addLogMessage("Error: ERROR_CIRCULAR_DEPENDENCY");
break;
case ERROR_DUPLICATE_SERVICE_NAME:
addLogMessage("Error: ERROR_DUPLICATE_SERVICE_NAME");
break;
case ERROR_INVALID_HANDLE:
addLogMessage("Error: ERROR_INVALID_HANDLE");
break;
case ERROR_INVALID_NAME:
addLogMessage("Error: ERROR_INVALID_NAME");
break;
case ERROR_INVALID_PARAMETER:
addLogMessage("Error: ERROR_INVALID_PARAMETER");
break;
case ERROR_INVALID_SERVICE_ACCOUNT:
addLogMessage("Error: ERROR_INVALID_SERVICE_ACCOUNT");
break;
case ERROR_SERVICE_EXISTS:
addLogMessage("Error: ERROR_SERVICE_EXISTS");
break;
default:
addLogMessage("Error: Undefined");
}
CloseServiceHandle(hSCManager);
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
addLogMessage("Success install service!");
return 0;
}
int RemoveService()
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!hSCManager) {
addLogMessage("Error: Can't open Service Control Manager");
return -1;
}
SC_HANDLE hService = OpenService(hSCManager, serviceName, SERVICE_STOP | DELETE);
if (!hService) {
addLogMessage("Error: Can't remove service");
CloseServiceHandle(hSCManager);
return -1;
}
DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
addLogMessage("Success remove service!");
return 0;
}
int StartServicee()
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
SC_HANDLE hService = OpenService(hSCManager, serviceName, SERVICE_START);
if (!StartService(hService, 0, NULL))
{
int error = GetLastError();
if(error == ERROR_INVALID_HANDLE)
addLogMessage("Error: Invalid handle");
if (error == ERROR_ACCESS_DENIED)
addLogMessage("Error: Access denied");
if (error == ERROR_PATH_NOT_FOUND)
addLogMessage("Error: Path not found");
if( error == ERROR_SERVICE_ALREADY_RUNNING)
addLogMessage("Error: Service already running");
if (error == ERROR_SERVICE_DATABASE_LOCKED)
addLogMessage("Error: Database locked");
if (error == ERROR_SERVICE_DEPENDENCY_DELETED)
addLogMessage("Error: Dependency deleted");
if (error == ERROR_SERVICE_DEPENDENCY_FAIL)
addLogMessage("Error: Dependency fail");
if (error == ERROR_SERVICE_DISABLED)
addLogMessage("Error: Service disabled");
if (error == ERROR_SERVICE_LOGON_FAILED)
addLogMessage("Error: Service logon faild");
if (error == ERROR_SERVICE_MARKED_FOR_DELETE)
addLogMessage("Error: Service maked for delete");
if (error == ERROR_SERVICE_NO_THREAD)
addLogMessage("Error: Service no thread");
if (error == ERROR_SERVICE_REQUEST_TIMEOUT)
addLogMessage("Error: Service timeout");
if (error == ERROR_FILE_NOT_FOUND)
addLogMessage("Error: File not found");
CloseServiceHandle(hSCManager);
addLogMessage("Error: Can't start service");
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return 0;
}
int main(int argc, char* argv[])
{
string str = "erere";
if (argc - 1 == 0)
{
wchar_t ws[100];
swprintf(ws, 100, L"SimpleService1");
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = ws;
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)NULL;
if (!StartServiceCtrlDispatcher(ServiceTable))
{
addLogMessage("Error: StartServiceCtrlDispatcher");
}
}
else if (strcmp(argv[argc - 1], "install") == 0)
{
InstallService();
}
else if (strcmp(argv[argc - 1], "remove") == 0)
{
RemoveService();
}
else if (strcmp(argv[argc - 1], "start") == 0)
{
StartServicee();
}
return 0;//a.exec();
} |