Solved

read registry value "Out Of Memory"

Posted on 2003-12-09
6
297 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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