You should take a look at the Win32_ComputerSystem class (WMI) and the TotalPhysicalMemory property. There are ways to access this information through .Net through the System.Management namespace for managed code (I use C #, so I have not tried using a visual studio to develop in C ++). You can also create a script to run WMI directly, and your C ++ program will call the script.
UPDATE: you can also look at the Win32_PhysicalMemory class (look at the Capacity property). This will facilitate inaccurate readings due to the use of BIOS using RAM, etc.
UPDATE 2:
I tried this in C # (3.5) and Windows XP (SP 2) and it works. I'm sure you can do something similar with the same WMI classes in C ++ (at least through Visual Studio). This is not a problem, therefore it is not a Vista problem or more. I'm not sure if this is exactly what you are looking for, but this code will return the total physical memory of the system (and not how much for free). Hope this is what you had in mind. In any case, here is an example of a code that defines each stick of RAM and displays some information about each of them (including capacity), and then the result below:
using System; using System.Collections.Generic; using System.Management; using System.Text; namespace WmiTest { public class RamCounter { private List<RamStick> _ramSticks; private int _totalRam; private StringBuilder _stringRepresentation; public RamCounter() { _ramSticks = new List<RamStick>(); _totalRam = 0; _stringRepresentation = new StringBuilder(); } public void GetRamSticks() { _ramSticks.Clear(); _totalRam = 0; ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory"); ManagementObjectCollection queryCollection = searcher.Get(); foreach (ManagementObject mo in queryCollection) { _ramSticks.Add( new RamStick(Convert.ToUInt64(mo.GetPropertyValue("Capacity")), mo.GetPropertyValue("DeviceLocator").ToString(), mo.GetPropertyValue("Description").ToString(), Convert.ToUInt32(mo.GetPropertyValue("FormFactor")), Convert.ToUInt32(mo.GetPropertyValue("Speed")))); } } public override string ToString() { _stringRepresentation.Capacity = 0; foreach (RamStick rs in _ramSticks) { _stringRepresentation.Append(rs.ToString()); _totalRam += rs.CapacityInMB; } _stringRepresentation.Append("Total RAM(MB): " + _totalRam); return _stringRepresentation.ToString(); } } public class RamStick { private UInt64 _capacity; private UInt32 _formFactor; public RamStick(UInt64 capacity, string location, string description, UInt32 formFactor, UInt32 speed) { _capacity = capacity; Location = location; Description = description; _formFactor = formFactor; Speed = speed; } public int CapacityInMB { get { return Convert.ToInt32(_capacity / (1024 * 1024)); } } public string Location { get; private set; } public string Description { get; private set; } public string GetFormFactor() { string formFactor = string.Empty; switch (_formFactor) { case 1: formFactor = "Other"; break; case 2: formFactor = "SIP"; break; case 3: formFactor = "DIP"; break; case 4: formFactor = "ZIP"; break; case 5: formFactor = "SOJ"; break; case 6: formFactor = "Proprietary"; break; case 7: formFactor = "SIMM"; break; case 8: formFactor = "DIMM"; break; case 9: formFactor = "TSOP"; break; case 10: formFactor = "PGA"; break; case 11: formFactor = "RIMM"; break; case 12: formFactor = "SODIMM"; break; case 13: formFactor = "SRIMM"; break; case 14: formFactor = "SMD"; break; case 15: formFactor = "SSMP"; break; case 16: formFactor = "QFP"; break; case 17: formFactor = "TQFP"; break; case 18: formFactor = "SOIC"; break; case 19: formFactor = "LCC"; break; case 20: formFactor = "PLCC"; break; case 21: formFactor = "BGA"; break; case 22: formFactor = "FPBGA"; break; case 23: formFactor = "LGA"; break; default: formFactor = "Unknown"; break; } return formFactor; } public UInt32 Speed { get; private set; } public override string ToString() { return string.Format("Description:{1}{0}" + "Location:{2}{0}" + "Form Factor:{3}{0}" + "Speed:{4}{0}" + "Capacity(MB):{5}{0}{0}", Environment.NewLine, Description, Location, GetFormFactor(), Speed, CapacityInMB); } } }
To use the code:
private void btnRam_Click(object sender, EventArgs e) { RamCounter rc = new RamCounter(); rc.GetRamSticks(); MessageBox.Show(rc.ToString()); }
Example output from my machine:
Description: Physical Memory Location: J6H1 Format Factor: DIMM Speed: 667 Capacity(MB): 1024 Description: Physical Memory Location: J6H2 Format Factor: DIMM Speed: 667 Capacity(MB): 1024 Description: Physical Memory Location: J6J1 Format Factor: DIMM Speed: 667 Capacity(MB): 1024 Total RAM(MB): 3072
Jason down
source share