您的位置:首页 > 编程语言 > C#

C# 调用windows API

2012-01-19 09:27 155 查看
一、定义两个结构用来获取系统及内存信息信息:

//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());

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: