使用 COM 传递数组?

2022-01-14 00:00:00 com c++

我是一个用 ATL 编写的 COM 对象,用于 C++ 应用程序,我想在两者之间传递一个 BYTE 数组.到目前为止,我对 COM/IDL 的体验仅限于传递简单类型(BSTR、LONG 等).

I am a COM object written in ATL that is used from a C++ application, and I want to pass an array of BYTEs between the two. My experience of COM/IDL so far is limited to passing simple types (BSTRs, LONGs, etc.).

有没有一种相对简单的方法可以让 COM 对象将数组传递给调用者?例如,我想传递原始图像 (TIFF) 而不是弄乱临时文件.

Is there a relatively easy way to have the COM object pass an array to the caller? For example, I want to pass a raw image (TIFF) instead of messing with temporary files.

推荐答案

尝试将 safearray 变体传递给 COM 对象.像这样将 BYTE 数组放入 safearray 变体中......

Try passing a safearray variant to the COM Object. Something like this to put a BYTE array inside a safearray variant....

bool ArrayToVariant(CArray<BYTE, BYTE>& array, VARIANT& vtResult)
{
SAFEARRAY FAR*  psarray;
SAFEARRAYBOUND sabounds[1]; 

sabounds[0].lLbound=0;
sabounds[0].cElements = (ULONG)array.GetSize();

long nLbound;

psarray = SafeArrayCreate(VT_UI1, 1, sabounds);
if(psarray == NULL)
    return false;

for(nLbound = 0; nLbound < (long)sabounds[0].cElements ; nLbound++){
    if(FAILED(SafeArrayPutElement(psarray, &nLbound, &array[nLbound]))){
        SafeArrayDestroy(psarray);
        return false;
    }
}

VariantFree(vtResult);
vtResult.vt = VT_ARRAY|VT_UI1;
vtResult.parray = psarray;
return true;
}

相关文章