Microsoft KB Archive/191806

{|
 * width="100%"|

BUG: RegQueryInfoKey's lpcbMaxSubKeyLen Returns Incorrect Data

 * }

Q191806

-

The information in this article applies to:


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

-

SYMPTOMS
RegQueryInfoKey's lpcbMaxSubKeyLen returns incorrect longest SubKey name length. Sometimes it returns the combined length of all SubKeys in the hierarchy. For example:

  Create a registry key HKEY_LOCAL_MACHINE\System\Foo\SubKey (SubKey is the only SubKey under Foo.)   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.

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 : kbAPI kbKernBase kbOSWinNT400 kbOSWin2000 kbRegistry kbDSupport kbGrpDSKernBase

Issue type : kbbug

Technology : kbAudDeveloper kbWin32sSearch kbWin32API