Search code examples
c++safearray

How do you get the underlying SAFEARRAY from a CComSafeArray?


I tried the GetSafeArrayPtr() method which returns a LPSAFEARRAY* that a typedef defined as:

typedef /* [wire_marshal] */ SAFEARRAY *LPSAFEARRAY;

I thought I would be able to directly assign this to a SAFEARRAY* variable but the compiler gives this error:

error C2440: '=' : cannot convert from 'LPSAFEARRAY *' to 'SAFEARRAY *'

I found this strange. What am I doing wrong here?

PS: I am doing this inside a C++/CLI dll (if that is of any relevance).


Solution

  • LPSAFEARRAY * is a pointer to SAFEARRAY *, so you need a double pointer, like this:

    {
        CComSafeArray<VARIANT> vArray;
        SAFEARRAY** pArray;
        pArray = vArray.GetSafeArrayPtr();
    }
    

    And then you can pass the SAFEARRAY * to the function that needs it as an argument by dereferencing the pointer returned from CComSafeArray:

    DummyFunction(*pArray);