Find out the path of the calling executable from a DLL

This is a discussion on Find out the path of the calling executable from a DLL within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. What I seek here is for you to point me to the right direction. I'm working on a ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1
    Hello everyone.

    What I seek here is for you to point me to the right direction.

    I'm working on a quite large project. It is a dynamic link library that does a lot of things for the calling exe.

    I've added loads of functions and at one point I just found out that I need to find out the path of the calling Exe (the exe with which my dll gets attached).

    I've been thinking about either getting the PID and extracting the process information (on location of exe) from it... But probably there is a MUCH easier way. Maybe even a macro or some easy library.

    Any kind of help will be deeply appreciated.

    Platform is Windows XP/7, Compiler is GCC, It's C++.

    Well, I am using this one now -

    Code:
    char strExePath [MAX_PATH];
    GetModuleFileName (NULL, strExePath, MAX_PATH);
    printf("%s\n", strExePath);
    Last edited by iShafayet; 04-01-2011 at 02:17 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    I assume you are allowed to use win32 API functions.

    Look up GetModuleFileName(), GetModuleFilenameEx(). With NULL as the first argument, these give you ways of retrieving the executable file name.

    Note those functions don't necessarily retrieve full path information. However, either GetFullPathName() or SearchPath() will - depending on your specific needs - be helpful in such cases.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    You can call NtQueryProcessInformation() and use the ProcessImageFileName info class to determine the path to the executable image.

    In reality, this information is stored down in the PEB structure which describes the running process -- NtQueryProcessInformation() is just a convenience function for accessing that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    That's a pretty bad idea:
    It almost requires dynamic linking. Nobody who isn't a driver dev has the WDK
    It returns the path in native format, which is useless to every other function whose name doesn't start with Nt
    It requires a trip to kernel mode, GetModuleFileName doesn't

    If, for some bizarre reason you prefer that function, you want the ProcessImageFileNameWin32 info class, which is Vista+. At which point, just use QueryFullProcessImageName which is a wrapper around both, or just stick with GMFN.

    Quote Originally Posted by grumpy
    Note those functions don't necessarily retrieve full path information
    Quote Originally Posted by http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx
    Retrieves the fully-qualified path for the file that contains the specified module

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Print the path of the executable
    By Termos in forum C Programming
    Replies: 14
    Last Post: 12-04-2003, 10:29 AM
  3. can .exe find the path to a file?
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 06-12-2002, 01:08 PM
  4. Executable with unwanted dll dependency (Borland)
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-23-2002, 04:04 AM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21