在C中获取进程时间的问题(请帮助我)
我是C和WinAPI的新手 我花了3个多小时试图做到这一点,但完全失败了 有人能帮我吗 这是我的密码:在C中获取进程时间的问题(请帮助我),c,winapi,mingw32,C,Winapi,Mingw32,我是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, K
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 !!
我认为这些可能有助于解决这个问题:
编译器:GCC-mingw32
谢谢。发件人:
此代码示例将输出从Windows中的性能计数器收集的当前CPU使用率数据。结果以百分比显示,其中100%表示最大使用量,0%表示最小使用量
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <stdio.h>
//------------------------------------------------------------------------------------------------------------------
// 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;
}
#包括
#包括
#包括
#包括
//------------------------------------------------------------------------------------------------------------------
//原型。。。
//------------------------------------------------------------------------------------------------------------------
int cpuusage(无效);
//------------------------------------------------------------------------------------------------------------------
//getcpuload()
//直接在屏幕上打印CPU使用情况。此函数需要在至少1秒的时间内调用两次
//延迟(msdn指南)以显示有用的内容。
//还返回使用率百分比0-100,其中100表示系统正在以最大容量工作。
//注意:对于多处理器系统:
//如果一个CPU以最大容量工作,则结果(如果对PdhAddCounter()使用(_total))将显示最大容量
//工作负载为50%,除非其他CPU也在最大负载下工作。
//------------------------------------------------------------------------------------------------------------------
INT getcpuload()
{
静态PDH_状态;
静态PDH_FMT_计数器值;
静态查询;
静态计数器;
静态DWORD ret;
静态字符跳动=1;
字符cput=0;
如果(跳动)
{
status=PdhOpenQuery(NULL、0和query);
如果(状态!=错误\u成功)
{
printf(“PdhOpenQuery()***错误:0x%X\n”,状态);
返回0;
}
PdhAddCounter(查询,文本(\\处理器(\\\%Processor Time))、0和计数器);//系统中所有CPU的总数
//PdhAddCounter(查询、文本(\\处理器(0)\\%处理器时间)、0和计数器);//用于具有多个CPU(Cpu0)的系统
//PdhAddCounter(查询、文本(\\处理器(1)\\%处理器时间)、0和计数器);//用于具有多个CPU(Cpu1)的系统
跳动=0;
PdhCollectQueryData(查询);//此处检查无错误
返回0;
}
状态=PdhCollectQueryData(查询);
如果(状态!=错误\u成功)
{
printf(“PhdCollectQueryData()***错误:0x%X\n”,状态);
如果(状态==PDH\u无效\u句柄)
printf(“PDH\u无效\u句柄”);
else if(状态==PDH\U无数据)
printf(“PDH\u无数据”);
其他的
printf(“未知错误\n”);
返回0;
}
状态=PdhGetFormattedCounterValue(计数器、PDH_FMT_DOUBLE、PDH_FMT_NOCAP100、ret和value);
如果(状态!=错误\u成功)
{
printf(“PdhGetFormattedCounterValue()***错误:0x%X\n”,状态);
返回0;
}
cput=value.doubleValue;
printf(“\n\n”
“CPU总使用率:%3d%%\n”,cput);
返回cput;
}
//------------------------------------------------------------------------------------------------------------------
//入口点
//------------------------------------------------------------------------------------------------------------------
内部主(空)
{
而(1)
{
getcpuload();
睡眠(1000);
}
返回0;
}
:“如果指定的进程是系统进程(0x00000000),则函数失败,最后一个错误代码是error\u INVALID\u PARAMETER。”您需要用特定信息更新您的问题。您想查询哪些信息。哪个过程?最终目标是什么?@IInspectable我想得到PID=8144的CPU时间使用率,但为什么我总是得到错误的值?谢谢(我在上面添加了我的代码)@IInspectable这次,GCC没有报告错误和警告,也没有崩溃。但只是得到了它的错误值。帮帮我,谢谢。你期望的价值是多少?你得到了什么价值?您应该检查所有返回值是否存在错误。打印wMilliseconds
以及wssecond
@IInspectable也可能有帮助。我得到的wssecond
的值是58
,并且总是得到相同的值。我希望wssecond
的值与任务管理器中显示的值相同。