윈도우 서비스 제거하기

-인자로 전달된 서비스명의 윈도우 서비스를 제거한다.

#include

#define BUFSIZE 512

void RemoveService(char* sc_name){
        SC_HANDLE hManager;
        SC_HANDLE hService;
        SERVICE_STATUS sStat;
        char path[BUFSIZE];

        hManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

        hService=OpenService(hManager,sc_name,SERVICE_ALL_ACCESS);

        if(hService){
                //서비스 실행 중지
                ControlService(hService,SERVICE_CONTROL_STOP,&sStat);

                while(QueryServiceStatus(hService,&sStat)){
                        if(sStat.dwCurrentState == SERVICE_STOP_PENDING){
                                Sleep(1000);
                        }else{
                                break;
                        }
                }

                //서비스 제거
                if(sStat.dwCurrentState == SERVICE_STOPPED){
                        DeleteService(hService);
                }

                CloseServiceHandle(hService);
        }

        CloseServiceHandle(hManager);
}


댓글 남기기

바로가기