title: Convert/cast SAFEARRAY of IUnknowns to an iterable array of interface pointers
body: <p>I have the following interface in C# with a class with a same name (without I) implementing it.</p>
  
  <pre><code>[ComVisible(true)]
  [Guid("B2B134CC-70A6-43CD-9E1E-B3A3D9992C3E")]
  public interface IOrder
  {
      long GetQuantity();
      long GetOrderType();
      long GetPositionType();
  }
  </code></pre>
  
  <p>The implementation of the public class Order : IOrder is just three private fields and a constructor with required 3 parameters.</p>
  
  <p>Somewhere else, I have the following method with a result with which I want to work inside a C++ unmanaged code, transferred there via COM and .tlb/.tlh files.</p>
  
  <pre><code>public ScOrder[] GetOrders()
  {
      //constant return value for simplicity
      return new Order[] {    
          new Order(1, 2, 3),
          new Order(4, 5, 6)
      };
  }
  </code></pre>
  
  <p>I've already managed to get the basics work between the C++ unmanaged code using the C# managed code.</p>
  
  <p>But class arrays proved to be a different challange...</p>
  
  <p>I admit that for me, the COM is new and brutally confusing and C++ long forgotten ... , but I'm developing both libraries so I'm not giving up; I want the C++ DLL to work as a proxy between some program and my C# code.</p>
  
  <p><strong>Clarification:</strong> I'm using neither MFC nor ATL. I use #import in the C++ code for getting the C# generated interface and class pointers and other COM stuff I don't quite understand yet.</p>
  
  <p>After hour of researching, I'm just going here and beg for help >.&lt;</p>
  
  <p>The following is the C++ code of what I'm trying to achieve.</p>
  
  <pre><code>//this is how the instance of C# gets created, read it from the internets
  //this type has the method GetOrders
  IProxyPtr iPtr;
  CoInitialize(NULL);
  iPtr.CreateInstance(CLSID_Proxy);
  
  IOrderPtr* ordArr; 
  //IOrderPtr is just a pointer to the interface type transferred
  //right? So IOrderPtr* should represent the array of those pointers, right? 
  
  SAFEARRAY* orders;
  iPtr-&gt;GetOrders(&amp;orders);
  </code></pre>
  
  <p>Now at this point, I need some COM magic I don't yet understand to convert that SAFEARRAY* to IOrderPtr* or something so I can iterate over the whole array returned and call the methods of the type "Order"</p>
  
  <ul>
  <li>GetQuantity()</li>
  <li>GetOrderType()</li>
  <li>GetPositionType()</li>
  </ul>
  
  <p>So for the first cycle, I'll get values 1,2,3 and for the second cycle, I'll get values 4,5,6.</p>
  
  <p>Since I'm the author of both C++ and C# library, I can just skip all of this COM crazy stuff and make methods to get the collection count and other methods to get the value of property on certain index.</p>
  
  <p>But that just doesn't seem nice. I suspect the mechanics of what I want are easy but all the answers I found on google are always missing something.   </p>