Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I use "GetRegistryString" in the following code to read string data from registry:

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 key's 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 key's 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 key's 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.

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...