Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing a CComSafeArray across a COM boundary
    primarykey
    data
    text
    <p>I'm trying to (i) get a safearray of longs across a COM boundary and (ii) use CComSafeArray for convenience.</p> <p>My problem is that I'm getting unpredictable crashes after setting the COM property (see <strong>pPrologue->EligibleProducts = var;</strong> below). I've found it very hard to understand how to use CComSafeArray from the Microsoft documentation, can anyone shed any light? Thank you in advance!</p> <p>In the IDL I have:</p> <pre><code>[propget, id(1), helpstring("property EligibleProducts")] HRESULT EligibleProducts([out, retval] VARIANT* pVal); [propput, id(1), helpstring("property EligibleProducts")] HRESULT EligibleProducts([in] VARIANT newVal); </code></pre> <p>My server code is:</p> <pre><code>STDMETHODIMP CPrologue::put_EligibleProducts(VARIANT newVal) { HRESULT hr = E_FAIL; AFX_MANAGE_STATE(AfxGetStaticModuleState()) //start by clearing out any existing data m_EligibleProducts.clear(); if(newVal.vt | (VT_ARRAY &amp; VT_I4)) { //construct a wrapper class with the passed in SAFEARRAY CComSafeArray&lt;long&gt; wrapper; wrapper.Attach(newVal.parray); int iProductID = 0; //loop through products and add them to our vector int iCount = wrapper.GetCount(); for(int iIndex = 0; iIndex &lt; iCount; iIndex++) { iProductID = wrapper.GetAt(iIndex); if(iProductID &gt; 0) { m_EligibleProducts.push_back(iProductID); } } hr = S_OK; return hr; } </code></pre> <p>And my calling code is:</p> <pre><code> VARIANT var; ::VariantInit(&amp;var); var.vt = VT_ARRAY | VT_I4; CComSafeArray&lt;long&gt; wrapper; for(std::vector&lt;long&gt;::const_iterator it = products.begin(); it != products.end(); it++) { wrapper.Add(*it); } //get the SAFEARRAY from the wrapper var.parray = wrapper.Detach(); //and store it on the appropriate business object IProloguePtr pPrologue = pCustomer-&gt;Prologue; **pPrologue-&gt;EligibleProducts = var;** //clean up the variant (and hence SAFEARRAY) ::VariantClear(&amp;var); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload