如何在Windows上使用C语言将AppPath获取到可执行文件的目录(使用MinGW gcc) [英] How to get AppPath to Executable file's directory using C language on Windows (using MinGW gcc )

查看:121
本文介绍了如何在Windows上使用C语言将AppPath获取到可执行文件的目录(使用MinGW gcc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的可执行文件在这里-> D:\ Examples \ C_Pro \ newApp.exe 同样在该"C_Pro"文件夹中,包含几个文件(file1.txt,file2.txt,file44.exe,newApp.c)

在我的newApp.c文件中,我包括一个ShellExecute函数,用于在同一文件夹中执行"file44.exe"文件,例如-> ShellExecute(NULL,"open","D:\ Examples \ C_Pro \ file44.exe ,NULL,NULL,1)

通过这种方式,所有功能都可以正常工作.

我说的是VB中类似AppPath的事情

但是这种情况是我想在其他PC上运行newApp.exe 因此,我想用另一台电脑(例如C:\ Software \)中包含"newApp.exe"文件的任何路径替换"D:\ Examples \ C_Pro \"

我使用GetModuleFileName函数获取路径,但其中包含newApp.exe部分 我只想要到那个新目录 PathRemoveFileSpec函数不起作用.

以及GetModuleFileName的返回路径,例如-> D:\ Examples \ C_Pro \ newApp.exe 但是,当我们向ShellEcxecute放置一些路径时,需要像这样的double shalse(空格序列)-> D:\ Examples \ C_Pro \

如何摆脱这个问题.

这是实际的代码片段...

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

但是我想这样做. (这是一个虚拟的,这里的"some_Funtion"是指用于说明目的的虚拟功能.

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

解决方案

C标准不标准地支持获取可执行文件所在目录的绝对路径,因为并非所有运行该程序的系统都支持这种概念.然而,实际上,这是一种理想的功能.简而言之:好问题.

不幸的是,它并不是那么简单,如果使用execl c.s来调用您的程序.这甚至是不可能的.您必须重播Shell,才能确定要运行哪个应用程序并以argv[0]开头,就像paulsm4一样.在Linux上,如果路径以/开头,则argv[0]是可执行文件的绝对路径,您可以通过在末尾去除可执行文件名称来找到目录.我不确定,在Windows上,您必须检查\以及可能的驱动器号.其余部分我们假设使用Linux,只需将每个/\都阅读一下,即可将其应用于Windows.

如果argv[0]不是上述的绝对路径,则应检查它是否完全包含任何/,因为如果确实存在,则它必须相对于getcwd,正如paulsm4所述.

如果argv[0]不包含任何/,那么您将必须遍历PATH环境变量来查找包含argv[0]的第一个目录.

如果全部失败,则说明您的应用程序是通过execl或其朋友之一调用的,他们对可执行文件的位置并不诚实.你真倒霉.

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

这篇关于如何在Windows上使用C语言将AppPath获取到可执行文件的目录(使用MinGW gcc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