title: C# not catching unhandled exceptions from unmanaged C++ dll
body: <p>I've got an unmanaged C++ dll which is being called from a C# app, I'm trying to get the C# app to catch all exceptions so that in the event of the dll failing due to an unmanaged exception then the user will get a half-decent error message (the C# app is a web service implementing it's own http handler).</p>
  
  <p>The problem I have is that not all types are being caught.  So if I create the following and execute the C# app then the dll throws an error and the entire application terminates.  Any ideas?</p>
  
  <p>This is being created in VS2005 and using .Net framework v2</p>
  
  <p>C++ - Test.h</p>
  
  <pre><code>#ifndef INC_TEST_H
  #define INC_TEST_H
  
  extern "C" __declspec(dllexport) void ProcessBadCall();
  
  #endif
  </code></pre>
  
  <p>C++ - Test.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  void ProcessBadCall()
  {
    vector&lt;int&gt; myValues;
    int a = myValues[1];
    cout &lt;&lt; a &lt;&lt; endl;
  }
  </code></pre>
  
  <p>C# - Program.cs</p>
  
  <pre><code>class Program
  {
    [DllImport("Test.dll", EntryPoint="ProcessBadCall")]
    static extern void ProcessBadCall();
  
    static void Main(string[] args)
    {
      try
      {
        ProcessBadCall();
      }
      catch (SEHException ex)
      {
        Console.WriteLine("SEH Exception: {0}", ex.Message);
      }
      catch (Exception ex)
      {
        Console.WriteLine("Exception: {0}", ex.Message);
      }
    }
  }
  </code></pre>
  
  <p>The dll is being compiled under the release configuration with the following compiler flags.</p>
  
  <blockquote>
    <p>/O2 /GL /D "WIN32" /D "NDEBUG" /D
    "_CRT_SECURE_NO_WARNINGS" /D
    "_UNICODE" /D "UNICODE" /D "_WINDLL"
    /FD /EHa /MD /Fo"Release\"
    /Fd"Release\vc80.pdb" /W4 /WX /nologo
    /c /Wp64 /Zi /TP /errorReport:prompt</p>
  </blockquote>