views:

117

answers:

4

MSDN documentation seems silent on whether RegCreateKeyEx ever updates the value of the handle referred to by its second-last parameter when it fails. My tests have only shown it not to update this - i.e., I set h = 0 before the call, pass &h to a call to RegCreateKeyEx to open a non-existent key, and see h = 0 after the call. Does anyone know of any situation in which the handle would be changed?

+3  A: 

It's not documented because it's undefined. Check the return value -- that's what it's there for.

Even if it works on your particular copy of Windows, some future version is free to do whatever it wants to the HKEY passed in. Programs that rely on such unspecified behavior are broken -- period.

Just check the return value and ignore the HKEY in the event of an error -- this is the only correct way to use RegCreateKeyEx.

Billy ONeal
A: 

I don't know the particular behavior of this function. However the second to last parameter represents the newly created handle. On failure I would expect it to point to an invalid handle value which is typically represented by 0. So having 0 after the function call fails is completely expected.

As to whether or not it would set it to 0 on failure I do not know. But if it's not documented I certainly wouldn't depend on it.

JaredPar
A: 

The API documentation (to which it would have been polite to provide a link to in your question), says:

A pointer to a variable that receives a handle to the opened or created key.

Why would you think it should change the handle if the open or create failed? The documentation says:

If the function succeeds, the return value is ERROR_SUCCESS.

which is what you should test.

anon
I don't think it should. My tests show that it doesn't, at least in my test environment. I do test the return value, always have, why would you do anything else? BUT, accidentally, a small piece of my code made the assumption that it would *not* be changed, and there is a claim from people analyzing bug reports that it looks like it was changed. Hence my question. What I was hoping was that some Microsoft insider would say that in a certain case, it was changed, and I could correlate that with bug reports.
Permaquid
A: 

The API makes no guarantees, but if it's going to be set to anything on failure, it will be set to 0, so to test this you should set it to something other than 0 before making the call, and then see if it gets set to 0 on failure.

John Knoeller