// The function executes when the system is shutting down. It calls OnShutdown
// virtual function.
void GServiceBase::Shutdown() {
  try {
    // Perform service-specific shutdown operations.
    OnShutdown();

    // Tell SCM that the service is stopped.
    SetServiceStatus(SERVICE_STOPPED);
  } catch (DWORD error) {
    WriteErrorLogEntry(L"Service Shutdown", error);
  } catch (...) {
    WriteErrorLogEntry(
        L"Service Shutdown.", GetLastError());
  }
}
//
//   FUNCTION: CServiceBase::Continue()
//
//   PURPOSE: The function resumes normal functioning after being paused if
//   the service supports pause and continue. It calls the OnContinue virtual 
//   function in which you can specify the actions to take when the service 
//   continues. If an error occurs, the error will be logged in the 
//   Application event log, and the service will still be paused.
//
void CServiceBase::Continue()
{
    try
    {
        // Tell SCM that the service is resuming.
        SetServiceStatus(SERVICE_CONTINUE_PENDING);

        // Perform service-specific continue operations.
        OnContinue();

        // Tell SCM that the service is running.
        SetServiceStatus(SERVICE_RUNNING);
    }
    catch (DWORD dwError)
    {
        // Log the error.
        WriteErrorLogEntry(L"Service failed to resume.", dwError);

        // Tell SCM that the service is still paused.
        SetServiceStatus(SERVICE_PAUSED);
    }
    catch (...)
    {
        // Log the error.
        WriteEventLogEntry(L"Service failed to resume.", TRACE_LEVEL_ERROR);

        // Tell SCM that the service is still paused.
        SetServiceStatus(SERVICE_PAUSED);
    }
}
//
//   FUNCTION: CServiceBase::Pause()
//
//   PURPOSE: The function pauses the service if the service supports pause 
//   and continue. It calls the OnPause virtual function in which you can 
//   specify the actions to take when the service pauses. If an error occurs, 
//   the error will be logged in the Application event log, and the service 
//   will become running.
//
void CServiceBase::Pause()
{
    try
    {
        // Tell SCM that the service is pausing.
        SetServiceStatus(SERVICE_PAUSE_PENDING);

        // Perform service-specific pause operations.
        OnPause();

        // Tell SCM that the service is paused.
        SetServiceStatus(SERVICE_PAUSED);
    }
    catch (DWORD dwError)
    {
        // Log the error.
        WriteErrorLogEntry(L"Service failed to pause.", dwError);

        // Tell SCM that the service is still running.
        SetServiceStatus(SERVICE_RUNNING);
    }
    catch (...)
    {
        // Log the error.
        WriteEventLogEntry(L"Service failed to pause.", TRACE_LEVEL_ERROR);

        // Tell SCM that the service is still running.
        SetServiceStatus(SERVICE_RUNNING);
    }
}
//
//   FUNCTION: CServiceBase::Stop()
//
//   PURPOSE: The function stops the service. It calls the OnStop virtual 
//   function in which you can specify the actions to take when the service 
//   stops. If an error occurs, the error will be logged in the Application 
//   event log, and the service will be restored to the original state.
//
void CServiceBase::Stop()
{
    DWORD dwOriginalState = m_status.dwCurrentState;
    try
    {
        // Tell SCM that the service is stopping.
        SetServiceStatus(SERVICE_STOP_PENDING);

        // Perform service-specific stop operations.
        OnStop();

        // Tell SCM that the service is stopped.
        SetServiceStatus(SERVICE_STOPPED);
    }
    catch (DWORD dwError)
    {
        // Log the error.
        WriteErrorLogEntry(L"Service failed to stop.", dwError);

        // Set the orginal service status.
        SetServiceStatus(dwOriginalState);
    }
    catch (...)
    {
        // Log the error.
        WriteEventLogEntry(L"Service failed to stop.", TRACE_LEVEL_ERROR);

        // Set the orginal service status.
        SetServiceStatus(dwOriginalState);
    }
}
//
//   FUNCTION: CServiceBase::Start(DWORD, PWSTR *)
//
//   PURPOSE: The function starts the service. It calls the OnStart virtual 
//   function in which you can specify the actions to take when the service 
//   starts. If an error occurs during the startup, the error will be logged 
//   in the Application event log, and the service will be stopped.
//
//   PARAMETERS:
//   * dwArgc   - number of command line arguments
//   * lpszArgv - array of command line arguments
//
void CServiceBase::Start(DWORD dwArgc, PWSTR *pszArgv)
{
    WriteEventLogEntry(L"Service starting.", TRACE_LEVEL_ERROR);
    try
    {
        // Tell SCM that the service is starting.
        SetServiceStatus(SERVICE_START_PENDING);

        // Perform service-specific initialization.
        OnStart(dwArgc, pszArgv);

        // Tell SCM that the service is started.
        SetServiceStatus(SERVICE_RUNNING);
    }
    catch (DWORD dwError)
    {
        // Log the error.
        WriteErrorLogEntry(L"Service failed to start.", dwError);

        // Set the service status to be stopped.
        SetServiceStatus(SERVICE_STOPPED, dwError);
    }
    catch (...)
    {
        // Log the error.
        WriteEventLogEntry(L"Service failed to start.", TRACE_LEVEL_ERROR);

        // Set the service status to be stopped.
        SetServiceStatus(SERVICE_STOPPED);
    }
}
//
//   FUNCTION: CServiceBase::Shutdown()
//
//   PURPOSE: The function executes when the system is shutting down. It 
//   calls the OnShutdown virtual function in which you can specify what 
//   should occur immediately prior to the system shutting down. If an error 
//   occurs, the error will be logged in the Application event log.
//
void CServiceBase::Shutdown()
{
    try
    {
        // Perform service-specific shutdown operations.
        OnShutdown();

        // Tell SCM that the service is stopped.
        SetServiceStatus(SERVICE_STOPPED);
    }
    catch (DWORD dwError)
    {
        // Log the error.
        WriteErrorLogEntry(L"Service failed to shut down.", dwError);
    }
    catch (...)
    {
        // Log the error.
        WriteEventLogEntry(L"Service failed to shut down.", TRACE_LEVEL_ERROR);
    }
}
Esempio n. 7
0
//
//   FUNCTION: CSampleService::ServiceWorkerThread(void)
//
//   PURPOSE: The method performs the main function of the service. It runs 
//   on a thread pool worker thread.
//
void CEscanService::ServiceWorkerThread(void)
{
	Bssvc *bs = new Bssvc();
	int res = bs->Init(FindThreatCall, this);
	if (res)
	{
		WriteErrorLogEntry(L"Bssvc Init", res);
		goto Exit;
	}
	m_running = true;

	while (m_running)
	{
		Sleep(1000);
	}

Exit:
    // Signal the finished event.
	delete bs;
    SetEvent(m_hFinishedEvent);
}
// The function resumes normal functioning after being paused by calling
// OnContinue virtual function.
void GServiceBase::Continue() {
  try {
    // Tell SCM that the service is resuming.
    SetServiceStatus(SERVICE_CONTINUE_PENDING);

    // Perform service-specific continue operations.
    OnContinue();

    // Tell SCM that the service is running.
    SetServiceStatus(SERVICE_RUNNING);
  } catch (DWORD error) {
    WriteErrorLogEntry(L"Service Continue", error);

    // Tell SCM that the service is still paused.
    SetServiceStatus(SERVICE_PAUSED);
  } catch (...) {
    LogOperationalMessage(L"Service failed to resume.");

    // Tell SCM that the service is still paused.
    SetServiceStatus(SERVICE_PAUSED);
  }
}
// The function pauses the service if the service supports pause and continue.
// It calls the OnPause virtual function.
void GServiceBase::Pause() {
  try {
    // Tell SCM that the service is pausing.
    SetServiceStatus(SERVICE_PAUSE_PENDING);

    // Perform service-specific pause operations.
    OnPause();

    // Tell SCM that the service is paused.
    SetServiceStatus(SERVICE_PAUSED);
  } catch (DWORD error) {
    WriteErrorLogEntry(L"Service Pause", error);

    // Tell SCM that the service is still running.
    SetServiceStatus(SERVICE_RUNNING);
  } catch (...) {
    LogOperationalMessage(L"Service failed to pause.");

    // Tell SCM that the service is still running.
    SetServiceStatus(SERVICE_RUNNING);
  }
}
// The function stops the service and calls the OnStop virtual function.
void GServiceBase::Stop() {
  DWORD originalState = vss_agent_status.dwCurrentState;
  try {
    // Tell SCM that the service is stopping.
    SetServiceStatus(SERVICE_STOP_PENDING);

    // Perform service-specific stop operations.
    OnStop();

    // Tell SCM that the service is stopped.
    SetServiceStatus(SERVICE_STOPPED);
  } catch (DWORD error) {
    WriteErrorLogEntry(L"Service Stop", error);

    // Set the orginal service status.
    SetServiceStatus(originalState);
  } catch (...) {
    LogOperationalMessage(L"Service failed to stop.");

    // Set the orginal service status.
    SetServiceStatus(originalState);
  }
}
// The function starts the service. It calls the OnStart virtual function.
//
// Arguments:
//   argc - number of command line arguments
//   argv - array of command line arguments
void GServiceBase::Start(DWORD argc, LPWSTR *argv) {
  try {
    // Tell SCM that the service is starting.
    SetServiceStatus(SERVICE_START_PENDING);

    // Perform service-specific initialization.
    OnStart(argc, argv);

    // Tell SCM that the service is started.
    SetServiceStatus(SERVICE_RUNNING);
  } catch (DWORD error) {
    // Log the error.
    WriteErrorLogEntry(L"Service Start", error);

    // Set the service status to be stopped.
    SetServiceStatus(SERVICE_STOPPED, error);
  } catch (...) {
    // Log the error.
    LogOperationalMessage(L"Service failed to start");

    // Set the service status to be stopped.
    SetServiceStatus(SERVICE_STOPPED);
  }
}