Python
  Home arrow Python arrow Page 4 - Windows Programming in Python: Creating COM Servers
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Windows Programming in Python: Creating COM Servers
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-01-22


    Table of Contents:
  • Windows Programming in Python: Creating COM Servers
  • Developing COM Servers Step by Step
  • Annotating the Class with Attributes
  • Creating COM Servers in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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:

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek