Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing C++ strings by reference
#15
C++
Code:
Copy      Help
#include <oleauto.h>
typedef SAFEARRAY* ARRAY; //QM ARRAY in C++ is SAFEARRAY*

//Creates or resizes 1-dim ARRAY.
//vt - array type VT_ constant. VT_I4 for int, VT_BSTR for BSTR, etc. Used only when creating.
//Sets added elements to 0/empty. Frees removed elements if need (BSTR, VARIANT etc).
extern "C" __declspec(dllexport)
HRESULT ResizeSafeArray(ARRAY& a, int n, int vt)
{
,HRESULT hr;
,SAFEARRAYBOUND b={n};
,//int nOld=0;
,if(a) //resize
,{
,,//nOld=a->rgsabound[0].cElements;
,,if(hr=SafeArrayRedim(a, &b)) return hr;
,}
,else //create
,{
,,a=SafeArrayCreate(vt, 1, &b);
,,if(!a) return E_OUTOFMEMORY;
,}

,return 0;
}

QM
Macro Macro2114
Code:
Copy      Help
dll "test.dll" #ResizeSafeArray ARRAY*a n vt

ARRAY(int) a
if(ResizeSafeArray(&a 3 VT_INT)) end "error"
for(_i 0 a.len) a[_i]=_i
if(ResizeSafeArray(&a 4 VT_INT)) end "error"
for(_i 0 a.len) out a[_i]

ARRAY(BSTR) b
if(ResizeSafeArray(&b 3 VT_BSTR)) end "error"
for(_i 0 b.len) b[_i]=F"string {_i}"
if(ResizeSafeArray(&b 4 VT_BSTR)) end "error"
for(_i 0 b.len) out b[_i]


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)