获取系统运行进程:
BOOL bRe = FALSE;
DWORD err =0;
HANDLEhSnapshot;//create system snapshot
hSnapshot =CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot==INVALID_HANDLE_VALUE)
{
err = GetLastError();
printf("Error: file=%s,line=%u.n %u CreateToolhelp32Snapshot Errorn.",
__FILE__,__LINE__, err);
return FALSE;
}
PROCESSENTRY32 pe;
pe.dwSize =sizeof(PROCESSENTRY32);//getset the object size,important
if (!Process32First(hSnapshot,&pe)) //get the first process ofsystem
{
err = GetLastError();
printf("Error:file=%s,line=%u.n %u. Cann't get the first process.n",
__FILE__,__LINE__, err);
CloseHandle(hSnapshot);//close the Snapshot handle
return FALSE;
}
charpProc[MAX_KEY_LENGTH];
char *pSoft= new char[MAX_SOFWARENAME];
do
{
memset(pProc, 0,MAX_SOFWARENAME);
strncpy_s(pProc, sizeof(pProc),pe.szExeFile, sizeof(pe.szExeFile));
if(IsVirusDefenderProcess(pProc,&pSoft)==1)
{
strcpy_s(*outVirSoftwareName,MAX_SOFWARENAME, pSoft);
returnTRUE;
}
} while (Process32Next(hSnapshot,&pe)); //getthe next process
CloseHandle(hSnapshot);//close the Snapshot handle
delete []pSoft;
return FALSE;
因为前两日漏了红色的那句 进程对象初始化大小,搞出莫名其妙的错误, 有时能正确运行,有时毫无理由的失败```
又是忘记“变量使用前要先初始化”的原则。废时耗力
恨啊
《代码大全》确是一部好书,章章经典。