首页 > 开发 > 综合 > 正文

C# 和 API

2024-07-21 02:19:43
字体:
来源:转载
供稿:网友
api (application programming interface) is a set of commands, which interfaces the programs with the processors. the most commonly used set of external procedures are those that make up microsoft windows itself. the windows api contains thousands of functions, structures, and constants that you can declare and use in your projects. those functions are written in the c language, however, so they must be declared before you can use them. the declarations for dll procedures can become fairly complex. specifically to c# it is more complex than vb. you can use api viewer tool to get api function declaration but you have to keep in mind the type of parameter which is different in c#.

api(应用编程接口)是程序与处理器接口的命令集。最常用的就是在外部调用微软windows内部的进程。windows api包括成千的你可以使用的函数、结构、常量。这些函数是用c语言写的,在使用他们之前,你必须声明。 定义dll的进程将相当的复杂,甚至比vb还复杂。你可以使用api viewer工具得到api函数的声明,但是必须注意的是,它的参数类型跟c#的不一样。

most of the advanced languages support api programming. the microsoft foundation class library (mfc) framework encapsulates a large portion of the win32 (api). odbc api functions are useful for performing fast operations on database. with api your application can request lower-level services to perform on computer's operating system. as api supports thousands of functionality from simple message box to encryption or remote computing, developers should know how to implement api in their program.

大部分的高级语言都支持api,微软函数类库(mfc)封装了大部分的win32 api。odbc api对提高数据库的操作速度大有好处。使用api,可以请求更底层的系统服务。api从简单的对话框到复杂的加密运算都提供支持。开发者应该知道如何在他们程序中使用api

api has many types depending on os, processor and functionality.

api有许多类型,(针对不同的操作系统、处理器…………)

os specific api:
操作系统特有api:

each operating system has common set of api's and some special e.g. windows nt supports ms-dos, win16, win32, posix (portable operating system interface), os/2 console api and windows 95 supports ms-dos, win16 and win32 apis,

每种操作系统都有一套公用api和专有api。比如:windows nt 支持ms-dos, win16, win32, posix (便携式操作系统接口),os/2 console api ;同时windows 95 supports ms-dos, win16 和win32 api。

win16 和 win32 api:
win16 is an api created for 16-bit processor and relies on 16 bit values. it has platform independent nature e.g. you can tie win16 programs to ms-dos feature like tsr programs.

win16 是基于16位的处理器,并使用16位的值,它是一个独立的平台。比如:你可以运行tsr 程序在ms-dos环境下。

win32 is an api created for 32-bit processor and relies on 32 bit values. it is portable to any operating system, wide range of processors and platform independent nature.

win32 是基于32位的处理器,并使用32位的值。他可用于任何操作系统,它的使用范围更广。

win32 api has 32 prefix after the library name e.g. kernel32, user32 etc?

win32 api的dll一般都具有32的后缀,比如:kernel32, user32等。

all apis are implemented using 3 libraries.

所有的api都在下面3个dll中实现的。

kernel
user
gdi

1. kernel
it is the library named kernel32.dll, which supports capabilities that are associated with os such as

它的库名是:kernel32.dll,它是操作系统管理的api集

process loading. 加载进程
context switching.
file i/o. 文件操作
memory management. 内存管理
e.g. the globalmemorystatus function obtains information about the system's current usage of both physical and virtual memory

比如:globalmemorystatus 函数获得目前系统物理虚拟内存的使用信息。

2. user
this is the library named "user32.dll" in win32.

在win32下,它的库名是 user32.dll

this allows managing the entire user interfaces such as

它管理全部的用户界面,比如:

windows 窗口
menus 菜单
dialog boxes 对话框
icons etc., 图标等
e.g. the drawicon function draws an icon or cursor into the specified device context.

比如:drawicon 画一个图标在指定的设备上。

3. gdi (graphical device interface)
this is the library named "gdi32.dll" in win32. it is graphic output library. using gdi windows draws windows, menus and dialog boxes.

这个dll是gdi32.dll,它负责图像的输出,使用gdi绘出窗口,菜单,对话框

it can create graphical output. 输出图像
it can also use for storing graphical images. 存储图像
e.g. the createbitmap function creates a bitmap with the specified width, height, and color format (color planes and bits-per-pixel).

比如:createbitmap 函数创建一个指定宽度、高度和颜色格式的位图。

c# and api:
implementing api in c# is tuff job for beginners. before implementing api you should know how to implement structure in c#, type conversion, safe/unsafe code, managed/unmanaged code and lots more.

c#中api的工具对初学者是相当不错的。在c#使用中使用api之前,你应该先知道c#中如何使用结构、类型转换,安全与不安全代码等。

before implementing complex api we will start with simple messagebox api. to implement code for messagebox api open new c# project and add one button. when button gets clicked the code will display message box.

使用复杂的api之前,我们先用一个简单的messagebox api作为列子。打开一个c#工程,增加一个按钮,在按钮的点击事件中,我们将显示一个信息框。

增加使用外部库的命名空间:

using system.runtime.interopservices;
下面声明api

[dllimport("user32.dll")]
public static extern int messagebox(int h, string m, string c, int type);

where dllimport attribute used for calling method from unmanaged code. "user32.dll" indicates library name. dllimport attribute specifies the dll location that contains the implementation of an extern method. the static modifier used to declare a static member, which belongs to the type itself rather than to a specific object, extern is used to indicate that the method is implemented externally. a method that is decorated with the dllimport attribute must have the extern modifier.

dllimport属性用来指定包含外部方法的动态连接库的位置。 "user32.dll"指出了库名,static 指明它不属于特定的对象。extern 指明是一个外部的方法。带有dllimport 属性的方法必须带有修饰符extern 。

messagebox is function name, which returns int and takes 4 parameters as shown in declaration.

messagebox 是一个汉数名,带四个参数返回一个int型值。

many api uses structure to pass and retrieve values, as it is less expensive. it also uses constant data type for passing constant data and simple data type for passing built-in data type as seen in previous declaration of messagebox function.

许多api使用结构来传递、返回参数,这样可以减少复杂度。它也允许使用象messagebox 函数那样,使用固定的参数。

在按钮的点击事件中增加下面代码:

protected void button1_click (object sender, system.eventargs e)
{

messagebox (0,"api message box","api demo",0);

}

编译并运行程序,点击按钮以后,你就可以看到一个由api调用的信息框。

using structure 使用结构
working with api, which uses complex structure or structure in structure, is somewhat complex than using simple api. but once you understand the implementation then whole api world is yours.

api中经常使用复杂的结构。不过一旦你明白了他们,将是很简单的。

in next example we will use getsysteminfo api which returns information about the current system.

下面的列子,我们用getsysteminfo api得到当前系统的信息。

第一步:增加一个c#窗口,并在上面增加一个按钮,在窗口代码页增加一个命名空间:

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

where ref is method parameter keyword causes a method to refer to the same variable that was passed into the method.

ref是一个标志参量传递形式的关键字,它使传入传出的变量指向同一个变量(传址传递)

add following code in button click event in which first create struct object and then pass it to function.

在按钮点击事件中增加下面的代码,

protected void button1_click (object sender, system.eventargs e)
{

try
{

system_info psi = new system_info();
getsysteminfo(ref psi);
//
//
//

once you retrieve the structure perform operations on required parameter

比如:

listbox1.items.insert(0,psi.dwactiveprocessormask.tostring());

//
//
//


}


catch(exception er)
{


messagebox.show (er.message);

}


}

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表