在包含 SAFEARRAY 的 VARIANT 上调用 VariantClear() 时会引发异常

发布于 2024-09-09 17:26:02 字数 1138 浏览 5 评论 0原文

我试图将 BYTES 数组中的一些数据包装到 VARIANT 中,但我似乎无法释放数据:

当我运行此代码时...

SAFEARRAY * NewSArray;

SAFEARRAYBOUND aDim[1]; // a one dimensional array
aDim[0].lLbound = 0; //Sets the index to start from 0

//Sets the number of elements (bytes) that will go into the SAFEARRAY
aDim[0].cElements = pBuffer->GetSize();

NewSArray = SafeArrayCreate(VT_UI1, 1, aDim); // create a 1D SafeArray of BYTES

//Put the data from the man view into the SAFEARRAY
NewSArray->pvData = pBuffer->GetBuffer();

//FP Spread expects the spreadsheet data in the form of a VARIANT so we must pack the data from the SAFEARRAY into a
//VARIANT
VARIANT SpreadsheetBuffer;
VariantInit(&SpreadsheetBuffer);

SpreadsheetBuffer.vt= VT_ARRAY | VT_UI1; // set type to an array of bytes
SpreadsheetBuffer.parray= NewSArray;

try
{
    VariantClear(&SpreadsheetBuffer);
}
catch (char *str)
{
    AfxMessageBox(str);
}

我收到此消息: “在...0xC015000F中...处出现未处理的异常:正在停用的激活上下文不是最近激活的上下文。”

顺便说一句,这条消息不会在我的 AfxMessageBox 中弹出。它似乎与变体类型有关,因为如果我不设置它,我就不会得到异常。 pBuffer 中的数据只是之前从 SAFEARRAY 中拉出的 BYTE 数组。

有人知道我做错了什么吗?

谢谢

I am trying to wrap up some data from an array of BYTES into a VARIANT but I can't seem to free the data:

When I run this code...

SAFEARRAY * NewSArray;

SAFEARRAYBOUND aDim[1]; // a one dimensional array
aDim[0].lLbound = 0; //Sets the index to start from 0

//Sets the number of elements (bytes) that will go into the SAFEARRAY
aDim[0].cElements = pBuffer->GetSize();

NewSArray = SafeArrayCreate(VT_UI1, 1, aDim); // create a 1D SafeArray of BYTES

//Put the data from the man view into the SAFEARRAY
NewSArray->pvData = pBuffer->GetBuffer();

//FP Spread expects the spreadsheet data in the form of a VARIANT so we must pack the data from the SAFEARRAY into a
//VARIANT
VARIANT SpreadsheetBuffer;
VariantInit(&SpreadsheetBuffer);

SpreadsheetBuffer.vt= VT_ARRAY | VT_UI1; // set type to an array of bytes
SpreadsheetBuffer.parray= NewSArray;

try
{
    VariantClear(&SpreadsheetBuffer);
}
catch (char *str)
{
    AfxMessageBox(str);
}

I get this message:
"Unhandeled exception at ... in ... 0xC015000F: The activation context being deactivated is not the most recently activated one."

This message dosen't pop up in my AfxMessageBox by the way. It seems to have something to do with the variant type because if I don't set it I don't get the exception. The data in pBuffer is just a BYTE array that was previously pulled out of a SAFEARRAY.

anyone know what I'm doing wrong?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2024-09-16 17:26:02

SafeArrayCreate 创建一个安全数组并为 pvData 成员分配内存。此后您不应重置 pvData 成员。您应该将数据从 pBuffer 复制到 pvData 指向的位置,或使用 SafeArrayAccessDataSafeArrayPutElement 函数。

SafeArrayCreate creates a safe-array and allocates memory for the pvData member. You should not reset the pvData member after this. You should copy your data from pBuffer into what pvData points to, or use the SafeArrayAccessData or SafeArrayPutElement functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文