본문 바로가기

Dev/Windows

RestartService

void RestartService(std::string strServiceName)
{
    SC_HANDLE hSCM = NULL;
    SC_HANDLE hDns = NULL;

    try {
        hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
        if (!hSCM) {
            cout << "OpenSCManager Error" << endl;
            throw -1;
        }

        hDns = OpenService(hSCM, strServiceName.c_str(), SERVICE_ALL_ACCESS);
        if (!hDns) {
            cout << "OpenService Error" << endl;
            throw -1;
        }

        SERVICE_STATUS schStatus = {0, };

        BOOL bStop = ControlService(hDns, SERVICE_CONTROL_STOP, &schStatus);

        if (bStop) {
            cout << strServiceName << " Stop" << endl;
        }

        BOOL bStart = StartService(hDns, 0, NULL);

        if (bStart) {
            cout << strServiceName << " Start" << endl;
        }
    }
    catch (...) {}

    if (hDns) {
        CloseServiceHandle(hDns);
    }
   
    if (hSCM) {
        CloseServiceHandle(hSCM);
    }
}

'Dev > Windows' 카테고리의 다른 글

COM+ Queued Components  (0) 2007.08.09
SWC (Services Without Components)  (0) 2007.08.08
RestartService  (0) 2007.07.19
MultiMedia Timer  (0) 2007.05.14
.NET Enterprise Services 성능 ( vs COM+)  (0) 2007.05.14
DLL 파일 찾는 순서  (0) 2007.05.14

태그