c#调用api

[DllImport("动态库名")]
public static extern 返回值类型 函数名称 (参数);
这样就可以调用了

 

所有的API都在下面3个DLL中实现的。
Kernel
User
GDI
1. KERNEL
它的库名是:KERNEL32.DLL,它是操作系统管理的API集
Process loading. 加载进程
Context switching.
File I/O. 文件操作
Memory management. 内存管理
比如:GlobalMemoryStatus 函数获得目前系统物理虚拟内存的使用信息。
2. USER
在WIN32下,它的库名是 USER32.DLL
This allows managing the entire user interfaces such as
它管理全部的用户界面,比如:
Windows 窗口
Menus 菜单
Dialog Boxes 对话框
Icons etc., 图标等
比如:DrawIcon 画一个图标在指定的设备上。
3. GDI (Graphical Device Interface)
这个DLL是GDI32.dll,它负责图像的输出,使用GDI绘出窗口,菜单,对话框
It can create Graphical Output. 输出图像
比如:CreateBitmap 函数创建一个指定宽度、高度和颜色格式的位图。

 

增加使用外部库的命名空间:
using System.Runtime.InteropServices;
下面声明API
[DllImport("User32.dll")]
DllImport属性用来指定包含外部方法的动态连接库的位置。 "User32.dll"指出了库名,static 指明它不属于特定的对象。extern 指明是一个外部的方法。带有DllImport 属性的方法必须带有修饰符extern 。
MessageBox 是一个汉数名,带四个参数返回一个int型值。
许多API使用结构来传递、返回参数,这样可以减少复杂度。它也允许使用象MessageBox 函数那样,使用固定的参数。

在按钮的点击事件中增加下面代码:
protected void button1_Click (object sender, System.EventArgs e)
{
MessageBox (0,"API Message Box","API Demo",0);
}

posted @ 2008-12-23 13:01  alex hu  阅读(444)  评论(0编辑  收藏  举报