Reading Binary and DWord Info from Registry 
Author Message
 Reading Binary and DWord Info from Registry

Does anyone know how to read Binary and Dword settings from the Registry?
I am using VB5 and the API function RegQueryValueEx.  I can read, write,
etc. strings but I can't figure out how to work with Binary and Dword info.
This is driving me nuts!!!

Any help whatsoever will be greatly appreciated!!!

Thanks in advance,
Jesse Olson



Sat, 04 Aug 2001 03:00:00 GMT  
 Reading Binary and DWord Info from Registry

Quote:

>Does anyone know how to read Binary and Dword settings from the Registry?
>I am using VB5 and the API function RegQueryValueEx.  I can read, write,
>etc. strings but I can't figure out how to work with Binary and Dword info.
>This is driving me nuts!!!

>Any help whatsoever will be greatly appreciated!!!

Well, here's a sample that I had laying around.  If you have any problems
with the example just post..

*************************************************************
** Store Binary Registry Values Example                    **
*************************************************************

Add this code to a module
(I've just called
it Module1.bas)

Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const ROOT_KEY = HKEY_LOCAL_MACHINE
Private Const KEY_NAME = "Software\YourApp\1.0"
Private Const VALUE_NAME = "Options"

Private Const REG_OPTION_NON_VOLATILE = 0
Private Const REG_BINARY = 3
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const SYNCHRONIZE = &H100000
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or _
                           KEY_QUERY_VALUE Or _
                           KEY_SET_VALUE Or _
                           KEY_CREATE_SUB_KEY Or _
                           KEY_ENUMERATE_SUB_KEYS Or _
                           KEY_NOTIFY Or _
                           KEY_CREATE_LINK) And (Not SYNCHRONIZE))

'types  (Your options go here...)
Public Type RegStruct
  Option1 As Long
  Option2 As Long
  Option3 As Long
End Type

'registry declares...
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, _
    ByRef phkResult 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, ByVal lpSecurityAttributes As Long, _
    ByRef phkResult As Long, ByRef lpdwDisposition 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, ByRef lpType As Long, _
    ByRef lpData As Any, ByRef lpcbData 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, _
    ByRef lpData As Any, ByVal cbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long

Public rsRegGlobal As RegStruct

'...now to read and write binary to the registry

Public Sub ReadRegValues()

    Dim rsReg As RegStruct
    Dim dwType As Long
    Dim dwSize As Long
    Dim hKey As Long

    On Error GoTo TheErr

    If RegOpenKeyEx(ROOT_KEY, KEY_NAME, 0, KEY_ALL_ACCESS, hKey) = 0 Then

        dwType = REG_BINARY
        dwSize = Len(rsReg)
        If RegQueryValueEx(hKey, VALUE_NAME, 0, dwType, _
                                rsReg, dwSize) = 0 Then

            'Copy settings into the regStruct record
            If dwSize = Len(rsReg) Then rsRegGlobal = rsReg
        End If
    End If
    RegCloseKey hKey
    Exit Sub
TheErr:
  '
End Sub

Public Sub WriteRegValues()

    Dim hKey As Long
    Dim dwDisposition As Long

    On Error GoTo TheErr

    ' Create or open the key where settings must be stored
    If RegCreateKeyEx(ROOT_KEY, KEY_NAME, 0, "", _
        REG_OPTION_NON_VOLATILE, _
        KEY_ALL_ACCESS, 0, hKey, dwDisposition) = 0 Then
        ' Store the settings
        RegSetValueEx hKey, VALUE_NAME, 0, REG_BINARY, rsRegGlobal, _
            Len(rsRegGlobal)
    End If

    ' Close the key
    RegCloseKey hKey
    Exit Sub
TheErr:
'
End Sub

...on a form somewhere with a command button...

Private Sub Command1_Click()

    With rsRegGlobal
        .Option1 = 1
        .Option2 = 2
        .Option3 = 3
    End With

    Module1.WriteRegValues

    Module1.ReadRegValues

    With rsRegGlobal
        Debug.Print .Option1
        Debug.Print .Option2
        Debug.Print .Option3
    End With

End Sub

;-) Cool
Hasta Luego
Adrian Maull [MCSD]



Sat, 04 Aug 2001 03:00:00 GMT  
 Reading Binary and DWord Info from Registry

Quote:

>Any Idea how would I modify this example to write (for example) "FF FF FF
FF"
>(or all 1's) into the first four bytes of a binary registry key"  Since the
>option var is long it can't go past FF FF FF 7F (which is the long max of
>2147483647).

A long with value = -1 gives you FF FF FF FF
you can also use byte arrays to deal with any length/sequence of bytes


Mon, 27 Aug 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reading Binary and DWord Info from Registry

2. Reading DWORD from Registry

3. Reading registry value (DWORD)

4. Read and write DWORD values to registry

5. Read DWORD Registry Value

6. Read file info binary from a Winword-Document

7. Reading File Header Info (Binary Files)

8. Reading File Header Info (Binary Files)

9. Read file-info binary from a Winword6.0 Document

10. Reading File Header Info (Binary Files)

11. Reading binary data from registry ?

12. Reading binary registry values (REG_BINARY)

 

 
Powered by phpBB® Forum Software