winapi - 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);
解决方案
不要手动锁定数组,然后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);
}
}
}
推荐阅读
- kubernetes - 了解删除有状态集
- python - 如何在mac上检测用户登录失败?
- apache-spark - Aws Emr Spark 使用胶水作为配置单元元存储?
- memory - 估计 AWS Neptune 上的图形数据库大小
- flutter - 如何在颤动中创建一个始终粘在页面底部的按钮?
- javascript - 几个图在flot中具有相同的图例
- ais - 如何根据 AIS 140 标准计算校验和
- python - 如何使用 igraph 将 numpy 数组转换为图形?
- rabbitmq - 业务数据在 RabbitMQ 消息中的位置
- audio - ffmpeg 默认音频编解码器,而不是使用 acodec 选项指定它