CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: How To Get Path of other Exe file in my MFC Code

    Quote Originally Posted by AbhiMFC View Post
    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
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured