How can I add/change an environment variable?

Just starting out? Need help? Post your questions and find answers here.
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

How can I add/change an environment variable?

Post by zikitrake »

How can I add/change an environment variable and make it permanent?

Example: to change USERDOMAIN=MGA18 to USERDOMAIN=newuser

thank you
PB 6.21 beta, PureVision User
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

if you look in the code archive you might find what you need.
for windows xp and posibly windows 2000 you can use this vbscript.

save as filename.vbs

Code: Select all

Set ws=WScript.CreateObject("WScript.Shell")
ws.Environment("SYSTEM").Item("USERDOMAIN")="newuser"
if you want to change or create the variable in the user environment, then simply replace "SYSTEM" with "USER" in the above code.

btw, when you double-click on the filename.vbs nothing appears to happen,
but if you right-click on "mycomputer" -> properties, tab advanced, "Environment Variables", you will see that the variable
was indeed created.
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

Thank you, Jack, but I need this code in PB

I know that exist SetEnvironmentVarialbe_(@variable$, @value$) but I don't know how can I use it :S
PB 6.21 beta, PureVision User
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@zikitrake:
Maybe this source helps you (its from the german forum - found on
PureArea.Net )

If you should need any translation of the german comments - feel free
to ask.

Code: Select all

; German forum: http://robsite.de/php/pureboard/viewtopic.php?t=1643&highlight= 
; Author: wichtel 
; Date: 08. July 2003 

; Umgebungsvariablen lesen und setzen  
; (gesetzte Umgebungsvariablen werden an Unterprozesse übergeben,  
;  bleiben aber nach Programmende nicht erhalten und können auch nicht  
;  von bereits laufenden Programmen gelesen werden)  

Structure envstruct  
  name.s  
  value.s  
EndStructure  

NewList env.envstruct()  


; gibt als Rückgabewert die Anzahl der Umgebungsvariablen zurück  
; braucht ein vorher angelegte LinkedList vom Typ envstruct  
Procedure.l myListEnv()  
  ret$=""  
  envcount.l=0  
  envblock.l=GetEnvironmentStrings_()  
  ClearList(env())  
  Repeat  
    ret$=PeekS(envblock)  
    If ret$<>"" And Left(ret$,1)<>"=" And Left(ret$,1)<>":"  
      AddElement(env())  
      env()\name=StringField(ret$,1,"=")  
      env()\value=StringField(ret$,2,"=")  
    EndIf  
    envblock+Len(ret$)+1  
  Until ret$=""  
  ProcedureReturn CountList(env())  
EndProcedure  


; gibt als Rückgabewert den Wert der Umgebungsvariablen zurück  
; oder einenLeerstring wenn die Variable nicht existiert  
Procedure.s myGetEnv(name.s)  
  value.s=Space(256)  
  size.l=Len(value)  
  GetEnvironmentVariable_(@name, @value, @size)  
  ProcedureReturn(value)  
EndProcedure  

; gibt als Rückgabewert den Wert der Umgebungsvariablen zurück  
; oder einenLeerstring wenn das Setzen nicht geklappt hat  
Procedure.s mySetEnv(name.s,value.s)  
  If SetEnvironmentVariable_(@name, @value)  
    ProcedureReturn value  
  Else  
    ProcedureReturn ""   
  EndIf   
EndProcedure  



OpenConsole()  

; gezielt eine Umgebungsvariable abfragen  
PrintN("lesen...")  
PrintN("systemroot: "+myGetEnv("systemroot"))  

; eine Umgebungsvariable setzen  
PrintN("setzen...")  
PrintN("MeineVariable: "+mySetEnv("MeineVariable","MeinWert"))  



PrintN("")  
PrintN("Eingabe druecken...")  
Input()  
PrintN("")  


; alle Umgebungsvariablen in ein LinkedList und auflisten  
PrintN("Auflisten...")  
Anzahl.l=myListEnv()  
PrintN(Str(Anzahl)+" Umgebungsvariablen")  
PrintN("")  
ResetList(env())  
While NextElement(env())  
  PrintN(env()\name+": "+env()\value)  
  Delay(300) ; da kann man besser zuschauen  
Wend  


PrintN("")  
PrintN("Eingabe druecken...")  
Input()  

CloseConsole()  
regards,
benny!
-
pe0ple ar3 str4nge!!!
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

Hi, benny and thank you, but your code produces this error:

