title: VirtualBox COM API from C#
body: <p>I am trying to use the VirtualBox COM API (VBoxC.dll) from C#.  I ran</p>
  
  <pre><code>tlbimp VirtualBox.tlb
  </code></pre>
  
  <p>against the typelib included in the VirtualBox SDK. Referencing the output assembly builds OK but at runtime I get a <code>SafeArrayTypeMispatchException</code> ("Specificed array was not of the expected type") whenever I try to access properties that return arrays.</p>
  
  <p>I can see, for example, that <code>IVirtualBox.get_Machines()</code> is defined as</p>
  
  <pre><code>[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_DISPATCH)]
  [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime), DispId(0x60020009)]
  public virtual extern IMachine[] get_Machines();
  </code></pre>
  
  <p>How can I get this call to marshall correctly?</p>