{"id":44,"date":"2013-07-30T12:31:22","date_gmt":"2013-07-30T12:31:22","guid":{"rendered":"https:\/\/praveenkatiyar.wordpress.com\/?p=44"},"modified":"2013-07-30T12:31:22","modified_gmt":"2013-07-30T12:31:22","slug":"interoperability-returning-a-string-from-native-dll","status":"publish","type":"post","link":"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/30\/interoperability-returning-a-string-from-native-dll\/","title":{"rendered":"Interoperability Returning a string from Wn32 DLL"},"content":{"rendered":"<p>Define&#160; a function in native dll (let say it \u201cWin32Native.dll\u201d)&#160; as shown below.<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>extern &quot;C&quot; __declspec(dllexport)&#160; char * GetStringFromDLL()        <br \/>{         <br \/>&#160;&#160;&#160; const size_t alloc_size = 128;         <br \/>&#160;&#160;&#160; STRSAFE_LPSTR result=(STRSAFE_LPSTR)<\/strong><\/font><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><strong>CoTaskMemAlloc(alloc_size);        <br \/>&#160;&#160;&#160; STRSAFE_LPCSTR teststr = &quot;This is return string From Native DLL&quot;;         <br \/>&#160;&#160;&#160; StringCchCopyA ( result, alloc_size, teststr );         <br \/>&#160;&#160;&#160; return result;         <br \/>}<\/strong><\/font><\/p>\n<h3>Point of Interest<\/h3>\n<ul>\n<li><strong>STRSAFE_LPSTR<\/strong> is a typedef of char * <\/li>\n<li><strong>StringCchCopy<\/strong> is a replacement for strcpy (with safety).&#160; The size, in characters, of the destination buffer is provided to the function to ensure that StringCchCopyb does not write past the end of this buffer. has two variants.\n<ul>\n<ul>\n<li><strong>StringCchCopyA<\/strong> (ANSI Version) <\/li>\n<li><strong>StringCchCopyW<\/strong> (Unicode\/Wide character Version) <\/li>\n<\/ul>\n<li>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.\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<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>Writing the client code (the managed part) <\/h3>\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\">using System;      <br \/>using System.Collections.Generic;       <br \/>using System.Linq;       <br \/>using System.Runtime.InteropServices;       <br \/>using System.Text;<\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">namespace MarshallingTest      <br \/>{       <br \/>&#160;&#160;&#160; class Program       <br \/>&#160;&#160;&#160; {       <br \/>&#160;&#160;&#160; <font color=\"#004080\">&#160;&#160; <\/font><font color=\"#c0504d\"><strong>[DllImport(&quot;Win32Native.dll&quot;)] <\/strong><\/font><\/font><font color=\"#0000ff\" size=\"2\" face=\"Courier New\"><font color=\"#c0504d\"><strong>public static extern String GetStringFromDLL();<\/strong><\/font><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#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; Console.WriteLine(&quot;Line displayed below is returned from a Native DLL&quot;);       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; string strData = GetStringFromDLL();       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Console.WriteLine ( &quot;Returned value [&quot; + strData +&quot;]&quot; );       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br \/>&#160;&#160;&#160; }       <br \/>}<\/font><\/p>\n<h3>Point of Interest<\/h3>\n<ul>\n<li><strong>namespace System.Runtime.InteropServices;<\/strong>&#160; defines the declarations necessary for Interop operations, like DllImport, <\/li>\n<li>DllImport 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\/07\/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\/07\/image_thumb2.png\" width=\"530\" height=\"53\" \/><\/a><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Courier New\">&#160;<\/font><\/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)&#160; char * GetStringFromDLL() { &#160;&#160;&#160; const size_t alloc_size = 128; &#160;&#160;&#160; STRSAFE_LPSTR result=(STRSAFE_LPSTR)CoTaskMemAlloc(alloc_size); &#160;&#160;&#160; STRSAFE_LPCSTR teststr = &quot;This is return string From Native DLL&quot;; &#160;&#160;&#160; StringCchCopyA ( result, alloc_size, teststr ); &#160;&#160;&#160; return result; } Point of Interest STRSAFE_LPSTR&hellip; <a class=\"more-link\" href=\"https:\/\/praveenkatiyar.in\/blog\/index.php\/2013\/07\/30\/interoperability-returning-a-string-from-native-dll\/\">Continue reading <span class=\"screen-reader-text\">Interoperability Returning a string from Wn32 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":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-interoperability","category-win32","entry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/44","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=44"}],"version-history":[{"count":0,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praveenkatiyar.in\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}