0

How can i get data regardless of OS? I'm using Qt Creator and C++

MEMORYSTATUSEX status;
    status.dwLength = sizeof(status);
    GlobalMemoryStatusEx(&status);
    unsigned long long installedRAM = status.ullTotalPhys;
    ui->label_21->setText(QString("Installed memory: %1 GB").arg(installedRAM / 1024.0 / 1024.0 / 1024.0, 0, 'f', 1));

So, I have this code, but as I know, it only works on Windows.

Mark Garai
  • 29
  • 7
  • 3
    You can't, but you can implement something for each OS that you care about, which ones would that be? – harold Jul 05 '23 at 13:41
  • well all kind of OS... But like on python I can do it right? Can I add python files to a c++ project too? – Mark Garai Jul 05 '23 at 13:44
  • 1
    ***Can I add python files to a c++ project too?*** Yes you can spawn a python interpreter from c++ or a Qt application pretty easily but then you have a python dependency for each OS you use. That will be easy for POSIX operating systems because they usually have python installed but windows does not. – drescherjm Jul 05 '23 at 13:45
  • 2
    You can cover a large range of OS with what is mentioned here [Getting memory information with Qt](https://stackoverflow.com/questions/8122277/getting-memory-information-with-qt) or a library like [hwinfo](https://github.com/lfreist/hwinfo) – t.niese Jul 05 '23 at 13:49
  • 2
    You can use Python, but that's really just exchanging a platform dependent thing in your code for a platform dependent thing in Python implementations (Python doesn't quite support "all kind of OS" either, many of them certainly, but you're still choosing a subset of platforms that you care about except you also lose control over what that subset is) – harold Jul 05 '23 at 13:52
  • We all want code that runs on "every" OS, but it will never happen. There are far too many operating systems to make that assertion. Do you mean Windows + macOS + Linux? Hopefully not QNX or Plan 9 or Xenix... – tadman Jul 05 '23 at 14:05
  • 1
    _well all kind of OS_ Would Windows, Linux and macOS be enough? If so, researching how to do it for each should not be hard. – Paul Sanders Jul 05 '23 at 14:05
  • But its a physical component, thats why i thought it. Like the CPU. You can read out its type from register. – Mark Garai Jul 05 '23 at 14:07
  • 1
    Many OSes prevent direct access to hardware due to security reasons (that's how access permissions work). Only drivers (which are running with the system's privileges) are allowed to do that. Regular, user-land software, needs to use API provided by the OS to query such information. – heap underrun Jul 05 '23 at 14:16

0 Answers0