dcsimg
December 2, 2016
Hot Topics:

Accessing the Windows Registry with the API

  • November 19, 2002
  • By John Percival
  • Send Email »
  • More Articles »
Public Function GetSettingByte( _
          ByVal hKey As Long, _
          ByVal strPath As String, _
          ByVal strValueName As String, _
  Optional Default As Variant) As Variant
Dim lValueType As Long
Dim byBuffer() As Byte
Dim lDataBufferSize As Long
Dim lRegResult As Long
Dim hCurKey As Long

If Not IsEmpty(Default) Then
If VarType(Default) = vbArray + vbByte Then
GetSettingByte = Default
Else
GetSettingByte = 0
End If

Else
GetSettingByte = 0
End If

lRegResult = RegOpenKey(hKey, strPath, hCurKey)

lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, _
lValueType, ByVal 0&, lDataBufferSize)

If lRegResult = ERROR_SUCCESS Then

If lValueType = REG_BINARY Then

ReDim byBuffer(lDataBufferSize - 1) As Byte
lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, _
lValueType, byBuffer(0), lDataBufferSize)
GetSettingByte = byBuffer

End If

Else
'there is a problem
End If

lRegResult = RegCloseKey(hCurKey)

End Function

Public Sub SaveSettingByte(ByVal hKey As Long, ByVal _
strPath As String, ByVal strValueName As String, byData() As Byte)
Dim lRegResult As Long
Dim hCurKey As Long

lRegResult = RegCreateKey(hKey, strPath, hCurKey)

lRegResult = RegSetValueEx(hCurKey, strValueName, _
0&, REG_BINARY, byData(0), UBound(byData()) + 1)

lRegResult = RegCloseKey(hCurKey)

End Sub

These are probably the hardest of the functions. This is because you cannot pass arrays to API calls, so you must pass the first array element, and the number of elements. The retrieve function, initialises the default value, and then makes a call to the RegQueryValueEx to determine the length of the array. It then initialises an array of bytes as a buffer to accept the data. The RegQueryValueEx is then called again to collect the data.  The save function is easier, as we just pass the first array element, and the number in the array.

This function returns a variant containing a byte array. To use it:

Dim mybytes As Variant
Dim loopy As Integer
mybytes = GetSettingByte(HKEY_CURRENT_USER, _
    "CompanyMyApp", "MyBinaryData")

If VarType(mybytes) = vbArray + vbByte Then
For loopy = 0 To Ubound(mybytes)
Debug.Print mybytes(loopy)
Next
End If

This will print all of the array onto the Debug window.

Conclusion

Well that's it... a whirlwind tour of the Windows Registry API functions. Hopefully you followed that, but if not you can contact me, and there is always the Q and A forum for more general questions. Next week, we will look at how we can control certain aspects of Windows from the Registry, and what the Registry can be used for in your project. We will also look at creating a GetAllSettings function to mimic the VB one. That's all for this week...





Page 6 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel