Detect the path where is running an application VC++
-
I'd like to detect the folder (path) where a my application is running and put the data in a string. - Can somebody give me some suggestion (without using the system() funcion)? - Thanks - giovanni.
If you mean the path where your .exe is located, you can use a function: GetModuleFileName(...), where you put your exe module instance handle and retrieve the whole path including the exe module ("c:\windows\notepad.exe"). Then you have to simply strip the exe name and voila you have path. If you mean the current directory for process, use GetCurrentDirectory()
-
If you mean the path where your .exe is located, you can use a function: GetModuleFileName(...), where you put your exe module instance handle and retrieve the whole path including the exe module ("c:\windows\notepad.exe"). Then you have to simply strip the exe name and voila you have path. If you mean the current directory for process, use GetCurrentDirectory()
geo_m is completely right. But I would add little remark: you can pass NULL to GetModuleFileName() to retrieve your app path. It's easier to use. Especially it is important to pass NULL when you get AppPath from DLL. How to cut filedir from path? All functions for manipulating with filenames are placed in shlwapi.h. Their names are beginning from "Path" word. For example: PathFindFileName (Searches a path for a file name). Also you can use C++ function _splitpath
-
geo_m is completely right. But I would add little remark: you can pass NULL to GetModuleFileName() to retrieve your app path. It's easier to use. Especially it is important to pass NULL when you get AppPath from DLL. How to cut filedir from path? All functions for manipulating with filenames are placed in shlwapi.h. Their names are beginning from "Path" word. For example: PathFindFileName (Searches a path for a file name). Also you can use C++ function _splitpath
An example: char szDrive[_MAX_DRIVE]; char szDir[_MAX_DIR]; char szFileName[_MAX_FNAME]; char szExt[_MAX_EXT]; char szBuffer[_MAX_PATH]; // get path of executable GetModuleFileName(NULL, szBuffer, _MAX_PATH); // split into parts, to get the Path const char* pFilePath = szBuffer; _splitpath(pFilePath, szDrive, szDir, szFileName, szExt); CString strPath; strPath.Format("%s%s", szDrive, szDir); TRACE("File Path is %s\n", strPath); Gaulles
-
I'd like to detect the folder (path) where a my application is running and put the data in a string. - Can somebody give me some suggestion (without using the system() funcion)? - Thanks - giovanni.
/*
* Gets the name of the application's directory.
*/
void getAppDir
(CString& strAppDir) // buffer to receive application's directory
{
char* pSlash = NULL; // location of last backslash in filespec
char szFilespec [_MAX_PATH]; // filespec// Get name of .exe file sans name
::GetModuleFileName (AfxGetInstanceHandle(), szFilespec, _MAX_PATH);
pSlash = strrchr (szFilespec, '\\');
ASSERT (pSlash != NULL);
pSlash++;
*pSlash = '\0';
strAppDir = szFilespec;
}/ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com
-
I'd like to detect the folder (path) where a my application is running and put the data in a string. - Can somebody give me some suggestion (without using the system() funcion)? - Thanks - giovanni.
use GetModuleFileName() Hari Krishnan