Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 answers

573 users

How to get the total physical memory RAM in C++ Win32 API

1 Answer

0 votes
#include <Windows.h> 
#include <sstream>

// Use to convert bytes to KB
#define KB 1024

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    /*
        typedef struct _MEMORYSTATUSEX {
            DWORD dwLength;
            DWORD dwMemoryLoad;
            DWORDLONG ullTotalPhys;
            DWORDLONG ullAvailPhys;
            DWORDLONG ullTotalPageFile;
            DWORDLONG ullAvailPageFile;
            DWORDLONG ullTotalVirtual;
            DWORDLONG ullAvailVirtual;
            DWORDLONG ullAvailExtendedVirtual;
            } MEMORYSTATUSEX, *LPMEMORYSTATUSEX;
    */


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

    // Retrieves information about the system's current physical and virtual memory usage
    GlobalMemoryStatusEx(&memInfo);
    DWORDLONG totalPhysicalMemory_RAM = memInfo.ullTotalPhys / (KB * KB * KB);

    std::ostringstream oss;

    oss << totalPhysicalMemory_RAM << " GB";

    std::string s = oss.str();

    MessageBoxA(0, s.c_str(), "Total Virtual Memory", MB_OK);

    return 0;
}



/*
run:

31 GB

*/

 



answered Dec 12, 2024 by avibootz
...