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

What is an invalid HKEY?

507 views
Skip to first unread message

Stefan Kuhr

unread,
Apr 2, 2004, 4:20:18 PM4/2/04
to
Hello everyone,

I am just wondering if code with registry functions that I am used to
write for years is correct. Maybe this is also my own little personal
paranoia...

The problem with registry functions like RegOpenKeyEx is that they both
have a return value that indicates success or failure and that they also
fill in a Handle. I usually have at the end of my routines a general
resource cleanup section that frees all resources a function claims by
testing handle values or ptrs for invalid values and closing/freeing the
handles/ptrs if they are valid. In order to do so I initialize them with
an invalid value like NULL (e.g. for heap allocations) or
INVALID_HANDLE_VALUE.

To illustrate this, I usually use for CreateFile something like this:

BOOL Foo (...)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
BOOL bResult = TRUE;

// lots of stuff follows ...

hFile = CreateFile(...);

if (INVALID_HANDLE_VALUE==hFile)
{
bResult = FALSE;
goto CLEANUP;
}

/// lots
/// of
/// stuff here can follow on success...


CLEANUP:

// this is the function's general cleanup section

if (INVALID_HANDLE_VALUE!=hFile)
CloseHandle(hFile);

return bResult;
}

Now back to registry functions like RegOpenKeyEx. What is the correct
initialization value for an HKEY? Is it NULL or is it
INVALID_HANDLE_VALUE? Or are both possible valid HKEY values and I have
to keep around an additional BOOL for the return value of RegOpenKeyEx
and the like? Up to now I inizialize all HKEYs with NULL (and test for
non-NULL in the cleanup section for a RegCloseKey) but I am getting
doubts if this is correct.

Any insights? Please don't give me answers like "Goto considered
harmful" - people who answer this haven't understood my dilemma.


TIA,

--
Stefan

Ara Avanesyan

unread,
Apr 2, 2004, 5:25:41 PM4/2/04
to
0 is an invalid HKEY so it's okay to setup the hkey to 0 (at least registry
functions check their HKEY args agains 0:)).

Btw, if you are programming in C++, you are using quite old method for
cleanup, instead you would better use one of the auto_ cleaners. This makes
the code significantly easy-to-read and less buggy.

Thanks,
Ara,
0xLLC


"Stefan Kuhr" <kust...@gmx.li> wrote in message
news:406DD912...@gmx.li...

Stefan Kuhr

unread,
Apr 3, 2004, 10:04:01 AM4/3/04
to
Hi Ara

Ara Avanesyan wrote:
>
> 0 is an invalid HKEY so it's okay to setup the hkey to 0 (at least registry
> functions check their HKEY args agains 0:)).

Thanks. So it seems like I always did the right thing, which is a big
relief for me. But how do you know that the Reg* functions internally
check for NULL HKEYs?


> Btw, if you are programming in C++, you are using quite old method for
> cleanup, instead you would better use one of the auto_ cleaners. This makes
> the code significantly easy-to-read and less buggy.

Thanks for your advice. I typically use this approach, combined with SEH
in plain C code. I sometimes use it with C++ code that aims to be a
better type-checked C code as well. Nevertheless, I don't think you can
judge from the abbreviated snippet I posted how easy to read and buggy
my code typically is :-)

--
Stefan

Ara Avanesyan

unread,
Apr 4, 2004, 9:18:59 AM4/4/04
to
Hi Stefan,

"Stefan Kuhr" <kust...@gmx.li> wrote in message

news:406ED261...@gmx.li...


> Hi Ara
>
> Ara Avanesyan wrote:
> >
> > 0 is an invalid HKEY so it's okay to setup the hkey to 0 (at least
registry
> > functions check their HKEY args agains 0:)).
>
> Thanks. So it seems like I always did the right thing, which is a big
> relief for me. But how do you know that the Reg* functions internally
> check for NULL HKEYs?

Just debug the first few instructions of the functions

>
> Nevertheless, I don't think you can
> judge from the abbreviated snippet I posted how easy to read and buggy
> my code typically is :-)

I never meant something like that about your code:) I was just talking about
the mechanisms...

>
> --
> Stefan


0 new messages