#include "psapi.h"
#pragma comment(lib, "psapi.lib")
// Get the process name.
INT GetProcessBaseName(DWORD processID, LPTSTR szProcessName, int iBuffLen)
{
INT iRet = 0;
ZeroMemory(szProcessName, iBuffLen*sizeof(TCHAR));
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, processID);
if(hProcess != NULL )
{
HMODULE hMod = NULL;
DWORD cbNeeded = 0;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded)
&& GetModuleBaseName( hProcess, hMod, szProcessName, iBuffLen))
{
TRACE(TEXT("%s (PID: %u)\n"), szProcessName, processID);
iRet = _tcslen(szProcessName);
}
CloseHandle(hProcess);
}
return iRet;
}
INT GetApplicatePID(LPCTSTR szExe, DWORD *pdwPIDA, int iBuffCnt)
{
INT iRet = 0;
DWORD aProcesses[1024], cbNeeded=0;
ZeroMemory(aProcesses, sizeof(aProcesses));
EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded);
int cProcess = cbNeeded/sizeof(DWORD);
for(DWORD i=0; i<cProcess; i++)
{
DWORD processID = aProcesses[i];
TCHAR szProcessName[MAX_PATH + 1] = {0};
if(!GetProcessBaseName(processID, szProcessName, MAX_PATH))
continue;
if(_tcsicmp(szProcessName, szExe) != 0)
continue;
if(pdwPIDA && iBuffCnt > 0)
{
if(iRet < iBuffCnt)
{
pdwPIDA[iRet] = processID;
iRet++;
}
}
else
{
iRet++;
}
}
return iRet;
}
//应用实例
DWORD dwPIDA[16] = {0};
int iAppCnt = GetApplicatePID(_T("Explorer.exe"), dwPIDA, 16);
//调试输出
smss.exe (PID: 980)
csrss.exe (PID: 1052)
winlogon.exe (PID: 1080)
services.exe (PID: 1124)
lsass.exe (PID: 1136)
svchost.exe (PID: 1388)
QQPCRTP.exe (PID: 1468)
svchost.exe (PID: 1600)
svchost.exe (PID: 1756)
spoolsv.exe (PID: 1840)
QQProtect.exe (PID: 1024)
alg.exe (PID: 680)
Explorer.EXE (PID: 2360)
QQPCTray.exe (PID: 2632)
ctfmon.exe (PID: 2700)
QQPCRealTimeSpeedup.exe (PID: 2532)
svchost.exe (PID: 1496)
MSDEV.EXE (PID: 3228)
taskmgr.exe (PID: 3284)
conime.exe (PID: 2328)
firefox.exe (PID: 584)
Dlg6.exe (PID: 1480) |