View Full Version : VB6 Help - Modify Path System Variable
02-25-2005, 02:47 PM
I am hoping someone can help me with something that is giving me a lot of problems.
I am trying to use a VB6 application to modify the System Environment variable Path. (running in Windows XP - so i cannot simply change the autoexec.bat)
I have tracked the path environment variable to the registry at the following location. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
However, Although my VB app can read all other values in the environment key it cannot read the path!
What is the best way for me to edit the path system environment variable with a VB6 application?
Thanks in advance for the help.
02-25-2005, 03:27 PM
I don't know if you can call API functions within VB, but there is one called SetEnvironmentVariable() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/setenvironmentvariable.asp) which does the exact thing you are looking to do. There's also a function called setenv(), but I don't know if that's available within VB.
02-25-2005, 03:28 PM
How are you reading the values from the registry???
In order to help we have to see your code...
02-25-2005, 04:00 PM
Thanks to both of you for your help so far.
I have actually fixed the problem in my code while writing this reply to the forum! :) I had forgotten that the function I was using to get the registry value was preseting the length to 128 characters. My path is a lot longer than this and so this was why i was getting the error! :rolleyes:
I have included my code below for general reference more than anything else.
I have a function called getRegistryKeyLocalMachine to which I am passing the following values
as_key = SYSTEM\CurrentControlSet\Control\Session Manager\Environment
as_valueName = "Path"
al_type = REG_EXPAND_SZ (constant long value = 2)
as_value = "" (to be returned by reference)
In the code the constant
Private Function getRegistryKeyLocalMachine(ByVal as_key As String, _
ByVal as_valueName As String, _
ByVal al_type As Long, _
ByRef as_value As String) As Long
'value for the returned key
Dim ll_hKey As Long
'get the value of this registry key
'if cannot open key then throw an error
Dim ll_success As Long
ll_success = RegOpenKeyEx(HKEY_LOCAL_MACHINE, as_key, 0, KEY_READ, ll_hKey)
If ll_success = 0 Then
Dim ll_len As Long
Dim ls_val As String
ll_len = 128 '(should be more than 128)
ls_val = Space$(ll_len)
'get the value for the chosen key
Dim ll_getKeySuccess As Long
ll_getKeySuccess = RegQueryValueEx(ll_hKey, as_valueName, 0, al_type, ByVal ls_val, _
'if we are successful then get the directory and work on it
If ll_getKeySuccess = 0 Then
as_value = Left$(ls_val, ll_len - 1)
'set the return value to -2 to identify a value error
getRegistryKeyLocalMachine = -2
'close the registry key
'set the return value to -1 to identify a key error
getRegistryKeyLocalMachine = -1
Seems as though I have now fixed this problem. Thanks to both for your help tho!
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.