{"id":447,"date":"2017-02-07T21:55:33","date_gmt":"2017-02-07T13:55:33","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=447"},"modified":"2023-08-07T20:57:30","modified_gmt":"2023-08-07T12:57:30","slug":"c-article33","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/07\/c-article33\/","title":{"rendered":"C++\u8c03\u7528windows.h\u663e\u793a\u7535\u8111\u5185\u5b58CPU"},"content":{"rendered":"<h2>\u5206\u4eab\u4e00\u6bb5c++\u8c03\u7528windows.h\uff0c\u663e\u793a\u5f53\u524d\u7cfb\u7edf\u5185\u5b58\u4f7f\u7528\u7387\uff0c\u5269\u4f59\u5185\u5b58\uff0c\u52a8\u6001\u663e\u793acpu\u4f7f\u7528\u7387\u7684\u4ee3\u7801\uff0c\u5728c++\u7f16\u8bd1\u5668\u4e2d\u7f16\u8bd1\u597d\u4ee5\u540e\u76f4\u63a5\u590d\u5236.exe\u4f7f\u7528\u5373\u53ef\u3002\u6548\u679c\u5982\u4e0b\u56fe\u3002<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/photo\/c.png\" alt=\"\" \/><\/p>\n<p>```c++<br \/>\n#include &quot;iostream&quot;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;conio.h&gt;<br \/>\n#include &lt;windows.h&gt;<br \/>\nusing namespace std;<br \/>\nconst int KB_DIV = 1024;<br \/>\nconst int MB_DIV = 1024 * 1024;<br \/>\nclass CCPUUseRate&lt;br \/&gt;<br \/>\n{&lt;br \/&gt;<br \/>\npublic:&lt;br \/&gt;<br \/>\n    BOOL Initialize()&lt;br \/&gt;<br \/>\n    {&lt;br \/&gt;<br \/>\n        FILETIME ftIdle, ftKernel, ftUser;&lt;br \/&gt;<br \/>\n        BOOL flag = FALSE;&lt;br \/&gt;<br \/>\n        if (flag = GetSystemTimes(&amp;ftIdle, &amp;ftKernel, &amp;ftUser))&lt;br \/&gt;<br \/>\n        {&lt;br \/&gt;<br \/>\n            m_fOldCPUIdleTime = FileTimeToDouble(ftIdle);&lt;br \/&gt;<br \/>\n            m_fOldCPUKernelTime = FileTimeToDouble(ftKernel);&lt;br \/&gt;<br \/>\n            m_fOldCPUUserTime = FileTimeToDouble(ftUser);<\/p>\n<p>&lt;pre&gt;&lt;code&gt;    }<br \/>\n    return flag;<br \/>\n}<br \/>\n\/\/\u8c03\u7528Initialize\u540e\u8981\u7b49\u5f851\u79d2\u518d\u8c03\u7528\u6b64\u51fd\u6570<br \/>\nint GetCPUUseRate()<br \/>\n{<br \/>\n    int nCPUUseRate = -1;<br \/>\n    FILETIME ftIdle, ftKernel, ftUser;<br \/>\n    if (GetSystemTimes(&amp;ftIdle, &amp;ftKernel, &amp;ftUser))<br \/>\n    {<br \/>\n        double fCPUIdleTime = FileTimeToDouble(ftIdle);<br \/>\n        double fCPUKernelTime = FileTimeToDouble(ftKernel);<br \/>\n        double fCPUUserTime = FileTimeToDouble(ftUser);<br \/>\n        nCPUUseRate= (int)(100.0 - (fCPUIdleTime - m_fOldCPUIdleTime)<br \/>\n            \/ (fCPUKernelTime - m_fOldCPUKernelTime + fCPUUserTime - m_fOldCPUUserTime)<br \/>\n            *100.0);<br \/>\n        m_fOldCPUIdleTime = fCPUIdleTime;<br \/>\n        m_fOldCPUKernelTime = fCPUKernelTime;<br \/>\n        m_fOldCPUUserTime = fCPUUserTime;<br \/>\n    }<br \/>\n    return nCPUUseRate;<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>private:&lt;br \/&gt;<br \/>\n    double FileTimeToDouble(FILETIME &amp;filetime)&lt;br \/&gt;<br \/>\n    {&lt;br \/&gt;<br \/>\n        return (double)(filetime.dwHighDateTime * 4.294967296E9) + (double)filetime.dwLowDateTime;&lt;br \/&gt;<br \/>\n    }&lt;br \/&gt;<br \/>\nprivate:&lt;br \/&gt;<br \/>\n    double m_fOldCPUIdleTime;&lt;br \/&gt;<br \/>\n    double m_fOldCPUKernelTime;&lt;br \/&gt;<br \/>\n    double m_fOldCPUUserTime;&lt;br \/&gt;<br \/>\n};&lt;br \/&gt;<br \/>\nint main(){<br \/>\n    MEMORYSTATUS memStatus;<br \/>\n    GlobalMemoryStatus(&amp;memStatus);<br \/>\n    cout&lt;&lt;&quot;**************\u7cfb\u7edf\u8d44\u6e90\u663e\u793a**************&quot;&lt;&lt;endl&lt;&lt;endl;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;        printf(&quot;*******\u5f53\u524d\u5185\u5b58\u4f7f\u7528\u7387\uff1a%d%%\\n&quot;,memStatus.dwMemoryLoad);<br \/>\nprintf(&quot;*******\u5f53\u524d\u53ef\u7528\u7269\u7406\u5185\u5b58\uff1a%dKB(%dMB)\\n&quot;,memStatus.dwAvailPhys \/ KB_DIV,memStatus.dwAvailPhys \/ MB_DIV);<br \/>\nCCPUUseRate cpuUseRate;<br \/>\nif (!cpuUseRate.Initialize())<br \/>\n{<br \/>\n    printf(&quot;Error! %d\\n&quot;, GetLastError());<br \/>\n    getch();<br \/>\n    return -1;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n    while (true)<br \/>\n    {<br \/>\n        Sleep(1000);<br \/>\n        printf(&quot;\\r\u5f53\u524dCPU\u4f7f\u7528\u7387\u4e3a\uff1a%4d%%&quot;, cpuUseRate.GetCPUUseRate());<br \/>\n    }<br \/>\n}  <\/p>\n<p>while(true){<br \/>\n    Sleep(1000);<\/p>\n<p>}<br \/>\nint a;cin&gt;&gt;a;<br \/>\nreturn 0;<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>}<br \/>\n```<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u4e00\u6bb5c++\u8c03\u7528windows.h\uff0c\u663e\u793a\u5f53\u524d\u7cfb\u7edf\u5185\u5b58\u4f7f\u7528\u7387\uff0c\u5269\u4f59\u5185\u5b58\uff0c\u52a8\u6001\u663e\u793acpu\u4f7f\u7528\u7387\u7684\u4ee3\u7801\uff0c\u5728c++\u7f16\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/07\/c-article33\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":448,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[29],"tags":[39],"class_list":["post-447","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-cplus","tag-cplus","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=447"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/447\/revisions"}],"predecessor-version":[{"id":1507,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/447\/revisions\/1507"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/448"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}