bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
689 stars 70 forks source link

Fixed #31: COM fails to initialize when COM was already initialized #32

Closed devzeb closed 6 years ago

devzeb commented 6 years ago

Fixed Bug #31

devzeb commented 6 years ago

Okay, copying from bug report #31 :

When the connection to COM was already initialized before (e.g. by the debugger of the Pycharm IDE), the call to _combase.CoInitializeEx will return a runtime error (RPC_E_CHANGED_MODE 0x80010106).

This means that, despite the runtime error, COM is initialized successfully, so we can continue the execution of the program. Therefore I implemented the try - except block to check if this particular error occurred and continue execution.

The flag self.com_loaded (self.comLoadedByThisClass) is required to properly un-initialize COM in the destructor. We don't want to un-initialize COM if other components (e.g. the debugger) still require it, therefore we only un-initialize it if the class directly initialized it (which is true if the runtime error RPC_E_CHANGED_MODE 0x80010106 did not occur).

Note: I am referring to the solution of Jonathan Caves in this thread where the same problem was described: https://social.msdn.microsoft.com/Forums/vstudio/en-US/e1bc9fe4-d985-473a-88f7-ef2ed47f77b3/native-c-return-quothresult-0x80010106-cannot-change-thread-mode-after-it-is-set-quot-in?forum=vclanguage

bastibe commented 6 years ago

I did it for you, since other users were requesting this fix. Anyway, thank you very much for your contribution!

devzeb commented 6 years ago

Thanks for making the changes yourself, I didn't have time the last couple of days. Also thank you for this great project

bastibe commented 6 years ago

No problem, we all have busy schedules.