Click to See Complete Forum and Search --> : Enviroment Variables


Nabeel Khan
September 25th, 2001, 07:42 AM
Hi there..

Is there any Windows API to set environment variables and they would also be viewable from the cmd.exe without restrating the system ??

Regards

Nabeel

John G Duffy
September 25th, 2001, 08:26 AM
Here are two examples taken from API-Guide. One to Set the other to Get the environment variables.

private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (byval lpName as string, byval lpValue as string) as Long
private Sub Form_Load()
'This code was submitted by Brad McDonald (bradm@uhaulit.com)
SetEnvironmentVariable "API-Guide", "Rocks!"
Shell "CMD.EXE"
'In the CMD windows type set, you'll see the Environment Variable set, just for that session.
End Sub
'
'
'
private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (byval lpName as string, byval lpBuffer as string, byval nSize as Long) as Long
Function GetEnvironmentVar(sName as string) as string
GetEnvironmentVar = string(255, 0)
GetEnvironmentVariable sName, GetEnvironmentVar, len(GetEnvironmentVar)
If InStr(1, GetEnvironmentVar, Chr$(0)) > 0 then GetEnvironmentVar = Left$(GetEnvironmentVar, InStr(1, GetEnvironmentVar, Chr$(0)) - 1)
GetEnvironmentVar = sName + ": " + GetEnvironmentVar
End Function
private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
me.AutoRedraw = true
me.print GetEnvironmentVar("USERNAME")
me.print GetEnvironmentVar("USERDOMAIN")
me.print GetEnvironmentVar("PROCESSOR_IDENTIFIER")
me.print GetEnvironmentVar("NUMBER_OF_PROCESSORS")
me.print GetEnvironmentVar("OS")
End Sub




John G

Nabeel Khan
September 25th, 2001, 08:35 AM
Yes I have seen already seen this but that is for the current process, as long as the current process terminates, it also terminates.

Michael Hartmann
November 21st, 2001, 03:26 AM
This is a very nice piece of code, but it doesn't work for Win98 :-(

Under WinNT 4 and Win2K it works very fine.

Is there any solution to set an environment variable under Win98?

Best regards

Michael Hartmann

Biasha
November 26th, 2001, 10:57 AM
The environment variables sit in the follosing key path in the registry:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
You can make adjustments there via regular API calls programmatically, however unless you grant an update privilege to regular users, they will not be able to update those subkeys, only admins will.

hope this helps...

Iouri
November 26th, 2001, 11:30 AM
That will work under W98

Code: Environ("<Param>")

Returns a string, where <Param> can be any of the following:
ALLUSERSPROFILE
APPDATA
CommonProgramFiles
COMPUTERNAME
ComSpec
HOMEDRIVE
HOMEPATH
LOGONSERVER
NUMBER_OF_PROCESSORS
OS
Os2LibPath
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
ProgramFiles
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
windir

If you wish to dump all of the environment variables, this simple piece
of code will work:

For i = 1 To 26
Debug.Print Environ(i) & vbCrLf
Next i


Iouri Boutchkine
iouri@hotsheet.com

Michael Hartmann
November 27th, 2001, 02:08 AM
I want to SET environment variables, not GET environment variables. I know, that you can get some of the environment variables with the Environ function, but this is not what I want.

I need to set/change environment variables for starting a 16 bit process with CreateProcess. In the MSDN-library I've found something, that you can create an environment block (???) and give a pointer to this block of strings (ABC=DEF[NULL]GHI=JKL[NULL][NULL]) as a parameter for CreateProcess.
If this parameter is NULL the new process should inherit the environment of the calling process, but that's wrong for 16 bit processes (I seem). But also if you define a environment block and give a pointer to this, the 16 bit process doesn't know about that.

Any help still wanted ....

Michael Hartmann

Michael Hartmann
November 27th, 2001, 02:11 AM
This key only exists under Win2000, not Win98, so I don't have the chance to mainpulate this key. I thing there's no way to manipulate environment variables for 16 bit processes which are started with CreateProcess.

Thanks for your help

Michael Hartmann