获取设备内存(Windows CE)

获取设备内存
若要获取设备的可用内存,请使用平台调用来调用本机 GlobalMemoryStatus 和 GetSystemMemoryDivision 函数。
此代码示例定义下面的内容:
1.Windows CE 设备中的平台调用声明。
2.一个要传递给设备方法并从设备方法接收的结构。
3.显示设备内存信息。

// 程序名: MemoryStatus.exe
// 文件名: MemoryStatus.cs
// 功能: 通过调用win32 函数和传递数据结构指针

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 MemoryStatus
{
    public class MemoryStatus
    {
        public const string CRLF = "/r/n";

        [DllImport("coredll.dll")]
        public static extern void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);

        public struct MEMORYSTATUS
        {
            public int dwLength;
            public int dwMemoryLoad;
            public int dwTotalPhys;
            public int dwAvailPhys;
            public int dwTotalPageFile;
            public int dwAvailPageFile;
            public int dwTotalVirtual;
            public int dwAvailVirtual;
        }

        public static void Main()
        {
            MEMORYSTATUS memoryStatus = new MEMORYSTATUS();
            memoryStatus.dwLength = Marshal.SizeOf(memoryStatus);
            GlobalMemoryStatus(ref memoryStatus);

            string appName = "WINCE 内存状态";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("Memory Load: ");
            stringBuilder.Append(memoryStatus.dwMemoryLoad.ToString() + "%");
            stringBuilder.Append(CRLF);
            stringBuilder.Append("Total phys: ");
            stringBuilder.Append(memoryStatus.dwTotalPhys.ToString("#,##0"));
            stringBuilder.Append(CRLF);
            stringBuilder.Append("Avail phys ");
            stringBuilder.Append(memoryStatus.dwAvailPhys.ToString("#,##0"));
            stringBuilder.Append(CRLF);
            stringBuilder.Append("Total Page = ");
            stringBuilder.Append(memoryStatus.dwTotalPageFile.ToString("#,##0"));
            stringBuilder.Append(CRLF);
            stringBuilder.Append("Avail Page = ");
            stringBuilder.Append(memoryStatus.dwAvailPageFile.ToString("#,##0"));
            stringBuilder.Append(CRLF);
            stringBuilder.Append("Total Virtual = ");
            stringBuilder.Append(memoryStatus.dwTotalVirtual.ToString("#,##0"));
            stringBuilder.Append(CRLF);
            stringBuilder.Append("Avail Virtual = ");
            stringBuilder.Append(memoryStatus.dwAvailVirtual.ToString("#,##0"));
            stringBuilder.Append(CRLF);
            MessageBox.Show(stringBuilder.ToString(), appName);
        }
    }
}

你可能感兴趣的:(获取设备内存(Windows CE))