emily_hall
asked on
Runtime error: application has requested runtime to terminate it in an unusual way - calling C# from C++
I have a C# DLL that uses SQLDMO and I have a C++ program that calls it. I can run it in the debugger and the code goes through and calls the DLL and executes the function just like I would expect it to but when the function exits I get this error:
-------------------------- -
Microsoft Visual C++ Runtime Library
-------------------------- -
Runtime Error!
Program: C:\C# DLL\TestProgram2\Debug\Tes tProgram2. exe
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
-------------------------- -
OK
-------------------------- -
any ideas?
--------------------------
Microsoft Visual C++ Runtime Library
--------------------------
Runtime Error!
Program: C:\C# DLL\TestProgram2\Debug\Tes
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
--------------------------
OK
--------------------------
any ideas?
What call conventions are you using? How do you call from C++ to C#?
ASKER
This is my C++ code:
::CoInitialize(NULL);
SetConnectionString(connec tionString );
_DbClassPtr p;
p.CreateInstance("Database Library.Db Class");
m_DLLInterfacePtr = p.GetInterfacePtr();
m_DLLInterfacePtr->loadSto redProcedu reDetails( m_connecti onString);
loadStoredProcedureDetails returns a bool but I have tried returning void and the same thing happens...
::CoInitialize(NULL);
SetConnectionString(connec
_DbClassPtr p;
p.CreateInstance("Database
m_DLLInterfacePtr = p.GetInterfacePtr();
m_DLLInterfacePtr->loadSto
loadStoredProcedureDetails
Is m_connectionString a BSTR? Who allocates the memory and who is deallocating it?
ASKER
m_connectionString is a char*.
and that is the code for the whole function - connectionString is passed in.
and that is the code for the whole function - connectionString is passed in.
I think that's the problem COM usually marhals string as BSTR and not as char*. The process tries to free memory that it doesn't own.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A coworker of mine actually found the problem but we so far haven't been able to figure out how to fix it. The problem isn't with that function but with the one that is called after it:
int machineId = m_DLLInterfacePtr->execute StoredProc edure("sp_ GetMachine Id", m_connectionString, (tagSAFEARRAY *)getMachineId, 3);
the problem is with the 3rd parameter which I typecast to a tagSAFEARRAY* in order to make it compile. What that is is an array of struct. On the other side, the C# is expecting an array of struct but it is dying when it tries to pass that through...
int machineId = m_DLLInterfacePtr->execute
the problem is with the 3rd parameter which I typecast to a tagSAFEARRAY* in order to make it compile. What that is is an array of struct. On the other side, the C# is expecting an array of struct but it is dying when it tries to pass that through...
ASKER
hey just going through my old questions trying to tidy things up. thanks for the help.