I am working on program that uses a DLL library (LibPlcTag : https://github.com/libplctag/libplctag) and I wanted to add some exception handling to 'my' code. However after finding this was not working, that is the thrown exceptions were not being caught, I managed to distill it all down to the following bit of demonstration code.
So the question is what is going on here ? Why does a call to a DLL function kill C++ try catch ? Is there a way to fix this ?
FYI : I am using Mingw32 C++ with -std=c++11 compiler build flag on Windows 11.
#include <iostream>
#define SHOWERROR 1
#if SHOWERROR
  #ifdef __cplusplus
  extern "C" {
  #endif
  __declspec(dllimport) int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch);
  #ifdef __cplusplus
  }
  #endif
#endif
int main(int argc, char *argv[])
{
  #if SHOWERROR
    std::cout << "Call DLL function" << std::endl;
    plc_tag_check_lib_version(0,0,0);
    std::cout << "Call Finished" << std::endl;
  #else
    std::cout << "No DLL Function Call !!" << std::endl;
  #endif
  try
  {
    std::cout << "Throw error" << std::endl;
    throw "Error";
  }
  catch( ... )
  {
    std::cout << "Exception happened" << std::endl;
  }
  std::cout << "End" << std::endl;
  return 0;
}
With SHOWERROR = 0 the exception is caught :
No DLL Function Call !!
Throw error
Exception happened
End
C:\...\test>
But with SHOWERROR = 1 the exception is not caught and the program terminates without printing 'End' :
Call DLL function
Call Finished
Throw error
C:\...\test>
