473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Calling a C++ DLL from a C# program. Unexpected behaviour

Hi there!

I’m having a strange problem with a c++ dll that is called from a c#
program. The dll wraps a large piece of c++ software that uses exceptions for
its error-signalling. To keep the exception mechanism away from the c# end,
all functions in the dll are embedded in catch-all blocks that return ‘false’
if anything throws an exception. Using a c++ program as the caller, the dll
can be shown to work and return false on an exception.

Now, the fun part: If I use c# to access the function, it returns ‘true’
every time an exception occurs. If I step into the dll, using the mixed-mode
debugger, the results are pretty interesting. Upon stepping over the line
that will case an exception to be thrown, the debugger jumps back to the c#
code, as if the function returned. It does not enter the exception handler,
like it did with c++ code calling the function.

I get this behaviour with all similar code.

FWIW, the DllImport lines look like this: [DllImport("somedll.dll",
CharSet=CharSet.Ansi)]

I would really appreciate some help, as this is a major showstopper for us.

Thanks in advance,

Ulf
Nov 22 '05 #1
2 1367
Ascaron wrote:
Now, the fun part: If I use c# to access the function, it returns
'true' every time an exception occurs. If I step into the dll, using
the mixed-mode debugger, the results are pretty interesting. Upon
stepping over the line that will case an exception to be thrown, the
debugger jumps back to the c# code, as if the function returned. It
does not enter the exception handler, like it did with c++ code
calling the function.


Can you give the complete prototype of one of the methods you are
calling?

I suspect that this is a marshalling problem. If I do this:

// C++
extern "C" __declspec(dllexport) bool Test()
{
try
{
throw 1;
}
catch(...)
{ return false;}
return true;
}
// C#
class Tester
{
[DllImport("lib")]
public static extern bool Test();
}

I find that Tester.Test() returns true, whereas, as you say, it should
return false. If I change the C++ to:

extern "C" __declspec(dllexport) BOOL Test()
{
try
{
throw 1;
}
catch(...)
{ return FALSE;}
return TRUE;
}

then Tester.Test() returns false, as it should.

I don't know what is happening here, because if I remove the try/catch
block from Test version with bool (and return false), then it works as
expected.

Richard
--
www.richardgrimes.com
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
Nov 22 '05 #2
Hello Richard

Thanks for your reply, this solved the problem perfectly. Still it left us
wondering why ;)

Cheers

Ulf
"Richard Grimes [MVP]" wrote:
Ascaron wrote:
Now, the fun part: If I use c# to access the function, it returns
'true' every time an exception occurs. If I step into the dll, using
the mixed-mode debugger, the results are pretty interesting. Upon
stepping over the line that will case an exception to be thrown, the
debugger jumps back to the c# code, as if the function returned. It
does not enter the exception handler, like it did with c++ code
calling the function.


Can you give the complete prototype of one of the methods you are
calling?

I suspect that this is a marshalling problem. If I do this:

// C++
extern "C" __declspec(dllexport) bool Test()
{
try
{
throw 1;
}
catch(...)
{ return false;}
return true;
}
// C#
class Tester
{
[DllImport("lib")]
public static extern bool Test();
}

I find that Tester.Test() returns true, whereas, as you say, it should
return false. If I change the C++ to:

extern "C" __declspec(dllexport) BOOL Test()
{
try
{
throw 1;
}
catch(...)
{ return FALSE;}
return TRUE;
}

then Tester.Test() returns false, as it should.

I don't know what is happening here, because if I remove the try/catch
block from Test version with bool (and return false), then it works as
expected.

Richard
--
www.richardgrimes.com
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rajorshi | last post by:
Hi, I have two classes, say, Base1 and Base2. Now Derv is a class derived from both like class Derv(Base1,Base2)..... In the destructor of the derived class, if I write something like this def...
2
by: Shailan | last post by:
Hi Im having trouble with the following code that seems to be behave differently when called from the browser as opposed to the command line. The calling script is a cgi that forks, with the...
2
by: Ascaron | last post by:
Hi there! I’m having a strange problem with a c++ dll that is called from a c# program. The dll wraps a large piece of c++ software that uses exceptions for its error-signalling. To keep the...
24
by: gswork | last post by:
Let's write a c program, without knowing what it does... Some of you may recall Jim Roger's excellent series of posts (on comp.programming) exploring the implementation of common software...
10
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output...
19
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
18
by: utab | last post by:
Dear all, I am making a system call to the well known Gnuplot with system("gnuplot"); gnuplot opens if I only supply this command but I would like to pipe that command line in my C++...
2
by: ramasubramanian.rahul | last post by:
hi i am trying to call some java APIs from c . i use the standatd JNI calls to load the JVM from a c program and call all java functions by using a pointer to the jvm which was returned by the JNI...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.