Knowing path of MFC executable
-
Hi All, I want to know the path of an MFC excutable. Whenever an MFC app is executed, I want to know the path of that executable from where it is executing?
TCHAR Buffer\[MAX\_PATH\]; DWORD dwRet; dwRet = GetModuleFileName(NULL, Buffer, MAX\_PATH);
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
-
TCHAR Buffer\[MAX\_PATH\]; DWORD dwRet; dwRet = GetModuleFileName(NULL, Buffer, MAX\_PATH);
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
-
GetModuleFileName()
Sometimes returns wrong results when you have a deep directory structure. Use_getcwd()
instead. I've used both and experienced the above problem withGetModuleFileName()
. -
Hi All, I want to know the path of an MFC excutable. Whenever an MFC app is executed, I want to know the path of that executable from where it is executing?
Another way is...
CString appPath = AfxGetApp()->m\_pszHelpFilePath; appPath = appPath.Left(appPath.ReverseFind('\\\\') + 1);
:) Tony
-
GetModuleFileName()
Sometimes returns wrong results when you have a deep directory structure. Use_getcwd()
instead. I've used both and experienced the above problem withGetModuleFileName()
.Hi, The getcwd function will "get the current working directory" which is not always the same as the directory the process executable is residing in. There are many standard API calls that will change the current working directory of your process. In fact... the CreateProcess function itself has the option of starting your process in an arbitrary working directory. Best Wishes, -David Delaune
-
GetModuleFileName()
Sometimes returns wrong results when you have a deep directory structure. Use_getcwd()
instead. I've used both and experienced the above problem withGetModuleFileName()
.getcwd()
is not appropriate for this... it returns the current working directory, which can be changed by file open dialogs or any other code that wants to change its working directory. -
Hi All, I want to know the path of an MFC excutable. Whenever an MFC app is executed, I want to know the path of that executable from where it is executing?
In InitInstance(), member m_pszExeName is your image name. So, use SearchPath() on the result of CString(m_pszExeName) + CString(".exe"). Use the lpFilePart parameter of the call to isolate the path from the file name.
-
The C Runtime to the rescue:
// Get the full path to our process image
_TCHAR *pszExeFullPath;
int nReturn = ATL_CRT_ERRORCHECK(_get_tpgmptr(&pszExeFullPath));I use the ATL_CRT_ERRORCHECK() macro to provide some runtime handling of possible CRT errors.
/* Charles Oppermann */ http://weblogs.asp.net/chuckop