I am using this Python implementation of RTD server.
I get an error:
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
The error occurs when the excel type-libe call InvokeTypes(10, LCID, 1, (24, 0), (),)
.
I gather form this question that every time a thread wants to use COM, I should call CoInitialize(). Now I don't know why it is not implemented in the code than. Anyway I tried to add this inside the Update function, which start a timer thread:
def Update(self):
# Get our wake-up thread ready...
pythoncom.CoUninitialize()
pythoncom.CoInitialize()
self.ticker = threading.Timer(self.INTERVAL, self.Update)
try:
# Check if any of our topics have new info to pass on
for topic in self.topics.values():
topic.Update(self)
if topic.HasChanged():
refresh = True
if len(self.topics):
refresh = False
topic.Reset()
if refresh:
self.SignalExcel()
finally:
self.ticker.start() # Make sure we get to run again
The uninitialize is called based on the documentation saying import pythoncom alone makes the first initialization.
Perhaps I should do the initialization somewhere else in the code?
I realize the whole complexity of the problem is not explained in this post, so please ask questions.
The rules for which object an apartment lives in are slightly more complex. If the COM object in question is implemented in any way other than an InProc DLL (for example, a LocalServer or RemoteServer EXEbased object), the question becomes moot, as the object is running in a separate process, and therefore can not be in the same apartment. For DLL-implemented objects, the apartment is determined both by the apartment of the thread creating the object and the threading models actually supported by the object.