{"id":74,"date":"2013-08-04T14:20:00","date_gmt":"2013-08-04T14:20:00","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=74"},"modified":"2013-08-04T14:20:00","modified_gmt":"2013-08-04T14:20:00","slug":"fetching-an-array-of-strings-from-a-native-dll","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/08\/04\/fetching-an-array-of-strings-from-a-native-dll\/","title":{"rendered":"Fetching an array of strings from a Win32 DLL"},"content":{"rendered":"<p>Define&#160; a function in native dll (let say it \u201c<strong>Win32Native.dll<\/strong>\u201d)&#160; as shown below.<\/p>\n<p><span style=\"font-size:small;font-family:courier new;color:#0000ff;\"><strong>extern &quot;C&quot; __declspec(dllexport) void FetchStringArray( int nCount, char* ppStrArray[])        <br \/>{         <br \/>&#160;&#160;&#160; int result = 0;         <br \/>&#160;&#160;&#160; STRSAFE_LPSTR temp;         <br \/>&#160;&#160;&#160; size_t cchDest = 40;<\/strong><\/span><\/p>\n<p><span style=\"font-size:small;font-family:courier new;color:#0000ff;\"><strong>&#160;&#160;&#160; const size_t alloc_size = sizeof(char) * 40;<\/strong><\/span><\/p>\n<p><span style=\"font-size:small;font-family:courier new;color:#0000ff;\"><strong>&#160;&#160;&#160; for ( int nI = 0; nI &lt; nCount; nI++ )        <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160; char *pszFormat = &quot;from DLL &gt;&gt; [returned String %d]&quot;;         <br \/>&#160;&#160;&#160;&#160;&#160;&#160; STRSAFE_LPSTR temp = (STRSAFE_LPSTR)CoTaskMemAlloc( alloc_size );         <br \/>&#160;&#160;&#160;&#160;&#160;&#160; StringCchPrintfA(temp, cchDest, pszFormat, nI);<\/strong><\/span><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160; CoTaskMemFree( ppStrArray[nI] );        <br \/>&#160;&#160;&#160;&#160;&#160;&#160; ppStrArray[nI] = (char *) temp;         <br \/>&#160;&#160;&#160; }         <br \/>}<\/strong><\/font><\/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<h3>Writing the client code (the managed part)<\/h3>\n<p>one can simply create a console based application<\/p>\n<p><span style=\"font-family:courier new;color:#0000ff;\"><font size=\"2\"><strong>using System;          <br \/>using System.Runtime.InteropServices;           <br \/>using System.Text;<\/strong><\/font><\/span><\/p>\n<p><span style=\"font-family:courier new;color:#0000ff;\"><code><font color=\"#0000ff\" size=\"2\"><strong>namespace MarshallingTest            <br \/>{             <br \/>&#160;&#160; class Program             <br \/>&#160;&#160; {             <br \/>&#160;&#160;&#160;&#160;&#160;&#160; [DllImport(&quot;Win32Native.dll&quot;)]             <br \/>&#160;&#160;&#160;&#160;&#160;&#160; public static extern void FetchStringArray(int nCount, [In, Out] String[] arrStr); <\/strong><\/font><\/code><\/span><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160;&#160;&#160;&#160;&#160; static void Main(string[] args)        <br \/>&#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; String[] arrStr = new String[nSize];         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FetchStringArray(nSize, arrStr);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine(&quot;Returned String Array&quot;);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int nI = 0; nI &lt; nSize; nI++)         <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;&#160; Console.WriteLine(arrStr[nI]);         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br \/>&#160;&#160;&#160;&#160;&#160;&#160; }<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>&#160;&#160; }<\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" face=\"Courier New\"><font size=\"2\"><strong>}          <br \/><\/strong><\/font><\/font><\/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\/image1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"background-image:none;padding-top:0;padding-left:0;margin:0;display:inline;padding-right:0;border-width:0;\" border=\"0\" alt=\"image\" src=\"http:\/\/praveenkatiyar.wordpress.com\/wp-content\/uploads\/2013\/08\/image_thumb1.png\" width=\"244\" height=\"128\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Define&#160; a function in native dll (let say it \u201cWin32Native.dll\u201d)&#160; as shown below. extern &quot;C&quot; __declspec(dllexport) void FetchStringArray( int nCount, char* ppStrArray[]) { &#160;&#160;&#160; int result = 0; &#160;&#160;&#160; STRSAFE_LPSTR temp; &#160;&#160;&#160; size_t cchDest = 40; &#160;&#160;&#160; const size_t alloc_size = sizeof(char) * 40; &#160;&#160;&#160; for ( int nI = 0; nI &lt; nCount; nI++&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/08\/04\/fetching-an-array-of-strings-from-a-native-dll\/\">Continue reading <span class=\"screen-reader-text\">Fetching an array of strings 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":[3,9,15],"tags":[17,29],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-interoperability","category-win32","tag-net","tag-marshalling","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/74","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=74"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}