{"id":26,"date":"2013-07-19T03:53:30","date_gmt":"2013-07-19T03:53:30","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=26"},"modified":"2013-07-19T03:53:30","modified_gmt":"2013-07-19T03:53:30","slug":"packing-a-array-inside-a-variant-vc-6-0-series-3-of-n","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/19\/packing-a-array-inside-a-variant-vc-6-0-series-3-of-n\/","title":{"rendered":"Packing a array inside a VARIANT (VC++ 6.0) (Series 3 of N)"},"content":{"rendered":"<p>This is the third post about variant, in this post I I am going to explain, how an array of structures can be packed inside a VARIANT variable. suppose we have a structure as definition shown below.<\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\">typedef struct MyStructType      <br \/>{       <br \/>&#160;&#160;&#160; int nID ;       <br \/>&#160;&#160;&#160; long lVal ;       <br \/>&#160;&#160;&#160; double dblVal ;       <br \/>&#160;&#160;&#160; TCHAR szBuffer[255];       <br \/>} MyStructType ;<\/font><\/p>\n<h3>Packing&#160; a array of structure inside a Variant<\/h3>\n<p><font face=\"Courier New\"><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">int&#160; PackVariantWithStructArray(short nCnt, VARIANT * pVar )          <br \/>{           <br \/>&#160;&#160;&#160; \/\/ TODO: Add your dispatch handler code here           <br \/>&#160;&#160;&#160; USES_CONVERSION ;<\/font><\/sup><\/font><\/p>\n<p><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">&#160;&#160;&#160; \/\/Initialize the VARIANT (Type is SAFEARRAY of BYTE)        <br \/>&#160;&#160;&#160; VariantInit(pVar);         <br \/>&#160;&#160;&#160; pVar-&gt;vt = VT_ARRAY | VT_UI1 ;<\/font><\/sup><\/p>\n<p><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">&#160;&#160;&#160; int nBufferSize = nCnt * sizeof ( MyStructType );<\/font><\/sup><\/p>\n<p><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">&#160;&#160;&#160; \/\/ Define a safe array of nCnt Item and Starting index as 0        <br \/>&#160;&#160;&#160; SAFEARRAYBOUND safeBounds = { nBufferSize, 0};         <br \/>&#160;&#160;&#160; <br \/>&#160;&#160;&#160; \/\/Create the Safe Array passing it the bounds         <br \/>&#160;&#160;&#160; SAFEARRAY* pSafeArray = SafeArrayCreate(VT_UI1, 1, &amp;safeBounds);<\/font><\/sup><\/p>\n<p><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">&#160;&#160;&#160; \/\/Get a pointer to the array data, This actually increments the array&#8217;s lock count)        <br \/>&#160;&#160;&#160; MyStructType * structArray = NULL;         <br \/>&#160;&#160;&#160; SafeArrayAccessData(pSafeArray, (void**)&amp;structArray);         <br \/>&#160;&#160;&#160; for ( int i = 0 ; i &lt; nCnt ; i++ )         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CString strTmp ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; strTmp.Format ( _T(&quot;This is Item %2d&quot;), i );         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; structArray[i].dblVal = (i +1) * 101.0 ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; structArray[i].lVal = (i +1) * 11 ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; structArray[i].nID = (i +1) ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _tcscpy ( structArray[i].szBuffer, strTmp) ;         <br \/>&#160;&#160;&#160; } <\/font><\/sup><\/p>\n<p><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">&#160;&#160;&#160; \/\/ We are done wth populating the array Decrement the lock<\/font><\/sup><\/p>\n<p><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">&#160;&#160;&#160;&#160; SafeArrayUnaccessData(pSafeArray);<\/font><\/sup><\/p>\n<p><sup><font color=\"#0000ff\" size=\"3\" face=\"Courier New\">&#160;&#160;&#160; \/\/Assign our VARIANT out param with the array&#160;&#160;&#160; <br \/>&#160;&#160;&#160; pVar-&gt;parray = pSafeArray ;         <br \/>&#160;&#160;&#160; return nCnt ;         <br \/>}<\/font><\/sup><\/p>\n<p>as it can be seen from the above code, here I have just created a byte buffer sufficient to accommodate the all structures, <\/p>\n<p>it involves following steps.<\/p>\n<ol>\n<li>Allocating the desired temporary buffer space to hold the array of structures (that should be equivalent to (number of structure x size of structure), 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_U1)<\/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 structure from a variant.<\/h4>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">MyStructType * UnPackVariantWithStructArray(short nCount, VARIANT var )      <br \/>{       <br \/>&#160;&#160;&#160; MyStructType *pBuffer = (MyStructType *) calloc ( nCount, sizeof (MyStructType)) ;&#160; <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; USES_CONVERSION ;      <br \/>&#160;&#160;&#160; SAFEARRAY* pSafeArray&#160; = var.parray ;       <br \/>&#160;&#160;&#160; \/\/Get a pointer to the array data, This actually increments the array&#8217;s lock count)       <br \/>&#160;&#160;&#160; MyStructType *structArray = NULL ;       <br \/>&#160;&#160;&#160; SafeArrayAccessData ( pSafeArray, (void**)&amp;structArray );       <br \/>&#160;&#160;&#160; for ( int i = 0 ; i &lt; nCount ; i++ )       <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; pBuffer[ i].dblVal = structArray [i].dblVal&#160; ;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; pBuffer[ i].lVal = structArray [i].lVal ;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; pBuffer[ i].nID = structArray [i].nID&#160;&#160; ;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; pBuffer[ i].dblVal = structArray [i].dblVal&#160; ;       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _tcscpy ( pBuffer[ i].szBuffer, structArray [i].szBuffer );       <br \/>&#160;&#160;&#160; }       <br \/>&#160;&#160;&#160; \/\/&#160;&#160;&#160; We are done wth populating the array Decrement the array&#8217;s lock count       <br \/>&#160;&#160;&#160; SafeArrayUnaccessData(pSafeArray);       <br \/>&#160;&#160;&#160; return pBuffer ;       <br \/>}<\/font><\/p>\n<p>this is a crude method, although there is a better method using type libraries, that I will explain in some other article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the third post about variant, in this post I I am going to explain, how an array of structures can be packed inside a VARIANT variable. suppose we have a structure as definition shown below. typedef struct MyStructType { &#160;&#160;&#160; int nID ; &#160;&#160;&#160; long lVal ; &#160;&#160;&#160; double dblVal ; &#160;&#160;&#160; TCHAR&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/19\/packing-a-array-inside-a-variant-vc-6-0-series-3-of-n\/\">Continue reading <span class=\"screen-reader-text\">Packing a array inside a VARIANT (VC++ 6.0) (Series 3 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,9,15],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-interoperability","category-win32","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/26","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=26"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}