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;
}
... {
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:
Header | Declared in Psapi.h. |
---|---|
Library | Link to Psapi.lib. |