Windows 95 RegOpenKeyEx Incompatible with Windows NT (137202)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows 95
    • the operating system: Microsoft Windows 98
    • the operating system: Microsoft Windows Millennium Edition
    • the operating system: Microsoft Windows NT 4.0

This article was previously published under Q137202

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.

Modification Type:MinorLast Reviewed:9/27/2004
Keywords:kbKernBase kbRegistry KB137202