Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can MSVC6 handle exceptions from extern "C" functions?

I'm working on an application written in Visual Studio 6 (I know, FML) that is calling functions in a DLL using LoadLibrary and GetProcAddress. The newer code can't compile in VC6, and needs a newer compiler. The DLL has a few functions that construct a C++ object, and then the VC6 program uses the object through an abstract class.

This works just fine usually, but it runs into problems when the functions retrieved by GetProcAddress throw exceptions -- even when the exceptions are caught within the DLL. I've noticed that this doesn't happen when the abstract class's methods throw an exception. Things work normally in that case.

What am I doing wrong here? How can I make VC6 generate code to handle the exceptions properly?

Edit: Here's an example of a function that causes the program to crash:

extern "C" __declspec(dllexport) Box* getBox(const char* addr)
{
    try {
        return createBox(addr);
    } catch (std::exception& ex) {
        LOG_ERROR("Open failed: " << ex.what());
        return 0;
    } catch (...) {
        LOG_ERROR("Error while opening.");
        return 0;
    }
}
like image 260
Brian Avatar asked Nov 14 '22 17:11

Brian


1 Answers

You cannot do inheritance cross compiler versions like that. It almost works but exceptions and a few other things go crazy.

like image 173
Joshua Avatar answered Dec 05 '22 14:12

Joshua