开发者

Should I be closing HKEY?

开发者 https://www.devze.com 2023-03-18 14:13 出处:网络
If I do: HKEY lKey = NULL; if(AssocQueryKey(..., &lKey) == S_OK) { : if(RegCloseKey(lKey) == ERROR_SUCCESS)

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 h开发者_高级运维ave been opened by the RegCreateKeyEx, RegCreateKeyTransacted, RegOpenKeyEx, RegOpenKeyTransacted, or RegConnectRegistry function".

Should I be closing lKey? If so, how?


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消