我如何从另一个C ++ .exe打开一个.exe? [英] How do I open an .exe from another C++ .exe?

查看:345
本文介绍了我如何从另一个C ++ .exe打开一个.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是从另一个.exe打开一个.exe。我真的不知道如何做到这一点,所以我在互联网上搜索。我尝试了一些来自互联网的建议方法,但是没有工作。

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.

这是我的代码:

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

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



当我在DEV C ++中运行它时,一个错误。

When I run it in DEV C++, it doesn't compile and I get a error. Can someone please help me?

推荐答案

您应该避免使用system(),因为

You should always avoid using system() because


  • 这是资源繁重的

  • 它会打败安全性 - 你不知道你是一个有效的命令, ,你甚至可以启动你不打算启动的程序。
    危险的是,当你直接执行一个程序,它获得与你的程序相同的权限 - 意味着,例如,如果你是作为系统管理员运行,那么你只是无意中执行的恶意程序是也作为系统管理员运行。

  • 反病毒程序讨厌它,您的程序可能会被标记为病毒。

您应该使用 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.

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

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
    );
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

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

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.

这篇关于我如何从另一个C ++ .exe打开一个.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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