locked
QueryServiceConfig2W always return null in structure SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO (pmszRequiredPrivileges) RRS feed

  • Question

  • Hi All, 
    I am trying to get service configuration information for SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO. my code as below

    void DoQuerySvc()
    {
        SC_HANDLE schSCManager;
        SC_HANDLE schService;
        DT_String  szSvcName = Server.GetProductName(); // get the service name
        LPSERVICE_REQUIRED_PRIVILEGES_INFO lpsd;
        DWORD dwBytesNeeded = 0, dwError = 0; 
        DWORD cbBufSize = 0;

        // Get a handle to the SCM database. 

        schSCManager = OpenSCManager( 
            NULL,                    // local computer
            NULL,                    // ServicesActive database 
            SC_MANAGER_ALL_ACCESS);  // full access rights 

        if (NULL == schSCManager) 
        {
            printf("OpenSCManager failed (%d)\n", GetLastError());
            return;
        }

        // Get a handle to the service.

        schService = OpenService( 
            schSCManager,          // SCM database 
            szSvcName,             // name of service 
            SERVICE_QUERY_CONFIG); // need query config access 

        if (schService == NULL)
        { 
            printf("OpenService failed (%d)\n", GetLastError()); 
            CloseServiceHandle(schSCManager);
            return;
        }

        // Get the configuration information.

       
    if( !QueryServiceConfig2W( 
            schService, 
            SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO,
            NULL, 
            0, 
            &dwBytesNeeded))
        {
            dwError = GetLastError();
            if( ERROR_INSUFFICIENT_BUFFER == dwError )
            {
                cbBufSize = dwBytesNeeded;
    lpsd = (LPSERVICE_REQUIRED_PRIVILEGES_INFO) LocalAlloc(LMEM_FIXED, cbBufSize);
            }
            else
            {
                printf("QueryServiceConfig2 failed (%d)", dwError);
                goto cleanup; 
            }
        }

        if (! QueryServiceConfig2W( 
            schService, 
    SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO,
            (LPBYTE) lpsd, 
            cbBufSize, 
            &dwBytesNeeded) ) 
        {
    dwError = GetLastError();
            printf("QueryServiceConfig2 failed (%d)", GetLastError());
            goto cleanup;
        }
      
    //bool bvalue = lpsd->pmszRequiredPrivileges;
        if (lpsd->pmszRequiredPrivileges != NULL && lstrcmp([b]lpsd->pmszRequiredPrivileges[/b], TEXT("")) != 0)
            _tprintf(TEXT("  Description: %s\n"), lpsd->pmszRequiredPrivileges);
       
        LocalFree(lpsd);

    cleanup:
        CloseServiceHandle(schService); 
        CloseServiceHandle(schSCManager);
    }
    For second time when the call is made to QueryServiceConfig2W need to get information about privilege value lpsd->pmszRequiredPrivileges but getting it as null always
    Thursday, April 12, 2018 7:24 AM

All replies

  • First, I assume that since the code contains a mixture of Unicode specific calls (e.g., QueryServiceConfig2W) and generic functions (_tprintf) and generic pointer declarations (LPSERVICE_REQUIRED_PRIVILEGES_INFO) that you are building for UNICODE.

    If I understand correctly you are saying that after determining the required buffer size and allocating the buffer that the call to QueryServiceConfig2 to retrieve the data is failing.  If so, what does GetLastError() return?

    How does the code function if you try to retrieve the privilege information from a different service?
    • Edited by RLWA32 Saturday, April 14, 2018 5:44 PM added question
    Saturday, April 14, 2018 3:19 PM