C++ ShellExecuteEx 调用exe 文件传参

环境: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);

不会阻塞进程

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值