access a com object from different threads

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
W

Willy Denoyette [MVP]

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.
 
F

Fabian Härle

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
 
F

Fabian Härle

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top