08-07-2013, 05:38 AM
C++
QM
Macro Macro2114
#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
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]