Source: Mosquito 132 How to combine cmenfile with CArchive for efficient sequencing ...
Serial number storage is our usual way of storing files, mosquito 132 This article describes how to combine cmenfile with CArchive for efficient serial number storage.
BYTE * PPBUF;
Long Pcbuflen;
CMemFile file (Ppbuf,pcbuflen);
CArchive ar (&file,carchive::load);
Convert CArchive and COleVariant to a large segment of the database in memory files
Here is the COleVariant with byte conversion:
Extensive error checking is left off to make the code more readable
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 Bitmap 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;
}
How to might call these functions
Cdbrecordset rs;
Code for initializing DAO and opening the recordset left out ...
COleVariant ovdata = Rs. GetField (_t ("Mybinaryfield"));
BYTE * PBuf = NULL;
unsigned long Cbuflen;
if (Getbinaryfromvariant (Ovdata,&pbuf,&cbuflen))
{
Do something with binary data in PBuf ...
Write back a new record containing the binary data
COleVariant ovData2;
if (Putbinaryintovariant (&ovdata2,pbuf,cbuflen))
{
Rs. AddNew ();
Rs. SetField (_t ("Mybinaryfield"), OVDATA2); Write our COleVariant
to the table
Rs. Update ();
}
Clean up memory allocated by getbinaryfromvariant
if (PBUF)
Delete PBuf;
Copy the Code