vc6获取cpu占用率(非NtQuerySystemInformation)

NtQuerySystemInformation 在win7下无法使用,但是win10可以.

GetSystemTime在win7和win10下均可以使用.

源码来自网络.

double m_fOldCPUIdleTime;  
double m_fOldCPUKernelTime;  
double m_fOldCPUUserTime;  
typedef BOOL
(WINAPI 
 *GetSystemTimesT)(
 __out_opt LPFILETIME lpIdleTime, //空闲时间
 __out_opt LPFILETIME lpKernelTime, //内核时间
 __out_opt LPFILETIME lpUserTime //用户时间
 );

GetSystemTimesT pGetSystemTimes=(GetSystemTimesT)GetProcAddress(LoadLibrary("KERNEL32.dll"),"GetSystemTimes");
double FileTimeToDouble(FILETIME &filetime)  
{  
	return (double)(filetime.dwHighDateTime * 4.294967296E9) + (double)filetime.dwLowDateTime;  
}    
BOOL Initialize()   
{  
	FILETIME ftIdle, ftKernel, ftUser;  
	BOOL flag = FALSE;  
	if (flag = pGetSystemTimes(&ftIdle, &ftKernel, &ftUser))  
	{  
		m_fOldCPUIdleTime = FileTimeToDouble(ftIdle);  
		m_fOldCPUKernelTime = FileTimeToDouble(ftKernel);  
		m_fOldCPUUserTime = FileTimeToDouble(ftUser);  
		
	}  
	return flag;  
}
//调用Initialize后要等待1秒再调用此函数  
int GetCPUUseRate()  
{  
	int nCPUUseRate = -1;  
	FILETIME ftIdle, ftKernel, ftUser;  
	if (pGetSystemTimes(&ftIdle, &ftKernel, &ftUser))  
	{  
		double fCPUIdleTime = FileTimeToDouble(ftIdle);  
		double fCPUKernelTime = FileTimeToDouble(ftKernel);  
		double fCPUUserTime = FileTimeToDouble(ftUser);  
		nCPUUseRate= (int)(100.0 - (fCPUIdleTime - m_fOldCPUIdleTime)   
			/ (fCPUKernelTime - m_fOldCPUKernelTime + fCPUUserTime - m_fOldCPUUserTime)   
			*100.0);  
		m_fOldCPUIdleTime = fCPUIdleTime;  
		m_fOldCPUKernelTime = fCPUKernelTime;  
		m_fOldCPUUserTime = fCPUUserTime;  
	}  
	return nCPUUseRate;  
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    Initialize();
    char buf[12];
    while (true)  
    {     
	 Sleep(500);
	 wsprintf(buf, "%u", GetCPUUseRate());
	 OutputDebugString(buf);
    }  
    return 0;
}

评论

  1. 小学生
    博主
    Windows Firefox
    5 年前
    2020-1-30 14:08:19

    我得到的

  2. 小学生
    博主
    Windows Firefox
    5 年前
    2020-1-30 14:10:35

    xxs

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