EnumProcess()不返回所有预期的进程 [英] EnumProcess() not returning all the expected processes

查看:65
本文介绍了EnumProcess()不返回所有预期的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用这段代码来查找我系统中的所有正在运行的进程,但它显然无法枚举所有这些进程,如果有的话,它会枚举一半正在进行的进程,并且在其枚举的三分之一进程中,失败给它一个正确的名称并留下未知





  void  PrintProcessNameAndID(DWORD processID)
{
wchar_t szProcessName [MAX_PATH] = TEXT( Unknown \\\\ n);
wchar_t CheckName [MAX_PATH] = TEXT( 未知\\ 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 == LUnknown \\\\ n)
{
AllProcesses + = szProcessName;
return ;
}

AllProcesses + = szProcessName;
AllProcesses + = L \\\\ 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 ;
}





为什么会这样?对上述代码的任何更正可能会解决问题?或者是否有某种障碍阻止应用程序枚举所有正在进行的进程?

解决方案

如果您阅读了您使用的示例代码的附录:

http://msdn.microsoft .com / zh-CN / library / windows / desktop / ms682623(v = vs.85).aspx [ ^ ]

它解释:

如果OpenProcess失败,则输出将进程名称显示为unknown。例如,OpenProcess因Idle和CSRSS进程而失败,因为它们的访问限制会阻止用户级代码打开它们。



您的用户级代码为当然

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)//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 ?

解决方案

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.


这篇关于EnumProcess()不返回所有预期的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