See the question and my original answer on StackOverflow

Use the _CrtSetReportMode functions, something like this:

extern "C" {
    __declspec(dllexport) void terminate_me(void) {
        _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); // define that anywhere in your init code, etc.
        puts("hello from C");
        puts("bb from C");

Note when _DEBUG is not defined (so, in release), calls to _CrtSetReportMode are removed during preprocessing.

Your BadImageFormatException error probably comes from that fact you've checked the "Prefer 32-bit" checkbox in .NET project properties in release mode. This error is always a x86-x64 mismatch issue.