Share via


서비스 프로그램의 기본 함수 작성

서비스 프로그램의기본 함수는 StartServiceCtrlDispatcher 함수를 호출하여 SCM(서비스 제어 관리자)에 연결하고 컨트롤 디스패처 스레드를 시작합니다. 디스패처 스레드가 반복되어 디스패치 테이블에 지정된 서비스에 대한 들어오는 제어 요청을 대기합니다. 이 스레드는 오류가 있거나 프로세스의 모든 서비스가 종료되었을 때 를 반환합니다. 프로세스의 모든 서비스가 종료되면 SCM은 종료하라는 제어 요청을 디스패처 스레드에 보냅니다. 그런 다음 이 스레드는 StartServiceCtrlDispatcher 호출에서 를 반환하고 프로세스를 종료할 수 있습니다.

이 샘플에서는 다음과 같은 전역 정의가 사용됩니다.

#define SVCNAME TEXT("SvcName")

SERVICE_STATUS          gSvcStatus; 
SERVICE_STATUS_HANDLE   gSvcStatusHandle; 
HANDLE                  ghSvcStopEvent = NULL;

다음 예제는 단일 서비스를 지원하는 서비스 프로그램의 진입점으로 사용할 수 있습니다. 서비스 프로그램에서 여러 서비스를 지원하는 경우 디스패처 스레드에서 모니터링할 수 있도록 디스패치 테이블에 추가 서비스의 이름을 추가합니다.

_tmain 함수는 진입점입니다. SvcReportEvent 함수는 정보 메시지와 오류를 이벤트 로그에 씁니다. SvcMain 함수 작성에 대한 자세한 내용은 ServiceMain 함수 작성을 참조하세요. SvcInstall 함수에 대한 자세한 내용은 서비스 설치를 참조하세요. SvcCtrlHandler 함수 작성에 대한 자세한 내용은 컨트롤 처리기 함수 작성을 참조하세요. SvcReportEvent 함수의 원본을 비롯한 전체 예제 서비스는 Svc.cpp를 참조하세요.

//
// Purpose: 
//   Entry point for the process
//
// Parameters:
//   None
// 
// Return value:
//   None, defaults to 0 (zero)
//
int __cdecl _tmain(int argc, TCHAR *argv[])
{ 
    // If command-line parameter is "install", install the service. 
    // Otherwise, the service is probably being started by the SCM.

    if( lstrcmpi( argv[1], TEXT("install")) == 0 )
    {
        SvcInstall();
        return;
    }

    // TO_DO: Add any additional services for the process to this table.
    SERVICE_TABLE_ENTRY DispatchTable[] = 
    { 
        { SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain }, 
        { NULL, NULL } 
    }; 
 
    // This call returns when the service has stopped. 
    // The process should simply terminate when the call returns.

    if (!StartServiceCtrlDispatcher( DispatchTable )) 
    { 
        SvcReportEvent(TEXT("StartServiceCtrlDispatcher")); 
    } 
} 

다음은 메시지 컴파일러에서 생성된 예제 Sample.h입니다. 자세한 내용은 Sample.mc 참조하세요.

 // The following are message definitions.
//
//  Values are 32 bit values layed out as follows:
//
//   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
//   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
//  +---+-+-+-----------------------+-------------------------------+
//  |Sev|C|R|     Facility          |               Code            |
//  +---+-+-+-----------------------+-------------------------------+
//
//  where
//
//      Sev - is the severity code
//
//          00 - Success
//          01 - Informational
//          10 - Warning
//          11 - Error
//
//      C - is the Customer code flag
//
//      R - is a reserved bit
//
//      Facility - is the facility code
//
//      Code - is the facility's status code
//
//
// Define the facility codes
//
#define FACILITY_SYSTEM                  0x0
#define FACILITY_STUBS                   0x3
#define FACILITY_RUNTIME                 0x2
#define FACILITY_IO_ERROR_CODE           0x4


//
// Define the severity codes
//
#define STATUS_SEVERITY_WARNING          0x2
#define STATUS_SEVERITY_SUCCESS          0x0
#define STATUS_SEVERITY_INFORMATIONAL    0x1
#define STATUS_SEVERITY_ERROR            0x3


//
// MessageId: SVC_ERROR
//
// MessageText:
//
//  An error has occurred (%2).
//  
//
#define SVC_ERROR                        ((DWORD)0xC0020001L)

서비스 진입점

전체 서비스 샘플