Accessing COM Objects from Assembly

 

Ernest Murphy ernie@surfree.com

 

Revised Dec 26 2000 for inclusion as part of MASM32

Revised July 10 2000 for the new form of coinvoke.

 

Sample code for this article is available at ...COMexamplesshortcut

 

 

Abstract:
--------------------------------------------------------------------------------------------------------------------

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.

 

 

Introduction:
--------------------------------------------------------------------------------------------------------------------

COM may seem complicated with it's 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.

 

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 it's interface. This process is performed by the API function CoCreateInstance. When you are done with the interface you call it's Release method, and COM and
the coclass will take care of deleting the object and unloading the 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.

 

 

Assessing COM Methods
--------------------------------------------------------------------------------------------------------------------
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. Let's 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: