打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Write and read binary data in VARIANT – Code Library

Write and read binary data in VARIANT

Share on:more0

2006-11-22 Category: Win32/MFC 0 2,898 views

We offen need to store some binary data in the mdb database, thus we have to transefer the data by VARIANT structure.
But how to read from binary data from VARIANT or write to? Here is a solution by safe array of VARRIANT.


BOOL GetBinaryFromVariant(COleVariant & ovData, BYTE ** ppBuf, unsigned long * pcBufLen)
{
BOOL fRetVal = FALSE;
//Binary data is stored in the variant as an array of unsigned char
if(ovData.vt == (VT_ARRAY|VT_UI1)) // (OLE SAFEARRAY)
{
//Retrieve size of array
*pcBufLen = ovData.parray->rgsabound[0].cElements;
*ppBuf = new BYTE[*pcBufLen]; //Allocate a buffer to store the data
if(*ppBuf != NULL)
{
void * pArrayData;
//Obtain safe pointer to the array
SafeArrayAccessData(ovData.parray,&pArrayData);
//Copy the bitmap into our buffer
memcpy(*ppBuf, pArrayData, *pcBufLen); //Unlock the variant data
SafeArrayUnaccessData(ovData.parray);
fRetVal = TRUE;
}
}
return fRetVal;
}



BOOL PutBinaryIntoVariant(COleVariant * ovData, BYTE * pBuf,unsigned long cBufLen)
{
BOOL fRetVal = FALSE;
VARIANT var;
VariantInit(&var); //Initialize our variant
//Set the type to an array of unsigned chars (OLE SAFEARRAY)
var.vt = VT_ARRAY | VT_UI1;
//Set up the bounds structure
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].cElements = cBufLen;
rgsabound[0].lLbound = 0;
//Create an OLE SAFEARRAY
var.parray = SafeArrayCreate(VT_UI1,1,rgsabound);
if(var.parray != NULL)
{
void * pArrayData = NULL;
//Get a safe pointer to the array
SafeArrayAccessData(var.parray,&pArrayData);
//Copy data to it
memcpy(pArrayData, pBuf, cBufLen);
//Unlock the variant data
SafeArrayUnaccessData(var.parray);
*ovData = var;
// Create a COleVariant based on our variant
VariantClear(&var);
fRetVal = TRUE;
}
return fRetVal;
}

Permalink: Code Library ? http://www.ucosoft.com/write-and-read-binary-data-in-variant.html

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
关于SafeArray的使用说明
读写Access表中的图像字段
JavaScript奇技淫巧:执行“二进制”代码
PHP中的强制类型转换
使用delphi 开发多层应用(五)第一个kbmMW 服务例子
js 数组去重
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服