Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

An Interface Based Implementation by ATL

0.00/5 (No votes)
9 Dec 2002 1  
This article presents a practical technique to achieve interface based object implementation by ATL only. This technique implements basic COM concepts about interface while doesn't rely on COM runtime.

Introduction

This article presents a practical technique to achieve interface based object implementation by ATL only. This technique implements basic COM concepts about interface while doesn't rely on COM runtime.

COM is found on the idea of interface-based programming, and COM offers more features and functionalities in addition to the basic interface's functionalities, for instance, object instantiation, remote object invoking, inter-process marshalling, security and permission, thread modals, etc. All these besides-interfaces features require COM runtime DLLs. In the other hand, interfaces are the foundation of COM, it doesn't require COM runtime DLLs by definition. Since interfaces offer power and flexibility in software design, and it forces the client to access objects only by interface reference, it would produce a loose-coupling architecture for interactive objects and won't compromise the extensibility of an application.

The basic COM concept about interfaces is that any interface has it's unique IID (interface ID), and is derived from IUnknown: it implements reference counting and the capability to query for other interfaces by IID. When the object's reference count gets to zero, it can release itself automatically. When these are all the requirements for the interfaces among the objects in your project, this approach fits. And it's easy to use.

This approach will implement the above basic interface features while doesn't reply on any COM runtime DLLs, both in client side and server side. Of course, this type of "basic-COM" object won't own any features and functionalities provided by COM runtime. As you can see, it would require its own class factory to instantiate instances. By leverage of ATL, it's easy to code and maintain. Now that it doesn't rely on COM runtime to instantiate the object, it's not necessary to register the object; it allows same interface's different implementations work side by side ---- as long as they reside in different folders. Plus, this approach also gives certain portability to our code, it only requires C++ compiler to support the language feature. It has no registry API call and no COM runtime methods calls, such as CoInitialize, CoUninitialize, CoCreateInstance, etc.

In the server side, MSIL is not needed to define the interface. IID can be generated by GUISGen tools from Visual Studio. You can define the class factory method in the interface definition file. When declaring the implementation class, make it derive from CUnknownImpl< IYourInterfaceName >, then you don't need to care about IUnknown at all. To implement the class factory method, use CREATE_COM_OBJECT macro ---- only one line of code. The left code is to declare the COM objects by BEGIN_COM_OBJECT_MAP and END_COM_OBJECT_MAP macros; it has two different map macros for singleton objects and non-singleton objects. Please download the demo project for details. All the macros and templates needed to implement this idea are defined in one file: AIBBase.h, it's included with both download files.

In the client side, it only needs to include the interface definition header file, for example, AIBByATL.h (in the demo project). After loading the Server DLL, call AIBByATL_CreateObject(�) which is the class factory method for the basic-COM object instantiation, then the client can get a COM interface pointer back. Please see AIBByATL_Demo.zip for details, it has two projects in the VC6 workspace, please compile AIBByATL first, then compile AIBByATL_Client, you can see how easy server is implemented and how concise client is coded.

The beauty of this idea is to adopt a most important interface-based concept with a light weight easy approach. Because it doesn't have dependencies on any COM runtime or COM library DLLs, it even enables the interface-based object design on non-Microsoft platforms ---- as long as the target platform's C++ compiler supports pure virtual method definitions.

If you have any questions, please send me an e-mail to ModestyZ@hotmail.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here