|
视窗操作系统应用程序接口(Windows API),有非正式的简称法为WinAPI,是微软对于Microsoft Windows操作系统中可用的核心应用程序编程接口的称法。它设计为由C/C++ 程序调用,而且它也是应用软件 与Windows系统最直接的交互方式 . 而大多数驱动程序所需要的对Windows系统的更底层次访问接口, 由所用版本的Windows的Native API 来提供接口。
Windows有一个software development kit(软件开发包) (SDK) , 提供相应的文档和工具,以使程序员开发使用Windows API的软件和利用Windows技术。
[编辑] Windows API分类
Windows API所提供的功能可以归为七类:[1]
- Base Services(基础服务)[2]
- 提供对Windows系统可用的基础资源的访问接口。比如象:file systems(文件系统), devices(外部设备), processes(进程) and threads(线程), 以及访问 Windows registry(注册表)和error handling(错误处理机制). 这些功能接口位于,16-bit(16位) Windows下的 kernel.exe, krnl286.exe 或 krnl386.exe 系统文件中 , 以及32-bit(32位) Windows下的 kernel32.dll 和 advapi32.dll 中.
- Graphics Device Interface(图形设备接口)[3]
- 提供功能为:输出图形内容到monitors(显示器), printers(打印机) 以及其他 output devices(外部输出设备). 它位于bit(16位) Windows下的 gdi.exe , 以及32-bit(32位)Windows下的gdi32.dll .
- User Interface(图形化用户界面)[4]
- 提供的功能有创建和管理 windowscreen(屏幕)和大多数基本控件(control)(比如button(按钮)和scrollbar(滚动条)),和接收鼠标和键盘输入,以及其他与WindowsGUI(图形化用户界面)有关的功能。 这些调用接口位于:16-bit (16位) Windows下的user.exe, 以及32-bit(32位)Windows下的 user32.dll s. 从Windows XP版本之后, 基本控件和通用对话框控件(Common Control Library)的调用接口放在comctl32.dll中.
- Common Dialog Box Library(通用对话框链接库)[5]
- 为应用程序提供标准对话框,比如打开/保存文件对话框,颜色对画框和字体对话框等等。 这个链接库位于:16-bit (16位) Windows下的commdlg.dll 中, 以及32-bit(32位)Windows下 comdlg32.dll 中. 它被归类为User Interface API之下.
- Common Control Library(通用控件链接库)[6]
- 为应用程序提供接口来访问操作系统提供的一些高级控件. 比如像:status bar(状态栏)s, progress bars(进度条), toolbar(工具栏)s 和 tab(标签)s. 这个链接库位于:16-bit (16位) Windows下的 DLL(动态链接库) 文件commctrl.dll中, 以及32-bit(32位)Windows下 comctl32.dll 中. 它被归类为User Interface API之下.
- Windows Shell(Windows外壳)[7][8]
- 作为Windows API的组成部分,不仅允许应用程序访问operating system shell(操作系统shell) 提供的功能,还对之有所改进和增强。它位于:16-bit (16位) Windows下的 shell.dll 中, 以及32-bit(32位)Windows下的 shell32.dll 中(Windows 95则在 shlwapi.dll中)。 它被归类为User Interface API之下.
- Network Services(网络服务)[9]
- 为访问操作系统提供的多种networking(网络) 功能提供接口. 它包括 NetBIOS, Winsock, NetDDE, RPC等.
[编辑] Web相关API
Internet Explorer 网页浏览器也提供许多程序接口 [10]. 它将不会在Windows Vista中集成。特别提供以下接口:
- 可嵌入的网页浏览器控件, 位于shdocvw.dll 和 mshtml.dll之中.
- urlmon.dll中的URL monikers service(命名服务),利用COM对象为应用程序提供解析URL服务.应用程序也可以为其他程序提供自己的URL处理程序.
- 一个支持多种语言文本的链接库(mlang.dll).
- DirectX Transforms, 一组图像过滤组件.
- XML 支持 (MSXML组件).
- 访问Windows Address Book.
[编辑] 多媒体相关API
自从Windows 95 OSR2以来,Microsoft把DirectX API 作为Windows安装的一部分. DirectX提供一组松散相关的多媒体和游戏服务, 包括:
[编辑] 程序通信API
Windows API的功能主要通过使Operating System 和应用程序之间的交互来实现. 为了实现不同Windows应用程序的通信, Microsoft随着主要Windows API,推出一系列的技术. 最初的Dynamic Data Exchange (DDE)升级为 Object Linking and Embedding (OLE) ,再后来是Component Object Model (COM).
[编辑] Wrapper Libraries(封装库)
Microsoft利用许多更底层的Windows API函数,开发出许多wrapper让应用程序以更抽象的方式与Windows API进行交互. Microsoft Foundation Class Library (MFC)用C++ 類別来封装 Windows API的功能, 因而允许用更object oriented(面向对象)的方式与API进行交互.Active Template Library (ATL) 是对COM的template oriented(面向模板)的封装 . Windows Template Library (WTL) 作为ATL的增强, 被用来作为MFC的轻型的替代物.
其他著名的封装库是Borland's公司的产品. 为了与MFC竞争而推出的Object Windows Library (OWL)提供了类似的object-oriented封装. 不久Borland又推出Visual Component Library (VCL)来取而代之.
所有的Windows application framework(程序框架)都是(至少部分是) 对Windows API的封装. 因而, .NET Framework和Java,以及其他在Windows下的programming languages(程序语言), 都是(或者包含)封装库.
[编辑] 歷史
Windows API總會為程序員提供大量的構建不同Windows的底層結構. 這有助於為Windows程序員開發應用程序提供大量的靈活性和功能. 但是,它同樣使Windows applications要負責處理大量底層的,有時是繁瑣的,與 Graphical user interface相關的操作.
Charles Petzold, 許多有關Windows API的暢銷書的作者曾經說過:[11]
The original hello-world program in the Windows 1.0 SDK was a bit of a scandal. HELLO.C was about 150 lines long, and the HELLO.RC resource script had another 20 or so more lines. (...) Veteran C programmers often curled up in horror or laughter when encountering the Windows hello-world program.
– Charles Petzold, Programming Microsoft Windows with C#
常用的例子程序Hello world program , 通常是用來演示一個系統上最簡單的程序 (即打印一行"Hello World").
這些年來,Windows操作系統已經今非昔比,而Windows API也隨之改變和成長並反映出這種變化。 Windows API的 Windows 1.0 版本只提供不到450個 函數調用, 而現在的版本提供了上千個。然而,整體而言,Windows接口保持了相當好的一致性,古老的Windows 1.0程序對習慣於現在版本Windows API的程序員也並不陌生。[12]
Microsoft 特別強調維持軟件的 向後兼容性。為了實現此一目標,有時微軟甚至不惜支持使用了非官方乃至(編程上)非法的API的軟件Raymond Chen,一位致力於Windows API的Windows開發者, 曾提及他:[13]
could probably write for months solely about bad things apps do and what we had to do to get them to work again (often in spite of themselves). Which is why I get particularly furious when people accuse Microsoft of maliciously breaking applications during OS upgrades. If any application failed to run on Windows 95, I took it as a personal failure.
– Raymond Chen, What about BOZOSLIVEHERE and TABTHETEXTOUTFORWIMPS?
[编辑] 其它实现
尽管微软的Windows API实现有版权保护,但被普遍认可的是,根据美国的法律先例,其他厂商仍然可以通过提供一致的API来模拟Windows,而不会侵犯版权。
Wine 项目是为Unix类平台提供 Win32 API Compatibility layer 的尝试。 ReactOS 走得更远,提供了整个Windows操作系统的模拟,与Wine项目紧密结合,以便促进代码重用和兼容。HX DOS-Extender (external link) 是另一个模拟Windows API的项目,允许通过DOS命令行来运行简单的Windows程序。
[编辑] 编译器支持
为了开发使用 Windows API的软件,一个编译器必须能处理和导入微软相关的DLLs和COM对象。编译器必须接受一种C或C++方言,并处理揭示了内部API函数名称的IDL (接口定义语言)文件和头文件。概括而言,这些预备条件(编译器、开发工具、库和头文件)被统称为Microsoft Platform SDK.很长时间以来,包含了编译器和开发工具的专利产品如Microsoft Visual Studio系列和Borland编译器(尽管至少在Windows下,SDK是可以从整个IDE环境中剥离出来单独免费下载的,据 Microsoft Platform SDK Update),是仅有的能提供整套开发环境的选择。如今MinGW 和 Cygwin也能提供一套这样的开发环境——是基于采用一种独立头文件集合来保证能与微软DLLs连接的GNU Compiler Collection。LCC-Win32是由Jacob Navia (a comp.lang.c regular)维护的一种“非商业用途免费”的C编译器。Pellesc是由Pelle Orinius维护的一种免费C编译器。MASM32是一个成熟的项目,它通过自制或由SDK平台转换的头文件和库,并利用32位微软汇编器来实现支持Windows API。
微软相关的编译器支持也是 Structured Exception Handling (SEH)特性所需要的。这个体制有双重目的:它提供了语言相关的异常处理赖以实现的基础,同时也是内核藉以通知程序发生诸如解除一个非法指针的引用或堆栈溢出之类异常状况的渠道。甫一被引入Windows 95和NT,微软/Borland C++编译器就有使用这种体制的能力,然而实际实现未被公开,而且必须经过反向工程方可用于Wine项目和免费编译器。SEH的运行机制是先把异常的句柄推入堆栈,继而将它们添加到存储于线程本地资源(即线程环境块的首字段)的一个链表里。一旦一个异常被抛出,the kernel and base libraries unwind the stack running handlers and filters as they are encountered. 事实上,每一个未有程序本身处理的异常,都将由会弹出常规Windows崩溃对话框的默认backstop处理器处理。
API在Visual Basic中的实现示例 (这个简化的例子使得用户可以让命令按钮在窗体上四处移动):
Private Const WM_NCLBUTTONDOWN As Long = &HA1&
Private Const HTCAPTION As Long = 2&
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd&, ByVal wMsg&, wParam As Any, lParam As Any) As Long
If Command1.MousePointer = 14 Then
Call ReleaseCapture
Call SendMessage(Command1.hWnd, WM_NCLBUTTONDOWN, ByVal HTCAPTION, ByVal 0&)
End If
[编辑] 注释
- ↑ Microsoft Developer Network (July 2005). Overview of the Windows API. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (July 2005). Base Services. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (July 2005). Graphics Device Interface. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (July 2005). User Interface. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (2005). Common Dialog Box Library. Retrieved September 22, 2005.
- ↑ Microsoft Developer Network (July 2005). Common Control Library. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (July 2005). Windows Shell. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (2005). Shell Programmer's Guide. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (July 2005). Network Services. Retrieved August 28, 2005.
- ↑ Microsoft Developer Network (January 2006). Programming and reusing the browser Retrieved January 22, 2006.
- ↑ Charles Petzold (December 2001). Programming Microsoft Windows with C#. Microsoft Press. Beyond the Console, page 47.
- ↑ Charles Petzold (November 11, 1998). Programming Windows, Fifth Edition. Microsoft Press. APIs and Memory Models, page 9.
- ↑ Raymond Chen (October 15, 2003). What about BOZOSLIVEHERE and TABTHETEXTOUTFORWIMPS? Retrieved August 27, 2005.
[编辑] 参考资料
- Diomidis Spinellis. A critique of the Windows application programming interface. Computer Standards & Interfaces, 20(1):1–8, November 1998. doi:10.1016/S0920-5489(98)00012-9.
[编辑] 外部链接
|
|