Changing the environment variables of the calling process

This seems trivial, but the answer eluded me for a few days.

I have a Windows batch file that calls a C # program to perform an additional check that cannot be performed in a batch file. Upon completion of the check, I need to return the status and the string back to the calling shell.

Now the return value is trivial, and my C # console application just sets the return value (exit code if you want). And I thought the string would also be a piece of cake. I tried to define a new shell variable with:

Environment.SetEnvironmentVariable("ERR", "Some text");

This call should (and should) define a shell variable in the current process - this is the C # process itself that created the variable. The value is lost as soon as the C # application terminates, and the shell that created the C # application knows nothing about this variable. So ... Calling without much use ... Anyway ... If, maybe, if I created a child process from a C3 application, maybe it would inherit my variables.

The EnvironmentVariableTarget.Machine and EnvironmentVariableTarget.User objects to call SetEnvironmentVariable also do not solve the problem, since only the newly created process will receive these new values ​​from the registry.

Therefore, the only working solution I can think of is:

  • write to stdout
  • write to file
  • encodes extra meaning into the return value

, .

( )? , (, PATH)...

.

+5
5

, , , , , , , .

ConsoleApplication1.exe:

'put some sensible code here
'put result in variable myResult
Dim myResult As String = Guid.NewGuid().ToString("D").ToUpperInvariant()
Console.WriteLine("Normal output from the consonle app")
Console.Error.WriteLine("@ECHO OFF")
Console.Error.WriteLine("SET zzzResult={0}", myResult)

Test.cmd( ):

@ECHO OFF
:Jump to folder of batch file
PUSHD %~d0%~p0

:Define a temp file
SET zzzTempFile=%TEMP%\TMP%Random%.CMD

:Call .NET console app
ConsoleApplication1.exe 2>%zzzTempFile%

:Call the generated batch file
CALL %zzzTempFile%

:Clean up temp file
DEL %zzzTempFile%

:Clean up variable
SET zzzTempFile=

:Do something with the result
ECHO Yeah, we finally got it!
ECHO:
ECHO The value is "%zzzResult%".
ECHO:

:Clean up result variable
SET zzzResult=

:Go back to original folder
POPD

. , , , , - , ...

+3

, , - . SeDebugPrivilege.

. Stdout - script.

, Windows. , " ".

+2

, :

FOR /F "usebackq tokens=4 delims=\[\] " %i IN (`ver`) DO set VERSION=%i
ECHO %VERSION%

:

6.1.7601

'usebackq' , , , . , . "" ( M-N). , "skip = X" ). "delims" - (, string-Split() .Net).

. , , , .

+1

BAT , , "" , , , %ERRORLEVEL%. , ,

Environment.Exit(123); // where 123 = error code

- , .bat.

, stdout, , .

0

, , . , , - bat Process, ..

// Spawn your process as you normally would... but also have it dump the environment varaibles
Process process = new Process();
process.StartInfo.FileName = mybatfile.bat;
process.StartInfo.Arguments = @"&&set>>envirodump.txt";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = false;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

// Read the environment variable lines into a string array
string[] envirolines = File.ReadAllLines("envirodump.txt");
File.Delete("envirodump.txt");

// Now simply set the environment variables in the parent process
foreach(string line in a)
{
    string var = line.Split('=')[0];
    string val = line.Split('=')[1];
    Environment.SetEnvironmentVariable(var, val);
} 

, . , .:)

0

All Articles