Click to See Complete Forum and Search --> : How to deal with binary data?


john101
June 1st, 1999, 11:46 AM
I need to build a control which has a function that returns a binary data array. The function will be called in VB. What data type should I use in VC++?

Note, the binary data array size is dynamic. If I dynamically allocates memory, where should I delete the memory?

Any smaple codes are greatly appreciated.

Thanks.

David L Hill
June 1st, 1999, 12:44 PM
I use a variant safearray when I need to pass an array of bytes.


// initialize variant
VARIANT vData;
VariantInit(&vData);

// create safearray
SAFEARRAY* psa = NULL;
SAFEARRAYBOUND rgsabound;
rgsabound.lLbound = 0;
rgsabound.cElements = lNumBytes;
psa = SafeArrayCreate(VT_UI1, 1, &rgsabound);

// access safearray
BYTE HUGEP* pArrayData;
SafeArrayAccessData(psa, (void HUGEP**) &pArrayData);
// fill array
memcpy(pArrayData, pData, lNumBytes);
// release
SafeArrayUnaccessData(psa);

// return
vData.vt = VT_UI1 | VT_ARRAY;
vData.parray = psa;




The caller (VB) is responsible to release memory allocated.

john101
June 1st, 1999, 01:11 PM
Thank you.
I still have a question.
Should the function prototype be:
STDMETHOD(Flush)(/*[out, retval]*/ VARIANT* buf);
or
STDMETHOD(Flush)(/*[out, retval]*/ SAFEARRAY* buf); ?

Can you recommend a good book on this?

Thanks.

David L Hill
June 1st, 1999, 01:35 PM
Use the VARIANT* parameter type.


I have found the following books very helpful.

Beginning ATL COM Programming by Dr. Richard Grimes and others from Wrox Press

Professional ATL COM Programming by Dr. Richard Grimes from Wrox Press

Essential COM by Don Box from The Addison-Wesley Object Technology Series