将SAFEARRAY数据转换为未签名的字符*

时间:2019-06-21 11:03:34

标签: winapi com safearray

我正在尝试将SAFEARRAY数据指针转换为unsinged char*。但是我没有得到预期的数据。这是一个片段。

SafeArrayLock(psaFrameData);
psaFrameData->rgsabound->cElements;
int nCount = psaFrameData->rgsabound->cElements -   psaFrameData->rgsabound->lLbound + 1;
frameData = new unsigned char[nCount];
memset(frameData, 0, nCount);

for (int i = 0; i < nCount; ++i)
{
    frameData[i] = ((unsigned char*)(psaFrameData)->pvData)[i];
}    
SafeArrayUnlock(psaFrameData);

1 个答案:

答案 0 :(得分:0)

请勿手动锁定数组,然后直接访问其pvData(或其任何其他数据成员)。请改用各种访问器函数,例如SafeArrayAccessData()

  

增加数组的锁计数,并检索指向数组数据的指针。

尝试更多类似的方法:

// safety check: make sure the array has only 1 dimension...
if (SafeArrayGetDim(psaFrameData) != 1)
{
    // handle the error ...
}
else
{
    // safety check: make sure the array contains byte elements...
    VARTYPE vt = 0;
    SafeArrayGetVartype(psaFrameData, &vt);
    if (vt != VT_UI1)
    {
        // handle the error ...
    }
    else
    {
        // get a pointer to the array's byte data...
        unsigned char *data;
        if (FAILED(SafeArrayAccessData(psaFrameData, (void**)&data)))
        {
            // handle the error ...
        }
        else
        {
            // calculate the number of bytes in the array...
            LONG lBound, uBound;
            SafeArrayGetLBound(psaFrameData, 1, &lBound);
            SafeArrayGetUBound(psaFrameData, 1, &uBound);
            long nCount = uBound - lBound + 1; 

            // copy the bytes...
            frameData = new unsigned char[nCount];
            memcpy(frameData, data, nCount);

            // release the pointer to the array's byte data...
            SafeArrayUnaccessData(psaFrameData);
        }
    }
}