Click here to Skip to main content
Licence CPOL
First Posted 30 Nov 2006
Views 77,812
Bookmarked 42 times

Get Your DLL's Path/Name

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)

About the Author

John Simmons / outlaw programmer

Software Developer (Senior)

United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmemberkhosob15:16 1 Mar '11  
GeneralRe: My vote of 4 [modified] Pinmembervuadapass14:19 1 Jan '12  
GeneralNice work PinmemberTaeyoung Jin17:05 4 Mar '07  
GeneralGood work PinmemberMonty21:53 2 Dec '06  
GeneralRe: Good work PinmemberJohn Simmons / outlaw programmer2:13 2 Dec '06  
GeneralRe: Good work PinmemberMonty22:17 2 Dec '06  
AnswerRe: Good work PinmemberGogglesPisano8:36 6 Dec '06  
GeneralRe: Good work PinmvpJohn Simmons / outlaw programmer7:55 15 Apr '08  
QuestionWhat about DLL Rebasing PinmemberDougCo10:57 30 Nov '06  
AnswerRe: What about DLL Rebasing PinmemberJohn Simmons / outlaw programmer12:16 30 Nov '06  
GeneralRe: What about DLL Rebasing PinmemberDougCo20:12 30 Nov '06  
GeneralRe: What about DLL Rebasing PinmemberJohn Simmons / outlaw programmer23:27 30 Nov '06  
AnswerA more portable method Pinmembermmatitya2:33 5 Dec '06  
GeneralRe: A more portable method PinmemberJohn Simmons / outlaw programmer12:12 5 Dec '06  
GeneralRe: A more portable method Pinmembertbrammer4:51 6 Dec '06  
GeneralRe: A more portable method PinmemberJohn Simmons / outlaw programmer3:51 8 Dec '06  
GeneralRe: A more portable method Pinmemberdenis2k614:00 27 Mar '08  
GeneralRe: A more portable method - Bug on 1 line PinmemberN_Nguyen13:34 24 Sep '08  
GeneralRe: A more portable method - Bug on 1 line Pinmembertbrammer22:28 22 Oct '08  
GeneralNot work for Vista on my case. PinmemberJinyang Yu2:38 11 Aug '09  
GeneralRe: Not work for Vista on my case. PinmemberJohn Slagel7:53 29 Dec '11  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 30 Nov 2006
Article Copyright 2006 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid