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.
Printable View
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.
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.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.VB Code:
Private Declare Function SetEnvironmentVariable Lib "kernel32.dll" Alias "SetEnvironmentVariableA" ( _ ByVal lpName As String, _ ByVal lpValue As String _ ) As Long Public Sub DeleteEnvironVariable(ByVal sName As String) Call SetEnvironmentVariable(sName, vbNullString) End Sub
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.Look up the declaration of all the named constants and the function in the API viewer.VB Code:
Call SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, _ 0, ByVal "Environment", SMTO_ABORTIFHUNG, 5000, 0)
You mean using the "Environment Variables" example on this page?
http://www.allapi.net/apilist/SetEnv...Variable.shtml
:) 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).
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.
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?
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:
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" ( _ ByVal hKey As Long, _ ByVal lpSubKey As String, _ ByVal ulOptions As Long, _ ByVal samDesired As Long, _ ByRef phkResult As Long _ ) As Long Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" ( _ ByVal hKey As Long, _ ByVal lpValueName As String _ ) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" ( _ ByVal hKey As Long _ ) As Long Private Declare Function SendMessageTimeoutStr Lib "user32.dll" 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, _ ByRef lpdwResult As Long _ ) As Long Private Const HKEY_LOCAL_MACHINE As Long = &H80000002 Private Const STANDARD_RIGHTS_ALL As Long = &H1F0000 Private Const KEY_QUERY_VALUE As Long = &H1 Private Const KEY_SET_VALUE As Long = &H2 Private Const KEY_CREATE_SUB_KEY As Long = &H4 Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8 Private Const KEY_NOTIFY As Long = &H10 Private Const KEY_CREATE_LINK As Long = &H20 Private Const SYNCHRONIZE As Long = &H100000 Private Const KEY_ALL_ACCESS As Long = ( _ (STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or _ KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _ KEY_CREATE_LINK) And (Not SYNCHRONIZE)) Private Const ERROR_SUCCESS As Long = 0& Private Const HWND_BROADCAST As Long = &HFFFF& Private Const WM_SETTINGCHANGE As Long = &H1A Private Const SMTO_ABORTIFHUNG As Long = &H2 Public Function DeleteSysEnviron(ByVal sEnvStr As String) As Boolean Dim hKey As Long, nResult As Long Const SUBKEY As String = "System\CurrentControlSet\Control\Session Manager\Environment" If RegOpenKeyEx(HKEY_LOCAL_MACHINE, SUBKEY, 0, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then 'Failed to open key, exit function (returns False) Exit Function End If If RegDeleteValue(hKey, sEnvStr) <> ERROR_SUCCESS Then 'Failed to delete value, exit function (returns False) Call RegCloseKey(hKey) Exit Function End If Call RegCloseKey(hKey) 'Broadcast the change to all top-level windows Call SendMessageTimeoutStr(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment", _ SMTO_ABORTIFHUNG, 5000, nResult) 'Success! Return True DeleteSysEnviron = True End Function