枚举进程调用Process32First失败返回24
在进行系统进程枚举时,调用了windows的API Process32First,结果调用失败,使用GetLastError返回错误号为24,这是因为没有初始化它的LPPROCESSENTRY32结构体dwSize值。
在MSDN对Process32First函数的介绍Remarks中提到,必须要为PROCESSENTRY32的dwSize赋值:
[php]
The calling application must set the dwSize member of PROCESSENTRY32 to the size, in bytes, of the structure.
[/php]
枚举进程代码如下:
[php]
int CMonitorModule::EnumProcess()
{
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
DWORD error = GetLastError();
printf("Cannot get system process snapshot (%d)\n", error);
return -1;
}
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32); //必须填充结构体的大小值
if(!Process32First(hProcessSnap, &pe32))
{
DWORD error = GetLastError();
printf("Process32First failed (%d)\n",error );
CloseHandle(hProcessSnap);
return -1;
}
do
{
printf(Process name :%s\n", pe32.szExeFile);
} while(Process32Next(hProcessSnap, &pe32));
CloseHandle( hProcessSnap );
return 0;
}
[/php]