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
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...
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
"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