#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <pdh.h>#define MAXPATH 80int __cdecl _tmain (int argc, TCHAR **argv)
{
   HQUERY          hQuery;
   HCOUNTER        *pCounterHandle;
   PDH_STATUS      pdhStatus;
   PDH_FMT_COUNTERVALUE   fmtValue;
   DWORD           ctrType;
   CHAR            szPathBuffer[MAXPATH] = {'\0'};
   int             nRetCode = 0;   // Open the query object.
   pdhStatus = PdhOpenQuery (0, 0, &hQuery);
   pCounterHandle = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER));
/*\\Processor(_Total)\\% Processor Time CPU使用率
\\System\\Processes 当前系统进程数
\\System\\Threads 当前系统线程数
\\Memory\\Commit Limit 总共内存数K (包括虚拟内存)
\\Memory\\Committed Bytes 已用内存数K (包括虚拟内存)
\\TCP\\Connections Active 系统中已建立的 TCP连接个数其它Object Items 可以利用PdhEnumObjects()和PdhEnumObjectItems()得到反正我只要用到上面的东西:)
*/   strcat(szPathBuffer,"\\System\\Processes"); 
   pdhStatus = PdhAddCounter (hQuery,szPathBuffer,0,pCounterHandle);
   // "Prime" counters that need two values to display a 
   //   formatted value.
   pdhStatus = PdhCollectQueryData (hQuery);
   // Get the current value of this counter.
   pdhStatus = PdhGetFormattedCounterValue (*pCounterHandle,
                                              PDH_FMT_DOUBLE,
                                              &ctrType,
                                              &fmtValue);     //fmtValue.doubleValue为所要的结果
     if (pdhStatus == ERROR_SUCCESS) {
         printf (TEXT(",\"%.20g\"\n"), fmtValue.doubleValue);
     }
     else {
     // Print the error value.
         printf (TEXT("error.\"-1\"")); 
   }   // Close the query.
   pdhStatus = PdhCloseQuery (hQuery);
   return nRetCode;
}
我把上面的程序放在VC里面编译运行,编译时无错误,LInk时发生错误:
如下:-------------------Configuration: process - Win32 Debug--------------------
Linking...
process.obj : error LNK2001: unresolved external symbol _PdhCloseQuery@4
process.obj : error LNK2001: unresolved external symbol _PdhGetFormattedCounterValue@16
process.obj : error LNK2001: unresolved external symbol _PdhCollectQueryData@4
process.obj : error LNK2001: unresolved external symbol _PdhAddCounterA@16
process.obj : error LNK2001: unresolved external symbol _PdhOpenQueryA@12
Debug/process.exe : fatal error LNK1120: 5 unresolved externals
Error executing link.exe.process.exe - 6 error(s), 0 warning(s)
我的操作是,打开VC,然后New Text file,将上面的代码粘贴,然后保存为一个process.c文件,然后Compile,通过,然后再Build,发生上面的错误。