PDA

View Full Version : Problem with #include "windows.h"



Momergil
28th June 2012, 20:21
I'm not sure if that would be a Qt programming question, but anyway.

I want to create a software in Qt that do some things regarding process in Windows (see http://www.qtcentre.org/threads/49644-How-to-command-other-process?p=222983). I'm using Windows API functions to do what I want, at least for the moment.

Now I'ld like to receive some data regarding the RAM memory consumption of a given process, what is taught how to do it here: http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process

In a first moment I tried this codes in Borland C++ Builder; it worked fine.

But when I ctr+c + ctrl+v the API code in Qt, I got some errors; essentially it seems that Qt is not recognizing the presence of the #include "windows.h", so it's not being able to recognize the variables the code needs to work.


Code:


#include "windows.h"

/.../
/.../

MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);

Errors:


15:20:27: Running build steps for project WD...
15:20:27: Configuration unchanged, skipping qmake step.
15:20:27: Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe"
C:/QtSDK/mingw/bin/mingw32-make.exe -f Makefile.Debug
mingw32-make.exe[1]: Entering directory `C:/Users/Endrigo/Desktop/Central350/trunk/central BD novo -v18.2.2/WD'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:\QtSDK\Desktop\Qt\4.8.0\mingw\include\QtCore" -I"c:\QtSDK\Desktop\Qt\4.8.0\mingw\include\QtGui" -I"c:\QtSDK\Desktop\Qt\4.8.0\mingw\include" -I"c:\QtSDK\Desktop\Qt\4.8.0\mingw\include\ActiveQt" -I"debug" -I"." -I"c:\QtSDK\Desktop\Qt\4.8.0\mingw\mkspecs\win32-g++" -o debug\main.o main.cpp
main.cpp: In function 'int qMain(int, char**)':
main.cpp:28: error: 'MEMORYSTATUSEX' was not declared in this scope
main.cpp:28: error: expected ';' before 'memInfo'
main.cpp:29: error: 'memInfo' was not declared in this scope
main.cpp:30: error: 'GlobalMemoryStatusEx' was not declared in this scope
main.cpp:39: error: 'ui' was not declared in this scope
mingw32-make.exe[1]: Leaving directory `C:/Users/Endrigo/Desktop/Central350/trunk/central BD novo -v18.2.2/WD'
mingw32-make.exe[1]: *** [debug/main.o] Error 1
mingw32-make.exe: *** [debug] Error 2
15:20:29: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project WD (target: Desktop)
When executing build step 'Make'


Thanks,


Momergil

mvuori
28th June 2012, 22:49
Could be that your compiler has been configured to support an old Windows version which didn't have MEMORYSTATUSEX. See
http://old.nabble.com/Compiling-a-C-function--Memory-check-td30060037.html

ChrisW67
29th June 2012, 01:57
From the MinGW windef.h:


#ifndef WINVER
#define WINVER 0x0400
/*
* If you need Win32 API features newer the Win95 and WinNT then you must
* define WINVER before including windows.h or any other method of including
* the windef.h header.
*/
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT WINVER

and from the header that defines MEMORYSTATUSEX:


#if (_WIN32_WINNT >= 0x0500)
typedef struct _MEMORYSTATUSEX {
...


So, in your code:


#define WINVER 0x0500
#include <windows.h>
...


Microsoft's headers work the same way, it is just that they always default you to the latest API version (in order to drive upgrades by making software backward incompatible by default).

Momergil
29th June 2012, 16:40
Thanks! The #define worked!


(I would have never imagined that answer >.<)