📄 service.c
字号:
/* * NT Service interface Utility. * Based on code written by * Chas Woodfield, Fretwell Downing Informatics. * $Log: service.c,v $ * Revision 1.5 2001/11/13 23:00:42 adam * Separate malloc debug library. Removal of ASN_COMPILED-#ifdefs. * * Revision 1.4 2000/12/05 19:05:10 adam * Service automatically starts in the directory from which it was installed. * * Revision 1.3 1999/06/10 11:45:30 adam * Added bend_start, bend_stop handlers and removed pre_init. * Handlers bend_start/bend_stop are called when service/daemon is * started/stopped. * * Revision 1.2 1999/02/02 13:57:36 adam * Uses preprocessor define WIN32 instead of WINDOWS to build code * for Microsoft WIN32. * * Revision 1.1 1997/11/07 13:31:52 adam * Added NT Service name part of statserv_options_block. Moved NT * service utility to server library. * * Revision 1.6 1997/09/18 08:49:14 adam * Option -runnormal no needed to run server in standalone mode. * * Revision 1.5 1997/09/17 12:10:43 adam * YAZ version 1.4. * * Revision 1.4 1997/09/09 10:10:20 adam * Another MSV5.0 port. Changed projects to include proper * library/include paths. * Server starts server in test-mode when no options are given. * * Revision 1.3 1997/09/04 13:50:30 adam * Bug fix in ztest. * *//************************************************************//* Note this file is shared by all processes *//* Should really put it somewhere other than here *//* For some strange reason it won't work when part of a lib *//************************************************************/#ifdef WIN32#include <windows.h>#include <stdio.h>#include <tchar.h>#include <direct.h>#include "service.h"static AppService *pService = NULL;static BOOL bRunAsService = TRUE;static void *pAppHandle = NULL;/* Private functions to this module */void Service_Create(LPTSTR pAppName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependancies, int argc, char **argv);void Service_Delete();void Service_Initialize();BOOL NotifyServiceController();BOOL UpdateServiceStatus(DWORD Status);void FailServiceStart(DWORD Win32Code, DWORD PrivateCode);void CmdInstallService(int argc, char *argv[], BOOL bAutoStart);void CmdRemoveService();LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize);BOOL CheckServiceArguments(int argc, char *argv[]);/* Callback functions for thee service manager */void WINAPI ServiceMain(DWORD argc, LPTSTR argv[]);void WINAPI ServiceControlHandler(DWORD fdwControl);/* Function to handle Ctrl + C etc... */BOOL EventHandlerRoutine(DWORD dwCtrlType);void Service_Create(LPTSTR pAppName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependancies, int argc, char **argv){ pService = malloc(sizeof(AppService)); pService->pAppName = pAppName; pService->pServiceName = pServiceName; pService->pServiceDisplayName = pServiceDisplayName; pService->pDependancies = pDependancies; pService->hService = 0; pService->ServiceTable[0].lpServiceName = pServiceName; pService->ServiceTable[0].lpServiceProc = ServiceMain; pService->ServiceTable[1].lpServiceName = NULL; pService->ServiceTable[1].lpServiceProc = NULL; pService->argc = argc; pService->argv = argv;}void Service_Delete(){ if (pService != NULL) { /* Mark the service as stopping */ UpdateServiceStatus(SERVICE_STOP_PENDING); /* Stop the service */ StopAppService(pAppHandle); /* Service has now stopped */ UpdateServiceStatus(SERVICE_STOPPED); /* Free the memory */ free(pService); pService = NULL; }}void Service_Initialize(){ if (pService != NULL) { /* Register ourselves with the control dispatcher */ StartServiceCtrlDispatcher(pService->ServiceTable); }}void WINAPI ServiceMain(DWORD argc, LPTSTR argv[]){ if (pService != NULL) { if (NotifyServiceController()) { /* Set the status to pending */ UpdateServiceStatus(SERVICE_START_PENDING); /* Lets attempt to start the service */ if (StartAppService(pAppHandle, pService->argc, pService->argv)) { /* Service is now up and running */ UpdateServiceStatus(SERVICE_RUNNING); /* Lets wait for our clients */ RunAppService(pAppHandle); } else { FailServiceStart(GetLastError(), 0); Service_Delete(); } } }}BOOL NotifyServiceController(){ if (pService == NULL) { return(FALSE); } else { if (bRunAsService) { pService->ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; pService->ServiceStatus.dwCurrentState = SERVICE_STOPPED; pService->ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; pService->ServiceStatus.dwWin32ExitCode = 0; pService->ServiceStatus.dwServiceSpecificExitCode = 0; pService->ServiceStatus.dwCheckPoint = 0; pService->ServiceStatus.dwWaitHint = 0; pService->hService = RegisterServiceCtrlHandler(pService->pServiceName, ServiceControlHandler); if (pService->hService) UpdateServiceStatus(SERVICE_START_PENDING); else return(FALSE); } return(TRUE); }}void WINAPI ServiceControlHandler(DWORD fdwControl){ if (pService != NULL) { switch (fdwControl) { case SERVICE_CONTROL_STOP: /* Update the service status to be pending */ Service_Delete(); break; case SERVICE_CONTROL_INTERROGATE: UpdateServiceStatus(pService->ServiceStatus.dwCurrentState); break; default: break; } }}BOOL UpdateServiceStatus(DWORD Status){ if (pService != NULL) { if (pService->hService) { pService->ServiceStatus.dwCurrentState = Status; if ((Status == SERVICE_START_PENDING) || (Status == SERVICE_STOP_PENDING)) { pService->ServiceStatus.dwCheckPoint ++; pService->ServiceStatus.dwWaitHint = 5000; /* 5 sec.*/ } else { pService->ServiceStatus.dwCheckPoint = 0; pService->ServiceStatus.dwWaitHint = 0; } return(SetServiceStatus(pService->hService, &pService->ServiceStatus)); } } return(FALSE);}void FailServiceStart(DWORD Win32Code, DWORD PrivateCode){ if (pService != NULL) { pService->ServiceStatus.dwWin32ExitCode = Win32Code; pService->ServiceStatus.dwServiceSpecificExitCode = PrivateCode; UpdateServiceStatus(SERVICE_STOPPED); }}void CmdInstallService(int argc, char *argv[], BOOL bAutoStart){ if (pService != NULL) { SC_HANDLE schService; SC_HANDLE schSCManager; TCHAR szPath[2048]; if (GetModuleFileName(NULL, szPath, 512) == 0) { _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(pService->pServiceDisplayName), GetLastErrorText(pService->szErr, 256)); } else { int i; char cwdstr[_MAX_PATH];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -