how to know the exact path from where a program starts ?
-
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