1. Computing
An Introduction to COM Programming with Delphi (1 / 6)
A brief historical rundown on COM's glorious past. Abstract methods vs. Interfaces. Classes and Interfaces: An interesting paradox.
 More Delphi COM Lessons
• Lesson 2
• Lesson 3
• Lesson 4
Lesson 5
Lesson 6
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• COM / OLE / ActiveX programming with Delphi
 Download Source Code
• COMCodeExample1
• Code Solution

Article written by Curtis Socha, brought to you by Zarko Gajic.

A brief historical rundown on COM’s glorious past

COM began as Object Linking and Embedding, which we all know as OLE. Basically, OLE allows a client application to store data from a server. It also contains information on how to get that server kick started whenever the server is needed. OLE has theoretically been discontinued by you-know-who and the term COM should be used instead. These days, OLE is used to refer to the higher level features that are constructed on top of COM technology.

COM stands for Component Object Model. It allows you to create COM objects that are not specific to any language, and in some cases, even platforms. For instance, COM objects can be ported to a Unix system. COM also allows you to create COM Objects that will be instantiated on a different machine across the world if you so desired. COM programming brings programmers the following benefits:

  • Create objects that can be used by multiple programming languages
  • Create ActiveX controls
  • Control other programs via OLE automation (like MSWord, Excel, I.E)
  • Work with objects or programs on other machines (DCOM)

Abstract methods vs. Interfaces

Before we learn how to use Interfaces, we are going to need to understand whence the theory originates. Thus, we will discuss Abstract methods. Let us examine the following chunk of code. Pay special attention to the way virtual, abstract, and override are used.

  TSomeNumber = class
    function SomeString: string; virtual; abstract;

  TSomeInteger = class(TSomeNumber)
     FValue : Integer; 
     function SomeString: string; override;

  TSomeDouble = class(TSomeNumber)
     FValue : Double;
     function SomeString: string; override;

After your classes are defined, you can create a procedure called ShowSomeNumber as shown below.

procedure ShowSomeNumber(F : TSomeNumber);

Below is an example of how you can create two different classes, but pass either of them into ShowSomeNumber. ShowSomeNumber will then call the appropriate method for the class that was passed in.

  MyDouble : TSomeDouble;
  MyInteger : TSomeInteger;
  MyDouble := TSomeDouble.Create;
  MyInteger := TSomeInteger.Create;

As we can see, ShowSomeNumber does not care if you pass in a TSomeDouble or a TSomeInteger. Since both of those classes were derived from the class TSomeNumber that contains the abstract method SomeString, you can call the sub-classes implementation of SomeString by calling F. SomeString. It is also important to note that you should not create an instance of TSomeNumber class. If you try to compile code in which you create a direct instance of this class, you will receive the following warning from Delphi:

"[Warning] COMCodeExample1_Unit.pas(126): Constructing instance of 'TSomeNumber' containing abstract methods"

Obviously, Delphi is concerned that you are creating a class that implements nothing. Let us see what happens if you decide to create an instance of this class and then try to call SomeString.

  F : TSomeNumber;
  S : string;
  F := TSomeNumber.Create;
  S := F. SomeString;

If you executed this program, you would receive the abstract warning from Delphi and then you would receive the following exception from the OS for obvious reasons: "Abstract error!".

You cannot access a method that has no implementation. There is nothing to access! It is also interesting to note that Delphi will not you to do CTRL-SHIFT-C for code completion in the class that contains the abstract method. It knows that if it is declared as abstract that it cannot implement it! Clever clever….

These concepts may seem difficult to grasp, but it must be understood in order to understand how COM technology operates. Refer to COMCodeExample1 to see an actual program that incorporates this concept.

Classes and Interfaces: An interesting paradox

The way in which an Interface functions is really no different than a class that only contains abstract methods. The functionality is the same, however, the way you create an interface and use them is different than that of a class. So, let us establish a rule. Interfaces are not classes. They are an independent construct that has it’s own set of rules just like classes do. They have some similarities, but it is easier to think of them as entirely different branch of the language.

Homework Assignment

In this homework assignment, we will need to solidify your understanding of how abstract methods work within the context of classes and the sub-classes that implement the inherited abstract methods. This must be written from scratch because as I teach you more about interfaces, you will quickly become lost if you do not have a strong foundation of knowledge on how abstract methods work.

Homework Notes:
You will use TStringList in this homework. If you are not familiar with this class, study up on it. It is very useful.
The implementation will use the PostMessage API command and the WM_KEYDOWN Windows message.

Homework Project:

  1. Create a form in which there are three buttons and one TEdit
  2. Create a class that contains one abstract procedure called DoMessage
  3. Create two sub-classes that implement the abstract method
  4. If button one is clicked, it adds your first sub-class to the TStringList
  5. If button two is clicked, it adds your second sub-class to the TStringList
  6. If button three is clicked, the program will loop through the classes that you added to the TStringList and have it call the overridden implementation of your sub-classes
  7. The implementation for sub-class one should do a PostMessage that sends a WM_KEYDOWN and the letter "A" to the TEdit you placed on the form
  8. The implementation for sub-class two should do a PostMessage that sends a WM_KEYDOWN and the letter "B" to the TEdit you placed on the form

Homework Summary:
Essentially, your program should allow you add a bunch of your two sub-classes to a TStringList and then loop through that TStringList, calling the sub-classes implementation of the abstract method. The implementation of the DoMessage will do a PostMessage to the TEdit you dropped on the form. You should end up with results like "AABABAAABA…" in the TEdit when you click on the 3rd mouse button.

You should use COMCodeExample1 for reference. This should take roughly 2-4 hours to complete.
Here's the solution (COMCodeSolution1) to the homework. Remember: Looking at the homework solutions without writing the code yourself does not help you!

An Introduction to COM Programming with Delphi: Table of Content
>> Next Lesson (2): What is an Interface? How to implement an Interface? Describing the TInterfacedObject.


©2014 About.com. All rights reserved.