Registry question: Save and read binary data


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Registry question: Save and read binary data

  1. #1
    Paulo Costa Guest

    Registry question: Save and read binary data

    Hello,

    I've posted a question a few days ago at
    microsoft.public.vb.general.discussion about this subject but till now I've
    got no help. I want to store in the registry some binary data. I'm joining
    together my registry sub-routines in an ActiveX DLL but they don't work in
    what concerns to save binary data. I've been studying this subject for a
    couple of days and I think the problem maybe while using the RegSetValueEx
    API. Perhaps I'm missing something to understand how it works (if it works
    with binary in VB). I'm working with Windows NT4 sp6 and VB6 sp4. I didn't
    want to use the ActiveX DLL RegObj.dll available from Microsoft for now
    because I would have to distribute one more dll.

    The questions are:

    1. Does RegSetValueEx perform some type of conversion?

    2. Must the lpData parameter (data buffer) of the RegSetValueEx API be a
    byte array or may be a string?

    3. Is there available any example about this (I have searched the homepages
    through the web and the newsgroups)?

    The code I'm using is above. For the RegKeyValueSet function all the API
    declarations are the standard as in win32api.txt.

    Public Function RegKeyValueSet( _
    KeyRoot As Long, _
    KeyPathName As String, _
    ValueName As String, _
    Value As Variant, _
    ValueType As Long _
    ) As Boolean
    On Error GoTo Err_Handler
    Dim lngHKey As Long ' Handle to the key to be updated
    Dim lngResOpen As Long ' Buffer for RegOpenKeyEx result
    Dim lngResSetValue As Long ' Buffer for RegSetValueEx result
    Dim strValue As String ' Buffer for string values
    Dim lngValue As Long ' Buffer for long values
    ' Open the key and get its handle:
    lngResOpen = RegOpenKeyEx(KeyRoot, _
    KeyPathName, 0, _
    KEY_ALL_ACCESS, lngHKey)
    If lngResOpen = ERROR_SUCCESS Then
    Select Case ValueType
    Case REG_BINARY ' Value type is binary
    If ArrayIsVarTypeByte(Value) Then
    If ArrayIsBounded(Value) Then
    Dim lngLen As Long
    lngLen = ArrayLen(Value)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_BINARY,
    Value(0), lngLen)
    End If
    End If
    Case REG_DWORD ' Value type is Long
    lngValue = CLng(Value)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_DWORD,
    lngValue, 4)
    Case REG_SZ ' Value type is String
    strValue = Trim(CStr(Value)) & Chr(0)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_SZ, ByVal
    strValue, Len(strValue))
    End Select
    Else
    GoTo Err_Handler
    End If
    If lngResSetValue = ERROR_SUCCESS Then
    ' Close the handle to the key:
    RegCloseKey (lngHKey)
    RegKeyValueSet = True
    End If
    Exit Function
    Err_Handler:
    RegCloseKey (lngHKey)
    End Function

    Other functions:

    Public Function ArrayIsBounded(ArrayVar As Variant) As Boolean
    ' Note: the application in the IDE will stop
    ' at this line when first run if the IDE error
    ' mode is not set to "Break on Unhandled Errors"
    ' (Tools/Options/General/Error Trapping)
    On Error Resume Next
    ArrayIsBounded = IsNumeric(UBound(ArrayVar))
    End Function

    Public Function ArrayIsVarTypeByte( _
    ByRef VarArg As Variant _
    ) As Boolean
    On Error GoTo Err_Handler
    Dim intByteArrayVarType As Integer
    intByteArrayVarType = vbByte Or vbArray
    If VarType(VarArg) = intByteArrayVarType Then ArrayIsVarTypeByte = True
    Err_Handler:
    End Function

    Public Function ArrayLen( _
    ByRef ArrayVar As Variant _
    ) As Long
    On Error GoTo Err_Handler
    If VarType(ArrayVar) > vbArray Then
    If ArrayIsBounded(ArrayVar) Then ArrayLen = UBound(ArrayVar) -
    LBound(ArrayVar) + 1
    Else
    GoTo Err_Handler
    End If
    Exit Function
    Err_Handler:
    ArrayLen = 0
    End Function

    It's urgent. Perhaps somebody can help. Thanks in advance.

    --
    Paulo Costa



  2. #2
    Klaus H. Probst Guest

    Re: Registry question: Save and read binary data

    Paulo,

    Check out the CRegistry class at www.vbbox.com/codebox/utility.htm
    It does binary as Unicode strings (well, byte arrays).


    --
    .. . . . . . . . . . . . . . . . . . . . . .
    Klaus H. Probst, MVP
    http://www.vbbox.com/


    Please post/reply to the newsgroup(s)


    "Paulo Costa" <pcosta@esagri.pt> wrote in message
    news:3b65cd43@news.devx.com...
    > Hello,
    >
    > I've posted a question a few days ago at
    > microsoft.public.vb.general.discussion about this subject but till now

    I've
    > got no help. I want to store in the registry some binary data. I'm joining
    > together my registry sub-routines in an ActiveX DLL but they don't work in
    > what concerns to save binary data. I've been studying this subject for a
    > couple of days and I think the problem maybe while using the RegSetValueEx
    > API. Perhaps I'm missing something to understand how it works (if it works
    > with binary in VB). I'm working with Windows NT4 sp6 and VB6 sp4. I didn't
    > want to use the ActiveX DLL RegObj.dll available from Microsoft for now
    > because I would have to distribute one more dll.
    >
    > The questions are:
    >
    > 1. Does RegSetValueEx perform some type of conversion?
    >
    > 2. Must the lpData parameter (data buffer) of the RegSetValueEx API be a
    > byte array or may be a string?
    >
    > 3. Is there available any example about this (I have searched the

    homepages
    > through the web and the newsgroups)?
    >
    > The code I'm using is above. For the RegKeyValueSet function all the API
    > declarations are the standard as in win32api.txt.
    >
    > Public Function RegKeyValueSet( _
    > KeyRoot As Long, _
    > KeyPathName As String, _
    > ValueName As String, _
    > Value As Variant, _
    > ValueType As Long _
    > ) As Boolean
    > On Error GoTo Err_Handler
    > Dim lngHKey As Long ' Handle to the key to be updated
    > Dim lngResOpen As Long ' Buffer for RegOpenKeyEx result
    > Dim lngResSetValue As Long ' Buffer for RegSetValueEx result
    > Dim strValue As String ' Buffer for string values
    > Dim lngValue As Long ' Buffer for long values
    > ' Open the key and get its handle:
    > lngResOpen = RegOpenKeyEx(KeyRoot, _
    > KeyPathName, 0, _
    > KEY_ALL_ACCESS, lngHKey)
    > If lngResOpen = ERROR_SUCCESS Then
    > Select Case ValueType
    > Case REG_BINARY ' Value type is binary
    > If ArrayIsVarTypeByte(Value) Then
    > If ArrayIsBounded(Value) Then
    > Dim lngLen As Long
    > lngLen = ArrayLen(Value)
    > lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&,

    REG_BINARY,
    > Value(0), lngLen)
    > End If
    > End If
    > Case REG_DWORD ' Value type is Long
    > lngValue = CLng(Value)
    > lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_DWORD,
    > lngValue, 4)
    > Case REG_SZ ' Value type is String
    > strValue = Trim(CStr(Value)) & Chr(0)
    > lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_SZ, ByVal
    > strValue, Len(strValue))
    > End Select
    > Else
    > GoTo Err_Handler
    > End If
    > If lngResSetValue = ERROR_SUCCESS Then
    > ' Close the handle to the key:
    > RegCloseKey (lngHKey)
    > RegKeyValueSet = True
    > End If
    > Exit Function
    > Err_Handler:
    > RegCloseKey (lngHKey)
    > End Function
    >
    > Other functions:
    >
    > Public Function ArrayIsBounded(ArrayVar As Variant) As Boolean
    > ' Note: the application in the IDE will stop
    > ' at this line when first run if the IDE error
    > ' mode is not set to "Break on Unhandled Errors"
    > ' (Tools/Options/General/Error Trapping)
    > On Error Resume Next
    > ArrayIsBounded = IsNumeric(UBound(ArrayVar))
    > End Function
    >
    > Public Function ArrayIsVarTypeByte( _
    > ByRef VarArg As Variant _
    > ) As Boolean
    > On Error GoTo Err_Handler
    > Dim intByteArrayVarType As Integer
    > intByteArrayVarType = vbByte Or vbArray
    > If VarType(VarArg) = intByteArrayVarType Then ArrayIsVarTypeByte = True
    > Err_Handler:
    > End Function
    >
    > Public Function ArrayLen( _
    > ByRef ArrayVar As Variant _
    > ) As Long
    > On Error GoTo Err_Handler
    > If VarType(ArrayVar) > vbArray Then
    > If ArrayIsBounded(ArrayVar) Then ArrayLen = UBound(ArrayVar) -
    > LBound(ArrayVar) + 1
    > Else
    > GoTo Err_Handler
    > End If
    > Exit Function
    > Err_Handler:
    > ArrayLen = 0
    > End Function
    >
    > It's urgent. Perhaps somebody can help. Thanks in advance.
    >
    > --
    > Paulo Costa
    >
    >




  3. #3
    Klaus H. Probst Guest

    Re: Registry question: Save and read binary data

    Paulo,

    Check out the CRegistry class at www.vbbox.com/codebox/utility.htm
    It does binary as Unicode strings (well, byte arrays).


    --
    .. . . . . . . . . . . . . . . . . . . . . .
    Klaus H. Probst, MVP
    http://www.vbbox.com/


    Please post/reply to the newsgroup(s)


    "Paulo Costa" <pcosta@esagri.pt> wrote in message
    news:3b65cd43@news.devx.com...
    > Hello,
    >
    > I've posted a question a few days ago at
    > microsoft.public.vb.general.discussion about this subject but till now

    I've
    > got no help. I want to store in the registry some binary data. I'm joining
    > together my registry sub-routines in an ActiveX DLL but they don't work in
    > what concerns to save binary data. I've been studying this subject for a
    > couple of days and I think the problem maybe while using the RegSetValueEx
    > API. Perhaps I'm missing something to understand how it works (if it works
    > with binary in VB). I'm working with Windows NT4 sp6 and VB6 sp4. I didn't
    > want to use the ActiveX DLL RegObj.dll available from Microsoft for now
    > because I would have to distribute one more dll.
    >
    > The questions are:
    >
    > 1. Does RegSetValueEx perform some type of conversion?
    >
    > 2. Must the lpData parameter (data buffer) of the RegSetValueEx API be a
    > byte array or may be a string?
    >
    > 3. Is there available any example about this (I have searched the

    homepages
    > through the web and the newsgroups)?
    >
    > The code I'm using is above. For the RegKeyValueSet function all the API
    > declarations are the standard as in win32api.txt.
    >
    > Public Function RegKeyValueSet( _
    > KeyRoot As Long, _
    > KeyPathName As String, _
    > ValueName As String, _
    > Value As Variant, _
    > ValueType As Long _
    > ) As Boolean
    > On Error GoTo Err_Handler
    > Dim lngHKey As Long ' Handle to the key to be updated
    > Dim lngResOpen As Long ' Buffer for RegOpenKeyEx result
    > Dim lngResSetValue As Long ' Buffer for RegSetValueEx result
    > Dim strValue As String ' Buffer for string values
    > Dim lngValue As Long ' Buffer for long values
    > ' Open the key and get its handle:
    > lngResOpen = RegOpenKeyEx(KeyRoot, _
    > KeyPathName, 0, _
    > KEY_ALL_ACCESS, lngHKey)
    > If lngResOpen = ERROR_SUCCESS Then
    > Select Case ValueType
    > Case REG_BINARY ' Value type is binary
    > If ArrayIsVarTypeByte(Value) Then
    > If ArrayIsBounded(Value) Then
    > Dim lngLen As Long
    > lngLen = ArrayLen(Value)
    > lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&,

    REG_BINARY,
    > Value(0), lngLen)
    > End If
    > End If
    > Case REG_DWORD ' Value type is Long
    > lngValue = CLng(Value)
    > lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_DWORD,
    > lngValue, 4)
    > Case REG_SZ ' Value type is String
    > strValue = Trim(CStr(Value)) & Chr(0)
    > lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_SZ, ByVal
    > strValue, Len(strValue))
    > End Select
    > Else
    > GoTo Err_Handler
    > End If
    > If lngResSetValue = ERROR_SUCCESS Then
    > ' Close the handle to the key:
    > RegCloseKey (lngHKey)
    > RegKeyValueSet = True
    > End If
    > Exit Function
    > Err_Handler:
    > RegCloseKey (lngHKey)
    > End Function
    >
    > Other functions:
    >
    > Public Function ArrayIsBounded(ArrayVar As Variant) As Boolean
    > ' Note: the application in the IDE will stop
    > ' at this line when first run if the IDE error
    > ' mode is not set to "Break on Unhandled Errors"
    > ' (Tools/Options/General/Error Trapping)
    > On Error Resume Next
    > ArrayIsBounded = IsNumeric(UBound(ArrayVar))
    > End Function
    >
    > Public Function ArrayIsVarTypeByte( _
    > ByRef VarArg As Variant _
    > ) As Boolean
    > On Error GoTo Err_Handler
    > Dim intByteArrayVarType As Integer
    > intByteArrayVarType = vbByte Or vbArray
    > If VarType(VarArg) = intByteArrayVarType Then ArrayIsVarTypeByte = True
    > Err_Handler:
    > End Function
    >
    > Public Function ArrayLen( _
    > ByRef ArrayVar As Variant _
    > ) As Long
    > On Error GoTo Err_Handler
    > If VarType(ArrayVar) > vbArray Then
    > If ArrayIsBounded(ArrayVar) Then ArrayLen = UBound(ArrayVar) -
    > LBound(ArrayVar) + 1
    > Else
    > GoTo Err_Handler
    > End If
    > Exit Function
    > Err_Handler:
    > ArrayLen = 0
    > End Function
    >
    > It's urgent. Perhaps somebody can help. Thanks in advance.
    >
    > --
    > Paulo Costa
    >
    >




  4. #4
    Paulo Costa Guest

    Re: Registry question: Save and read binary data

    Hi Klaus,

    Thank you very much for your information. After looking to your code I could
    put my routine to work.

    I tested your CRegistry class at www.vbbox.com/codebox/utility.htm. The
    binary data that I want to be able to save to the registry must include any
    binary data the methods SaveBinary and GetBinary worked fine even with
    UDT's. So I wanted to know what caused the wrong performance of my routine
    RegKeyValueSet and compared with yours SaveBinary. One of the differences
    was that with SaveBinary the data is passed as a string parameter and copied
    to a procedure level array of bytes variable while with my RegKeyValueSet
    the data was passed as a variant parameter. So I tried creating a procedure
    level array of bytes variable and copying the data from the variant
    parameter to it and it worked.

    I have no explanation for this behavior but it worked. So I improved my
    routine RegKeyValueSet and now it seems like this:

    '< BEGIN CODE>
    Public Function RegKeyValueSet( _
    KeyRoot As Long, _
    KeyPathName As String, _
    ValueName As String, _
    ByRef Value As Variant, _
    ValueType As Long _
    ) As Boolean
    On Error GoTo Err_Handler
    Dim lngHKey As Long ' Handle to the key to be updated
    Dim lngResOpen As Long ' Buffer for RegOpenKeyEx result
    Dim lngResSetValue As Long ' Buffer for RegSetValueEx result
    Dim strValue As String ' Buffer for string values
    Dim lngValue As Long ' Buffer for long values
    Dim bytValue() As Byte ' Buffer for binary values (byte array)
    Dim lngLen As Long ' Buffer for bytValue() length
    ' Open the key and get its handle:
    lngResOpen = RegOpenKeyEx(KeyRoot, _
    KeyPathName, 0, _
    KEY_ALL_ACCESS, lngHKey)
    If lngResOpen = ERROR_SUCCESS Then
    Select Case ValueType
    Case REG_BINARY ' Value type is binary
    If IsArray(Value) Then
    If ArrayIsVarTypeByte(Value) Then
    If ArrayIsBounded(Value) Then
    bytValue = Value
    lngLen = ArrayLen(bytValue)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0,
    REG_BINARY, bytValue(0), lngLen)
    End If
    End If
    End If
    Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN ' Value type is a 32-bit number
    lngValue = CLng(Value)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_DWORD,
    lngValue, 4)
    Case REG_SZ ' Value type is String
    strValue = Trim(CStr(Value)) & Chr(0)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_SZ, ByVal
    strValue, Len(strValue))
    End Select
    Else
    GoTo Err_Handler
    End If
    If lngResSetValue = ERROR_SUCCESS Then
    ' Close the handle to the key:
    RegCloseKey (lngHKey)
    RegKeyValueSet = True
    End If
    Exit Function
    Err_Handler:
    RegCloseKey (lngHKey)
    End Function ' RegKeyValueSet
    '< END CODE>

    Thanks again, Klaus.
    PS: You've been working hard with your site, congratulations.
    --
    Paulo Costa
    http://home.talkcity.com/RedmondAve/paulo_costa/



  5. #5
    Paulo Costa Guest

    Re: Registry question: Save and read binary data

    Hi Klaus,

    Thank you very much for your information. After looking to your code I could
    put my routine to work.

    I tested your CRegistry class at www.vbbox.com/codebox/utility.htm. The
    binary data that I want to be able to save to the registry must include any
    binary data the methods SaveBinary and GetBinary worked fine even with
    UDT's. So I wanted to know what caused the wrong performance of my routine
    RegKeyValueSet and compared with yours SaveBinary. One of the differences
    was that with SaveBinary the data is passed as a string parameter and copied
    to a procedure level array of bytes variable while with my RegKeyValueSet
    the data was passed as a variant parameter. So I tried creating a procedure
    level array of bytes variable and copying the data from the variant
    parameter to it and it worked.

    I have no explanation for this behavior but it worked. So I improved my
    routine RegKeyValueSet and now it seems like this:

    '< BEGIN CODE>
    Public Function RegKeyValueSet( _
    KeyRoot As Long, _
    KeyPathName As String, _
    ValueName As String, _
    ByRef Value As Variant, _
    ValueType As Long _
    ) As Boolean
    On Error GoTo Err_Handler
    Dim lngHKey As Long ' Handle to the key to be updated
    Dim lngResOpen As Long ' Buffer for RegOpenKeyEx result
    Dim lngResSetValue As Long ' Buffer for RegSetValueEx result
    Dim strValue As String ' Buffer for string values
    Dim lngValue As Long ' Buffer for long values
    Dim bytValue() As Byte ' Buffer for binary values (byte array)
    Dim lngLen As Long ' Buffer for bytValue() length
    ' Open the key and get its handle:
    lngResOpen = RegOpenKeyEx(KeyRoot, _
    KeyPathName, 0, _
    KEY_ALL_ACCESS, lngHKey)
    If lngResOpen = ERROR_SUCCESS Then
    Select Case ValueType
    Case REG_BINARY ' Value type is binary
    If IsArray(Value) Then
    If ArrayIsVarTypeByte(Value) Then
    If ArrayIsBounded(Value) Then
    bytValue = Value
    lngLen = ArrayLen(bytValue)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0,
    REG_BINARY, bytValue(0), lngLen)
    End If
    End If
    End If
    Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN ' Value type is a 32-bit number
    lngValue = CLng(Value)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_DWORD,
    lngValue, 4)
    Case REG_SZ ' Value type is String
    strValue = Trim(CStr(Value)) & Chr(0)
    lngResSetValue = RegSetValueEx(lngHKey, ValueName, 0&, REG_SZ, ByVal
    strValue, Len(strValue))
    End Select
    Else
    GoTo Err_Handler
    End If
    If lngResSetValue = ERROR_SUCCESS Then
    ' Close the handle to the key:
    RegCloseKey (lngHKey)
    RegKeyValueSet = True
    End If
    Exit Function
    Err_Handler:
    RegCloseKey (lngHKey)
    End Function ' RegKeyValueSet
    '< END CODE>

    Thanks again, Klaus.
    PS: You've been working hard with your site, congratulations.
    --
    Paulo Costa
    http://home.talkcity.com/RedmondAve/paulo_costa/



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center