Detecting cross-thread marshaling by COM RCW objects in C# Detecting cross-thread marshaling by COM RCW objects in C# multithreading multithreading

Detecting cross-thread marshaling by COM RCW objects in C#


You should be able to accomplish this by testing whether you can get to the IMarshal interface, which should be aggregated into the proxy if the call is a cross-apartment call. First, you will need to declare IMarshal somewhere in your project:

  [System.Runtime.InteropServices.InterfaceTypeAttribute(1)]  [System.Runtime.InteropServices.Guid("00000003-0000-0000-C000-000000000046")]  public interface IMarshal  {     // no methods needed, just querying for the interface  }

Then, you can test for the interface like so.

  if (suspiciousComInterface is IMarshal)     // cross-apartment call  else     // direct call