Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Return code 2 trying to do RegOpenKeyEx

98 views
Skip to first unread message

Anna Rouse

unread,
Sep 3, 2003, 8:44:28 PM9/3/03
to
I am baffled by this one and would love some ideas!

I have a utility that reads data from the registry. Up to this point
it has been working well, all of the data that I have needed to read
has been in HKEY_LOCAL_MACHINE. The following is the routine that I
am using and that has been working for these entries:

Public Sub RegGetData(lSection As Long, sKeyName As String, sValueName
As String, sValue As String)

Dim lRetVal As Long
Dim hKey As Long
Dim lBuffSize As Long

sValue = Space(255)
lBuffSize = Len(sValue)

' Open the key
lRetVal = RegOpenKeyEx(lSection, sKeyName, 0, KEY_ALL_ACCESS,
hKey)

' Get the value
lRetVal = RegQueryValueEx(hKey, sValueName, 0, REG_SZ, sValue,
lBuffSize)
sValue = Left$(sValue, lBuffSize - 1)

' Close the key
RegCloseKey (hKey)

End Sub


(I know that I'm not passing back any return codes, I'm still
developing and I'll get to that later, right now I'm just checking
them through the debugger)

I now need to read something out of HKEY_CLASSES_ROOT. No matter what
key I try to access, I am getting a return code of 2 for the
RegOpenKeyEx. I have double checked, and the entries do exist. It
won't work for anything under HKEY_CLASSES_ROOT. I have tried
changing the permissions to KEY_READ. If I query a different section,
all is well. I just can't ready anything under HKEY_CLASSES_ROOT.

The user ID I am using has administrator privileges (I am on Windows
2000) It sounds like a permissions thing, but I can't figure out how
to get around it. Any ideas?

Thanks

Anna

mayayana

unread,
Sep 3, 2003, 10:58:37 PM9/3/03
to
It shouldn't be a permission issue. HKCR is actually
the same thing as HKLM\Software\Classes\.
Are you sure you've declared the constant for HKCR?

--
--
Anna Rouse <Anna....@cci-triad.com> wrote in message
news:511e99f9.03090...@posting.google.com...

Tom Esh

unread,
Sep 3, 2003, 11:02:38 PM9/3/03
to
On 3 Sep 2003 17:44:28 -0700, Anna....@cci-triad.com (Anna Rouse)
wrote:

Really hard to tell for sure without seeing your declares and what
you're passing in the hKey and lpSubKey args, but a return of 2 is
ERROR_FILE_NOT_FOUND, which in this case would mean the key doesn't
exist. (Perhaps a typo in the keyname arg.) Also be sure it doesn't
have leading or trailing "\" delimiter chars.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)

Anna Rouse

unread,
Sep 4, 2003, 10:57:17 AM9/4/03
to
My declares are
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003

The name of the key that I'm passing in is
TypeLib\{2F3042A1-65C6-4F01-A861-5781E4846791}\1.0\0\win32

But it doesn't matter what key I pass in, I always get a return code
of 2. That key does exist. I went into the registry, copied the key
value and manually copied that into the variable in the debugger, and
still got the same error. Plus, I tried other keys, such as "ADCS"
and "acwfile", and get the same result.

When I access something in HKEY_LOCAL_MACHINE it works fine. The key
I pass in then is
Software\CCI/Triad\TriadCatalog\Catalog

In this example, the subkey I pass in is null because I want the
default entry, but I haven't even gotten to that point yet, it's
failing in the open. hkey is zero because the open should fill it in

Very curious, never had a problem reading the registry like this
before ...

Tom Esh

unread,
Sep 4, 2003, 12:06:31 PM9/4/03
to
On 4 Sep 2003 07:57:17 -0700, Anna....@cci-triad.com (Anna Rouse)
wrote:

>My declares are

Yeah, that's odd allright. It really doesn't act like a security
issue, but what happens if you log in as admin?

Anna Rouse

unread,
Sep 4, 2003, 2:06:37 PM9/4/03
to
Just on a whim, I deleted the line that had the defines and retyped it
in, not changing anything, and now it works fine. Sure don't
understand, but maybe there was an unprintable character in there.
Very weird, but thanks for the help!

Tom Esh

unread,
Sep 4, 2003, 4:29:59 PM9/4/03
to
On 4 Sep 2003 11:06:37 -0700, Anna....@cci-triad.com (Anna Rouse)
wrote:

