簡體   English   中英

將托管(C#)字符串[]數組傳遞到COM DLL

[英]Passing a managed (C#) string[] array to a COM DLL

設定:
我有一個COM DLL,可在托管C#DLL中調用方法。 該函數返回一個C#string []數組,該數組被封送到SAFEARRAY。

問題:
當我嘗試訪問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);
    }

謝謝你的幫助,
-Sean!

BSTR是一個unicode字符串,因此您必須使用wchar_t緩沖區和wsprintf_s 現在,您打印第一個Unicode字符的ANSI部分,然后停在\\ 0上。 而且請,請不要那樣堆疊堆棧(原文如此!)。 使用安全的_vsnwprintf_s_l及其家族,您的代碼很容易受到黑客的歡迎,因為現在您可以將其偽裝。 參見http://msdn.microsoft.com/zh-cn/library/d3xd30zz(VS.80).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM