Solved

Retrieving Registry Info Problem

Posted on 1997-08-31
8
279 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 3

Accepted Solution

by:
wolfcrag earned 30 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

624 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