Article ID: 137202
Article Last Modified on 11/21/2006
APPLIES TO
- Microsoft Win32 Application Programming Interface, when used with:
- Microsoft Windows 98 Standard Edition
- Microsoft Windows Millennium Edition
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.
Additional query words: 4.00 Windows win95fest
Keywords: kbkernbase kbregistry KB137202