471,322 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,322 software developers and data experts.

How to create a System Environment Variable and update it in current cmd.exe?

Hello -

I am putting together a little ConsoleApplication that is supposed to
check for an Environment Variable and create it if it does not exist.
I found some code that will add a new Environment Variable to the
Registry and send a broadcast message to all open windows to update
their environment.

When I run this *.exe out of a cmd.exe, it will not update the
environment of that shell. The new Environment Variable shows up
under My Computer Properties Advanced Environment Variables but
the rest of the system does not seem to know about it. I guess if I
restart my computer it would work.

Here is the code ...

Public Declare Function SendMessageTimeout Lib "user32" Alias
"SendMessageTimeoutA" (ByVal hWnd As Long, _

ByVal Msg As Long, _

ByVal wParam As Long, _

ByVal lParam As String, _

ByVal fuFlags As Long, _

ByVal uTimeout As Long, _

ByVal lpdwResult As Long) As Long

Public Const HWND_BROADCAST As Long = &HFFFF&
Public Const WM_WININICHANGE As Long = &H1A
Public Const SMTO_ABORTIFHUNG As Long = &H2

Sub SetSystemEnvironmentVariable(ByVal EnvName As String, ByVal
EnvValue As String)
Dim resApi As Long
Dim regVersion As RegistryKey
Dim subkey As String

subkey = "SYSTEM\CurrentControlSet\Control\Session Manager

regVersion = Registry.LocalMachine.OpenSubKey(subkey, True)

regVersion.SetValue(EnvName, EnvValue)

SendMessageTimeout(HWND_BROADCAST, _
0, _
"Environment", _
5000, _
End Sub

Has anyone ever done something similar and got it to work?


Mar 1 '07 #1
0 1797

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by munanxue | last post: by
1 post views Thread by Eric Wood | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.