<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Library &#187; Database</title>
	<atom:link href="http://www.ucosoft.com/tag/database/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ucosoft.com</link>
	<description>Free Source Code and Program Tips</description>
	<lastBuildDate>Thu, 22 Jul 2010 05:17:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Write and read binary data in VARIANT</title>
		<link>http://www.ucosoft.com/write-and-read-binary-data-in-variant.html</link>
		<comments>http://www.ucosoft.com/write-and-read-binary-data-in-variant.html#comments</comments>
		<pubDate>Wed, 22 Nov 2006 08:51:33 +0000</pubDate>
		<dc:creator>hamo</dc:creator>
				<category><![CDATA[Win32/MFC]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[Program]]></category>

		<guid isPermaLink="false">http://www.ucosoft.com/archives/30.html</guid>
		<description><![CDATA[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 &#038; ovData, BYTE ** ppBuf, unsigned long * pcBufLen) { BOOL [...]]]></description>
			<content:encoded><![CDATA[<p>We offen need to store some binary data in the mdb database, thus we have to transefer the data by VARIANT structure.<br />But how to read from binary data from VARIANT or write to? Here is a solution by safe array of VARRIANT.</p>
<p> <span id="more-30"></span></p>
<p><coolcode lang="cpp" linenum="off" download="src.cpp"><br />
BOOL GetBinaryFromVariant(COleVariant &#038; ovData, BYTE ** ppBuf,  unsigned long * pcBufLen)<br />
{<br />
 BOOL fRetVal = FALSE;<br />
 //Binary data is stored in the variant as an array of unsigned char<br />
 if(ovData.vt == (VT_ARRAY|VT_UI1))  // (OLE SAFEARRAY)<br />
 {<br />
  //Retrieve size of array<br />
  *pcBufLen = ovData.parray->rgsabound[0].cElements;<br />
  *ppBuf = new BYTE[*pcBufLen]; //Allocate a buffer to store the data<br />
  if(*ppBuf != NULL)<br />
  {<br />
   void * pArrayData;<br />
   //Obtain safe pointer to the array<br />
   SafeArrayAccessData(ovData.parray,&#038;pArrayData);<br />
   //Copy the bitmap into our buffer<br />
   memcpy(*ppBuf, pArrayData, *pcBufLen);  //Unlock the variant data<br />
   SafeArrayUnaccessData(ovData.parray);<br />
   fRetVal = TRUE;<br />
  }<br />
 }<br />
 return fRetVal;<br />
}<br />
</coolcode><br />
<!--adsense#468l--><br />
<coolcode lang="cpp" linenum="off" download="src.cpp"><br />
BOOL PutBinaryIntoVariant(COleVariant * ovData, BYTE * pBuf,unsigned long cBufLen)<br />
{<br />
 BOOL fRetVal = FALSE;<br />
 VARIANT var;<br />
 VariantInit(&#038;var); 	//Initialize our variant<br />
 //Set the type to an array of unsigned chars (OLE SAFEARRAY)<br />
 var.vt = VT_ARRAY | VT_UI1;<br />
 //Set up the bounds structure<br />
 SAFEARRAYBOUND  rgsabound[1];<br />
 rgsabound[0].cElements = cBufLen;<br />
 rgsabound[0].lLbound = 0;<br />
 //Create an OLE SAFEARRAY<br />
 var.parray = SafeArrayCreate(VT_UI1,1,rgsabound);<br />
 if(var.parray != NULL)<br />
 {<br />
  void * pArrayData = NULL;<br />
  //Get a safe pointer to the array<br />
  SafeArrayAccessData(var.parray,&#038;pArrayData);<br />
  //Copy data to it<br />
  memcpy(pArrayData, pBuf, cBufLen);<br />
  //Unlock the variant data<br />
  SafeArrayUnaccessData(var.parray);<br />
  *ovData = var;<br />
  // Create a COleVariant based on our variant<br />
  VariantClear(&#038;var);<br />
  fRetVal = TRUE;<br />
 }<br />
 return fRetVal;<br />
}</p>
<p></coolcode></p>

	Tags: <strong><a href="http://www.ucosoft.com/tag/cpp" title="Cpp" rel="tag">Cpp</a>, <a href="http://www.ucosoft.com/tag/database" title="Database" rel="tag">Database</a>, <a href="http://www.ucosoft.com/tag/how-to" title="How-to" rel="tag">How-to</a>, <a href="http://www.ucosoft.com/tag/program" title="Program" rel="tag">Program</a>, <a href="http://www.ucosoft.com/tag/win32mfc" title="Win32/MFC" rel="tag">Win32/MFC</a></strong><br />
]]></content:encoded>
			<wfw:commentRss>http://www.ucosoft.com/write-and-read-binary-data-in-variant.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

