logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 Handy Class to Manage Environment Variables

Posted on
3/8/2002
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
NT, 9x(?), 2k, XP
Product:
5, 6



Download...(13 kb)

VB's own Environ function is very weak. It only retrieves environment variables and that's it. Thus, the need existed to create a better method. So, linked above & below is a set of components that will allow you to create/set/get system environment variables, user environment variables and process environment variables.

I should note that if you plan on changing environment variables, two of these components (Environment.System and Environment.User) are useless on Win9x series, because the environment variables are set in the autoexec.bat. Plus, after you change autoexec.bat, a reboot is required for them to take effect. Environment.Process can be used in Win9x as well. However, if you simply want to list environment variables all the classes will work on Win9x as well.

Another note of caution - be cafeful changing system environment variables. For instance, wiping out the PATH variable can have undesirable effects.

Another note, you should be aware of, is the effect (or lack of it) that changing system environment variables have on NT service-based programs like IIS service or World Wide Web service. Usually, it has no effect. Only a reboot will refresh the IIS service. The proper method to change a system environment variable is to make an entry in the registry and then broadcast a message to all top-level windows. Since IIS doesn't have any windows, it is not made aware of the change until reboot.

The download includes the ActiveX DLL project as well as a Standard EXE test project. After unzipping, run g.vbg

Download...(13 kb)

Here is a little piece of code showing how to do operations on system environment variables

Add the following code to class
'Note: this piece of code has dependencies on cValuePair and cValuePairs classes
Option Explicit

Private mstrEnvironment As String
Private Const REGISTRY_PATH = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Private oRegistry As Registry

Private 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, lpdwResult As Long) As Long
    
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const SMTO_ABORTIFHUNG As Long = &H2
Private Const WM_SETTINGCHANGE As Long = &H1A

Private mValuePairs As cValuePairs
Private Sub EnumerateVariables()
    Dim sKeys() As String
    Dim iKeyCount As Long
    Dim x As Long
    Dim oValuePair As cValuePair
    
    Set mValuePairs = New cValuePairs
    
    oRegistry.EnumerateValues sKeys(), iKeyCount
    For x = 1 To iKeyCount
        oRegistry.ValueKey = sKeys(x)
        
        Set oValuePair = New cValuePair
        oValuePair.Variable = sKeys(x)
        oValuePair.Value = oRegistry.Value
        
        mValuePairs.Add oValuePair, oValuePair.Variable
        Set oValuePair = Nothing
    Next
End Sub

Public Property Get Environment(ByVal strName As String) As String
    oRegistry.ValueKey = strName
    Environment = oRegistry.Value
End Property

Public Property Let Environment(ByVal strName As String, ByVal strValue As String)
    oRegistry.ValueKey = strName
    oRegistry.ValueType = REG_SZ
    oRegistry.Value = strValue
    
    BroadcastEnvironmentChange
End Property

Private Sub BroadcastEnvironmentChange()
    Dim lngReturnValue As Long

    SendMessageTimeout HWND_BROADCAST, WM_SETTINGCHANGE, 0&, _
        "Environment", SMTO_ABORTIFHUNG, 5000&, lngReturnValue
End Sub


Public Property Get List() As cValuePairs
    If mValuePairs Is Nothing Then
        EnumerateVariables
    End If
    
    Set List = mValuePairs
End Property

Private Sub Class_Initialize()
    Set oRegistry = New Registry
    oRegistry.ClassKey = HKEY_LOCAL_MACHINE
    oRegistry.SectionKey = REGISTRY_PATH
End Sub


Private Sub Class_Terminate()
    Set oRegistry = Nothing
    Set mValuePairs = Nothing
End Sub




Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By bill-0970. Posted on 4/17/2007 8:33:41 PM
?????,??????VB,???,?????,??!

#2. By Eric. Posted on 2/20/2008 4:37:55 PM
This is excellent