C语言 如何使用 c prog 运行 exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4705725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:32:27  来源:igfitidea点击:

How to run an exe using c prog

c

提问by Ujjwal

I am new to this forum. I am in need of a program in C that runs an exe file in Windows. While googling I found the code below :

我是这个论坛的新手。我需要一个在 Windows 中运行 exe 文件的 C 程序。在谷歌搜索时,我发现了下面的代码:

1. Code:

1. 代码:

#include<stdlib.h>
#include<stdio.h>
int main()
 {
  (void)system("C:\Windows\notepad.exe");
   return 0;
  }

The above code compiles successfully in Borland Turbo C. But it fails to run Notepad.

上述代码在 Borland Turbo C 中编译成功,但在 Notepad 中运行失败。

2 Code:

2 代码:

#include<stdlib.h>
#include<stdio.h>
void main()
 {
  int result ;
   result=system("C:\Windows\notepad.exe");
   printf("%d",result);

  }

The above code on running gives -1 as output. Why am I getting -1.

上面运行的代码给出 -1 作为输出。为什么我得到 -1。

My OS Windows XP Borland Turbo C Compiler

我的操作系统 Windows XP Borland Turbo C 编译器

Please help.

请帮忙。

回答by Matteo Italia

There are at least two wrong things here:

这里至少有两个错误的地方:

  1. you're using system();
  2. you're hardcoding a path.
  1. 你正在使用system()
  2. 你正在硬编码一条路径。

For the first problem, I already wrote a long rant some time ago, you can have a look at it here; long story short, to start a process you should go with the platform-specific way, namely, on Windows, CreateProcessor, if you want to open a file with it's associated application, ShellExecute.

对于第一个问题,我已经写了一个很长的咆哮前一段时间,你可以看看它在这里; 长话短说,要启动一个进程,您应该使用特定于平台的方式,即在 Windows 上,CreateProcess或者,如果您想使用与其关联的应用程序打开文件,ShellExecute.

For the second problem, you're assuming (1) that c:\windowsexists, (2) that it is the windows directory of the currently running windows instance (3) that notepad.exeactually exists and (4) that it is in such directory.

对于第二个问题,您假设 (1)c:\windows存在,(2) 它是当前运行的 Windows 实例的 windows 目录 (3)notepad.exe实际存在,以及 (4) 它在这样的目录中。

While notepad.exeis pretty much guaranteed to exist on every Windows installation, it's not clear where you should search it. Since Windows 3.0 it was in the Windows directory, but on the NT family it used to stay in the system32subdirectory. So, from some Windows version onward Microsoft put two copies of notepad, both in the windowsdirectory and in the system32directory (see this blogpost).

虽然notepad.exe几乎可以保证在每个 Windows 安装中都存在,但不清楚您应该在哪里搜索它。从 Windows 3.0 开始,它位于 Windows 目录中,但在 NT 系列中,它过去一直位于system32子目录中。因此,从某些 Windows 版本开始,Microsoft 在windows目录和system32目录中放置了两个记事本副本(请参阅此博客文章)。

Additional fun: from Windows Server 2008 the copy from the Windows directory has been removed (link- incidentally, the title of the post is What idiot would hard-code the path to Notepad?:D), so your program will fail to open notepadeven if Windows resides in c:\windows.

额外的乐趣:从 Windows Server 2008 中删除了 Windows 目录中的副本(链接- 顺便说一句,帖子的标题是什么白痴会硬编码记事本的路径?:D),因此notepad即使 Windows也无法打开您的程序居住在c:\windows.

But the biggest problem here is that Windows isn't guaranteed to be installed in c:\windows; on every NT-family Windows before Windows XP it was actually installed by default in c:\winnt, so your code would fail here.

但这里最大的问题是 Windows 不能保证安装在c:\windows; 在 Windows XP 之前的每个 NT 系列 Windows 上,它实际上默认安装c:\winnt在 .

Moreover, if you have more than one copy of Windows installed (say Windows 7 64 bit on c:, Windows XP 32 bit on d:) c:\windowsmay actually exist, but it may contain a copy of Windows different from the one currently executing, so you'd be opening the notepadfrom another copy of Windows (and if that copy is 64 bit and the running one is 32 bit it won't run).

