Results 1 to 7 of 7

Thread: Environment variable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    126

    Environment variable

    Hi,


    I'm wondering how one could delete an enviroment variable (user environment) from VB code? I know how to find environment variables (using the Envir function), but how can I delete one? Thanks in advance.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Environment variable

    It depends on what you mean. You can use the SetEnvironmentVariable API function, however each process that starts gets a copy of the current environment table and changes you make to this table will only be for your process and not for others.
    VB Code:
    1. Private Declare Function SetEnvironmentVariable Lib "kernel32.dll" Alias "SetEnvironmentVariableA" ( _
    2.     ByVal lpName As String, _
    3.     ByVal lpValue As String _
    4. ) As Long
    5.  
    6. Public Sub DeleteEnvironVariable(ByVal sName As String)
    7.     Call SetEnvironmentVariable(sName, vbNullString)
    8. End Sub
    The above will delete the environment variable you specifiy for your process only. To permanently remove an environment variable you will need to edit the registry, which only works on NT based computers, on Win9x/ME you can only remove them by editing the Autoexec.bat file and restart your computer.

    The user environment table is stored under the following registry key:
    HKEY_CURRENT_USER\Environment

    However even if you make changes to the environment, by editing the registry, applications will not be notified about these changes (not even processes that are started after the edit). So you either need to restart the computer or broadcast a WM_SETTINGCHANGE message to all top-level windows. Sending such a broadcast should be done by using the SendMessageTimeout function so your application doesn't hang around forever waiting for a reply for each and every top-level window.
    VB Code:
    1. Call SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, _
    2.   0, ByVal "Environment", SMTO_ABORTIFHUNG, 5000, 0)
    Look up the declaration of all the named constants and the function in the API viewer.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    126

    Re: Environment variable

    You mean using the "Environment Variables" example on this page?

    http://www.allapi.net/apilist/SetEnv...Variable.shtml

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Environment variable

    Yes if the example I posted above isn't enough for you. Just understand that SetEnvironmentVariable doesn't change the environment for anything else but for your application (while it's running).

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    126

    Re: Environment variable

    I tried the code in the link I submitted, but it didn't work
    I have my environment variable in the system actually, so probably I have to change the constant in the SetEnvironment API call to system somehow? If so, what's the value of that constant? Thanks in advance.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    126

    Re: Environment variable

    I found the following code to remove a system environment variable:

    http://www.codeguru.com/cpp/w-p/win3...le.php/c10849/

    It works. So it's not possible to remove a system environment variable from a VB application one has created?

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Environment variable

    I thought you said that it was a user environment variable. The system environment variables are also stored in the Registry under the following key:
    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment

    You can remove one with a call to the RegDeleteValue API function. But as I said before all top-level windows have to be notified about this change so you need to broadcast a WM_SETTINGCHANGE message. So the code is pretty straight-forward, however whenever you use the API to access the registry you need to declare a bunch of constants.

    I've wrapped up the code for you in a function called DeleteSysEnviron which returns True on success and False on failure. This function can fail (first of all it doesn't work on Win9x/ME since they don't store environment variables in the registry) for many different reasons, the simpliest reason would be that the passed environment variable doesn't exist, another reason could be that the user running the application doesn't have the correct security rights to delete a system environment variable.
    VB Code:
    1. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" ( _
    2.     ByVal hKey As Long, _
    3.     ByVal lpSubKey As String, _
    4.     ByVal ulOptions As Long, _
    5.     ByVal samDesired As Long, _
    6.     ByRef phkResult As Long _
    7. ) As Long
    8.  
    9. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" ( _
    10.     ByVal hKey As Long, _
    11.     ByVal lpValueName As String _
    12. ) As Long
    13.  
    14. Private Declare Function RegCloseKey Lib "advapi32.dll" ( _
    15.     ByVal hKey As Long _
    16. ) As Long
    17.  
    18. Private Declare Function SendMessageTimeoutStr Lib "user32.dll" Alias "SendMessageTimeoutA" ( _
    19.     ByVal hWnd As Long, _
    20.     ByVal msg As Long, _
    21.     ByVal wParam As Long, _
    22.     ByVal lParam As String, _
    23.     ByVal fuFlags As Long, _
    24.     ByVal uTimeout As Long, _
    25.     ByRef lpdwResult As Long _
    26. ) As Long
    27.  
    28. Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
    29. Private Const STANDARD_RIGHTS_ALL As Long = &H1F0000
    30. Private Const KEY_QUERY_VALUE As Long = &H1
    31. Private Const KEY_SET_VALUE As Long = &H2
    32. Private Const KEY_CREATE_SUB_KEY As Long = &H4
    33. Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
    34. Private Const KEY_NOTIFY As Long = &H10
    35. Private Const KEY_CREATE_LINK As Long = &H20
    36. Private Const SYNCHRONIZE As Long = &H100000
    37. Private Const KEY_ALL_ACCESS As Long = ( _
    38.     (STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or _
    39.      KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _
    40.      KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    41. Private Const ERROR_SUCCESS As Long = 0&
    42. Private Const HWND_BROADCAST As Long = &HFFFF&
    43. Private Const WM_SETTINGCHANGE As Long = &H1A
    44. Private Const SMTO_ABORTIFHUNG As Long = &H2
    45.  
    46. Public Function DeleteSysEnviron(ByVal sEnvStr As String) As Boolean
    47.     Dim hKey As Long, nResult As Long
    48.     Const SUBKEY As String = "System\CurrentControlSet\Control\Session Manager\Environment"
    49.    
    50.     If RegOpenKeyEx(HKEY_LOCAL_MACHINE, SUBKEY, 0, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
    51.         'Failed to open key, exit function (returns False)
    52.         Exit Function
    53.     End If
    54.     If RegDeleteValue(hKey, sEnvStr) <> ERROR_SUCCESS Then
    55.         'Failed to delete value, exit function (returns False)
    56.         Call RegCloseKey(hKey)
    57.         Exit Function
    58.     End If
    59.     Call RegCloseKey(hKey)
    60.     'Broadcast the change to all top-level windows
    61.     Call SendMessageTimeoutStr(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment", _
    62.         SMTO_ABORTIFHUNG, 5000, nResult)
    63.     'Success! Return True
    64.     DeleteSysEnviron = True
    65. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width