472,779 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

Mixed-mode DLL crashes when calling managed code

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
1 2822
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Niels Berkers | last post by:
Hi, i'd like to host my web pages using multiparts to reduce the number of hits on the server. i know this isn't a real PHP subject, but i'll try it anyway. i've been searching the web for...
3
by: Perttu Pulkkinen | last post by:
No questions, but just consider if this is useful to you:-) but of course feedback & corrections are welcome. function php_mixed_to_js_value($jsname, $mixed) { if(is_null($mixed)) { return "\n...
0
by: Swaroop Kumar | last post by:
Hi: I'm trying to write a schema that contains information as described below: 1. The first element is a mandatory fixed string. 2. The second element is a mixed content element that can...
2
by: Paul A. Hoadley | last post by:
Hello, I am trying to convert a RELAX NG schema to DTD using Trang. I am currently trying to add some inline elements to the schema, such as <emph> for marking emphasised text. Here is an...
0
by: Imiro | last post by:
Hi, i'm trying to express mixed content like this with a schema: <root>string<tag1/></root> or <root>string</root> but not <root></root> I guess it would be like this as dtd declaration...
0
by: Fender Mussel | last post by:
Hi all, Is it possible to check the content of a mixed type? I would like to define an enumeration for it. The following example works syntactically, but XMLSpy does no checks on the content at...
0
by: Chishun Kwong | last post by:
Hi, I would like to use xsd to restrict the text in a mixed, complex-type element. My reading so far seems to say that it is not achievable, is this true? I find it hard to imagine. For...
15
by: Bill Cohagan | last post by:
I'm trying to generate class definitions from an XSD that contains something like: <xs:complexType name="foo" mixed="true"> <xs:choice minOccurs = "0" maxOccurs="unbounded"> <xs:element name =...
5
by: Jeff | last post by:
I am trying to crete a method that will convert an improper fraction to a mixed number... I am not sure how about how to acomplish this. I know I can get the remainder with the modulus operator...
4
by: natG | last post by:
Hi; I am transferring data from MySql to db2 using my own java/jdbc program. Working out ok, except for the fact that our apps use mixed-case names for tables and columns. Although my CREATE TABLE...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.