Solved

Retrieving Registry Info Problem

Posted on 1997-08-31
8
270 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 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…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now