Page 1 of 1

How do i return a string from cpp program to caller batch file?

Posted: 18 May 2018 14:38
by uglyninja
The title is a bit long, it may sound really long and confusing (maybe not for Aaciini :D ) but,

How do i put the result into variable %result% in the caller batch file from c++ program?
Is there no other way than saving stdout to a temp file and then reading it with set /p?
The way i showed above requires two files to function as a user-friendly plugin:

1 - (the file that you call) a bat file that saves the output of (2) to a temp file, saves the result to the %result% variable and deletes the temp file
2 - the C++ plugin (.exe)

I asked this question on StackOverflow before and they don't seem to find any other way than this one.

The way i showed above is possible because the batch file that you called share environmental variables but the cpp program and caller .bat don't.

Thanks for any help as this topic should be actually discussed.

- uglyninja

Re: How do i return a string from cpp program to caller batch file?

Posted: 18 May 2018 17:04
by aGerman
You can parse strings written to the StdOut in a FOR /F loop.

In your C++ code you would have something like

Code: Select all

std::cout << "whatever string value" << std::endl;
And in the Batch script

Code: Select all

for /f "delims=" %%i in ('program.exe') do set "result=%%i"
Steffen

Re: How do i return a string from cpp program to caller batch file?

Posted: 19 May 2018 01:54
by uglyninja
I know that way, so it isn't possible to set result variable without putting it into a for loop?
This way isn't possible i guess:

just calling the cpp exe

Code: Select all

call test arg1 arg2

Re: How do i return a string from cpp program to caller batch file?

Posted: 19 May 2018 02:59
by aGerman
Child processes inherit environment variables from the parent process. But it doesn't work vice versa. The value of an environment variable that was changed in the child process isn't accessible in the parent process.

I assume your example is about using CALL to run another batch code where it would work. The reason for that is that both the calling and the called script run in the same cmd.exe process which means they share the same environment.

Steffen

Re: How do i return a string from cpp program to caller batch file?

Posted: 19 May 2018 08:59
by sst
It is typically not possible to do it the way you want, because of the reasons that aGerman has pointed out.
But in a pure technical standpoint view it is possible. First your cpp (or whatever language that is capable of accessing windows API) program needs to inject a thread to it's parent process (which is presumably cmd.exe) and via that injected thread define environment variables in the parent process. This is an advanced topic entirely on it's own which does nothing have to do with batch and is out of scope of this forum.

Re: How do i return a string from cpp program to caller batch file?

Posted: 21 May 2018 12:54
by jfl
Another way would be for the cpp program to generate a temporary batch file with the set commands.
Then, after invoking the cpp program, the parent batch file would call that temporary batch, then delete it.
This is useful if you want the cpp program to generate output that is visible by the user.

Re: How do i return a string from cpp program to caller batch file?

Posted: 21 May 2018 19:46
by sst
jfl wrote:
21 May 2018 12:54
Another way would be for the cpp program to generate a temporary batch file with the set commands.
The OP is already aware of temp file technique, the question was about possibility of a method to do the assignment directly with custom external program, so using a temporary batch file instead of temporary data file doesn't address the question.
uglyninja wrote:
19 May 2018 01:54
just calling the cpp exe

Code: Select all

call test arg1 arg2

Re: How do i return a string from cpp program to caller batch file?

Posted: 22 May 2018 09:59
by jfl
Sorry, I read the question too fast.

If you have a pointer to the injection method you mention, I'm interested: I got several requests in the past for setting shell environment variables!
And (for old timers) this used to be feasible easily in MS-DOS' command.com.

Re: How do i return a string from cpp program to caller batch file?

Posted: 22 May 2018 11:46
by sst
@jfl,
There are numerous resources on the net

for start
https://www.endgame.com/blog/technical- ... ng-process
https://github.com/secrary/InjectProc
http://blogs.microsoft.co.il/pavely/201 ... te-thread/

The task in not trivial and implementing it the right way is tricky, the topic is so vast and advanced that it doesn't worth the effort for such a simple task unless your are really interested in the subject. There are many factors one have to consider in designing and implementing it, such as dealing 32bit and 64bit processes, direct code injection or Dll-Injection, how to deliver data to the thread; by inlining data or by communicating with thread,......... And there is chance of being captured by anti-malware products because of the behavior. But like anything else once it has resolved it becomes easy.

Sorry if it went off-topic.