Sign in to follow this  
CProgrammer

dll current directory

Recommended Posts

Hi guys, Im trying to load a file within dll code. It seemed logical to me that opening it by specifying just the filename should open it in the same directory as the dll is in. Sadly this is not the case. Whats an elegant way to achieve this effect. Do I have to get the dlls path from its handle and then cut off the filename and add mine? Any better way? EDIT: Oh by the way this is in c++, windows with no stl. Thanks. -CProgrammer

Share this post


Link to post
Share on other sites
Just a guess: if you'r running from the debugger the current directory won't be the output directory (Debug or Release) but the one that contains your project file (usually one directory level lower).

Share this post


Link to post
Share on other sites
Hmm well im not starting it from the compiler. However reading your post reminded me that since this project is built ontop of someone elses code the active dircetory could be anything.
However is there a command to get the directory of the current process which should be the dll I made?

Share this post


Link to post
Share on other sites
The current directory is the same process-wide. It doesn't matter if you're in a DLL or not. So basically yeah, you need to get the path to your DLL (GetModuleFilename will give this to you) and mangle it yourself.

Share this post


Link to post
Share on other sites
You can get the active directory using GetCurrentDirectory.

If you want the path in which a module resides, use GetModuleFileName with a handle to the module. This will return the full path and file-name of the module. You can get the module handle by passing the unqualified module name (e.g. "plugin.dll") to GetModuleHandle. If you pass NULL to GetModuleFileName, it will return the directory of the executable file from which the process was spawned.

Admiral

Share this post


Link to post
Share on other sites
Hi guys,

thanks for the replys. Well having no choice I obtained the file path to the dll and edited it using memcpy and a while statement. Not the prettiest but it'll do :)

Thanks.

-CProgrammer

Share this post


Link to post
Share on other sites
Once you've got the full path to the module (GetModuleFileName), you can use PathFindFileName to get a pointer to the filename part of the path. Stick a null character there to get rid of the filename part and be left with the directory only. Then use PathCombine to combine the directory with the name of the file you're trying to open.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this