Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

return value from my executable

Sep
30
0
I have written my own .exe and I want to return a value (string) from this executable to an environment variable so that it can be used in a batch file. How can I pass from my .exe to environment?
 
There are several ways to accomplish this. Here is a method that I use.

First, I determine the Process ID of the TCC.EXE that I am using. I do this with a function;
Code:
CurrentPID=%@strip[*,%@word[0,%@execstr[tasklist tcc | findstr "*"]]]

Next, I store the CurrentPID into an environment variable;
Code:
set _CurrentPID=%@Currentpid[]

From my .EXE, I obtain the value of environment variable _CurrentPID, then use the TCC SETP command to set an environment variable in TCC;
Code:
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG
DIM TCCPID AS STRING
DIM Command2Run AS STRING
DIM TCCRT AS STRING

TCCRT="C:\Program Files\JPSoft\TCC_RT_24\tcc.exe"

TCCPID=ENVIRON$("_CurrentPID")

? TCCPID

Command2Run=TCCRT + " /C setp " + TCCPID + " FromEXE=Test"

SHELL Command2Run

END FUNCTION

When the .EXE is finished running, I have a new environment variable, FromEXE, with the value Test.

There are other methods to accomplish this, but this works best for me.

Joe
 
You'll save a lot of work by putting the string result into a file (easy) instead of an environment variable in the parent process (hard). The batch file can refer to the first line of the file as easily as it can refer to an environment variable ... %@line[filename,0].

If your EXE can call Win32 API functions, you could use the registry instead of a file. With RegCreateKeyEx and RegSetValueEx you can set a variable (my_var) in "HKCU\Volatile Environment". Your batch file can get the value of that variable with %@regquery["HKCU\Volatile Environment\my_var"].
 
Expanding on what @vefatica said, here is an example which writes to an .INI file;
Code:
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG

OPEN "c:\utils\test.ini" FOR OUTPUT AS #1
PRINT #1, "[Main]"
PRINT #1, "FromEXE=Test"
CLOSE #1

END FUNCTION

After the .EXE has been run;
Code:
c:\users\jlc\utils>type test.ini
[Main]
FromEXE=Test

c:\users\jlc\utils>echo %@iniread[c:\utils\test.ini,Main,FromEXE]
Test

Joe
 
A third approach: Just write your string to stdout. Snag it with @EXECSTR.
 
A third approach: Just write your string to stdout. Snag it with @EXECSTR.
And if it would be confused with other output, write the return string to stderr and redirect stderr when calling your EXE, something like
Code:
2>result.txt my_proggie.exe
echo my_proggie.exe gave me the string %@line[result.txt,0]
 
Back
Top