Page 1 of 1

Read Environment Variables (Windows and Linux)

Posted: Sat May 22, 2004 3:55 pm
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

Posted: Sat May 22, 2004 4:17 pm
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:

Posted: Mon Jun 21, 2004 7:33 am
by blueznl
getenv_() is not a win32 function i think... it's not listed in win32.hlp

Posted: Mon Jun 21, 2004 9:04 am
by Num3
This code just ends the executable under linux has soon has the first *Environ\l is called....

Posted: Mon Jun 21, 2004 1:14 pm
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

Re: Read Environment Variables (Windows and Linux)

Posted: Tue Jun 22, 2004 12:44 am
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:

Posted: Tue Jun 22, 2004 3:10 pm
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

Posted: Tue Jun 22, 2004 3:44 pm
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" ;)

Posted: Tue Jun 22, 2004 4:56 pm
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

Posted: Tue Jun 22, 2004 5:04 pm
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.

Posted: Wed Jun 23, 2004 2:14 am
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:

Posted: Tue Sep 21, 2004 7:44 pm
by wichtel
Thanks freak, just what I needed to get started in Linux.