Getting system environment variables only (Windows)?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Getting system environment variables only (Windows)?

Post 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.
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Getting system environment variables only (Windows)?

Post 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?
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Getting system environment variables only (Windows)?

Post by Quin »

Does that library allow for the filtering of system variables as opposed to user ones?
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Getting system environment variables only (Windows)?

Post by Demivec »

There's no filtering available that I am aware of.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Getting system environment variables only (Windows)?

Post 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
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Getting system environment variables only (Windows)?

Post 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
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Getting system environment variables only (Windows)?

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Getting system environment variables only (Windows)?

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Getting system environment variables only (Windows)?

Post 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
Post Reply