Output CPU usage not working

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.
LVL 25
InteractiveMindAsked:
Who is Participating?
 
r-kCommented:
If you do want to do this on an NT based system, see:

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

0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
r-kCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
r-kCommented:
>>>
   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
 
InteractiveMindAuthor Commented:
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
 
r-kCommented:
>>>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
 
jkrCommented:
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
 
InteractiveMindAuthor Commented:
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
 
r-kCommented:
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
 
jkrCommented:
You'll need to add the files from the sample to your project,
0
 
InteractiveMindAuthor Commented:
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
 
InteractiveMindAuthor Commented:
..It also cannot find "comdef.h"..

What are these classes?

Cheers.
0
 
r-kCommented:
>>>
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
 
jkrCommented:
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
 
InteractiveMindAuthor Commented:
> 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
 
jkrCommented:
What errors are you getting there?
0
 
InteractiveMindAuthor Commented:
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
 
InteractiveMindAuthor Commented:
Any ideas?  :o\
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.