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 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)
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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!
...
|
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
>>>...
|
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++...
|
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...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
| |