Getting to the Environmental Variables
Getting to the Environmental Variables
Okay, I am starting on my first program in PureBASIC (I've only had it a week), and the first thing I want to do is check the USERPROFILE in the Environmental Variables to find out what my UserName and folders are under the \Documents and Settings\ folder. But I can't find any command for doing that -- No ENVIRON$() or GetEnv$() function. No search of the Help or the Forums turns up a match. Any ideas?
has-been wanna-be (You may not agree with what I say, but it will make you think).
-
DominiqueB
- Enthusiast

- Posts: 103
- Joined: Fri Apr 25, 2003 4:00 pm
- Location: France
Just try that :-)
;Change variable$ to what ever environment variable you are looking for.
result$=Space(512)
variable$="Windir"
res=GetEnvironmentVariable_(variable$,@result$,512)
If res
MessageRequester("Result",result$,0)
Else
MessageRequester("Error","Not Found",0)
EndIf
End
It's windows funcs .
result$=Space(512)
variable$="Windir"
res=GetEnvironmentVariable_(variable$,@result$,512)
If res
MessageRequester("Result",result$,0)
Else
MessageRequester("Error","Not Found",0)
EndIf
End
It's windows funcs .
Dominique
Windows 10 64bits. Pure basic 32bits
Windows 10 64bits. Pure basic 32bits
you can also use the Setup user-library. You can download it here : http://www.purearea.net/pb/download/userlibs/Setup.zip 
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Getting all the Environmental Variables or a Single one
Okay, I am starting to get a little bit of a handle on using PureBASIC. One of the features I like in other BASICS is being able to get an Environmental Variable (you know, those things in DOS and at the command prompt where you type SET and see what a %variable% was set to, like the %PATH% or %INCLUDE% variables. But since each user can set, clear, midify any variable, you can't always be sure of a name or what it is currently set to. So I adopted a method used in other BASICS that let you locate a variable by its relative position among the several variables in the environment. This little program runs the included procedure to show how this information can be used.
Note: Don't use a similar technique for updating. deleting, or modifying a variable or its assignment. Instead, use the available SetEnvVariable_() procedure so that Windows can do this for you correctly!
Note: Don't use a similar technique for updating. deleting, or modifying a variable or its assignment. Instead, use the available SetEnvVariable_() procedure so that Windows can do this for you correctly!
Code: Select all
;Get Environmental Variable Directly
;--------------------------------------------------------------------------------
; My first PureBASIC Program
; By Donald Darden
;This program finds the starting address for Current
;Environmental Space where the Environmental Variables
;are stored. Normally, in calling GetEnvVariable_(), you
;must already know the specific variable name. This
approach exposes each one already in the environmental
space, regardless of what it is called.
;This is a program To demonstrate how you can see all the
;current environmental variables (count.l=0) or just the
;one corresponding to the count.l downdown
;Declare Procedure.s GetEnvVarByNum(count.l)
Procedure.s GetEnvVarByNum(count.l)
counter.l=count.l
*envptr=GetEnvironmentStrings_()
envstr$=""
MessageRequester("Pointer to Variable #"+Str(count.l),"Pointer = "+Str(*envptr),0)
;counter.l=0 ;dummy assignment
aptr=*envptr
Repeat
envs$=""
If PeekS(aptr,1)=Null$ ;Environmental Variables end with
Goto alldone ;two consecutive zero bytes
EndIf
bptr=aptr
Repeat
envs$=PeekS(bptr,1)
bptr=bptr+1
If envs$=Null$ ;each environmental variable is
Goto gotcha ;Null character terminatged
EndIf
ForEver
gotcha:
If counter.l<2
envs$=PeekS(aptr,bptr-aptr) ;collect the environmental variable
; PrintN(envs$) ;print it to the console window
envstr$=envstr$+envs$+crlf$
EndIf
If counter.l>0 ;do a variable @ countdown
counter.l=counter.l-1
If counter.l=0 ;exit when we get to zero
Goto alldone
EndIf
EndIf
aptr=bptr ;continue if no one variable sought
ForEver ;(counter=0) or not yet to variable
alldone:
;MessageRequester("Returned Environmental",envstr$,0)
ProcedureReturn envstr$
EndProcedure
OpenConsole()
Global Null,s,crlf.s
Null$=Chr(0)
crlf$=Chr(13)+Chr(10)
For n.l=0 To 1000
environ$=GetEnvVarByNum(n.l)
If environ$=""
Goto pau
EndIf
b.l=0
While b.l<Len(environ$)
c.l=FindString(environ$,crlf$,b.l+1)
If c.l=0
c.l=Len(environ$)+1
endif
nstr$=Mid(environ$,b.l+1,c.l-b.l)
PrintN(nstr$)
b.l=c.l+1
Wend
MessageRequester("Environmental Variable #"+Str(n.l),environ$,0)
Next
pau:
result$=Space(512)
variable$="USERPROFILE"
res=GetEnvironmentVariable_(variable$,@result$,512)
If res
MessageRequester(variable$+" Result",result$,0)
Else
MessageRequester(variable$+" Error","Variable Not Found",0)
EndIf
CloseConsole()
End
has-been wanna-be (You may not agree with what I say, but it will make you think).
