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, _
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
GoTo Err_Handler
End If
If lngResSetValue = ERROR_SUCCESS Then
' Close the handle to the key:
RegCloseKey (lngHKey)
RegKeyValueSet = True
End If
Exit Function
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
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
GoTo Err_Handler
End If
Exit Function
ArrayLen = 0
End Function

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

Paulo Costa