Solved

Retrieving Registry Info Problem

Posted on 1997-08-31
8
267 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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