Code: Select all

POLINK: error: Unresolved external symbol '_GetEnvironmentStringA'
POLINK: fatal error: 1 unresolved external(s).
PB 6.21 beta, PureVision User
BalrogSoft
Enthusiast
Enthusiast
Posts: 203
Joined: Sat Apr 26, 2003 6:33 pm
Location: Spain
Contact:

Post by BalrogSoft »

Hi Zikitrake, setenviromentvariable function is very easy to use, i got this info on API-Guide:
Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long

· lpName
[in] Pointer to a null-terminated string that specifies the environment variable whose value is being set. The operating system creates the environment variable if it does not exist and lpValue is not NULL.

· lpValue
[in] Pointer to a null-terminated string containing the new value of the specified environment variable. If this parameter is NULL, the variable is deleted from the current process's environment.
To apply this to pb:

Code: Select all

lpName.s="USERNAME"
lpValue.s="newuser"
SetEnviromentVariable_(@lpName.s, @lpValue.s)
lpName is the name of the key that you want to change, and lpValue is the new value for the key, be carefull, because if lpValue is a null value, the key is deleted.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@Zitrake:
Strange .. code runs fine with PureBasic V3.90 :?: :!:
Haven't updated to V3.91 yet ... sorry for any inconvenience :oops:
regards,
benny!
-
pe0ple ar3 str4nge!!!
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Hehe I copied and paste BalrogSoft's code and wondered why it didnt work. One small thing, Environment has an n in it :) . And you dont need to do all that @ stuff either. For me, simply

Code: Select all

SetEnvironmentVariable_("USERNAME", "newuser")
works fine.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

GreenGiant wrote:Hehe I copied and paste BalrogSoft's code and wondered why it didnt work. One small thing, Environment has an n in it :) . And you dont need to do all that @ stuff either. For me, simply

Code: Select all

SetEnvironmentVariable_("USERNAME", "newuser")
works fine.
The SetEnvironmentVariable function sets the contents of the specified environment variable for the current process.

I beleive he wants the change to be permanent, not just for the current
process.

Now, unless I'm mistaken, I thought there was a way to launch vbs
inline.. I will search and post back.

- np
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

*removed - control is blown out *
Last edited by NoahPhense on Mon Aug 02, 2004 3:59 pm, edited 1 time in total.
BalrogSoft
Enthusiast
Enthusiast
Posts: 203
Joined: Sat Apr 26, 2003 6:33 pm
Location: Spain
Contact:

Post by BalrogSoft »

Hi GreenGiant, it works without @ because it`s the same on this case, a string variable is really a long variable that points to the real string on memory, then if you set the value directly, PB pass the pointer for you, this only affect to API functions, don't try with commands like PrintN because it don't work, PB check the type passed to this commands and only accept strings.

To NoahPhense, i used this function because Zikitrake mention this function, but he doesn't know how to use, so i post a example and information about how to use from api-guide.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

BalrogSoft wrote:To NoahPhense, i used this function because Zikitrake mention this function, but he doesn't know how to use, so i post a example and information about how to use from api-guide.
Oh, ok, I haven't used that command before.. But I'm sure there is a way
to make it a permanent change right?

Because I launched it on my system, and it didn't change or set any new
variables..

- np
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

NoahPhense wrote:
Because I launched it on my system, and it didn't change or set any new
variables.
:? yes, this change is temporal and I'm finding for permanent change 8O
Thank you, forum!
PB 6.21 beta, PureVision User
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

I think to make a system-wide change to them you'd have to modify registry keys, found here

Code: Select all

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
then post a systemwide #WM_SETTINGCHANGE message. But I'm not sure how safe it is to be changing these.
upnorth
User
User
Posts: 18
Joined: Wed Jul 21, 2004 8:54 pm
Location: California, USA

Post by upnorth »

In the case of certain environment variables, they are system generated and CANNOT be changed. On NT, Windows 2000 and XP USERDOMAIN is one of these. See This Link: http://www.jsiinc.com/SUBA/tip0400/rh0454.htm for a list of them.

USERDOMAIN will always contain the domain name if the workstation is a member of a domain and it will contain the computer name if the workstation is not a member of a domain.

There is also a utility available from Microsoft called "SETX" that gives a great deal of control over environment variables. Available here: http://www.microsoft.com/windows2000/te ... setx-o.asp
Post Reply