{"id":21,"date":"2013-07-17T18:35:15","date_gmt":"2013-07-17T18:35:15","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=21"},"modified":"2013-07-17T18:35:15","modified_gmt":"2013-07-17T18:35:15","slug":"packing-a-array-inside-a-variant-vc-6-0-series-1-of-n","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/17\/packing-a-array-inside-a-variant-vc-6-0-series-1-of-n\/","title":{"rendered":"Packing a array inside a VARIANT (VC++ 6.0) (Series 1 of N)"},"content":{"rendered":"<p>VARIANTs are versatile data types, they can hold any type of value, apart from that they can hold array of different types. In this post I am going to explain how can you pack a array of bytes, inside a variant variable.<\/p>\n<h3>Packing&#160; a array of simple types inside a Variant<\/h3>\n<h4>Packing&#160; a array of bytes<\/h4>\n<p>int GetVariantWithByteBuffer (int nBufferSize, VARIANT FAR* pVar)    <br \/>{<\/p>\n<p>&#160;&#160;&#160; \/* here size of buffer desired (<strong>nBufferSize<\/strong>) has been passed to the function, which can be determined before hand. *\/<\/p>\n<p><strong><font color=\"#c0504d\">\/* Creating a temporary buffer to hold the values one need to return and filling that buffer. fill buffer, this buffer can be filled some other ways too, reading from a file or reading from some hardware or whatever&#160; here it has been filled from values 1 &#8211; 255*\/<\/font><\/strong><\/p>\n<p>&#160;&#160;&#160;&#160; BYTE *pBuffer = (BYTE *) calloc ( <strong><font color=\"#0000ff\">nBufferSize, sizeof (BYTE))<\/font><\/strong> ;&#160; <br \/>&#160;&#160;&#160;&#160; for ( int i = 0 ;&#160; i &lt; <strong>nBufferSize<\/strong>; i++ ) pBuffer [i] = ( i + 1 ) % 256 ;&#160; <\/p>\n<p><strong><font color=\"#c0504d\"><\/font><\/strong><\/p>\n<p><strong><font color=\"#c0504d\">&#160;&#160;&#160;&#160; \/* Creating a variant to return, which can hold the safe array of bytes .*\/<\/font><\/strong><\/p>\n<p>&#160;&#160;&#160;&#160; SAFEARRAY FAR* psa ;    <br \/>&#160;&#160;&#160;&#160; <font color=\"#0000ff\"><strong>psa = SafeArrayCreateVector (VT_UI1, 0, nBufferSize);&#160; <br \/><\/strong><\/font>&#160;&#160;&#160;&#160;&#160; if(psa == NULL)&#160;&#160;&#160; return \u20131 ;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160; <br \/><strong><font color=\"#c0504d\">&#160;&#160;&#160;&#160; \/* Copying the temporary buffer to the variant to return *\/<\/font><\/strong><\/p>\n<p>&#160;&#160;&#160;&#160; VariantInit ( pVar ) ; <\/p>\n<p>&#160;&#160;&#160;&#160; pVar-&gt;parray = psa;    <br \/><font color=\"#0000ff\"><strong>&#160;&#160;&#160;&#160; pVar-&gt;vt = VT_ARRAY | VT_UI1 ;<\/strong><\/font>&#160; <br \/>&#160;&#160;&#160;&#160; memcpy ( <strong><font color=\"#0000ff\">pVar-&gt;<\/font><\/strong>parray-&gt;pvData , pBuffer, <font color=\"#0000ff\"><strong>nBufferSize <\/strong>* <strong>sizeof (BYTE)<\/strong><\/font> ) ;&#160;&#160; <br \/><strong><font color=\"#c0504d\"><\/font><\/strong><\/p>\n<p><strong><font color=\"#c0504d\">&#160;&#160;&#160; \/* free the temporary buffer *\/<\/font><\/strong><\/p>\n<p>&#160;&#160;&#160; free ( pBuffer ) ; <\/p>\n<p><strong><font color=\"#c0504d\">&#160;&#160;&#160; \/* return the number of elements&#160; this variant hold. *\/<\/font><\/strong>     <br \/>&#160;&#160;&#160; return <strong>nBufferSize<\/strong>;<\/p>\n<p>}<\/p>\n<p>as it can be seen from the above code, it involves following steps.<\/p>\n<ol>\n<li>Allocating the desired temporary buffer space to hold the array of bytes, and filling that array with values one need to return. <\/li>\n<li>creating a safe array of the desired (<strong><font color=\"#0000ff\">VT_UI1)<\/font><\/strong> type. <\/li>\n<li>copying the temporary buffer to the safe array of the variant. <\/li>\n<li>free the memory allocated for temporary buffer. (avoid memory leaks). <\/li>\n<\/ol>\n<h4>Fetching a array of bytes from a variant.<\/h4>\n<p>BYTE * GetByteBufferFromVariant (int nBufferSize, VARIANT var )    <br \/>{<\/p>\n<p>&#160;&#160;&#160; \/* here size of buffer desired (<strong>nBufferSize<\/strong>) has been passed to the function, which can help us determine the size of the buffer required. *\/<\/p>\n<p>&#160;&#160;&#160; BYTE *pBuffer = (BYTE *) calloc ( <strong><font color=\"#0000ff\">nBufferSize, sizeof (BYTE))<\/font><\/strong> ;&#160;&#160; <br \/>&#160;&#160;&#160; memcpy ( pBuffer, var.parray-&gt;pvData, <font color=\"#0000ff\"><strong>nBufferSize <\/strong>* <strong>sizeof (BYTE)<\/strong><\/font> ) ;&#160;&#160; <br \/>&#160;&#160;&#160; return <strong>pBuffer <\/strong>;<\/p>\n<p>}<\/p>\n<p>In&#160; a similar fashion array of some other simple types can be&#160; packed in a variant. the following table describes the data types for different simple types.<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"527\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"43\"><strong><font color=\"#4f81bd\" size=\"3\">S.N.<\/font><\/strong><\/td>\n<td valign=\"top\" width=\"221\"><strong><font color=\"#4f81bd\" size=\"3\">Data Type<\/font><\/strong><\/td>\n<td valign=\"top\" width=\"261\"><strong><font color=\"#4f81bd\" size=\"3\">Value of vt<\/font><\/strong> <\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"43\">1<\/td>\n<td valign=\"top\" width=\"221\">BYTE <\/td>\n<td valign=\"top\" width=\"261\">VT_ARRAY | VT_UI1 <\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"43\">2<\/td>\n<td valign=\"top\" width=\"221\">SHORT (2 byte signed integer )<\/td>\n<td valign=\"top\" width=\"261\">VT_ARRAY | VT_I2 <\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"43\">3<\/td>\n<td valign=\"top\" width=\"221\">USHORT (2 byte unsigned integer )<\/td>\n<td valign=\"top\" width=\"261\">VT_ARRAY | VT_UI2 <\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"43\">4<\/td>\n<td valign=\"top\" width=\"221\">INT (4 byte signed integer )<\/td>\n<td valign=\"top\" width=\"261\">VT_ARRAY | VT_I4 <\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"43\">5<\/td>\n<td valign=\"top\" width=\"221\">UINT (4 byte unsigned integer )<\/td>\n<td valign=\"top\" width=\"261\">VT_ARRAY | VT_UI4 <\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"43\">6<\/td>\n<td valign=\"top\" width=\"221\">FLOAT ( 4 byte float value)<\/td>\n<td valign=\"top\" width=\"261\">VT_ARRAY|VT_R4<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"43\">7<\/td>\n<td valign=\"top\" width=\"221\">DOUBLE (8 byte double value)<\/td>\n<td valign=\"top\" width=\"261\">VT_ARRAY|VT_R8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#160;<\/p>\n<p>using the table above one can pack array of above defined types, to variant, these conversion is useful in <strong>middleware scenario<\/strong>. in the next article I will explain how to <strong>pack array of strings and array of structure<\/strong> inside a variant.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>VARIANTs are versatile data types, they can hold any type of value, apart from that they can hold array of different types. In this post I am going to explain how can you pack a array of bytes, inside a variant variable. Packing&#160; a array of simple types inside a Variant Packing&#160; a array of&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/17\/packing-a-array-inside-a-variant-vc-6-0-series-1-of-n\/\">Continue reading <span class=\"screen-reader-text\">Packing a array inside a VARIANT (VC++ 6.0) (Series 1 of N)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11,15],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-mfc","category-win32","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}