HomePython Page 2 - Windows Programming in Python: Creating COM Servers
Developing COM Servers Step by Step - Python
In an earlier article I discussed accessing COM components from within Python programs. However, I left a question dangling, namely, can COM servers be created in Python, and can they be accessed by applications created in other languages or platforms such as Visual Basic? The answer is an emphatic yes.
Developing a COM server in Python is easier than in any other language. If the process is broken down into steps there are primarily two steps, which are developing the class and annotating the class with attributes. Furthermore, in the first step you must decide on which model the class must be based.
Developing the class
The philosophy of COM is that a fixed interface must be built and the class must be modeled around it. In essence, once the interface has been defined, it must not be changed. To work with this philosophy there are three design patterns that can be used. They are:
COM base class, pure Python subclass
Pure Python base class, COM subclass
COM interface, Python delegate
The COM interface, Python delegate pattern is the most commonly used. The reason for this is the ability to develop the class first.
In the pattern of a COM base class, pure Python subclass, a base class has to be defined and exposed as a COM server. The methods in the base class perform no tasks. The base class works as a COM interface. Then a sub class, which implements the base class, has to be developed; this class provides the actual services. In other words a class that implements the base class performs the tasks. This pattern is most appropriate when designing a class whose main function is to be used from COM and not from Python.
Now let's look at a pure Python base class, COM subclass pattern. It is in a way the opposite of the previous pattern. Here, the existing COM class is inherited from a Python base class. The COM class can be differentiated by the the attributes used to annotate the class, which will be discussed shortly.
Finally, let's examine the COM interface, Python delegate pattern. In this case, the COM interface is defined. The class that defines the COM interface has variables internal to it that point to the pure Python counterpart. The Python counterpart is known as the delegate. The methods of class that represent the COM interface translate their arguments, return values as needed, and forward them to the delegate.
Let's take the third pattern as an example. For the sake of the example, suppose there is a BookSet class that implements the methods of the COM interface. Now if the third pattern is applied to define a COM interface, it would look like this:
def double(self, arg): # trivial test function to check it's alive return arg * 2
The __BookSet variable points to the pure Python class BookSet. This is how it delegates the task to the pure Python class. You will observe that there are certain attributes such as _reg_clsid_. The explanation of these attributes is coming up next.