I am in a situation where I have a COM object that I need to use in come windows only C++ code. The COM object has functions that accept SAFEARRAYs as arguments to pass arrays of bytes. After looking at the SAFEARRAY API ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms221145(v=vs.85).aspx ) I decided it wasn’t what I wanted and that I should find an object oriented wrapper. I tried looking for open source ones and I didn’t find any. I found that microsoft has created two objects that seem to encapsulate SAFEARRAY. It looks like the CComSafeArray is exactly what I need, and like the the COleSafeArray might be useful but could exist only for legacy compatibility.
Is COleSafeArray just around for historical compatibility or is there something I am missing?
When should COleSafeArray be used instead of CComSafeArray?
Are there any open source implementations that might be worth looking into?
What are the Pros and Cons of each?
The difference is obvious from class names.
MFC
COleSafeArrayis designed to support OLE Automation and is actually wrapper for OLEVARIANTstruct (which can containSAFEARRAY). It works generally with array elements as they are ofVARIANTtype so you need to select & extract appropriate type manually.ATL
CComSafeArrayis designed to supportSAFEARRAYfor generic COM and is actually wrapper forSAFEARRAYstruct. It’s template class parametrized with array elements type.In general you shall use
CComSafeArray, it’s easier and simplier, accessing elements almost same way as for regular arrays/vectors.COleSafeArraymay be preferable sometimes if you work with OLE Automation interfaces that intensively useVARIANTparameters, e.g. automating MS Office, using Visual Basic components etc. For comparision in this case withCComSafeArrayyou will need to wrap/unwrap it manually to/fromVARIANTobject.