Home arrow Python arrow Page 4 - Windows Programming in Python: Creating COM Servers

Creating COM Servers in the Real World - 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.

TABLE OF CONTENTS:
  1. Windows Programming in Python: Creating COM Servers
  2. Developing COM Servers Step by Step
  3. Annotating the Class with Attributes
  4. Creating COM Servers in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 8
January 22, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

  1. PyCOMServer.py - The COM server implemented in Python
  2. 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ů



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: