I have written a performance data retrieval function which uses
RegQueryValueEx(HKEY_PERFORMANCE_DATA, "<threadsindex> costly", ......)
to retrieve data about all the threads on my system. <threadsindex> is the
index of the Threads peformance object.
This works fine - I allocate myself some heap memory using
HeapAlloc(GetProcessHeap(), HEAP_ZERO, dwBytes)
and retrieve the performance data block in to the allocated memory.
Here's the problem though: If I (either immediately after retrieving the
data, or after analysing it) use
RegCloseKey(HKEY_PERFORMANCE_DATA)
to close the key (as required by the SDK docs), then after I have called
my performance retrieval function twice the whole application crashes with
an access violation (or sometimes 0xC0000008) in ADVAPI32.DLL around the
time that the next thread termination occurs in my app. It's difficult
with VC++ to tell exactly where my program was at the time, because the
call stack only contains ADVAPI32 and KERNEL32.
If I don't call RegCloseKey, everything works fine, but of course I've got
an open handle which prevents performance data modules from being
installed or removed (although how you remove the threads counters I don't
know).
Does anyone know of a problem with the threads counters which causes a
crash?
Thanks,
Giles
> RegCloseKey(HKEY_PERFORMANCE_DATA)
Huh? You are supposed to close the handle that you opened ...
--
Cheers,
Felix.
If you post a reply, kindly refrain from emailing it, too.
Note to spammers: fel...@mvps.org is my real email address.
No anti-spam address here. Just one comment: IN YOUR FACE!
> Huh? You are supposed to close the handle that you opened ...
>
Hi Felix,
I'm somewhat puzzled about this too. The SDK says that if you use
RegQueryValueEx(HKEY_PERFORMANCE_DATA ... , then you:
* MUST NOT call RegOpenKeyEx to open the key
* MUST use RegCloseKey to close the key.
Since there is no key returned if I just use RegOpenKeyEx, the only "key"
I am left with to close is HKEY_PERFORMANCE_DATA, which I implicitly
opened with RegQueryValueEx.
The sample in 'Custom performance monitoring for your Windows NT
applications' in the MSDN library gives examples of how to collect
performance data both for the local machine and a remote one. They only
use RegCloseKey for the remote one, because they've received a key handle
through RegConnectRegistry.
Am I only supposed to call RegCloseKey if I'm looking at a remote machine?
Thanks,
Giles
Sorry, I should have read your original post more carefully. No, as far
as I can tell, you are doing it right. I'll have to cruft a sample
together and try it out.
>Giles,
>
> > RegCloseKey(HKEY_PERFORMANCE_DATA)
>
>Huh? You are supposed to close the handle that you opened ...
He seems to be doing it correctly (I can't comment on his problem). From
the docs:
"To obtain performance data from the local system, use the RegQueryValueEx
function, with the HKEY_PERFORMANCE_DATA key. The first call opens the key;
you do not need to explicitly open the key first. However, be sure to use
the RegCloseKey function to close the handle to the key when you are
finished obtaining performance data. The user cannot install or remove a
software component while its performance data is in use."
and from a sample:
// Get perf data
while( RegQueryValueEx( HKEY_PERFORMANCE_DATA, "Global", NULL,
NULL, (LPBYTE) pdb, &BufferSize ) == ERROR_MORE_DATA ) {
BufferSize += BYTEINCREMENT;
pdb = (PPDB) realloc( pdb, BufferSize );
}
RegCloseKey(HKEY_PERFORMANCE_DATA);
- Vince
___
Vincent Fatica
Syracuse University Mathematics
vefa...@syr.edu
http://barnyard.syr.edu/~vefatica/
you (and Giles) are correct. Giles' reply finally registered on my
meagre cogitative facilities, and I noticed he wasn't talking about any
old key.
Have a look a MSDN-article Q178887, it discusses this kind of situation.
I discovered a similar problem with the immediate call of
RegCloseKey(HKEY_PERFORMANCE_DATA)
As suggested in the samples/docs, I was using a loop to increase my buffer
every time
RegQueryValueEx(HKEY_PERFORMANCE_DATA, ...) returned ERROR_MORE_DATA, until
it succeeded.
Afterwards I called RegCloseKey(HKEY_PERFORMANCE_DATA);
I had to query performance-data periodically and I realized, that doing
things this way,
caused my application to consume a bit more memory after every query. Till
it ate up all the memory in
the system. I changed my code to gather a buffer just the first time and I
am calling RegCloseKey(HKEY_PERFORMANCE_DATA)
only at the end of my application (within a destructor). Since then, the
memory-consumption of my app stays normal.
I also learned to wrap RegQueryValueEx() within a try/catch-handling, to
catch (nasty) exceptions.
Hope this helps!
Regards,
Hans Pesata