蚊子132 如何结合 CMenFile 与 CArchive 进行高效的序列 ...

来源:蚊子132 如何结合 CMenFile 与 CArchive 进行高效的序列 ...

序列号存储是我们常用的文件存储方式,蚊子132 这篇文章介绍如何结合 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.    }
复制代码


你可能感兴趣的:(数据库,bitmap,存储,Something,structure)