BUG: RegQueryInfoKey's lpcbMaxSubKeyLen Returns Incorrect Data

ID: Q191806

The information in this article applies to:

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 the Microsoft products listed at the beginning of this article.

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);

Additional query words:
Keywords          : kbKernBase kbRegistry 
Issue type        : kbbug
Solution Type     : kbpending

Last Reviewed: August 26, 1998