Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing C++ strings by reference
#13
Macro Macro2009
Code:
Copy      Help
dll "..." !myfunc ARRAY(BSTR)a

ARRAY(BSTR) b.create(5)
out myfunc(b)
ARRAY(str) a.create(b.len); for(_i 0 a.len) a[_i]=b[_i] ;;tip: create function for this
out a

extern "C" __declspec(dllexport)
bool myfunc(SAFEARRAY* a) //ARRAY(BSTR)
{
if(!a || !(a->fFeatures&FADF_BSTR) || a->cDims!=1 || a->rgsabound[0].lLbound || a->cLocks) return 0;
BSTR* p=(BSTR*)a->pvData, *pe=p+a->rgsabound[0].cElements;
for(; p<pe; p++) *p=SysAllocString(L"test");
return true;
}

It is possible to make calling the function simpler. For example, set a callback function that allocates array and converts BSTR to str. Then caller will not have to do it.


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)