#include <Psapi.h>
CString GetProcessNameByID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
//GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName)/sizeof(TCHAR));
}
}
// Print the process name and identifier.
TRACE(TEXT("%s (PID: %u)\n"), szProcessName, processID );
_tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );
CloseHandle( hProcess );
return CString(szProcessName);
}
//************************************
// Method: CheckProcessIsAlive
// FullName: CheckProcessIsAlive
// Access: public
// Returns: bool
// Qualifier: 检查对应路径的程序是否在进程列表中
// Parameter: CString filePath
//************************************
bool CheckProcessIsAlive(CString filePath)
{
//////////////////////////////////////////////////////////////////////////
/*
* 遍历所有进程 得到进程ID 通过ID 得到模块名字
*/
HANDLE Snapshot;
Snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
//获得某一时刻系统的进程、堆(heap)、模块(module)或线程的快照信息
PROCESSENTRY32 processListStr;
processListStr.dwSize=sizeof(PROCESSENTRY32);
BOOL return_value;
return_value=Process32First(Snapshot,&processListStr);
//获得系统进程链表中第一个进程的信息
int i=0;//item index
while (return_value)
{
CString sFileName,sGetPath;
//sGetPath = getFilePath(processListStr.th32ProcessID);
sGetPath = GetProcessNameByID(processListStr.th32ProcessID);
sFileName = (sGetPath=="")? processListStr.szExeFile:sGetPath;
if (filePath.MakeLower() ==sFileName.MakeLower())
{
CloseHandle(Snapshot);
return true;
}
return_value=Process32Next(Snapshot,&processListStr);
//获得系统进程链表中下一个进程的信息
i++;
}
CloseHandle(Snapshot);
return false;
}
试试这个。。。
回复 更多评论