环境:Win10, x64, VS2015
记录日期:2020/10/15
实现内容:
1.ShellExecuteEx 调用exe
2.以文件为参数传参
代码:
#include <iostream>
#include <Windows.h>
#include "psapi.h"
#include "shellapi.h"
using namespace std;
int main()
{
std::string partPath = "D:\\WorkPlacetest.prt";
std::string writeFilePath = "C:\\Users\\think\\Desktop\\outputInfo222.txt";
std::string exePath = CFI::GetApplicationDir() + "FindPartDir.exe";
CFI::RemoveFile(writeFilePath);
std::vector<std::string> inputContents(2);
inputContents[0] = partPath;
inputContents[1] = writeFilePath;
CFI::WriteFile(writeFilePath, inputContents, true);
//这里必须阻塞进程,否则调用的程序没有计算完就去读取文件内容
//::ShellExecute(NULL, "open", LPCSTR(exePath.c_str()),
//LPCSTR(writeFilePath.c_str()), NULL, SW_HIDE);
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = exePath.c_str();
ShExecInfo.lpParameters = writeFilePath.c_str();
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
std::vector<std::string> contents;
CFI::ReadFile(writeFilePath, contents, true);
if (contents.size() == 1)
{
std::string dir = contents[0];
int i = 5 + 6;
}
return 0;
}
注意事项:
1. 调用EXE时,如果需要用到EXE运算后的数据,应该阻塞当前进程
2. :ShellExecute(NULL, "open", LPCSTR(exePath.c_str()), LPCSTR(writeFilePath.c_str()), NULL, SW_HIDE);
不会阻塞进程