Results 1 to 36 of 36

Thread: [RESOLVED] Why can't I update the registry ?

  1. #1

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Resolved [RESOLVED] Why can't I update the registry ?

    Hello,

    I'm trying to run a program at STARTUP (programatically) by placing an entry in the registry under:

    HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run"

    I've scoured this forum and the web for at least 5 different examples on how to do this. No matter what I try, it doesn't update the registry. I'm on Win XP and I have Admin rights. Is there some XP restriction that I'm not aware of that's not allowing me to update the registry?

    If anyone has a program that does this, can you email it to me ?
    Thanks for any help. I'm at the end of my rope...
    VB6 Enterprise SP4

  2. #2

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    Try this:

    VB Code:
    1. [B]'in module[/B]
    2. Option Explicit
    3.  
    4. Public Const HKEY_LOCAL_MACHINE = &H80000002
    5. Public Const REG_SZ = 1
    6. Public Const ERROR_SUCCESS = 0&
    7.  
    8. Public Declare Function RegCloseKey Lib _
    9. "advapi32.dll" (ByVal hKey As Long) As Long
    10.  
    11. Public Declare Function RegCreateKey Lib _
    12. "advapi32.dll" Alias "RegCreateKeyA" (ByVal _
    13. hKey As Long, ByVal lpSubKey As String, _
    14. phkResult As Long) As Long
    15.  
    16. Public Declare Function RegSetValueEx Lib _
    17. "advapi32.dll" Alias "RegSetValueExA" (ByVal _
    18. hKey As Long, ByVal lpValueName As String, ByVal _
    19. Reserved As Long, ByVal dwType As Long, lpData As _
    20. Any, ByVal cbData As Long) As Long
    21.  
    22. Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
    23.     Dim hCurKey As Long
    24.     Dim lRegResult As Long
    25.         lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    26.         lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    27.             If lRegResult <> ERROR_SUCCESS Then
    28.             End If
    29.                 lRegResult = RegCloseKey(hCurKey)
    30. End Sub
    31.  
    32. [B]'in form[/B]
    33. Option Explicit
    34.  
    35. Private Sub Command1_Click()
    36.     SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "myApp", "c:\myApp.exe"
    37. End Sub

  4. #4
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    You need to set the correct permissions to write to the registry.
    VB Code:
    1. Option Explicit
    2.  
    3. 'Form level code.
    4.  
    5. Private Sub Form_Click()
    6.    modWriteRegSZ HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "MyApp", "C:\MyApp.exe"
    7. End Sub
    VB Code:
    1. Option Explicit
    2.  
    3. 'Module level code.
    4.  
    5. 'Registry APIs.
    6. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    7.  
    8. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    9.    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
    10.    ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    11.    lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
    12.  
    13. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    14.    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    15.    ByVal samDesired As Long, phkResult As Long) As Long
    16.  
    17. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    18.    (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    19.    ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    20.  
    21. 'Registry manipulation
    22. Private Const REG_OPTION_NON_VOLATILE = 0
    23. Private Const KEY_CREATE_SUB_KEY = &H4
    24. Private Const KEY_SET_VALUE = &H2
    25. Public Const HKEY_LOCAL_MACHINE = &H80000002
    26. Private Const REG_SZ = 1
    27. Private Const ERROR_SUCCESS = 0&
    28.  
    29. Public Sub modWriteRegSZ(lngHKey As Long, strSubKey As String, strValueName As String, _
    30.    strValue As String)
    31. 'Create the key and respective REG_SZ value.
    32.    Dim lngRetVal     As Long
    33.    Dim lngKeyHandle  As Long
    34.  
    35. 'Create the key.
    36.    If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
    37.       ByVal 0&, lngKeyHandle, lngRetVal) <> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
    38. 'Open the new key.
    39.    If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) <> _
    40.       ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
    41. 'Create a key and set its value.
    42.    If RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue) + 1) _
    43.       <> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
    44. 'Close the key.
    45.    Call RegCloseKey(lngKeyHandle)
    46. 'Exit.
    47.    Exit Sub
    48. WRITE_SZ_ERROR:
    49. 'Close the key.
    50.    Call RegCloseKey(lngKeyHandle)
    51. 'Time for any error handling.
    52.    MsgBox "Error in modWriteRegSZ"
    53. End Sub
    EDIT:- BTW, if you've got regedit open, make sure you refresh it after writing the key
    Last edited by schoolbusdriver; Sep 2nd, 2006 at 03:16 PM. Reason: Changed Private Const HKEY_LOCAL_MACHINE = &H80000002 to PUBLIC

  5. #5

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    Thanks... I added a couple message box displays to the code:

    VB Code:
    1. 'Create the key.
    2.    If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
    3.       ByVal 0&, lngKeyHandle, lngRetVal) <> ERROR_SUCCESS Then
    4.       GoTo WRITE_SZ_ERROR
    5.    Else
    6.       [B]MsgBox ("success creating key")[/B]
    7.    End If
    8. 'Open the new key.
    9.    If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) <> _
    10.       ERROR_SUCCESS Then
    11.       GoTo WRITE_SZ_ERROR
    12.    Else
    13.       [B]MsgBox ("success opening new key")[/B]
    14.    End If
    15. 'Create a key and set its value.
    16.    If RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue) + 1) _
    17.       <> ERROR_SUCCESS Then
    18.       GoTo WRITE_SZ_ERROR
    19.    Else
    20.       [B]MsgBox ("success setting value")[/B]
    21.    End If


    I'm getting successful return codes from all three steps. I go to the registry,
    refresh it, and the entry is NOT there !! arrrggghhhhh!
    VB6 Enterprise SP4

  6. #6

  7. #7

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    No, but I will.

    I do have an update, though... I shipped the program over to my Windows 98 SE machine, and it works fine there.....

    Hummmm...... something to do with permissions on XP, maybe ?
    VB6 Enterprise SP4

  8. #8

  9. #9

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    OK.... maybe I need to understand something then.

    I have 'Admin' rights to my machine, but I'm not signed on as the 'Administrator', if that makes sense. I'm signed on with my own Userid.

    Could this be the problem ?
    VB6 Enterprise SP4

  10. #10
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    Shouldn't make any difference. Sure you're looking under Local_Machine and not Current_User ?

  11. #11
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    Just spotted that the module constant HKEY_LOCAL_MACHINE should be Public...

  12. #12

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    I sent Gavio's code over to my Win 98/SE machine and that works there also ......

    VB6 Enterprise SP4

  13. #13
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    Quote Originally Posted by schoolbusdriver
    Just spotted that the module constant HKEY_LOCAL_MACHINE should be Public...
    That's because they are declared in a module but used also in a form... nothing else.

    edit: missunderstood you, my bad

  14. #14

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    My last 2 attempts on my XP machine had it declared as PUBLIC, and
    'no go' ......

    It still seems strange that I'm getting successful return codes, though....

    VB6 Enterprise SP4

  15. #15
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    Quote Originally Posted by gavio
    edit: missunderstood you, my bad
    No prob, I should have been more specific It's getting late...

  16. #16
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    Try this and see what's the message:

    VB Code:
    1. [B]'in module[/B]
    2.  
    3. Option Explicit
    4.  
    5. Public Const HKEY_LOCAL_MACHINE = &H80000002
    6. Public Const REG_SZ = 1
    7. Public Const ERROR_SUCCESS = 0&
    8.  
    9. Public Declare Function RegCloseKey Lib _
    10. "advapi32.dll" (ByVal hKey As Long) As Long
    11.  
    12. Public Declare Function RegCreateKey Lib _
    13. "advapi32.dll" Alias "RegCreateKeyA" (ByVal _
    14. hKey As Long, ByVal lpSubKey As String, _
    15. phkResult As Long) As Long
    16.  
    17. Public Declare Function RegSetValueEx Lib _
    18. "advapi32.dll" Alias "RegSetValueExA" (ByVal _
    19. hKey As Long, ByVal lpValueName As String, ByVal _
    20. Reserved As Long, ByVal dwType As Long, lpData As _
    21. Any, ByVal cbData As Long) As Long
    22.  
    23. Public Declare Function RegOpenKey Lib _
    24. "advapi32.dll" Alias "RegOpenKeyA" (ByVal _
    25. hKey As Long, ByVal lpSubKey As _
    26. String, phkResult As Long) As Long
    27.  
    28. Public Declare Function RegQueryValueEx Lib _
    29. "advapi32.dll" Alias "RegQueryValueExA" (ByVal _
    30. hKey As Long, ByVal lpValueName As String, ByVal _
    31. lpReserved As Long, lpType As Long, lpData As _
    32. Any, lpcbData As Long) As Long
    33.  
    34.  
    35. Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
    36.     Dim hCurKey As Long
    37.     Dim lRegResult As Long
    38.         lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    39.         lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    40.             If lRegResult <> ERROR_SUCCESS Then
    41.             End If
    42.                 lRegResult = RegCloseKey(hCurKey)
    43. End Sub
    44.  
    45. Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
    46.     Dim hCurKey As Long
    47.     Dim lValueType As Long
    48.     Dim strBuffer As String
    49.     Dim lDataBufferSize As Long
    50.     Dim intZeroPos As Integer
    51.     Dim lRegResult As Long
    52.         If Not IsEmpty(Default) Then
    53.           GetSettingString = Default
    54.         Else
    55.           GetSettingString = ""
    56.         End If
    57.             lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    58.             lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
    59.                 If lRegResult = ERROR_SUCCESS Then
    60.                     If lValueType = REG_SZ Then
    61.                         strBuffer = String(lDataBufferSize, " ")
    62.                         lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
    63.                         intZeroPos = InStr(strBuffer, Chr$(0))
    64.                             If intZeroPos > 0 Then
    65.                               GetSettingString = Left$(strBuffer, intZeroPos - 1)
    66.                             Else
    67.                               GetSettingString = strBuffer
    68.                             End If
    69.                     End If
    70.                 Else
    71.                 End If
    72.                     lRegResult = RegCloseKey(hCurKey)
    73. End Function
    74.  
    75. [B]'in form[/B]
    76.  
    77. Option Explicit
    78.  
    79. Private Sub Command1_Click()
    80.     SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "myApp", "c:\myApp.exe"
    81.         MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "myApp", "False!")
    82. End Sub
    Try to restart the machine to see what happens.

  17. #17
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    Quote Originally Posted by tcurrier
    My last 2 attempts...
    That's wierd. Don't know how your OS is set up, but it's vaguely possible that although the key has been written to a cache, it hasn't been flushed to the registry. ie it needs RegFlushKey to complete. From MSDN:-
    It is not necessary to call RegFlushKey to change a key. Registry changes are flushed to disk by the registry using its lazy flusher. Registry changes are also flushed to disk at system shutdown.

    Unlike RegCloseKey, the RegFlushKey function returns only when all the data has been written to the registry.

    The RegFlushKey function may also write out parts of or all of the other keys. Calling this function excessively can have a negative effect on an application's performance.

    An application should only call RegFlushKey if it requires absolute certainty that registry changes are on disk. In general, RegFlushKey rarely, if ever, need be used.
    Dare I suggest a reboot ? (there's no smilie for "sucking in breath between teeth")

  18. #18

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    The message that I'm getting is the pathname of the executable file that I entered in the SaveSettingString function.

    So, obviously it's getting set in the registry ?!? I still can't see it in the Registry Editor, though ..
    VB6 Enterprise SP4

  19. #19
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    Quote Originally Posted by tcurrier
    The message that I'm getting is the pathname of the executable file that I entered in the SaveSettingString function.

    So, obviously it's getting set in the registry ?!? I still can't see it in the Registry Editor, though ..
    Then something is wrong with your system or something. The key has obviously been written to the reg. I'm suggestion, what also the driver suggested, restart your machine and see what happens.

  20. #20

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    I don't think it's getting written to the registry permanently. I wrote another program with just :

    VB Code:
    1. GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    2. "Tom Currier", "False!")

    and this time the message came up "False" ....

    I'm going to restart my machine and try it again ....
    VB6 Enterprise SP4

  21. #21
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    Just spotted something in "SaveSettingString" Try altering it as follows:-
    VB Code:
    1. lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData) [B][COLOR=Red]+ 1[/COLOR][/B])

  22. #22
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    It's returning "False!" because the "Tom Currier" key doesn't exist. That "False!" i've put there is just a default String, which is shown if the searched key doesn't exist...

  23. #23

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    Yes, thanks.... I realize that the message "False" meant that it didn't find the key.

    I tried this:

    VB Code:
    1. [B]Private Sub Command1_Click()[/B]' set:
    2. SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    3. "Tom Currier", "c:\temp\weekday.exe"
    4.  
    5. ' retrieve:
    6. MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    7. "Tom Currier", "False!")
    8. End Sub
    9.  
    10. [B]Private Sub Command2_Click()[/B]
    11. ' retrieve again:
    12. MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    13. "Tom Currier", "False!")
    14. End Sub

    A click of command button #1 sets the key, and retrieves it successfully.

    Then clicking on command button #2 results in the "False" message.

    I tried the RegFlushKey function, but I got a return code 6 from it.
    VB6 Enterprise SP4

  24. #24

  25. #25

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    No, it didn't start up when Windows started.

    I don't understand what could have happened in between the time Command button 1 and Command button 2 were clicked, though. If the key was returned from button 1 it also should have been returned from button 2.

    VB6 Enterprise SP4

  26. #26

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    Try this one on for size : (all in the same subroutine)

    ' retrieve once:
    MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    "Tom Currier", "False!")

    ' retrieve second time :
    MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    "Tom Currier", "False!")

    ' retrieve third time:
    MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    "Tom Currier", "False!")

    Result:
    1- c:\temp\weekday.exe
    2- False
    3- False
    VB6 Enterprise SP4

  27. #27
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    Quote Originally Posted by tcurrier
    No, it didn't start up when Windows started.

    I don't understand what could have happened in between the time Command button 1 and Command button 2 were clicked, though. If the key was returned from button 1 it also should have been returned from button 2.

    I have tried on both of my computers in both ways (schoolbusdriver's and mine) and everything works as it should. You said you are not loged in as the "Administrator". Have you tried it like so?

  28. #28
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    Everytime result is the same... it should be, because i can actually see it in my registry. Put another MsgBox between SaveSettingString and GetSettingString and go to your registry before passing it and see what happens...

  29. #29

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Unhappy Re: Why can't I update the registry ?

    That would be interesting, but I don't have an Administrator's Id and password.
    VB6 Enterprise SP4

  30. #30
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    I think that there's something wrong with the rights, or something God knows you have on your system. If it works on mine (both!!) and on yours (the other one as you've said), then i'm pretty sure nothing is wrong with my functions and for that mather schoollbusdriver's. I'm going to dig a little bit more, but i'll come back with that (if any) tomorrow...

  31. #31

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    One more try:

    VB Code:
    1. Public Sub SaveSettingString(hkey As Long, strPath As String, strValue As String, strData As String)
    2. Dim hCurKey As Long
    3. Dim lRegResult As Long
    4. lRegResult = RegCreateKey(hkey, strPath, hCurKey)
    5. lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    6.  
    7. If lRegResult <> ERROR_SUCCESS Then
    8.    Form1.Text1.Text = "Error in RegSetValueEx"
    9. Else
    10.    Form1.Text1.Text = "RegSetValueEx was successful"
    11. End If
    12.  
    13. MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    14. "Tom Currier", "False!")
    15. MsgBox GetSettingString(hCurKey, "Software\Microsoft\Windows\CurrentVersion\Run", _
    16. "Tom Currier", "False!")
    17. MsgBox GetSettingString(hkey, "Software\Microsoft\Windows\CurrentVersion\Run", _
    18. "Tom Currier", "False!")
    19.  
    20. lRegResult = RegCloseKey(hCurKey)
    21.  
    22. MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    23. "Tom Currier", "False!")
    24. MsgBox GetSettingString(hCurKey, "Software\Microsoft\Windows\CurrentVersion\Run", _
    25. "Tom Currier", "False!")
    26. MsgBox GetSettingString(hkey, "Software\Microsoft\Windows\CurrentVersion\Run", _
    27. "Tom Currier", "False!")
    28.  
    29. End Sub

    Result:
    "RegSetValueEx was successful"
    False
    False
    False
    False
    False
    False
    VB6 Enterprise SP4

  32. #32
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    A possibility (apart from anti-virus/malware progs) is that somehow the permissions for altering entries in the "Run" key have been changed. In regedit, go to the "Current Version\Run" key, right-click on it and choose "Permissions". Make sure the "Deny" checkboxes aren't ticked. There's an "Advanced" button too...

  33. #33

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    It says:

    Permission entry for Run:

    This permission is inherited from the parent object.

    All of the 'allow' check boxes are shaded out. All of the 'deny' check boxes are unchecked.

    I'm probably getting more into this than I should. Unless you have any obvious ideas...
    VB6 Enterprise SP4

  34. #34
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Why can't I update the registry ?

    Have you tried with a MsgBox between Save and Get functions to see if the value is really in the registry?

  35. #35
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Why can't I update the registry ?

    I think post 9 is probably the closest indicator of what's going on. I assume this is not your own PC (a works PC ?). SE doesn't have the same level os security as XP, so no prob. Still, I wouldn't have thought it would make this difference. The lack of errors from my code, and the successful messages from gavio's code indicate that the key IS being written, then automatically removed. I assume you don't want to mess about with the registry's permissions...
    Quote Originally Posted by tcurrier
    I'm probably getting more into this than I should
    You could try merging a .reg file, but that may also fail. The only other registry option to you is to try to make a manual entry for testing purposes. You could always put shortcut in the startup group. For the sake of job security ............

  36. #36

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Why can't I update the registry ?

    Yep, it's my work PC and you're right.. I don't want to mess around with the registry permissions.

    Gavio,

    I have the following 9 consecutive lines of code in a subroutine. The first Msgbox retrieves the value "c:\temp\weekday.exe".

    The second Msgbox returns "False" ... Go figure !!

    VB Code:
    1. ' set:
    2. SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    3. "Tom Currier", "c:\temp\weekday.exe"
    4.  
    5. ' retrieve #1
    6. MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    7. "Tom Currier", "False!")
    8.  
    9. ' retrieve #2
    10. MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
    11. "Tom Currier", "False!")


    GetSettingString routine:

    VB Code:
    1. Public Function GetSettingString(hkey As Long, strPath As String, strValue As String, Optional Default As String) As String
    2.     Dim hCurKey As Long
    3.     Dim lValueType As Long
    4.     Dim strBuffer As String
    5.     Dim lDataBufferSize As Long
    6.     Dim intZeroPos As Integer
    7.     Dim lRegResult As Long
    8.         If Not IsEmpty(Default) Then
    9.           GetSettingString = Default
    10.         Else
    11.           GetSettingString = ""
    12.         End If
    13.             lRegResult = RegOpenKey(hkey, strPath, hCurKey)
    14.             lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
    15.                 If lRegResult = ERROR_SUCCESS Then
    16.                     If lValueType = REG_SZ Then
    17.                         strBuffer = String(lDataBufferSize, " ")
    18.                         lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
    19.                         intZeroPos = InStr(strBuffer, Chr$(0))
    20.                             If intZeroPos > 0 Then
    21.                               GetSettingString = Left$(strBuffer, intZeroPos - 1)
    22.                             Else
    23.                               GetSettingString = strBuffer
    24.                             End If
    25.                     End If
    26.                 Else
    27.                 End If
    28.                     lRegResult = RegCloseKey(hCurKey)
    29. End Function
    VB6 Enterprise SP4

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