📄 ntservice.cpp
字号:
#include "StdAfx.h"
#include ".\ntservice.h"
void OutputMessage(char *pFileName , char *pMessage)
{
FILE *pFile;
if((pFile = fopen(pFileName , "a")))
{
fwrite(pMessage , strlen(pMessage) , 1 , pFile);
fclose(pFile);
}
}
CNTService *CNTService::m_pThis = NULL;
CNTService::CNTService(LPCTSTR lpServiceName)
{
lstrcpyn(m_szServiceName , lpServiceName , sizeof(m_szServiceName) - 1);
m_pThis = this;
m_hServiceStatus = NULL;
m_Status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
m_Status.dwCurrentState = SERVICE_STOPPED;
m_Status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_Status.dwWin32ExitCode = 0;
m_Status.dwServiceSpecificExitCode = 0;
m_Status.dwCheckPoint = 0;
m_Status.dwWaitHint = 0;
m_bIsRunning = FALSE;
}
CNTService::~CNTService(void)
{
}
BOOL CNTService::Initialize(void)
{
SetStatus(SERVICE_START_PENDING);
// Perform the actual initialization
BOOL bResult = OnInit();
// Set final state
m_Status.dwWin32ExitCode = GetLastError();
m_Status.dwCheckPoint = 0;
m_Status.dwWaitHint = 0;
if (!bResult) {
SetStatus(SERVICE_STOPPED);
return FALSE;
}
SetStatus(SERVICE_RUNNING);
return TRUE;
}
void CNTService::Run(void)
{
while(m_bIsRunning)
{
::Sleep(5000);
};
}
BOOL CNTService::IsInstalled(void)
{
BOOL bResult = FALSE;
// Open the Service Control Manager
SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access
if (hSCM) {
// Try to open the service
SC_HANDLE hService = ::OpenService(hSCM,
m_szServiceName,
SERVICE_QUERY_CONFIG);
if (hService) {
bResult = TRUE;
::CloseServiceHandle(hService);
}
::CloseServiceHandle(hSCM);
}
return bResult;
}
BOOL CNTService::Install(void)
{
SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access
if (!hSCM) return FALSE;
// Get the executable file path
char szFilePath[_MAX_PATH];
::GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
// Create the service
SC_HANDLE hService = ::CreateService(hSCM,
m_szServiceName,
m_szServiceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, // start condition
SERVICE_ERROR_NORMAL,
szFilePath,
NULL,
NULL,
NULL,
NULL,
NULL);
if (!hService) {
::CloseServiceHandle(hSCM);
return FALSE;
}
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
return TRUE;
}
BOOL CNTService::Uninstall(void)
{
SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access
if (!hSCM) return FALSE;
BOOL bResult = FALSE;
SC_HANDLE hService = ::OpenService(hSCM,
m_szServiceName,
DELETE);
if (hService)
{
if (::DeleteService(hService))
{
bResult = TRUE;
}
::CloseServiceHandle(hService);
}
::CloseServiceHandle(hSCM);
return bResult;
}
BOOL CNTService::StartService(void)
{
SERVICE_TABLE_ENTRY st[] = { {m_szServiceName , &ServiceMain} ,
{NULL , NULL}
};
BOOL bResult = ::StartServiceCtrlDispatcher(st);
return bResult;
}
BOOL CNTService::OnInit(void)
{
return TRUE;
}
void CNTService::OnStop(void)
{
}
void CNTService::OnPause(void)
{
}
void CNTService::OnContinue(void)
{
}
void CNTService::OnShutdown(void)
{
}
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
CNTService *pService = m_pThis;
pService->m_hServiceStatus = ::RegisterServiceCtrlHandler(pService->m_szServiceName , Handler);
if(m_pThis->m_hServiceStatus == NULL)
{
return ;
}
pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
if (pService->Initialize()) {
pService->m_bIsRunning = TRUE;
pService->m_Status.dwWin32ExitCode = 0;
pService->m_Status.dwCheckPoint = 0;
pService->m_Status.dwWaitHint = 0;
pService->Run();
}
pService->SetStatus(SERVICE_STOPPED);
}
void CNTService::Handler(DWORD dwOpcode)
{
CNTService* pService = m_pThis;
switch (dwOpcode) {
case SERVICE_CONTROL_STOP: // 1
pService->SetStatus(SERVICE_STOP_PENDING);
pService->m_bIsRunning = FALSE;
pService->OnStop();
break;
case SERVICE_CONTROL_PAUSE: // 2
pService->OnPause();
break;
case SERVICE_CONTROL_CONTINUE: // 3
pService->OnContinue();
break;
/*
case SERVICE_CONTROL_INTERROGATE: // 4
pService->OnInterrogate();
break;
*/
case SERVICE_CONTROL_SHUTDOWN: // 5
pService->OnShutdown();
break;
/*
default:
break;*/
}
// Report current status
::SetServiceStatus(pService->m_hServiceStatus, &pService->m_Status);
}
void CNTService::SetStatus(DWORD dwState)
{
m_Status.dwCurrentState = dwState;
::SetServiceStatus(m_hServiceStatus, &m_Status);
}
BOOL CNTService::ParseStandardArgs(int argc , _TCHAR* argv[])
{
if (argc <= 1) return FALSE;
if (lstrcmpi(argv[1], "-i") == 0)
{
// Request to install.
if (IsInstalled())
{
printf("%s is already installed\n", m_szServiceName);
}
else
{
// Try and install the copy that's running
if (Install())
{
printf("%s installed\n", m_szServiceName);
}
else
{
printf("%s failed to install. Error %d\n", m_szServiceName, GetLastError());
}
}
return TRUE; // say we processed the argument
}
else
if (lstrcmpi(argv[1], "-u") == 0)
{
// Request to uninstall.
if (!IsInstalled())
{
printf("%s is not installed\n", m_szServiceName);
}
else
{ // Try and remove the copy that's installed
if (Uninstall())
{
// Get the executable file path
char szFilePath[_MAX_PATH];
::GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
printf("%s removed. (You must delete the file (%s) yourself.)\n",
m_szServiceName, szFilePath);
} else
{
printf("Could not remove %s. Error %d\n", m_szServiceName, GetLastError());
}
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -