title: Passing an object from C++ to C# though COM
body: <p>hi have a COM visible API in C# which looks like the following:</p>
  
  <pre><code>public void DoSomething(string par1, string par2, object additionalParameter)
  </code></pre>
  
  <p>The idea is that based on the value of the string parameters I expect a different class as the third parameter and cast it appropriately in the implementation (I know this design is not optimal but I don't have much flexibility here).</p>
  
  <p>Suppose that for some combination of the string parameters the type of the additional parameter is the following:</p>
  
  <pre><code>[ComVisible(true)]
  [Serializable]
  public class TestObject    
  {
      public string String{ get; set; }
  
      public long Long { get; set; }
  }
  </code></pre>
  
  <p>I need to my API method from some unmanaged code; however I am having difficulties in creating the a proper <code>variant</code> needed for the third parameter.  </p>
  
  <p>I am using the <code>CComVariant(...)</code> passing an <code>IDispatch</code> pointing to a TestObject that I have just built. </p>
  
  <p>Suppose that <code>pTestObject</code> is an IDispatch pointer to my TestObject, I have something like the following:</p>
  
  <pre><code>CComVariant pObjectVariant(pTestObject);
  DoSomething(BSTR(L"first"), BSTR(L"second"), pObjectVariant);
  </code></pre>
  
  <p>However, when the C# function is finally invoked, I see that the object has type <code>bool</code> instead of <code>TestObject</code> that I was expecting. </p>
  
  <p>Any idea?</p>
  
  <p>Stefano</p>