How can I add/change an environment variable?
How can I add/change an environment variable?
How can I add/change an environment variable and make it permanent?
Example: to change USERDOMAIN=MGA18 to USERDOMAIN=newuser
thank you
Example: to change USERDOMAIN=MGA18 to USERDOMAIN=newuser
thank you
PB 6.21 beta, PureVision User
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
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.
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"
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.
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
I know that exist SetEnvironmentVarialbe_(@variable$, @value$) but I don't know how can I use it :S
PB 6.21 beta, PureVision User
@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.
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!!!
benny!
-
pe0ple ar3 str4nge!!!
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
-
- Enthusiast
- Posts: 203
- Joined: Sat Apr 26, 2003 6:33 pm
- Location: Spain
- Contact:
Hi Zikitrake, setenviromentvariable function is very easy to use, i got this info on API-Guide:
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.
To apply this to pb: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.
Code: Select all
lpName.s="USERNAME"
lpValue.s="newuser"
SetEnviromentVariable_(@lpName.s, @lpValue.s)
-
- Enthusiast
- Posts: 252
- Joined: Fri Feb 20, 2004 5:43 pm
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 works fine.

Code: Select all
SetEnvironmentVariable_("USERNAME", "newuser")
- NoahPhense
- Addict
- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
The SetEnvironmentVariable function sets the contents of the specified environment variable for the current process.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
works fine.Code: Select all
SetEnvironmentVariable_("USERNAME", "newuser")
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
- NoahPhense
- Addict
- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
*removed - control is blown out *
Last edited by NoahPhense on Mon Aug 02, 2004 3:59 pm, edited 1 time in total.
-
- Enthusiast
- Posts: 203
- Joined: Sat Apr 26, 2003 6:33 pm
- Location: Spain
- Contact:
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.
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.
- NoahPhense
- Addict
- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
Oh, ok, I haven't used that command before.. But I'm sure there is a wayBalrogSoft 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.
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
NoahPhense wrote:
yes, this change is temporal and I'm finding for permanent change 8O
Thank you, forum!
Because I launched it on my system, and it didn't change or set any new
variables.

Thank you, forum!
PB 6.21 beta, PureVision User
-
- Enthusiast
- Posts: 252
- Joined: Fri Feb 20, 2004 5:43 pm
I think to make a system-wide change to them you'd have to modify registry keys, found here
then post a systemwide #WM_SETTINGCHANGE message. But I'm not sure how safe it is to be changing these.
Code: Select all
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
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
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