|
-
Sep 2nd, 2006, 11:45 AM
#1
Thread Starter
Addicted Member
[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...
-
Sep 2nd, 2006, 11:53 AM
#2
Re: Why can't I update the registry ?
What method are you using to store the data into the registry?
-
Sep 2nd, 2006, 12:04 PM
#3
Re: Why can't I update the registry ?
Try this:
VB Code:
[B]'in module[/B]
Option Explicit
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const REG_SZ = 1
Public Const ERROR_SUCCESS = 0&
Public Declare Function RegCloseKey Lib _
"advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKey Lib _
"advapi32.dll" Alias "RegCreateKeyA" (ByVal _
hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
Public Declare Function RegSetValueEx Lib _
"advapi32.dll" Alias "RegSetValueExA" (ByVal _
hKey As Long, ByVal lpValueName As String, ByVal _
Reserved As Long, ByVal dwType As Long, lpData As _
Any, ByVal cbData As Long) As Long
Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim hCurKey As Long
Dim lRegResult As Long
lRegResult = RegCreateKey(hKey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
If lRegResult <> ERROR_SUCCESS Then
End If
lRegResult = RegCloseKey(hCurKey)
End Sub
[B]'in form[/B]
Option Explicit
Private Sub Command1_Click()
SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "myApp", "c:\myApp.exe"
End Sub
-
Sep 2nd, 2006, 12:29 PM
#4
Re: Why can't I update the registry ?
You need to set the correct permissions to write to the registry.
VB Code:
Option Explicit
'Form level code.
Private Sub Form_Click()
modWriteRegSZ HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "MyApp", "C:\MyApp.exe"
End Sub
VB Code:
Option Explicit
'Module level code.
'Registry APIs.
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
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, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
'Registry manipulation
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_SET_VALUE = &H2
Public Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1
Private Const ERROR_SUCCESS = 0&
Public Sub modWriteRegSZ(lngHKey As Long, strSubKey As String, strValueName As String, _
strValue As String)
'Create the key and respective REG_SZ value.
Dim lngRetVal As Long
Dim lngKeyHandle As Long
'Create the key.
If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
ByVal 0&, lngKeyHandle, lngRetVal) <> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
'Open the new key.
If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) <> _
ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
'Create a key and set its value.
If RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue) + 1) _
<> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
'Close the key.
Call RegCloseKey(lngKeyHandle)
'Exit.
Exit Sub
WRITE_SZ_ERROR:
'Close the key.
Call RegCloseKey(lngKeyHandle)
'Time for any error handling.
MsgBox "Error in modWriteRegSZ"
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
-
Sep 2nd, 2006, 02:14 PM
#5
Thread Starter
Addicted Member
Re: Why can't I update the registry ?
Thanks... I added a couple message box displays to the code:
VB Code:
'Create the key.
If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
ByVal 0&, lngKeyHandle, lngRetVal) <> ERROR_SUCCESS Then
GoTo WRITE_SZ_ERROR
Else
[B]MsgBox ("success creating key")[/B]
End If
'Open the new key.
If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) <> _
ERROR_SUCCESS Then
GoTo WRITE_SZ_ERROR
Else
[B]MsgBox ("success opening new key")[/B]
End If
'Create a key and set its value.
If RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue) + 1) _
<> ERROR_SUCCESS Then
GoTo WRITE_SZ_ERROR
Else
[B]MsgBox ("success setting value")[/B]
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!
-
Sep 2nd, 2006, 02:50 PM
#6
Re: Why can't I update the registry ?
-
Sep 2nd, 2006, 02:53 PM
#7
Thread Starter
Addicted Member
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 ?
-
Sep 2nd, 2006, 02:55 PM
#8
Re: Why can't I update the registry ?
There shouldn't be any if you are administrator.
-
Sep 2nd, 2006, 03:02 PM
#9
Thread Starter
Addicted Member
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 ?
-
Sep 2nd, 2006, 03:10 PM
#10
Re: Why can't I update the registry ?
Shouldn't make any difference. Sure you're looking under Local_Machine and not Current_User ?
-
Sep 2nd, 2006, 03:14 PM
#11
Re: Why can't I update the registry ?
Just spotted that the module constant HKEY_LOCAL_MACHINE should be Public...
-
Sep 2nd, 2006, 03:14 PM
#12
Thread Starter
Addicted Member
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 ......
-
Sep 2nd, 2006, 03:20 PM
#13
Re: Why can't I update the registry ?
 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
