前言
同事要用bat脚本调用exe,给定参数,得到exe的返回串去干别的活。
查了下资料,做了个实验。以前没这么玩过。
实验
exe代码
// function_exe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
// 测试一下,外面给2个参数, 这里不校验参数了
// use parameters by caller give
std::string strIn = argv[1];
strIn += "_";
strIn += argv[2];
// dont' print \n, don't print ' '
printf("function.exe_tmain");
printf("some opt"); // only get 'some' not 'some opt' on bat file call this exe
printf("%s", strIn.c_str());
return 0;
}
bat脚本代码
echo off
set @return_string="init_value"
echo "call exe take param"
for /f %%a in ('D:\\my_svn_checkout\\src\\bat_call_dll\\function_exe\\Release\\function_exe.exe 11 22') do set "@return_string=%%a"
echo return_string = [%@return_string%]
pause
运行结果
D:\my_svn_checkout\src\bat_call_dll\function_exe\Release>echo off
"call exe take param"
return_string = [function.exe_tmainsome]
请按任意键继续. . .