title: Returning Strings from DLL Functions
body: <p>For some reason, returning a string from a DLL function crashes my program on runtime with the error <code>Unhandled exception at 0x775dfbae in Cranberry Library Tester.exe: Microsoft C++ exception: std::out_of_range at memory location 0x001ef604..</code>.</p>
  <p>I have verified it's not a problem with the function itself by compiling the DLL code as an <code>.exe</code> and doing a few simple tests in the <code>main</code> function.</p>
  <p>Functions with other return types (<code>int</code>, <code>double</code>, etc.) work perfectly.</p>
  <ul>
  <li>Why does this happen?</li>
  <li>Is there a way to work around this behavior?</li>
  </ul>
  <p>Source code for DLL:</p>
  <pre><code>// Library.h
  #include &lt;string&gt;
  
  std::string GetGreeting();
  </code></pre>
  <p>.</p>
  <pre><code>// Library.cpp
  #include &quot;Library.h&quot;
  
  std::string GetGreeting()
  {
      return &quot;Hello, world!&quot;;
  }
  </code></pre>
  <p>Source code for tester:</p>
  <pre><code>// Tester.cpp
  #include &lt;iostream&gt;
  #include &lt;Library.h&gt;
  
  int main()
  {
      std::cout &lt;&lt; GetGreeting()
  }
  </code></pre>
  <p>EDIT: I'm using VS2010.</p>
  <hr />
  <h1>Conclusion</h1>
  <p>A workaround is to make sure the library and source are compiled using the <strong>same compiler</strong> with the <strong>same options</strong>, etc.</p>