{"id":23,"date":"2013-07-18T08:36:34","date_gmt":"2013-07-18T08:36:34","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=23"},"modified":"2013-07-18T08:36:34","modified_gmt":"2013-07-18T08:36:34","slug":"packing-a-array-of-strings-inside-a-variant-vc-6-0-series-2-of-n","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/18\/packing-a-array-of-strings-inside-a-variant-vc-6-0-series-2-of-n\/","title":{"rendered":"Packing a array inside a VARIANT (VC++ 6.0) (Series 2 of N)"},"content":{"rendered":"<p>This is the second post about variant, in this post I I am going to explain, how an array of strings can be packed inside a VARIANT variable.<\/p>\n<h3>Packing&#160; a array of string inside a Variant<\/h3>\n<p>i<font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>nt&#160; PackVariantWithStringArray(short nCnt, VARIANT * pVar )        <br \/>{        <br \/>&#160;&#160;&#160; USES_CONVERSION ;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160; \/\/Initialize the VARIANT (Type is SAFEARRAY of BSTRs)       <br \/>&#160;&#160;&#160; VariantInit(pVar);        <br \/>&#160;&#160;&#160; pVar-&gt;vt = VT_ARRAY | VT_BSTR;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160; \/\/ Define a safe array of nCnt Item and Starting index as 0       <br \/>&#160;&#160;&#160; SAFEARRAYBOUND safeBounds = { nCnt, 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_BSTR, 1, &amp;safeBounds);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160; \/\/Get a pointer, actually increments the array&#8217;s lock count)       <br \/>&#160;&#160;&#160; BSTR* bstrArray = NULL;        <br \/>&#160;&#160;&#160; SafeArrayAccessData(pSafeArray, (void**)&amp;bstrArray);        <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; bstrArray [i] = strTmp.AllocSysString () ;        <br \/>&#160;&#160;&#160; } <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160; \/\/done wth populating, Decrement the array&#8217;s lock count       <br \/>&#160;&#160;&#160; SafeArrayUnaccessData(pSafeArray);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#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 \/>}<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong><\/strong><\/font><\/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_BSTR)<\/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 strings from a variant.<\/h4>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>TCHAR ** UnPackVariantWithStringArray(short nCnt, VARIANT var )        <br \/>{        <br \/>&#160;&#160;&#160; TCHAR **pBuffer = (TCHAR **) calloc ( nCnt, sizeof (INT)) ;&#160; <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160; USES_CONVERSION ;       <br \/>&#160;&#160;&#160; SAFEARRAY* pSafeArray&#160; = var.parray ;        <br \/>&#160;&#160;&#160; \/\/pointer to array data, actually increments the lock count        <br \/>&#160;&#160;&#160; BSTR* bstrArray = NULL ;        <br \/>&#160;&#160;&#160; SafeArrayAccessData ( pSafeArray, (void**)&amp;bstrArray);        <br \/>&#160;&#160;&#160; for ( int i = 0 ; i &lt; nCnt ; i++ )        <br \/>&#160;&#160;&#160; {        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; LPTSTR szTmp = OLE2T ( bstrArray [i] ) ;        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; int nLen = _tcslen ( szTmp );        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; pBuffer [i]= (TCHAR *) calloc ( nLen, sizeof ( TCHAR));        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _tcscpy ( pBuffer [i], szTmp);        <br \/>&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; <br \/>&#160;&#160;&#160; \/\/Done wth populating, Decrement the array&#8217;s lock count        <br \/>&#160;&#160;&#160; SafeArrayUnaccessData(pSafeArray);        <br \/>&#160;&#160;&#160; return pBuffer ;        <br \/>}<\/strong><\/font><\/p>\n<p>these conversion is useful in <strong>middleware scenario<\/strong>. in the next article I will explain how to <strong>pack array of structure<\/strong> inside a variant.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second post about variant, in this post I I am going to explain, how an array of strings can be packed inside a VARIANT variable. Packing&#160; a array of string inside a Variant int&#160; PackVariantWithStringArray(short nCnt, VARIANT * pVar ) { &#160;&#160;&#160; USES_CONVERSION ; &#160;&#160;&#160; \/\/Initialize the VARIANT (Type is SAFEARRAY of&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/18\/packing-a-array-of-strings-inside-a-variant-vc-6-0-series-2-of-n\/\">Continue reading <span class=\"screen-reader-text\">Packing a array inside a VARIANT (VC++ 6.0) (Series 2 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-23","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\/23","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=23"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}