如何用c PROG运行一个exe [英] How to run an exe using c prog

查看:84
本文介绍了如何用c PROG运行一个exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来这个论坛。我需要在C程序运行在Windows中的EXE文件。
虽然google搜索,我发现下面的code:

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 :

1。
code:

1. Code:

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

以上code编译成功在Borland公司的Turbo C.但它无法运行记事本。

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

2
code:

2 Code:

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

  }

在运行上面的code给出-1作为输出。为什么我会得到-1。

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

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

My OS Windows XP Borland Turbo C Compiler

请帮忙。

推荐答案

有这里至少有两种错误的东西:

There are at least two wrong things here:


  1. 您正在使用系统();

  2. 您是硬编码的路径。

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

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, CreateProcess or, if you want to open a file with it's associated application, ShellExecute.

对于第二个问题,你假设(1) C:\\ WINDOWS 存在,(2),这是当前正在运行的Windows实例的Windows目录(3)的notepad.exe 确实存在,(4),它是在这样的目录。

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

的notepad.exe 是pretty多少保证每一个Windows安装存在,目前还不清楚,你应该寻找它。由于Windows 3.0是在Windows目录中,但在NT系列以前留在 SYSTEM32 子目录。所以,从某些Windows版本起,微软把记事本的两个副本,无论是在窗口目录,并在 SYSTEM32 目录(请参见这个博客后)。

While notepad.exe is 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 system32 subdirectory. So, from some Windows version onward Microsoft put two copies of notepad, both in the windows directory and in the system32 directory (see this blog post).

其他的乐趣:从Windows Server 2008从Windows目录拷贝已被删除(的链接 - 顺便说一下,文章的标题是什么白痴才会硬code路径到记事本:D ),这样你的程序将无法打开即使视窗驻留在记事本 C:\\ WINDOWS

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 notepad even if Windows resides in c:\windows.

但这里最大的问题是不能保证的Windows安装在 C:\\ WINDOWS ;每NT系列的Windows的Windows XP之前,它是由默认实际安装的C:\\ WINNT ,所以你的code将无法在这里

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的多个副本(比如Windows 7的64位上的 C:时,Windows XP 32位上的 D C:\\ WINDOWS 实际上可能存在,但它可能会包含一个当前正在执行的不同的Windows副本,所以你会从Windows的另一个副本开放记事本(如果该副本是64位,运行的是32位的,将无法运行)。

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:\windows may actually exist, but it may contain a copy of Windows different from the one currently executing, so you'd be opening the notepad from another copy of Windows (and if that copy is 64 bit and the running one is 32 bit it won't run).

如果您已经包含了一个窗口目录的磁盘上安装Windows类似的东西,也可能发生;在这种情况下,安装程序将投入的Windows在的Windows(01)目录(或类似的东西),而 C:\\ WINDOWS 可能是空的。

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

长话短说:


  1. 避免使用系统:除了它的其他缺陷,在所有这些情况下您的应用程序不会有任何线索,记事本没有启动;

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

避免硬编码路径: C:\\ WINDOWS 并不保证存在;如果你需要获得Windows目录的路径,可以扩大环境变量%WINDIR%(或的%systemroot ),或使用API​​ GetWindowsDirectory ;

avoid hardcoding paths: c:\windows isn'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;

如果您的应用程序是在 PATH ,你可以利用这样一个事实:在窗口 SYSTEM32 目录是在 PATH 环境变量,这意味着,如果你只是尝试启动记事本,可以避开指定它的完整路径;在另一方面,你暴露自己的漏洞,如果恶意用户在应用程序的工作目录中放一个危险的应用程序;

if your app is in PATH, you may exploit this fact: the Windows and system32 directory are in the PATH environment 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;

如果你想打开一个文件,使用的ShellExecute :它会自动打开该文件与相关的应用程序

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

这篇关于如何用c PROG运行一个exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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