How to find application path in c++


How to find the path of a application in c++???? I used the code below to find the application path,


`#include <dir.h>
#include<conio.h>
#include<stdio.h>
int main()
{
char tuh[_MAX_PATH];
char *abc;
abc=getcwd(tuh,sizeof(tuh));
printf("The current directory is: %s\n", abc);
getch();
} `

But the problem is that, suppose I compile the code with name app_path.exe and put it into "D:\abc\app_path.exe" . And I have another application named "open_app.exe" which contains in the drive "E:\xyz\open_app.exe" and it will open the exe file from "D:\abc\app_path.exe". But the problem is after opening the "app_path.exe" via "open_app.exe" it will show that current directory is "E:\xyz" instead of "D:\abc". But "app_path.exe" is not located in "E:\xyz".
I want to show the path exactly where the app_path.exe located,like "D:\abc\app_path.exe"
How to solve this problem????? plzzzz give me a solution........
For a console app, you can also use the first command line argument passed to the alternative form of main() -- this is my preferred technique for such apps.

Depending on how the application is started, the path which turns up in arg[0] can be an absolute path or a relative path (wrt current working directory). So to ensure I always get the absolute path I used _fullpath().

You can use GetFullPathName() rather than _fullpath(). I tend to use the latter as it is provided but the CRT, so I don't have to pull windows.h in for a single call. If you are using other WIN32 functionality, then it doesn't really matter.

(I do the same thing with Linux, but in that case using the realpath() function)

1
2
3
4
5
6
7
8
9
// main
int main(int argc, char* argv[])
{
    char basePath[255] = "";
    _fullpath(basePath, argv[0], sizeof(basePath));

    //TODO strip exe name off to get directory path

    ...


Andy

Note: the above technique will work under normal circumstances (when you are linking to the CRT). It is possible with Windows -- and no doubt Linux -- to build an ultra-light console app which avoids linking to the CRT. In this case the command lines args are likely to be missing. But this is only for specially these built apps.
Last edited on
anyway, why when i tried like this the results is different when i run it on the debug and through command line.
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <conio.h>
using namespace std;

int main (int argc, char * argv[]) {
	cout << "the address is: " << argv [0] << endl;
	getch();
	return 0;
}


in debug:

the address is: c:\documents and settings\blablabla...


meanwhile in cmd:

the address is: c_test.exe
Last edited on
Yeah, argv[0] just contains the first part of the command line that was used to start the program. So it cannot be used to reliably determine the path the executable is in.
i get it! when the debugger run the program, it executing the program by "enter" a command line to the cmd while when i run the program i wrote the program name... of course it has a different value...
argv[0] just contains the first part of the command line


This is why you need to use _fullpath() or GetFullPath() to convert argv[0] to an absolute path.

When launched by a debugger or through an Explorer file association, the full path will be passed. But it is benign to path an absolute path to _fullpath(), etc.

I assume that what turns up in argv[0] is whatever was given to the CreateProcess() call that kicked the process off.
Last edited on
Topic archived. No new replies allowed.