Click here to Skip to main content
15,883,883 members
Articles / Mobile Apps / Windows Phone 7

Found My First Bug in Windows 7 APIs: PdhBrowseCounters Requires Elevation

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Aug 2010CPOL1 min read 11.7K   1
PdhBrowseCounters Requires Elevation in Windows 7 APIs

I just found out the following, which does not seem to be documented anywhere:

The Windows API has a function for displaying a dialog that lets the user select a specific performance counter from all counters available on the local or a remote computer. The dialog is used by perfmon.exe, for example, and looks like this:

This dialog is instantiated by calling the API function PdhBrowseCounters. This works well enough - the dialog displays all objects, counters and instances, the user is able to select a specific counter and clicks on "OK".

The bad thing is that PdhBrowseCounters always returns an empty string instead of the counter path selected by the user. This happens if the application does not run elevated (i.e., with admin rights).

I have two problems with that:

  1. It is documented nowhere that elevation is required for PdhBrowseCounters.
  2. Why is elevation necessary? The user can see all objects, counters and their instances without elevation, so why not return the path selected?

For theses reasons, I think this is a bug.

By the way, how does perfmon.exe work around this? It requires elevation...

I have tested this on Windows 7 x64 German RTM with all patches till 10/27/2009.

And here is the code I used for testing (from my free tool DiskLED):

C++
void CDialogConfig::OnBnClickedCounterpathSelect()
{
PDH_STATUS pdhStatus;
PDH_BROWSE_DLG_CONFIG oPDHBrowseDialogCfg;
TCHAR sBuffer[PDH_MAX_COUNTER_PATH + 1];

// Zero memory structures
ZeroMemory (&oPDHBrowseDialogCfg, sizeof (PDH_BROWSE_DLG_CONFIG));

// Initialize the path buffer
ZeroMemory (&sBuffer, sizeof (sBuffer));
//_tcscpy_s (sBuffer, PDH_MAX_COUNTER_PATH + 1, m_sCounterPath);

// Initialize the browser dialog window settings
oPDHBrowseDialogCfg.bIncludeInstanceIndex = FALSE; 
oPDHBrowseDialogCfg.bSingleCounterPerAdd = TRUE;
oPDHBrowseDialogCfg.bSingleCounterPerDialog = TRUE;
oPDHBrowseDialogCfg.bLocalCountersOnly = FALSE;
oPDHBrowseDialogCfg.bWildCardInstances = TRUE;
oPDHBrowseDialogCfg.bHideDetailBox = TRUE;
oPDHBrowseDialogCfg.bInitializePath = FALSE;
oPDHBrowseDialogCfg.bDisableMachineSelection = FALSE;
oPDHBrowseDialogCfg.bIncludeCostlyObjects = FALSE;
oPDHBrowseDialogCfg.bShowObjectBrowser = FALSE;
oPDHBrowseDialogCfg.hWndOwner = m_hWnd;
oPDHBrowseDialogCfg.szReturnPathBuffer = sBuffer;
oPDHBrowseDialogCfg.cchReturnPathLength = sizeof (sBuffer) / sizeof (TCHAR);
oPDHBrowseDialogCfg.pCallBack = NULL;
oPDHBrowseDialogCfg.dwCallBackArg = 0;
oPDHBrowseDialogCfg.CallBackStatus = ERROR_SUCCESS;
oPDHBrowseDialogCfg.dwDefaultDetailLevel = PERF_DETAIL_WIZARD;
oPDHBrowseDialogCfg.szDialogBoxCaption = TEXT ("Select a counter for DiskLED");

// Display the counter browser window. The dialog is configured
// to return a single selection from the counter list.
pdhStatus = PdhBrowseCounters (&oPDHBrowseDialogCfg);
if (pdhStatus != ERROR_SUCCESS)
{
if (pdhStatus != PDH_DIALOG_CANCELLED)
{
m_pMainFrame->LogError (TEXT ("OnBnClickedCounterpathSelect"), 
    TEXT ("PdhBrowseCounters"), pdhStatus);
}
}
else
{
m_sCounterPath = sBuffer;

// Update the dialog with the new data
UpdateData (FALSE);
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Helge Klein GmbH
Germany Germany
Helge Klein is an independent consultant and developer. As a consultant, he has worked in Windows and Citrix projects for various larger German corporations. As a developer, he architected sepago's user profile management product sepagoPROFILE whose successor is now available as Citrix Profile Management. In 2009 Helge received the Citrix Technology Professional (CTP) Award, in 2011 he was nominated a Microsoft Most Valuable Professional (MVP).

Helge's professional interests are focused on Microsoft server technologies, various Citrix products and programming in several languages. He publishes his knowledge in English in his blog at http://helgeklein.com/blog. Helge can also be found on Twitter as @HelgeKlein. He has presented on many occasions, e.g. Citrix TechEdge Munich 2009, ice Lingen (2009 and 2011), PubForum (2010 and 2011), Microsoft TechDay Online 2010, Citrix Synergy 2011 and 2012.

Helge is the author of SetACL, a powerful tool for managing Windows permissions from the command line or from scripts and programs. SetACL is open source and has been downloaded more than 500,000 times. SetACL's modern cousin SetACL Studio comes with an intuitive graphical user interface and is available for a small fee. Another popular tool, Delprof2, automates the deletion of user profiles.

Helge lives in Cologne, Germany.

Comments and Discussions

 
GeneralPDH.DLL on Vista & Win 7 is rife with bugs, this is just one of them Pin
Ascend4nt131-May-11 19:38
Ascend4nt131-May-11 19:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.