简体   繁体   中英

How do I get a Windows Performance Counter for the current process

I am writing code for a C++ application foo.exe. Inside the application, I want to log its address space usage. So the performance counter I want to look at is "\\Process(foo)\\Virtual Bytes". The problem I am encountering is that there may be multiple instances of foo.exe running on the system. In perfmon, I can see that these are referenced as "\\Process(foo#2)\\Virtual Bytes" where the #2 could be any number depending on the number of processes.

How can I construct a counter path to get the virtual bytes for the current process that handles the case where there are multiples instances of the process?

Your instance is the one that has the ID Process counter value that matches your current process ID. Unfortunately there is no other way than to get all instances and enumerate through them untill you find the instance that is yours, but that is fairly trivial to do.

If all you want is the Virtual Bytes (or other memory statistics) for your process, it's much easier to use GetProcessMemoryInfo , as described in How to determine a process "virtual size" (WinXP)? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM