From kunal.s.chauhan at fleetship.com Fri Jul 9 02:52:47 2021 From: kunal.s.chauhan at fleetship.com (Kunal Chauhan (Student Intern)) Date: Fri, 9 Jul 2021 06:52:47 +0000 Subject: [python-win32] Pivot Table Borders Message-ID: Hello all, I am making pivot tables using pywin32 and was wondering how I could add borders to the pivot tables through python. Would greatly appreciate any help. Thank you in advance. Regards, Kunal Singh Chauhan -------------- next part -------------- An HTML attachment was scrubbed... URL: From thebig3on3 at gmail.com Mon Jul 19 20:57:33 2021 From: thebig3on3 at gmail.com (The Big 3on3) Date: Mon, 19 Jul 2021 17:57:33 -0700 Subject: [python-win32] Calling Dispatch - Error -2147221005 Message-ID: Earlier today I downloaded the pywin package to work with Microsoft Outlook. On the below code I received Error -2147221005. I'm uncertain if this has to do with an installation of the library not working properly/version issues. I use a 32 bit version of Python 3.7. *Python Code:* import win32com.client as client outlook = client.Dispatch("Outlook Application") message = outlook.CreateItem(0) message.Display() *Full Error Message:* Traceback (most recent call last): File "C:\Users\Eric\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatch IDispatch = pythoncom.connect(IDispatch) pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "Outlook_text_text.py", line 2, in outlook = client.Dispatch("Outlook Application") File "C:\Users\Eric\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) File "C:\Users\Eric\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 98, in _GetGoodDispatchAndUserName return (_GetGoodDispatch(IDispatch, clsctx), userName) File "C:\Users\Eric\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 83, in _GetGoodDispatch IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) Thanks in advance. Best, Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: From steven at manross.net Tue Jul 20 12:07:50 2021 From: steven at manross.net (Steven Manross) Date: Tue, 20 Jul 2021 16:07:50 +0000 Subject: [python-win32] Calling Dispatch - Error -2147221005 Message-ID: <75a33c1b91a0423f9edf04ac8d320527@manross.net> Seems like you have the class string invalid? ?Outlook Application? should read ?Outlook.Application? Note the period. ? And then of course, you need Outlook installed, and the correct 32-bit or 64-bit version of Outlook for your python version. HTH and Enjoy Steven -------------- next part -------------- An HTML attachment was scrubbed... URL: From william.belr at gmail.com Tue Jul 27 17:58:51 2021 From: william.belr at gmail.com (William Belanger) Date: Tue, 27 Jul 2021 17:58:51 -0400 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() Message-ID: Hi everyone, I'm trying to make a cross-platform Python interface for a third-party API (Druide Antidote). The D-Bus part is done and working ( https://gitlab.com/-/snippets/2151173), it consist of a server adapter (AdaptateurAntidote), which relay the events to an handler class (ImplementationAntidote). I want to do the same with the COM interface. However, I could not get DispatchWithEvents() to work, as it fails on "ti = disp._oleobj_.GetTypeInfo()"; C:\Users\user>"C:\Users\user\Desktop\AntidoteCOM.py - Shortcut.lnk" > --register > > Requesting elevation and retrying... > > Registered: Correcteur.Antidote > > > C:\Users\user>"C:\Users\user\Desktop\AntidoteCOM.py - Shortcut.lnk" > > Traceback (most recent call last): > > File > "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\__init__.py", > line 256, in DispatchWithEvents > > ti = disp._oleobj_.GetTypeInfo() > > pywintypes.com_error: (-2147352567, 'Exception occurred.', None, None) > > > During handling of the above exception, another exception occurred: > > > Traceback (most recent call last): > > File "Z:\.antidote_\AntidoteCOM.py", line 54, in _connect > > self.server = win32com.client.DispatchWithEvents("Correcteur.Antidote", > EventHandler) > > File > "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\__init__.py", > line 264, in DispatchWithEvents > > raise TypeError("This COM object can not automate the makepy process - > please run makepy manually for this object") > > TypeError: This COM object can not automate the makepy process - please > run makepy manually for this object > I noticed that when running localserve.serve() in a separate console, the server returns as DispatchWithEvents() is called. No errors are thrown on the server side, but I assume there is something wrong or missing in the class definition of AdaptateurAntidote. I tried changing its policy to dynamic, but that did not help. *If someone could provide a minimal example of a custom server working along with DispatchWithEvents(), that would be very helpful. *All the examples I could find use third-party servers, such as those provided by Microsoft. Full code available is here; https://gitlab.com/-/snippets/2154411 Will -------------- next part -------------- An HTML attachment was scrubbed... URL: From michel.claveau at gmail.com Wed Jul 28 04:09:58 2021 From: michel.claveau at gmail.com (michel.claveau at gmail.com) Date: Wed, 28 Jul 2021 10:09:58 +0200 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: References: Message-ID: <69ad9d8c-3735-faca-48ed-383685db397a@gmail.com> /Hi!/ / / /Druide-Antidote is not a server; it is a client, who connect to different servers (COM-servers), like Word, Excel, OpenOffice.org, etc. / / / /@-salutations / /-- / Le 27/07/2021 ? 23:58, William Belanger a ?crit?: > > Hi everyone, > > I'm trying to make a cross-platform Python interface for a third-party > API (Druide Antidote). The D-Bus part is done and working > (https://gitlab.com/-/snippets/2151173 > ), it consist of a server > adapter (AdaptateurAntidote), which relay the events to an handler > class (ImplementationAntidote). > > I want to do the same with the COM interface. However, I could not get > DispatchWithEvents() to work, as it fails on "ti = > disp._oleobj_.GetTypeInfo()"; > > C:\Users\user>"C:\Users\user\Desktop\AntidoteCOM.py - > Shortcut.lnk" --register > > Requesting elevation and retrying... > > Registered: Correcteur.Antidote > > C:\Users\user>"C:\Users\user\Desktop\AntidoteCOM.py - Shortcut.lnk" > > Traceback (most recent call last): > > File > "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\__init__.py", > line 256, in DispatchWithEvents > > ti = disp._oleobj_.GetTypeInfo() > > pywintypes.com_error: (-2147352567, 'Exception occurred.', None, None) > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > > File "Z:\.antidote_\AntidoteCOM.py", line 54, in _connect > > self.server = > win32com.client.DispatchWithEvents("Correcteur.Antidote", > EventHandler) > > File > "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\__init__.py", > line 264, in DispatchWithEvents > > raise TypeError("This COM object can not automate the makepy > process - please run makepy manually for this object") > > TypeError: This COM object can not automate the makepy process - > please run makepy manually for this object > > I noticed that when running localserve.serve() in a separate console, > the server returns as DispatchWithEvents() is called. No errors are > thrown on the server side, but I assume there is something wrong or > missing in the class definition of AdaptateurAntidote. I tried > changing its policy to dynamic, but that did not help. > > *If someone could provide a minimal example of a custom server working > along with DispatchWithEvents(), that would be very helpful. *All the > examples I could find use third-party servers, such as those provided > by Microsoft. > > Full code available is here; https://gitlab.com/-/snippets/2154411 > > > Will > > > _______________________________________________ > python-win32 mailing list > python-win32 at python.org > https://mail.python.org/mailman/listinfo/python-win32 -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: signature0.gif Type: image/gif Size: 2292 bytes Desc: not available URL: From skippy.hammond at gmail.com Wed Jul 28 04:25:30 2021 From: skippy.hammond at gmail.com (Mark Hammond) Date: Wed, 28 Jul 2021 18:25:30 +1000 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: References: Message-ID: On 28/07/2021 7:58 am, William Belanger wrote: > Hi everyone, > > > I'm trying to make a cross-platform Python interface for a third-party > API (Druide Antidote). The D-Bus part is done and working > (https://gitlab.com/-/snippets/2151173 > ), it consist of a server adapter > (AdaptateurAntidote), which relay the events to an handler class > (ImplementationAntidote). > > Traceback (most recent call last): > > File > "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\__init__.py", > line 256, in DispatchWithEvents > > ti = disp._oleobj_.GetTypeInfo() > > pywintypes.com_error: (-2147352567, 'Exception occurred.', None, None) DispatchWithEvents creates a COM object, then gets the typeinfo needed to implement the interfaces defined for events - but this COM object is failing to provide that type info at runtime. It might still have a .tlb available though, and if it does, you can run `makepy.py` once and the snippet should work. The typelib can also live in a .dll, but I've never heard of Antidote so have no better suggestions. Cheers, Mark From william.belr at gmail.com Wed Jul 28 19:56:24 2021 From: william.belr at gmail.com (William Belanger) Date: Wed, 28 Jul 2021 19:56:24 -0400 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: References: Message-ID: Hi, There is no typelib file unfortunately. The interface needs to communicate with Antidote's API by creating a COM server which exposes the methods defined in the AdapteurAntidote class. The problem is that the widget queried (a QTextEdit) is instantiated in another class (MainWindow), and I cannot attribute a "parent" to the dispatched COM object (self.server), as it raises either an AttributeError or a TypeError, depending on its declaration in __public_attrs__. >From the documentation, I thought I needed to use DispatchWithEvents to handle the events in a separate "sink" class (ImplementationAntidote) that would behave more like a generic python object. Is there any other way to achieve this? It is quite a trivial feature so I assume it is possible to do so? Best, Will Le mer. 28 juil. 2021 ? 04:25, Mark Hammond a ?crit : > > On 28/07/2021 7:58 am, William Belanger wrote: > > Hi everyone, > > > > > > I'm trying to make a cross-platform Python interface for a third-party > > API (Druide Antidote). The D-Bus part is done and working > > (https://gitlab.com/-/snippets/2151173 > > ), it consist of a server > adapter > > (AdaptateurAntidote), which relay the events to an handler class > > (ImplementationAntidote). > > > > Traceback (most recent call last): > > > > File > > > "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\__init__.py", > > line 256, in DispatchWithEvents > > > > ti = disp._oleobj_.GetTypeInfo() > > > > pywintypes.com_error: (-2147352567, 'Exception occurred.', None, > None) > > DispatchWithEvents creates a COM object, then gets the typeinfo needed > to implement the interfaces defined for events - but this COM object is > failing to provide that type info at runtime. It might still have a .tlb > available though, and if it does, you can run `makepy.py` once and the > snippet should work. The typelib can also live in a .dll, but I've never > heard of Antidote so have no better suggestions. > > Cheers, > > Mark > -------------- next part -------------- An HTML attachment was scrubbed... URL: From skippy.hammond at gmail.com Wed Jul 28 22:21:30 2021 From: skippy.hammond at gmail.com (Mark Hammond) Date: Thu, 29 Jul 2021 12:21:30 +1000 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: References: Message-ID: <5d219ceb-928c-b5e6-5659-db8e96cde856@gmail.com> On 29/07/2021 9:56 am, William Belanger wrote: > Hi, > > > There is no typelib file unfortunately. The interface needs to > communicate with Antidote's API by creating a COM server which exposes > the methods defined in the AdapteurAntidote class. > > > The problem is that the widget queried (a QTextEdit) is instantiated in > another class (MainWindow), and I cannot attribute a "parent" to the > dispatched COM object (self.server), as it raises either an > AttributeError or a TypeError, depending on its declaration in > __public_attrs__. > > > From the documentation, I thought I needed to use DispatchWithEvents to > handle the events in a separate "sink" class (ImplementationAntidote) > that would behave more like a generic python object. Is there any other > way to achieve this? It is quite a trivial feature so I assume it is > possible to do so? DispatchWithEvents helps with the most common pattern used for COM events, particularly with MS products - in this model, the interfaces are described in a typelib. 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. However, I've no idea how that product works nor what it does, so can't really answer the question. I suspect you aren't the first person to run into this though and I assume the object is usable in a number of languages which support COM, so you might be better off asking in a forum specific to the product. Cheers, Mark From ludovic.giraud-ext at amundi.com Thu Jul 29 09:05:09 2021 From: ludovic.giraud-ext at amundi.com (Giraud Ludovic (AMUNDI)) Date: Thu, 29 Jul 2021 13:05:09 +0000 Subject: [python-win32] Troubleshooting with pip install pywin32 using WSL2:Ubuntu and Python 3.8.8 Message-ID: <57f0ee0a99a74039afcee3bd58219b1d@amundi.com> Hello, When I run pip install pywin32 in WSL2:Ubuntu using Python 3.8.8 I get the following error log: (base) ? ~ pip install pywin32 ERROR: Could not find a version that satisfies the requirement pywin32 (from versions: none) ERROR: No matching distribution found for pywin32 python -V Python 3.8.8 pip --version pip 21.2.1 from /home/ludo915/.local/lib/python3.8/site-packages/pip (python 3.8) Any ideas? Kind regards. Ludovic [cid:89B49E07-4580-47BA-A59D-B0A59F32E6C5] Ludovic Giraud PBM - Portfolios & Benchmarks Management Global Data Management 91 boulevard Pasteur CS 21564 75730 Paris Cedex 15 Web : www.amundi.com T?l : +33 1 76 32 67 92 @ : ludovic.giraud-ext at amundi.com Suivez-nous sur : [twitter] [linkedin] [facebook] [Sans titre-1] [youtube] -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.png Type: image/png Size: 2890 bytes Desc: image001.png URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image012.png Type: image/png Size: 1729 bytes Desc: image012.png URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image013.png Type: image/png Size: 1566 bytes Desc: image013.png URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image014.png Type: image/png Size: 1472 bytes Desc: image014.png URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image015.png Type: image/png Size: 2172 bytes Desc: image015.png URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image016.png Type: image/png Size: 1771 bytes Desc: image016.png URL: From skippy.hammond at gmail.com Thu Jul 29 20:55:04 2021 From: skippy.hammond at gmail.com (Mark Hammond) Date: Fri, 30 Jul 2021 10:55:04 +1000 Subject: [python-win32] Troubleshooting with pip install pywin32 using WSL2:Ubuntu and Python 3.8.8 In-Reply-To: <57f0ee0a99a74039afcee3bd58219b1d@amundi.com> References: <57f0ee0a99a74039afcee3bd58219b1d@amundi.com> Message-ID: On 29/07/2021 11:05 pm, Giraud Ludovic (AMUNDI) wrote: > Hello, > > When I run pip install pywin32 in WSL2:Ubuntu using Python 3.8.8 I get > the following error log: WSL is a linux environment - pywin32 simply doesn't work there. You can install it via a Windows command-prompt, or in WSL stick to the versions of Python that work for Ubuntu but without pywin32. HTH, Mark From william.belr at gmail.com Fri Jul 30 09:24:43 2021 From: william.belr at gmail.com (William Belanger) Date: Fri, 30 Jul 2021 09:24:43 -0400 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: <5d219ceb-928c-b5e6-5659-db8e96cde856@gmail.com> References: <5d219ceb-928c-b5e6-5659-db8e96cde856@gmail.com> Message-ID: 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? Looking back at Antiote's C++ example, there is an idl file available from them. Is it necessary though? I would rather do everything dynamically, such as setting the GUID, just like I did for the DBus interface. DBus interface: https://gitlab.com/-/snippets/2151173 COM interface: https://gitlab.com/-/snippets/2154411 Thanks, Will Le mer. 28 juil. 2021 ? 22:21, Mark Hammond a ?crit : > > On 29/07/2021 9:56 am, William Belanger wrote: > > Hi, > > > > > > There is no typelib file unfortunately. The interface needs to > > communicate with Antidote's API by creating a COM server which exposes > > the methods defined in the AdapteurAntidote class. > > > > > > The problem is that the widget queried (a QTextEdit) is instantiated in > > another class (MainWindow), and I cannot attribute a "parent" to the > > dispatched COM object (self.server), as it raises either an > > AttributeError or a TypeError, depending on its declaration in > > __public_attrs__. > > > > > > From the documentation, I thought I needed to use DispatchWithEvents to > > handle the events in a separate "sink" class (ImplementationAntidote) > > that would behave more like a generic python object. Is there any other > > way to achieve this? It is quite a trivial feature so I assume it is > > possible to do so? > > DispatchWithEvents helps with the most common pattern used for COM > events, particularly with MS products - in this model, the interfaces > are described in a typelib. 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. However, > I've no idea how that product works nor what it does, so can't really > answer the question. > > I suspect you aren't the first person to run into this though and I > assume the object is usable in a number of languages which support COM, > so you might be better off asking in a forum specific to the product. > > Cheers, > > Mark > -------------- next part -------------- An HTML attachment was scrubbed... URL: From skippy.hammond at gmail.com Sat Jul 31 00:15:19 2021 From: skippy.hammond at gmail.com (Mark Hammond) Date: Sat, 31 Jul 2021 14:15:19 +1000 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: References: <5d219ceb-928c-b5e6-5659-db8e96cde856@gmail.com> Message-ID: 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 From william.belr at gmail.com Sat Jul 31 10:21:27 2021 From: william.belr at gmail.com (William Belanger) Date: Sat, 31 Jul 2021 10:21:27 -0400 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: References: <5d219ceb-928c-b5e6-5659-db8e96cde856@gmail.com> Message-ID: 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 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: From mhammond at skippinet.com.au Sat Jul 31 21:36:38 2021 From: mhammond at skippinet.com.au (Mark Hammond) Date: Sun, 1 Aug 2021 11:36:38 +1000 Subject: [python-win32] COM: Minimal example of a custom server working with DispatchWithEvents() In-Reply-To: References: <5d219ceb-928c-b5e6-5659-db8e96cde856@gmail.com> Message-ID: 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 > > > 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