?
Solved

Output CPU usage not working

Posted on 2005-05-01
18
Medium Priority
?
1,194 Views
Last Modified: 2008-01-09
Hi,

I have the following code (which I mostly found on this site):


#include <iostream>
#include <windows.h>

using namespace std;


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow )
{

    HKEY hkey;
    DWORD dwDataSize;
    DWORD dwType;
    DWORD dwCpuUsage;

    if(RegOpenKeyEx(HKEY_DYN_DATA,"PerfStats\\StartStat",0,KEY_ALL_ACCESS,&hkey) != ERROR_SUCCESS)
    {
      // error    
    }
    dwDataSize = sizeof(DWORD);
    RegQueryValueEx(hkey,"KERNEL\\CPUUsage",NULL,&dwType,
                        (LPBYTE)&dwCpuUsage,&dwDataSize );  

    RegCloseKey(hkey);

    cout << dwCpuUsage << "%" << endl;
}



I want it to output the current CPU usage, but the output that I get *every* time that I attempt to run it, is:

   2293728%


Why is this? What is wrong with it? If anyone can provide a very simple solution to output the current CPU usage, then I would be very greatful!! :-)

Thanks in advance,
Rob.
0
Comment
Question by:InteractiveMind
  • 7
  • 6
  • 4
  • +1
18 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13904789
Are you sure CPU usage is DWORD type?
Have a look to this example:
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_10034055.html
0
 
LVL 32

Expert Comment

by:r-k
ID: 13904819
I think the main reason is that the way you are doing it is not supported in Windows NT/2K/XP. HKEY_DYN_DATA is only available in Win/9x
0
 
LVL 32

Expert Comment

by:r-k
ID: 13904827
>>>
   if(RegOpenKeyEx(HKEY_DYN_DATA,"PerfStats\\StartStat",0,KEY_ALL_ACCESS,&hkey) != ERROR_SUCCESS)
    {
      // error    
    }
<<<
 
Are you actually checking for the error code returned by RegOpenKeyEx. It should return an error if you are using Win?NT or later.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Accepted Solution

by:
r-k earned 1200 total points
ID: 13904837
If you do want to do this on an NT based system, see:

 http://www.codeproject.com/system/cpuusage.asp

0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13904882
Ah, the code I provided only works on Win 9x? Shame.

Jaime, the code in the link you provided doesn't seem to be working either, the output I'm getting, (when I append this line to the end of the code):

   cout << cpuPerCent << "%" << endl;

is:

   4292967295%


How can I get this to work on WinXP?

Thank you.
0
 
LVL 32

Expert Comment

by:r-k
ID: 13904890
>>>How can I get this to work on WinXP?

You can't. See the link I provided earlier:

http://www.codeproject.com/system/cpuusage.asp

for an example of how to do it in NT or later.

BTW, the reason you're getting that large number is because did not initialize dwCpuUsage in your program. If you had used:

 DWORD dwCpuUsage=0;

you would getting 0% as the result.
 
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 800 total points
ID: 13904908
You'd use performance counters - see e.g. http://www.codeproject.com/system/cpuusage.asp ("How to get CPU usage by performance counters (without PDH)") or alternatively http://www.codeproject.com/threads/Get_CPU_Usage.asp ("Get CPU usage with GetSystemTimes ")
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13904909
Thanks r-k,

