C#操作API函数

 作为初学者来说,在C#中使用API确是一件令人头疼的问题。在使用API之前你必须知道如何在C#中使用结构、类型转换、安全/不安全代码,可控/不可控代码等许多知识。
一切从简单开始,复杂的大家一时不能接受。我们就从实现一个简单的MessageBox开始。首先打开VS.Net ,创建一个新的C#工程,并添加一个Button按钮。当这个按钮被点击,则显示一个MessageBox对话框。

即然我们需要引用外来库,所以必须导入一个Namespace:

using System.Runtime.InteropServices;

接着添加下面的代码来声明一个API:

[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);

此处DllImport属性被用来从不可控代码中调用一方法。”User32.dll”则设定了类库名。DllImport属性指定dll的位置,这个dll中包括调用的外部方法。Static修饰符则声明一个静态元素,而这个元素属于类型本身而不是上面指定的对象。extern则表示这个方法将在工程外部执行,使用DllImport导入的方法必须使用extern修饰符。
MessageBox 则是函数名,拥有4个参数,其返回值为数字。
大多数的API都能传递并返回值。
添中Click点击事件代码:

protected void button1_Click(object sender, System.EventArgs e)
{
MessageBox (0,"API Message Box","API Demo",0);
}

编译并运行这个程序,当你点击按钮后,你将会看到对话框,这便是你使用的API函数。
使用结构体
操作带有结构体的API比使用简单的API要复杂的多。但是一旦你掌握了API的过程,那个整个API世界将在你的掌握之中。
下面的例子中我们将使用GetSystemInfo API 来获取整个系统的信息。
第一步还是打开C#建立一个Form工程,同样的添中一个Button按钮,在代码窗中输入下面的代码,导入Namespace:

using System.Runtime.InteropServices;

声明一个结构体,它将作为GetSystemInfo的一个参数:

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

声明API函数:

[DllImport("kernel32")]
static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

添加下面的代码至按钮的点击事件处理中:
首先创建一个SYSTEM_INFO结构体,并将其传递给GetSystemInfo函数。

protected void button1_Click (object sender, System.EventArgs e)
{
try
{
SYSTEM_INFO pSI = new SYSTEM_INFO();
GetSystemInfo(ref pSI);
//
//
// 

//一旦你接收到返回的结构体,那么就可以以返回的参数来执行操作了。

e.g.listBox1.InsertItem (0,pSI.dwActiveProcessorMask.ToString());:
//
//
//
}
catch(Exception er)

{
MessageBox.Show (er.Message);
}
}

//Created By Ajit Mungale


//程序补充   苍马之子

新建两个Form窗体:Mainform和about,在Mainform窗体中添加MenuStrip,然后添加子菜单abouttoolmenustrip,另外添加一个ListBox和Button。

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 WINAPI

{

//Struct 收集系统信息

[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 收集内存情况

[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;

}





public partial class Form1 : Form

{

//获取系统信息

[DllImport("kernel32")]

static extern void GetSystemInfo(ref SYSTEM_INFO pSI);

//获取内存信息

[DllImport("kernel32")]

static extern void GlobalMemoryStatus(ref MEMORYSTATUS buf);

//处理器类型

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 Form1()

{

InitializeComponent();

}



private void aboutToolStripMenuItem_Click(object sender, EventArgs e)

{

Form abt = new about();

abt.ShowDialog();

}



private void btn_display_Click(object sender, EventArgs e)

{

try

{

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;

default:

CPUType = "(unknown)";

break;

}

listBox1.Items.Add("Active Processor Mask :" + pSI.dwActiveProcessorMask.ToString());

listBox1.Items.Add("Allocation Granularity :" + pSI.dwAllocationGranularity.ToString());

listBox1.Items.Add("Number Of Processors :" + pSI.dwNumberOfProcessors.ToString());

listBox1.Items.Add("OEM ID :" + pSI.dwOemId.ToString());

listBox1.Items.Add("Page Size:" + pSI.dwPageSize.ToString());

listBox1.Items.Add("Processor Level Value:" + pSI.dwProcessorLevel.ToString());

listBox1.Items.Add("Processor Revision:" + pSI.dwProcessorRevision.ToString());

listBox1.Items.Add("CPU type:" + CPUType);

listBox1.Items.Add("Maximum Application Address: " + pSI.lpMaximumApplicationAddress.ToString());

listBox1.Items.Add("Minimum Application Address:" + pSI.lpMinimumApplicationAddress.ToString());

/************** 从 GlobalMemoryStatus 获取返回值****************/

MEMORYSTATUS memSt = new MEMORYSTATUS();

GlobalMemoryStatus(ref memSt);

listBox1.Items.Add("Available Page File :" + (memSt.dwAvailPageFile/1024).ToString());

listBox1.Items.Add("Available Physical Memory : " + (memSt.dwAvailPhys/1024).ToString());

listBox1.Items.Add("Available Virtual Memory:" + (memSt.dwAvailVirtual/1024).ToString());

listBox1.Items.Add("Size of structur :" + memSt.dwLength.ToString());

listBox1.Items.Add("Memory In Use :" + memSt.dwMemoryLoad.ToString());

listBox1.Items.Add("Total Page Size :" + (memSt.dwTotalPageFile/1024).ToString());

listBox1.Items.Add("Total Physical Memory :" + (memSt.dwTotalPhys/1024).ToString());

listBox1.Items.Add("Total Virtual Memory :" + (memSt.dwTotalVirtual/1024).ToString());

}

catch (Exception er)

{

MessageBox.Show(er.Message);

}

}

}

}

转载: http://hi.baidu.com/glaivelee/blog/item/ccda97d3234d773b960a1642.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值