[python-win32] COM: Minimal example of a custom server working with DispatchWithEvents()
William Belanger
william.belr at gmail.com
Sat Jul 31 10:21:27 EDT 2021
Hi,
Thank you for the follow up. Perhaps I did not express my need clearly
enough, so I made this minimal example;
# https://gitlab.com/-/snippets/2155778
>
> import pythoncomimport sysimport threadingimport win32com.clientimport win32com.server.utilfrom win32com.server import localserverdef init_server(): pythoncom.CoInitialize() localserver.serve(["{D390AE78-D6A2-47CF-B462-E4F2DC9C70F5}"])class Main: def __init__(self): self.server = win32com.client.Dispatch("Minimal.Example") self.server.parent = self # TypeError: must be real number, not Main self.server.Ping() def pong(self): return "pong"class ServerAdapter: _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER _reg_clsid_ = "{D390AE78-D6A2-47CF-B462-E4F2DC9C70F5}" _reg_progid_ = "Minimal.Example" _reg_verprogid_ = "Minimal.Example.1" _reg_class_spec_ = "MinimalExample.ServerAdapter" _public_methods_ = ["Ping"] _public_attrs_ = ["parent"] def Ping(self): print(self.parent.pong()) # How ??if __name__ == '__main__': if '--register' in sys.argv[1:] or '--unregister' in sys.argv[1:]: import win32com.server.register win32com.server.register.UseCommandLine(ServerAdapter) else: server_thread = threading.Thread(target=init_server) server_thread.start() app = Main()
>
>
The Ping() method would usually be called from the third-party app, but
here it is done locally for the sake of demonstration.
>From the server adapter, how can I query values or call methods of another
class instance ?
Thanks,
Will
Le sam. 31 juil. 2021 à 00:15, Mark Hammond <skippy.hammond at gmail.com> a
écrit :
> On 30/07/2021 11:24 pm, William Belanger wrote:
> > Hi,
> >
> >
> > However, products are free to define events in whatever makes sense
> > for them - eg, the object could take a plain IDispatch and try and
> > dynamically query and deliver events to the methods here. If that's
> > what the app in question does, then yeah, you don't need
> > DispatchWithEvents and can do something simpler.
> >
> >
> > In short, Antidote binary is launched, then its IApiOle interface takes
> > the Dispatch object of the custom server I made and uses it to
> > communicate back and forth.
> >
> > self.server = win32com.client.Dispatch("Correcteur.Antidote")
> > self.antidote = win32com.client.Dispatch("Antidote.ApiOle")
> > self.antidote.LanceOutilDispatch2(self.server, "C", "",
> > str(version)) # Server, tool, lang, API version
> >
> >
> > Using the Dispatch() method, the interface I made is working as
> > expected, which means that the methods exposed in AdapteurAntidote are
> > then being queried (updated example below). I did not try delivering
> > back the resuts but I assume it should work.
> >
> >
> > What I could not achieve is to get the server adapter class
> > (AdapteurAntidote) to work with objects which are created at runtime,
> > namely instances of other classes. I cannot set another runtime object
> > as an attribute of the adapter class, nor pass it on instantiation.
> > Thus, the adapter is quite useless in this case. Is it even possible
> > using pywin32? If so, how?
>
> You can get and set properties which are COM objects in the same way you
> can get and set any other properties.
>
> >
> > Looking back at Antiote's C++ example, there is an idl file available
> > from them.
>
> A .idl file isn't much good to pywin32 - it wants a compiled version
> (aka a typelib.) I'd be quite surprised if their DLL had a .idl file and
> not a compiled version of it.
>
>
> > Is it necessary though? I would rather do everything
> > dynamically, such as setting the GUID, just like I did for the DBus
> > interface.
>
> In general, early-binding via a typelib should not be necessary -
> however, pywin32 does far less guessing about things when a typelib
> exists. It will be clearer what properties exist, what type they are and
> how to call them. It may well be that it is the difference between
> things working and not working. - so in this case it may well be
> necessary in practice.
>
> Mark
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/python-win32/attachments/20210731/39a33089/attachment.html>
More information about the python-win32
mailing list