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

你说的曾经没有我的故事 提交于 2019-12-13 20:05:03

问题


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);
}

回答1:


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 c.s. 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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/15776269/how-to-get-apppath-to-executable-files-directory-using-c-language-on-windows-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!