windows 以独立于语言的方式检索性能计数器值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5581859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:35:10  来源:igfitidea点击:

Retrieve performance counter value in a language-independent way

c#c++windowsperformancecounter

提问by

Under Windows, performance counters have different names, depending on the operating system language. For example, on an English Windows version, there is the performance counter \Processor(_Total)\% Processor Time. The same counter is called \Prozessor(_Total)\Prozessorzeit (%)on a German Windows version.

在 Windows 下,性能计数器有不同的名称,具体取决于操作系统语言。例如,在英文 Windows 版本上,有性能计数器\Processor(_Total)\% Processor Time\Prozessor(_Total)\Prozessorzeit (%)在德语 Windows 版本上调用相同的计数器。

Is there any way to retrieve the performance counter value in a language-independent way (using C++ or C#)? Or is there an alternative to get the processor load of the whole computer without performance counters?

有没有办法以独立于语言的方式(使用 C++ 或 C#)检索性能计数器值?或者有没有其他方法可以在没有性能计数器的情况下获得整台计算机的处理器负载?

采纳答案by 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).

每个 PerfMon 计数器都被赋予一个唯一的(每台机器)整数 ID 来标识 PerfMon 计数器(但是在标准计数器的情况下,这个 id 保证保持不变)。

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:

将 PerfMon 计数器 ID 与其美国英语名称和本地化名称相关联的信息存储在注册表中的以下键中:

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 PdhLookupPerfNameByIndexfunction to look up the localized name for the given counter ID.

使用注册表获取 PerfMon 计数器名称(您可以将其作为标准计数器的常量嵌入应用程序中)后,您可以使用PdhLookupPerfNameByIndex函数查找给定计数器 ID 的本地化名称。

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

有关详细信息,请参阅以本地化语言 (Microsoft KB) 正确使用 PDH API

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

您可能还会发现通过 winreg (StackOverflow) 查找 perfmon counter id有点相关。

回答by Tek465B

Add this

添加这个

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

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

在您的类中导入 DLL(我的类名为 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
Program m = new Program();
            string result = m.GetEnglishName("Mémtheitroade");
            Console.WriteLine(result);
9"); 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

这是如何使用它

##代码##

回答by Puppy

There are the WinAPI functions, QueryHighPerformanceCounterand QueryHighPerformanceFrequency.

有 WinAPI 函数QueryHighPerformanceCounterQueryHighPerformanceFrequency.

回答by selbie

Have you tried using the Pdh helper functions and the PdhAddEnglishCounterfunction?

您是否尝试过使用 Pdh 辅助函数和PdhAddEnglishCounter函数?