title: Passing a c# array of object to COM interface method
body: <p>I should pass a pointer of object array to a COM interface with the following IDL and C++ definitions:
  C++ code:</p>
  <pre><code>UpdateItem( LONG lID, LONG lNumOfFields, FieldIdEnum* pFields, VARIANT* pvValues )
  </code></pre>
  <p>IDL:</p>
  <pre><code>HRESULT UpdateItem( [in] LONG lID, [in] LONG lNumOfFields, [in, size_is(lNumOfFields)] FieldIdEnum* pFields, [in, size_is(lNumOfFields)] VARIANT* pvValues );
  </code></pre>
  <p>tlbimp generated C# code:</p>
  <pre><code>UpdateItem([In] int lID, [In] int lNumOfFields, [In] ref FieldIdEnum pFields, [In][MarshalAs(UnmanagedType.Struct)] ref object pvValues);
  </code></pre>
  <p>... and I try to call with:</p>
  <pre><code>FieldIdEnum[] fieldIDs = { FieldIdEnum.fi1ID, FieldIdEnum.fi2At };
  object[] values = { 3, DateTime.UtcNow };
  mi.UpdateItem(1, 2, ref fieldIDs[0], ref values[0]);
  </code></pre>
  <p>I get this interface from my system as result of login and authentication process... The UpdateItem method is a public member of that interface.</p>
  <p>I want to pass two or more field and value pairs (in same time) by the two array.
  The field array always is passed correctly to C++ code. I have debugged C++ code and the &quot;FieldIdEnum* pFields&quot; represents the first element of an integer array and the 2nd element (pFields<a href="https://stackoverflow.com/questions/15216951/how-to-pass-an-array-from-c-sharp-to-unmanaged-com-by-reference-vt-byref">1</a>) is correct too. The pField[] is a c-style array.</p>
  <p>But the 2nd array is a variant array with various element type in this case an integer and a date time. This appears as a SafeArray at c++ side instead of c_style array. I have checked this 2nd array too, it is a simple variant array if the method calling starts from another C code, but C# tries to marshal it to safearray.</p>
  <p>I have changed the C# definition to</p>
  <pre><code>UpdateItem([In] int lID, [In] int lNumOfFields, [In] ref FieldIdEnum pFields, [In] ref object pvValues);
  </code></pre>
  <p>It does not work I always get an exception like &quot;Value does not fall within the expected range.&quot;    &quot;System.Exception {System.ArgumentException}&quot;</p>
  <p>I have tried to use/marshal various managed/unmanaged types and Intptr, did not work.</p>
  <p>I have found an <a href="https://stackoverflow.com/questions/15216951/how-to-pass-an-array-from-c-sharp-to-unmanaged-com-by-reference-vt-byref">other topic</a> with same/similar problem, I could not make it work.</p>
  <p>How should I pass a pointer of object array to COM interface?</p>