Mixed-mode DLL crashes when calling managed code

J

Jesse McGrew

Hi all,

I'm trying to make a plugin DLL for a third-party application, using
VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of
the application, and my actual plugin code written in C#.

When the app calls my unmanaged functions, they work fine. But as soon
as my unmanaged functions call managed functions (in the same source
file!), the app reports an "unknown exception" error.

Please refer to my code below. Note:
* LEditPlugin is a C# class defined in a separate assembly.
* LMacro_Register is an API function in the 3rd party app, provided by a
static library, which registers a named function (the 2nd parameter) in
the current DLL as a plugin function.
* LDialog_MsgBox is defined in the same library.

Also notice that there are two separate code paths here (in a vain
attempt to track down the problem):
1. If SEPARATE_MANAGED_FUNC is defined, the exposed function HelloFunc()
is unmanaged, and it calls the managed function HelloFuncManaged() to
instantiate the C# object. In this case I see *one* message box ("Before
calling managed") before the error.
2. Otherwise, HelloFunc() is managed and instantiates the object itself.
In this case I see *none* of the message boxes; the error occurs as soon
as I call HelloFunc() from the app.

I can attach VC's debugger to the application and step through the DLL,
but it reveals nothing helpful. As soon as I step into the managed
function call, the application shows an error.

What am I doing wrong? Do I need to initialize the CLR somehow before I
can call managed code? How can I change this DLL so the application can
call it and it can use managed code?

====================

#pragma managed

#ifdef SEPARATE_MANAGED_FUNC
void HelloFuncManaged(void)
{
using namespace NLI::polamax;

LDialog_MsgBox("Before creating plugin");
LEditPlugin * plug = new LEditPlugin(UPI::Instance);
LDialog_MsgBox("Before calling method");
plug->SayHello();
}
#endif

#pragma unmanaged

#define POLAMAX_API extern "C" __declspec(dllexport)

POLAMAX_API int UPI_Entry_Point(void)
{
LMacro_Register("Hello, world!", "HelloFunc");
return 1;
}

#ifdef SEPARATE_MANAGED_FUNC
POLAMAX_API void HelloFunc(void)
{
LDialog_MsgBox("Before calling managed...");
HelloFuncManaged();
LDialog_MsgBox("After calling managed...");
}
#else
#pragma managed
POLAMAX_API void HelloFunc(void)
{
using namespace NLI::polamax;

LDialog_MsgBox("Before creating plugin");
LEditPlugin * plug = new LEditPlugin(UPI::Instance);
LDialog_MsgBox("Before calling method");
plug->SayHello();
}
#endif

====================

Jesse
 
J

Jesse McGrew

I said:
When the app calls my unmanaged functions, they work fine. But as soon
as my unmanaged functions call managed functions (in the same source
file!), the app reports an "unknown exception" error.
....

#pragma managed
POLAMAX_API void HelloFunc(void)
{
using namespace NLI::polamax;

LDialog_MsgBox("Before creating plugin");
LEditPlugin * plug = new LEditPlugin(UPI::Instance);
LDialog_MsgBox("Before calling method");
plug->SayHello();
}

OK, I found the problem after noticing that it was caused by the
reference to my other assembly. Running managed code alone doesn't cause
the problem, even if it uses other assemblies such as Windows Forms. The
error must have been caused by loading the C# assembly.

The C# assembly was in the same directory as the MC++ assembly; I
thought the system would be able to find it there, but I guess not. It
works if I put the C# assembly in the application's directory, but
that's ugly. I'll have to put it in the GAC instead.

Jesse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top