C++/CLI How to Display Ram Info in GB

Wami007 296 Reputation points
2021-02-15T16:13:14.55+00:00

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
ManagementObjectSearcher searcher("select * from Win32_Processor");
ManagementObjectSearcher searcher1("select * from Win32_VideoController");
ManagementObjectSearcher searcher2("select * from Win32_TotalVisibleMemory");
for each (ManagementObject ^ d in searcher.Get())
{
lb1->Text = d["Name"]->ToString();
break;
}
for each (ManagementObject ^ d in searcher1.Get())
{
lb2->Text = d["Name"]->ToString();
break;
}
for each (ManagementObject ^ d in searcher2.Get())
{
lb3->Text = d["Capacity"]->ToString();
break;
}
}

Hello...

This Code Show to me ram size in bytes but i need make it like "8 GB"

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,843 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,690 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,152 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wami007 296 Reputation points
    2021-02-15T19:40:01.567+00:00

    Answer Is It, Thanks ^_^

    ManagementObjectSearcher searcher2("select * from Win32_OperatingSystem");

    for each (ManagementObject ^ d in searcher2.Get())
    {
    unsigned long long size = System::Convert::ToUInt32(d["TotalVisibleMemorySize"]);

    if (size >= 1'000'000)
    {
    lb3->Text = (size / 1'000'000) + " GB";
    }
    else
    {
    lb3->Text = size + " bytes";
    }
    }

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Viorel 116.6K Reputation points
    2021-02-15T16:49:35.203+00:00

    Check if this works:

    unsigned long long size = System::Convert::ToUInt64( d["Capacity"] );
    
    if( size >= 1'000'000'000 )
    {
       lb3->Text = ( size / 1'000'000'000 ) + " GB";
    }
    else
    {
       lb3->Text = size + " bytes";
    }
    

  2. Castorix31 84,471 Reputation points
    2021-02-15T16:57:15.707+00:00

    You can use StrFormatByteSize which rounds the bytes and formats the string
    (#include <shlwapi.h>
    (#pragma comment(lib, "Shlwapi") )

    Test :

    String^ sMemory = d["Capacity"]->ToString();  
    marshal_context context;  
    LPCTSTR pwsMemory = context.marshal_as<const TCHAR*>(sMemory);  
    TCHAR wsMemory[32] = TEXT("");  
    StrFormatByteSize(_wtof(pwsMemory), wsMemory, 32);  
    String^ sMemoryNew = gcnew System::String(wsMemory);  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.