C# 和 Win32API

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

}



 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为Microsoft 32位平台的应用程序编程接口, Win32 API是从事Windows应用程序开发所必备的。 首先对Win32 API函数做完整的概述;然后收录五大类函数: 窗口管理、图形设备接口、系统服务、国际特性以及网络服务; 在附录部分,讲解如何在Visual Basic和Delphi中对其调用。 本书是从事Windows应用程序开发的软件工程师的必备参考手册。 控件与消息函数 共91个函数 硬件与系统函数 共98个函数 设备场景函数 共73个函数 绘图函数 共105个函数 位图、图标和光栅运算函数 共39个函数 菜单函数 共37个函数 文本和字体函数 共41个函数 打印函数 共66个函数 文件处理函数 共118个函数 进程和线程函数 共40个函数 Windows消息函数 共11个函数 网络函数 共14个函数 目 录 第一章 Win32 API概论…………………………………………………………………………1 1.1 为什么使用Win32 API …………………………………………………………………1 1.2 Win32 API简介 …………………………………………………………………………1 1.3 综述………………………………………………………………………………………11 第二章 窗口管理函数(Windows Control Function) ……………………………………13 2.1 易用特性函数(Accessibility Features)…………………………………………13 2.2 按钮函数(Button)……………………………………………………………………20 2.3 插入标记(^)函数(Caret)…………………………………………………………21 2.4 组合框函数(Combo box) ……………………………………………………………24 2.5 通用对话框函数(Common Dialog Box) ……………………………………………25 2.6 标函数(Cursor)………………………………………………………………………36 2.7 对话框函数(Dialog Box)……………………………………………………………40 2.8 编辑控制函数(Edit Control)………………………………………………………54 2.9 图标函数(Icon)………………………………………………………………………54 2.10 键盘加速器函数(Keyboard Accelerator)……………………………………… 61 2.11 键盘输入函数(Keyboard InPut) …………………………………………………63 2.12 列表框函数(List box) ……………………………………………………………75 2.13 菜单函数(Menu) ……………………………………………………………………76 2.14 消息和消息队列函数(Message and Message Queue)……………………………90 2.15 鼠标输入函数(Mouse Input) ……………………………………………………100 2.16 多文档接口函数(Multiple Document Interface) ……………………………103 2.17 资源函数(Resource)………………………………………………………………105 2.18 滚动条函数(Scroll Bar)…………………………………………………………113 2.19 窗口函数(Window)…………………………………………………………………119 2.20 窗口类函数(Window Class)………………………………………………………144 2.21 窗口过程函数(Window Procedure)………………………………………………150 2.22 窗口属性函数(Window Property) ………………………………………………152 第三章 图形设备接口函数(Graphic Device Interface Function) …………………155 3.1 位图函数(Bitmap) …………………………………………………………………155 3.2 笔刷函数(Brush)……………………………………………………………………171 3.3 剪切函数(Clipping) ………………………………………………………………176 3.4 颜色函数(Color)……………………………………………………………………179 3.5 坐标空间与变换函数(Coordinate Space Transformation)……………………186 3.6 设备环境函数(Device Context) …………………………………………………195 3.7 填充形态函数(Filled shape) ……………………………………………………211 3.8 字体和正文函数(Font and Text)…………………………………………………215 3.9 ICM 2.0函数 …………………………………………………………………………238 3.10 线段和曲线函数(Line and Curve)………………………………………………295 3.11 图元文件函数(Metafile)…………………………………………………………300 3.12 多显示器函数(Multiple Display Monitors) …………………………………311 3.13 绘图函数和画图函数(Painting and Drawing)…………………………………313 3.14 路径函数(Path)……………………………………………………………………328 3.15 画笔函数(Pen) ……………………………………………………………………332 3.16 打印及打印假脱机程序函数(Printing and Print Spooler)…………………334 3.17 矩形函数(Rectangle) ……………………………………………………………371 3.18 区域函数(Region)…………………………………………………………………374 第四章 系统服务函数(System Service Function) ……………………………………383 4.1 访问控制函数(Access Control) …………………………………………………383 4.2 原子函数(Atom) ……………………………………………………………………406 4.3 客户/服务器访问控制函数(Client/Server Access Control) ………………409 4.4 剪贴板函数(Clipboard)……………………………………………………………431 4.5 通信函数(Communication)…………………………………………………………436 4.6 控制台函数(Console)………………………………………………………………444 4.7 数据解压库函数(Data Decompression Library) ………………………………463 4.8 调试函数(Debugging)………………………………………………………………466 4.9 设备输入输出函数(Device Input and Output)…………………………………472 4.10 动态数据交换函数(Dynamic Data Exchange) …………………………………474 4.11 动态数据交换管理函数(Dynamic Data Exchange Management)………………476 4.12 动态链接库函数(Dynamic-Link Library)………………………………………489 4.13 错误函数(Error) …………………………………………………………………496 4.14 事件日志函数(Event Logging) …………………………………………………499 4.15 文件函数(File)……………………………………………………………………503 4.16 文件安装库函数(File Installation Library) ………………………………542 4.17 文件映射函数(File Mapping)……………………………………………………546 4.18 文件系统函数 File System)………………………………………………………551 4.19 句柄和对象函数(Handle and Object)………………………………………………556 4.20 挂钩函数(Hook)………………………………………………………………………560 4.21 ImageHlp函数…………………………………………………………………………572 4.22 大整数操作函数(Iarge Integer Operations)……………………………………594 4.23 低层访问控制函数(Low-Level Access Control)………………………………596 4.24 LSAPI函数 …………………………………………………………………………617 4.25 邮槽函数(Mailslot)………………………………………………………………622 4.26 内存管理函数(Memory Management) ……………………………………………623 4.27 管道函数(Pipe) …………………………………………………………………655 4.28 电源管理函数(Power Management) …………………………………………… 663 4.29 进程和线程函数(Process and Thread)…………………………………………666 4.30 注册表函数(Registry)……………………………………………………………700 4.31 字符串操作函数(String Manipulation)……………………………………… 724 4.32 结构化异常处理函数(Structured Exception Handling) ……………………742 4.33 同步函数(Synchronization) ……………………………………………………745 4.34 系统信息函数(System Information)……………………………………………766 4.35 系统消息函数(System Message)…………………………………………………780 4.36 系统关机函数(System Shutdown) ………………………………………………781 4.37 磁带备份函数(Tape Backup) ……………………………………………………783 4.38 时间函数(Time)……………………………………………………………………789 4.39 计时器函数(Timer) ………………………………………………………………795 4.40 工具帮助函数(Tool Help) ………………………………………………………796 4.41 窗口站和桌面函数(Window Station and Desktop)……………………………799 4.42 Windows NT 4.0访问控制函数(Window NT 4.0 Access-Control)……………808 4.43 WinTrust函数(WinTrust)…………………………………………………………814 第五章 国际特性函数(International Peatures Punction)时性…………………………815 5.1 输入方法编辑函数(Input Method Editor)…………………………………………815 5.2 国家语言支持函数(National Language Support)………………………………… 828 5.3 Unicode和字符集函数(Unicode and Character Set)……………………………… 843 第六章 网络服务函数(Networding Service Function)……………………………………849 6.1 数据链路控制函数(DLC)………………………………………………………………849 6.2 网络函数(Net)…………………………………………………………………………849 6.3 NetBIOS函数……………………………………………………………………………896 6.4 网络DDE函数(Networking DDE)……………………………………………………897 6.5 RAS服务器管理函数(RAS Server Administration)………………………………901 6.6 远程访问服务函数(Remote Access Administration)………………………………910 6.7 服务函数(Service)……………………………………………………………………929 6.8 Windows网络函数(Windows Networking)……………………………………………930 附录1 如何在VB中调用DLL API ……………………………………………………………945 1 DLL API的声明……………………………………………………………………………945 2 DLL API的调用……………………………………………………………………………947 附录2 在Delphi中直接调用Windows API…………………………………………………953

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值