您的位置:首页 > 其它

Get Process ID by Name

2008-03-26 09:33 721 查看

DWORD CChatMessageDlg::GetProcessId(LPCTSTR pszProcessName)




...{


BOOL bFound = FALSE;


DWORD aProcesses [1024], cbNeeded, cProcesses;


unsigned int i;




// Enumerate all processes


if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))


return FALSE;




// Calculate how many process identifiers were returned.


cProcesses = cbNeeded / sizeof(DWORD);






TCHAR szEXEName[MAX_PATH] = ...{0};


// Loop through all process to find the one that matches


// the one we are looking for


for (i = 0; i < cProcesses; i++)




...{


// Get a handle to the process


HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |


PROCESS_VM_READ, FALSE, aProcesses[i]);




// Get the process name


if (NULL != hProcess)




...{


HMODULE hMod;


DWORD cbNeeded;




if(EnumProcessModules(hProcess, &hMod,


sizeof(hMod), &cbNeeded))




...{


//Get the name of the exe file


GetModuleBaseName(hProcess, hMod, szEXEName,


sizeof(szEXEName)/sizeof(TCHAR));




if (_tcsicmp(szEXEName, pszProcessName) == 0)




...{


bFound = TRUE;


CloseHandle(hProcess);


break;


}


}


CloseHandle(hProcess);


}


}




return bFound ? aProcesses[i] : 0;


}



Note:

HeaderDeclared in Psapi.h.

LibraryLink to Psapi.lib.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