我的 IDL 中有以下内容,MIDL 似乎很喜欢。我正在尝试创建一个返回字节数组数组(不是矩阵)的方法。我在 C++ 方面遇到问题,编译器不喜欢 ATL::CComSafeArray<LPSAFEARRAY>ATL::CComSafeArray<ATL::CComSafeArray<byte> >。我得到一个 error C2039: 'type' : is not a member of 'ATL::_ATL_AutomationType<T>'

[id(1001)] HRESULT Find([out] SAFEARRAY(SAFEARRAY(byte))* features);

STDMETHOD(Find)(/*[out]*/ SAFEARRAY** features)

有没有更好的方法来传递字节数组(我正在跨越进程边界)?

最佳答案

好的,试试这个(C++ ATL EXE 服务器):

[
    object,
    uuid(F7F8E65F-EBAD-4191-B916-2DB2A1EA0720),
    dual,
    nonextensible,
    helpstring("IAofA Interface"),
    pointer_default(unique)
]
interface IAofA : IDispatch{
    [id(1), helpstring("method GetArray")] HRESULT GetArray([out,retval] VARIANT* pVals);
};

CComVariant CreateByteArrayAsVariant(BYTE *pData, ULONG size)
{
    CComSafeArray<BYTE> sa(size);
    for (ULONG i=0; i!=size; ++i)
    {
        sa.SetAt(i, pData[i]);
    }

    CComVariant var(sa);

    return var;
}


STDMETHODIMP CAofA::GetArray(VARIANT* pVals)
{
    CComSafeArray<VARIANT> sa(3);

    BYTE a[] = {1};
    BYTE b[] = {1, 2};
    BYTE c[] = {3, 2, 1};

    sa.SetAt(0, CreateByteArrayAsVariant(a, sizeof(a)));
    sa.SetAt(1, CreateByteArrayAsVariant(b, sizeof(b)));
    sa.SetAt(2, CreateByteArrayAsVariant(c, sizeof(c)));

    CComVariant var(sa);

    var.Detach(pVals);

    return S_OK;
}

这是一个 VB.net 客户端:
Imports AofAServerLib

Module Module1

    Sub Main()
        Dim s As IAofA = New AofA
        Dim a() = s.GetArray

        For i As Integer = 0 To a.Length - 1
            For j As Integer = 0 To a(i).Length - 1
                Console.Write(a(i)(j))
                Console.Write(" ")
            Next
            Console.WriteLine()
        Next
    End Sub

End Module

没有错误检查,但它的工作原理。

关于c++ - SafeArray 的 COM SafeArray,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11829424/

10-13 04:18