Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieving Registry Info Problem

Posted on 1997-08-31
8
Medium Priority
?
280 Views
Last Modified: 2012-08-13
I want to get info from a specific registry key so am using two API functions : RegOpenKeyEx and RegQueryValueEx.
You can see the declations and code below.
My problem is that VB "performs an illegal operation " in OLEAUT32.DLL when it executes the line labeled "10:"

In fact all code works well except the mentioned line.
(I mean that RegOpenKeyEx returns a handle(always <0 ) and the first call of RegQueryValueEx perfectly determines the length of the data)

Can anyone tell me where my mistake is or help me with an easier way to retieve registry info.
------------------------------------------------------------
'General Declarations
Private 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
Private 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

Const KEY_QUERY_VALUE = &H1
Const HKEY_CURRENT_USER = &H80000001
Const ERROR_MORE_DATA = 234

here is my code:
Private Sub cmdButton_Click()

'Openning Key
Dim KeyHandle As Long
Call RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\Colors", 0, KEY_QUERY_VALUE, KeyHandle)
MsgBox "Key handle is: " & KeyHandle, , "Openning Registry Key..."
'if opening successful then Getting Value's Data
If KeyHandle Then
Dim Dtype As Long
Dim DValue As String
Dim DSize As Long, Callit As Long
DSize = 1
'calling the function with parameter lpData=0 and pcbData<>0
    'to determine how long the buffer must be
    Callit = RegQueryValueEx(KeyHandle, "Background", 0, Dtype, 0, DSize)
    If Callit = ERROR_MORE_DATA Then
        'setting buffer lenght...
        DValue = Space(DSize)
        '...and calling the function with all needed parameters
        'to retrieve rigistry info
10:   Callit = RegQueryValueEx(KeyHandle, "Background", 0, Dtype, DValue, DSize)
        'dispalying retrieved information
        lblSize.Caption = CStr(DSize)
        lblValue.Caption = DValue
        lblReturn = CStr(Callit)
    Else
        MsgBox "Error", , "Error"
    End If
End Sub
0
Comment
Question by:polygon
[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
  • 5
  • 3
8 Comments
 
LVL 2

Author Comment

by:polygon
ID: 1433156
Edited text of question
0
 
LVL 2

Author Comment

by:polygon
ID: 1433157
Edited text of question
0
 
LVL 2

Author Comment

by:polygon
ID: 1433158
Edited text of question
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Accepted Solution

by:
wolfcrag earned 60 total points
ID: 1433159
Change line 10 to read:

10: Callit = RegQueryValueEx(KeyHandle, "Background", 0, Dtype, ByVal DValue, DSize)

Also, are you setting Dtype at all? I think the value it should be set to is 1 (REG_SZ = 1).
0
 
LVL 2

Author Comment

by:polygon
ID: 1433160
Edited text of question
0
 
LVL 3

Expert Comment

by:wolfcrag
ID: 1433161
You seem to be setting DSize to 1 early on, when really, you should be setting DType to 1. DSize is set by the first call to RegQueryValueEx. I think with these two alterations, (including the one I gave in the answer above) your code should work!
0
 
LVL 3

Expert Comment

by:wolfcrag
ID: 1433162
Actually, Dtype depends on what sort of data you are extracting. If it is a string, then Dtype should be set to 1, otherwise it should be set to a different value. So, what sort of data are you retrieving? Binary, long, string...??
0
 
LVL 2

Author Comment

by:polygon
ID: 1433163
Thanks wolfcrag.
The whole problem was that I had omitted ByVal infront of Dvalue.
(I still don't understand why this caused VB to crash)
But now everything works.
-------------------------
I set Dsize to 1 'cos it mustn't be 0.
I think DType is only a variable to store value's type in so it don't need be 1.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

730 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