{"id":52,"date":"2013-07-31T16:25:31","date_gmt":"2013-07-31T16:25:31","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=52"},"modified":"2013-07-31T16:25:31","modified_gmt":"2013-07-31T16:25:31","slug":"fetching-a-byte-buffer-from-a-native-dll","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/31\/fetching-a-byte-buffer-from-a-native-dll\/","title":{"rendered":"Fetching a byte buffer from a Win32 DLL"},"content":{"rendered":"<p>&#160;<\/p>\n<p>Define&#160; a function in native dll (let say it \u201c<strong>Win32Native.dll<\/strong>\u201d)&#160; as shown below.<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>extern &quot;C&quot; __declspec(dllexport) int FetchByteArray ( int nSize, byte** ppnArray )        <br \/>{         <br \/>&#160;&#160;&#160; int result = 0;         <br \/>&#160;&#160;&#160; \/\/&#160;&#160;&#160; CoTaskMemAlloc must be used instead of the new operator because code on the managed side will call         <br \/>&#160;&#160;&#160; \/\/&#160;&#160;&#160; Marshal.FreeCoTaskMem to free this memory.         <br \/>&#160;&#160;&#160; byte* newArray = (byte*)CoTaskMemAlloc( sizeof(byte) * nSize );         <br \/>&#160;&#160;&#160; for ( int j = 0; j &lt; nNewSize ; j++ )         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; newArray[j] = ( j+1 ) % 255 ;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; result += newArray[j];         <br \/>&#160;&#160;&#160; }<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\"><strong><font size=\"2\" face=\"Courier New\">&#160;&#160;&#160; \/\/ release the previous buffer, if any allocated.<\/font><\/strong>       <br \/><\/font><strong><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; if ( *ppnArray != NULL ) CoTaskMemFree( *ppnArray );        <br \/>&#160;&#160;&#160; *ppnArray = newArray;         <br \/>&#160;&#160;&#160; return result;         <br \/>}<\/font><\/strong><\/p>\n<h5>Point of Interest<\/h5>\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<h5>Writing the client code (the managed part) <\/h5>\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.Collections.Generic;         <br \/>using System.Linq;         <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; [DllImport(&quot;Win32Native.dll&quot;)]         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public static extern int FetchByteArray(int nSize, ref IntPtr arrInt);         <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 = 10;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IntPtr ptrArr = IntPtr.Zero;<\/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; int nSum = FetchByteArray(nSize, ref ptrArr);        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; byte[] arrByte = new byte[nSize];         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Marshal.Copy(ptrArr, arrByte, 0, nSize);<\/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;\\nReturned Buffer\\n&quot;);<\/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; 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.Write ( &quot;{0:D2}&#160; &quot;, arrByte[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; <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.Write(&quot;\\nSum of Buffer : {0}\\n&quot;, nSum );         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Marshal.FreeCoTaskMem(ptrArr);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160; }         <br \/>}         <br \/><\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong><\/strong><\/font><\/p>\n<h5>Point of Interest<\/h5>\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\/07\/image3.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;float:none;padding-top:0;padding-left:0;margin-left:auto;display:block;padding-right:0;margin-right:auto;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/07\/image_thumb3.png\" width=\"440\" height=\"62\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#160; Define&#160; a function in native dll (let say it \u201cWin32Native.dll\u201d)&#160; as shown below. extern &quot;C&quot; __declspec(dllexport) int FetchByteArray ( int nSize, byte** ppnArray ) { &#160;&#160;&#160; int result = 0; &#160;&#160;&#160; \/\/&#160;&#160;&#160; CoTaskMemAlloc must be used instead of the new operator because code on the managed side will call &#160;&#160;&#160; \/\/&#160;&#160;&#160; Marshal.FreeCoTaskMem to free&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/31\/fetching-a-byte-buffer-from-a-native-dll\/\">Continue reading <span class=\"screen-reader-text\">Fetching a byte buffer from a Win32 DLL<\/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-52","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\/52","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=52"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}