在包含 SAFEARRAY 的 VARIANT 上调用 VariantClear() 时会引发异常
我试图将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SafeArrayCreate
创建一个安全数组并为pvData
成员分配内存。此后您不应重置pvData
成员。您应该将数据从pBuffer
复制到pvData
指向的位置,或使用SafeArrayAccessData
或SafeArrayPutElement
函数。SafeArrayCreate
creates a safe-array and allocates memory for thepvData
member. You should not reset thepvData
member after this. You should copy your data frompBuffer
into whatpvData
points to, or use theSafeArrayAccessData
orSafeArrayPutElement
functions.