/*++

Routine Description:

    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.

Arguments:

    VOID

Return Value:

    VOID

--*/
VOID
CServiceBase::Shutdown()
{
    try
    {
        //
        // Perform service-specific shutdown operations.
        //
        OnShutdown();

        //
        // Tell SCM that the service is stopped.
        //
        SetServiceStatus(SERVICE_STOPPED);
    }
    catch (DWORD Error)
    {
        //
        // Log the error.
        //
        WriteToErrorLog(L"Service Shutdown", Error);
    }
    catch (...)
    {
        //
        // Log the error.
        //
        WriteToEventLog(L"Service failed to shut down.", EVENTLOG_ERROR_TYPE);
    }
}
Example #2
0
bool FileLogger::Log(const gs2d::str_type::string& str, const TYPE& type) const
{
	#if defined(WIN32) || defined(APPLE_IOS) || defined(MACOSX)
	GS2D_COUT << str << std::endl;
	if (type == ERROR)
	{
		GS2D_CERR << GS_L("\x07");
	}
	#endif
	if (type == ERROR)
		WriteToErrorLog(str);
	else if (type == WARNING)
		WriteToWarningLog(str);

	#ifdef ANDROID
		switch (type)
		{
		case ERROR:
		case WARNING:
			LOGE(str.c_str());
			break;
		default:
			LOGI(str.c_str());
		}
	#endif
	return AppendToFile(m_fileName, str);
}
/*++

Routine Description:

    This 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.

Arguments:

    Argc - The number of command line arguments

    Argv - The array of command line arguments

Return Value:

    VOID

--*/
VOID
CServiceBase::Start(
    DWORD  Argc,
    PWSTR *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.
        //
        WriteToErrorLog(L"Service Start", Error);

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

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

Routine Description:

    This 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.

Arguments:

    VOID

Return Value:

    VOID

--*/
VOID
CServiceBase::Stop()
{
    DWORD OriginalState = 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 Error)
    {
        //
        // Log the error.
        //
        WriteToErrorLog(L"Service Stop", Error);

        //
        // Set the orginal service status.
        //
        SetServiceStatus(OriginalState);
    }
    catch (...)
    {
        //
        // Log the error.
        //
        WriteToEventLog(L"Service failed to stop.", EVENTLOG_ERROR_TYPE);

        //
        // Set the orginal service status.
        //
        SetServiceStatus(OriginalState);
    }
}
/*++

Routine Description:

    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.

Arguments:

    VOID

Return Value:

    VOID

--*/
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 Error)
    {
        //
        // Log the error.
        //
        WriteToErrorLog(L"Service Continue", Error);

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

        //
        // Tell SCM that the service is still paused.
        //
        SetServiceStatus(SERVICE_PAUSED);
    }
}
/*++

Routine Description:

    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.

Arguments:

    VOID

Return Value:

    VOID

--*/
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 Error)
    {
        //
        // Log the error.
        //
        WriteToErrorLog(L"Service Pause", Error);

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

        //
        // Tell SCM that the service is still running.
        //
        SetServiceStatus(SERVICE_RUNNING);
    }
}