vs2017调用linux脚本,[代码] ShellExecute调用外部程序_VS2017

简介

ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。有几个API函数都可以实现这些功能,但是在大多数情况下ShellExecute是更多的被使用的,同时它并不是太复杂。

示例

宿主程序

#include "stdafx.h"

#include "windows.h "

#include "shellapi.h "

int main()

{

char filePath[MAX_PATH];

GetCurrentDirectory(1000, filePath); //得到当前工作路径

strcat_s(filePath, "\\ToolConsole.exe"); //文件名

SHELLEXECUTEINFO ShExecInfo = { 0 };

ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;

ShExecInfo.hwnd = NULL;

ShExecInfo.lpVerb = _T("open");

ShExecInfo.lpFile = _T(filePath); //文件名

ShExecInfo.lpParameters = _T("123456 HelloWorld!");

ShExecInfo.lpDirectory = NULL;

ShExecInfo.nShow = SW_SHOW;

ShExecInfo.hInstApp = NULL;

printf("shell execute exe.\n");

ShellExecuteEx(&ShExecInfo);

printf("wait for exe to execute.\n");

WaitForSingleObject(ShExecInfo.hProcess, INFINITE); // 等待进程结束

printf("exe execute finished.\n");

return 0;

}

外部程序

// ToolConsole.cpp: 定义控制台应用程序的入口点。

//

#include "stdafx.h"

// argv[0]: "ToolConsole.exe"

// argv[1]: int

// argv[2]: 字符串

int main(int argc, char* argv[])

{

if (argc != 3)

{

return 0;

}

int int_value;

char* str_value = argv[2];

sscanf_s(argv[1], "%d", &int_value);

printf("int value = %d \n", int_value);

printf("str value = %s \n", str_value);

getchar();

return 0;

}

程序运行

a88522a89f27?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

等待外部程序结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值