0
votes

I have a c++ dll, which at a certain point, needs to delegate a task to a dll written in .Net. For this i Created a c++/cli static library which consumes/uses the .net dll to achive some TASK. Then i statically link the c++/cli static lib with c++ dll. It looks something like this.

        cli.aTask()         netdll.aTask()
c++ dll ----------> c++/cli ----------------> .net dll
    < static link  >    <normal assembly inclusion> 

Now, the connection b/w c++dll and c++/cli is fine. i can do anything using .net library in c++/cli layer and then call this from c++dll. but when i create an object of a class from .net dll, or access a static method of .net dll class, i get exception in c++dll (actually c++dll crashes, no exceptions). below is the msg i got.

foo.exe is using c++dll
Unhandled exception at 0x7c812aeb in "foo.exe": OxE0434f4d: 0xe0434f4d.

c++dll code calling cli method:

CLILibrary::CCLILib lib;
lib.GetUsersInput();

cli code calling .net dll:

DotNetLibrary::DotNetClass::aMethod();   // static method`

.net dll class code:

namespace DotNetLibrary
{
    public class DotNetClass
    {
        public DotNetClass(){}

       public static void aMethod(){}
    }
}

Now, if i dont use DotNetLibrary::DotNetClass in any way in c++/cli lib, then exception stops coming.

Please help. my aim is to call .net lib from c++ dll, if there is a better way, then it will be nice too. thanks in advance.

3

3 Answers

1
votes

Unhandled exception at 0x7c812aeb in "foo.exe": OxE0434f4d: 0xe0434f4d

0xe0434f4d is the exception code for a managed exception. The managed code you wrote is bombing, there is no handler anywhere that catches that exception. That's a kaboom without a decent diagnostic if you don't run the debugger in managed mode.

You'll need to use the try/catch keywords in the C++/CLI code to catch System::Exception, generate a diagnostic from the caught exception object's ToString() value and pass some kind of "he's dead Jim" status code back to your native code. Don't try to continue using the managed code after this, it's dead with an unpredictable state.

1
votes

As already mentioned you need to catch the (managed) exception either in your .Net or in your C++/CLI module. What I usualy do in my C++/CLI .Net wrappers is to catch the managed exceptions and re-throw them as standard c++ ones. Something like

try {
   // ... managed call
}
catch (System::Exception^ e)
{
    throw std::exception(ConvertString(e->Message));
}

(where ConvertString is a function that converts a managed string to a char* or std::string). In this way you'll still have to catch the exception in your native code but you will not lose information about the source error (well not quite since the callstack from the original code that generated the exception and other information will be lost but you could save this too if you wanted to).

0
votes

Compile foo.exe as console application, and run without debugger. When exception will appear, .NET will print it's name and an additional information.