>Just on a whim, I deleted the line that had the defines and retyped it

Yep, posting declares for Api questions can save a lot of wasted time.
<bg>

steve hotmail.com>

unread,
Sep 4, 2003, 5:53:01 PM9/4/03
to
On 4 Sep 2003 07:57:17 -0700, Anna....@cci-triad.com (Anna Rouse)
wrote:

>My declares are


Anna, might just be a typo but this

Software\CCI/Triad\TriadCatalog\Catalog

(Software\CCI / Triad\TriadCatalog\Catalog)
should be this

Software\CCI\Triad\TriadCatalog\Catalog

Notice ^^^^ the key separator


Regards
steve
---------------------------
http://www.thesourcesoft.com

MikeD

unread,
Sep 5, 2003, 9:11:42 PM9/5/03
to

"Anna Rouse" <Anna....@cci-triad.com> wrote in message
news:511e99f9.03090...@posting.google.com...
> I am baffled by this one and would love some ideas!
>
> I have a utility that reads data from the registry. Up to this point
> it has been working well, all of the data that I have needed to read
> has been in HKEY_LOCAL_MACHINE. The following is the routine that I
> am using and that has been working for these entries:
>
> Public Sub RegGetData(lSection As Long, sKeyName As String, sValueName
> As String, sValue As String)
>
> Dim lRetVal As Long
> Dim hKey As Long
> Dim lBuffSize As Long
>
> sValue = Space(255)
> lBuffSize = Len(sValue)
>
> ' Open the key
> lRetVal = RegOpenKeyEx(lSection, sKeyName, 0, KEY_ALL_ACCESS,
> hKey)
>
> ' Get the value
> lRetVal = RegQueryValueEx(hKey, sValueName, 0, REG_SZ, sValue,
> lBuffSize)
> sValue = Left$(sValue, lBuffSize - 1)
>
> ' Close the key
> RegCloseKey (hKey)
>
> End Sub
>

I'd bet big bucks the problem is because you're specifying KEY_ALL_ACCESS.
Don't do this! KEY_ALL_ACCESS is just what it says. The user MUST have
*all* Registry permissions in order for the RegOpenKeyEx function to succeed
when you specify KEY_ALL_ACCESS. Only admins have all Registry permissions.
So, for any user who is not part of the Admin group, the function will
always fail. Since you're querying a value, you should use KEY_READ. You'd
use KEY_WRITE if you need to write a value or create a new Registry key.
Very seldom do you ever need to specify any other access rights. You should
only use KEY_ALL_ACCESS in very specialized circumstances and only if you've
determined the logged on user is an admin.

Mike


Mike


Greg Wright

unread,
Oct 2, 2003, 4:02:45 AM10/2/03
to

Anna, I too am pulling my hair out over this same error.
To add a piece to the puzzle, my exact same code works OK
on my Windows 98 platform. It does not work on my XP Pro
platform. I too thought it must be permissions. Something
gross is wrong. Going to support tomorrow.

Greg Wright

>.
>

alpine

unread,
Oct 2, 2003, 10:32:18 AM10/2/03
to
When an API call works on a 9x machine but not an NT based machine, it
usually means that something in your API function declare isn't
correct. The 9x OSs are a bit more forgiving of these mistakes which
accounts for the calls working on those systems.

HTH,
Bryan
____________________________________________________________
New Vision Software "When the going gets weird,"
Bryan Stafford "the weird turn pro."
alpine_don'tsen...@mvps.org Hunter S. Thompson -
Microsoft MVP-Visual Basic Fear and Loathing in LasVegas


On Thu, 2 Oct 2003 01:02:45 -0700, "Greg Wright" <gdwri...@cs.com>
wrote:

Björn Holmgren

unread,
Oct 3, 2003, 7:52:54 AM10/3/03
to
Greg,

The reason the code submitted by "Anna" doesn't work on NT/2K/XP boxes is
that it's asking for full access to the registry keys (KEY_ALL_ACCESS). This
works fine on 95/98/ME because there's no registry security implementation
on those systems.

Best practice is never to ask for more permissions than you're going to
need. In this case KEY_QUERY_VALUE should be enough.

--
Björn Holmgren
Guide Konsult AB

"Greg Wright" <gdwri...@cs.com> wrote in message
news:195701c388bb$900f0070$a101...@phx.gbl...

0 new messages