Windows Programming in Python: Creating COM Servers - Creating COM Servers in the Real World
(Page 4 of 4 )
In the previous sections I introduced the basic requirements for creating a COM server in Python. Now let's put the concepts into practice. The example will provide a simple functionality -- splitting a given string. There are two parts of the application:
- PyCOMServer.py - The COM server implemented in Python
- SampleClent.vb - The client implemented in VB
Let's start with the server. First let's define the Python class contained in PyCOMServer.py - PythonUtilities
class PythonUtilities:
def SplitString(self, val, item=None):
import string
if item != None: item = str(item)
return string.split(str(val), item)
This class defines a single method, SplitString, that takes two arguments: item, which is the string to be split; and the value contained in val on the basis of which string has to be split.
The next step is to embed the attributes so that the class can expose its functionalities through the COM:
class PythonUtilities:
_public_methods_ = [ 'SplitString' ]
_reg_progid_ = "PythonServer.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)
Since there is only one method that needs to be exposed, the list for _public_methods_ contains only the SplitString method. Next, the name by which it can be called is given via _reg_progid_ which is PythonServer.Utilities. Finally the class id generated using pythoncom.CreateGuid().
Next is the main code that is required to register and run the COM server.
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)
Next comes the client. For developing the client start the Macro editor either in MS Word or MS Excel. Enter the name for the macro. The implementation of the macro is as follows:
Set PythonUtils = CreateObject("PythonDemos.Utilities")
response = PythonUtils.SplitString("Hello from VB")
for each Item in response
MsgBox Item
Next
That brings us to the end of this discussion. The application developed doesn't apply the design patterns, as certain advanced aspects of COM programming have to be covered yet. That's the agenda for the next part: advanced aspects of COM servers. Till then…
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |