
Getting path of MFC application executable
Absolutely, positively NOT! This will not work! The current directory can be arbitrarily
set by the user in a shortcut. This means that the current directory is set by
CreateProcess and you have no idea if you can possibly be doing anyting "before any code
that changes the current directory". So the use of GetCurrentDirectory is nonsensical. It
might work some times, but it is certainly not guaranteed and I wouldn't trust anything
this meaningless and accidental. If you notice the current directory is the executable
directory, you are observing a mere coincidence.
TCHAR home;
GetModuleFileName(NULL, home, MAX_PATH);
TCHAR drive[MAX_PATH];
TCHAR path[MAX_PATH];
_tsplitpath(home, drive, path, NULL, NULL);
TCHAR myDataFile[MAX_PATH];
_tmakepath(myDataFile, drive, path, _T("MYDATA"), _T(".DAT"));
now you have the desired path based on where the executable is, not on something as
useless as the current directory.
joe
Quote:
>Call GetCurrentDirectory() (straight Windows API) in your
>code, before any code that changes the current directory
>(if any), and it will be the directory where the EXE is
>running from.
>Jeff
>>-----Original Message-----
>>I want to get the path of my MFC's EXE so I can use that
>as the default
>>directory to store other data. What is the best way to
>get this? Was
>>thinking of using m_pszHelpFilePath, but I'm sure there
>must be a better way
>>of doing this.....?
>>Thanks
>>.
Joseph M. Newcomer [MVP]
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm