954,558 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Writing DWORD values to the registry

Hi all,

I have this code

Option Explicit
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey 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, ByVal lpData As String, ByVal cbData As Long) As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_DWORD = 4
Dim nBufferKey As Long
Dim nVal As Long

Private Sub Form_Load()
    nVal = 2
    RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines", nBufferKey
    RegSetValueEx nBufferKey, "SandBoxMode", 0, REG_DWORD, nVal, Len(nVal)
    'MsgBox "Sandbox Mode Changed", vbInformation, "Sandbox"
    RegCloseKey nBufferKey
    Unload Me
End Sub


All I am try to do is write a DWORD value of 2 to the registry. The above code does work without error but the key has a value of 32(50), if I change nVal to 0 the key reads 30(48). I know that the RegSetValueEx function is expecting nVal to be a string but if I declare nVal as a string then the SandBoxMode DWORD value becomes an "Invalid DWORD value". Can anyone assist in getting the key to read 2.

Bomber686
Newbie Poster
17 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

I found it.... Turns out you declare the function different

Option Explicit
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
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_DWORD = 4
Dim nBufferKey As Long
Dim nVal As Long
Private Sub Form_Load()
    nVal = 2
    RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines", nBufferKey
    RegSetValueEx nBufferKey, "SandBoxMode", 0, REG_DWORD, nVal, Len(nVal)
    MsgBox "Sandbox Mode Changed", vbInformation, "Sandbox"
    RegCloseKey nBufferKey
    Unload Me
End Sub


Works now, hope this is useful

Bomber686
Newbie Poster
17 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You