C# 调用windows API

一、定义两个结构用来获取系统及内存信息信息:

    //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;
    }

二、引入Windows DLL

        //To get system information
        [DllImport("kernel32")]
        static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

        //To get Memory status
        [DllImport("kernel32")]
        static extern void GlobalMemoryStatus(ref MEMORYSTATUS buf);

三、   调用API 方法: GetSystemInfo()及GlobalMemoryStatus 为结构成员赋值并获取结构成员

                SYSTEM_INFO pSI = new SYSTEM_INFO();
                GetSystemInfo(ref pSI);
                string CPUType;
                switch (pSI.dwProcessorType)
                {

                    case 586:
                        CPUType = "Intel 386";
                        break;
                    default:
                        CPUType = "(unknown)";
                        break;
                }

                listBox1.Items.Insert(0, "Active Processor Mask :  " + pSI.dwActiveProcessorMask.ToString());
                listBox1.Items.Insert(1, "Allocation Granularity :  " + pSI.dwAllocationGranularity.ToString());
                listBox1.Items.Insert(2, "Number Of Processors :  " + pSI.dwNumberOfProcessors.ToString());
                listBox1.Items.Insert(3, "OEM ID :    " + pSI.dwOemId.ToString());
                listBox1.Items.Insert(4, "Page Size :   " + pSI.dwPageSize.ToString());
                // Processor Level (Req filtering to get level)
                listBox1.Items.Insert(5, "Processor Level Value :  " + pSI.dwProcessorLevel.ToString());
                listBox1.Items.Insert(6, "Processor Revision :  " + pSI.dwProcessorRevision.ToString());
                listBox1.Items.Insert(7, "CPU type :   " + CPUType);
                listBox1.Items.Insert(8, "Maximum Application Address : " + pSI.lpMaximumApplicationAddress.ToString());
                listBox1.Items.Insert(9, "Minimum Application Address : " + pSI.lpMinimumApplicationAddress.ToString());

                /************** To retrive info from GlobalMemoryStatus ****************/

                MEMORYSTATUS memSt = new MEMORYSTATUS();
                GlobalMemoryStatus(ref memSt);

                listBox1.Items.Insert(10, "Available Page File :  " + (memSt.dwAvailPageFile / 1024).ToString());
                listBox1.Items.Insert(11, "Available Physical Memory :  " + (memSt.dwAvailPhys / 1024).ToString());
                listBox1.Items.Insert(12, "Available Virtual Memory :  " + (memSt.dwAvailVirtual / 1024).ToString());
                listBox1.Items.Insert(13, "Size of structur :   " + memSt.dwLength.ToString());
                listBox1.Items.Insert(14, "Memory In Use :   " + memSt.dwMemoryLoad.ToString());
                listBox1.Items.Insert(15, "Total Page Size :   " + (memSt.dwTotalPageFile / 1024).ToString());
                listBox1.Items.Insert(16, "Total Physical Memory :  " + (memSt.dwTotalPhys / 1024).ToString());
                listBox1.Items.Insert(17, "Total Virtual Memory :  " + (memSt.dwTotalVirtual / 1024).ToString());

               

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值