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.