简体   繁体   English

如何在Windows上使用C语言将AppPath发送到可执行文件的目录(使用MinGW gcc)

[英]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 ) 我的可执行文件位于此处 - > D:\\ Examples \\ C_Pro \\ newApp.exe此外,“C_Pro”文件夹中还包含多个文件(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) 在我的newApp.c文件中,我包含一个ShellExecute函数来在这样的文件夹中执行“file44.exe”文件 - > 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 我正在谈论App中的AppPath

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\\ ) 但是我想在不同的PC上运行这个newApp.exe所以我想用另一个pc中包含“newApp.exe”文件的路径替换“”D:\\ Examples \\ C_Pro \\“这个。(比如C) :\\软件\\)

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. 我使用GetModuleFileName函数获取路径,但它包含newApp.exe部分,我只想到新目录PathRemoveFileSpec函数不起作用的Point。

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\\ 以及GetModuleFileName的返回路径,如 - > D:\\ Examples \\ C_Pro \\ newApp.exe,但是当我们将一些路径放入ShellEcxecute时,需要双重shalse(空格序列),如下所示 - > D:\\ Examples \\ C_Pro \\

How can I get rid of this problem. 我怎样才能摆脱这个问题。

Actual code snippt is this... 实际代码snippt是这个......

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. (这是虚拟的,这里“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);
}

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. 在C标准中没有标准地支持获取可执行文件所在目录的绝对路径,因为并非所有运行程序的系统都支持这样的概念。 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. 不幸的是,它不是那么简单,如果使用execl cs调用你的程序,它甚至可能是不可能的。 You will have to replay the shell in determining which application to run and start with argv[0] as paulsm4 also does. 您将必须重新运行shell以确定要运行的应用程序,并以aruls argv[0]开头,因为paulsm4也可以。 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. 在Linux上,如果路径以/开头,则argv[0]是可执行文件的绝对路径,您可以通过在末尾剥离可执行文件名来找到该目录。 On Windows you will have to check for \\ and possibly a drive letter, I'm not sure. 在Windows上,你将不得不检查\\和可能的驱动器号,我不确定。 We will assume Linux in the remainder, just read \\ for every / to apply it to Windows. 我们将在其余部分假设Linux,只需读取\\为每个/将其应用于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. 如果argv[0]不是上面的绝对路径,你应该检查它是否包含任何/根本,因为如果它,它必须是相对于getcwd如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] . 如果argv[0]不包含任何/ ,则必须运行PATH环境变量才能找到包含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. 如果全部失败,则通过execl或其中一个朋友调用了您的应用程序,并且他们对可执行文件的位置并不诚实。 You are out of luck. 你运气不好。

Something like this works on Windows: 这样的东西适用于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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Windows 中编译简单的 C 程序文件时出错 10 CMD 提示使用 MinGW(gcc:错误:没有这样的文件或目录) - Error When Compiling a Simple C Program File in Windows 10 CMD Prompt Using MinGW (gcc: error: No such file or directory) 如何使用 MinGW GCC 在 Windows 下将用 D 编写的库链接到用 C 编写的程序? - How to link a lib written in D to use it with a program written in C, under Windows, using MinGW GCC? 在Windows上使用GCC(MinGW)编译OpenGL - Using GCC (MinGW) to compile OpenGL on Windows 使用MinGW在Windows上编译小型Gcc项目 - Compiling Small Gcc Project on Windows Using MinGW 在Linux上如何将C与Mingw-GCC一起使用正则表达式库? - How use Regex Library Using C With Mingw-GCC On Linux? 如何使用MinGW-gcc摆脱DLL .reloc部分? - How to get rid of DLL .reloc section using MinGW-gcc? 如何使用C ++获取有关Windows可执行文件(.exe)的信息 - How to get information about a Windows executable (.exe) using C++ 如何在MinGW上使用带有libcc和gcc的应用程序 - How to compile an application using libcurl with gcc on mingw 如何使用C语言获取Windows进程的状态(正在运行或已终止)? - How to get the Status of a Windows Process (Running or Terminated) using C language? 如何使用 C 获取目录中最旧的文件? - How to get the oldest file in a directory using C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM