C/C++ ShellExecuteEx调用exe可执行文件

本文详细介绍了在C/C++中如何通过ShellExecuteEx函数直接调用可执行文件exe,并提供了实际示例。同时解答了在编译时可能遇到的转换错误问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


本系列文章由 @YhL_Leo 出品,转载请注明出处。
文章链接: http://blog.csdn.net/yhl_leo/article/details/49591995


以商业的软件Enblend为例,进行图像无缝拼接和匀光匀色,可以如下直接在Dos中使用命令行调用:

C:\...\Test> enblend -o blend.tif 0.tif 1.tif 2.tif 3.tif 4.tif

输入数据:

Input

输出结果:

Output

C/C++中,有几种方法可以直接调用可执行文件exe,这里以最常用的ShellExcecuteEx函数为例。上面使用命令行操作,可转化为:

// ShellExcecuteEx call the Enblend.exe 

#include <windows.h>
#include <shellapi.h>
#include <stdio.h>
#include <tchar.h>

void main()
{
    SHELLEXECUTEINFO shExecInfo = {0};
    shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    shExecInfo.fMask  = SEE_MASK_NOCLOSEPROCESS;
    shExecInfo.hwnd   = NULL;
    shExecInfo.lpVerb = _T("open");
    shExecInfo.lpFile = _T("C:\\Users\\Leo\\Desktop\\Test\\enblend.exe");
    shExecInfo.lpParameters = _T("-o blend.tif 0.tif 1.tif 2.tif 3.tif 4.tif");
    shExecInfo.lpDirectory  = _T("C:\\Users\\Leo\\Desktop\\Test");
    shExecInfo.nShow        = SW_SHOW;
    shExecInfo.hInstApp     = NULL;
    ShellExecuteEx(&shExecInfo);
    WaitForSingleObject(shExecInfo.hProcess,INFINITE);
}

两者的运行结果是完全一样的。如果编译遇到BUG:cannot convert from 'const char [7]' to 'LPCWSTR',请见博客Multi-Byte Character Set & Unicode Character Set

对于ShellExcecuteEx函数,感兴趣的可以阅读以下两篇文章:

除此外,C/C++中调用可执行exe文件的方法还有:

  • system函数
  • exec或者execv函数
  • WinExec函数
  • CreateProcess函数

详细请阅读:

转载于:https://www.cnblogs.com/hehehaha/p/6332223.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值