c++ 启动exe(启动别的exe程序)

时间:2022-08-29 16:21:27

http://blog.csdn.net/sdcxyz/article/details/13631613

1例程

1.1面向过程例程如下:

c++ 启动exe(启动别的exe程序)

 

#include <iostream>  
#include<windows.h>
#pragma comment(lib, "Kernel32.lib")
using namespace std;
int main()
{


STARTUPINFO si; //一些必备参数设置
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
PROCESS_INFORMATION pi; //必备参数设置结束
//if (!CreateProcess(NULL, "\"C:\\Program Files\\MyApp.exe\"" NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) //8888为命令行参数,ExcuteApp.exe为当前目录下的一个exe文件。
if (!CreateProcess(NULL, "\"F:\\0tool\\转换器\\FormatFactory\\FormatFactory.exe\"" ,NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) //8888为命令行参数,ExcuteApp.exe为当前目录下的一个exe文件。

{
cout << "Create Fail!" << endl;
exit(1);
}
else
{
cout << "Sucess!" << endl;
}
return 0;
}

1.2面向对象例程如下:

c++ 启动exe(启动别的exe程序)

 EXELoal.h

#pragma once
//主函数:
#include<windows.h>
#include "atlstr.h"
//#include "string"
#pragma comment(lib, "Kernel32.lib")


class EXELoad
{
public:
EXELoad(CString filename);

STARTUPINFO si; //一些必备参数设置
PROCESS_INFORMATION pi; //必备参数设置结束
CString filename;
};

EXELoal.cpp

//实现文件  
#include <iostream>
#include "EXELoal.h"
#pragma comment(lib, "Kernel32.lib")

EXELoad::EXELoad(CString filename)
{

memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
//std::string file(filename.GetBuffer(0));
if (!CreateProcess(NULL, filename.GetBuffer(0), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) //8888为命令行参数
//CreateProcess(NULL,”\”C:\\Program Files\\MyApp.exe\” -L -S”,…….)
       //- L和 - S是MyApp.exe可执行文件的参数。{
std::cout << "模拟器未加载成功" << std::endl;
exit(1);
}
}

 主函数调用exe2.cpp

 

#include "EXELoal.h""  
int main()
{
//CString strfileName("ExcuteApp.exe 8888");、
//CreateProcess(NULL,”\”C:\\Program Files\\MyApp.exe\” -L -S”,…….)
   //- L和 - S是MyApp.exe可执行文件的参数。 // CString strfileName("\"F:\\2Project\\YOLO\\yolo2\\YOLO_RECT3\\darknet-rect1\\build\\darknet\\x64\\darknet-rect1.exe\" detector demo F:/2Project/YOLO/yolo2/3data/TestData/data/voc.data F:/2Project/YOLO/yolo2/3data/TestData/cfg/yolo-voc.cfg F:/2Project/YOLO/yolo2/3data/TestData/weight/yolo-voc.weights");
CString strfileName("\"F:\\0tool\\转换器\\FormatFactory\\FormatFactory.exe\"");
EXELoad* ExeLoad = new EXELoad(strfileName);
return 0;
};

2 原理讲解