📄 pgpservice.cpp
字号:
{
if (g_platformID == VER_PLATFORM_WIN32_WINDOWS) {
ServiceMain(0,0);
return TRUE;
} else if (g_platformID == VER_PLATFORM_WIN32_NT) {
SERVICE_TABLE_ENTRY st[] =
{
{const_cast<char*>(m_szServiceName.c_str()), ServiceMain},
{NULL, NULL}
};
debugMsg("Calling StartServiceCtrlDispatcher()");
PGPBoolean b = StartServiceCtrlDispatcher(st);
debugMsg("Returned from StartServiceCtrlDispatcher()");
return b;
}
return FALSE;
}
//
// static member function (callback)
//
void
CService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
// Get a pointer to the C++ object
CService* pService = CService::m_pThis;
if (g_platformID == VER_PLATFORM_WIN32_WINDOWS) {
pService->debugMsg("Entering CNTService::ServiceMain()");
// Start the initialisation
if (pService->Initialize()) {
// Do the real work.
// When the Run function returns, the service has stopped.
pService->debugMsg("Calling Run()");
pService->Run();
}
pService->debugMsg("Leaving CNTService::ServiceMain()");
} else if (g_platformID == VER_PLATFORM_WIN32_NT) {
pService->debugMsg("Entering CService::ServiceMain()");
if (!pService->commandLine()) {
// Register the control request handler
pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
pService->m_hServiceStatus =
RegisterServiceCtrlHandler(pService->m_szServiceName.c_str(), Handler);
if (pService->m_hServiceStatus == NULL) {
pService->LogEvent(EVENTLOG_ERROR_TYPE,
EVMSG_CTRLHANDLERNOTINSTALLED);
return;
}
}
// Start the initialisation
if (pService->Initialize()) {
// Do the real work.
// When the Run function returns, the service has stopped.
pService->m_Status.dwWin32ExitCode = 0;
pService->m_Status.dwCheckPoint = 0;
pService->m_Status.dwWaitHint = 0;
pService->debugMsg("Calling pService->Run()");
pService->Run();
}
// Tell the service manager we are stopped
pService->SetStatus(SERVICE_STOPPED);
pService->debugMsg("Leaving CService::ServiceMain()");
}
}
//
// status functions
//
void
CService::SetStatus(DWORD dwState)
{
if (!m_commandLine) {
debugMsg("CService::SetStatus(%lu, %lu)", m_hServiceStatus, dwState);
m_Status.dwCurrentState = dwState;
SetServiceStatus(m_hServiceStatus, &m_Status);
}
}
//
// Service initialization
//
PGPBoolean CService::Initialize()
{
CPGPnetDebugLog::instance()->dbgOut("Entering CService::Initialize()");
if (g_platformID == VER_PLATFORM_WIN32_WINDOWS) {
// Call RegisterServiceProcess
HMODULE hnd;
REGPROC fproc;
hnd = LoadLibrary("kernel32.dll");
if (!hnd) {
CPGPnetDebugLog::instance()->dbgOut(
"Unable to register service: no handle to kernel32");
return FALSE;
}
fproc = (REGPROC) GetProcAddress(hnd, "RegisterServiceProcess");
if (!fproc) {
CPGPnetDebugLog::instance()->dbgOut(
"Unable to register service: "
"RegisterServiceProcess not found");
return FALSE;
}
int err = (*fproc)(NULL, 1);
if (!err) {
CPGPnetDebugLog::instance()->dbgOut(
"Unable to register service: "
"RegisterServiceProcess failed");
return FALSE;
}
(void) FreeLibrary(hnd);
// Perform the actual initialization
PGPBoolean bResult = OnInit();
CPGPnetDebugLog::instance()->dbgOut("Leaving CService::Initialize()");
return bResult;
} else if (g_platformID == VER_PLATFORM_WIN32_NT) {
// Start the initialization
SetStatus(SERVICE_START_PENDING);
debugMsg("Entering CService::Initialize()");
// Perform the actual initialization
PGPBoolean bResult = OnInit();
// Set final state
m_Status.dwWin32ExitCode = GetLastError();
m_Status.dwCheckPoint = 0;
m_Status.dwWaitHint = 0;
if (!bResult) {
LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_FAILEDINIT);
SetStatus(SERVICE_STOPPED);
return FALSE;
}
LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_STARTED);
SetStatus(SERVICE_RUNNING);
debugMsg("Leaving CService::Initialize()");
return TRUE;
}
return FALSE;
}
//
// main function to do the real work of the service
//
// This function performs the main work of the service.
// When this function returns the service has stopped.
//
void
CService::Run()
{
debugMsg("Entering CService::Run()");
PGPBoolean running = TRUE;
while (running) {
PGPUInt32 ret = WaitForSingleObject(m_StopEvent, 10000);
switch (ret) {
case WAIT_TIMEOUT:
debugMsg("Sleeping...");
break;
case WAIT_OBJECT_0:
running = FALSE;
debugMsg("Stop Event signaled");
break;
case WAIT_ABANDONED:
running = FALSE;
debugMsg("Stop event abandoned?");
break;
case WAIT_FAILED:
running = FALSE;
debugMsg("Wait for stop event failed?");
break;
default:
running = FALSE;
break;
}
}
// nothing more to do
debugMsg("Leaving CService::Run()");
}
//
// Control request handlers
// static member function (callback) to handle commands from the
// service control manager
//
void
CService::Handler(DWORD dwOpcode)
{
// Get a pointer to the object
CService* pService = CService::m_pThis;
pService->debugMsg("CService::Handler(%lu)", dwOpcode);
switch (dwOpcode)
{
case SERVICE_CONTROL_STOP: // 1
pService->SetStatus(SERVICE_STOP_PENDING);
pService->OnStop();
PGPCondSignal(&pService->m_StopEvent);
pService->LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_STOPPED);
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:
if (dwOpcode >= SERVICE_CONTROL_USER) {
if (!pService->OnUserControl(dwOpcode)) {
pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_BADREQUEST);
}
} else {
pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_BADREQUEST);
}
break;
}
// Report current status
pService->debugMsg("Updating status (%lu, %lu)",
pService->m_hServiceStatus,
pService->m_Status.dwCurrentState);
}
BOOL
CService::CtrlHandler(DWORD fdwCtrlType)
{
// Get a pointer to the object
CService* pService = CService::m_pThis;
switch (fdwCtrlType)
{
case CTRL_LOGOFF_EVENT:
CPGPnetDebugLog::instance()->dbgOut("Got CTRL_LOGOFF_EVENT");
pService->OnLogoff();
return FALSE;
break;
case CTRL_SHUTDOWN_EVENT:
pService->debugMsg("Got CTRL_SHUTDOWN_EVENT");
pService->OnShutdown();
return FALSE;
case PBT_APMSUSPEND:
pService->OnSuspend();
return FALSE;
break;
case PBT_APMRESUMESUSPEND:
pService->OnResume();
return FALSE;
break;
default:
return FALSE;
}
}
//
// Called when the service is first initialized
//
PGPBoolean
CService::OnInit()
{
debugMsg("CService::OnInit()");
return TRUE;
}
//
// Called when the service control manager wants to stop the service
//
void
CService::OnStop()
{
debugMsg("CService::OnStop()");
}
//
// called when the service is interrogated
//
void
CService::OnInterrogate()
{
debugMsg("CService::OnInterrogate()");
}
//
// called when the service is paused
//
void
CService::OnPause()
{
debugMsg("CService::OnPause()");
}
//
// called when the service is continued
//
void
CService::OnContinue()
{
debugMsg("CService::OnContinue()");
}
//
// called when the service is shut down
//
void
CService::OnShutdown()
{
debugMsg("CService::OnShutdown()");
}
void
CService::OnLogoff()
{
debugMsg("CService::OnLogoff()");
}
//
// called when the service gets a user control message
//
PGPBoolean
CService::OnUserControl(DWORD dwOpcode)
{
debugMsg("CService::OnUserControl(%8.8lXH)", dwOpcode);
return FALSE; // say not handled
}
void
CService::debugMsg(const char *fmt, ...)
{
if (CPGPnetDebugLog::isDebug()) {
va_list args;
va_start(args, fmt);
CPGPnetDebugLog::instance()->vdbgOut(fmt, args);
va_end(args);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -