Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2005
    Thanked 0 Times in 0 Posts

    VB6 Help - Modify Path System Variable

    Hi All.

    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.

  • #2
    New Coder
    Join Date
    Nov 2004
    Thanked 0 Times in 0 Posts
    I don't know if you can call API functions within VB, but there is one called SetEnvironmentVariable() 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.

  • #3
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Thanked 15 Times in 15 Posts
    How are you reading the values from the registry???

    In order to help we have to see your code...

    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #4
    New to the CF scene
    Join Date
    Feb 2005
    Thanked 0 Times in 0 Posts
    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!

    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
            End If
            'close the registry key
            RegCloseKey ll_hKey
            'set the return value to -1 to identify a key error
            getRegistryKeyLocalMachine = -1
        End If
    End Function
    Seems as though I have now fixed this problem. Thanks to both for your help tho!


    Posting Permissions

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