You misunderstand how PdhLookupPerfNameByIndex () works. Its task is not to match the performance counter, but to display a string. It should be used for both the category of the counter and its name. Not for the counter instance, if applicable, it is not localized.
The best way to see what it does is to use Regedit.exe. Go to HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Perflib. Pay attention to the key "009", its value "Counter" has an index for matching strings in English. Double-click "Counter" and copy the contents of the window into a text editor to see better. The key "CurrentLanguage" is the same mapping, but uses localized names.
So, PdhLookupPerfNameByIndex () uses the CurrentLanguage key, use the list obtained in the previous step to find the row index number. Another way to do this, as indicated (in confusion) at the bottom of the KB article, is to first look at the index number from the registry key "009". This allows you to translate from an English string to a localized string. Please note that the database location does not indicate the location of the registry, I do not know why.
Keep in mind that it is less perfect, as indicated in the KB article, these mappings exist only for “base” counters, and the key “009” is ambiguous because some indices refer to the same row. Testing on a localized version of Windows is very important.
Some code that does this in both directions:
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using System.Diagnostics; using System.Runtime.InteropServices; public static class PerfMapper { private static Dictionary<string, int> English; private static Dictionary<int, string> Localized; public static PerformanceCounter FromEnglish(string category, string name, string instance = null) { return new PerformanceCounter(Map(category), Map(name), instance); } public static PerformanceCounter FromIndices(int category, int name, string instance = null) { return new PerformanceCounter(PdhMap(category), PdhMap(name), instance); } public static bool HasName(string name) { if (English == null) LoadNames(); if (!English.ContainsKey(name)) return false; var index = English[name]; return !Localized.ContainsKey(index); } public static string Map(string text) { if (HasName(text)) return Localized[English[text]]; else return text; } private static string PdhMap(int index) { int size = 0; uint ret = PdhLookupPerfNameByIndex(null, index, null, ref size); if (ret == 0x800007D2) { var buffer = new StringBuilder(size); ret = PdhLookupPerfNameByIndex(null, index, buffer, ref size); if (ret == 0) return buffer.ToString(); } throw new System.ComponentModel.Win32Exception((int)ret, "PDH lookup failed"); } private static void LoadNames() { string[] english; string[] local;
Sample Usage:
class Program { static void Main(string[] args) { var ctr1 = PerfMapper.FromEnglish("Processor", "% Processor Time"); var ctr2 = PerfMapper.FromIndices(238, 6); } }
I only have access to the English version of Windows, so I can not vouch for the accuracy in the localized version. Please correct all errors that you encounter by editing this message.