I'm having a little bit of difficulty compiling... I downloaded the Source Project, I've loaded the Main.cpp file into my IDE (Dev-C++); I attempt to compile it, and I'm getting 9 errors; here's my Compile Log:

Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Documents and Settings\rob\Desktop\CpuUsage_src\Main.cpp" -o "C:\Documents and Settings\rob\Desktop\CpuUsage_src\Main.exe"    -I"C:\Program Files\Dev-Cpp\include\c++\3.3.1"  -I"C:\Program Files\Dev-Cpp\include\c++\3.3.1\mingw32"  -I"C:\Program Files\Dev-Cpp\include\c++\3.3.1\backward"  -I"C:\Program Files\Dev-Cpp\lib\gcc-lib\mingw32\3.3.1\include"  -I"C:\Program Files\Dev-Cpp\include"  -I"C:\Program Files\Java\jdk1.5.0_01\include"  -I"C:\Program Files\Java\jdk1.5.0_01\include\win32"   -L"C:\Program Files\Dev-Cpp\lib" -L"C:\Program Files\Java\jdk1.5.0_01\include" -L"C:\Program Files\Java\jdk1.5.0_01\include\win32"
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x13e):Main.cpp: undefined reference to `CCpuUsage::CCpuUsage()'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x153):Main.cpp: undefined reference to `CCpuUsage::CCpuUsage()'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x16b):Main.cpp: undefined reference to `CCpuUsage::CCpuUsage()'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x198):Main.cpp: undefined reference to `CCpuUsage::GetCpuUsage()'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x1b1):Main.cpp: undefined reference to `CCpuUsage::GetCpuUsage(char const*)'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x1cc):Main.cpp: undefined reference to `CCpuUsage::GetCpuUsage(unsigned long)'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x263):Main.cpp: undefined reference to `CCpuUsage::~CCpuUsage()'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x290):Main.cpp: undefined reference to `CCpuUsage::~CCpuUsage()'
C:\DOCUME~1\rob\LOCALS~1\Temp/cc2Faaaa.o(.text+0x2bd):Main.cpp: undefined reference to `CCpuUsage::~CCpuUsage()'

Execution terminated


..I don't understand why this is happening.. any ideas?

Thank you.
0
 
LVL 32

Expert Comment

by:r-k
ID: 13904922
I am not at all familiar with that IDE, but as guess, did you remember to include the other source file in that project, CpuUsage.cpp, also in your project?
0
 
LVL 86

Expert Comment

by:jkr
ID: 13904927
You'll need to add the files from the sample to your project,
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13904940
Okay, thanks.. I've got passed that problem, now it's moaning that it cannot find the header file "atlbase.h" (which I can't find either..); which is referenced in CpuUsage.cpp

What's atlbase.h? :o\
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13904993
..It also cannot find "comdef.h"..

What are these classes?

Cheers.
0
 
LVL 32

Expert Comment

by:r-k
ID: 13905044
>>>
Okay, thanks.. I've got passed that problem, now it's moaning that it cannot find the header file "atlbase.h" (which I can't find either..); which is referenced in CpuUsage.cpp
<<<

I am not terribly familiar with that sample code, but looking at the comments, it would seem that he's using the CRegKey class from there. You could probably replace that by calling the API functions such as RegOpenKeyEx(..) etc. directly, in which case you can just include <windows.h> instead of <atlbase.h>, but it will take some knowledge of C and the Windows API, though not much.

FYI: I am offline for the next several hours. Someone else more knowledgeable may be able to help better in case you have questions. Good luck.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13905097
Hm, DevC++ probably does not have the ATL headers - have you tried the alternative solution at http://www.codeproject.com/threads/Get_CPU_Usage.asp ("Get CPU usage with GetSystemTimes")?
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13905318
> Hm, DevC++ probably does not have the ATL headers.
Shame.

> Hm, DevC++ probably does not have the ATL headers - have you tried the alternative solution at ...
I'm getting compilation problems even with this! :o\

Damn, I don't get why this won't work for me! :-(

Is there really *no* simpler way to achieve this??


Cheers.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13905484
What errors are you getting there?
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13905662
Sorry about the delay..

Before I post any error messages, I'm just going to quickly explain exactly what I've done so far:

 (1)  Download GetCPU.zip   (from the link you provided).
 (2)  Unzip content.
 (3)  Open Dev-C++ 4.9.9.1
 (4)  Create a new C++ Project (entitled "CPU").
 (5)  Add all 8 source files, from within the GetCPU directory, to the new project.
 (6)  Ensure that all source files have a new line at the end of the files.
 (7)  Open the file "GetCPU.cpp".
 (8)  Hit "Compile".

And here's my compile log:

Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\rob\Desktop\Makefile.win"
Executing  make...
make.exe -f "C:\Documents and Settings\rob\Desktop\Makefile.win" all
g++.exe -c GetCPU/GetCPU/GetCPU/CPU.cpp -o GetCPU/GetCPU/GetCPU/CPU.o -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1"  -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/mingw32"  -I"C:/Program Files/Dev-Cpp/include/c++/3.3.1/backward"  -I"C:/Program Files/Dev-Cpp/lib/gcc-lib/mingw32/3.3.1/include"  -I"C:/Program Files/Dev-Cpp/include"  -I"C:/Program Files/Java/jdk1.5.0_01/include"  -I"C:/Program Files/Java/jdk1.5.0_01/include/win32"  

In file included from GetCPU/GetCPU/GetCPU/CPU.cpp:1:
GetCPU/GetCPU/GetCPU/StdAfx.h:4:9: warning: #pragma once is obsolete
GetCPU/GetCPU/GetCPU/StdAfx.h:12:20: crtdbg.h: No such file or directory
In file included from GetCPU/GetCPU/GetCPU/CPU.cpp:2:
GetCPU/GetCPU/GetCPU/cpu.h:1:9: warning: #pragma once is obsolete
In file included from GetCPU/GetCPU/GetCPU/cpu.h:3,
                 from GetCPU/GetCPU/GetCPU/CPU.cpp:2:
GetCPU/GetCPU/GetCPU/TKTime.h:2:9: warning: #pragma once is obsolete
In file included from GetCPU/GetCPU/GetCPU/TKTime.h:4,
                 from GetCPU/GetCPU/GetCPU/cpu.h:3,

                 from GetCPU/GetCPU/GetCPU/CPU.cpp:2:
GetCPU/GetCPU/GetCPU/TKTypes.h:2:9: warning: #pragma once is obsolete
In file included from GetCPU/GetCPU/GetCPU/cpu.h:3,
                 from GetCPU/GetCPU/GetCPU/CPU.cpp:2:
GetCPU/GetCPU/GetCPU/TKTime.h:7: error: integer constant is too large for "long
   " type

GetCPU/GetCPU/GetCPU/TKTime.h:8: error: integer constant is too large for "long
   " type
GetCPU/GetCPU/GetCPU/TKTime.h:16: error: integer constant is too large for "
   long" type
GetCPU/GetCPU/GetCPU/TKTime.h:18: error: integer constant is too large for "
   long" type
GetCPU/GetCPU/GetCPU/TKTime.h:20: error: integer constant is too large for "
   long" type

In file included from GetCPU/GetCPU/GetCPU/cpu.h:3,
                 from GetCPU/GetCPU/GetCPU/CPU.cpp:2:
GetCPU/GetCPU/GetCPU/TKTime.h:251:11: invalid suffix "fh" on integer constant
GetCPU/GetCPU/GetCPU/TKTime.h:251:11: invalid suffix "h" on integer constant
GetCPU/GetCPU/GetCPU/TKTime.h: In static member function `static LARGE_INTEGER
   TKCPUDelay::_GetTicks()':
GetCPU/GetCPU/GetCPU/TKTime.h:250: error: syntax error before `;' token
GetCPU/GetCPU/GetCPU/TKTime.h: At global scope:
GetCPU/GetCPU/GetCPU/TKTime.h:255: error: parse error at end of saved function
   text

In file included from GetCPU/GetCPU/GetCPU/CPU.cpp:4:
GetCPU/GetCPU/GetCPU/SmartLock.h:1:9: warning: #pragma once is obsolete

make.exe: *** [GetCPU/GetCPU/GetCPU/CPU.o] Error 1

Execution terminated


It seems to me, that Dev-C++ doesn't support something that it's doing/using ... ?

Thank you.
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13960261
Any ideas?  :o\
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question