We help IT Professionals succeed at work.
Get Started

Registry entry for DWORD

1,310 Views
Last Modified: 2012-05-11
I have some code to write values to the registry in VBA.  however, it's designed to write out a text string.
I need it to write out a 32-bit DWORD value.

Below are the pieces I have.

When I run the code, I get a value of 0 in the registry.  Any idea how to get a value of &H10003 into a registry value?

(FYI I'm trying to use Outlook VBA to write a registry entry that will create a new account here: HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676.  I'm having trouble with things like "Leave one Server" and "SMTP Port")

Oh, and if you happen to know how to create a Mini UID, that would help too.


Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_PERFORMANCE_DATA = 0
Public Const HKEY_PERFORMANCE_TEXT = 0 ' after Windows 2000, Unicode
Public Const HKEY_PERFORMANCE_NLSTEXT = 0 ' after Windows 2000, Unicode
Public Const HKEY_CURRENT_USER_LOCAL_SETTINGS = 0 ' as of Windows 7 and Windows Server 2008
Public Enum RegistrySections
    HK_Root = HKEY_CLASSES_ROOT
    HK_Current_User = HKEY_CURRENT_USER
    HK_Local_Machine = HKEY_LOCAL_MACHINE
    HK_Users = HKEY_USERS
    HK_Current_Config = HKEY_CURRENT_CONFIG
End Enum

Public Const REG_NONE = 0&
Public Const REG_SZ = 1&                        ' Unicode nul terminated string
Public Const REG_EXPAND_SZ = 2&
Public Const REG_BINARY = 3&
Public Const REG_DWORD = 4&                     ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4&
Public Const REG_DWORD_BIG_ENDIAN = 5&
Public Const REG_LINK = 6&
Public Const REG_MULTI_SZ = 7& '? multi-line string and foreign text?
Public Const REG_RESOURCE_LIST = 8&
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9&
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10&
Public Const REG_QWORD = 11& ' 64-bit number
Public Const REG_QWORD_LITTLE_ENDIAN = 11&

Public Enum RegistryDataTypes
    TypeUndefined = REG_NONE
    TypeString = REG_SZ ' null-terminated string
    TypeExpandedString = REG_EXPAND_SZ ' null-terminated string
    TypeBinary = REG_BINARY
    Type32Bit_Number = REG_DWORD
    TypeSymbolicLink = REG_LINK
    TypeMultipleStrings = REG_MULTI_SZ ' double-null-terminated string
    TypeResourceList = REG_RESOURCE_LIST
    TypeResourceRequirements = REG_RESOURCE_REQUIREMENTS_LIST
    Type64Bit_Number = REG_QWORD
End Enum

Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    (ByVal hRegistryKey As Long, _
    ByVal lpValueName As String, _
    ByVal dwReserved As Long, _
    ByVal dwDataType As Long, _
    ByVal lpDataBuffer As Any, _
    ByVal cbDataSize As Long) _
        As Long

...

Sub X
...
    RegistrySetValue RegistrySections.HK_Current_User, strAccountKey, _
        cLeaveOnServer, &H10003, RegistryDataTypes.Type32Bit_Number
End Sub
...
    'hTopKey must be one of the Key Constants from RegistrySection Enum
    Dim lErrCode As Long                 'returned by registry functions, should be 0&
    Dim RegistryKey As RegistryDataTypes 'return handle to opened key
    Dim lpDisposition As Long            '?
    Dim Security_Attr As LPSECURITY_ATTRIBUTES

    On Error GoTo HandleErr
    
    Security_Attr.nLength = Len(Security_Attr) ' 12&
    Security_Attr.lpSecurityDescriptor = 0&
    Security_Attr.bInheritHandle = CLng(True) ' False
    
    lErrCode = RegCreateKeyEx(hTopKey, RegistryPath, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_WRITE, Security_Attr, RegistryKey, lpDisposition)
    If lErrCode <> ERROR_SUCCESS Then
        Exit Sub       'No key open, so leave
    End If
    If DataType = Type32Bit_Number Then
        lErrCode = RegSetValueEx(RegistryKey, RegistryName, 0&, DataType, ByVal CLng(Val(RegistryValue)), 4)
    Else
        lErrCode = RegSetValueEx(RegistryKey, RegistryName, 0&, DataType, ByVal RegistryValue, CLng(Len(RegistryValue) + 1))
    End If
    lErrCode = RegCloseKey(RegistryKey)
ExitHere:
    Exit Sub

HandleErr:
    Select Case Err.Number
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Registry Functions.RegistrySetValue"
    End Select
    Resume ExitHere
' End Error handling block.
End Sub

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE