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


本系列文章由 @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