如何从另一个 C++ .exe 打开 .exe?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15435994/
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-08-27 19:21:24  来源:igfitidea点击:

How do I open an .exe from another C++ .exe?

c++windowsexe

提问by S.Y

What I want to do is open an .exe from another .exe. I really don't know how to do this, so I searched the internet. I tried some suggested methods from the internet, but it didn't work.

我想要做的是从另一个 .exe 打开一个 .exe。我真的不知道该怎么做,所以我在网上搜索。我尝试了一些从互联网上推荐的方法,但没有奏效。

Here's my code:

这是我的代码:

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    system ("OpenFile.exe");
    system ("pause");
    return 0;
}

When I run it in DEV C++, it compiles, but I get a error. Can someone please help me?

当我在 DEV C++ 中运行它时,它可以编译,但出现错误。有人可以帮帮我吗?

回答by Jona

You should always avoid using system()because

你应该总是避免使用system()因为

  • It is resource heavy
  • It defeats security -- you don't know you it's a valid command or does the same thing on every system, you could even start up programs you didn't intend to start up. The danger is that when you directly execute a program, it gets the same privileges as your program -- meaning that if, for example, you are running as system administrator then the malicious program you just inadvertently executed is also running as system administrator. If that doesn't scare you silly, check your pulse.
  • Anti virus programs hate it, your program could get flagged as a virus.
  • 资源繁重
  • 它破坏了安全性——你不知道这是一个有效的命令,或者在每个系统上做同样的事情,你甚至可以启动你不打算启动的程序。 危险在于,当您直接执行程序时,它会获得与您的程序相同的权限——这意味着,例如,如果您以系统管理员身份运行,那么您无意中执行的恶意程序也会以系统管理员身份运行。如果这还没有吓到你,那就检查一下你的脉搏吧。
  • 防病毒程序讨厌它,您的程序可能会被标记为病毒。

You should use CreateProcess().

您应该使用CreateProcess()

You can use Createprocess() to just start up an .exe and creating a new process for it. The application will run independent from the calling application.

您可以使用 Createprocess() 来启动一个 .exe 并为其创建一个新进程。应用程序将独立于调用应用程序运行。

Here's an example I used in one of my projects:

这是我在我的一个项目中使用的示例:

#include <windows.h>

VOID startup(LPCTSTR lpApplicationName)
{
   // additional information
   STARTUPINFO si;     
   PROCESS_INFORMATION pi;

   // set the size of the structures
   ZeroMemory( &si, sizeof(si) );
   si.cb = sizeof(si);
   ZeroMemory( &pi, sizeof(pi) );

  // start the program up
  CreateProcess( lpApplicationName,   // the path
    argv[1],        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi             // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
    );
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

EDIT: The error you are getting is because you need to specify the path of the .exe file not just the name. Openfile.exe probably doesn't exist.

编辑:您收到的错误是因为您需要指定 .exe 文件的路径而不仅仅是名称。Openfile.exe 可能不存在。

回答by Morpheus13

I've had great success with this:

我在这方面取得了巨大的成功:

#include <iostream>
#include <windows.h>

int main() {
    ShellExecute(NULL, "open", "path\to\file.exe", NULL, NULL, SW_SHOWDEFAULT);
}

If you're interested, the full documentation is here:

如果你有兴趣,完整的文档在这里:

http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx.

http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx

回答by ILys Hdm

Try this:

尝试这个:

#include <windows.h>

int main ()

{
    system ("start notepad.exe") // As an example. Change [notepad] to any executable file //

    return 0 ;
}

回答by Sanjeev prasad

Provide the full path of the file openfile.exeand remember not to put forward slash /in the path such as c:/users/username/etc....instead of that use c:\\Users\\username\etc(for windows)

提供文件的完整路径openfile.exe并记住不要/在路径中放置斜杠,例如 c:/users/username/etc....代替使用 c:\\Users\\username\etc(对于Windows)

May be this will help you.

可能这会帮助你。

回答by aaron34weston

You can use the ifstream fin()function:

您可以使用该ifstream fin()功能:

#include <fstream>
int main(){
   ifstream fin("C:\path\to\your\program\program.exe");
   return 0;
}

回答by CHE98

When executable path has whitespace in system, call

当可执行路径在系统中有空格时,调用

#include<iostream>
using namespace std;
int main()
{
    system("explorer C:\Program Files\Google\Chrome\Application\chrome.exe ");
    system("pause");
return 0;
}

回答by udit043

You are getting this error because you are not giving full path. (C:\Users...\file.exe) If you want to remove this error then either give full path or copy that application (you want to open) to the folder where your project(.exe) is present/saved.

您收到此错误是因为您没有提供完整路径。(C:\Users...\file.exe) 如果要删除此错误,请提供完整路径或将该应用程序(要打开)复制到项目(.exe)所在/保存的文件夹中。

#include <windows.h>
using namespace std;
int main()
{
  system ("start C:\Users\Folder\chrome.exe https://www.stackoverflow.com"); //for opening stackoverflow through google chrome , if chorme.exe is in that folder..
  return 0;
}