Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C++
Article

Get Your DLL's Path/Name

Rate me:
Please Sign up or sign in to vote.
4.05/5 (51 votes)
30 Nov 2006CPOL 165.2K   1.4K   49   23
Two methods to retrieve the path/name of a VS2002 or higher DLL from within that DLL.

Introduction

I'm working on a project that involves writing a plugin DLL for a game, and being the lazy programmer I am, I'm using the example provided by the game author and modifying it for my needs. I don't have access to the DLL's HINSTANCE (like we do with MFC DLLs). This presented a problem when I decided I needed to know the full path to the DLL in question.

The Code

Believe it or not, it only takes three lines of code to accomplish this task:
// near the top of your CPP file
EXTERN_C IMAGE_DOS_HEADER __ImageBase;

// and then, anywhere you need it:
LPTSTR  strDLLPath1 = new TCHAR[_MAX_PATH];
::GetModuleFileName((HINSTANCE)&__ImageBase, strDLLPath1, _MAX_PATH);

It seems that any EXE or DLL compiled with the VS2002 (and higher) linkers provides a psuedo-variable called __ImageBase that represents the DOS header of the module (all 32 bit binaries have this). Simply cast this variable to a HINSTANCE, and you can pass it as the first parameter to GetModuleFileName().

For those of you that need this functionality in VC6 or earlier, research the VirtualQuery() function. The approach is somewhat similar.

Disclaimers

I don't know if this will work in Vista.

The sample code includes source and the compiled EXE and DLL files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
Questionbetter way is to use GetModuleHandleEx and GetModuleFileName Pin
pwm30-May-15 17:26
professionalpwm30-May-15 17:26 
GeneralMy vote of 5 Pin
slavne13-Nov-14 17:45
slavne13-Nov-14 17:45 
GeneralMy vote of 4 Pin
khosob1-Mar-11 15:16
khosob1-Mar-11 15:16 
GeneralRe: My vote of 4 Pin
vuadapass1-Jan-12 14:19
vuadapass1-Jan-12 14:19 
GeneralNice work Pin
Taeyoung Jin4-Mar-07 17:05
Taeyoung Jin4-Mar-07 17:05 
GeneralGood work Pin
Monty22-Dec-06 1:53
Monty22-Dec-06 1:53 
GeneralRe: Good work Pin
#realJSOP2-Dec-06 2:13
mve#realJSOP2-Dec-06 2:13 
GeneralRe: Good work Pin
Monty22-Dec-06 2:17
Monty22-Dec-06 2:17 
AnswerRe: Good work Pin
GogglesPisano6-Dec-06 8:36
GogglesPisano6-Dec-06 8:36 
GeneralRe: Good work Pin
#realJSOP15-Apr-08 7:55
mve#realJSOP15-Apr-08 7:55 
QuestionWhat about DLL Rebasing Pin
DougCo30-Nov-06 10:57
DougCo30-Nov-06 10:57 
AnswerRe: What about DLL Rebasing Pin
#realJSOP30-Nov-06 12:16
mve#realJSOP30-Nov-06 12:16 
GeneralRe: What about DLL Rebasing Pin
DougCo30-Nov-06 20:12
DougCo30-Nov-06 20:12 
GeneralRe: What about DLL Rebasing Pin
#realJSOP30-Nov-06 23:27
mve#realJSOP30-Nov-06 23:27 
AnswerA more portable method Pin
mmatitya5-Dec-06 2:33
mmatitya5-Dec-06 2:33 
Doug:

Your method will only work on Win XP, Win Server 2003, or Vista, since GetModuleHandleEx() does not exist in earlier OS's. Here is an alternative method, using VirtualQuery(), that will work on any version of Windows:

<br />
    MEMORY_BASIC_INFORMATION    mbiInfo                 = { 0 };<br />
    TCHAR                       szPath  [MAX_PATH + 1]  = "";<br />
    HMODULE                     hmodModule              = NULL;<br />
    DWORD                       dwRet                   = 0;<br />
<br />
    dwRet = VirtualQuery (((LPCVOID) (&mbiInfo)), &mbiInfo,<br />
                          ((DWORD) (sizeof (MEMORY_BASIC_INFORMATION))));<br />
    if (dwRet)<br />
    {<br />
        hmodModule = ((HMODULE) (_mbiInfo.AllocationBase));<br />
<br />
        GetModuleFileName (hmodModule, szPath, MAX_PATH);<br />
    }<br />


Moshe
GeneralRe: A more portable method Pin
#realJSOP5-Dec-06 12:12
mve#realJSOP5-Dec-06 12:12 
GeneralRe: A more portable method Pin
tbrammer6-Dec-06 4:51
professionaltbrammer6-Dec-06 4:51 
GeneralRe: A more portable method Pin
#realJSOP8-Dec-06 3:51
mve#realJSOP8-Dec-06 3:51 
GeneralRe: A more portable method Pin
denis2k627-Mar-08 14:00
denis2k627-Mar-08 14:00 
GeneralRe: A more portable method - Bug on 1 line Pin
GotCodeToo24-Sep-08 13:34
GotCodeToo24-Sep-08 13:34 
GeneralRe: A more portable method - Bug on 1 line Pin
tbrammer22-Oct-08 22:28
professionaltbrammer22-Oct-08 22:28 
GeneralNot work for Vista on my case. Pin
Jinyang Yu11-Aug-09 2:38
Jinyang Yu11-Aug-09 2:38 
GeneralRe: Not work for Vista on my case. Pin
John Slagel29-Dec-11 7:53
John Slagel29-Dec-11 7:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.