title: SAFEARRAY of COM objects
body: <p>I'm using COM library and I have interface defined in .tlh file as following:</p>
  
  <pre><code>_COM_SMARTPTR_TYPEDEF(IMyInterface, __uuidof(IMyInterface));
  </code></pre>
  
  <p>Then I create object:</p>
  
  <pre><code>//1st object
  IMyInterfacePtr pMyInterface1;
  pMyInterface1.CreateInstance(CLSID_MyInterface);
  pMyInterface1-&gt;call_some_method(BSTR("pass example text1"));
  
  //2nd object
  IMyInterfacePtr pMyInterface2;
  pMyInterface2.CreateInstance(CLSID_MyInterface);
  pMyInterface2-&gt;call_some_method(BSTR("pass example text2"));
  </code></pre>
  
  <p>Then I need to create SAFEARRAY of these objects:</p>
  
  <pre><code>SAFEARRAYBOUND rgsabound[1];
  rgsabound[0].cElements = 2;
  rgsabound[0].lLbound = 0;
  
  SAFEARRAY *pData = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
  
  LONG i = 0;
  SafeArrayPutElement(pData, &amp;i, pMyInterface1);
  i = 1;
  SafeArrayPutElement(pData, &amp;i, pMyInterface2);
  </code></pre>
  
  <p>But unfortunately after this array elements remains empty. Which is the proper way to fill this array of IMyInterfacePtr objects?</p>
  
  <p>Thank you so much for help.</p>
  
  <p>------<strong><em>EDIT</em></strong>------</p>
  
  <p>Thanks for answers. I tried these solutions from comments, but unfortunately, it still doesn't work. I need to send back this array to COM by COM method. So I'm creating a new object:</p>
  
  <pre><code>IResponsePtr pResponse;//This is also smart com ptr
  pResponse.CreateInstance(CLSID_Response);
  pResponse-&gt;put_Response(pData);//safearray here
  </code></pre>
  
  <p>where <code>put_Response</code> has following signature: <code>(SAFEARRAY *value)</code>.
  Unfortunately, during calling this method I'm getting the following exception:</p>
  
  <pre><code>First-chance exception at 0x76BEC54F in MyApp.exe: Microsoft C++ exception: EEException at memory location 0x0042F144.
  First-chance exception at 0x76BEC54F (KernelBase.dll) in MyApp.exe: 0xE0434352 (parameters: 0x80131533, 0x00000000, 0x00000000, 0x00000000, 0x72D30000).
  </code></pre>
  
  <p>Any ideas what might be wrong with this SAFEARRAY ptr?</p>