在C中获取处理时间的问题(请帮帮我)

我是C和WinAPI的新手。

我花了3个多小时尝试这样做,但完全失败了。

谁能帮助我?

这是我的代码:

FILETIME *KernelTime; // Or struct _FILETIME *KernelTime HANDLE Process = OpenProcess ( PROCESS_ALL_ACCESS, FALSE, 0); // 0 is the PID of System Idle Process GetProcessTimes (Process, NULL, NULL, KernelTime, NULL); /* How to write here? */ double ElapsedProcessTime // Target !! 

我认为这些可能对解决这个问题很有用:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683223%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724280(v=vs.85).aspx

编译器:GCC-mingw32

谢谢。

从这里 :

此代码示例将输出从Windows中的性能计数器收集的当前CPU使用率数据。 结果以百分比显示,其中100%表示最大使用量,0%表示最小使用量。

 #include  #include  #include  #include  //------------------------------------------------------------------------------------------------------------------ // Prototype(s)... //------------------------------------------------------------------------------------------------------------------ int cpuusage(void); //------------------------------------------------------------------------------------------------------------------ // getcpuload() // directly prints the CPU usage on screen. This function need to be called twice with a minimum of 1 seconds // delay (msdn guideline) to display something usefull. // Also returns the usage in percent 0-100 where 100 means the system is working at maximum capacity. // Note for multiprocessor systems: // If one CPU is working at max capacity the result will (if using (_total) for PdhAddCounter() ) show a maximum // workload of 50% unless the other CPU(s) is also working at max load. //------------------------------------------------------------------------------------------------------------------ INT getcpuload() { static PDH_STATUS status; static PDH_FMT_COUNTERVALUE value; static HQUERY query; static HCOUNTER counter; static DWORD ret; static char runonce=1; char cput=0; if(runonce) { status = PdhOpenQuery(NULL, 0, &query); if(status != ERROR_SUCCESS) { printf("PdhOpenQuery() ***Error: 0x%X\n",status); return 0; } PdhAddCounter(query, TEXT("\\Processor(_Total)\\% Processor Time"),0,&counter); // A total of ALL CPU's in the system //PdhAddCounter(query, TEXT("\\Processor(0)\\% Processor Time"),0,&counter); // For systems with more than one CPU (Cpu0) //PdhAddCounter(query, TEXT("\\Processor(1)\\% Processor Time"),0,&counter); // For systems with more than one CPU (Cpu1) runonce=0; PdhCollectQueryData(query); // No error checking here return 0; } status = PdhCollectQueryData(query); if(status != ERROR_SUCCESS) { printf("PhdCollectQueryData() ***Error: 0x%X\n",status); if(status==PDH_INVALID_HANDLE) printf("PDH_INVALID_HANDLE\n"); else if(status==PDH_NO_DATA) printf("PDH_NO_DATA\n"); else printf("Unknown error\n"); return 0; } status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100 ,&ret, &value); if(status != ERROR_SUCCESS) { printf("PdhGetFormattedCounterValue() ***Error: 0x%X\n",status); return 0; } cput = value.doubleValue; printf("\n\n" "CPU Total usage: %3d%%\n",cput); return cput; } //------------------------------------------------------------------------------------------------------------------ // Entry point //------------------------------------------------------------------------------------------------------------------ int main(void) { while(1) { getcpuload(); sleep(1000); } return 0; }