Solved

Retrieving Registry Info Problem

Posted on 1997-08-31
8
273 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

777 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