獲取系統(tǒng)運行進程:
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;
因為前兩日漏了紅色的那句 進程對象初始化大小,搞出莫名其妙的錯誤, 有時能正確運行,有時毫無理由的失敗```
又是忘記“變量使用前要先初始化”的原則。廢時耗力
恨啊
《代碼大全》確是一部好書,章章經(jīng)典。
愛華網(wǎng)



