3 HANDLE killServiceEvent = NULL;
4 static SERVICE_STATUS ssStatus; // current status of the service
5 static SERVICE_STATUS_HANDLE sshStatusHandle;
6 static DWORD dwErr = 0;
7 static char szErr[1024] = "";
9 char *name_service = "ortemanager";
10 char *name_service_disp = "ortemanager";
13 managerInit(void); //forward declaration
15 managerStart(void); //forward declaration
17 managerStop(void); //forward declaration
20 AddToMessageLog(char *lpszMsg)
26 dwErr = GetLastError();
27 hEventSource = RegisterEventSource(NULL, name_service);
28 sprintf(szMsg, "%s error: %li", name_service, dwErr);
29 lpszStrings[0] = szMsg;
30 lpszStrings[1] = lpszMsg;
31 if (hEventSource != NULL) {
32 ReportEvent(hEventSource, // handle of event source
33 EVENTLOG_ERROR_TYPE, // event type
36 NULL, // current user's SID
37 2, // strings in lpszStrings
38 0, // no bytes of raw data
39 (LPCSTR *)lpszStrings, // array of error strings
41 DeregisterEventSource(hEventSource);
46 ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode,
49 static DWORD dwCheckPoint = 1;
52 if (dwCurrentState == SERVICE_START_PENDING)
53 ssStatus.dwControlsAccepted = 0;
55 ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
56 ssStatus.dwCurrentState = dwCurrentState;
57 ssStatus.dwWin32ExitCode = dwWin32ExitCode;
58 ssStatus.dwWaitHint = dwWaitHint;
59 if ((dwCurrentState == SERVICE_RUNNING) || (dwCurrentState == SERVICE_STOPPED))
60 ssStatus.dwCheckPoint = 0;
62 ssStatus.dwCheckPoint = dwCheckPoint++;
63 if (!(fResult = SetServiceStatus(sshStatusHandle, &ssStatus))) {
64 AddToMessageLog("SetServiceStatus");
70 service_ctrl(DWORD dwCtrlCode)
73 case SERVICE_CONTROL_STOP:
74 ssStatus.dwCurrentState = SERVICE_STOP_PENDING;
76 SetEvent(killServiceEvent);
78 case SERVICE_CONTROL_INTERROGATE:
83 ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
87 service_main(DWORD dwArgc, char **lpszArgv)
91 sshStatusHandle = RegisterServiceCtrlHandler(name_service, service_ctrl);
92 if (sshStatusHandle) {
93 ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
94 ssStatus.dwServiceSpecificExitCode = 0;
95 killServiceEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
96 if (!killServiceEvent)
98 if (!ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000))
102 AddToMessageLog("Starting communication");
107 if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0))
109 WaitForSingleObject(killServiceEvent, INFINITE);
110 CloseHandle(killServiceEvent);
113 if (sshStatusHandle) {
114 ReportStatusToSCMgr(SERVICE_STOP_PENDING, dwErr, 0);
115 ReportStatusToSCMgr(SERVICE_STOPPED, dwErr, 0);
120 serviceDispatchTable(void)
122 SERVICE_TABLE_ENTRY dispatchTable[] = {
123 { name_service, (LPSERVICE_MAIN_FUNCTION)service_main },
127 if (!StartServiceCtrlDispatcher(dispatchTable)) {
128 AddToMessageLog("StartServiceCtrlDispatcher failed.");
133 GetLastErrorText(char *lpszBuf, DWORD dwSize)
136 char *lpszTemp = NULL;
138 dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
145 // supplied buffer is not long enough
146 if (!dwRet || ((long)dwSize < (long)dwRet+14)) {
149 lpszTemp[lstrlen(lpszTemp)-2] = '\0'; //remove cr and newline character
150 sprintf(lpszBuf, "%s (%li)", lpszTemp, GetLastError());
153 LocalFree((HLOCAL)lpszTemp);
160 SC_HANDLE schService;
161 SC_HANDLE schSCManager;
164 if (GetModuleFileName(NULL, szPath, sizeof(szPath) - 1) == 0) {
165 printf("Unable to install %s - %s\n",
167 GetLastErrorText(szErr, sizeof(szErr)));
170 strcat(szPath, " -s");
171 schSCManager = OpenSCManager(NULL, // machine (NULL == local)
172 NULL, // database (NULL == default)
173 SC_MANAGER_ALL_ACCESS); // access required
175 schService = CreateService(schSCManager, // SCManager database
176 name_service, // name of service
177 name_service_disp, // name to display
178 SERVICE_ALL_ACCESS, // desired access
179 SERVICE_WIN32_OWN_PROCESS, // service type
180 SERVICE_AUTO_START, // start type
181 SERVICE_ERROR_NORMAL, // error control type
182 szPath, // service's binary
183 NULL, // no load ordering group
184 NULL, // no tag identifier
185 NULL, // dependencies
186 NULL, // LocalSystem account
187 NULL); // no password
189 CloseServiceHandle(schSCManager);
190 printf("Service name (%s) instaled!\n", name_service);
192 printf("OpenSCManager failed - %s\n", GetLastErrorText(szErr, sizeof(szErr)));
200 SC_HANDLE schService;
201 SC_HANDLE schSCManager;
203 schSCManager = OpenSCManager(NULL, // machine (NULL == local)
204 NULL, // database (NULL == default)
205 SC_MANAGER_ALL_ACCESS); // access required
207 schService = OpenService(schSCManager, name_service, SERVICE_ALL_ACCESS);
209 // try to stop the service
210 if (ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus)) {
211 printf("Stopping %s.", name_service);
213 while (QueryServiceStatus(schService, &ssStatus)) {
214 if (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) {
221 if (ssStatus.dwCurrentState == SERVICE_STOPPED) {
222 printf("\n%s stopped.\n", name_service);
224 printf("\n%s failed to stop.\n", name_service);
227 // now remove the service
228 if (DeleteService(schService)) {
229 printf("%s removed.\n", name_service);
231 printf("DeleteService failed - %s\n", GetLastErrorText(szErr, sizeof(szErr)));
233 CloseServiceHandle(schService);
235 printf("OpenService failed - %s\n", GetLastErrorText(szErr, sizeof(szErr)));
237 CloseServiceHandle(schSCManager);
239 printf("OpenSCManager failed - %s\n", GetLastErrorText(szErr, sizeof(szErr)));