简体   繁体   中英

How to get AppPath to Executable file's directory using C language on Windows (using MinGW gcc )

My executable file is in this place --> D:\\Examples\\C_Pro\\newApp.exe Also in that "C_Pro" folder contain several files ( file1.txt, file2.txt, file44.exe, newApp.c )

In my newApp.c file, I includes a ShellExecute Function to execute "file44.exe" file in same folder like this --> ShellExecute(NULL,"open","D:\\Examples\\C_Pro\\file44.exe",NULL,NULL,1)

in this way all work properly..

I'm talking about AppPath like thing in VB

But the case is I want to run this newApp.exe on different pc's So I want to replace ""D:\\Examples\\C_Pro\\" this by whatever the path that contain "newApp.exe" file in another pc. (like C:\\Software\\ )

I get the path using GetModuleFileName Function but it contain newApp.exe part I want only at to the Point that new directory PathRemoveFileSpec function doesn't work.

and also the return path of GetModuleFileName like --> D:\\Examples\\C_Pro\\newApp.exe but when we put some path in to ShellEcxecute the require double shalse (space sequence) like this --> D:\\Examples\\C_Pro\\

How can I get rid of this problem.

Actual code snippt is this...

int main()
{
    ShellExecute(NULL,"open","D:\\Softwares\\TypingMaster700.exe",NULL,NULL,SW_SHOWNORMAL);
}

But I want to do like this. (this is dummy one, here "some_Funtion" means dummy function for explanation purpose.

int main()
{
    char *dirPath = some_Function(a,x,d);
    char *fullPath;
    fullPath = strcat(dirPath,"\\TypingMaster700.exe");
    ShellExecute(NULL,"open",fullPath,NULL,NULL,SW_SHOWNORMAL);
}

Getting the absolute path to the directory where the executable is located is not standardly supported in the C standard because not all systems where the program runs support such a concept. Nevertheless in practice it is a desirable function to have. In short: good question.

Unfortunately it is not so simple and if your program is invoked using execl cs it may even be impossible. You will have to replay the shell in determining which application to run and start with argv[0] as paulsm4 also does. On Linux if the path starts with / , then argv[0] is the absolute path to the executable and you can find the directory by stripping of the executable name at the end. On Windows you will have to check for \\ and possibly a drive letter, I'm not sure. We will assume Linux in the remainder, just read \\ for every / to apply it to Windows.

If argv[0] is not an absolute path as above, you should check if the it contains any / at all, because if it does it must be relative to getcwd as was described also by paulsm4.

If argv[0] does not contain any / , then you will have to run through the PATH environment variable to find the first directory containing argv[0] .

If that all fails, your application has been invoked through execl or one of its friends and they were not honest about the location of the executable. You are out of luck.

Something like this works on Windows:

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  char buff[255];
  getcwd (buff, sizeof (buff));
  printf ("path=%s\\%s\n", buff, argv[0]);
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM