473,581 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_MANAGE D_FUNC is defined, the exposed function HelloFunc()
is unmanaged, and it calls the managed function HelloFuncManage d() 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_MANAGE D_FUNC
void HelloFuncManage d(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(dlle xport)

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

#ifdef SEPARATE_MANAGE D_FUNC
POLAMAX_API void HelloFunc(void)
{
LDialog_MsgBox( "Before calling managed...");
HelloFuncManage d();
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 2891
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
4435
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 solutions and examples with no succes. does anybody know a good starting point hints / tips are also welcome Regards
3
2561
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 $jsname = null;\n"; } elseif(is_string($mixed)) { return "\n $jsname = '$mixed';\n"; } elseif(is_numeric($mixed) || is_boolean($mixed))
0
1767
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 contain multiple occurrences of two possible elements interspersed with regular text.
2
1759
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 excerpt from the schema: <define name="description"> <element name="description"> <mixed>
0
1361
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 (though not valid): <!ELEMENT root (#PCDATA|tag1|tag2)+> I've come up with this, but it doesnt quite do it yet:
0
1629
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 all: <xsd:simpleType name="my_enum_type"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="enum1"/> <xsd:enumeration value="enum2"/>
0
1026
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 example, in <foo> some text
15
5395
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 = "bar" type = "something"/> ... <xs:element name = "baz" type = "somethingelse"/> </xs:choice> </xs:complexType>
5
5747
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 (%), but I am not sure how to get the quotent. any insight would be appreciated. thanks
4
10271
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 commands issued via jdbc uses mixed case for the names, db2 insists on using only upper case. (This also happens from the CLP or DB2CC, but my...
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.