title: Sending and receiving arrays over COM
body: <p>What is the right way to receive and send arrays over COM?  Here's my attempt so far: a safearray of doubles wrapped in a variant.</p>
  
  <pre><code>//takes variant holding safearray of doubles
  //returns a similar variant having multipled every element by 2
  STDMETHODIMP MyComClass::safearraytimestwo(VARIANT in, VARIANT* out)
  {
      CComSafeArray&lt;double&gt; sa_in;
      sa_in.Attach(*in.pparray);
      ULONG size = sa_in.GetCount();
  
      CComSafeArray&lt;double&gt; *out_sa = new CComSafeArray&lt;double&gt;(size);
  
      for (long i=0;i&lt;size;i++)
          out_sa-&gt;SetAt(i,sa_in[i]*2);
  
      out = new CComVariant(out_sa);
      return S_OK;
  }
  </code></pre>
  
  <p>Problems:
   - currently compilation fails on the loop operation: <code>error C2679: binary '=' : no operator found which takes a right-hand operand of type 'ATL::_ATL_AutomationType&lt;DOUBLE&gt;::_typewrapper' (or there is no acceptable conversion)</code> <strong>edit: solved using <code>SetAt()</code> instead of <code>operator[]</code></strong>
   - Should I be declaring <code>out_sa</code> on the heap?  Will it get deallocated when <code>out</code> gets deallocated (which I can only presume the client will do?)</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>Edit 2:</strong> here is a partial implementation that tries just to return a safearray.</p>
  
  <pre><code>STDMETHODIMP CSpatialNet::array3(VARIANT in, VARIANT* out)
  {
      CComSafeArray&lt;double&gt; out_sa;
      out_sa.Create(2);
      out_sa.SetAt(0,1.2);
      out_sa.SetAt(1,3.4);
      *out = CComVariant(out_sa);
      out_sa.Detach();
      return S_OK;
  }
  </code></pre>
  
  <p>This also fails; lisp reports </p>
  
  <pre><code>(vl-load-com)
  (setq n (vlax-create-object "sdnacomwrapper.SpatialNet"))
  (setq v (vlax-make-variant 1.0))
  (vlax-invoke-method n 'array3 v 'newvar)
  ; error: ActiveX Server returned an error: The parameter is incorrect
  </code></pre>
  
  <p>Replacing <code>CComSafeArray&lt;double&gt;</code> with an array of variants produces the same error.</p>