-
Sep 2nd, 2006, 03:25 PM
#14
Thread Starter
Addicted Member
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....
-
Sep 2nd, 2006, 03:25 PM
#15
Re: Why can't I update the registry ?
 Originally Posted by gavio
edit: missunderstood you, my bad 
No prob, I should have been more specific It's getting late...
-
Sep 2nd, 2006, 03:34 PM
#16
Re: Why can't I update the registry ?
Try this and see what's the message:
VB Code:
[B]'in module[/B]
Option Explicit
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const REG_SZ = 1
Public Const ERROR_SUCCESS = 0&
Public Declare Function RegCloseKey Lib _
"advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKey Lib _
"advapi32.dll" Alias "RegCreateKeyA" (ByVal _
hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
Public Declare Function RegSetValueEx Lib _
"advapi32.dll" Alias "RegSetValueExA" (ByVal _
hKey As Long, ByVal lpValueName As String, ByVal _
Reserved As Long, ByVal dwType As Long, lpData As _
Any, ByVal cbData As Long) As Long
Public Declare Function RegOpenKey Lib _
"advapi32.dll" Alias "RegOpenKeyA" (ByVal _
hKey As Long, ByVal lpSubKey As _
String, phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib _
"advapi32.dll" Alias "RegQueryValueExA" (ByVal _
hKey As Long, ByVal lpValueName As String, ByVal _
lpReserved As Long, lpType As Long, lpData As _
Any, lpcbData As Long) As Long
Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim hCurKey As Long
Dim lRegResult As Long
lRegResult = RegCreateKey(hKey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
If lRegResult <> ERROR_SUCCESS Then
End If
lRegResult = RegCloseKey(hCurKey)
End Sub
Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
Dim hCurKey As Long
Dim lValueType As Long
Dim strBuffer As String
Dim lDataBufferSize As Long
Dim intZeroPos As Integer
Dim lRegResult As Long
If Not IsEmpty(Default) Then
GetSettingString = Default
Else
GetSettingString = ""
End If
lRegResult = RegOpenKey(hKey, strPath, hCurKey)
lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
If lRegResult = ERROR_SUCCESS Then
If lValueType = REG_SZ Then
strBuffer = String(lDataBufferSize, " ")
lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
intZeroPos = InStr(strBuffer, Chr$(0))
If intZeroPos > 0 Then
GetSettingString = Left$(strBuffer, intZeroPos - 1)
Else
GetSettingString = strBuffer
End If
End If
Else
End If
lRegResult = RegCloseKey(hCurKey)
End Function
[B]'in form[/B]
Option Explicit
Private Sub Command1_Click()
SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "myApp", "c:\myApp.exe"
MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "myApp", "False!")
End Sub
Try to restart the machine to see what happens.
Last edited by gavio; Sep 2nd, 2006 at 03:40 PM.
-
Sep 2nd, 2006, 03:39 PM
#17
Re: Why can't I update the registry ?
 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")
-
Sep 2nd, 2006, 03:50 PM
#18
Thread Starter
Addicted Member
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 ..
-
Sep 2nd, 2006, 03:53 PM
#19
Re: Why can't I update the registry ?
 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.
-
Sep 2nd, 2006, 03:59 PM
#20
Thread Starter
Addicted Member
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:
GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
and this time the message came up "False" ....
I'm going to restart my machine and try it again ....
-
Sep 2nd, 2006, 04:01 PM
#21
Re: Why can't I update the registry ?
Just spotted something in "SaveSettingString" Try altering it as follows:-
VB Code:
lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData) [B][COLOR=Red]+ 1[/COLOR][/B])
-
Sep 2nd, 2006, 04:02 PM
#22
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...
-
Sep 2nd, 2006, 05:27 PM
#23
Thread Starter
Addicted Member
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:
[B]Private Sub Command1_Click()[/B]' set:
SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "c:\temp\weekday.exe"
' retrieve:
MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
End Sub
[B]Private Sub Command2_Click()[/B]
' retrieve again:
MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
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.
-
Sep 2nd, 2006, 05:51 PM
#24
Re: Why can't I update the registry ?
Did application fired when Windows started?
-
Sep 2nd, 2006, 06:22 PM
#25
Thread Starter
Addicted Member
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.
-
Sep 2nd, 2006, 06:43 PM
#26
Thread Starter
Addicted Member
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
-
Sep 2nd, 2006, 06:43 PM
#27
Re: Why can't I update the registry ?
 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?
