?
Solved

Trouble with RegOpenKeyEx in NT40

Posted on 1998-07-30
8
Medium Priority
?
360 Views
Last Modified: 2012-06-21
I have declared a function for the API Call RegOpenKeyEx.  When using the function in windows NT40 the call returns a value of 5 instead of the expected 0.  Incidently, the function works fine under windows 95 and 98.  The API Book I have notes that this API should work with either 95 or NT.  Has anyone had this same kind of problem with this API? Here is the code for my procedure. Maybe I have something incorrect. Basically, all this procedure does it pull the display names from the "uninstall" key found under HKEY_LOCAL_MACHINE, and lists them in a listbox control.

#If Win32 Then
    Dim ft As FILETIME
    Dim keyhandle&
    Dim res&
    Dim curidx&
    Dim keyname$, ClassName$
    Dim keylen&, classlen&
    Dim msg$
    Dim Reserved&
    Dim RegPath
    RegPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    res& = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 0, KEY_READ, keyhandle)
    Debug.Print res  ' Here is where the result ends up as 5
    If res <> ERROR_SUCCESS Then
        MsgBox "Can't open Stupid key"
        Exit Sub
    End If
    Do
        keylen& = 2000
        classlen& = 2000
        keyname$ = String$(keylen, 0)
        ClassName$ = String$(classlen, 0)
        res = RegEnumKeyEx(keyhandle, curidx, keyname$, keylen, Reserved, ClassName$, classlen, ft)
        curidx = curidx + 1
        If res = ERROR_SUCCESS Then
                lstitem = Left$(keyname$, keylen)
                Debug.Print lstitem
                Dim regvalue As Variant
                regvalue = QueryValue("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" & "\" & lstitem, "DisplayName")
                If regvalue = "" Then
                ' "Not Installed"
                Else
                lstApps2.AddItem regvalue
                lstApps2.ItemData(lstApps2.NewIndex) = curidx
                End If
                End If
    Loop While res = ERROR_SUCCESS
   
    Call RegCloseKey(keyhandle)
    CompuClass
    SendTwoFile
#Else
    MsgBox "This function is not supported under Win16 in this example."
#End If
End Sub

Public Sub FindPrinters()
#If Win32 Then
    Dim ft As FILETIME
    Dim keyhandle&
    Dim res&
    Dim curidx&
    Dim keyname$, ClassName$
    Dim keylen&, classlen&
    Dim msg$
    Dim Reserved&
    Dim RegPath
   
    res& = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "system\currentcontrolset\control\print\printers", 0, KEY_READ, keyhandle)
    If res <> ERROR_SUCCESS Then
        MsgBox "Can't open key"
        Exit Sub
    End If
    Do
        keylen& = 2000
        classlen& = 2000
        keyname$ = String$(keylen, 0)
        ClassName$ = String$(classlen, 0)
        res = RegEnumKeyEx(keyhandle, curidx, keyname$, keylen, Reserved, ClassName$, classlen, ft)
        curidx = curidx + 1
        If res = ERROR_SUCCESS Then
                lstitem = Left$(keyname$, keylen)
                lstPrinters.AddItem lstitem
                lstPrinters.ItemData(lstPrinters.NewIndex) = curidx
        End If
    Loop While res = ERROR_SUCCESS
   
    Call RegCloseKey(keyhandle)
    Send2File
#Else
    MsgBox "This function is not supported under Win16 in this example."
#End If
0
Comment
Question by:gwaltersii
[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
  • 2
8 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1467304
The "Access Denied" error and NT-only failure suggest that you are running afoul of NT's security. Unlike with 95, not all users have the ability to access the registry.

To verify this, try running your program under NT as administrator (or as a user with administrator privileges). Your code should work. Another way to test is by changing the key from the HKEY_LOCAL_MACHINE hierarchy to a key in HKEY_CURRENT_USER, which doesn't have the same security.

Hope this helps!
0
 

Author Comment

by:gwaltersii
ID: 1467305
So a return of 5 means access denied? I am already logged in as administrator. So I should have full access to the registry.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1467306
Yes, 5 means Access Denied. Your code (the RegOpenkeyEx part of it) works fine for me, though I had to supply my declarations and constants, so that's a possible problem. I used
Const KEY_READ = &H19
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

If you change the key from HKLM to an appropriate place in HKCU what happens? If it works, it points to the security again.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:alamo
ID: 1467307
I can reproduce the error (Access Denied) by setting KEY_READ to 0, so that may well be it (missing or bad constant).

If that's it, let me lnow and I'll repost as an answer.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1467308
So, did it work?
0
 

Author Comment

by:gwaltersii
ID: 1467309
the Const KEY_READ = &H19 worked! thanks! Sorry I couldn't get back sooner.. Experts Exchange was down last night. Lock the question and I'll grade your answer.

0
 
LVL 6

Accepted Solution

by:
alamo earned 400 total points
ID: 1467310
Excellent! Glad I could help.
0
 

Expert Comment

by:kpatil
ID: 22331769
does it have any threat to security when i change access to key all to key read ??>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 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…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 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