Solved

Trouble with RegOpenKeyEx in NT40

Posted on 1998-07-30
8
348 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
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…

840 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