Home > Technology > COM Objects? Bus dho minute!*

COM Objects? Bus dho minute!*

Interning at Microsoft IDC, Hyderabad**, I was asked to create a prototype of a COM(ActiveX) object that will wrap aroung ADO DB provider and avoid the connection string while force the user to store DB files in a centralised location. Now if you don't understand what ADO DB is, that's perfectly OK, because that will not be our topic of discussion today. Just understand that it is an object that you use to connect to a Database. Now, I knew nothing about COM objects and know nothong much yet ;-). and how do i pull it off then, In comes Python to the rescue!

 

Creating and usuing COM objects is ridiculously easy in Python. You would need Python's win32 extensions to fiddle with COM and you can it from here and ofcourse you'd need Windows if you want to run it πŸ˜€

 

Creating COM Objects

Lets look at creating objects first. For the sake of discussion lets create a simple HelloWorld class here:

class helloworld:
def say(self):
return "Hello World"

Now to make this a COM object you just have to add a few variables,

_public_methods_ = ["say"]
_reg_progid_ = "helloworld"
_reg_clsid_ = "{BCC85C2C-87DA-4ee4-A0CF-0664B36A56F2}"

Lets take a look at each of these:

_public_methods_ is used to specify which methods in the class you’re willing to expose. There is also a _public_attrs_ to specific the attributes you’re willing to expose.

_reg_progid_ contains the progid, the ‘name’ which you will use inorder to create COM objects later.

_reg_clsid_ is the classid the is something like a primary key for COM objects. This is a unique key that has to be created for each COM object. DO NOT COPY THIS KEY INTO YOUR MACHINE!!

 

In order to create guids, do the following code in the interpreter

>>> import pythoncom

>>> pythoncom.CreateGuid()

IID('{BCC85C2C-87DA-4ee4-A0CF-0664B36A56F2}')

 

You can copy this value and assign it to _reg_clsid_ variable and don’t worry about duplicates, The algorithm behind GUID created has apparently been proven to have an extremely low probability of creating duplicates. A Senior of mine told me that even if you continuously create keys from it, it will not create a duplicate till 2035. (and don’t ask me how, ‘cause it didn’t ask him either πŸ˜‰ ).

 

And that it you have it, your very own COM object! But wait, there’s still a little bit work to do, you have to register this COM object with the registry in order to make it accessible. To do that lets make include the following code in your script

if __name__=='__main__':

    import win32com.server.register  

    win32com.server.register.UseCommandLine(helloworld)

 

Incase you’re a newbie to python(like me), the first if statement allows this code to be executed only when this file is run as a script.Here's the complete Code:

#————————————————————————————————————————-#

import pythoncom

class helloworld:
_public_methods_ = ["say"]
_reg_progid_ = "helloworld"
_reg_clsid_ = "{BCC85C2C-87DA-4ee4-A0CF-0664B36A56F2}"

def say(self):
return "Hello World"

if __name__=='__main__':
import win32com.server.register
win32com.server.register.UseCommandLine(helloworld)

#————————————————————————————————————————-#

Now to register our COM…

D:\Projects\DBMS COM>HelloCOM.py

Registered: helloworld

 

D:\Projects\DBMS COM>

 

Now for any reason you want to unregister this COM objects just give –unregister as a command line argument

 

D:\Projects\DBMS COM>HelloCOM.py –unregister

Unregistered: helloworld

 

Using COM objects

Now to use COM objects from Python, The following example I hope is self-explanatory

>>> import win32com.client as w32c

>>> helloObj = w32c.Dispatch('helloworld')

>>> helloObj.say()

u'Hello World'

>>>

 

And there you go! With this you can even call Application like Excel and Word from Python.

 

* Hindi for “Just 2 minutes!” and inspired by the countless Maggi Noodles Ads. (Just In case you didn’t know.J)

** I just had to say that somewhere!

Advertisements
Categories: Technology
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: