I'm using this code here to find all the running process in my system but it apparently fails at enumerating all of them, if anything, it's enumerating half of the ongoing processes and in a third of the ones it enumerates, fails to give it a proper name and leaves it as "Unknown"
void PrintProcessNameAndID(DWORD processID)
{
wchar_t szProcessName[MAX_PATH] = TEXT("Unknown \r\n");
wchar_t CheckName[MAX_PATH] = TEXT("Unknown \r\n");
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
&cbNeeded))
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName) / sizeof(wchar_t));
}
if (wcscmp(szProcessName, CheckName) == 0) {
AllProcesses += szProcessName;
return;
}
AllProcesses += szProcessName;
AllProcesses += L"\r\n";
CloseHandle(hProcess);
}
}
int EnumProcessesBlt()
{
DWORD allProcesses[1024], cbNeeded, howManyProcesses;
unsigned int i;
if (!EnumProcesses(allProcesses, sizeof(allProcesses), &cbNeeded))
return 1;
howManyProcesses = cbNeeded / sizeof(DWORD);
for (i = 0; i < howManyProcesses; i++)
if (allProcesses[i] != 0)
PrintProcessNameAndID(allProcesses[i]);
return 0;
}
Why so ? Any correction to the above code may fix the problem ? or is there a barrier of some sort to prevent an application to enumerate all the ongoing processes ?