troubleshooting Question

Registry entry for DWORD

Avatar of rspahitz
rspahitzFlag for United States of America asked on
.NET ProgrammingMicrosoft AccessVisual Basic.NET
3 Comments1 Solution1319 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros