用汇编访问com对象
revised july 10 2000 for the new form of coinvoke. 【程序编程相关:二点tips】
ernest murphy ernie@surfree.comrevised dec 26 2000 for inclusion as part of masm32 【推荐阅读:malloc,free和new dele】
abstract: 【扩展信息:制作可执行的JAR文件包】
sample code for this article is available at ...\com\examples\shortcut
原理:
the com (component object model) is used by the windows operation system in increasing ways. for example, the shell.dll uses com to access some of its api methods. the ishelllink and ipersistfile interfaces of the shell32.dll will be demonstrated to create a shortcut shell link. a basic understanding of com is assumed. the code sample included is masm specific.
com(组件对象模型)越来越多地被windows操作系统使用.例如,shell.dll使用com来访问一些它自己的api函数.shell32.dll的ishelllink与ipersistfile接口将被用来演示如何创建一个快捷方式.com的基本理解是需要的.包含的示例代码是masm专用的.
introduction:
导言:
com may seem complicated with its numerous details, but in use these complications disappear into simple function calls. the hardest part is understanding the data structures involved so you can define the interfaces. i apologize for all the c++ terminology used in here. while com is implementation neutral, it borrows much terminology from c++ to define itself.
com由于其众多的细节可能看上去复杂,但在实际使用中这些复杂因素消失于简单的函数调用之中.最困难的部分是理解其中用到的数据结构,知道了它你旧可以定义接口(interfaces).我对在此处使用的c++术语表示抱歉.虽然com被实现为中立的,但它从c++借用了许多术语来定义自身.
in order to use the com methods of some object, you must first instance or create that object from its coclass, then ask it to return you a pointer to its interface. this process is performed by the api function cocreateinstance. when you are done with the interface you call its release method, and com and the coclass will take care of deleting the object and unloading the coclass.
为了使用一些对象的com函数(method),你必须首先根据它的coclass具像化(示例化)或者说是创建这个对象.当你用完了这个接口,你调用它的release函数,com与coclass将会处理删除对象与卸载coclass的任务.
a com object is referred to as the server. the program that calls up a com object so it may use it is referred to as the client.
com对象被称为server(服务器).调用com对象的程序被称为client(客户)
assessing com methods
访问com函数
to use com methods you need to know before hand what the interface looks like. even if you "late bind" through an idispatch interface, you still need to know what idispatch looks like. a com interface is just table of pointers to functions. lets start with the iunknown interface. if you were to create a component that simply exports the iunknown interface, you have a fully functional com object (albeit on the level of "hello world"). iunknown has the 3 basic methods of every interface, since all interfaces inherit from iunknown. keep in mind all an interface consists of is a structure of function pointers. for iunknown, it looks like this:
为了使用com函数你需要事先知道接口(interface)的模样.即使你通过一个idispatch接口“动态绑定”,你仍然需要知道idispatch的模样.一个com接口就是一个函数指针的表格.让我们从iunknown接口开始.如果你要创建一个仅仅导出(export)iunknown接口的组件,你将得到的是一个完善的com对象(虽然仅是在“hello world”的层次上).每个接口有iunknown的3个基本的函数,因为所有的接口都从iunknown继承而来.始终记得所有的接口是包含函数指针的结构体(structure).对于iunknown来说,它就这模样:
iunknown struct dword ; iunknown methods iunknown_queryinterface queryinterface_pointer ? iunknown_addref addref_pointer ? iunknown_release release_pointer ? iunknown endsthats it, just 12 bytes long. it holds 3 dword pointers to the procedures that actually implement the methods. it is the infamous "vtable" you may have heard of. the pointers are defined as such so we can have masm do some type checking for us when compiling our calls. since the vtable holds the addresses of functions, or pointers, these pointers are typedefed in our interface definition as such:
这就是它,仅仅12字节长.... 下一页