HomePython Windows Programming in Python: Creating COM Servers
Windows Programming in Python: Creating COM Servers
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.
In this discussion I will be tackling the whys and wherefores of creating COM servers in Python. In the first section the focus will be on COM servers. In the second section and third sections, I will detail the necessary steps for creating a COM server in Python. The fourth section will provide a real world example of developing a COM server and calling it from Visual Basic. That's the outline for this discussion.
More About COM
In the world of COM there is a clear line of separation between accessing COM components and implementing COM objects. While accessing COM objects, method calls are made on them, provided externally using the interfaces exposed by the COM object. When implementing COM objects, which are also known as COM servers, the object has to publish or expose its interfaces so that external clients can use them.
This difference is evident from the way the interfaces and methods implemented are utilized either to access a COM component or expose the functionalities of a COM component. The interface under consideration is IDispatch. Programs that use an IDispatch object must call the GetIDsOfNames() and Invoke() methods to perform method calls or property reference, whereas the objects that wish expose their functionalities via IDispatch must implement the GetIDsOfNames() and Invoke() methods, providing the logic for translating between names and IDs, and so on. In the world of PythonCOM, the former is called client-side COM and the latter is server-side COM.
The next topic I will cover is the types of server-side COM objects. There are three types of server-side COM objects:
In-Proc Server
Local Server
Remote Server
The difference in the types is based on where the COM server is executing. The COM server could be executing in the process space of the client, in its own separate process space, or on a different system. Here are the details.
In-Proc Server
When the COM server is loaded into the process space of the client, it is known as an In-Proc server. In other words, "A Component Object Model (COM) object that is executing in the same process space as the user of that object is called an In Process object (InProc for short)." An In-Proc server is implemented as a DLL in other languages.
Local Server
When the COM server is loaded and executed in its own process space, it is called a Local Server. By definition, "When the user code is executing in the process space of Application A, and the COM object is executing in the process space of Application B, interface method invocations by A on interfaces implemented on B clearly must cross process boundaries, then the COM objects in Application B are cross-process objects, or Local Servers." A Local Server is implemented in the form of an EXE.
Remote Server
When the COM server is implemented as an EXE but executed on a remote machine, it is known as a Remote Server. Remote Servers come under the heading of Distributed COM or DCOM.
The important aspect of these differences is that they are mutually inclusive. That means a COM component can be registered as a combination of any of the above three types. Now that the types of COM servers have been introduced, let's see how to develop COM servers in Python step-by-step.