-
Sep 2nd, 2006, 06:47 PM
#28
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...
-
Sep 2nd, 2006, 06:49 PM
#29
Thread Starter
Addicted Member
Re: Why can't I update the registry ?
That would be interesting, but I don't have an Administrator's Id and password.
-
Sep 2nd, 2006, 06:56 PM
#30
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...
-
Sep 2nd, 2006, 07:14 PM
#31
Thread Starter
Addicted Member
Re: Why can't I update the registry ?
One more try:
VB Code:
Public Sub SaveSettingString(hkey As Long, strPath As String, strValue As String, strData As String)
Dim hCurKey As Long
Dim lRegResult As Long
lRegResult = RegCreateKey(hkey, strPath, hCurKey)
lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
If lRegResult <> ERROR_SUCCESS Then
Form1.Text1.Text = "Error in RegSetValueEx"
Else
Form1.Text1.Text = "RegSetValueEx was successful"
End If
MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
MsgBox GetSettingString(hCurKey, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
MsgBox GetSettingString(hkey, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
lRegResult = RegCloseKey(hCurKey)
MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
MsgBox GetSettingString(hCurKey, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
MsgBox GetSettingString(hkey, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
End Sub
Result:
"RegSetValueEx was successful"
False
False
False
False
False
False
-
Sep 3rd, 2006, 02:53 AM
#32
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...
-
Sep 3rd, 2006, 01:04 PM
#33
Thread Starter
Addicted Member
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...
-
Sep 3rd, 2006, 01:17 PM
#34
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?
-
Sep 3rd, 2006, 03:08 PM
#35
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...
 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 ............
-
Sep 3rd, 2006, 03:41 PM
#36
Thread Starter
Addicted Member
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:
' set:
SaveSettingString HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "c:\temp\weekday.exe"
' retrieve #1
MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
' retrieve #2
MsgBox GetSettingString(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", _
"Tom Currier", "False!")
GetSettingString routine:
VB Code:
Public Function GetSettingString(hkey As Long, strPath As String, strValue As String, Optional Default As String) As String
Dim hCurKey As Long
Dim lValueType As Long
Dim strBuffer As String
Dim lDataBufferSize As Long
Dim intZeroPos As Integer
Dim lRegResult As Long
If Not IsEmpty(Default) Then
GetSettingString = Default
Else
GetSettingString = ""
End If
lRegResult = RegOpenKey(hkey, strPath, hCurKey)
lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
If lRegResult = ERROR_SUCCESS Then
If lValueType = REG_SZ Then
strBuffer = String(lDataBufferSize, " ")
lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
intZeroPos = InStr(strBuffer, Chr$(0))
If intZeroPos > 0 Then
GetSettingString = Left$(strBuffer, intZeroPos - 1)
Else
GetSettingString = strBuffer
End If
End If
Else
End If
lRegResult = RegCloseKey(hCurKey)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|