Get PerformanceCounter by Index

You misinterpret how PdhLookupPerfNameByIndex() works. Its job is not to map a performance counter but to map a string. It should be used both for the counter's category as well as its name. Not for the counter's instance, if applicable, it is not localized.

Best way to see what it does is by using Regedit.exe. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib. Note the "009" key, its Counter value has the index to English string mapping. Double-click Counter and copy-paste the content of the box into a text editor to have a better look-see. The "CurrentLanguage" key is the same mapping but uses the localized names.

So PdhLookupPerfNameByIndex() uses the CurrentLanguage key, use the list you obtained in the previous step to know the index number of the string. The other way to do it as noted (confusingly) at the bottom of the KB article is by first looking up the index number from the "009" registry key. This lets you translate from the English string to the localized string. Do note that the KB article documents the registry key location wrong, no idea why.

Keep in mind that it is less than perfect, as pointed out in the KB article, these mappings only exist for the "base" counters and the "009" key is ambiguous because some indices map to the same string. Testing on a localized Windows version is very important.

Some code that does it both ways:

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;
        // Retrieve English and localized strings
        using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) {
            using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009")) {
                english = (string[])key.GetValue("Counter");
            }
            using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\CurrentLanguage")) {
                local = (string[])key.GetValue("Counter");
            }
        }
        // Create English lookup table
        English = new Dictionary<string, int>(english.Length / 2, StringComparer.InvariantCultureIgnoreCase);
        for (int ix = 0; ix < english.Length - 1; ix += 2) {
            int index = int.Parse(english[ix]);
            if (!English.ContainsKey(english[ix + 1])) English.Add(english[ix + 1], index);
        }
        // Create localized lookup table
        Localized = new Dictionary<int, string>(local.Length / 2);
        for (int ix = 0; ix < local.Length - 1; ix += 2) {
            int index = int.Parse(local[ix]);
            Localized.Add(index, local[ix + 1]);
        }
    }

    [DllImport("pdh.dll", CharSet = CharSet.Auto)]
    private static extern uint PdhLookupPerfNameByIndex(string machine, int index, StringBuilder buffer, ref int bufsize);
}

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 an English version of Windows so can't vouch for accuracy on a localized version. Please correct any bugs you encounter by editing this post.

Tags:

C#

Winapi

Perfmon