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