title: Use an returned string array from c++ function in C#
body: <p>I return a string array from C++ function and want to use this string array in C#. When i try to get the array from C# it gives me this error:</p>
  <blockquote>
  <p>System.Runtime.InteropServices.MarshalDirectiveException: &quot;Cannot marshal 'return value': Invalid managed/unmanaged type</p>
  </blockquote>
  <p>Here is my C++ code:</p>
  <p><strong>Header</strong></p>
  <pre><code>#pragma once
  
  #ifdef CRITELAB_EXPORTS
  #define API __declspec(dllexport)
  #else
  #define  API __declspec(dllimport)
  #endif
  
  #include &lt;string&gt;
  
  
  extern &quot;C&quot; API std::string* gatherInfos();
  </code></pre>
  <p><strong>CPP</strong></p>
  <pre><code>std::string* gatherInfos()
  {
      std::string username;
      std::string password;
      
      puts(&quot;Please enter a username:&quot;);
      fflush(stdout);
      std::cin &gt;&gt; username;
      std::cout &lt;&lt; std::endl;
  
      puts(&quot;Please enter a password:&quot;);
      fflush(stdout);
      std::cin &gt;&gt; password;
  
      std::string* const gatheredInfos = new std::string[2];
      gatheredInfos[0] = username;
      gatheredInfos[1] = password;
  
      return gatheredInfos;
  }
  </code></pre>
  <p>And here is my <strong>C#</strong> code</p>
  <pre><code>using System;
  using System.Runtime.InteropServices;
  
  namespace ConsoleApp1
  {
      internal class Program
      {
          private static void Main(string[] args)
          {
  
              string[] gatheredInfos = gatherInfos();    
              Console.ReadLine();
          }
  
  
          [DllImport(&quot;critelab.dll&quot;)]
          public static extern int connectToDB();
  
          [DllImport(&quot;critelab.dll&quot;, CallingConvention = CallingConvention.Cdecl)]
          public static extern string[] gatherInfos();
      }
  }
  </code></pre>