BUG: ANSI Version of RegQueryValueEx Can Cause Application HangID: Q226371
|
A Win32 application calling the ANSI version of RegQueryValueEx Win32 API with HKEY_PERFORMANCE_DATA can return incorrect information for lpcbData, the sixth parameter. In a buffer size retry loop, this can cause an application to hang.
Because the performance data block returned by RegQueryValueExA is the same as RegQueryValueExW (Unicode version), the best resolution is to call RegQueryValueExW directly, even from an ANSI build of an application. The problem does not occur in the Unicode version of RegQueryValueEx. This requires passing a Unicode string for lpValueName, the second parameter of RegQueryValueEx.
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
When using RegQueryValueEx to query performance data, exactly how much buffer space is needed for the data is not known. To determine how much space is needed, an arbitrary buffer size is tried and if RegQueryValueEx returns ERROR_MORE_DATA, a larger buffer is tried until RegQueryValueEx does not return ERROR_MORE_DATA.
On account of this problem, an application can get caught in an endless loop trying to find the appropriate size for the Perf Data buffer. Below is a typical implementation of this:
// Allocate the buffer.
PerfData = malloc(BufferSize);
while( RegQueryValueEx( HKEY_PERFORMANCE_DATA,
lpszObject,
NULL,
&dwPerfType,
(LPBYTE) PerfData,
&BufferSize ) == ERROR_MORE_DATA )
{
// Get a buffer that is big enough.
BufferSize += BYTEINCREMENT;
free(PerfData);
PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );
}
For more information on Windows NT Performance data see the Platform SDK Documentation in Windows Base Services, Performance Monitoring.
Additional query words: bug hang corrupt
Keywords : kbAPI kbKernBase kbRegistry kbDSupport
Version : winnt:4.0
Platform : winnt
Issue type : kbbug
Last Reviewed: June 23, 1999