using EnumServices function to enumerator all the running services
DWORD dwServiceEntries = 20;//we assume the number of services is less than 20
DWORD nLen = dwServiceEntries * sizeof(ServiceEnumInfo) + dwServiceEntries * sizeof(TCHAR) * 50;
//here we assume the max length of the dll name is 50
LPBYTE buffer = new BYTE[nLen];
memset(buffer,0,nLen);
EnumServices(buffer, &dwServiceEntries,&nLen);
//be sure the GetLastError is 0, if it is ERROR_INSUFFICIENT_BUFFER
// we should malloc larger memory
ServiceEnumInfo* info = new ServiceEnumInfo[dwServiceEntries];
int LEN = sizeof(ServiceEnumInfo);
LPBYTE szTemp = buffer;
for(int i = 0; i < dwServiceEntries; i++)
{
memcpy(&info[i],szTemp,sizeof(ServiceEnumInfo));
// here you can see that the servicesHandle member is zero? why?
szTemp += sizeof(ServiceEnumInfo);
}
LPTSTR szDLLName = (LPTSTR)szTemp;
for(int i = 0; i < dwServiceEntries; i++)
{
int len = wcslen(szDLLName) + 1;
info[i].szDllName = new TCHAR[len];
memset(info[i].szDllName,0,sizeof(TCHAR)*len);
wcscpy_s(info[i].szDllName,len,szDLLName);
szDLLName += len;
}
// ....
// Do something else with the services, or find some specifical service
// ....
//Delete, free memory
for (int i = 0; i < dwServiceEntries; i++)
{
delete []info[i].szDllName;
info[i].szDllName = NULL;
}
delete []info;
info = NULL;