Retrieve performance counter value in a language-independent way

徘徊边缘 提交于 2019-11-29 04:07:15
Justin

Each PerfMon counter is given a unique (per machine) integer ID to identify the PerfMon counter (however in the case of the standard counters this id is guaranteed to stay the same).

The information linking PerfMon counters id's to both their US English name and their Localized name is stored in the registry in the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib

Once you have used the registry to obtain the PerfMon counter name (which you can embed in your app as a constant for standard counters) you can use the PdhLookupPerfNameByIndex function to look up the localized name for the given counter ID.

See Using PDH APIs correctly in a localized language (Microsoft KB) for more details.

You might also find Finding perfmon counter id via winreg (StackOverflow) somewhat relevant.

There are the WinAPI functions, QueryHighPerformanceCounter and QueryHighPerformanceFrequency.

Have you tried using the Pdh helper functions and the PdhAddEnglishCounter function?

Add this

using System.Runtime.InteropServices;
using Microsoft.Win32;

In your class import the DLL(my classed is named Program)

[DllImport("pdh.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern UInt32 PdhLookupPerfIndexByName(string szMachineName, string szNameBuffer, ref uint pdwIndex);

Send the name of the counter you want in your OS language and it return the english NAME

public string GetEnglishName(string name)
        {
            string buffer2 = name;
            UInt32 iRet2 = new UInt32();
            iRet3 = PdhLookupPerfIndexByName(null, buffer2, ref iRet2);
            //Console.WriteLine(iRet2.ToString());

            RegistryKey pRegKey = Registry.LocalMachine;
            pRegKey = pRegKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009");
            string[] after;
            after = (string[])pRegKey.GetValue("Counter");
            string value = iRet2.ToString();
            int pos = Array.IndexOf(after, value);
            return after[pos + 1];
        }

Here is how to use it

Program m = new Program();
            string result = m.GetEnglishName("Mémoire");
            Console.WriteLine(result);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!