6

What is the most efficient and fundamental way to get cpu % usage and network throughput on windows platform?

Correct Me If I'm Wrong...

  1. windows api // Win32? SDK? (c/c++) #sorry, I have no idea...
  2. Windows Management Instrumentation (WMI),
    • Win32_PerfFormattedData_Tcpip_NetworkInterface class BytesReceivedPerSec
    • Win32_PerfRawData_PerfOS_Processor class PercentProcessorTime
  3. System.Diagnostics PerformanceCounter
  4. Performance Data Helper (PDH) , pdh.dll
  5. PerformanceCounter (c#/.net framework) or another nowaday method?
  6. NtQuerySystemInformation in ntdll.dll

So what is the best way to get this values, could I only import windows.h and kernel32.dll to get both cpu percent usage and network interface speed values?

thanks

Bart
  • 19,692
  • 7
  • 68
  • 77

2 Answers2

1

In C++ I would use PdhOpenQuery, PdhAddCounter, etc... Don't know for C#, but I guess there is something available in .NET.

Richard
  • 106,783
  • 21
  • 203
  • 265
1

what is the best way

Depends on the rest of the program. In .NET (noting the C# tag on the question) both WMI and Performance Counters are easy to access.

From Win32 API, if you are using COM already WMI might be the easiest as reading Performance Data needs significant boilerplate code (whatever API you start from).

Richard
  • 106,783
  • 21
  • 203
  • 265
  • do you think there is any straight to the kernal method to collect this kind of values? – user1045565 Nov 16 '11 at 06:51
  • @user1045565 What do you mean by "the kernel method"? – Richard Nov 16 '11 at 08:09
  • could I only import windows.h and kernel32.dll to get both cpu percent usage and network interface speed values? – user1045565 Nov 17 '11 at 09:06
  • i am not saying the win32asm/MASM, just asking the C/C++ basic way, to fetch the cpu info,thanks – user1045565 Nov 17 '11 at 09:08
  • @user1045565 Also consider Waiting To Be Deleted's answer, but that does require `Pdh.h` and `Pdh.lib` (these are part of the Windows SDK). Why the restriction to just `Windows.h`/`kernel32.dll`? – Richard Nov 17 '11 at 09:19
  • alright, WMI sounds great, i will give it a try, thank you all – user1045565 Nov 17 '11 at 12:33
  • Mr. Richard, the WMI way, compared to pdh.dll , which one will reduce the system footprint, less memory use, and less CPU consume? – user1045565 Nov 17 '11 at 12:41
  • @user1045565 No idea, but the size of the two dlls is unlikely to make any difference (unless you have very unusual requirements) as it is quite possible other components will load them anyway. – Richard Nov 17 '11 at 13:39
  • according to windows SDK helper :ms-help://MS.W7SDK.1033/MS.W7SDKCOM.1033/perfctrs/perf/about_performance_counters.htm, it seems WMI is fastest way to only access cpu and network interface, – user1045565 Nov 17 '11 at 15:13