我需要记录用户有多少RAM的信息。我的第一种方法是使用GlobalMemoryStatusEx,但这只能给我窗口可用的内存量,而不是安装量。我发现这个函数GetPhysicallyInstalledSystemMemory但它只有Vista和更高版本。我需要这个在XP上工作。是否有一种相当简单的方法可以查询GetPhysicallyInstalledSystemMemory正在使用的SMBIOS信息,或者是否存在我可以找到的注册表值。
答案 0 :(得分:4)
您应该查看Win32_ComputerSystem类(WMI)和TotalPhysicalMemory属性。有很多方法可以通过。System.Management命名空间访问托管代码来访问这些信息(我使用C#,所以我自己没有尝试过使用visual studio进行c ++开发)。您还可以创建一个脚本来直接运行WMI并让您的c ++程序调用该脚本。
更新:您还可以查看Win32_PhysicalMemory类(请查看Capacity属性)。这将缓解由于BIOS使用某些RAM等导致的不准确读数。
更新2:
我在C#(3.5)和Windows XP(SP 2)中尝试过这种方法,但它确实有用。我相信你可以用C ++中的相同WMI类做类似的事情(至少通过Visual Studio)。它没有问题,所以它不是Vista或更大的问题。我不确定这是否正是您正在寻找的,但此代码将返回系统的总物理内存容量(而不是多少是免费的)。希望这就是你的意思。无论如何,这里有一些示例代码,它们可以找到每个RAM的内容,并显示每个内容的一些信息(包括容量),然后显示底部的总数:
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);
}
}
}
使用代码:
private void btnRam_Click(object sender, EventArgs e)
{
RamCounter rc = new RamCounter();
rc.GetRamSticks();
MessageBox.Show(rc.ToString());
}
我机器的样品输出:
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
答案 1 :(得分:2)
我认为WMI可能会向您提供此信息......
http://briandesmond.com/blog/getting-total-installed-memory-with-wmi/
答案 2 :(得分:2)
我不相信Vista之前的Windows版本会跟踪此信息 - 您必须执行一些系统特定的BIOS或主板枚举才能在Vista之前找到真正的价值。最好的办法是调用新的API GetPhysicallyInstalledSystemMemory
,并为XP系统故障转移到GlobalMemoryStatusEx
。
答案 3 :(得分:1)
GlobalMemoryStatusEx返回的值之一是ullTotalPhys,这似乎是您正在寻找的。 p>
ram用于视频内存的东西不在那里,但我怀疑是否有办法实现这一点。