BUG: RegQueryInfoKey's lpcbMaxSubKeyLen Returns Incorrect Data (191806)



The information in this article applies to:

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

This article was previously published under Q191806

SYMPTOMS

RegQueryInfoKey's lpcbMaxSubKeyLen returns incorrect longest SubKey name length. Sometimes it returns the combined length of all SubKeys in the hierarchy. For example:
  1. Create a registry key:
    HKEY_LOCAL_MACHINE\System\Foo\SubKey
    						
    (SubKey is the only SubKey under Foo.)
  2. Obtain a handle to:
    HKEY_LOCAL_MACHINE\System\Foo
    					
RegQueryInfoKey API should return the longest subkey name length as 6 for SubKey on Windows NT. In the incorrect case it returns a length of System\Foo\SubKey, which is 17.

NOTE: Under Windows NT, lpcbMaxSubKeyLen count does not include the terminating null character. Under Windows 95 and Windows 98, lpcbMaxSubKeyLen count includes the terminating null character.

RESOLUTION

You should create the registry key and SubKey(s) using the following technique:
   HKEY  hMainKey, hSubKey;
   DWORD dwDisp, dwValues, cbMaxSubKeyLen;

   RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\Foo", 0, REG_NONE,
   REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hMainKey, &dwDisp);

   RegCreateKey(hMainKey, "SubKey", &hSubKey);
   RegCloseKey(hSubKey);
   RegQueryInfoKey (hMainKey, NULL, NULL, NULL, &dwValues, &cbMaxSubKeyLen,
   NULL, NULL, NULL, NULL, NULL, NULL);
   RegCloseKey(hMainKey);
				

STATUS

Microsoft has confirmed this to be a bug in Microsoft Windows NT.

NOTE: This has been fixed in Microsoft Windows 2000 and Windows XP.

MORE INFORMATION

Steps to Reproduce Behavior

The following code reproduces the incorrect behavior with RegQueryInfoKey():
   HKEY  hMainKey, hSubKey;
   DWORD dwDisp, dwValues, cbMaxSubKeyLen;

   RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\Foo", 0, REG_NONE,
   REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hMainKey, &dwDisp);

   RegCreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\\Foo\\SubKey", &hSubKey);
   RegCloseKey(hSubKey);
   RegQueryInfoKey (hMainKey, NULL, NULL, NULL, &dwValues, &cbMaxSubKeyLen,
   NULL, NULL, NULL, NULL, NULL, NULL);
   RegCloseKey(hMainKey);
				

Modification Type:MajorLast Reviewed:10/27/2003
Keywords:kbAPI kbbug kbKernBase kbpending kbRegistry KB191806