使用C#擷取WinCE系統中的記憶體狀態及系統狀態

來源:互聯網
上載者:User
一直想取得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();
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.