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:olamax;
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:olamax;
LDialog_MsgBox("Before creating plugin");
LEditPlugin * plug = new LEditPlugin(UPI::Instance);
LDialog_MsgBox("Before calling method");
plug->SayHello();
}
#endif
====================
Jesse
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:olamax;
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:olamax;
LDialog_MsgBox("Before creating plugin");
LEditPlugin * plug = new LEditPlugin(UPI::Instance);
LDialog_MsgBox("Before calling method");
plug->SayHello();
}
#endif
====================
Jesse