Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It must be something in the part of your code which you did not show. This works for me:</p> <p>C# class:</p> <pre><code>[ComVisible(true)] [Guid("BE55747F-FEA9-4C1F-A103-32A00B162DF0")] [ClassInterface(ClassInterfaceType.AutoDual)] public class Test { //[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)] public string[] GetStringArray() { var a = new string[3]; a[0] = "string0"; a[1] = null; a[2] = "string2"; return a; } public string[] GetStringArray2() { return null; } } </code></pre> <p>Calling <code>GetStringArray</code> and <code>GetStringArray2</code> from C++:</p> <pre><code>SAFEARRAY* pSA = NULL; testObject-&gt;GetStringArray(&amp;pSA); printf("str0: %ls\n", ((BSTR*)(pSA-&gt;pvData))[0]); printf("ptr1: %x\n", ((BSTR*)(pSA-&gt;pvData))[1]); printf("str2: %ls\n", ((BSTR*)(pSA-&gt;pvData))[2]); SAFEARRAY* pSA2 = NULL; testObject-&gt;GetStringArray2(&amp;pSA2); printf("pSA2: %x\n", pSA2); </code></pre> <p>Run:</p> <pre><code>str0: string0 ptr1: 0 str2: string2 pSA2: 0 </code></pre> <p>I did not have to specify how to marshal the array (the commented out line), because it gets marshaled as <code>SAFEARRAY(VT_BSTR)</code> by default. </p> <p><strong>EDITED:</strong> I guess I see where the problem is. You're using ATL <code>CComSafeArray</code> which doesn't expect a <code>NULL</code> SAFEARRAY by design:</p> <pre><code>CComSafeArray(_In_ const SAFEARRAY *psaSrc) : m_psa(NULL) { ATLASSERT(psaSrc != NULL); HRESULT hRes = CopyFrom(psaSrc); if (FAILED(hRes)) AtlThrow(hRes); } </code></pre> <p>You should change your code like this:</p> <pre><code>CComSafeArray&lt;BSTR&gt; itemEntities; SAFEARRAY* pItemEntities = objController1-&gt;ListItems(sPath); if (NULL != pItemEntities) itemEntities.Attach(pItemEntities); </code></pre> <p>Or, assign <code>m_psa</code> directly:</p> <pre><code>CComSafeArray&lt;BSTR&gt; itemEntities itemEntities.m_psa = objController1-&gt;ListItems(sPath); if (!itemEntities) { // NULL returned } </code></pre>
    singulars
    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.
 

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