|
-
September 20th, 2010, 10:30 AM
#5
Re: How To Get Path of other Exe file in my MFC Code
 Originally Posted by AbhiMFC
Dont we have any Library Function to get the Path.
AFAIK there is no Windows API function to directly retrieve the full path given the executable/process (other than caller process) name.
Here is another example using PSAPI (Process Status API) functions EnumProcesses, GetModuleBaseName, and GetModuleFileNameEx:
Code:
#include <Psapi.h>
#pragma comment(lib, "Psapi.lib")
BOOL GetProcessPath(LPCTSTR pszExeFile, CString& strPath)
{
BOOL bFound = FALSE;
const DWORD cb = 1024; // assume enough
DWORD dwBytesReturned = 0;
DWORD dwIDs[cb];
if(::EnumProcesses(dwIDs, cb, &dwBytesReturned))
{
size_t size = dwBytesReturned / sizeof(DWORD);
CString strBaseName;
const DWORD dwSize = MAX_PATH + 1;
for(size_t index = 0; index < size; index++)
{
HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE, dwIDs[index]);
::GetModuleBaseName(hProcess, NULL,
strBaseName.GetBufferSetLength(dwSize), dwSize);
strBaseName.ReleaseBuffer();
if(!strBaseName.CompareNoCase(pszExeFile))
{
::GetModuleFileNameEx(hProcess, NULL,
strPath.GetBufferSetLength(dwSize), dwSize);
bFound = TRUE;
}
::CloseHandle(hProcess);
}
}
return bFound;
}
Not so much sweat like in case of using WMI, but sweat a little...
Last edited by ovidiucucu; September 20th, 2010 at 10:45 AM.
Reason: typo
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|