How to get current program directory?
Use GetModuleFileName to get full path of executing program, and use PathRemoveFileSpec to remove file name part which leaves us with parent directory path of our program.
Here is a sample function which does this…
CString GetCurrentProgramParentDirectory()
{
// Get current modules full path
TCHAR szModuleFileName[MAX_PATH] = { 0 };
GetModuleFileName( 0, szModuleFileName, MAX_PATH );
// Remove file part for e.g. SomeExe.exe from C:/SomeProgram/SomeExe.exe
// include shlwapi.h and link to shlwapi.lib
PathRemoveFileSpec( szModuleFileName );
// Return parent directory path
return CString( szModuleFileName );
}// End GetCurrentProgramParentDirectory()


That’s true BM!
Thanks posting additional information here. These days shlwapi is used in plenty AFAIK because of the utility functions it includes.
I too use above code for retrieving parent folder in projects where shlwapi is not linked into and I wouldn’t link to shlwapi just for the sake of this one function.
Well if it’s already there I would prefer to use this function. Also I wanted people to know there is something like this.
Come on, binding shlwapi.dll just to use 1 function that can be replaced by inline strrchr (or _tcsrchr for TCHARs) is in my opinion not a good way to optimize your code…
*(strrchr(szModulePath, ‘\\’) + 1) = ”;