Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I'm trying to print the list of windows services in a file. I have been trying this code.
                if ((EnumServicesStatusEx(
                    scHandle,
                    SC_ENUM_PROCESS_INFO,
                    SERVICE_TYPE_ALL,
                    SERVICE_STATE_ALL,
                    data,
                    bytesNeeded,
                    &bytesNeeded,
                    &servicesReturnedCount,
                    NULL,
                    NULL))) //looping here
                {
                    
                    LPENUM_SERVICE_STATUS_PROCESS essp = { NULL };

                    for (DWORD i = 0; i < servicesReturnedCount; ++i)
                    {
                        serv[i].ServiceName = essp[i].lpServiceName;
                        serv[i].DisplayName = essp[i].lpDisplayName;
                        serv[i].Status = essp[i].ServiceStatusProcess;
                        fprintf(fp, "%s %s %s", (char *)serv[i].ServiceName,(char *)serv[i].DisplayName, serv[i].Status);

                    }
                  
                }
                else
                {
                    break;
               }
	
}


What I have tried:

I'm using visual studio 2019. The code runs fine and it is generating .exe file. It kind of gets into a loop(mentioned above) and it just exits. I'm new to this. So, kindly tell me what I'm doing wrong here.
Posted
Updated 5-May-21 0:12am
v2
Comments
Richard MacCutchan 5-May-21 9:06am    
I see you have updated the question, but you do not say whether you have a new problem.

C++
LPBYTE data =(unsigned char *) bytesNeeded;

You cannot allocate dynamic memory like that; you are just casting a number to a pointer, which makes no sense. You need to call one of the memory allocation routines:
C++
LPBYTE data =(unsigned char *)malloc(bytesNeeded);

And in later calls you need to check if the needed size is larger than what you previously allocated, and use realloc if necessary.
 
Share this answer
 
I have simplified your code, and removed all the non essential parts, to the following, which lists the (basic) service information. You just need to add some prettier formatting as necessary.
C++
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<winsvc.h>

int main()
{
    SC_HANDLE scHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
    if (scHandle == 0)
    {
        printf("Handle not got\n");
        exit(1);
    }
    else
    {
        printf("handle success\n");
    }

    DWORD bytesNeeded = 0; //4294967295
    DWORD servicesReturnedCount = 0;
    DWORD resumeHandle = 0;

    do
    {
        printf("Enter do\n");
        if (!EnumServicesStatusEx(
            scHandle,
            SC_ENUM_PROCESS_INFO,
            SERVICE_TYPE_ALL,
            SERVICE_STATE_ALL,
            NULL,
            0,
            &bytesNeeded,
            &servicesReturnedCount,
            &resumeHandle,
            NULL))
        {


            if (ERROR_MORE_DATA == GetLastError())
            {
                LPENUM_SERVICE_STATUS_PROCESS services = (LPENUM_SERVICE_STATUS_PROCESS)malloc(bytesNeeded);


                if ((EnumServicesStatusEx(
                    scHandle,
                    SC_ENUM_PROCESS_INFO,
                    SERVICE_TYPE_ALL,
                    SERVICE_STATE_ALL,
                    (PBYTE)services,
                    bytesNeeded,
                    &bytesNeeded,
                    &servicesReturnedCount,
                    NULL,
                    NULL))) //looping here
                {

                    for (DWORD i = 0; i < servicesReturnedCount; ++i)
                    {
                        printf("%s %s\n", services[i].lpServiceName, services[i].lpDisplayName);

                    }

                }
                else
                {
                    break;
                }
            }
            else
            {
                break;
            }
        }
    } while (resumeHandle != 0);
    CloseServiceHandle(scHandle);

    getc(stdin);
}
 
Share this answer
 
v2
Comments
[no name] 5-May-21 2:17am    
Works very well. Thank you
Richard MacCutchan 5-May-21 4:04am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900