comtypes question

Jorgen Bodde jorgen.maillist at gmail.com
Mon Mar 17 08:21:50 EDT 2008


Hi Thomas,

Thank you a lot for going through great lenghts to help me. I am
relatively new to COM and to python, so using those two together is
sometimes watching water burn and be totally amazed by it ;-)

It's greatly aprpeciated that you installed the app and even checked
out the typelib to see what might go wrong. I will investigate furter
and look at your working example to see what I was missing in my code.

I will also subscribe to the comtypes-users mailinglist for future questions.

ps. For background info about Bartender, it is a label application and
it can be automated by COM. By sending an XML script to it, I can set
the variables for that label and issue a print command.

With kind regards,
- Jorgen

On Mon, Mar 17, 2008 at 12:58 PM, Thomas Heller <theller at ctypes.org> wrote:
> 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
>
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list