Get Registry Value (DWORD)

ADO DOT NET

Well-known member
Joined
Dec 20, 2006
Messages
156
Hi,
I use "GetRegistryString" in the following code to read string data from registry:
Code:
Const REG_SZ = 1 Unicode nul terminated string
Const REG_BINARY = 3 Free form binary
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public 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
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
   On Error Resume Next
   Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
   retrieve nformation about the key
   lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
   If lResult = 0 Then
    If lValueType = REG_SZ Then
     Create a buffer
     strBuf = String(lDataBufSize, Chr$(0))
     retrieve the keys content
     lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
     If lResult = 0 Then
      Remove the unnecessary chr$(0)s
      RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
     End If
    ElseIf lValueType = REG_BINARY Then
     Dim strData As Integer
     retrieve the keys value
     lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
     If lResult = 0 Then
      RegQueryStringValue = strData
     End If
    End If
   End If
End Function

Function GetRegistryString(hKey As Long, strPath As String, strValue As String)
   On Error Resume Next
   Dim Ret
   Open the key
   RegOpenKey hKey, strPath, Ret
   Get the keys content
   GetRegistryString = RegQueryStringValue(Ret, strValue)
   Close the key
   RegCloseKey Ret
End Function
I just works fine but only for string values.
What modifications should I do to read DWORD data with this same code?!
Thanks.
 
Hi, this is not really what you are asking. I just wanted to let you know that you do not need to use the APIs with VB.NET to access the registry. There is a registry class in the framework under: Microsoft.Win32.

I would also get rid of the On Error Resume codes and make a Try/Catch structure to capture and handle errors.

Anyways, just a suggestion :)


Jason
 

Similar threads

G
Replies
0
Views
243
Gowrisankarrs
G
I
Replies
0
Views
156
Innovators World Wide
I
Back
Top