I can find what directory the process is running in using GetCurrentDirectory(), but what about finding the directory the EXE resides in?
Can you give an example? It's not clear to me where I get the HMODULE handle.
John
2010-07-29 16:12:56
@John: `GetModuleHandle(NULL)`
Billy ONeal
2010-07-29 16:14:43
If you want the main executable file, `GetModuleHandle(NULL)`. If you want a DLL, the handle to that DLL is (among other things) passed when `DllMain` is called.
Jerry Coffin
2010-07-29 16:19:13
OK, cool. So this gets me a pull path including the file-name. Is there a handy function to get me the full directory path, without the file-name? e.g takes c:\temp\app.exe and returns c:\temp?
John
2010-07-29 16:28:52
@John: Yes, it's PathRemoveFileSpec -> http://msdn.microsoft.com/en-us/library/bb773748(v=VS.85).aspx
Billy ONeal
2010-07-29 16:31:27
John
2010-07-30 07:58:21
That won't work if the program is started from the command line; the full path is not always specified there.
Billy ONeal
2010-07-29 16:15:08
A:
In visual C++, we use
CString m_sAppFolder = __targv[0] ;
which returns a string like "C:\blah\blah\executable_name.exe"
PSUnderwood
2010-07-29 16:18:06
-1 for: 1. Unneeded dependence on MFC. 2. Having the same problem as Neil Moss's answer. 3. Using nonstandard `__targv`.
Billy ONeal
2010-07-29 16:19:00
A:
Please forgive my ignorance, but concerning Jerry Coffin's answer, should we pass GetModuleHandle(NULL) as a first parameter to GetModuleFileName()? Wouldn't it be sufficient to simply pass NULL? i.e. calling the function the following way:
GetModuleFileName(NULL, ..., ...)?
I was under the impression that those two function calls are equivalent:
GetModuleFileName(NULL, lpFileName, nSize)
and
GetModuleFileName(GetModuleHandle(NULL), lpFileName, nSize)
Isn't this true?
Thanks in advance.
vbdasc
2010-09-09 13:47:03