AV on PdhAddCounter()

Hello everyone,

I was very puzzled with an AV (invalid write address, with the CPU
monitor poping up) with the following code:

HQUERY               hQuery;
HCOUNTER             hCounter;
PDH_FMT_COUNTERVALUE fValue;
AnsiString           Path = "\\Processor(0)\\% Processor Time";

if (PdhOpenQuery(NULL, 0, &hQuery) == ERROR_SUCCESS)
{
    if (PdhAddCounter(hQuery, Path.c_str(), 0, &hCounter) ==
ERROR_SUCCESS)
    {
        if (PdhCollectQueryData(hQuery) == ERROR_SUCCESS)
        {
            if (PdhGetFormattedCounterValue(hCounter, PDH_FMT_LONG,
NULL, &fValue) == ERROR_SUCCESS)
            {
                ShowMessage("Ok!");
            }
        }
    }
    PdhCloseQuery(hQuery);

Quote
}

The AV happened on the PdhAddCounter line. This is the first time that
I encounter an API call that is able to generate an AV. I did a search
on Google but didn't find anything.

Suddenly, I had the idea of running the program outside the IDE.
Everything is working as expected.

Any ideas?

Regards,
Nick B. Cassos