[Mono-dev] P/Invoking a VS2012 C++ DLL, DLL behavior is materially different in 3.2.3 than in 3.1 and .net

Dennis Fantoni dennis at fantoni.dk
Sun Sep 29 18:03:17 UTC 2013


I am calling the function in the DLL using interop, the function is declared in the c++ program as extern C. After all it’s just a void function with no parametres so it’s perfectly possible for it to have an extern C interface, and be non-mangled.

The exception that is not caught when running, is both thrown and caugt (in code) inside the c code in that function.. In fact the problem is with throw. A cerr<< statement the very next line after a throw is executed, which makes very little sense. mono is breaking some rules when setting stuff up before invoking a dll call (at least this is my best guess)

I fear that any assembly that calls a native VS2012 c++ DLL (possbly other compilers too) that internally depends on being able to throw and catch exceptions – will not work correctly on mono 3.2.3 on windows, even though it worked before 3.2.3

I cannot rule out that I am using some wrong compiler settings – if I do , the same wrong compiler settings are in the small project filed with the bug, as that project also shows the error.

I have filed a bug with two visual studio projects to build a C# program,and a c++ DLL (visual studio). I have deliberately tried to use as many default settings as possible, as that probably matches well with what is out there in the open. The C dll could be made somewhat simpler.

the bug report is here :

https://bugzilla.xamarin.com/show_bug.cgi?id=14989

It would be very interesting to have someone confirm that I am correct in my assumption that mono 3.2.3 does not work as it is supposed to do, when calling DLL’s on windows.

I am aware that exceptions thrown in a dll cannot be caught in the calling C# program, even though this works fine in .net. My c++ program does not leak exceptions to managed code. The example programs in the bug report doesn’t do that either)

regards,

Dennis




From: Ian Norton 
Sent: Friday, September 27, 2013 8:59 PM
To: Dennis Fantoni 
Cc: Mono Developer List 
Subject: Re: [Mono-dev] P/Invoking a VS2012 C++ DLL, DLL behavior is materially different in 3.2.3 than in 3.1 and .net

Er.. How are you calling this? Surely the function name gets mangled?

On 27 Sep 2013 09:01, "Dennis Fantoni" <dennis at fantoni.dk> wrote:

  (running on windows 7)

  In short, if I wrote this in a dll in .net or mono 3.1  i would get to “this shows”

  void tester() {
  try {
  throw(foo);
  cerr<<”this never shows”
  }
  ctch(...){
  cerr<<”this shows”
  }
  }

  However, in 3.2.3 above code when called via Pinvoke, will display “this never shows” , indicating that the data structures used by the exception system inside the DLL has been damaged by mono.

  I have created two VS2012 sample solutions to create a C# dll caller, and a C++ callee that exposes the problem – and filed a bug

  https://bugzilla.xamarin.com/show_bug.cgi?id=14989

  If someone have the time to confirm or comment, I would be very pleased. If I am not mistaken it is a pretty severe bug – it might happen that any vs2012 c++ DLL that uses exceptions internally has ceased to work with mono on windows. (the problem might be bigger, I havent tried to build dll’s with VS2010 or other languages).

  Anyways, I would expect mono to behave similar to .net, but it does not in this respect.

  Regards,


  Dennis



  _______________________________________________
  Mono-devel-list mailing list
  Mono-devel-list at lists.ximian.com
  http://lists.ximian.com/mailman/listinfo/mono-devel-list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130929/2467f9c7/attachment.html>


More information about the Mono-devel-list mailing list