접기
SC_HANDLE hScm = OpenSCManager (NULL, NULL, SC_MANAGER_CREATE_SERVICE );
if (hScm==NULL) {
MessageBox(hDlgMain,"SCM을 열 수 없습니다.","알림",MB_OK);
return;
}
SC_HANDLE hSrv = CreateService (hScm, "...", DISPNAME, SERVICE_PAUSE_CONTINUE |
SERVICE_CHANGE_CONFIG, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START,
SERVICE_ERROR_IGNORE, sSrvPath, NULL, NULL, NULL, NULL, NULL);
if (hSrv != NULL) {
SERVICE_DESCRIPTION lpDes;
lpDes.lpDescription = sDesc;
ChangeServiceConfig2 (hSrv, SERVICE_CONFIG_DESCRIPTION , &lpDes);
MessageBox(hDlgMain, "설치했습니다.", "알림", MB_OK);
CloseServiceHandle (hSrv);
}
hSrv = OpenService (hScm, "..", SERVICE_ALL_ACCESS );
if (DeleteService (hSrv)) {
MessageBox(hDlgMain,"서비스를 제거했습니다.","알림",MB_OK);
}
CloseServiceHandle (hSrv);
CloseServiceHandle (hScm);
접기 접기
SERVICE_STATUS ss;
SC_HANDLE hScm = OpenSCManager (NULL, NULL, GENERIC_READ );
SC_HANDLE hSrv = OpenService (hScm, "..", SERVICE_ALL_ACCESS );
if (StartService (hSrv,0,NULL) == TRUE) {
QueryServiceStatus (hSrv, &ss);
while (ss.dwCurrentState != SERVICE_RUNNING) {
Sleep(ss.dwWaitHint);
QueryServiceStatus (hSrv, &ss);
}
}
ControlService (hSrv, SERVICE_CONTROL_STOP , &ss);
CloseServiceHandle (hSrv);
CloseServiceHandle (hScm);
접기
접기
void MySetStatus(DWORD dwState, DWORD dwAccept=SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE)
{
SERVICE_STATUS ss;
ss.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
ss.dwCurrentState=dwState;
ss.dwControlsAccepted=dwAccept;
ss.dwWin32ExitCode=0;
ss.dwServiceSpecificExitCode=0;
ss.dwCheckPoint=0;
ss.dwWaitHint=0;
// 현재 상태를 보관해 둔다.
SetServiceStatus (s_hSrv, &ss);
s_NowState=dwState;
}
static HANDLE s_ExitEvent;
static BOOL s_bPause;
void MyServiceHandler(DWORD fdwControl)
{
HANDLE hFile;
// 현재 상태와 같은 제어 코드일 경우는 처리할 필요 없다.
if (fdwControl == s_NowState)
return;
switch (fdwControl) {
case SERVICE_CONTROL_PAUSE:
MySetStatus(SERVICE_PAUSE_PENDING,0);
s_bPause=TRUE;
MySetStatus(SERVICE_PAUSED);
break;
case SERVICE_CONTROL_CONTINUE:
MySetStatus(SERVICE_CONTINUE_PENDING,0);
s_bPause=FALSE;
MySetStatus(SERVICE_RUNNING);
break;
case SERVICE_CONTROL_STOP:
MySetStatus(SERVICE_STOP_PENDING,0);
SetEvent(s_ExitEvent);
break;
...
case SERVICE_CONTROL_INTERROGATE:
default:
MySetStatus(s_NowState);
break;
}
}
void MyServiceMain(DWORD argc, LPTSTR *argv)
{
// 서비스 핸들러를 등록한다.
s_hSrv = RegisterServiceCtrlHandler ("...",(LPHANDLER_FUNCTION)MyServiceHandler);
if (s_hSrv==0) {
return;
}
// 서비스가 시작중임을 알린다.
MySetStatus(SERVICE_START_PENDING);
// 전역 변수를 초기화한다.
s_bPause=FALSE;
// 이벤트를 생성한다.
s_ExitEvent=CreateEvent(NULL, TRUE, FALSE, "...");
// 초기화 코드
...
// 서비스가 시작되었음을 알린다.
MySetStatus(SERVICE_RUNNING);
// 서비스 작동
for (;;) {
if (s_bPause == FALSE) {
...
}
if (WaitForSingleObject(s_ExitEvent, 10000) == WAIT_OBJECT_0)
break;
}
MySetStatus(SERVICE_STOPPED);
}
int main(int argc, char* argv[])
{
// {
SERVICE_TABLE_ENTRY ste[]={
{ "...", (LPSERVICE_MAIN_FUNCTION)MyServiceMain },
{ NULL, NULL}
};
StartServiceCtrlDispatcher (ste);
// }
// printf("Hello World!\n");
return 0;
}
접기