By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,135 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,135 IT Pros & Developers. It's quick & easy.

Mixed-mode DLL crashes when calling managed code

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I wrote:
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
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.