W-ZERO3用 メモリ容量取得クラス

W-ZERO3 プログラミングコンテスト用の部品を作り始める。
1月末まであと10日。実質、3日間。間に合うか?
複雑なことはできないので、とりあえず簡単にできるものを。
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
 
namespace Uchukamen.WZero3
{
    public class MemoryStatus
    {
        private class MEMORYSTATUS
        {
            public uint dwLength = 0;
            public uint dwMemoryLoad = 0;
            public uint dwTotalPhys = 0;
            public uint dwAvailPhys = 0;
            public uint dwTotalPageFile = 0;
            public uint dwAvailPageFile = 0;
            public uint dwTotalVirtual = 0;
            public uint dwAvailVirtual = 0;
        }
        [DllImport("CoreDll.dll")]
        private static extern void GlobalMemoryStatus
        (
        MEMORYSTATUS lpBuffer
        );
        [DllImport("CoreDll.dll")]
        public static extern int GetSystemMemoryDivision
        (
        ref uint lpdwStorePages,
        ref uint lpdwRamPages,
        ref uint lpdwPageSize
        );
        private float totalPhys = 0;
        public float TotalPhysicalMB
        {
            get { return totalPhys; }
        }
        private float availPhys = 0;
        public float AvailPhysicalMB
        {
            get { return availPhys; }
        }
        private float totalVirtual = 0;
        public float TotalVirtualMB
        {
            get { return totalVirtual; }
        }
        private float availVirtual = 0;
        public float AvailVirtualMB
        {
            get { return availVirtual; }
        }
        public void Get()
        {
            uint storePages = 0;
            uint ramPages = 0;
            uint pageSize = 0;
            int res = GetSystemMemoryDivision(ref storePages, ref ramPages, ref pageSize);
            MEMORYSTATUS memStatus = new MEMORYSTATUS();
            GlobalMemoryStatus(memStatus);
            totalPhys = memStatus.dwTotalPhys / 1024F / 1024F;
            availPhys = memStatus.dwAvailPhys / 1024F / 1024F;
            totalVirtual = memStatus.dwTotalVirtual / 1024F / 1024F;
            availVirtual = memStatus.dwAvailVirtual / 1024F / 1024F;
        }
    }
}
 
呼び出し
            MemoryStatus mstat = new MemoryStatus();
            mstat.Get();            textBox1.Text =
             "合計 物理   " + mstat.TotalPhysicalMB.ToString("n2") + " MB\r\n" +
             "空き 物理   " + mstat.AvailPhysicalMB.ToString("n2") + " MB\r\n" +
             "合計 仮想   " + mstat.TotalVirtualMB.ToString("n2") + " MB\r\n" +
             "空き 仮想   " + mstat.AvailVirtualMB.ToString("n2") + " MB\r\n";
 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です