Solved

Trouble with RegOpenKeyEx in NT40

Posted on 1998-07-30
8
339 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
  • 5
  • 2
8 Comments
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
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 6

Expert Comment

by:alamo
Comment Utility
So, did it work?
0
 

Author Comment

by:gwaltersii
Comment Utility
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 100 total points
Comment Utility
Excellent! Glad I could help.
0
 

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now