{"id":87,"date":"2013-08-05T08:45:06","date_gmt":"2013-08-05T08:45:06","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=87"},"modified":"2013-08-05T08:45:06","modified_gmt":"2013-08-05T08:45:06","slug":"modifying-array-of-numeric-types-default-marshaling","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/08\/05\/modifying-array-of-numeric-types-default-marshaling\/","title":{"rendered":"Modifying array of numeric types (default Marshaling)"},"content":{"rendered":"<p>Define some functions in native dll (let say it \u201c<strong>Win32Native.dll<\/strong>\u201d)&#160; as shown below. These 2 functions receives integer buffer, and double buffer respectively. <\/p>\n<p><strong><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">extern &quot;C&quot; __declspec(dllexport) int ModifyIntegerArrayValues ( int nSize, int* pArray)<\/font><\/strong><\/p>\n<p><strong><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">extern &quot;C&quot; __declspec(dllexport) double ModifyDoubleArrayValues ( int nSize, double* pArray)        <br \/><\/font><\/strong><\/p>\n<h3>Implementing Functions<\/h3>\n<p><font color=\"#0000ff\" face=\"Courier New\"><strong>extern &quot;C&quot; __declspec(dllexport) int ModifyIntegerArrayValues ( int nSize, int* pArray)        <br \/>{         <br \/>&#160;&#160;&#160; int nResult = 0 ;         <br \/>&#160;&#160;&#160; for ( int i = 0; i &lt; nSize; i++ )         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; nResult += pArray[ i ];         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; pArray[i] += 100;         <br \/>&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; return nResult;         <br \/>}<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\"><strong>extern &quot;C&quot; __declspec(dllexport) double ModifyDoubleArrayValues ( int nSize, double* pArray)        <br \/>{         <br \/>&#160;&#160;&#160; double dblResult = 0 ;         <br \/>&#160;&#160;&#160; for ( int i = 0; i &lt; nSize; i++ )         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; dblResult += pArray[ i ];         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; pArray[i] += 200 ;         <br \/>&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; return dblResult ;         <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<\/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; public static extern void ModifyIntegerArrayValues(int nCount, [In, Out] int[] arrStr);<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#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 int ModifyDoubleArrayValues(int nSize, [In, Out] double[] nArr);         <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; int[] arrInt = new int[nSize];         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; nSize; nI++) arrInt[nI] = (nI + 1) * 10;<\/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;\\nValues of Integer 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:D3}&quot;, i, arrInt[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; ModifyIntegerArrayValues(nSize, arrInt);<\/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;\\nValues of Integer 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:D3}&quot;, i, arrInt[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; double [] arrDbl= new double [nSize];        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; nSize; nI++) arrDbl[nI] = (nI + 1) * 5.0 ;<\/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;\\nValues of Double 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:F2}&quot;, i, arrDbl[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; ModifyDoubleArrayValues(nSize, arrDbl);<\/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;\\nValues of double 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:F2}&quot;, i, arrDbl[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<h6>Point of Interest<\/h6>\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<li><strong>Marshal.Copy<\/strong> function used to copy buffer from managed buffer to unmanaged buffer and vice versa. <\/li>\n<li>Marshal.<strong>FreeCoTaskMem<\/strong> frees the memory allocated by native DLL. <\/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\/image2.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_thumb2.png\" width=\"455\" height=\"247\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Define some functions in native dll (let say it \u201cWin32Native.dll\u201d)&#160; as shown below. These 2 functions receives integer buffer, and double buffer respectively. extern &quot;C&quot; __declspec(dllexport) int ModifyIntegerArrayValues ( int nSize, int* pArray) extern &quot;C&quot; __declspec(dllexport) double ModifyDoubleArrayValues ( int nSize, double* pArray) Implementing Functions extern &quot;C&quot; __declspec(dllexport) int ModifyIntegerArrayValues ( int nSize, int* pArray)&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/08\/05\/modifying-array-of-numeric-types-default-marshaling\/\">Continue reading <span class=\"screen-reader-text\">Modifying array of numeric types (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":[2,3,9,15],"tags":[28],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-net","category-codeproject","category-interoperability","category-win32","tag-marshaling","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/87","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=87"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}