예제 #1
0
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    // Get a pointer to the C++ object
    CNTService* pService = m_pThis;

    pService->DebugMsg("Entering CNTService::ServiceMain()");
    // Register the control request handler
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandlerA(pService->m_szServiceName,
                                                           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_bIsRunning = TRUE;
        pService->m_Status.dwWin32ExitCode = 0;
        pService->m_Status.dwCheckPoint = 0;
        pService->m_Status.dwWaitHint = 0;
        pService->Run();
    }

    // Tell the service manager we are stopped
    pService->SetStatus(SERVICE_STOPPED);

    pService->DebugMsg("Leaving CNTService::ServiceMain()");
}
예제 #2
0
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    // Get a pointer to the C++ object
    CNTService* pService = m_pThis;
    
    pService->DebugMsg(_("Entering CNTService::ServiceMain()"));

    // Register the control request handler
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandler(pService->m_szServiceName,
                                                           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_bIsRunning = true;
        pService->m_Status.dwWin32ExitCode = 0;
        pService->m_Status.dwCheckPoint = 0;
        pService->m_Status.dwWaitHint = 0;
        pService->Run();
    }


	// Now wait for threads to exit.
	DWORD dwWaitRes;
    if ( ( dwWaitRes = WaitForMultipleObjects( MAX_THREADS, ghThreads, TRUE, 3000 ) )
					== WAIT_OBJECT_0 ) {
		// This is OK! Nothing more to do.....
		;
	}
    else if ( ( dwWaitRes == WAIT_FAILED ) || ( dwWaitRes == WAIT_ABANDONED ) ) {
		// We failed to kill the threads we have to
		// abort anyway but we tell the world that we do so....
		pService->DebugMsg(_("Failed to terminate all threads in CNTService::ServiceMain()"));
	}    

	// close the event handle and the thread handle
	CloseHandle( ghStopEvent );
	for (int i=0; i<MAX_THREADS; i++ ) {
		if ( ghThreads[i] ) {
			CloseHandle( ghThreads[i] );	 
		}
	}

    // Tell the service manager we are stopped
    pService->SetStatus( SERVICE_STOPPED );

    pService->DebugMsg(_("Leaving CNTService::ServiceMain()"));
}
예제 #3
0
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    CNTService* pService = m_pThis;
    
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandler(pService->m_szServiceName, Handler);
    if (pService->m_hServiceStatus != NULL) {
        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);
    }
}
예제 #4
0
// static member function (callback)
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
#ifdef _DEBUG
    cout << "in NTService ServiceMain" << endl;
#endif
    // Get a pointer to the C++ object
    CNTService* pService = m_pThis;
    
    // Register the control request handler
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandler(pService->m_szServiceName,
                                                           Handler);
    if (pService->m_hServiceStatus == NULL) {
        pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_CTRLHANDLERNOTINSTALLED);
#ifdef _DEBUG
    cout << "NTService:could not register control request handler" << endl;
#endif
        return;
    }

    // Start the initialisation
    if (pService->Initialize()) {

#ifdef _DEBUG
    cout << "NTService: Initialization was successful - ready to go" << endl;
#endif
        // Do the real work. 
        // When the Run function returns, the service has stopped.
        pService->m_bIsRunning = TRUE;
        pService->m_Status.dwWin32ExitCode = 0;
        pService->m_Status.dwCheckPoint = 0;
        pService->m_Status.dwWaitHint = 0;
        pService->Run();
    }

#ifdef _DEBUG
    cout << "NTService: Initialize failed!" << endl;
#endif

    // Tell the service manager we are stopped
    pService->SetStatus(SERVICE_STOPPED);

}