Re: COM interop multithreading problem : Unable to cast COM object of type 'System.__ComObject' to interface



You definately cannot invoke the COM object from an MTA thread.
Ideally you want to just call methods from the STA thread that created
the object. IIRC, you should be able to call from one STA thread to
another if you have a typelib registered which allows the cirrect
marshalling to happen.

ziph...@xxxxxxxxx wrote:
Hi,

I have a DirectShow filter which implements a COM interface called
IDVBSubtitle. It is defined like this on the C++ side (simplified
version used to debug of course, real interface has more methods :)

// C19647D5-A861-4845-97A6-EBD0A135D0BF
DEFINE_GUID(IID_IDVBSubtitle,
0xc19647d5, 0xa861, 0x4845, 0x97, 0xa6, 0xeb, 0xd0, 0xa1, 0x35, 0xd0,
0xbf);

DECLARE_INTERFACE_( IDVBSubtitle, IUnknown )
{
STDMETHOD(Test)(int status) PURE;
};

On the C# side it looks like this:
[Guid( ... ),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDVBSubtitle
{
void Test(int status);
}

When i create an instance of the filter in C# i can call test as much
as i like succesfully, but ONLY from within the same thread that
created it. If i try to call the Test method from any other thread i
get this:
Unable to cast COM object of type 'System.__ComObject' to interface
type 'MediaPortal.Player.IDVBSubtitle'. This operation failed because
the QueryInterface call on the COM component for the interface with IID
'{C19647D5-A861-4845-97A6-EBD0A135D0BF}' failed due to the following
error: No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).

Again, note that this ONLY occurs when the call is made from another
thread than the one that created the filter. I have tried both creating
a seperate C# thread to call Test, and had a callback from the COM
object call Test, both gave the above error.

Reading the thread referenced below lead me to believe this problem is
related to thread apartment states, but unfortunately didnt give me a
solution. I have tried creating the calling thread as both STA and MTA,
but it makes do difference. Could someone give me a hint here, been
stuck on this for a few days?

.



Relevant Pages

  • Re: [STAThread]
    ... Java failed on the Windows client. ... SQL Server and many more expsose ... The unmanaged hosting interface ... 'Apartment' threaded COM objects have STA thread affinity, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Event Sinks Across Apartments
    ... > dialog & then marshals its interface into Global Interface Table ... > I need to implement event sink in the calling thread. ... the dialog that runs in STA thread, but to get it called somehow from ... the caller thread. ...
    (microsoft.public.win32.programmer.ole)
  • COM interop multithreading problem : Unable to cast COM object of type System.__ComObject to interfa
    ... I have a DirectShow filter which implements a COM interface called ... DECLARE_INTERFACE_(IDVBSubtitle, IUnknown) ... If i try to call the Test method from any other thread i ...
    (microsoft.public.dotnet.framework.interop)