Tech Support > Microsoft Windows > Development Resources > Trouble with GetPerformanceInfo()
Trouble with GetPerformanceInfo()
Posted by Daniel Berger on November 2nd, 2003


Hi all,

Win2k or WinXP
VC++ 6
psapi.h/psapi.lib

I'm having trouble getting GetPerformanceInfo() to work. When I try
to build a simple program that uses it I get an error, even though I
*am* linking against psapi.lib and can see that the function is
defined in psapi.h:

proctest.obj : error LNK2001: unresolved external symbol
_GetPerformanceInfo@8
Debug/proctest.exe : fatal error LNK1120: 1 unresolved externals

Here's a sample script (largely taken from msdn.com):

#include <windows.h>
#include <stdio.h>
#include "psapi.h"

void PrintProcessNameAndID( DWORD processID )
{
char szProcessName[MAX_PATH] = "unknown";
PROCESS_MEMORY_COUNTERS pmc;
PERFORMACE_INFORMATION perfinfo; // yes, there's a typo in the header

// Get a handle to the process.

HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );

// Get the process name.

if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;

if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName) );
}
else return;
}
else return;

// Print the process name and identifier.

printf( "%s (Process ID: %u)\n", szProcessName, processID );

GetPerformanceInfo(
&perfinfo,
sizeof(perfinfo)
);

printf("Handle count: %i\n\n",perfinfo.HandleCount);

CloseHandle( hProcess );
}

void main( )
{
// Get the list of process identifiers.

DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;

if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;

// Calculate how many process identifiers were returned.

cProcesses = cbNeeded / sizeof(DWORD);

// Print the name and process identifier for each process.

for ( i = 0; i < cProcesses; i++ )
PrintProcessNameAndID( aProcesses[i] );
}

/* end code snippet */

The other question I have is whether this method can return
performance info for an individual process or if it only returns
system-wide information. The API suggests the latter to me (i.e. it
doesn't take a process handle), but I want to make sure.

Or should I just ditch this whole approach and use the WMI interface?

Any and all help appreciated.

Regards,

Dan

Posted by Jerry Coffin on November 10th, 2003


In article <6e613a32.0311021529.50563df8@posting.google.com>, djberg96
@hotmail.com says...
According to the docs, this requires XP, so you should expect it to die
on 2k.

If you need to support Win 2K, you probably need to go a different
route.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Posted by Daniel Berger on December 4th, 2003


Jerry Coffin <jcoffin@taeus.com> wrote in message news:<MPG.1a18d33c58d284ba989bea@news.clspco.adelp hia.net>...
Ah, you are correct sir. In any case, this only gives me the total
count, not the per-process count as I had hoped. Any suggestions?

Regards,

Dan


Similar Posts