how to know the exact path from where a program starts ?
-
One possibilty which I use is to create a member variable in your CWinApp derived class to store the path and add the following code to the top of InitInstance: ///////////////////////////////////////////////////////////////////////////// // CYourApp initialization BOOL CYourApp::InitInstance() { // Get current directory, which should be where the executable is // located, and save it. ::GetModuleFileName(NULL, m_strAppPath.GetBuffer(MAX_PATH), MAX_PATH); m_strAppPath.ReleaseBuffer(); ... Kind regards, Tim
I tried your solution, and it fails : I explain :the result is the same as the _getcwd. Try this : while programming, start the program, it will show the project root directory but the location of the exe is in the /debug subdirectory. :(( Try this now: make a shortcut in your desk, leaving the "starting from" entry to blank, the run from the shortcut, the program is still in the /debug subdir, but the message will be your /desktop dir ! :mad: So , has anybody already solve this problem ? hope so :-D So is there a solution ? here is the chalenge ! Regards Guy LECOMTE
-
I tried your solution, and it fails : I explain :the result is the same as the _getcwd. Try this : while programming, start the program, it will show the project root directory but the location of the exe is in the /debug subdirectory. :(( Try this now: make a shortcut in your desk, leaving the "starting from" entry to blank, the run from the shortcut, the program is still in the /debug subdir, but the message will be your /desktop dir ! :mad: So , has anybody already solve this problem ? hope so :-D So is there a solution ? here is the chalenge ! Regards Guy LECOMTE
Matt is correct. I took a look at my code again and verified that I actually do use ::GetModuleFileName(NULL, m_strAppPath.GetBuffer(MAX_PATH), MAX_PATH); My original GetCurrentDirectory was wrong because if the "Start from" is left blank then your current directory would not be the same as the exe pathname. I tried your shortcut test and it worked fine with GetModuleFileName. Kind regards, Tim
-
char szAppName[2000]; memset(szAppName, 0, 2000); ::GetModuleFileName(AfxGetInstanceHandle(), szAppName, 2000);
thank you very much Matt !:-D :-D :-D now is the time to have a cold drink in a parisian café ! What a hot shinnig sun !:cool: :cool: (it is the afternoon break) Best regards Guy LECOMTE
-
Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE
Err why not just use __argv[0] ??? Am I missing something?
-
Err why not just use __argv[0] ??? Am I missing something?
If I remember correctly, __argv[0] only gives you the program's whole path when you explicitly run it with it. For example, if I'm at the command prompt and say: c:\abc\def>c:\xyz\opq\myProg.exe __arg[0] will be equal to "c:\xyz\opq\myProg.exe" But if I just say c:\xyz\opt>myProg.exe __arg[0] will be equal to "myProg.exe" Regards, Alvaro
-
char szAppName[2000]; memset(szAppName, 0, 2000); ::GetModuleFileName(AfxGetInstanceHandle(), szAppName, 2000);
If you aren't using MFC, replace "AfxGetInstanceHandle()" with "GetModuleHandle(NULL)". John
-
If I remember correctly, __argv[0] only gives you the program's whole path when you explicitly run it with it. For example, if I'm at the command prompt and say: c:\abc\def>c:\xyz\opq\myProg.exe __arg[0] will be equal to "c:\xyz\opq\myProg.exe" But if I just say c:\xyz\opt>myProg.exe __arg[0] will be equal to "myProg.exe" Regards, Alvaro
Nop, Jeremy's right. Just create a new MFC Dialog app and try this: BOOL CMFCTestApp::InitInstance() { AfxMessageBox(__argv[0]); return(FALSE);
-
Nop, Jeremy's right. Just create a new MFC Dialog app and try this: BOOL CMFCTestApp::InitInstance() { AfxMessageBox(__argv[0]); return(FALSE);
Nope. Alvaro is 100% correct. argv[0] is the name by which the program was invoked. Need proof? 1. Bring up a command window. 2. Change directory to the directory where the app is located. 3. Launch the app by typing just <appname>. _argv[0] will only be <appname> and not the full app path. Kind regards, Tim
-
Nope. Alvaro is 100% correct. argv[0] is the name by which the program was invoked. Need proof? 1. Bring up a command window. 2. Change directory to the directory where the app is located. 3. Launch the app by typing just <appname>. _argv[0] will only be <appname> and not the full app path. Kind regards, Tim
-
Hello Pavlos, All my previous tests were done using Windows NT 4 or Windows 2000. After running the app on a 9X machine, I get the same results you did. Apparently 9X returns the whole pathname regardless of where it is launched, NT does not. FYI, Solaris behaves the same way as NT. Even the prestigious book "The C Programming Language" by Brian W Kernighan and Dennis M Ritchie state the following: "By convention, argv[0] is the name by which the program was invoked,..." Therefore, it looks like using GetModuleFileName() and stripping off the filename(_splitpath) is the safest way to get the location of the exe. Another way to do all of this assuming the application is using a logo compliant installer is to check the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\<YourAppName>.exe Value name: Path The "Path" value name should contain the path of where the app is installed. Kind regards, Tim
-
Hello Pavlos, All my previous tests were done using Windows NT 4 or Windows 2000. After running the app on a 9X machine, I get the same results you did. Apparently 9X returns the whole pathname regardless of where it is launched, NT does not. FYI, Solaris behaves the same way as NT. Even the prestigious book "The C Programming Language" by Brian W Kernighan and Dennis M Ritchie state the following: "By convention, argv[0] is the name by which the program was invoked,..." Therefore, it looks like using GetModuleFileName() and stripping off the filename(_splitpath) is the safest way to get the location of the exe. Another way to do all of this assuming the application is using a logo compliant installer is to check the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\<YourAppName>.exe Value name: Path The "Path" value name should contain the path of where the app is installed. Kind regards, Tim
Hi, Tim! Maybe you know also how to correctly split the UNC? _tsplitpath for UNC returns empty drive, interpreting the server and the share parts as directories. Regards, Gennady P.S. After posting this message I've found here on the CP site the article introducing simple CSplitPath class. The small addition enables you to split UNC too. Gennady
-
Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE
CWinApp::m_pszHelpFilePath will also give the application path, just remove the helpfile name. (i.e. unless you have specifically changed the default) e.g. BOOL CYourApp::InitInstance() { CString sHelpFile = m_pszHelpFilePath; int t = sHelpFile.ReverseFind('\\'); CString sAppPath= sHelpFile.Left(t+1); Regards Leo
-
Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE