COM interface use causes InvalidCastException in parallel foreach

2

I need a C# loop to call a function on a COM object repeatedly. A single-threaded one works but the parallel version throws...

foreach (Stub stub in inputStubs) { dc.Work(stub); }   // ok

Parallel.ForEach(inputStubs, stub => dc.Work(stub) ); // throws

In both cases, I can hit a breakpoint inside Work() so it is being called. The exception still occurs when all the functionality in Work() is commented out. The exception looks like this:

Exception thrown: 'System.InvalidCastException' in my.dll

Additional information: Unable to cast COM object of type 'System.__ComObject'
to interface type 'my.IDC'. This operation failed because the QueryInterface
call on the COM component for the interface with IID
'{99E2873B-4042-4F24-A680-2882E4C869DD}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

The C# definition for the COM object starts like this.

[ComImport, Guid("99E2873B-4042-4F24-A680-2882E4C869DD"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
SuppressUnmanagedCodeSecurity()]
public interface IDC
{

On the C++ side, the boilerplate stuff boils down to

interface __declspec(uuid("99E2873B-4042-4F24-A680-2882E4C869DD"))
IDC : IUnknown
{

and

class CDC : public IDC
{
     private:
         LONG refcount;

     public:

     CDC() { refcount = 1; }
     virtual ~CDC() {}

     virtual HRESULT __stdcall QueryInterface(REFIID riid, void** p)
     {
         if(riid == __uuidof(IUnknown)) { *p = static_cast<IUnknown*>(this); AddRef(); return 0; }
         if(riid == __uuidof(IDC))          { *p = static_cast<T*>(this);   AddRef(); return 0; }
         return E_NOINTERFACE;
     }

     virtual ULONG __stdcall AddRef() 
     {
         return InterlockedIncrement(&refcount);
     }

     virtual ULONG __stdcall Release() 
     { 
         ULONG count = InterlockedDecrement(&refcount);
         if(!count) 
            delete this;  
         return count;
     }

What could be causing this?

c#
c++
com
parallel.foreach
asked on Stack Overflow Dec 7, 2016 by tombola

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0