Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"


C++
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)//szProcessName == L"Unknown \r\n")
		{
			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;
	
	// Calculate how many process identifiers were returned.
	howManyProcesses = cbNeeded / sizeof(DWORD);
	// Print the name and process identifier for each process.

	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 ?
Posted
Comments
barneyman 5-Dec-14 18:56pm    
elevation?
Philippe Mori 5-Dec-14 19:08pm    
One thing is sure is that when you return early, you don't properly close hProcess handle.
Member 11287295 5-Dec-14 20:25pm    
you're right, I absolutely have to keep an eye on errors of this kind

1 solution

If you read the addendum to the example code you used:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682623(v=vs.85).aspx[^]
it explains:
If OpenProcess fails, the output shows the process name as unknown. For example, OpenProcess fails for the Idle and CSRSS processes because their access restrictions prevent user-level code from opening them.

Yours is user level code of course.
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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