Solved

Trouble with RegOpenKeyEx in NT40

Posted on 1998-07-30
8
355 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

695 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