0

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).

bobbyalex
  • 2,681
  • 3
  • 30
  • 51

1 Answers1

2

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);
Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • But I cannot change the definition of the SAFEARRAY variable. Is there any other way I can get the SAFEARRAY to assign to SAFEARRAY* pArray? – bobbyalex Sep 03 '13 at 08:52
  • I did not quite understand what are you trying to achieve. What do you mean by "cannot change the definition" ? If you want to modify the data you can do that already through the `CComSafeArray`, also via the `LPSAFEARRAY*` – Rudolfs Bundulis Sep 03 '13 at 09:16
  • I mean I need to pass the safearray to a method that takes SAFEARRAY* as a parameter. Its an external API so I cannot change it. – bobbyalex Sep 03 '13 at 09:19
  • Ok, so going forth from this example, why can't you just dereference the pointer you get from `GetSafeArrayPtr()` and pass it to the method? Like `*pArray`. – Rudolfs Bundulis Sep 03 '13 at 09:29