Implementing a COM Server
In Chapter 5 we presented a sample COM server. This example recaps that code:
# SimpleCOMServer.py - A sample COM server - almost as small as they come! # # We simply expose a single method in a Python COM object. class PythonUtilities: _public_methods_ = [ 'SplitString' ] _reg_progid_ = "PythonDemos.Utilities" # NEVER copy the following ID # Use "print pythoncom.CreateGuid()" to make a new one. _reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}" def SplitString(self, val, item=None): import string if item != None: item = str(item) return string.split(str(val), item) # Add code so that when this script is run by # Python.exe, it self-registers. if __name__=='__main__': print "Registering COM server..." import win32com.server.register win32com.server.register.UseCommandLine(PythonUtilities)
The main points from the example are:
Most COM servers are implemented as Python classes. These classes have special attribute annotations that indicate how the object is published via COM; our sample uses the minimum possible to register and expose a COM server.
The
win32com
package automatically registers and unregisters the COM server.
The list of annotation attributes can be broken into two sets: those that expose the object via COM and those that allow the object to be registered via COM. Table 12.3 lists the annotations used at runtime; registration attributes are covered in the next section.
Attribute |
Description ... |
Get Python Programming On Win32 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.