You should use toolhelp API's:
HANDLE OpenProcessByName(LPCTSTR Name, DWORD dwAccess)
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
ZeroMemory(&pe, sizeof(PROCESSENTRY32));
pe.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnap, &pe);
do
{
if (!lstrcmpi(pe.szExeFile, Name))
{
return OpenProcess(dwAccess, 0, pe.th32ProcessID);
}
} while (Process32Next(hSnap, &pe));
}
return INVALID_HANDLE_VALUE;
}
For example:
#include <windows.h>
#include <tlhelp32.h>
//...
int main()
{
HANDLE hFire = OpenProcessByName("firefox.exe", PROCESS_VM_READ);
cout << "Handle: " << hFire << endl;
CloseHandle(hFire);
cin.get();
return 0;
}
But you should be careful because if there are more than 1 copy of process you will get a handle of the first. To handle all processes in "return OpenProcess" use call of some function like Handler(OpenProcess(dwAccess, 0, pe.th32ProcessID)):
void Handler(HANDLE hndl)
{
//... work with your Handle
CloseHandle(hndl);
}
//...
if (!lstrcmpi(pe.szExeFile, Name))
{
Handler(OpenProcess(dwAccess, 0, pe.th32ProcessID)):
}
//...