{"id":91,"date":"2013-08-06T05:33:00","date_gmt":"2013-08-06T05:33:00","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=91"},"modified":"2013-08-06T05:33:00","modified_gmt":"2013-08-06T05:33:00","slug":"modifying-array-of-string-values-default-marshaling","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/08\/06\/modifying-array-of-string-values-default-marshaling\/","title":{"rendered":"Modifying array of string values (default Marshaling)"},"content":{"rendered":"<p>Define some functions in native dll (let say it \u201c<strong>Win32Native.dll<\/strong>\u201d)&#160; as shown below. this function receives array of char pointers (string). <\/p>\n<p><strong><font color=\"#0000ff\"><font size=\"2\"><font face=\"Courier New\">extern &quot;C&quot; __declspec(dllexport) void ModifyStringArrayValues( int nCount, char* ppStrArray[] )<\/font><\/font><\/font>       <br \/><\/strong><\/p>\n<h3>Implementing Functions<\/h3>\n<p><font color=\"#0000ff\" face=\"Courier New\"><strong>extern &quot;C&quot; __declspec(dllexport) void ModifyStringArrayValues( int nCount, char* ppStrArray[] )        <br \/>{         <br \/>&#160;&#160;&#160; size_t cchDest = 40;         <br \/>&#160;&#160;&#160; const size_t alloc_size = sizeof(char) * 40;         <br \/>&#160;&#160;&#160; for ( int nI = 0; nI &lt; nCount; nI++ )         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; char *pszFormat = &quot;&lt;&lt;from DLL&gt;&gt; [Modified String %2d]&quot;;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; STRSAFE_LPSTR temp = (STRSAFE_LPSTR)CoTaskMemAlloc( alloc_size );         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; StringCchPrintfA(temp, cchDest, pszFormat, nI);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CoTaskMemFree( ppStrArray[nI] );         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ppStrArray[nI] = (char *) temp;         <br \/>&#160;&#160; }         <br \/>}<\/strong><\/font><\/p>\n<p><strong><\/strong><\/p>\n<h4>Point of Interest<\/h4>\n<ul>\n<li><strong>CoTaskMemAlloc is used to allocated the memory required. <\/strong><\/li>\n<li><strong>CoTaskMemFree <\/strong>is used to free any previously allocated buffer, if null is passed then, <strong>CoTaskMemFree <\/strong>is not called<strong>.<\/strong> <\/li>\n<li><strong>StringCchPrintfA<\/strong> printf used to print a formatted output to a string buffer. <\/li>\n<\/ul>\n<p>If you want to use a <strong>heap that is shared between native and managed<\/strong>, it is more common to use the COM heap. <\/p>\n<ul>\n<li>On the native side use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms692727.aspx\"><code><strong>CoTaskMemAlloc()<\/strong><\/code><\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms680722.aspx\"><code><strong>CoTaskMemFree()<\/strong><\/code><\/a>. <\/li>\n<li>On the managed side use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.runtime.interopservices.marshal.alloccotaskmem.aspx\"><code><strong>Marshal.AllocCoTaskMem()<\/strong><\/code><\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.runtime.interopservices.marshal.freecotaskmem.aspx\"><code><strong>Marshal.FreeCoTaskMem()<\/strong><\/code><\/a>. <\/li>\n<\/ul>\n<h4>Writing the client code (the managed part) <\/h4>\n<p>one can simple create a console base application which can use this dll. let\u2019s name it <strong>MarshallingTest. <\/strong><\/p>\n<p>see the code snippet below.<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>using System;        <br \/>using System.Runtime.InteropServices;         <br \/>using System.Text;<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>namespace MarshallingTest        <br \/>{         <br \/>&#160;&#160;&#160; class Program         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [DllImport(&quot;Win32Native.dll&quot;)]         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; public static extern double ModifyStringArrayValues(int nSize, [In, Out] String[] strArr);         <br \/>&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; static void Main(string[] args)         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int nSize = 5;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; string[] arrStr = new string[nSize];         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; nSize; nI++) arrStr[nI] = &quot;String &quot; + (nI + 1).ToString();<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;String Values in Array, before calling function&quot;);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int i = 0; i &lt; nSize; i++)         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(string.Format(&quot;Array[{0:D2}] : {1}&quot;, i, arrStr[i]));         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ModifyStringArrayValues(nSize, arrStr);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;String Values in Array, after calling function&quot;);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int i = 0; i &lt; nSize; i++)         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(string.Format(&quot;Array[{0:D2}] : {1}&quot;, i, arrStr[i]));         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; }         <br \/>}<\/strong><\/font>     <\/p>\n<p><strong><\/strong><\/p>\n<p><strong><\/strong><\/p>\n<h4>Point of Interest<\/h4>\n<ul>\n<li><strong>namespace System.Runtime.InteropServices;<\/strong>&#160; defines the declarations necessary for Interop operations, like <strong>DllImport<\/strong>, <\/li>\n<li><strong>DllImport<\/strong> defines the DLL entry point. <\/li>\n<\/ul>\n<p>compile and execute you will get following output.<\/p>\n<p><a href=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/08\/image3.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;padding-top:0;padding-left:0;display:inline;padding-right:0;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/08\/image_thumb3.png\" width=\"515\" height=\"142\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Define some functions in native dll (let say it \u201cWin32Native.dll\u201d)&#160; as shown below. this function receives array of char pointers (string). extern &quot;C&quot; __declspec(dllexport) void ModifyStringArrayValues( int nCount, char* ppStrArray[] ) Implementing Functions extern &quot;C&quot; __declspec(dllexport) void ModifyStringArrayValues( int nCount, char* ppStrArray[] ) { &#160;&#160;&#160; size_t cchDest = 40; &#160;&#160;&#160; const size_t alloc_size = sizeof(char)&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/08\/06\/modifying-array-of-string-values-default-marshaling\/\">Continue reading <span class=\"screen-reader-text\">Modifying array of string values (default Marshaling)<\/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":[17,28],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-interoperability","category-win32","tag-net","tag-marshaling","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/91","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=91"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}