作为初学者来说,在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
//程序补充 飞刀
namespace usingapi
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.winforms;
using system.data;
using system.runtime.interopservices;
//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 class form1 : system.winforms.form
{
private system.componentmodel.container components;
private system.winforms.menuitem menuabout;
private system.winforms.mainmenu mainmenu1;
private system.winforms.listbox listbox1;
private system.winforms.button button1;
//获取系统信息
[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();
}
public override void dispose()
{
base.dispose();
components.dispose();
}
private void initializecomponent()
{
this.components = new system.componentmodel.container ();
this.mainmenu1 = new system.winforms.mainmenu ();
this.button1 = new system.winforms.button ();
this.listbox1 = new system.winforms.listbox ();
this.menuabout = new system.winforms.menuitem ();
mainmenu1.menuitems.all = new system.winforms.menuitem[1] {this.menuabout};
button1.location = new system.drawing.point (148, 168);
button1.size = new system.drawing.size (112, 32);
button1.tabindex = 0;
button1.text = "&get info";
button1.click += new system.eventhandler (this.button1_click);
listbox1.location = new system.drawing.point (20, 8);
listbox1.size = new system.drawing.size (368, 147);
listbox1.tabindex = 1;
menuabout.text = "&about";
menuabout.index = 0;
menuabout.click += new system.eventhandler (this.menuabout_click);
this.text = "system information - using api";
this.maximizebox = false;
this.autoscalebasesize = new system.drawing.size (5, 13);
this.minimizebox = false;
this.menu = this.mainmenu1;
this.clientsize = new system.drawing.size (408, 213);
this.controls.add (this.listbox1);
this.controls.add (this.button1);
}
protected void menuabout_click (object sender, system.eventargs e)
{
form abt=new about() ;
abt.showdialog();
}
protected void button1_click (object sender, system.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)";
}
listbox1.insertitem (0,"active processor mask :"+psi.dwactiveprocessormask.tostring());
listbox1.insertitem (1,"allocation granularity :"+psi.dwallocationgranularity.tostring());
listbox1.insertitem (2,"number of processors :"+psi.dwnumberofprocessors.tostring());
listbox1.insertitem (3,"oem id :"+psi.dwoemid.tostring());
listbox1.insertitem (4,"page size:"+psi.dwpagesize.tostring());
listbox1.insertitem (5,"processor level value:"+psi.dwprocessorlevel.tostring());
listbox1.insertitem (6,"processor revision:"+ psi.dwprocessorrevision.tostring());
listbox1.insertitem (7,"cpu type:"+cputype);
listbox1.insertitem (8,"maximum application address: "+psi.lpmaximumapplicationaddress.tostring());
listbox1.insertitem (9,"minimum application address:" +psi.lpminimumapplicationaddress.tostring());
/************** 从 globalmemorystatus 获取返回值****************/
memorystatus memst = new memorystatus ();
globalmemorystatus (ref memst);
listbox1.insertitem(10,"available page file :"+ (memst.dwavailpagefile/1024).tostring ());
listbox1.insertitem(11,"available physical memory : " + (memst.dwavailphys/1024).tostring());
listbox1.insertitem(12,"available virtual memory:" + (memst.dwavailvirtual/1024).tostring ());
listbox1.insertitem(13,"size of structur :" + memst.dwlength.tostring());
listbox1.insertitem(14,"memory in use :"+ memst.dwmemoryload.tostring());
listbox1.insertitem(15,"total page size :"+ (memst.dwtotalpagefile/1024).tostring ());
listbox1.insertitem(16,"total physical memory :" + (memst.dwtotalphys/1024).tostring());
listbox1.insertitem(17,"total virtual memory :" + (memst.dwtotalvirtual/1024).tostring ());
}
catch(exception er)
{
messagebox.show (er.message);
}
}
public static void main(string[] args)
{
try
{
application.run(new form1());
}
catch(exception er)
{
messagebox.show (er.message );
}
}
}
}
新闻热点
疑难解答