px schrieb:
> I just wanted to add, from more reading, all the examples that point to what
> I want to do seem to involve registering a new class as a COM component.
> The thing is, I only really want to use this in the same client, as that
> which dispatched the object.  I don't need a server here and would like to
> avoid registering a new COM component just for that purpose.  In C++ I see
> this is can be done by simply creating a subclass of ISomeInterface, and it
> seems to work off the bat.  You can create an instance of it, and use it as
> needed.
> 
> Can something similar be done with comtypes?  Is registering a COM server
> the only way to go?
> 
> Basically I want to do something like this:
> 
> import comtypes.client as cc
> import comtypes.gen.ExampleLib
> 
> class ISomeInterface_Impl(ExampleLib.ISomeInterface):
>     # This is the concrete implementation of ExampleLib.ISomeInterface
>     pass
> 
> # --
> 
> # Dispatch the COM application
> obj = cc.CreateObject(ExampleLib.MainApp)
> 
> # Create instance of my own implementation of ISomeInterface
> myimpl = ISomeInterface_Impl()
> 
> # Here I want to pass my own implementation of ISomeInterface as a parameter
> to a method of MainApp
> obj.SomeMethod(myimpl)
> 
> 
> At this point, I'm not sure how I can define the class ISomeInterface_Impl
> to do the above.

Yes, you can do that with comtypes.  Define a class that implements the methods,
and derive the class from 'comtypes.COMObject':

class MyImpl(comtypes.COMObject):
    # you must declare the interface(s) that your object implements
    _com_interfaces_ = [ExampleLib.ISomeInterface]

    def ISomeInterface_blah(self, ...):
        # whatever

then create an instance:

myimpl = MyImpl()

pass a COM interface pointer from your object to the method of the other object:

obj.SomeMethod(myimpl.QueryInterface(ExampleLib.ISomeInterface)


That's it, in a nutshell ;-)  For writing the method implementations,
be sure to read the docs about com servers in the web pages.  And to debug
this stuff, you might want to turn logging on, like this (at the top of your
script):

import logging
logging.basicConfig(level=logging.DEBUG)

Thomas


------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to