comtypes question

Thomas Heller theller at ctypes.org
Mon Mar 17 07:58:18 EDT 2008


Jorgen Bodde schrieb:
> Hi All,
> 
> I am trying to automate a 3rd party application, and all I have to
> work on is the type library and some documentation. I hope a Python /
> COM guru can answer this or put me on the right path because I don't
> know why it does not work.
> 
> First I imported the typelib with comtypes like;
> 
>>> from comtypes.client import GetModule
>>> GetModule("C:\\Program Files\\Seagull\\BarTender\\8.0\\bartend.exe")
> 
> Which blurbs out a lot of text:
> 
> # Generating comtypes.gen._D58562C1_E51B_11CF_8941_00A024A9083F_0_8_1
> # Generating comtypes.gen.BarTender
> <module 'comtypes.gen.BarTender' from
> 'C:\Python24\lib\site-packages\comtypes\gen\_D58562C1_E51B_11CF_8941_00A024A9083F_0_8_1.pyc'>
> 
> Which seems to be ok. Now, I need to call a function on the
> Application object on which I need a "Messages" instance. The
> Application object gets created properly:
> 
>>> import comtypes.gen.Bartender as bt
>>> app = comtypes.client.CreateObject(bt.Application)
> 
> Which gives me the application (the bt.Application points to a wrapper
> class containing the CLSID of the COM class to be instantiated).
> 
> Now I browse the typelibrary and I see there is a CoClass called
> Messages. I need one of those to pass as an argument, and since
> Messages is listed as a CoClass similar to Application, I assume it
> can also be instantiated. But when I try this I get an error:
> 
>>>> msgs = cc.CreateObject('{2B52174E-AAA4-443D-945F-568F60610F55}')

[...]

> WindowsError: [Errno -2147221164] Class not registered
> 
> Both Application and Messages are listed as CoClass inside the
> typelibrary. Does anybody know  why it gives me this error and what I
> am doing wrong?

Not all coclasses can be created from scratch with CreateObject.  Sometimes
they are created by calls to methods on some other object.
I downloaded the bartender trial, and looking into the typelib it seems that,
for example, the 'XMLScript' method on the IBtApplication interface returns
Messages instances:
    COMMETHOD([dispid(17), helpstring(u'Runs xml scripts')], HRESULT, 'XMLScript',
              ( ['in'], BSTR, 'XMLScript' ),
              ( ['in'], BtXMLSourceType, 'SourceType' ),
              ( ['out'], POINTER(POINTER(Messages)), 'Messages' ),
                                         ^^^^^^^^
              ( ['retval', 'out'], POINTER(BSTR), 'retval' )),

Here is an interactive session:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from comtypes.gen import BarTender
>>> from comtypes.client import CreateObject
>>> app = CreateObject(BarTender.Application)
>>> print app
<POINTER(IBtApplication) ptr=0x277074 at 11f0a30>
>>> app.XMLScript("foo", 0)
(<POINTER(IBtMessages) ptr=0x27b354 at 125bb70>, u'')
>>> msg, retval = app.XMLScript("foo", 0)
>>> print msg
<POINTER(IBtMessages) ptr=0x275a54 at 125bc10>
>>> msg[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "comtypes\__init__.py", line 308, in __getitem__
    result = self.Item(index)
_ctypes.COMError: (-2147220992, None, (u'The item at position 0 was not found.', u'bartend', None, 0, None))
>>> msg[1]
<POINTER(IBtMessage) ptr=0x276ffc at 125bc60>
>>> m = msg[1]
>>> print m
<POINTER(IBtMessage) ptr=0x276ffc at 125bcb0>
>>> m.Number
3908
>>> m.Message
u"An error occurred during XML script processing:\r\n\r\nInvalid at the top level of the document.\r\nLine 1, Column 1:
'foo'"
>>> ^Z

Not that the above makes much sense, but I hope it will get you started.

Thomas

BTW:  The 'official' comtypes mailing list is at
https://lists.sourceforge.net/lists/listinfo/comtypes-users.  Requires that you subscribe before you can
post, but you could disable mail delivery and use via gmane: gmane.comp.python.comtypes.user




More information about the Python-list mailing list