博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
蚊子132 如何结合 CMenFile 与 CArchive 进行高效的序列 ...
阅读量:3520 次
发布时间:2019-05-20

本文共 2416 字,大约阅读时间需要 8 分钟。

来源:

序列号存储是我们常用的文件存储方式, 这篇文章介绍如何结合 CMenFile 与 CArchive 进行高效的序列号存储。

BYTE * ppBuf;
long pcBufLen;
CMemFile file(ppBuf,pcBufLen);
CArchive ar(&file,CArchive::load);
将CArchive 与COleVariant相互转换就可以在内存文件存入到数据库大字段中
下面是COleVariant与BYTE 转换:

  1. //Extensive error checking is left out to make the code more readable
  2.    BOOL GetBinaryFromVariant(COleVariant & ovData, BYTE ** ppBuf,
  3.                                 unsigned long * pcBufLen)
  4.    {
  5.      BOOL fRetVal = FALSE;
  6.    //Binary data is stored in the variant as an array of unsigned char
  7.      if(ovData.vt == (VT_ARRAY|VT_UI1))  // (OLE SAFEARRAY)
  8.      {
  9.        //Retrieve size of array
  10.        *pcBufLen = ovData.parray->rgsabound[0].cElements;
  11.        *ppBuf = new BYTE[*pcBufLen]; //Allocate a buffer to store the data
  12.        if(*ppBuf != NULL)
  13.        {
  14.          void * pArrayData;
  15.          //Obtain safe pointer to the array
  16.          SafeArrayAccessData(ovData.parray,&pArrayData);
  17.          //Copy the bitmap into our buffer
  18.          memcpy(*ppBuf, pArrayData, *pcBufLen);
  19.          //Unlock the variant data
  20.          SafeArrayUnaccessData(ovData.parray);
  21.          fRetVal = TRUE;
  22.        }
  23.      }
  24.      return fRetVal;
  25.    }
  26.    BOOL PutBinaryIntoVariant(COleVariant * ovData, BYTE * pBuf,
  27.                                 unsigned long cBufLen)
  28.    {
  29.      BOOL fRetVal = FALSE;
  30.      VARIANT var;
  31.      VariantInit(&var);  //Initialize our variant
  32.      //Set the type to an array of unsigned chars (OLE SAFEARRAY)
  33.      var.vt = VT_ARRAY | VT_UI1;
  34.      //Set up the bounds structure
  35.      SAFEARRAYBOUND  rgsabound[1];
  36.      rgsabound[0].cElements = cBufLen;
  37.      rgsabound[0].lLbound = 0;
  38.      //Create an OLE SAFEARRAY
  39.      var.parray = SafeArrayCreate(VT_UI1,1,rgsabound);
  40.      if(var.parray != NULL)
  41.      {
  42.        void * pArrayData = NULL;
  43.        //Get a safe pointer to the array
  44.        SafeArrayAccessData(var.parray,&pArrayData);
  45.        //Copy bitmap to it
  46.        memcpy(pArrayData, pBuf, cBufLen);
  47.        //Unlock the variant data
  48.        SafeArrayUnaccessData(var.parray);
  49.        *ovData = var;  // Create a COleVariant based on our variant
  50.        VariantClear(&var);
  51.        fRetVal = TRUE;
  52.      }
  53.      return fRetVal;
  54.    }
  55.    //How you might call these functions
  56.    CdbRecordset rs;
  57.    //Code for initializing DAO and opening the recordset left out...
  58.    COleVariant ovData = rs.GetField(_T("MyBinaryField"));
  59.    BYTE * pBuf = NULL;
  60.    unsigned long cBufLen;
  61.    if(GetBinaryFromVariant(ovData,&pBuf,&cBufLen))
  62.    {
  63.      //Do something with binary data in pBuf...
  64.      //Write back a new record containing the binary data
  65.      COleVariant ovData2;
  66.      if(PutBinaryIntoVariant(&ovData2,pBuf,cBufLen))
  67.      {
  68.        rs.AddNew();
  69.        rs.SetField(_T("MyBinaryField"), ovData2); //Write our COleVariant
  70.    to the table
  71.        rs.Update();
  72.      }
  73.      //Clean up memory allocated by GetBinaryFromVariant
  74.      if(pBuf)
  75.        delete pBuf;
  76.    }
复制代码


转载地址:http://oxxqj.baihongyu.com/

你可能感兴趣的文章
spark运行模式
查看>>
PaddleX的C++使用
查看>>
MyBatis-Plus代码生成器
查看>>
我的第一个SpringBoot项目(一)
查看>>
回文数
查看>>
伪背包问题!
查看>>
求10000以内n的阶乘!
查看>>
static关键字
查看>>
类的继承
查看>>
final关键字
查看>>
抽象类
查看>>
java的多态现象
查看>>
java中对象的类型转换
查看>>
java基础入门 String
查看>>
Java基础入门 StringBuffer类
查看>>
Java基础入门 currentTimeMillis方法
查看>>
Java基础入门 arraycopy方法
查看>>
Java基础入门 Math类
查看>>
Java基础入门 Random类
查看>>
Java基础入门 Date类
查看>>