Repetive calling of C DLL in C# (using DLLimport) - c#

Using Mono (JIT compiler version 3.2.8) in Ubuntu 64bit, I am calling a C DLL within a C# source code using something like this:
[DllImport("/home/user/path/to/my.dll")]
private static extern void mydll(int argument1, int argument2, ref int arguemnt3,
[MarshalAs(UnmanagedType.LPArray)] double[] argument4,
[MarshalAs(UnmanagedType.LPArray)] int[] argument5,
string argument6)
This worked flawless on an older Ubuntu 32bit machine. Now, on my new 64bit machine I run into the following strange behavior.
The first time I call the DLL in the C# source code it (apparently) works fine. The DLL returns arguments as expected and no error is raised. However, any further attempt to call the same DLL in the further code fails with following errors:
Stacktrace:
at <unknown> <0xffffffff>
at (wrapper managed-to-native) object.__icall_wrapper_mono_array_to_lparray (object) <0xffffffff>
at (wrapper managed-to-native) mydll(int,int,int,int,int,int,int,double[],double[],double[],double[],double[],int&,int&,double[],double[],int,int[],int,double[],int,string,int,int,int,int) <0xffffffff>
at mydll (System.Collections.Generic.Dictionary`2<string, int>,double[],double[],double[],System.Collections.Generic.Dictionary`2<string, int>,System.Collections.Generic.Dictionary`2<string, double>,string) <0x0094b>
at Example.Main () <0x00457>
at (wrapper runtime-invoke) object.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff>
Native stacktrace:
mono() [0x4b73d8]
mono() [0x50f13b]
mono() [0x423d22]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x10340) [0x7f2898a2c340]
mono() [0x550136]
[0x4139cd9d]
Debug info from gdb:
Could not attach to process. If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.
No threads.
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
I am very new to C# and quite clueless why that happens.
Maybe anyone could enlighten me?
Thanks a lot!

Related

System.EntryPointNotFoundException: SetConsoleTitle assembly:<unknown assembly> type:<unknown type> member:(null) on Replit.com

Trying to run a small C# project with uploaded DLLs on Replit.com
Uploaded my DLL (Unit4.dll).
In Visual Studio on my desktop it works after I Added References to three Windows DLLs:
System.Drawing.Common.dll, System.Windows.Forms.dll, System.Configuration.ConfigurationManager.dll.
Downloaded the three DLLs and Uploaded them to Files on Replit.com
Changed the .replit command to
run = "set -e; mcs Main.cs -r:Unit4.dll -r:System.Windows.Forms.dll -r:System.Drawing.Common.dll -r:System.Configuration.ConfigurationManager.dll; mono Main.exe"
It compiles and runs, but gets a the Runtime Exception I added below.
Not sure what is missing or what is wrong and how to fix it.
Please help.
Unhandled Exception:
System.EntryPointNotFoundException: SetConsoleTitle assembly: type: member:(null)
at (wrapper managed-to-native) Unit4.BinTreeCanvasLib.Window.SetConsoleTitle(string)
at Unit4.BinTreeCanvasLib.Window.get_Instance () [0x00067] in <6a068eaab6084265b4099e03c146f5d7>:0
at Unit4.BinTreeCanvasLib.TreeCanvas.InitTreeToDraw[T] (Unit4.CollectionsLib.BinNode1[T] tree) [0x00366] in <6a068eaab6084265b4099e03c146f5d7>:0 at Unit4.BinTreeCanvasLib.TreeCanvas.AddTree[T] (Unit4.CollectionsLib.BinNode1[T] tree) [0x00001] in <6a068eaab6084265b4099e03c146f5d7>:0
at Program.Main (System.String[] args) [0x00054] in <95445213fc8b4cc9ba753b3d428925d5>:0

Mono error when running C# application on Mac: Invalid IL code in System.Drawing.Icon

Note: This is not a duplicate of the linked question because I'm not looking to develop a Mac application.
So I have a C# application on .NET 4.5 that has the following:
1) Windows Forms
2) References to System.Drawing, EmguCV, Aforge.NET dlls
This works perfectly on my Windows system.
When I try to run the program on a Mac system running OSX using Mono, I get the following error:
exception inside UnhandledException handler: An exception was thrown by the type initializer for System.Windows.Forms.Form
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for System.Windows.Forms.Form ---> System.InvalidProgramException: Invalid IL code in System.Drawing.Icon:.ctor (System.IO.Stream): method body is empty.
at (wrapper remoting-invoke-with-check) System.Drawing.Icon:.ctor (System.IO.Stream)
at System.Windows.Forms.ResourceImageLoader.GetIcon (System.String name) [0x00000] in :0
at System.Windows.Forms.Form..cctor () [0x00000] in :0
Has anyone faced anything like this? Appreciate any suggestions.

Xamarin iOS - How to get binding with CFNetwork framework dependency to work

I'm working on an iOS static library binding that requires CFNetwork framework.
I've seen examples of how to DllImport the CFNetwork, but it requires that the method is extern static.
Unfortunately this appears to be invalid on the methods in the linkwith.cs
The library seems to actually work and starts a curl command, but I get an error:
-[MKNetworkOperation operationFailedWithError:] [Line 1293] State: 0
I'm assuming this is because I haven't imported CFNetwork since the library works with a regular XCode iOS project.
Also, every time I run the project in the simulator it asks if I want to allow network connections.
This doesn't occur in a normal XCode project using the library either.
Here are all the relevant files:
ApiDefinition.cs http://pastebin.com/p3S0H28X
withlink.cs http://pastebin.com/UR0X2pZ1
Build output http://pastebin.com/aTbdW2Gj
App output http://pastebin.com/Hxcuufgj
I appreciate any help you folks can provide.
Thanks!
[EDIT - 2015-08-26]
So I found this:
https://developer.xamarin.com/guides/ios/advanced_topics/native_interop/
Which unfortunately just seems to have lead me down a rabbit hole.
In our test app I put in the extra build arguments as stated in the article:
To Configure Xamarin.iOS To Link the Library, on the project options for your final executable (not the library itself, but the final program) you need to add in iOS Build's Extra argument (these are part of your project options) the "-gcc_flags" option followed by a quoted string that contains all the extra libraries that are required for your program, for example:
-gcc_flags "-L${ProjectDir} -lMylibrary -force_load ${ProjectDir}/libMyLibrary.a"
The above example will link libMyLibrary.a
You can use the -gcc_flags to specify any set of command line
arguments to pass to the GCC compiler used to do the final link of
your executable. For example, this command line, also references the
CFNetwork framework:
-gcc_flags "-L${ProjectDir} -lMylibrary -lSystemLibrary -framework CFNetwork -force_load ${ProjectDir}/libMyLibrary.a"
I replaced -lMylibrary and libMyLibrary.a with the correct names and then I got a ton of errors stating I had duplicate entries. I imagine because I am already referencing the binding DLL. Also -lSystemLibrary was throwing errors so I removed it.
Finally I tried this:
-gcc_flags "-L${ProjectDir} -framework CFNetwork"
And it builds without error, but I still get
-[MKNetworkOperation operationFailedWithError:] [Line 1288]
The app's build output can be found here:
http://pastebin.com/sLNUwYwX
Still hoping someone can help!
[EDIT 2015-09-03]
I still get the MKNetworkOperation error when running in the simulators. Now that I have my iPad back I've tried to test it on that. The app crashes completely when run on the iPad with the following output:
Foundation.MonoTouchException: Objective-C exception thrown. Name:
NSInvalidArgumentException Reason: -[ApiEngine randomStringOfLength:]:
unrecognized selector sent to instance 0x1742a3900 at
ObjCRuntime.Runtime.ThrowNSException (IntPtr ns_exception) [0x00000]
in
/Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/ObjCRuntime/Runtime.cs:167
at ObjCRuntime.Runtime.throw_ns_exception (IntPtr exc) [0x00000] in
:0 at at (wrapper native-to-managed)
ObjCRuntime.Runtime:throw_ns_exception (intptr) at at (wrapper
managed-to-native) ApiDefinition.Messaging:void_objc_msgSend
(intptr,intptr) at shared2you.Shared2YouAdManager.StartDetection ()
[0x0001b] in :0 at
s2u_xamarin_uni.AppDelegate.FinishedLaunching (UIKit.UIApplication
application, Foundation.NSDictionary launchOptions) [0x00021] in
/Users/manns41078/Desktop/Xamarin
Projects/s2u_xamarin_uni/s2u_xamarin_uni/AppDelegate.cs:50 at at
(wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain
(int,string[],intptr,intptr) at UIKit.UIApplication.Main
(System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in
/Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/UIKit/UIApplication.cs:63
at UIKit.UIApplication.Main (System.String[] args, System.String
principalClassName, System.String delegateClassName) [0x0001c] in
/Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/UIKit/UIApplication.cs:46
at s2u_xamarin_uni.Application.Main (System.String[] args) [0x00008]
in /Users/manns41078/Desktop/Xamarin
Projects/s2u_xamarin_uni/s2u_xamarin_uni/Main.cs:12
The library IS a FAT library and works in XCode no problem. Why is Xamarin giving me such heartache for a binding?? The java version was a cake walk.

SIGSEGV In System.MonoEnumInfo.get_enum_info

I'm trying to run a C# app in Mono on Linux. I'm using EF6, it has some types that are mapped to enums. The first time my app queries the DB (VistaDB) I get the following stack trace (its actually much larger...)
at <unknown> <0xffffffff>
at (wrapper managed-to-native) System.MonoEnumInfo.get_enum_info (System.Type,System.MonoEnumInfo&) <IL 0x0000e, 0xffffffff>
at System.MonoEnumInfo.GetInfo (System.Type,System.MonoEnumInfo&) [0x00076] in /build/buildd/mono-3.2.8+dfsg/mcs/class/corlib/System/Enum.cs:176
at System.Enum.GetNames (System.Type) [0x0002d] in /build/buildd/mono-3.2.8+dfsg/mcs/class/corlib/System/Enum.cs:334
at System.Data.Entity.Core.Metadata.Edm.EnumType..ctor (System.Type) <IL 0x00056, 0x001eb>
at System.Data.Entity.Core.Metadata.Edm.ClrEnumType..ctor (System.Type,string,string) <IL 0x00002, 0x00027>
at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.TryCreateEnumType (System.Type,System.Data.Entity.Core.Metadata.Edm.EnumType,System.Data.Entity.Core.Metadata.Edm.EdmType&) <IL 0x00027, 0x0009f>
at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.TryCreateType (System.Type,System.Data.Entity.Core.Metadata.Edm.EdmType) <IL 0x0003b, 0x000eb>
at System.Data.Entity.Core.Metadata.Edm.ObjectItemConventionAssemblyLoader.LoadTypesFromAssembly () <IL 0x0006c, 0x0010c>
at System.Data.Entity.Core.Metadata.Edm.ObjectItemAssemblyLoader.Load () <IL 0x00007, 0x0001f>
at System.Data.Entity.Core.Metadata.Edm.ObjectItemAssemblyLoader.LoadAssemblies (System.Collections.Generic.IEnumerable`1<System.Reflection.Assembly>,System.Data.Entity.Core.Metadata.Edm.ObjectItemLoadingSessionData) <IL 0x00019, 0x00072>
at System.Data.Entity.Core.Metadata.Edm.ObjectItemAssemblyLoader.LoadClosureAssemblies () <IL 0x00011, 0x00023>
at System.Data.Entity.Core.Metadata.Edm.ObjectItemAssemblyLoader.Load () <IL 0x00013, 0x00031>
...
Native stacktrace:
mono() [0x4b73d8]
mono() [0x50f13b]
mono() [0x423d22]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x10340) [0x7fd7577c0340]
mono(mono_class_from_mono_type+0x6) [0x525736]
mono() [0x5428a4]
[0x41d0d6f3]
...
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
Based on this I suspect there is a problem with Mono. I'm running Ubuntu 14 with Mono 3.2.8 (installed using apt-get). Has anyone else run into this? If I remove the enum mappings from EF will that fix the problem?
Update
I tried running the same on my macbook running Mono 3.4, and I got the same error. So I suspect it's a bug with Mono.
I upgraded to mono 3.6.1 and EF seems to be working well! Looks like mono 3.2.8 does not work with EF 6 and vista db

