使用C#获取WinCE系统中的内存状态及系统状态
一直想取得WinCE5.0系统中的内存状态和系统状态,却苦于不得其法。使用Google一路疯寻乱找,终于找到两篇参考文章,写成下面的测试程序。实现步骤是,使用VS2005新建一个WinCE应用程序项目并添加一个Form. 然后在Form中拖入两个ListBox, 分别命名为listBox1, listBox2。再拖入两个Button,分别命名不btnGet, btnExit, 之后双击它们添加事件,全部代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinceMemoryManagementDemo
{
public partial class MainForm : Form
{
//Struct to retrive system info
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public uint dwOemId;
public uint dwPageSize;
public uint lpMinimumApplicationAddress;
public uint lpMaximumApplicationAddress;
public uint dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public uint dwProcessorLevel;
public uint dwProcessorRevision;
}
//struct to retrive memory status
[StructLayout(LayoutKind.Sequential)]
public struct MEMORYSTATUS
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
//To get system information
[DllImport("coredll")]
private static extern void GetSystemInfo(ref SYSTEM_INFO pSI);
//To get Memory status
[DllImport("coredll")]
private static extern void GlobalMemoryStatus(ref MEMORYSTATUS buf);
//Cnstants used for processor types
public const int PROCESSOR_INTEL_386 = 386;
public const int PROCESSOR_INTEL_486 = 486;
public const int PROCESSOR_INTEL_PENTIUM = 586;
public const int PROCESSOR_MIPS_R4000 = 4000;
public const int PROCESSOR_ALPHA_21064 = 21064;
public const int PROCESSOR_TELECHIPS_TCC79X = 2336;
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 获得信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGet_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox2.Items.Clear();
try
{
/**********************************************************************************/
MEMORYSTATUS memSt = new MEMORYSTATUS();
GlobalMemoryStatus(ref memSt);
listBox1.Items.Add("Available Page File: " + (memSt.dwAvailPageFile / 1024).ToString() + " Kb");
listBox1.Items.Add("Application Memory In Use: " + (memSt.dwTotalPhys / 1024 - memSt.dwAvailPhys / 1024).ToString() + " Kb");
listBox1.Items.Add("Available Physical Memory: " + (memSt.dwAvailPhys / 1024).ToString() + " Kb");
listBox1.Items.Add("Available Virtual Memory: " + (memSt.dwAvailVirtual / 1024).ToString() + " Kb");
listBox1.Items.Add("Size of Structure: " + memSt.dwLength.ToString());
listBox1.Items.Add("Memory In Use: " + memSt.dwMemoryLoad.ToString() + "%");
listBox1.Items.Add("Total Page Size: " + (memSt.dwTotalPageFile / 1024).ToString() + " Kb");
listBox1.Items.Add("Total Physical Memory: " + (memSt.dwTotalPhys / 1024).ToString() + " Kb");
listBox1.Items.Add("Total Virtual Memory: " + (memSt.dwTotalVirtual / 1024).ToString() + " Kb");
/**********************************************************************************/
SYSTEM_INFO pSI = new SYSTEM_INFO();
GetSystemInfo(ref pSI);
string CPUType;
switch (pSI.dwProcessorType)
{
case PROCESSOR_INTEL_386:
CPUType = "Intel 386";
break;
case PROCESSOR_INTEL_486:
CPUType = "Intel 486";
break;
case PROCESSOR_INTEL_PENTIUM:
CPUType = "Intel Pentium";
break;
case PROCESSOR_MIPS_R4000:
CPUType = "MIPS R4000";
break;
case PROCESSOR_ALPHA_21064:
CPUType = "DEC Alpha 21064";
break;
case PROCESSOR_TELECHIPS_TCC79X:
CPUType = "TELECHIPS-ARM926-TCC79X";
break;
default:
CPUType = "(Unknown)";
break;
}
listBox2.Items.Add("Active Processor Mask: " + pSI.dwActiveProcessorMask.ToString());
listBox2.Items.Add("Allocation Granularity: " + pSI.dwAllocationGranularity.ToString());
listBox2.Items.Add("Number Of Processors: " + pSI.dwNumberOfProcessors.ToString());
listBox2.Items.Add("OEM ID: " + pSI.dwOemId.ToString());
listBox2.Items.Add("Page Size: " + pSI.dwPageSize.ToString());
// Processor Level (Req filtering to get level)
listBox2.Items.Add("Processor Level Value: " + pSI.dwProcessorLevel.ToString());
listBox2.Items.Add("Processor Revision: " + pSI.dwProcessorRevision.ToString());
listBox2.Items.Add("CPU type: " + CPUType);
listBox2.Items.Add("Maximum Application Address: " + pSI.lpMaximumApplicationAddress.ToString());
listBox2.Items.Add("Minimum Application Address: " + pSI.lpMinimumApplicationAddress.ToString());
StatusBar.Text = pSI.dwProcessorType.ToString();
}
catch(Exception ex)
{
StatusBar.Text = ex.Message;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}