Getting path of MFC application executable 
Author Message
 Getting path of MFC application executable

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



Tue, 08 Mar 2005 17:50:27 GMT  
 Getting path of MFC application executable
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

Quote:
>-----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
Quote:
>of doing this.....?

>Thanks

>.



Tue, 08 Mar 2005 18:42:38 GMT  
 Getting path of MFC application executable
Or call GetModuleDirectory(0)
mith 0 it get you the exe  for your own process

Fred



Quote:
> 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



Tue, 08 Mar 2005 18:55:24 GMT  
 Getting path of MFC application executable
    Use GetModuleFileName(..). GetCurrentDirectory() will not yield the
correct result when your application is run from a shortcut.

--
Cheers
Check Abdoul
----------------

Quote:

> 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



Tue, 08 Mar 2005 19:44:23 GMT  
 Getting path of MFC application executable
I doubt that this is fail-safe.  I believe that you can alter the current
directory before your program ever gets started by specifying a path in the
"Start in" directory on the .lnk file shortcut.

TFM3

Note: Spam-resistant e-mail address


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

> >.



Tue, 08 Mar 2005 19:43:12 GMT  
 Getting path of MFC application executable
GetModuleFileName(...) returns the full path of your executable.
_tsplitpath(...) / _tmakepath(...) can be used to extract the portions you
need and reconstruct the part that you want.

TFM3

Note: Spam-resistant e-mail address

Quote:

> 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



Tue, 08 Mar 2005 19:41:39 GMT  
 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


Fri, 11 Mar 2005 08:12:38 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Getting executable path

2. Getting current executable path in C++

3. Get the application executable's path

4. Determining path to MFC executable

5. How to get the application startup path in an MFC application

6. Getting Application Path in C#?

7. Getting the application's path

8. Q: Getting Application's Path?

9. Getting Application's Path?

10. Getting the application's path

11. How can an application gets its current Path ?

12. Getting Application Path

 

 
Powered by phpBB® Forum Software