Page 1 of 1

Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 4:02 am
by Quin
I need to get all of the system environment variables in my app on Windows. I have this code, but it only gets the first variable. What am I missing here?

Code: Select all

EnableExplicit

Define Out
If CreateEnvironmentBlock_(@Out, #Null, #False)
  Define Res$ = PeekS(Out)
  Debug Res$
  DestroyEnvironmentBlock_(Out)
EndIf
It gives me:

Code: Select all

ALLUSERSPROFILE=C:\ProgramData
this is... correct, but only part of what I care about. I want all variables not just this one. It also makes a debugger window pop up, for whatever reason.

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 4:18 am
by Demivec
Is there anything standing in the way of you using the functions in PureBasic's Process library to get the variables and values?

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 4:19 am
by Quin
Does that library allow for the filtering of system variables as opposed to user ones?

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 5:13 am
by Demivec
There's no filtering available that I am aware of.

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 5:19 am
by BarryG
Quin wrote:I need to get all of the system environment variables in my app on Windows.
It's in the manual -> https://www.purebasic.com/documentation ... ables.html

Or use this:

Code: Select all

p=RunProgram(GetEnvironmentVariable("comspec"),"/c set","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If p
  While ProgramRunning(p)
    If AvailableProgramOutput(p)
      Debug ReadProgramString(p)
    EndIf
  Wend
  CloseProgram(p)
EndIf
Quin wrote:It also makes a debugger window pop up, for whatever reason
Because you used the "Debug" command.

See here for a list of system ones -> https://gist.github.com/RebeccaWhit3/5d ... -variables

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 11:51 am
by Fred
It returns an array:

Code: Select all

EnableExplicit

Define Out
If CreateEnvironmentBlock_(@Out, #Null, #False)
  Define Res$
  Repeat
    Res$ = PeekS(Out)
    Out + (Len(Res$)+1)*SizeOf(Character)
    Debug Res$
  Until Res$ = ""
  
  DestroyEnvironmentBlock_(Out)
EndIf

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 11:59 am
by Quin
Oh, thanks Fred! This almost works, although for some reason if I keep the debugger on and print the info with Debug, it hangs and says that the program quit unexpectedly. If I change it to MessageRequester(), all works sans the blank string at the end like so:

Code: Select all

EnableExplicit

Define Out
If CreateEnvironmentBlock_(@Out, #Null, #False)
  Define Res$
  Repeat
    Res$ = PeekS(Out)
    Out + (Len(Res$)+1)*SizeOf(Character)
    MessageRequester("", Res$)
  Until Res$ = ""
  DestroyEnvironmentBlock_(Out)
EndIf
It's PB 6.10 Beta 7 (64-bit) running on Windows 11 (ARM64).
Fred wrote: Mon Mar 11, 2024 11:51 am It returns an array:

Code: Select all

EnableExplicit

Define Out
If CreateEnvironmentBlock_(@Out, #Null, #False)
  Define Res$
  Repeat
    Res$ = PeekS(Out)
    Out + (Len(Res$)+1)*SizeOf(Character)
    Debug Res$
  Until Res$ = ""
  
  DestroyEnvironmentBlock_(Out)
EndIf

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 1:32 pm
by mk-soft
Fred wasn't paying attention ;)

You have to pass the start pointer to release.

Code: Select all

EnableExplicit

Define *lpEnvironment, *Out, Res$
If CreateEnvironmentBlock_(@*lpEnvironment, #Null, #False)
  *Out = *lpEnvironment
  Repeat
    Res$ = PeekS(*Out)
    *Out + (Len(Res$)+1)*SizeOf(Character)
    Debug Res$
  Until Res$ = ""
  DestroyEnvironmentBlock_(*lpEnvironment)
EndIf

Re: Getting system environment variables only (Windows)?

Posted: Mon Mar 11, 2024 2:25 pm
by Quin
Thanks, mk-soft! Works perfectly!
mk-soft wrote: Mon Mar 11, 2024 1:32 pm Fred wasn't paying attention ;)

You have to pass the start pointer to release.

Code: Select all

EnableExplicit

Define *lpEnvironment, *Out, Res$
If CreateEnvironmentBlock_(@*lpEnvironment, #Null, #False)
  *Out = *lpEnvironment
  Repeat
    Res$ = PeekS(*Out)
    *Out + (Len(Res$)+1)*SizeOf(Character)
    Debug Res$
  Until Res$ = ""
  DestroyEnvironmentBlock_(*lpEnvironment)
EndIf