I see a strange thing when connecting to the performance registry on 64-bit versions of Windows. The entire program of kiosks and stop tables becomes unreadable. After a long timeout, the connection attempt is interrupted, and everything returns to normal.
The only solution is to make sure that only one thread is requesting the remote registry, unless the remote computer is a 32-bit version of Windows XP, 2003, 2000, then you can use as many threads as you want.
Does anyone have a technical explanation why this could happen? I spent 2-3 days on the Internet without inventing anything.
Here is a test program, first run it with a single thread (connecting to 64-bit Windows), then delete the comment in tmain and run it with 4 threads. Starting with a single thread works, as expected, with 4, returns ERROR_BUSY (dwRet == 170) after stopping for a while.
Remember to install the remote computer correctly in RegConnectRegistry before starting the program.
#define TOTALBYTES 8192
#define BYTEINCREMENT 4096
void PerfmonThread(void *pData)
{
DWORD BufferSize = TOTALBYTES;
DWORD cbData;
DWORD dwRet;
PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );
cbData = BufferSize;
printf("\nRetrieving the data...");
HKEY hKey;
DWORD dwAccessRet = RegConnectRegistry(L"REMOTE_MACHINE",HKEY_PERFORMANCE_DATA,&hKey);
dwRet = RegQueryValueEx( hKey,L"global",NULL,NULL,(LPBYTE) PerfData, &cbData );
while( dwRet == ERROR_MORE_DATA )
{
BufferSize += BYTEINCREMENT;
PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, BufferSize );
cbData = BufferSize;
printf(".");
dwRet = RegQueryValueEx( hKey,L"global",NULL,NULL,(LPBYTE) PerfData,&cbData );
}
if( dwRet == ERROR_SUCCESS )
printf("\n\nFinal buffer size is %d\n", BufferSize);
else
printf("\nRegQueryValueEx failed (%d)\n", dwRet);
RegCloseKey(hKey);
}
int _tmain(int argc, _TCHAR* argv[])
{
_beginthread(PerfmonThread,0,NULL);
while(1)
{
Sleep(2000);
}
}