title: Passing std::string with dllexport
body: <p>I'm trying to make some sort of C++ "bridge" to connect an unmanaged C++ dll on one end (without modifying their code) to a C# Wrapper which uses DllImport for various imports.</p>
  
  <p>I was able to pass a C# string to my bridge using char pointers, but the receiving Dll needs to receive std::string, so I tried with <code>std::string(foo);</code> with no luck, it always gets transformed into weird characters.</p>
  
  <p>The structure is the following :</p>
  
  <p><strong>C# Wrapper</strong></p>
  
  <pre><code>[DllImport(@"Bridge.dll", CallingConvention = CallingConvention.Cdecl)]
  public static extern void initDetector(string foo, int something = 0);
  </code></pre>
  
  <p><strong>C++ Bridge</strong></p>
  
  <pre><code>extern "C" __declspec(dllexport) void initCppClass(char* foo, int something)
  {
      std::string bar = std::string(foo);
      std::cout &lt;&lt; bar &lt;&lt; std::endl; //Returns "foo"
      instance = new CppClass(bar, something);
  }
  </code></pre>
  
  <p><strong>C++ Imported DLL (not allowed to change code here)</strong></p>
  
  <pre><code>CppClass::CppClass(std::string foo, int something)
  {
      std::cout &lt;&lt; foo &lt;&lt; std::endl; //Returns garbage
  }
  </code></pre>
  
  <p><em>Note that this constructor is for demonstration purposes only, as I cannot disclose the original code.</em></p>
  
  <p>I originally tried passing the char* directly to the constructor but that didn't work either. Is there something I'm missing here ?</p>