Windows 95 RegOpenKeyEx Incompatible with Windows NT

Last reviewed: January 15, 1997
Article ID: Q137202
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows 95 version 4.0
    

SYMPTOMS

A Win32-based application runs under Windows 95 but does not run correctly under Windows NT. Or a Windows-based application runs under Windows 95 but leaks registry handles under Windows NT.

CAUSE

Windows NT RegOpenKeyEx always returns a unique handle. Windows 95 RegOpenKeyEx returns the same handle each time the same key is opened. Windows 95 keeps a reference count, incrementing it each time the key is opened with RegOpenKeyEx and decrementing it each time the key is closed with RegCloseKey. The key remains open as long as the reference count is greater than zero.

Consider the following code:

   RegOpenKeyEx(OldKey, NULL, 0, MAXIMUM_ALLOWED, &NewKey)
   RegCloseKey(NewKey)
   RegQueryValue(NewKey,...)
   RegCloseKey(NewKey)

This code is incorrect, but it works under Windows 95 because OldKey and NewKey refer to the same key. However, the code fails under Windows NT, because NewKey is not valid after it is closed.

In a related issue, RegOpenKey with a NULL subkey string on Windows NT will return the same handle that was passed in. Under Windows 95, the reference count is incremented.

   RegOpenKey(OldKey, NULL, 0, MAXIMUM_ALLOWED, &NewKey)
   RegCloseKey(OldKey)
   RegQueryValue(NewKey,...)
   RegCloseKey(NewKey)

This code works under Windows 95 because NewKey is still valid after closing OldKey, but the code fails under Windows NT. Code that is correct for Windows NT (don't close the handle until both OldKey and NewKey are no longer needed) leaks registry handles under Windows 95.

RESOLUTION

You should use RegOpenKeyEx and make sure that there is a corresponding close call for each open call. This will ensure that you do not use any handle that has been closed.

STATUS

This behavior is by design.


KBCategory: kbprg kbprb
KBSubcategory: BseRegistry
Additional reference words: 4.00 Windows 95


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 15, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.