Discussion:
CoInitialize versus CoInitializeEx
(too old to reply)
Saul775
2006-03-27 14:32:01 UTC
Permalink
Hello, all:

I'm creating an application that interfaces with a piece of machinery. The
software that came with the machinery has COM/OLE methods that I can use,
etc. to customize the machinery more to my use.

I've created a program that uses these methods. However, when I tried to
multithread the application I created, I noticed the global members that were
declared were not working in the new thread.

I determined the solution to the problem to be that I needed to call
CoInitializeEx(NULL, COINIT_MULTITHREADED) instead of CoInitialize(NULL).

Now here's the problem. Now that I've changed to CoInitializeEx(), it seems
the application will not CoCreateInstance() UNLESS the application that came
from the manufacturer is opened! However, before, when I CoCreateInstance()
with just CoInitialize(), MY application would CoCreateInstance(), which THEN
brought up the manufacturer's application. Any ideas how I can fix this to
revert to the original setup?

Thank you.
Brian Muth
2006-03-27 16:37:27 UTC
Permalink
What is the HRESULT value of CoCreateInstance()?

Brian
Saul775
2006-03-27 18:11:02 UTC
Permalink
The HRESULT of CoCreateInstance() is 0x80080005 (Server Execution Failed).
Post by Brian Muth
What is the HRESULT value of CoCreateInstance()?
Brian
Brian Muth
2006-03-27 19:17:41 UTC
Permalink
Reading between the lines a little, I'm guessing that when you say that you
are trying to "multithread" the application that you are probably trying to
instantiate the third party object in the main thread and then use one or
more worker threads to call into the objects methods and properties. If the
main thread calls CoCreateInstance(), then the main thread is in a
single-threaded apartment. The interface that is returned will therefore
need to be marshaled to the worker thread before the worker thread can use
it. You undoubtedly left out that step. That step is not required if the
main thread is in a multi-threaded apartment, as you have found out. See:
http://www.mvps.org/vcfaq/com/1.htm

It's not immediately clear to me why the third party COM object is failing
when the calling thread is in an MTA. Did you remember to call
CoInitializeEx on the worker thread, and not just the main thread?

Brian

Loading...