7

Possible Duplicate:
How to get the application executable name in Windows (C++ Win32 or C++/CLI)?

I can find what directory the process is running in using GetCurrentDirectory(), but what about finding the directory the executabke resides in?

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • This question is distinct from [How to get the application executable name in Windows (C++ Win32 or C++/CLI)?](http://stackoverflow.com/q/124886/145173) This question asks how to get the path to the directory. The other question asks how to get the path to the file. – Edward Brey Jun 17 '14 at 01:01
  • 1
    It's not meaningfully different (note how the *accepted* answer to this references the same function as those in the linked dupe, just with less detail). Splitting the result into directory and filename after is trivial. – nobody Jun 17 '14 at 01:36

2 Answers2

14

GetModuleFileName or GetModuleFileNameEx.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
-2

In visual C++, we use

CString m_sAppFolder =  __targv[0] ;

which returns a string like "C:\blah\blah\executable_name.exe"

  • 4
    -1 for: 1. Unneeded dependence on MFC. 2. Having the same problem as Neil Moss's answer. 3. Using nonstandard `__targv`. – Billy ONeal Jul 29 '10 at 16:19
  • 2
    This technique should not be used. Callers of `CreateProcess` can make up whatever value they want for `argv[0]` - it doesn't even have to exist. – nobody Jun 17 '14 at 01:38