nunit-console crashes after running the tests

I'm using nunit-console for unit testing a Mono application on Debian. It runs all the tests and then crashes after reporting the failed tests. TestResult.xml is not generated. If I use nunit-console with either -out or -err argument, it runs the tests and then crashes before reporting the results.
Another weird thing is that if I cut down the number of tests (from about 800 to 100) it sometimes wont crash. Throwing out even more testcases seems to decrease the chance of crashing.
Platform: Debian 6, Mono 2.6.7, NUnit 2.4.7
Error details:
ERROR:../../mono/io-layer/handles-private.h:362:_wapi_handle_share_release: assertion failed: (info->handle_refs > 0)
Stacktrace:
at (wrapper managed-to-native) System.IO.MonoIO.Close (intptr,System.IO.MonoIOError&) <0x00050>
at (wrapper managed-to-native) System.IO.MonoIO.Close (intptr,System.IO.MonoIOError&) <0x00050>
at System.IO.FileStream.Dispose (bool) <0x000b7>
at System.IO.Stream.Close () <0x0001b>
at System.IO.StreamWriter.Dispose (bool) <0x000c8>
at System.IO.StreamWriter.Close () <0x0001b>
at NUnit.ConsoleRunner.ConsoleUi.Execute (NUnit.ConsoleRunner.ConsoleOptions) <0x007ed>
at NUnit.ConsoleRunner.Runner.Main (string[]) <0x00487>
at NUnit.ConsoleRunner.Class1.Main (string[]) <0x0000f>
at (wrapper runtime-invoke) <Module>.runtime_invoke_int_object (object,intptr,intptr,intptr) <0x00054>
Native stacktrace:
/usr/bin/cli() [0x487ad6]
/lib/libpthread.so.0(+0xeff0) [0x7fdd0379aff0]
/lib/libc.so.6(gsignal+0x35) [0x7fdd031da1b5]
/lib/libc.so.6(abort+0x180) [0x7fdd031dcfc0]
/lib/libglib-2.0.so.0(g_assertion_message+0x131) [0x7fdd03e175d1]
/lib/libglib-2.0.so.0(g_test_timer_start+0) [0x7fdd03e17b50]
/usr/bin/cli() [0x59d649]
/usr/bin/cli() [0x597658]
/usr/bin/cli() [0x597774]
/usr/bin/cli() [0x5095fc]
[0x4135b410]
Debug info from gdb:
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
** (/usr/lib/nunit/nunit-console.exe:7436): WARNING **: process_set_termination_details: error looking up process handle 0x403
Try running NUnit with /noshadow, I recall this used to help, long time ago.
This is an old bug https://bugzilla.novell.com/show_bug.cgi?id=318809. I've not been able to find an up-to-date xamarin bug of this.
See also, https://stackoverflow.com/questions/4513928/handling-single-file-with-multiple-threads-in-mono.
So try running nunit with /nothread or better still, try mono 2.10.8.

Categories

Resources