您的位置:首页 > 运维架构 > Shell

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

2015-11-02 20:55 639 查看
本系列文章由 @YhL_Leo 出品,转载请注明出处。

文章链接: /article/3664571.html

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

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


输入数据:



输出结果:






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

[code]// 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
函数,感兴趣的可以阅读以下两篇文章:

SHELLEXECUTEINFO 和 ShellExecuteEx的使用(CSDN)

SHELLEXECUTEINFO structure(Microsoft Developer Network)

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

system
函数

exec
或者
execv
函数

WinExec
函数

CreateProcess
函数

详细请阅读:

C++程序怎样调用exe可执行文件(SINA)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: