• C

EnumServicesStatus Vs. EnumServicesStatusEx

I've a problem when using either EnumServicesStatus or EnumServicesStatusEx.

When using EnumServicesStatus it returns 85 services.

When using EnumServicesStatusEx it returns only 70 services (and no, i haven't deleted any services). I noticed that the Alerter service is showing up when using EnumServicesStatus but not when i'm using EnumServicesStatusEx.

All the parameteres are the same:
SC_ENUM_TYPE       InfoLevel = SC_ENUM_PROCESS_INFO;
DWORD            dwServiceType = SERVICE_WIN32,
            dwServiceState = SERVICE_STATE_ALL,
                      chBufSize = 30720,
            dwServicesReturned,
            dwBytesNeeded,
            dwResumeHandle = 0,
            dwProcID;
LPCTSTR                  pszGroupName = NULL;
LPENUM_SERVICE_STATUS_PROCESS lpServices;

Any1 got any explanations?
jead99Asked:
Who is Participating?
 
KurtVonConnect With a Mentor Commented:
This code worked perfectly (the first looped 3 times, the second twice):

    ENUM_SERVICE_STATUS Services[100];
    DWORD dwIndex = 0;
    DWORD cbNeeded, dwRet, dwHandle = 0;
    SC_HANDLE hSC = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
    do
    {
        EnumServicesStatus(hSC, SERVICE_WIN32, SERVICE_STATE_ALL,
                           &(Services[dwIndex]), sizeof(Services[0]) * (100 - dwIndex), &cbNeeded,
                           &dwRet, &dwHandle);
        dwIndex += dwRet;
    } while ((dwHandle != 0) && (dwIndex < 100));


    dwHandle = 0;
    dwIndex = 0;
    ENUM_SERVICE_STATUS_PROCESS ServicesEx[100];
    do
    {
        EnumServicesStatusEx(hSC, SC_ENUM_PROCESS_INFO, SERVICE_WIN32,
                             SERVICE_STATE_ALL, (BYTE*)&(ServicesEx[dwIndex]),
                             sizeof(ServicesEx[0]) * (100 - dwIndex),
                             &cbNeeded, &dwRet, &dwHandle,
                             NULL);
        dwIndex += dwRet;
    } while ((dwHandle != 0) && (dwIndex < 100));
0
 
KurtVonCommented:
Check and make sure GetLastError isn't returning ERROR_MORE_DATA.  The current size is about 384 bytes each for 80 items and 438 bytes each for 70.  I know the Ex version returns bigger structs, but only 16 bytes or so.  Still worth checking out though.

Note that the Ex version returns ERROR_MORE_DATA but the original returns ERROR_INSUFFICIENT_BUFFER, so if you are catching the error already, make sure you are checking for the correct one.

Hope this helps.
0
 
jead99Author Commented:
Allready tried that, it doesn't return any errors.
0
 
KurtVonCommented:
Keep in mind that if there is more data the function call doesn't return an error.  You still have to check the GetLastError() value.  Dumb, I know, but the logic seems to be that since the function returns as much data as can fit in the buffer you provided it did succeed, even if there was an "error" of the buffer being too small.
0
 
KurtVonCommented:
By the way, I tried running this code as a test:

    ENUM_SERVICE_STATUS Services[100];
    DWORD cbNeeded, dwRet, dwHandle = 0;
    SC_HANDLE hSC = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
    EnumServicesStatus(hSC, SERVICE_WIN32, SERVICE_STATE_ALL,
                       Services, sizeof(Services), &cbNeeded,
                       &dwRet, &dwHandle);


    dwHandle = 0;
    ENUM_SERVICE_STATUS_PROCESS ServicesEx[100];
    EnumServicesStatusEx(hSC, SC_ENUM_PROCESS_INFO, SERVICE_WIN32,
                         SERVICE_STATE_ALL, (BYTE*)ServicesEx, sizeof(ServicesEx),
                         &cbNeeded, &dwRet, &dwHandle,
                         NULL);

and despite there being more than enough entries, both required multiple calls to feed through the entire services set, even though there was plenty of room in the array.  In the end they returned the same number of services, though.

I'm surprised about the alerter service.  It showed up first on both lists for me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.