title: Using C++ native dll in .Net External component has thrown an exception
body: <p>I'm trying to use a native C++ dll in C# and am getting the "External component has thrown an exception" with an error code of -2147467259.</p>
  
  <p>C#:</p>
  
  <pre><code>[DllImport(@"MyDLL.dll", CharSet = CharSet.Auto)]
  public static extern string MyFunction([MarshalAs(UnmanagedType.LPStr)] StringBuilder input);
  </code></pre>
  
  <p>C++:</p>
  
  <pre><code>__declspec(dllexport) char * __stdcall MyFunction(const char* inputPtr);
  </code></pre>
  
  <p>The function works just fine in C++. How can I track this error down?
  I have tried using string and string builder for the parameter.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I found this article <a href="http://tom-shelton.net/index.php/2008/12/11/creating-a-managed-wrapper-for-a-lib-file/" rel="nofollow">http://tom-shelton.net/index.php/2008/12/11/creating-a-managed-wrapper-for-a-lib-file/</a></p>
  
  <p>It details a way to use managed C++ to wrap an unmanaged static library in C++ which can then be used in a managed language. Would this be a good way of going about tackling this problem? Can the lib be an unmanaged dll?</p>