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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,对于C语言程序申请管理员权限,可以使用Windows API函数ShellExecute来启动一个新的进程并请求管理员权限。示例代码如下: ``` #include <windows.h> #include <Shellapi.h> int main(int argc, char* argv[]) { SHELLEXECUTEINFO sei = { 0 }; sei.cbSize = sizeof(sei); sei.fMask = SEE_MASK_NOCLOSEPROCESS; sei.hwnd = NULL; sei.lpVerb = L"runas"; sei.lpFile = L"your_program.exe"; // 将your_program.exe替换为你的程序名 sei.lpParameters = NULL; sei.lpDirectory = NULL; sei.nShow = SW_SHOWDEFAULT; if (ShellExecuteEx(&sei)) { WaitForSingleObject(sei.hProcess, INFINITE); CloseHandle(sei.hProcess); } else { // 弹出错误提示框 MessageBox(NULL, L"Error", L"Error", MB_OK); } return 0; } ``` 对于MFC程序,在项目属性中设置“清单文件”为“嵌入的清单文件”,并在清单文件中指定需要管理员权限的级别即可。具体操作步骤如下: 1. 在Visual Studio中打开MFC项目,选择“项目”->“属性”。 2. 在属性页中选择“清单工具”,将“嵌入的清单文件”设置为“Yes”。 3. 在项目目录下创建一个名为“your_program.exe.manifest”的文件(将“your_program.exe”替换为你的程序名),并在文件中指定需要管理员权限的级别,示例代码如下: ``` <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> </assembly> ``` 完成以上操作后,编译并运行程序即可请求管理员权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值