Check if a services is installed using C

前端 未结 2 940
无人及你
无人及你 2021-01-25 07:21

I\'m writing a C application which creates a Windows service. I\'d like to check if the service is installed before trying to call the installation function, but I can\'t manage

2条回答
  •  盖世英雄少女心
    2021-01-25 08:03

    If you need a yes / no answer whether a service is installed or note, use the following function:

    bool IsServiceInstalled(LPWSTR ServiceName)
    {
        bool serviceInstalled = false;
        SC_HANDLE scm_handle = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
        if (scm_handle)
        {
            SC_HANDLE service_handle = OpenService(scm_handle, ServiceName, SERVICE_INTERROGATE);
            if (service_handle != NULL)
            {
                wprintf(L"Service  Installed\n");
                serviceInstalled = true;
                CloseServiceHandle(service_handle);
            }
            else
            {
                wprintf(_T("OpenService failed - service not installed\n"));
            }
            CloseServiceHandle(scm_handle);
        }
        else
            wprintf(_T("OpenService couldn't open - service not installed\n"));
    
        return serviceInstalled;
    
    }
    

提交回复
热议问题