0

设置:
我有一个 COM DLL,它调用托管 C# DLL 中的一个方法。此函数返回一个 C# string[] 数组,该数组被封送为 SAFEARRAY。

问题:
当我尝试访问安全数组中的字符串时,我只得到字符串的第一个字符。我究竟做错了什么?

编码:

    // Pointer to the managed interface
    DatabasePtr pODB(__uuidof(DBClass));

    // Get the string[] array from the managed method
    SAFEARRAY* safearray = pODB->GetStringArray();

    HRESULT hresult;

    long ubound;
    long lbound;

    hresult = SafeArrayGetUBound(safearray, 1, &ubound);
    hresult = SafeArrayGetLBound(safearray, 1, &lbound);

    long index;
    BSTR fromarray;

    for (; lbound <= ubound; lbound++)
    {
        index = lbound;

        hresult = SafeArrayGetElement(safearray, &index, (void*)&fromarray);

        char buffer[512];
        sprintf_s(buffer,"%s",fromarray);

        MessageBox(0, (LPCSTR)buffer, "...", 0);
    }

谢谢你的帮助,-
肖恩!

4

1 回答 1

2

BSTR 是一个 unicode 字符串,因此您必须使用wchar_t缓冲区和wsprintf_s. 现在你打印第一个 unicode 字符的 ANSI 部分,然后在 \0 上停止。请,请不要像那样堆叠溢出(原文如此!)。使用保险箱_vsnwprintf_s_l及其系列,您的代码就像现在一样是黑客的乐趣,您将被击败。请参阅http://msdn.microsoft.com/en-us/library/d3xd30zz(VS.80).aspx

于 2009-05-17T23:27:28.457 回答