An unmanaged library in a managed executable that throws managed exceptions

I am having a problem moving managed and unmanaged code. I created two projects under one solution in Visual Studio 2008 under Vista x64 SP1. One of them has no CLR support and is a static library. My second project is compiled as CLR enabled executable. It depends on the first static library and passes WinForms events to it.

When I run the application without debugging, I get an exception, I put the information I get from the exception here: http://pastebin.com/f46ad1211 .

Here is the code for the unmanaged lib:

void manager::init() // <-- Called from the .exe project
{
    this->log.open("C:\\development\\log.txt");
    this->storage = storage_manager(&(this->log), &(this->settings));
    this->storage.load_settings();
}

      

&

void storage_manager::load_settings()
{
    this->error_check(sqlite3_open("settings.db", &(this->db_settings_p)));
    sqlite3_stmt* read_settings;
    this->error_check(sqlite3_prepare_v2(this->db_settings_p, "SELECT name, value FROM settings", 1024, &read_settings, NULL));

    int step_code;
    std::string name;
    std::string value;
    while(true)
    {
        step_code = sqlite3_step(read_settings);
        if(step_code == SQLITE_DONE)
        {
            break;
        }
        else if(step_code == SQLITE_ROW)
        {
            name = std::string(reinterpret_cast<const char*>(sqlite3_column_text(read_settings, 0)));
            value = std::string(reinterpret_cast<const char*>(sqlite3_column_text(read_settings, 1)));
            (*(this->settings))[name] = value;

        }
        else
        {
            this->error();
        }
    }
    sqlite3_reset(read_settings);
    sqlite3_finalize(read_settings);
}

      

&

void storage_manager::error_check(int rc)
{
    if(rc)
    {
        this->error();
    }
}
void storage_manager::error() //Sure of error
{
    std::string error_msg;
    error_msg = "Storage Manager: SQLite Error (";
    error_msg += sqlite3_errcode(this->db_p);
    error_msg += ") - ";
    error_msg += sqlite3_errmsg(this->db_p);
    this->log->write(error_msg.c_str(), error_msg.length());
    this->log->flush();
}

      

I don't understand why I am getting a managed (System.BlahBlahBlah) exception in an unmanaged library. Is there a way to keep them completely separate?

0


a source to share


1 answer


The exception is actually a Windows exception, which the CLR seems to turn into a CLR exception for you. You have an access violation. What you should do - in Visual Studio - go to Debug> Exceptions and violate acess violations. This should allow you to take a peek and see where things went horribly wrong in your own code and start diagnosing the problem.



+1


a source







All Articles