将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程



我正在处理遗留代码,他们使用 c# 中的 Process 调用 bat 文件。

Process startV6DataExportor = new Process();
startV6DataExportor.EnableRaisingEvents = false;
startV6DataExportor.StartInfo.FileName = strV6BatchPath;
startV6DataExportor.StartInfo.Arguments = strV6batchArgs;
startV6DataExportor.Start();                    
startV6DataExportor.WaitForExit(); 
int v6exitcode = startV6DataExportor.ExitCode;

在此 bat 文件中,通过从 bat 文件传递参数来调用另一个 exe。 喜欢这个

call %V6_CATSTART_PATH% -run %V6_EXE_NAME% -object "%ROOT_ID% %ROOT_VERSION%"

同样在%V6_EXE_NAME%的exe中,excel exe被C++中的CreateProcess((调用 喜欢这个

int main(int argc, char* argv[])
{
if(!CreateProcess(lpctstrBatchPath, // No module name (use command line)
lpArgs,         // Command line
NULL,               // Process handle not inheritable
NULL,               // Thread handle not inheritable
FALSE,          // Set handle inheritance to FALSE
0,              // No creation flags
NULL,               // Use parent's environment block
NULL,               // Use parent's starting directory 
&si,                // Pointer to STARTUPINFO structure
&pi)                // Pointer to PROCESS_INFORMATION structure 
)
{
printf( "CreateProcess failed (%d).n", GetLastError() );
return E_FAIL;
}
else
{
WaitForSingleObject(pi.hProcess,INFINITE);
DWORD ec;
GetExitCodeProcess(pi.hProcess, &ec);       
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
unsigned int excelReturnValue = (unsigned int)ec;
int erv =  static_cast<int>(ec);
}
}

从这个 excel 中.exe我得到返回值 3。 如何将此值传递给初始 c# 代码?

最简单的方法是遍历环境变量

NAME
putenv - change or add an environment variable
SYNOPSIS
#include <stdlib.h>
int putenv(char *string);
DESCRIPTION
The  putenv()  function adds or changes the value of environment
variables.  The argument string is of the form name=value.  If name does
not already exist in the environment, then string is added  to  the
environment.   If name does exist, then the value of name in the
environment is changed to value.  The string pointed to by string becomes
part of the environment, so altering the string changes the environment.  

在C++程序中推送特定的环境变量,并在 C# 程序中捕获它

相关内容

最新更新