0

I am looking for a way to retrieve the RAM memory size of my computer in C++ with Qt.

My primary target is Windows, if that would make things easier.

Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
chani
  • 143
  • 1
  • 14
  • 3
    According to [this](http://stackoverflow.com/questions/8122277/getting-memory-information-with-qt), you'll have to use something like [this](http://msdn.microsoft.com/en-us/library/windows/desktop/cc300158(v=vs.85).aspx) or the one mentioned there, depending on your needs. – ghostofstandardspast Jun 16 '14 at 15:41
  • "Programatically"... It might also be problematic but that's a different story :) – Dennis Jun 16 '14 at 15:46
  • Thank you @ghostofstandardspast. I tried this code: MEMORYSTATUSEX memory_status; QString size; ZeroMemory(&memory_status, sizeof(MEMORYSTATUSEX)); memory_status.dwLength = sizeof(MEMORYSTATUSEX); if (GlobalMemoryStatusEx(&memory_status)) { size =memory_status.ullTotalPhys / (1024 * 1024); } But I got this:"\231" in the size... do you know why? – chani Jun 16 '14 at 15:51
  • 1
    @chani, [You can't just assign an integer to a `QString` and expect the integer to be converted to a string](http://qt-project.org/doc/qt-4.8/qstring.html#operator-eq-6). – ghostofstandardspast Jun 16 '14 at 16:00
  • OK. so now I got 3993 (if i divided it by 1024 - I got 3.8994... and after convert it to int: I got 3.). But my memory is 4 GB... – chani Jun 16 '14 at 16:08
  • @AdrianMcCarthy, I tried the code that in the link you gave, but is gave to me the same answer as before (3.8994... MB). do you think that I have to round the number up? In c# code (when using the Win32_PhysicalMemory class) I got the correct number-4. any idea? – chani Jun 16 '14 at 18:20
  • 2
    For various reasons, Windows is sometimes not able to use 100% of your physical RAM. There are several ways you can handle this: (1) Just accept it, and display the slightly-less-than-4GB value. (2) Round up to the nearest GB. (3) Query the actual RAM sticks installed and add up their capacity (e.g. using DMI queries). This may be a privileged operation unavailable to processes not running as an administrator. – nobody Jun 16 '14 at 19:03
  • I did it. Thanks to all of you!! – chani Jun 17 '14 at 05:13

0 Answers0