首页| 论坛| 消息

标题:Qt调用windows ntd dll函数获取cpu各个核的使用率遇到数据错误的问题
作者:wangqi
日期:2016-06-18 09:42
内容:

//获得CPU使用率void CpuUsage::CalProcessorsUses(){PROCNTQSINtQuerySystemInformation;__int64nIdleTime=0,nKernelTime=0,nUserTime=0,nDpcTime=0,nInterruptTime=0,nTotalTime=0;NtQuerySystemInformation=(PROCNTQSI)GetProcAddress(GetModuleHandleA("ntdll"),"NtQuerySystemInformation");//利用ntdll内置的API获取CPUint status=NtQuerySystemInformation(SystemProcessorPerformanceInformation, m_ProcessInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION_2) * m_nNumberProcessors, 0);for (int i = 0; i


#1 [wangqi 06-18 09:43]
上面乱码了,重新发下:有兴趣的可以找我要代码哈,求 解决这个问题啊

this->timer = new QTimer;
QObject::connect(this->timer,SIGNAL(timeout()),this,SLOT(get_system_info()));
this->timer->start(1000); //1秒

//获得CPU使用率
void CpuUsage::CalProcessorsUses()
{
PROCNTQSINtQuerySystemInformation;
__int64nIdleTime=0,nKernelTime=0,nUserTime=0,nDpcTime=0,nInterruptTime=0,nTotalTime=0;
NtQuerySystemInformation=(PROCNTQSI)GetProcAddress(GetModuleHandleA("ntdll"),"NtQuerySystemInformation");//利用ntdll内置的API获取CPU
int status=NtQuerySystemInformation(SystemProcessorPerformanceInformation, m_ProcessInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION_2) * m_nNumberProcessors, 0);
for (int i = 0; i < m_nNumberProcessors; i++)
{
nIdleTime = m_ProcessInfo.i64IdleTime - m_nOidleTime;
nKernelTime = m_ProcessInfo.i64KernelTime - m_nOkerenelTime;
nUserTime = m_ProcessInfo.i64UserTime - m_nOuserTime;
nDpcTime = m_ProcessInfo.i64DpcTime -m_nOdpctTime;
nInterruptTime = m_ProcessInfo.i64InterruptTime - m_nOinterruptTime;
nTotalTime = nKernelTime + nUserTime + nDpcTime;// + nInterruptTime;
m_nUses =100*(nTotalTime-nIdleTime)/nTotalTime;//计算CPU使用率
&n ..
#2 [wangqi 06-18 10:29]
调试信息:interrupttime会出现很大的负值???为啥啊? 比对了下 vs2010的调试,不会出现这种情况啊。
局部变量
NtQuerySystemInformation0x778d621c PROCNTQSI
i0int
nDpcTime4748514439long long
nIdleTime800110720882long long
nInterruptTime-7753833852956925205long long
nKernelTime836241800490long long
nTotalTime0long long
nUserTime29940647926long long
status0int
this @0x22cc40CpuUsage

回复 发表
主题 版块