visual-c++wmivariantuint64

How do I access a uint64 in a VARIANT structure?


How do I access a uint64 in a VARIANT structure?

Eg:

VARIANT vtProp{};
hres = pclsObj->Get(L"FreeSpaceInPagingFiles", 0, &vtProp, 0, 0);
strSystemInfo.AppendFormat(L"  Free Space In Paging Files: %d\r\n", vtProp.iVal);

These are properties from the Win32_ComputerSystem class.


I don't understand why I get:

Total Virtual Memory Size: 1860884006920
Total Visible Memory Size: 1860884006920

with this code:

hres = pclsObj->Get(L"TotalVirtualMemorySize", 0, &vtProp, 0, 0);
strSystemInfo.AppendFormat(L"  Total Virtual Memory Size: %lld\r\n", vtProp.ullVal);
VariantClear(&vtProp);

hres = pclsObj->Get(L"TotalVisibleMemorySize", 0, &vtProp, 0, 0);
strSystemInfo.AppendFormat(L"  Total Visible Memory Size: %lld\r\n", vtProp.ullVal);
VariantClear(&vtProp);

But PowerShell:

Get-WmiObject -Class "Win32_operatingsystem" | Format-List *

Reports:

TotalVirtualMemorySize                    : 35567932
TotalVisibleMemorySize                    : 33470780

What am I doing wrong?

For example, for Win32_ComputerSystem.TotalPhysicalMemory the docs say:

TotalPhysicalMemory

Data type: uint64

Access type: Read-only

Qualifiers: MappingStrings ("Win32API|Memory Management Structures|MEMORYSTATUS|dwTotalPhys"), Units ("bytes")

Total size of physical memory. Be aware that, under some circumstances, this property may not return an accurate value for the physical memory. For example, it is not accurate if the BIOS is using some of the physical memory. For an accurate value, use the Capacity property in Win32_PhysicalMemory instead.

Example: 67108864

For more information about using uint64 values in scripts, see Scripting in WMI.

To be honest, it doesn't matter which memory value properties I use as I struggle to interpret the VARIANT object data correctly.


Solution

  • I have part of the answer:

    enter image description here

    The value is correct in bstrVal.

    So ,taking the Capacity, I get:

    Total Physical Memory: 17179869184
    Total Physical Memory: 17179869184
    

    The values match PowerShell:

    Capacity             : 17179869184
    

    I have 2 banks, so the total is 34359738368. And this is similar to Win32_ComputerSystem.TotalPhysicalMemory -> Installed RAM: 34274078720.

    The short answer is that I can use bstrVal to get the actual value that PowerShell displays. But how I turn that bstrVal into a suitable GB value is another story.


    For the latter query:

    const double totalMemoryBytes = _tstof(CString(vtProp.bstrVal));
    const double totalMemoryKilobytes = totalMemoryBytes / 1024.0;
    const double totalMemoryMegabytes = totalMemoryKilobytes / 1024.0;
    const double totalMemoryGigabytes = totalMemoryMegabytes / 1024.0;