Check if a services is installed using C

前端 未结 2 939
无人及你
无人及你 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;
    
    }
    
    0 讨论(0)
  • 2021-01-25 08:09

    A possibility would be to always attempt to CreateService() and if failure query GetLastError() and check if it is equal to ERROR_SERVICE_EXISTS:

    SC_HANDLE service_handle = CreateService(...);
    if (0 == service_handle)
    {
        if (ERROR_SERVICE_EXISTS == GetLastError())
        {
            /* Handle service already exists. */
        }
        else
        {
            /* Handle failure. */
        }
    }
    

    This would require a slight change to your code from having two functions InstallService() and CheckService() to having one function (for example) EnsureServiceInstalled().

    Or, you could use the OpenService() function, which will fail with GetLastError() code of ERROR_SERVICE_DOES_NOT_EXIST:

    SC_HANDLE scm_handle = OpenSCManager(0, 0, GENERIC_READ);
    
    if (scm_handle)
    {
        SC_HANDLE service_handle = OpenService(scm_handle,
                                               "the-name-of-your-service",
                                               GENERIC_READ);
        if (!service_handle)
        {
            if (ERROR_SERVICE_DOES_NOT_EXIST != GetLastError())
            {
                fprintf(stderr,
                        "Failed to OpenService(): %d\n",
                        GetLastError());
            }
            else
            {
                /* Service does not exist. */
                fprintf(stderr, "Service does not exist.\n");
            }
        }
        else
        {
            fprintf(stderr, "Opened service.\n");
            CloseServiceHandle(service_handle);
        }
    
        CloseServiceHandle(scm_handle);
    }
    else
    {
        fprintf(stderr,
                "Failed to OpenSCManager(): %d\n",
                GetLastError());
    }
    
    0 讨论(0)
提交回复
热议问题