Instead of using the raw registry api's, just use the ATL CRegKey class and let it take care of closing handles for you.

No charge for this tip-of-the-day.