That works, thank you. I'm relieved to finally move forward with this. Revised example;
#!/usr/bin/python3 > # https://gitlab.com/-/snippets/2155778 > > import pythoncom > import sys > import threading > import win32com.client > import win32com.server.util > from win32com.server import localserver > > > class EventHandler: > _public_methods_ = ["ping"] > > def __init__(self): > self.server = win32com.client.Dispatch("Minimal.Example") > self.server.setParent(win32com.server.util.wrap(self)) > self.server.Ping() # ## > > def ping(self): > print("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_attrs_ = ["parent"] > _public_methods_ = ["setParent", "Ping"] > > def setParent(self, parent): > self.parent = win32com.client.Dispatch(parent) > > def Ping(self): > self.parent.ping() > > > def server_init(): > pythoncom.CoInitialize() > localserver.serve(["{D390AE78-D6A2-47CF-B462-E4F2DC9C70F5}"]) > > > 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=server_init) > server_thread.start() > EventHandler() > Best, Will Le sam. 31 juil. 2021 à 21:36, Mark Hammond <mhamm...@skippinet.com.au> a écrit : > > On 1/08/2021 12:21 am, William Belanger wrote: > > 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 > > <https://gitlab.com/-/snippets/2155778> > > > > import pythoncom > > import sys > > import threading > > import win32com.client > > import win32com.server.util > > from win32com.server import localserver > > > > > > def 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 > > That exception is very odd and I should look into it - but the problem > here is that you are trying to set an attribute on a COM object which is > a Python instance, which doesn't really make sense. What you want is to > set it to a COM object - which `self` is not. So something like: > > > class Main: > _public_methods_ = ["pong"] > def __init__(self): > self.server = win32com.client.Dispatch("Minimal.Example") > self.server.parent = win32com.server.util.wrap(self) > self.server.Ping() > > def pong(self): > return "pong" > > works. Sadly another paper-cut here is that the server just sees this as > a plain IDispatch object rather than a `Dispatch` wrapper - so `Ping` > needs to look something like: > > def Ping(self): > parent_as_object = win32com.client.Dispatch(self.parent) > print(parent_as_object.pong()) > > I guess you could, say, change `parent` into `set_parent()`, or > intercept via __setattr__() and do the wrapping just once. > > HTH, > > Mark >
_______________________________________________ python-win32 mailing list python-win32@python.org https://mail.python.org/mailman/listinfo/python-win32