SETENVIRON doesn't persist outside program

DOS specific questions.
Black_Magic
Posts: 3
Joined: Mar 16, 2017 17:15

SETENVIRON doesn't persist outside program

Postby Black_Magic » Mar 16, 2017 18:07

I am on FREEDOS 0.82, using FreeBASIC 1.05

I have a program that I want to set two environment variables, then come out to the console/prompt (exit) and have those persist for use in a batch file... however this is not happening.

Code: Select all

IF RunMode = "B" THEN
   ReturnCode = 0
   ReturnCode = SETENVIRON( "CMOSPATH=" & SavePath )
   ReturnCode = SETENVIRON( "CMOSNAME=" & SysName & ".SAV" )
   
   IF ReturnCode = 0 THEN
      PRINT " The environment variable CMOSPATH is: "
      PRINT TAB(8); ENVIRON( "CMOSPATH" )
      PRINT
      PRINT " The environment variable CMOSNAME is: "
      PRINT TAB(8); ENVIRON( "CMOSNAME" )
      PRINT
      PRINT "These may not exist yet! You may need to create the folder"
      QUIT(0, Verbosity, BeginDir)
   ELSE
      PRINT "Unable to create environment variables"
      QUIT(15, Verbosity, BeginDir)
   END IF
END IF

The above code is a snippet - the program compiles, and I see what is expected on screen when this code is executed, but when the program exits to the prompt...
    "SET" shows the environment variables that existed before running the program and not the two new ones.
    Setting the two variables to something
    Running the program,
    Issuing the "SET" command
    ... shows that they were not modified.

In the code, I set two environment variables - and then read them back out of the environment - and they show up fine?!

Any ideas?
MrSwiss
Posts: 1439
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SETENVIRON doesn't persist outside program

Postby MrSwiss » Mar 16, 2017 19:54

Black_Magic wrote:Any ideas?
Seems logical, because you're settings are done, in a "temporary" command interpreter.
Why "temporary" ? ... because it's opened by your Program, running in the "main instance"
of command (DOS specific).

However, what is preventing you, to set them inside the *batch-job* itself?
Alternative:
call the batch-job, after setting environment, from within your program, thereafter quit it.
marcov
Posts: 2288
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: SETENVIRON doesn't persist outside program

Postby marcov » Mar 16, 2017 20:26

The problem is that every launched program gets a copy of its parents environment. You can change the copy, but it will only propagate to programs that you call.

However under plain dos, you can access the parents environment, the so called master environment. IIRC you had to walk the PSP up to the parent. Search the web for master enviroment, there were a lot of examples back then when dos was still alive.
Black_Magic
Posts: 3
Joined: Mar 16, 2017 17:15

Re: SETENVIRON doesn't persist outside program

Postby Black_Magic » Mar 16, 2017 21:57

MrSwiss wrote:However, what is preventing you, to set them inside the *batch-job* itself?


Because my whole point is to probe the system (Motherboard/BIOS) to get some information, take that information and process it into something useful, set that data in the environment for use later in the batch file that calls this program.
If the information would always be the same for our purposes - your suggestion is the path I'd take.

My patience for 2 hour long phone calls walking people people through their BIOS settings after battery failure is not what it used to be.
Black_Magic
Posts: 3
Joined: Mar 16, 2017 17:15

Re: SETENVIRON doesn't persist outside program

Postby Black_Magic » Mar 16, 2017 22:00

marcov wrote:The problem is that every launched program gets a copy of its parents environment. You can change the copy, but it will only propagate to programs that you call.

Ahh - I was suspicious of that given the observations. I thought that only happened if I used "shell" or the like to run another instance of the command interpreter.

marcov wrote:However under plain dos, you can access the parents environment, the so called master environment. IIRC you had to walk the PSP up to the parent. Search the web for master enviroment, there were a lot of examples back then when dos was still alive.

Good pointer - I didn't have the words to know what I was looking for... came across this which appears promising.
http://www.piclist.com/techref/dos/pss.htm

Return to “DOS”

Who is online

Users browsing this forum: No registered users and 1 guest