此外,如果您安装了多个 Windows 副本(例如 Windows 7 64 位c:,Windows XP 32 位d:c:\windows实际上可能存在,但它可能包含与当前正在执行的不同的 Windows 副本,因此您将notepad从另一个 Windows 副本打开(如果该副本是 64 位,而正在运行的副本是 32 位,它将不会运行)。

Similar stuff may happen also if you install Windows on a disk that already contains a windowsdirectory; in that case the setup will put Windows in a Windows(01)directory (or something like that), and c:\windowsmay be empty.

如果您在已经包含windows目录的磁盘上安装 Windows,也可能会发生类似的事情;在这种情况下,安装程序会将 Windows 放在一个Windows(01)目录中(或类似的目录),并且c:\windows可能为空。

Long story short:

长话短说:

  1. avoid using system: apart from its other flaws, in all these scenarios your application wouldn't have any clue that notepaddidn't start;

  2. avoid hardcoding paths: c:\windowsisn't guaranteed to exist; if you need to get the path of the Windows directory, you can expand the environment variable %windir%(or %systemroot), or use the API GetWindowsDirectory;

  3. if your app is in PATH, you may exploit this fact: the Windowsand system32directory are in the PATHenvironment variable, which means that, if you just try to start notepad, you can avoid to specify it's full path; on the other hand, you're exposing yourself to vulnerabilities if a malicious user put a dangerous application in the working directory of your application;

  4. if you want to open a file, use ShellExecute: it will automatically open that file with the associated application.

  1. 避免使用system:除了其他缺陷外,在所有这些场景中,您的应用程序不会有任何notepad未启动的线索;

  2. 避免硬编码路径:c:\windows不保证存在;如果你需要获得Windows目录的路径,你可以展开环境变量%windir%(或%systemroot),或使用API GetWindowsDirectory;

  3. 如果你的应用程序在 中PATH,你可能会利用这个事实:Windowssystem32目录在PATH环境变量中,这意味着,如果你只是尝试启动notepad,你可以避免指定它的完整路径;另一方面,如果恶意用户将危险的应用程序放在您的应用程序的工作目录中,您就会暴露自己的漏洞;

  4. 如果要打开文件,请使用ShellExecute:它会自动使用关联的应用程序打开该文件。

回答by alam

Look where you save your source file, alway C++ Compilers generate two files, let say your source named "hello.cpp" These files should be in your source path: hello.obj hello.exe <--your prgram to distribut

看看你保存源文件的位置,总是 C++ 编译器生成两个文件,假设你的源名为“hello.cpp”这些文件应该在你的源路径中:hello.obj hello.exe <--your prgram to distribut

ALSO

I think you should use new free IDE/Compiler for better result such as:CodeBlocks at http://www.codeblocks.org

我认为您应该使用新的免费 IDE/编译器以获得更好的结果,例如:http://www.codeblocks.org 上的 CodeBlocks

回答by Mike Sherrill 'Cat Recall'

I'm not sure notepad has ever been stored in the Windows directory. This code works under WinXP.

我不确定记事本是否曾经存储在 Windows 目录中。此代码在 WinXP 下工作。

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

/* main() returns int, not void. */
int main( void ) {

  int result ;
  result=system("C:/Windows/system32/notepad.exe");
  printf("%d",result);
  return 0;
}

回答by Kumar Alok

As per me I dont see any problem with the code, did you try running the program with some standard IDE like, dev-cpp or code-blocks.

在我看来,我没有看到代码有任何问题,您是否尝试使用一些标准 IDE 运行程序,例如 dev-cpp 或代码块。

And do one thing

并且做一件事

try running the same command on the command prompt first and tell the result.

尝试先在命令提示符下运行相同的命令并告诉结果。



I would also like to tell you to go inside the Windows directory and check if Notepad.exe is there or not.

我还想告诉您进入 Windows 目录并检查 Notepad.exe 是否存在。

It is not likely but there is a chance.

这不太可能,但有机会。

Thanks

谢谢

Alok Kr.

阿洛克Kr。

回答by mikabytes

Could be that your path is wrong in some way. I would suggest following Kumar's advice and try running it in the command prompt first just to see that you are using the right path.

可能是你的路径在某种程度上是错误的。我建议遵循 Kumar 的建议并首先尝试在命令提示符下运行它,以查看您使用的是正确的路径。

Also, you might want to try running notepad.exe without a path at all. As it is located in the PATH, you should be able to specify just "notepad.exe".

此外,您可能想尝试在没有路径的情况下运行 notepad.exe。由于它位于 PATH 中,您应该能够仅指定“notepad.exe”。

回答by anilbey

use _wpopen function (windows version of popen)
source: http://msdn.microsoft.com/en-us/library/96ayss4b.aspx

使用 _wpopen 函数(windows 版本的 popen)
源:http: //msdn.microsoft.com/en-us/library/96ayss4b.aspx