How can I get an executable (.exe) to run from a batch process and get a return value in an environment variable.
Inside the batch file, I tried using START /W /B MyProgram.exe
- and - Call MyProgram.exe
Documentation that I found at https://ss64.com/nt/start.html says using /W and /B should start the exe in the same window and wait for it to finish. The same source says that using CALL will start the exe in the same environment and changes to variables will be preserved in the batch process that started it.
In the Visual Basic program, I tried both of the following paramaters in the Environment.SetEnvironmentVariable statement
EnvironmentVariableTarget.Process (variable remains blank)
EnvironmentVariableTarget.User (this works but the environment variable remains persistant in the user's environment variables, thereafter even after closing all cmd windows.
I definitely would like to set an environment variable inside the exe because it seems so simple. Is there a way to make this work?
My test batch file is as follows:
@echo off
REM This is a test to see if a bat file can start another program (a compiled visual basic program)
:: and use an environment variable to return a value (i.e., a short string or integer).
REM To return a value, the compiled visual basic program uses the following commmand:
:: Environment.SetEnvironmentVariable("exeReturn", sReturn, EnvironmentVariableTarget.Process)
setlocal EnableDelayedExpansion
REM EnableExtensions is set by default
title GetReturnValue_Test
set "exeReturn="
Start /W /B MyProgram.exe &REM The exe sets %exeReturn% for return value
echo exeReturn = %exeReturn%
pause
endlocal
exit
The specific snippet inside the Visual Basic file is:
lHnd = FindWindow(vbNullString, sCaption)
If lHnd = 0 Then
sReturn = "Window not found"
Environment.SetEnvironmentVariable("exeReturn", sReturn, EnvironmentVariableTarget.Process)
Exit Sub
End If
Actually, the parameter for setting the User scope environment variable did not show any change or setting of the variable until I start a new cmd window.
Sincerely SloppyCoder
CodePudding user response:
The setting EnvironmentVariableTarget.Process
cannot work because the VB application is running in its own process, which is a separate process to the one that your batch file is running in. A process is unable to change the environment table of another process.
The setting EnvironmentVariableTarget.User
changes the setting in the Windows Registry, from which the process cmd.exe reads and initializes the environment table when a new Command Prompt window is started.
Basically, your VB application cannot set or change any environment variables belonging to your batch file.
The way to return a string from a subprocess back to its parent process is to print that string (which means your VB application must be configured as a console application), and your batch file can read its output using the FOR /F
command (type for /?
for help).
CodePudding user response:
Thanks for all the help. I had to hack a bit and the following got the value I am going for.
FOR /F "tokens=3" %%a in ('REG QUERY HKCU\Environment /V exeReturn') DO (
Set "exeReturn=%%a"
)
Thanks, again