dll current directory

Started by
5 comments, last by phil_t 16 years, 10 months ago
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
Advertisement
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).
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?
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.
-Mike
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
Ring3 Circus - Diary of a programmer, journal of a hacker.
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
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.

This topic is closed to new replies.

Advertisement