Results 1 to 5 of 5

Thread: Dword value in registry

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    Hi, I've been writing string values to the registry but now need to write a dword value for a driverID. What option would I set to indicate that I'm writing a dword value so it doesn't write a string?

    Thanks in advance,
    Wade

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    I'll assume you're using the API RegSetValueEx(), the Parameter dwType determines what Type of value is saved to the Registry, instead of using 1 which is a Null Terminated String, use the Constant REG_DWORD:

    Private Const REG_DWORD = 4


    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    Close now...it's attempting to write a Dword value, but the registry shows (invalid DWord Value). I'm trying to write hexadecimal 0x00000019 (25). I've tried sending 19, 0x00000019, and 0x00000019 (25)..none of which worked. What should I send?

    Thanks again,
    Wade

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Use these routines I had written a while ago, I've updated them to allow you to Get/Save Numeric/Character Values.
    Code:
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 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 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
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    Private Const HKEY_CURRENT_USER = &H80000001
    Private Const HKEY_CURRENT_CONFIG = &H80000005
    Private Const HKEY_CLASSES_ROOT = &H80000000
    Private Const HKEY_DYN_DATA = &H80000006
    Private Const HKEY_PERFORMANCE_DATA = &H80000004
    Private Const HKEY_USERS = &H80000003
    
    Private Sub Command1_Click()
        Call SaveRegSetting(HKEY_LOCAL_MACHINE, "Software\VB-World", "Value", 19)
        Caption = GetRegSetting(HKEY_LOCAL_MACHINE, "Software\VB-World", "Value")
    End Sub
    
    Private Sub SaveRegSetting(ByVal lKey As Long, ByVal sSubKey As String, ByVal sValue As String, ByVal vData As Variant)
        Dim lRegKey As Long
        Dim lType As Long
        Dim lData As Long
        Dim sData As String
        
        lType = IIf(VarType(vData) = vbString, 1, 4)
        If lType = 4 Then
            lData = vData
        Else
            sData = vData
        End If
        If RegCreateKey(lKey, sSubKey, lRegKey) = 0 Then
            If lType = 1 Then
                Call RegSetValueEx(lRegKey, sValue, 0&, lType, ByVal sData, Len(sData))
            Else
                Call RegSetValueEx(lRegKey, sValue, 0&, lType, lData, 4)
            End If
            Call RegCloseKey(lRegKey)
        End If
    End Sub
    
    Private Function GetRegSetting(ByVal lKey As Long, ByVal sSubKey As String, ByVal sValue As String) As Variant
        Dim lRegKey As Long
        Dim lData As Long
        Dim sData As String
        Dim lType As Long
        
        If RegOpenKey(lKey, sSubKey, lRegKey) = 0 Then
            Call RegQueryValueEx(lRegKey, sValue, 0&, lType, ByVal 0&, 0&)
            sData = Space(255)
            If lType = 1 Then
                Call RegQueryValueEx(lRegKey, sValue, 0&, lType, ByVal sData, Len(sData))
                If InStr(sData, Chr(0)) Then
                    sData = Left$(sData, InStr(sData, Chr(0)) - 1)
                End If
            Else
                Call RegQueryValueEx(lRegKey, sValue, 0&, lType, lData, 4)
            End If
            GetRegSetting = IIf(lType = 1, sData, lData)
            Call RegCloseKey(lRegKey)
        End If
    End Function
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    Thanks again, Aaron!

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