#include "StdAfx.h" //precompile head file,if you have another , replace it.
#include "CX_NTService.h"
CX_NTService* CX_NTService::m_pThis = NULL;
CX_NTService::CX_NTService()
{
m_hSCManager = NULL;
m_pThis = this;
m_ServiceName = NULL;
}
CX_NTService::~CX_NTService()
{
if (m_hSCManager != NULL)
{
CloseServiceHandle(m_hSCManager);
m_hSCManager = NULL;
}
if (m_ServiceName != NULL)
{
delete []m_ServiceName;
m_ServiceName = NULL;
}
m_pThis = NULL;
}
BOOL CX_NTService::AddNtService(TCHAR* Service_name)
{
if (m_hSCManager == NULL)
{
m_hSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
}
int STRLEN = _tcslen(Service_name)+1;
m_ServiceName = new TCHAR[STRLEN];
_tcscpy(m_ServiceName, Service_name);
TCHAR Binary_exe[MAX_PATH];
::GetModuleFileName(NULL, Binary_exe,MAX_PATH);
SC_HANDLE service = ::CreateService(m_hSCManager,Service_name,Service_name,SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,
Binary_exe,NULL,NULL,_T("RPCSS\0"),NULL,NULL);
if (service == NULL)
{
CX_LOG("Create service faild");
return FALSE;
}
CX_LOG("Create service success");
::CloseServiceHandle(service);
return TRUE;
}
int CX_NTService::RegisterService(TCHAR* ServiceName)
{
if (!IsAdd(ServiceName))
{
AddNtService(ServiceName);
return 0; //if this is first running, return. and the user must entry system service start service
}
if (m_ServiceName == NULL)
{
int STRLEN = _tcslen(ServiceName)+1;
m_ServiceName = new TCHAR[STRLEN];
_tcscpy(m_ServiceName, ServiceName);
ASSERT(m_ServiceName);
}
SERVICE_TABLE_ENTRY ServiceTable[]=
{
{ServiceName,(LPSERVICE_MAIN_FUNCTION)_ServiceMain},
{NULL,NULL}
};
// Start the control dispatcher thread for our service
if (!::StartServiceCtrlDispatcher(ServiceTable) )
{
CX_LOG("start control dispatcher faild, error code: ");
}
return 0;
}
int CX_NTService::ServiceMain(int argc, LPCSTR* argv)
{
m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint =0;
m_ServiceStatus.dwControlsAccepted = SERVICE_CONTROL_STOP ;
m_hStatus = RegisterServiceCtrlHandler(m_ServiceName, (LPHANDLER_FUNCTION)_ControlHandle);
if (m_hStatus != NULL )
{ //如果控制函数注册成功,启动服务.
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
::SetServiceStatus(m_hStatus ,&m_ServiceStatus);
}
else
{
CX_LOG("控制函数注册失败\n");
return -1;
}
Run();
return 0;
}
void CX_NTService::ControlHandle( DWORD Cmd )
{
switch (Cmd)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
{
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
LetItStop(STOP);
Sleep(6000); //等待程序退出
break;
}
// case SERVICE_CONTROL_PAUSE:
// {
// m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
// break;
// }
// case SERVICE_CONTROL_CONTINUE:
// {
// m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
// break;
// }
}
m_ServiceStatus.dwWin32ExitCode = 0;
::SetServiceStatus(m_hStatus, &m_ServiceStatus);
}
BOOL CX_NTService::RemoveService( TCHAR* Service_name )
{
if (m_hSCManager == NULL)
{
m_hSCManager=::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
}
SC_HANDLE Deleted_SC=OpenService(m_hSCManager,Service_name,SERVICE_ALL_ACCESS);
if (Deleted_SC == NULL)
{
TRACE(_T("openservice faild"));
return false;
}
SERVICE_STATUS status;
::ControlService(Deleted_SC, SERVICE_CONTROL_STOP, &status);
DeleteService(Deleted_SC);
CloseServiceHandle(Deleted_SC);
return TRUE;
}
BOOL CX_NTService::IsAdd( TCHAR* Service_name )
{
if (m_hSCManager == NULL)
{
m_hSCManager=::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
}
SC_HANDLE hAdd = OpenService(m_hSCManager,Service_name,SERVICE_ALL_ACCESS);
if (hAdd == NULL)
{
CloseServiceHandle(hAdd);
return FALSE;
}
CloseServiceHandle(hAdd);
return TRUE;
}
int WINAPI CX_NTService::_ServiceMain( int argc, LPCSTR* argv )
{
return GetThis()->ServiceMain(argc, argv);
}
void WINAPI CX_NTService::_ControlHandle( DWORD Cmd)
{
GetThis()->ControlHandle(Cmd);
}
int WriteFileLog(char *LogMsg)
{
FILE* file;
file = fopen("c:\\log.txt","a");
if (file == NULL)
{
return -1;
}
fprintf(file,LogMsg);
fclose(file);
return 0;
}
- 1
- 2
- 3
前往页