Click here to Skip to main content
15,113,692 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm writing a code to list and print the windows services in a file. It runs fine and i have attached the code here.
FILE *fp;
fp=fopen("file.txt","w");
if ((EnumServicesStatusEx(
                    scHandle,
                    SC_ENUM_PROCESS_INFO,
                    SERVICE_TYPE_ALL,
                    SERVICE_STATE_ALL,
                    data,
                    bytesNeeded,
                    &bytesNeeded,
                    &servicesReturnedCount,
                    NULL,
                    NULL)))
                {
                  fprintf(fp,"works");
                    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);

                    }
                    fclose(fp);
                  
                }

The problem I'm having is that I don't know how to fetch and write the enumerated list of services. I have used a struct to store. I'm not sure if i'm fetching the list of services correctly.

What I have tried:

I have used
 auto essp = reinterpret_cast<LPENUM_SERVICE_STATUS_PROCESS>(buffer.data());
for (int i = 0 ; i < servicesReturnedCount; ++i)
                  { 
                     auto ssp = ServiceStatusProcess{};
                     ssp.ServiceName = essp[i].lpServiceName;
                     ssp.DisplayName = essp[i].lpDisplayName;
                     ssp.Status = essp[i].ServiceStatusProcess;

                     ssps.push_back(ssp);
                  }
in C++. I'm not familiar in c. Kindly help me with this.
Posted
Updated 4-May-21 7:30am
Comments
KarstenK 4-May-21 12:54pm
   
you need to allocate some memory for the data fetch. Try to learn the language when you want to talk to the computer ;-)

1 solution

This is essentially the same question you already posted at List windows services in a file using C[^]. And I already explained how you need to allocate the memory for the returned data. And again you are trying to extract data from a buffer that has not been allocated.
   

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