📄 winservice.c
字号:
/* *
* * Windows Service related function definitions
* * By Raju Krishnappa(raju_krishnappa@yahoo.com)
* *
*/
#include <windows.h>
#include <tchar.h>
#include <stdio.h> /* sprintf */
#include <process.h> /* beginthreadex */
#include <net-snmp/library/winservice.h>
/* *
* * External global variables used here
*/
/* * Application Name */
/* * This should be decalred by the application, which wants to register as
* * windows servcie
*/
extern LPTSTR g_szAppName;
/* *
* * Declare global variable
*/
/* * Flag to indicate, whether process is running as Service */
BOOL g_fRunningAsService = FALSE;
/* * Varibale to maintain Current Service status */
static SERVICE_STATUS ServiceStatus;
/* * Service Handle */
static SERVICE_STATUS_HANDLE hServiceStatus = 0L;
/* * Service Table Entry */
SERVICE_TABLE_ENTRY ServiceTableEntry[] = {
NULL, ServiceMain, /* Service Main function */
NULL, NULL};
/* * Handle to Thread, to implement Pause,Resume and stop funcitonality */
static HANDLE hServiceThread = NULL; /* Thread Handle */
/* * Holds calling partys Function Entry point, that should started
* * when entered to service mode
*/
static INT(*ServiceEntryPoint) (INT Argc, LPTSTR Argv[]) = 0L;
/* *
* * To hold Stop Function address, to be called when STOP request
* * recived from the SCM
*/
static VOID(*StopFunction) () = 0L;
/* *
* * To register as Windows Service with SCM(Service Control Manager)
* * Input - Service Name, Serivce Display Name,Service Description and
* * Service startup arguments
*/
VOID RegisterService(LPCTSTR lpszServiceName,
LPCTSTR lpszServiceDisplayName,
LPCTSTR lpszServiceDescription,
InputParams * StartUpArg) /* Startup argument to the service */
{
TCHAR szServicePath[MAX_PATH]; /* To hold module File name */
TCHAR MsgErrorString[MAX_STR_SIZE]; /* Message or Error string */
TCHAR szServiceCommand[MAX_PATH + 9]; /* Command to execute */
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
TCHAR szRegAppLogKey[] = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
TCHAR szRegKey[512];
HKEY hKey = NULL; /* Key to registry entry */
HKEY hParamKey = NULL; /* To store startup parameters */
DWORD dwData; /* Type of logging supported */
DWORD i, j; /* Loop variables */
GetModuleFileName(NULL, szServicePath, MAX_PATH);
__try
{
/* * Open Service Control Manager handle */
hSCManager = OpenSCManager(NULL,
NULL,
SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
{
DisplayError(_T("Can't open SCM"));
__leave;
}
/* * Generate the Command to be executed by SCM */
_stprintf(szServiceCommand, "%s %s", szServicePath, _T("-service"));
/* * Create the Desired service */
hService = CreateService(hSCManager,
lpszServiceName,
lpszServiceDisplayName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
szServiceCommand,
NULL, /* load-order group */
NULL, /* group member tag */
NULL, /* dependencies */
NULL, /* account */
NULL); /* password */
if (hService == NULL)
{
/* * Generate Error String */
_stprintf(MsgErrorString, "%s %s", _T("Can't Create Service"), lpszServiceDisplayName);
DisplayError(MsgErrorString);
__leave;
}
/* * Create registry entires for EventLog */
/* * Create registry Application event log key */
_tcscpy(szRegKey, szRegAppLogKey);
_tcscat(szRegKey, lpszServiceName);
/* * Create registry key */
if (RegCreateKey(HKEY_LOCAL_MACHINE, szRegKey, &hKey) != ERROR_SUCCESS)
{
_stprintf(MsgErrorString, "%s %s", _T("Unable to create registry entires"), lpszServiceDisplayName);
DisplayError(MsgErrorString);
__leave;
}
/* * Add Event ID message file name to the 'EventMessageFile' subkey */
RegSetValueEx(hKey,
"EventMessageFile",
0,
REG_EXPAND_SZ,
(CONST BYTE *) szServicePath,
_tcslen(szServicePath) + sizeof(TCHAR));
/* * Set the supported types flags. */
dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
RegSetValueEx(hKey,
"TypesSupported",
0,
REG_DWORD,
(CONST BYTE *) & dwData,
sizeof(DWORD));
/* * Close Registry key */
RegCloseKey(hKey);
/* * Set Service Description String and save startup parameters if present */
if (lpszServiceDescription != NULL || StartUpArg->Argc > 2)
{
/* * Create Registry Key path */
_tcscpy(szRegKey, _T("SYSTEM\\CurrentControlSet\\Services\\"));
_tcscat(szRegKey, g_szAppName);
hKey = NULL;
/* * Open Registry key */
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
szRegKey,
0,
KEY_WRITE, /* * Create and Set access */
&hKey) != ERROR_SUCCESS)
{
_stprintf(MsgErrorString, "%s %s", _T("Unable to create registry entires"), lpszServiceDisplayName);
DisplayError(MsgErrorString);
__leave;
}
/* * Create description subkey and the set value */
if (lpszServiceDescription != NULL)
{
if (RegSetValueEx(hKey,
"Description",
0,
REG_SZ,
(CONST BYTE *) lpszServiceDescription,
_tcslen(lpszServiceDescription) + sizeof(TCHAR)) != ERROR_SUCCESS)
{
_stprintf(MsgErrorString, "%s %s", _T("Unable to create registry entires"), lpszServiceDisplayName);
DisplayError(MsgErrorString);
__leave;
};
}
/* * Save startup arguments if they are present */
if (StartUpArg->Argc > 2)
{
/* * Create Subkey parameters */
if (RegCreateKeyEx (hKey, "Parameters", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hParamKey, NULL) != ERROR_SUCCESS)
{
_stprintf(MsgErrorString, "%s %s", _T("Unable to create registry entires"), lpszServiceDisplayName);
DisplayError(MsgErrorString);
__leave;
}
/* * Save parameters */
/* * Loop through arguments */
for (i = 2, j = 1; i < StartUpArg->Argc; i++, j++)
{
_stprintf(szRegKey, "%s%d", _T("Param"), j);
/* * Create registry key */
if (RegSetValueEx (hParamKey,
szRegKey,
0,
REG_SZ,
(CONST BYTE *) StartUpArg->Argv[i],
_tcslen(StartUpArg->Argv[i]) + sizeof(TCHAR)) != ERROR_SUCCESS)
{
_stprintf(MsgErrorString, "%s %s", _T("Unable to create registry entires"), lpszServiceDisplayName);
DisplayError(MsgErrorString);
__leave;
};
}
}
/* * Everything is set, delete hKey */
RegCloseKey(hParamKey);
RegCloseKey(hKey);
}
/* * Ready to Log messages */
/* * Successfully registered as service */
_stprintf(MsgErrorString, "%s %s", lpszServiceName, _T("- Successfully registered as Service"));
/* * Log message to eventlog */
WriteToEventLog(EVENTLOG_INFORMATION_TYPE, MsgErrorString);
MessageBox(NULL,
MsgErrorString,
g_szAppName,
MB_ICONINFORMATION);
}
__finally
{
if (hSCManager) CloseServiceHandle(hSCManager);
if (hService) CloseServiceHandle(hService);
if (hKey) RegCloseKey(hKey);
if (hParamKey) RegCloseKey(hParamKey);
}
}
/* *
* * Unregister the service with the Windows SCM
* * Input - ServiceName
* *
*/
VOID UnregisterService(LPCSTR lpszServiceName)
{
TCHAR MsgErrorString[MAX_STR_SIZE]; /* Message or Error string */
SC_HANDLE hSCManager = NULL; /* SCM handle */
SC_HANDLE hService = NULL; /* Service Handle */
SERVICE_STATUS sStatus;
TCHAR szRegAppLogKey[] = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
TCHAR szRegKey[512];
HKEY hKey = NULL; /* Key to registry entry */
__try
{
/* * Open Service Control Manager */
hSCManager = OpenSCManager(NULL,
NULL,
SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
{
/* * Error while opening SCM */
MessageBox(NULL,
_T("Can't open SCM"),
g_szAppName,
MB_ICONHAND);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -