Note that there are some explanatory texts on larger screens.

plurals
  1. POError: "bad variable type" in CComVariant::Copy when iterating through CComSafeArray
    text
    copied!<pre><code>CComSafeArray&lt;VARIANT&gt; fields; hr = _tab_file-&gt;get_Fields(fields.GetSafeArrayPtr()); for ( LONG i = fields.GetLowerBound(), ie = fields.GetUpperBound(); i &lt;= ie; ++i) { CComVariant fld = fields.GetAt(i); // (1) raises DISP_E_BADVARTYPE (0x80020008L) // Next code works fine CComQIPtr&lt;ITabField&gt; field = fields.GetAt(i).punkVal; // (2) Ok _bstr_t fieldName; hr = field-&gt;get_Name(fieldName.GetAddress()); ::OutputDebugString(fieldName + _T("\n")); // Ok } </code></pre> <p>Line (1): <code>fields.GetAt(i)</code> returns <code>CComVariant</code>. When I try to assign this value to <code>CComVariant fld</code> called copy constructor and method <code>CComVariant::Copy</code> inside the copy constructor. It raise an exception ("bad variable type", <code>DISP_E_BADVARTYPE (0x80020008L)</code>). At the same time the line (2) works well. What's wrong with line (1), and how to fix it.</p> <p><strong>EDIT:</strong> This is code for <code>get_Field</code> (filling <code>SAFEARRAY</code>).</p> <pre><code>STDMETHODIMP TabFile::get_Fields( SAFEARRAY** fields ) { if(mapInfoFile_ == 0) return E_UNEXPECTED; int fieldCount = getFieldCount(); SAFEARRAY* arr = ::SafeArrayCreateVector(VT_UNKNOWN, 0, fieldCount); for(LONG i = 0; i &lt; fieldCount; i++) { QField* field = getQField(i); ITabField* tabField = TabField::CreateInstance(); tabField-&gt;put_Name(_bstr_t(field-&gt;GetNameRef())); tabField-&gt;put_Type(field-&gt;GetNativeFieldType(i)); ::SafeArrayPutElement(arr, &amp;i, tabField); tabField-&gt;Release(); } *fields = arr; return S_OK; } </code></pre>
 

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