背景:在当前工程中调用外部三方exe
执行程序
环境:win10
VS2010
假设外部程序为ge_exe.exe
,具体功能为将输入的两个参数等样输出。
TIPS:三方exe属性,【配置属性】-【常规】-【字符集】选择使用多字节字符集
// ge_exe.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, char* argv[])
{
const char* a = static_cast<const char*>(argv[1]);
const char* b = static_cast<const char*>(argv[2]);
cout << a << endl;
cout << b << endl;
return 0;
}
使用以下代码来调用ge_exe.exe
Q: 有可能出现错误:_T
找不到标识符。
A: 尝试将_T
替换为TEXT
。
// test_exe.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
#include "shellapi.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = _T("open");
ShExecInfo.lpFile = _T("C:\\test_exe\\Debug\\ge_exe.exe"); //外部EXE路径
ShExecInfo.lpParameters = _T("para1 para2");
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW; // 设置为SW_HIDE可不弹出DOS框
ShExecInfo.hInstApp = NULL;
printf("shell execute exe.\n");
ShellExecuteEx(&ShExecInfo);
printf("wait for exe to execute.\n");
WaitForSingleObject(ShExecInfo.hProcess, INFINITE); // 等待进程结束
printf("exe execute finished.\n");
return 0;
}
PS:若不介意弹出DOS框, 可直接使用system()
。