title: How to pass COM parameter of type SAFEARRAY** from C#
body: <p>I'm trying to call a COM interface function from C# code.  This is an aggregated COM interface where I load the implementing COM interface by ProgID and I have to declare the interface myself.  The c++ function definition looks like:</p>
  
  <pre><code>STDMETHOD(VisualizeList) (HWND hWndParent, DWORD Options, SAFEARRAY **VialPositions, 
      SAFEARRAY **VialTypes, BSTR* TrayName) = 0;    
  </code></pre>
  
  <p>The SAFEARRAYs contain strings and integers respectively and are used for both input and output.  I've tried defining the function in C# like this:</p>
  
  <pre><code>[PreserveSig]
  int VisualizeList([In] IntPtr hwdParent,
      [In] uint Options, 
      [In, Out][MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_BSTR)] ref String[] VialPositions,
      [In, Out][MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_I4)] ref int[] VialTypes,
      [In, Out] ref String TrayName);
  </code></pre>
  
  <p>But when I attempt to call the interface function, it crashes.  I'm thinking its the SAFEARRAYs because everything else seems relatively straight forward.  I think I need one more order of referencing for the SAFEARRAYs, but I'm not sure how to do that.  So, how do you pass a parameter of type SAFEARRAY** from managed code?  I've scanned online and through other posts, but couldn't find anything that quite fit the bill.  Thanks in advance.</p>