Performance Counters API Fault PerformanceCounterCategory.Exists RRS feed

  • Question

  • Hi

    I have noticed a problem with the C# API PerformanceCounterCategory.Exists(categoryName);

    The exe that calls this API will get a handle to each performace dll installed on the system that is not set to disabled.

    Under the registry location HKLM\System\CurrentControlSet\Services
    there are a number of Performance sub folders which contain a Library string. This string will point to a dll.

    The exe will get a handle to each dll named in the Library, with the exception that the DWORD Disable Performance Counters exists and is set to 1 (in this case a handle to the dll is not returned).

    The reason i mention this bug is that if two apps are using performance counters and one of the apps is then shutdown. The uninstall will fail as a handle to its dlls are taken by the first app. I dont know what the process is with fixing C# bugs maybe someone can tell me where to post this correctly.

    I have included setps to reproduce the problem below.

    1) compile the attached code changing the string below "Change ME" to one of the counter names that appears in the perfmon add counters drop down list.
    Click -> Start -> Run :Type Perfmon
    Right Click in the main window->add counters -> copy the name of one of the counters to the Change Me String Below.


    using System;
    using System.Diagnostics;
    using Microsoft.VisualBasic;
    using System.Threading;

    class PerfCounterCatCreateExistMod

        public static void Main(string[] args)
            string categoryName = "Change ME";
            string machineName = "";
            bool objectExists = false;
            int hold;

            // Verify that the category name is not blank.
            if (categoryName.Length == 0)
                Console.WriteLine("Category name cannot be blank.");

            // Check whether the specified category exists.
            if (machineName.Length == 0)
                Console.WriteLine("Enter any number to make the API ");
                hold = Console.Read();
                objectExists = PerformanceCounterCategory.Exists(categoryName);

            // Tell the user whether the specified category exists.
            Console.WriteLine("Category \"{0}\" " + (objectExists ? "exists on " : "does not exist on ") +
                (machineName.Length > 0 ? "computer \"{1}\"." : "this computer."), categoryName, machineName);

            for (int i = 0; i < 1000; i++)
                Console.WriteLine("Holding DLL...");



    under HKLM\System\CurrentControlSet\Services\
    There are a number of key entries, look for one with a performance sub key and a Library string.
    Download a copy of the sysinternals tool procexp
    click find-> Find Handle or DLL
    in the text box type the name of any dll that is named in the Library string

    *Note insure that the performace subkey folder does not contain the DWORD Disable Performance Counters and that it is not set to 1.


    * Launch the application compiled in step 1

    * In procexp click the search button ("with the name of a valid dll entered in the text box").

    The search result may return but the owning process will NOT be the name of the exe you complied.

    *In the console windows for the exe you compiled, enter any number to move to the API call.

    *Click Search in procexp again

    you will see the dll is now owned by the exe you compiled. The exe will have a handle to all valid dlls under the HKLM\System\CurrentControlSet\Services\

    by valid i mean not Disabled.

    If you do reproduce this bug can you reply here or wherever the most suiatble place for this report should be.

    Thank You

    • Moved by Vijay Mullick Tuesday, October 7, 2008 8:24 AM BUG REPORT (Moved from Where is the Forum For…? to Bug Reporting for Forums Platform (not product support))
    Monday, August 11, 2008 12:57 PM