How can a FreeBasic program set a variable in windows batch file ?

Windows specific questions.
maTTse
Posts: 2
Joined: Mar 19, 2021 23:32

Re: How can a FreeBasic program set a variable in windows batch file ?

Post by maTTse »

Firstly, I want thank you all for the detailed explanation. I'm trying to study them in details but, as I prefaced, I'm a beginner therefore it will require some time to assimilate the topic.

May I consider as the core answer to my question what jj2007 said ?
I can't see a simple FreeBasic command like Launch$("...") that would allow the calling script or FB program to grab what the called program has to say.
I would be no capable to manage things like the BOM bug yet.

Provided that I understood rightly, is this circumstance a consequence of some features of the OS aimed to fence memory areas and prevent processes to interefre each others ?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How can a FreeBasic program set a variable in windows batch file ?

Post by jj2007 »

Hi maTTse,

Don't worry, normally you will not be confronted with such mysterious FreeBasic bugs. Overall it's a well-constructed language, but in rare occasions we stumble over such behaviour... bad luck.

As to your desire to grab the output: the Open Pipe example is the way to go. It is slightly more verbose than Launch$(), but it's not really complicated. Just try it.
jj2007 wrote:Besides, without the BOM the code compiles with fbc64.exe -gen gcc -Wc -O2 -s console (FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for win64 (64bit)) but crashes with an exception; the debugger shows that rbp has a strange value. The crash happens in the line sh=pipeout("echo "+"%"+g(1)+"%"); the following line env=environ(g(1)) crashes, too.
I investigated the problem in the debugger, and found that g(1) is the culprit (with Fbc64 only!). Here is test code:

Code: Select all

Dim as string myecho
Print "A: ";StrPtr(myecho)
myecho="echo "+"%"+g(1)+"%"
Print "B: ";StrPtr(myecho)
sh=pipeout(myecho)
Print "C: ";sh
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can a FreeBasic program set a variable in windows batch file ?

Post by dodicat »

Please note I split up the string from, for example:
s(1)="ALLUSERSPROFILE Local returns the location of the All Users Profile."
into an array g()
where g(1)=ALLUSERSPROFILE
g(2)= Local
g(3)=returns
etc.
etc.
Of course normally only ALLUSERSPROFILE is the shell command, I kept the whole sentence only for reference, thus using an array g() to hold each word in the sentence, and not needing g(2), g(3) . . .
So
sh=pipeout("echo "+"%"+"ALLUSERSPROFILE"+"%") is actually all that is required.
and
env=environ("ALLUSERSPROFILE")

and all the way down the list to

sh=pipeout("echo "+"%"+"WINDIR"+"%")
and
env=environ("WINDIR")
In other words, the arrays are only for the demo convenience.
I have no problem here with unicode, I use fbide which uses ansi strings.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How can a FreeBasic program set a variable in windows batch file ?

Post by jj2007 »

Not using g(n) does indeed help, but still, why this crash? And why only in FBC64?
Post Reply