tags:

views:

143

answers:

1

Hi everyone, I'm having a problem with mixing managed and unmanaged code. I have created two projects under a single solution in Visual Studio 2008 under Vista x64 SP1. One of them does not have CLR support and is a static library. My second project is compiled as an executable with CLR enabled. It depends on the first static library, and passes WinForms events to it.

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

Here is the code of the unmanaged lib that is run:

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 can't see why I'm getting a managed (System.BlahBlahBlah) exception in an unmanaged library. Is there any way to get the two to be totally separate?

+1  A: 

The underlying exception is actually a Windows exception that the CLR is apparently turning into a CLR exception for you. You have an access violation. What you should be able to do is, in Visual Studio, head to Debug > Exceptions and break on acess violations. This should let you drop in and see where in the native code it's gone all horribly wrong and start diagnosing the issue.

Logan Capaldo
Thanks for the info! The exception is thrown in line 18706 of my sqlite.c file, I'll try and figure out how to fix this.
LM