Jump to content
  • Advertisement
Sign in to follow this  
CrazyCamel

How to find filename and path of current DLL?

This topic is 4348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a DLL that needs to know what its location and filename is. I found the _getcwd() function, which gets the current working directory (path/location), but how do I find the filename of the DLL at runtime? Is there any way to do it, or do I have to just put in a constant? I'd prefer to find it at runtime because the DLL might be renamed on a different computer than it was compiled on. [Edited by - CrazyCamel on September 21, 2006 9:18:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by CrazyCamel
I have a DLL that needs to know what its location and filename is

That sounds pretty damn odd. For one thing, you have said that the DLL needs to know its own name/location, which means it is alredy loaded. Assuming you are doing things the standard way, the Dll is loaded with a call to LoadLibrary.......which means you allready know the name of the dll. And anyway, why would the name of the dll possibly change?

Share this post


Link to post
Share on other sites
Yes, its quite odd... very interesting situation. See, I'm registering a COM DLL from within the DLL, and it is standard to have the DLLRegisterServer() function within the DLL itself, to be loaded and called from another exe, such as regsvr32.exe, etc. All this function does is adds subkeys to the registry to give information about the DLL so that the contents may be used as COM-able objects.

The basic information I need to give is just the class name, the threading model (ie. if it can support multithreading, singlethreading, or both), and the path to the DLL, so that calling programs don't have to know where or what the DLL is to find the class, all they have to do is look it up in the registry.

So I need the path of the DLL from within the DLLRegisterServer() function, which is within the DLL, which is loaded by a registration program such as regsvr32.exe for the initial registration, which then allows programs to find the DLL by searching by the class. (I think I may have repeated myself, quite late here.) And from that all comes the question- how do I get the path of the DLL from within a function in the DLL while its loaded by another EXE?
And then came the (if not entirely perfect) answer, GetModuleFileName().


Now the only problem is that GetModuleFileName requires a module handle (if its null then it gives me the name of the EXE, which happens to be regsvr32.exe for my testing purposes, and its no good to put that in the registry). And so my new question is, oddly enough, how do I get the module handle from within the module (dll module handle from within a dll function)? I looked at GetModuleHandle(), but that requires the name of the DLL, which is what I am trying to eventually find.

Edit
Just by the way, if it isn't too obvious, this is an explanation of how I am not "doing things the standard way", as well as an update of my problem.
Even if I was doing things the standard way, how would I have the DLL's path in the scope of a function within the DLL anyway? You need the path to load the DLL from the EXE, but that doesn't mean you know it from within the DLL... either way, my problem is a bit different =)

Share this post


Link to post
Share on other sites
I think the module handle is just the base address of the DLL - I don't know the exact terminology. Probably your compiler gives you a way to get at it, otherwise you can have fun working it out with a small assembly routine - Not really advisable.

Share this post


Link to post
Share on other sites
If you have the original DirectX 9 SDK (the one with DirectShow stuff in it) or a recent platform SDK (I believe the DirectShow stuff has been moved there) you'll be able to find an example of exactly this. Take a look at the file <DXSDK>\Samples\C++\DirectShow\BaseClasses\DllSetup.cpp.

Share this post


Link to post
Share on other sites
ModuleHandle is just the base address of the PE image, but it's not trivial to determine. I've had success in the past with a bitwise-and with 0x10000 followed by a for-loop that decrements in steps of 0x10000 until it detects the PE 'magic' number, 0x5A4D. This, however, is incredibly hacky and isn't guaranteed to be compatible with future versions of Windows.

GetModuleHandleEx followed by GetModuleFileName will do the trick, provided you can get hold of some address within the module's space. You could (hack) cast the address of any function in the module, or use the address of one of its global variables.

Regards
Admiral

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!