access a com object from different threads

  • Thread starter Thread starter Fabian H?rle
  • Start date Start date
F

Fabian H?rle

Hi,

I have an STA COM object and I need to access it from different
threads. I get the obligatory "QueryInterface for interface xyz
failed." exception.
Is there any possibility apart from using Invoke on a hidden form
thread? Can I perhaps somehow create a class with a message loop?

Thanx for your help,

Fabian
 
Fabian,

If the COM object is a STA threaded object, then you can not just pass
it between threads. What you need to do is marshal the interface from the
thread it was created on, to the thread that you want to call it on. In
order to do this, you will need to use the global interface table
(accessible through the P/Invoke layer and COM interop). Check out the
section of the Platform SDK titled "Accessing Interfaces Across Apartments",
located at (watch for line wrap):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/aptnthrd_2eer.asp

This will give you the interfaces and functions you need to call to
marshal the interface between threads. You will have to use interop, but
everything you need is there.

Hope this helps.
 
The CLR is handling the necessary marshalling of interfaces when COM object
references are used from other threads than the creating thread, this should
work.
One possible cause is that the tlb is not registered.

Willy.
 
Hi Nicholas,

thanks a lot for the link to the MS Doc. I just read about how to create a
global interface
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/ap
tnthrd_4ew5.asp). This means, that I have to modify the COM Object's code,
doesn'it? Now, the problem is, that the object belongs to a third party
library - and I can' change it. Is there another possibility?

best regards,

Fabian

Nicholas Paldino said:
Fabian,

If the COM object is a STA threaded object, then you can not just pass
it between threads. What you need to do is marshal the interface from the
thread it was created on, to the thread that you want to call it on. In
order to do this, you will need to use the global interface table
(accessible through the P/Invoke layer and COM interop). Check out the
section of the Platform SDK titled "Accessing Interfaces Across Apartments",
located at (watch for line wrap):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/aptnthrd_2eer.asp

This will give you the interfaces and functions you need to call to
marshal the interface between threads. You will have to use interop, but
everything you need is there.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fabian H?rle said:
Hi,

I have an STA COM object and I need to access it from different
threads. I get the obligatory "QueryInterface for interface xyz
failed." exception.
Is there any possibility apart from using Invoke on a hidden form
thread? Can I perhaps somehow create a class with a message loop?

Thanx for your help,

Fabian
 
Hi Willy,
One possible cause is that the tlb is not registered.

I can import the library via

Add Reference --> COM

without tlbimp.

Doesn't this mean, that the library is registered correctly?

Regards,

Fabian
 
Back
Top