Sam
Sam

Reputation: 11

Should I be closing HKEY?

If I do:

HKEY lKey = NULL;

if(AssocQueryKey(..., &lKey) == S_OK)
{
    :
    if(RegCloseKey(lKey) == ERROR_SUCCESS)
    {
        //success
        int a = 0;//<- goes through here
    }
    else
    {
        //failure
        int a = 0;
    }
    if(RegCloseKey(lKey) == ERROR_SUCCESS)
    {
        //success
        int a = 0;
    }
    else
    {
        //failure
        int a = 0;//<- goes through here
    }
}

It would appear that lKey needs to be closed, but the documentation for AssocQueryKey says nothing about it, and the help for RegCloseKey specifically says "the handle must have been opened by the RegCreateKeyEx, RegCreateKeyTransacted, RegOpenKeyEx, RegOpenKeyTransacted, or RegConnectRegistry function".

Should I be closing lKey? If so, how?

Upvotes: 0

Views: 874

Answers (3)

David Heffernan
David Heffernan

Reputation: 613441

An HKEY needs to be closed. Nobody else is going to close this one for you, so you need to do so.

Upvotes: 0

Steve Townsend
Steve Townsend

Reputation: 54178

You can be sure about how handles are managed over time in your process using the Handle utility. Run your program in a debugger, and look at its handle usage (esp. Registry category) before and after the call to AssocQueryKey.

If there's a new Registry handle after the call, my guess is you have to close it, because afai can see nobody else is going to.

Upvotes: 0

Dialecticus
Dialecticus

Reputation: 16769

The documentation seems to be at fault. Consider this MSDN page with the code in which the key is closed after obtained with AssocQueryKey.

Upvotes: 1

Related Questions