what is an invalid HKEY?

> HKEY my_openKey(… ) /* returns (HKEY)0 on failure */

Or to use C++ class with a destructor.

Or to use:
Err = MyOpenKey(…, &hk);
if( SUCCESS(Err) )
{
MyDoSomethingWithKey(hk);
RegCloseKey(hk);
}

I personally hate the FASTFAT’s way of doing all cleanup in the end of the function. This works for automatic C++ destructors, but is IMHO ugly for C.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

>As per previous email, it’s to make cleanup easier. I’m opening many

registry keys

Most registry operations are “point”, single value update/read.

For such, it is a good idea to create wrappers like MyRegSetValueExByKeyName - same as RegSetValueEx but with name instead of handle.

If you need to query/set lots of values - then re-implementing RtlQueryRegistryValues with a table can be a good idea.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

>

>I think Mr[1] Skywing has already dealt the death blow to my plan
when
>he commented that the various RegXxx(…, &handle) values can modify
>handle even in the fail case

I think most functions do update their OUT parameters even in
failure case.

Experimental evidence supports this - RegOpenKeyEx under XP sets the
output to NULL in the case of ERROR_FILE_NOT_FOUND (I just tested it).
Not documented behaviour so obviously not something to depend on. As I
conceded earlier, I’ll just do it another way.

James

“James Harper” wrote in message
news:xxxxx@ntdev…
>>
>> > I initialise the output key myself. I only need a guarantee that the
> handle
>> will be untouched on error,
>> > and that the invalid value I use (whether it be NULL or
>> INVALID_HANDLE_VALUE) will never be returned
>> > to me as a valid handle
>>
>>
>> Why do you want to do something that like that??? It is so incredibly
> fragile.
>> Unless you get ERROR_SUCCESS you can assume that your call has
> failed, check
>> the error status and act accordingly. Pure, simple, independent of a
>> particular implementation and in 100% conformance with
> documentation…
>>
>
> As per previous email, it’s to make cleanup easier. I’m opening many
> registry keys and if an error occurs at any point I want to close them,
> but I want a single cleanup routine that just runs through them all and
> checks which are open, without having to have a bool or something to say
> handlex_is_open.
>
> James

You are tempted to overload the HKEY value as boolean indicating
whether it has been opened or not. Understandable.
Then resist the temptation - or, instead of the native API that does not
hold certain contract, roll your own wrapper that does, and indulge.

HKEY my_openKey(… )
/* returns (HKEY)0 on failure. we believe that valid HKEY cannot be 0 */
{
HKEY h;

if ( 0 == RegCreateKeyEx(… &h …) {
// success:
if ( 0 == h ) {
printf(“Impossible happened!”);
RegCloseKey(h);
}
} else {
h = 0;
}
return h;
}

> As per previous email, it’s to make cleanup easier. I’m opening many registry keys and if an error

occurs at any point I want to close them, but I want a single cleanup routine that just runs through
them all and checks which are open, without having to have a bool or something to say handlex_is_open.

If you think about it carefully you will realize that, in actuality, you don’t need to either introduce any boolean or check whether handle is valid. In you want to close all handles in a single call you have to keep all open handles in some array/list/etc, right? Therefore, if your call does not return ERROR_SUCCESS you just don’t add the returned handle to this list, and that’s it. Why do you want to introduce some unnecessary complications instead of keeping it all simple???

Anton Bassov

Not all code paths do this. You should stick to the contract, experimental evidence does not a contract make.

  • S

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of James Harper
Sent: Sunday, October 24, 2010 4:36 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] what is an invalid HKEY?

>I think Mr[1] Skywing has already dealt the death blow to my plan
when
>he commented that the various RegXxx(…, &handle) values can modify
>handle even in the fail case

I think most functions do update their OUT parameters even in
failure case.

Experimental evidence supports this - RegOpenKeyEx under XP sets the output to NULL in the case of ERROR_FILE_NOT_FOUND (I just tested it).
Not documented behaviour so obviously not something to depend on. As I conceded earlier, I’ll just do it another way.

James


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

>

Not all code paths do this. You should stick to the contract,
experimental
evidence does not a contract make.

Isn’t that what I just said?

James

  • S

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-428950-
xxxxx@lists.osr.com] On Behalf Of James Harper
Sent: Sunday, October 24, 2010 4:36 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] what is an invalid HKEY?

>
> >I think Mr[1] Skywing has already dealt the death blow to my plan
when
> >he commented that the various RegXxx(…, &handle) values can
modify
> >handle even in the fail case
>
> I think most functions do update their OUT parameters even in
failure case.
>

Experimental evidence supports this - RegOpenKeyEx under XP sets the
output to
NULL in the case of ERROR_FILE_NOT_FOUND (I just tested it).
Not documented behaviour so obviously not something to depend on. As I
conceded earlier, I’ll just do it another way.

James


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer