PDA

View Full Version : Using a COM object.


Benny
07-13-2002, 01:03 AM
Hey,

I was given a COM object in the form of a DLL.

I downloaded and install win32all package (which contains the win32com package).

I was then able to convert the DLL to a python module - I used this command:


C:\Documents and Settings\Ben Birnbaum>c:\Python\python.exe c:\Python\Lib\site-p
ackages\win32com\client\makepy.py d:\wordleaf\SWordCOM.dll > d:\wordleaf\SWordCO
M.py
Generating to C:\Python\lib\site-packages\win32com\gen_py\CA741864-5BBE-4083-83E
6-01865C006DADx0x1x0.py


So now I have a python module - the source of which can be viewed here:

http://www.marlolake.com.au/SWordCOM.py

The problem I've got is, Im having trouble using it........I was given this example VB code:


Private Sub Command1_Click()
Dim a As New SWORDCOMLib.WordComImp
Dim s As String
s = a.getDataFileName("c:\letter.doc")
MsgBox s

Call a.UpdateWordDataFileName("c:\letter.doc", "c:\letter.txt")

Set a = Nothing

End Sub


I cant get this working in Python:


>>> a = SWordCOM.WordComImp
>>> a
<class SWordCOM.WordComImp at 0x00FC9400>
>>> g = a.getDataFileName("c:\letter.doc")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: class WordComImp has no attribute 'getDataFileName'


or


>>> a = SWordCOM.IWordComImp
>>> s = a.GetDataFileName("c:\test.doc")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: unbound method GetDataFileName() must be called with IWordComImp instance as first argument (got str instance instead)


Im probably over looking something really simple here - but I've no idea.

Thanks....

kmj
07-13-2002, 01:02 PM
hmm,

here's a page that may be helpful
http://www.python.org/windows/win32com/QuickStartClientCom.html


Are you obtaining the COM object the way they do on that page?

bmoyles
07-13-2002, 01:11 PM
Try this (using ActiveState python or win32all):

import win32com.client

wordcom = win32com.client.GetObject("SWORDCOMLib.WordComImp")
file = wordcom.GetDataFileName("c:\test.doc")

Benny
07-14-2002, 12:34 AM
Originally posted by kmj

Are you obtaining the COM object the way they do on that page?

Um, yeh I saw that page, but I dont understand how I can obtain the COM object using that method....

Oridinally posted by bmoyles
Try this (using ActiveState python or win32all):

import win32com.client

wordcom = win32com.client.GetObject("SWORDCOMLib.WordComImp")
file = wordcom.GetDataFileName("c:\test.doc")


Yeh thats the way the page describes, but that doesn't work. How does it know about SWORDCOMLib? - what do I use as the Object.name??


>>> wordcom = win32com.client.GetObject("SWordCOM.WordComImp")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python\lib\site-packages\win32com\client\__init__.py", line 70, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Python\lib\site-packages\win32com\client\__init__.py", line 85, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147221020, 'Invalid syntax', None, None)
>>>
>>> wordcom = win32com.client.GetObject("SWORDCOMLIb.WordComImp")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python\lib\site-packages\win32com\client\__init__.py", line 70, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Python\lib\site-packages\win32com\client\__init__.py", line 85, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147221020, 'Invalid syntax', None, None)


Hmm I dont fully understand how these COM object work, never seen/used them before in any language....

:?

kmj
07-14-2002, 02:18 AM
" Yeh thats the way the page describes, but that doesn't work. How does it know about SWORDCOMLib? - what do I use as the Object.name??"

The documentation for the object you're using should explain it, I think. Basically, what it does is checks the windows registry for the object name to look up the interface... at least, that's how it's been done in the few C++ programs I've used.

Benny
07-14-2002, 03:00 AM
Originally posted by kmj
Basically, what it does is checks the windows registry for the object name to look up the interface... at least, that's how it's been done in the few C++ programs I've used.

Okay, so how does this information get put into the registry? Cause the COM object im trying to use is just a stand alone DLL file ..... ?

All the examples/docs illustrate how to use a COM object such as "Excel.Application" and stuff, but none that I've seen talk about a .dll file.......hmmm :td:

Benny
07-14-2002, 09:09 AM
Sweet, I figured things out.

I found out how to register the dll in windows:

C:\Documents and Settings\Ben Birnbaum>regsvr32 d:\wordleaf\SWordCOM.dll

then this worked:

>>> wordcom = win32com.client.Dispatch("SWordCom.WordComImp")
>>> wordcom
<win32com.gen_py.SWordCOM 1.0 Type Library.IWordComImp>

Coolies!

kmj
07-14-2002, 01:12 PM
Oh yeah, I should have mentioned regsvr32.. sorry. I'm a COM novice, myself. Glad you got it figured out.

Benny
07-15-2002, 01:48 AM
Originally posted by kmj
Oh yeah, I should have mentioned regsvr32.. sorry. I'm a COM novice, myself. Glad you got it figured out.

No need to apologise, thanks for your help. I've finally got the hang of COM now.

An interesting thing I came across while trying to find help on COM was, pySOAP - http://www.onlamp.com/lpt/a/python/2001/06/14/pysoap.html

Seems like it could be quite nifty and fun, going to have to delve deeper into that when I have time.

Cheers.

Edit - oooh this is a special post, my 30th, Im no longer a "Code Monkey"! hehe yay! Bring on "Coder"!