Read Environment Variables (Windows and Linux)

Share your advanced PureBasic knowledge/code with the community.
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Read Environment Variables (Windows and Linux)

Post by freak »

Code updated For 5.20+ (same As GetEnvironmentVariable())

Code: Select all

Procedure.s GetEnv(Variable$)
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows  ; On Windows, use the API
    
    Result$ = Space(1000)
    If GetEnvironmentVariable_(@Variable$, @Result$, 1000)
      ProcedureReturn Result$
    Else
      ProcedureReturn ""
    EndIf
    
  CompilerElse  ; On Linux, use the environ array
    
    Protected *Environ.LONG
    
    !extrn _environ
    !mov eax, [_environ]
    !mov [esp+4], eax
    
    Variable$ + "="
    
    While *Environ\l <> 0
      If CompareMemoryString(@Variable$, *Environ\l, 0, Len(Variable$)) = 0
        ProcedureReturn PeekS(*Environ\l + Len(Variable$))
      EndIf
      *Environ + 4
    Wend
    
    ProcedureReturn ""
  CompilerEndIf
  
EndProcedure
Timo
quidquid Latine dictum sit altum videtur
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2139
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Post by Andre »

Timo, could you provide some examples (= working env variables) for using this procedures under Windows and Linux. Would make it even more valuable, e.g. for the CodeArchive ... :wink:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

getenv_() is not a win32 function i think... it's not listed in win32.hlp
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

This code just ends the executable under linux has soon has the first *Environ\l is called....
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post by techjunkie »

blueznl wrote:getenv_() is not a win32 function i think... it's not listed in win32.hlp
http://msdn.microsoft.com/library/defau ... riable.asp
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Re: Read Environment Variables (Windows and Linux)

Post by Doobrey »

freak wrote: CompilerElse ; On Linux, use the environ array
Shouldn`t there be a check to see if it`s running on linux ?
I doubt the Amiga and (hopefully very soon) Mac versions would like running into x86 asm.. :wink:

Great tip though.. can`t wait to get stuck into crashing Linux :twisted:
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

The description states, it is only for win and linux, i don't know the Amiga, sorry :roll:

Num3: I will have another look once i do another Linux session.


Timo
quidquid Latine dictum sit altum videtur
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

freak wrote:The description states, it is only for win and linux, i don't know the Amiga, sorry :roll:
But shouldn't you use:

Code: Select all

CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
This is when a "CompilerError" directive would be useful so you could stick in something saying "this code ain't supported on your scummy OS" ;)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

tinman wrote: This is when a "CompilerError" directive would be useful so you could stick in something saying "this code ain't supported on your scummy OS" ;)
Why can't you use CompilerSelect for this??

Code: Select all

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    ; some Windows specific code
  CompilerCase #PB_OS_Linux
    ; some Linux specific code
  CompilerDefault
    ;OS not supported
CompilerEndSelect
Image Image
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Paul wrote:Why can't you use CompilerSelect for this??
Well I would, but that doesn't change the fact there's no CompilerError directive which when used could inform the developer that they are using routines not supported by the OS. Much better than randomly compiling in no code which might affect parts of the program later.

If they still want to use them it would be easy enough to stick a semi-colon in front of them.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Post by Doobrey »

freak wrote:The description states, it is only for win and linux, i don't know the Amiga, sorry :roll:
Lol..sorry Freak, I`ll put on my dunces hat and stand in the corner whilst writing "I must read the topic first" 1000 times.. :oops:
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

Thanks freak, just what I needed to get started in Linux.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
Post Reply