Solved

Trouble with RegOpenKeyEx in NT40

Posted on 1998-07-30
8
344 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

813 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