Solved

read registry value "Out Of Memory"

Posted on 2003-12-09
6
298 Views
Last Modified: 2010-05-01
I use this code to read remote registry key:
Public Function GetValue(ByVal PredefinedKey As HKEYs, ByVal KeyName As String, ByVal ValueName As String, Optional ComputerName As String) As Variant
    On Error GoTo ErrHand
   
    Dim GetHandle As Long
    Dim hKey As Long
    Dim lpData As String
    Dim lpDataDWORD As Long
    Dim lpcbData As Long
    Dim lpType As Long
    Dim lReturnCode As Long
    Dim lhRemoteRegistry As Long
    Dim lBin As Double
   
    If Left$(KeyName, 1) = "\" Then
        KeyName = Right$(KeyName, Len(KeyName) - 1)
    End If
   
    If ComputerName = "" Then
        GetHandle = RegOpenKeyEx(PredefinedKey, KeyName, 0, KEY_ALL_ACCESS, hKey)
    Else
        lReturnCode = RegConnectRegistry(ComputerName, PredefinedKey, lhRemoteRegistry)
        GetHandle = RegOpenKeyEx(lhRemoteRegistry, KeyName, 0, KEY_ALL_ACCESS, hKey)
    End If
           
    If GetHandle = ERROR_SUCCESS Then
        lpcbData = 255
        lpData = String(lpcbData, Chr(0))
       
        GetHandle = RegQueryValueEx(hKey, ValueName, 0, lpType, ByVal lpData, lpcbData)
           
        If GetHandle = ERROR_SUCCESS Then
            Select Case lpType
                Case REG_SZ
                    GetHandle = RegQueryValueExString(hKey, ValueName, 0, lpType, ByVal lpData, lpcbData)
               
                    If GetHandle = 0 Then
                        GetValue = Left$(lpData, lpcbData - 1)
                    Else
                        GetValue = ""
                    End If
                   
                Case REG_DWORD
                    GetHandle = RegQueryValueEx(hKey, ValueName, 0, lpType, lpDataDWORD, lpcbData)
                   
                    If GetHandle = 0 Then
                        GetValue = CLng(lpDataDWORD)
                    Else
                        GetValue = 0
                    End If
                Case REG_BINARY
                    GetHandle = RegQueryValueEx(hKey, ValueName, 0, lpType, lpDataDWORD, lpcbData)
                   
                    If GetHandle = 0 Then
                        GetValue = CByte(lpDataDWORD)
                    Else
                        GetValue = 0
                    End If
            End Select
        End If
       
        RegCloseKey hKey
    End If
   
    Exit Function

ErrHand:
    Err.Raise "11002", "clsRegistry", "GetValue"
End Function

and this code is OK. But if the key is REG_BINARY go in Out of Memory. Why?
0
Comment
Question by:macchitella
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9905043
Use a Variant to pick up the Value
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 9906751
Using of lpDataDWORD lpcbData data parameters is wrong and causes exception in RegQueryValueEx function. See this sample:

http://www.skillreactor.org/tutortxt/registry.htm

Saving and Retrieving Byte Arrays
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10307800
I think my answer is OK.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question