title: Marshalling out array of user-defined type between VB6 and .net
body: <p>I'm creating a COM callable .net assembly and now trying to use it from legacy COM clients (VB6 client in my case).</p>
  
  <p>Assembly should expose API style interface, so typical function declaration would look like this:  </p>
  
  <pre><code>int myRoutine (object inParam, out object result);
  </code></pre>
  
  <p>The problem is when I trying to use function declared as:  </p>
  
  <pre><code>int GetMultipleItems (out ItemData[] itemList);
  </code></pre>
  
  <p>In VB6 this translated to function that have array to be passed as parameter which fails when I'm calling it with 'Invalid procedure call or argument'.</p>
  
  <p>Actual call looks like:  </p>
  
  <pre><code>Dim items() As ItemData  
  result = SCServer.GetMultipleItems (items)
  </code></pre>
  
  <p>Investigated further, I tried several different ways of marking up my library with MarshalAs attributes. From my point of view, the problem is argument has to be array to be passed in and - on the other hand - a variant to be returned to VB code.</p>
  
  <p>After several experiments, I got following to work (1):  </p>
  
  <pre><code>int GetMultipleItems ([Out, MarshalAs (UnmanagedType.SafeArray,
      SafeArraySubType = VarEnum.VT_DISPATCH)]out object[] itemList);  
  </code></pre>
  
  <p>(having items() declared as Object at the client side).  </p>
  
  <p>But I have to use exactly my initial signature (2),  </p>
  
  <pre><code>int GetMultipleItems ([Out, MarshalAs (UnmanagedType.SafeArray,
      SafeArraySubType = VarEnum.VT_DISPATCH)]out ItemData[] itemList); 
  </code></pre>
  
  <p>Which does not work either with Object or with ItemData array type declaration at client.</p>
  
  <p>So, questions are:<br>
   - why (1) works while (2) not. What should I change in attribute declaration or in client to get to work with my actual type in signature (it exposes an interface which has been exported to tlb as well, so seems all should be OK here)<br>
   - what is recommended way to define sugh [out] parameter arrays<br>
   - maybe I lack some essential reading, I would be grateful for the links... still I need to get the sample working in a day or two.  </p>
  
  <p>Thanks in advance.</p>