Page 1 of 1
How can I add/change an environment variable?
Posted: Mon Aug 02, 2004 10:43 am
by zikitrake
How can I add/change an environment variable and make it permanent?
Example: to change USERDOMAIN=MGA18 to USERDOMAIN=newuser
thank you
Posted: Mon Aug 02, 2004 11:49 am
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.
Posted: Mon Aug 02, 2004 12:08 pm
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
Posted: Mon Aug 02, 2004 12:26 pm
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()
Posted: Mon Aug 02, 2004 12:31 pm
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).
Posted: Mon Aug 02, 2004 12:32 pm
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.
Posted: Mon Aug 02, 2004 3:17 pm
by benny
@Zitrake:
Strange .. code runs fine with PureBasic V3.90
Haven't updated to V3.91 yet ... sorry for any inconvenience

Posted: Mon Aug 02, 2004 3:29 pm
by GreenGiant
Hehe I copied and paste BalrogSoft's code and wondered why it didnt work. One small thing, Enviro
nment 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.
Posted: Mon Aug 02, 2004 3:39 pm
by NoahPhense
GreenGiant wrote:Hehe I copied and paste BalrogSoft's code and wondered why it didnt work. One small thing, Enviro
nment 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
Posted: Mon Aug 02, 2004 3:47 pm
by NoahPhense
*removed - control is blown out *
Posted: Mon Aug 02, 2004 3:52 pm
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.
Posted: Mon Aug 02, 2004 3:56 pm
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
Posted: Mon Aug 02, 2004 4:15 pm
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!
Posted: Mon Aug 02, 2004 4:35 pm
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.
Posted: Mon Aug 02, 2004 5:51 pm
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