这很可能有一个非常简单的答案,但我无法弄清楚。

我正在尝试重构一些看起来像这样的代码:

SAFEARRAY* psa;
long* count;
HRESULT hr = pSomeInterface->ListSomething(&psa, &count);
if (SUCCEEDED(hr))
{
    CComSafeArray<BSTR> sa;
    if (*count > 0)
    {
        sa.Attach(psa);
    }
}

// perform operations on sa
// allow CComSafeArray to destroy the object

return hr;

我想将代码更改为:
CComSafeArray<BSTR> sa;
long* count;
hr = pSomeInterface->ListSomething(&(sa.m_psa), &count);

if (SUCCEEDED(hr))
{
    // perform operations on sa
}

但是当我执行这个时,sa 包含垃圾。发生了什么,为什么?什么是正确的语法?

最佳答案

您应该使用 CComSafeArray<T>:: GetSafeArrayPtr() 。但是 Aamir 使用 &(sa.m_psa) 的方式也应该有效。

关于com - 将 CComSafeArray 传递给需要 SAFEARRAY 的方法时正确的语法是什么**,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/445286/

10-11 04:03