I am trying to call a simple native function (C++) from Unity3D. I have done the following:
Placed the libMSDKWrapper.so in Assets->Plugins->Android
This is my C# code
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class facebooktest : MonoBehaviour {
[DllImport ("MSDKWrapper")]
private static extern long getContextNative();
void Start () {
Debug.Log("Hello, World!");
long context = getContextNative(); // <-- Exception thrown here
Debug.Log(context);
}
}
I am getting the following exception:
system.entrypointnotfoundexception at [0x00000] in Filename unknown:0
I used the same libMSDKWrapper.so using java using the command
System.loadLibrary("MSDKWrapper");
and I was able to access and use the getContextNative() method.
Edit 1: I unzipped the contents of the apk and found that libMSDKWrapper.so file is present in test.apk/lib/armeabi-v7a
Edit 2: Here are the logcat logs.
03-25 11:40:17.543: D/dalvikvm(26176): Trying to load lib /data/data/com.disney.citygirl.goo/lib/libMSDKWrapper.so 0x41566348
03-25 11:40:17.568: D/MSDK(26176): Register Provider: SN_COMMUNITY
03-25 11:40:17.568: D/MSDK(26176): Register Provider: SN_FACEBOOK
03-25 11:40:17.568: D/dalvikvm(26176): Added shared lib /data/data/com.disney.citygirl.goo/lib/libMSDKWrapper.so 0x41566348
03-25 11:40:17.568: I/MSDK(26176): JNI_OnLoad called
03-25 11:40:17.573: D/dalvikvm(26176): Trying to load lib /data/data/com.disney.citygirl.goo/lib/libMSDKWrapper.so 0x41566348
03-25 11:40:17.573: D/dalvikvm(26176): Shared lib '/data/data/com.disney.citygirl.goo/lib/libMSDKWrapper.so' already loaded in same CL 0x41566348
03-25 11:40:17.598: I/Unity(26176): System.EntryPointNotFoundException: getContextNative
03-25 11:40:17.598: I/Unity(26176): at (wrapper managed-to-native) SocialAccess:getContextNative ()
03-25 11:40:17.598: I/Unity(26176): at SocialAccess.OnClick () [0x00000] in :0
03-25 11:40:17.598: I/Unity(26176):
03-25 11:40:17.598: I/Unity(26176): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
You are using a C++ plugin, but maybe your Unity3D version does not support it? I recall you need Pro to do that.
This page might provide a solution.
Make sure you are using the extern 'c' directive to avoid name-mangling issues. If you don't 'wrap' your calls in 'c' method names, you'll get this exact behavior because the c++ compiler 'mangles' or adds additional architecture-specific 'junk' to the method names. If you have questions, you can refer to my github project HERE
Focus on the main.cpp (the entry point which is the same in android-src/ as in the root). That is the pattern you will need to model your c++ dll after. This project will also show you how to build a c++ dll for android. Look at the README for full build instructions for both Ubuntu (and other Debian-like operating systems) and android.
#Virtlink I know its a pro-only feature, but it appears that Unity will just give a warning when using p/invokes. I'm not use if Unity fixed that in V4, but V3 allows you to use them anyways. The trial will also suffice.
Just as another note, is there a specific reason you NEED to use a C++ dll? In almost all cases, you can do what you want in managed C# code. Be absolutely sure you need this DLL as it will break cross-compatibility with a single codebase. With my dll, i decided to scrap my use-case for it and re-wrote the whole thing in c#.
Related
The problem has already been discussed earlier, but I got it after the update in September. Environment: the latest vs studio for mac, all the frameworks are updated, I also tried to switch to the preview channel, the error is the same. Actually the problem: after the update, the error of creating a native object began to appear.
From my renderer to - at Foundation.NSObject.InitializeObject (System.Boolean alloced) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/Foundation/NSObject2.cs:196 at Foundation.NSObject..ctor (Foundation.NSObjectFlag x) [0x0000d] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/Foundation/NSObject2.cs:131 at UIKit.UIResponder..ctor (Foundation.NSObjectFlag t) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/UIResponder.g.cs:69 at UIKit.UIView..ctor (Foundation.NSObjectFlag t) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/UIView.g.cs:83
Error message: Could not create an native instance of the type 'xxx': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
if set ObjCRuntime.Class.ThrowOnInitFailure = false; - native constructor returns null;
lib in Binding project is fat, all architectures are supported. i have no its code, only headers. ApiDefinition and Structs generated by sharpie, and works well.
mtouch options: --optimize=-remove-dynamic-registrar
please give some ways to solve, project is on fire..
If exist way to rollback all mono frameworks i can try, but its not good way.
Issue
When I try to use a 3rd-party library in my C# project, it almost always throws 'TypeLoadException' at me.
Details
The dependency I'm using in this case is a mongodb driver, though this issue replicates with other libraries such as Json.net and MySQL. I've tested with my own self-created small 'dependency' class and I was able to use this without it throwing an exception at me.
I'm able to build successfully, but when the .dll is loaded as a mod for a game, it logs out this rather unhelpful stack trace and fails to execute anything.
TypeLoadException: A type load exception has occurred.
at Test.Api.GameAwake () [0x00000] in <filename unknown>:0
at ModManager.GameAwake () [0x00000] in <filename unknown>:0
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
Logger:masterLogException(Exception)
Logger:Exception(Exception)
Log:Exception(Exception)
ModManager:GameAwake()
GameManager:Awake()
All that resides in 'Test.Api.GameAwake' is a simple connection handler taken from the mongodb documentation. There's nothing else going on in this project.
I can see that the assemblies do seem to load correctly, so I'm really out of ideas as to what to do next.
Could be a specific version dependency problem, or a missing assembly that you're not seeing.
I recommend checking a "dependency spy" program, such as http://ilspy.net/ and trying to load your assembly plugin to see what it's looking for.
First post. Let me know if I break any rules!
I would like to experiment with Pure Data and Unity 5. It seems uPD would be the best fit. I have followed the instructions on the github page and loaded the first sample scene. The unity console reports errors with the dll:
Failed to load 'Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll', expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64), but was IMAGE_FILE_MACHINE_I386. You must recompile your plugin for 64 bit architecture.
So I cloned the main libpd repo and used the included batch file to build an x64 dll. I replaced the libpdcsharp.dll in the unity project and the console now reports:
DllNotFoundException: Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll
LibPDBinding.LibPD.ProcessArgs[Int32] (System.Int32[] args, System.String& debug) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:402)
LibPDBinding.LibPD.SendMessage[Int32] (System.String receiver, System.String message, System.Int32[] args) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:354)
(wrapper synchronized) LibPDBinding.LibPD:SendMessage (string,string,int[])
LibPDBinding.LibPD.ComputeAudio (Boolean state) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:238)
(wrapper synchronized) LibPDBinding.LibPD:ComputeAudio (bool)
LibPDBinding.LibPD.Release () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:207)
(wrapper synchronized) LibPDBinding.LibPD:Release ()
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:79)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)
I have also tried copying the libPDBinding.dll file from the main libpd repo to the unity project no avail. I lack the understanding to know where to go next so any help at all would be appreciated!
Further Progress
It was suggested to try NuGet to obtain the binding dll. On this - I get an error about a framework mismatch between my unity project/visual studio solution and the nuget package libPDBinding version 0.10.0. This is to do with unitys profiles as described in an article titled: "Using NuGet in Visual Studio Tools for Unity". I cant post the link here because I am too new.
However, I think I am sucessful in compiling my own libPDBinding.dll. Apologies for dragging this out, just making sure I am doing this right:
After using libPD\mingw64_build_csharp.bat to compile a x64 version of libpdcsharp.dll, I reference this and libpd\libs\mingw64\libwinthread-1.dll in the VS solution and get the resulting LibPDBinding.dll which was built with .NET 3.5. As mentioned libpdcsharp.dll was compiled using the batch/make file, so am I correct to assume this file has been built with the correct settings?
I proceed to copy these 3 dll's the unity assets folder
For reference it's a blank unity project with uPD as the only package, loaded to a uPD example scene.
I still get some errors at run time and some visual studio warnings as follows.
Unity run time error:
EntryPointNotFoundException: libpd_safe_init
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:81)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)
Visual studio warnings x 50 or so:
Warning CS0436 The type 'LibPD' in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs' conflicts with the imported type 'LibPD' in 'LibPDBinding, Version=0.3.0.177, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs'. libpdinunity.CSharp D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\PureDataCommunicator.cs 87 Active
Thanks again for any help. Much appreciated.
Looking at your error, libpdcsharp is currently placed at <ProjectDirectory>/Assets/Magicolo/AudioTools/PureData/Plugins/
This DLL file should be placed at <ProjectDirectory>/Asset.
Also, make sure that libpdcsharp is compiled with .NET 2.0 or 3.5. This won't work if you compile it with .NET 4.0 and above.
I am the maintainer of C# binding for LibPD.
You also need to change the link to the correct libwinpthread-1.dll version (64 bit instead of 32). In your final project you need all 3 dlls:
LibPDBinding.dll (the managed wrapper)
libpdcsharp.dll (the native dll)
libwinpthread-1.dll (for enabling POSIX threads on Windows)
Also: Have you tried the version from NuGet? No need to compile your own version of the binding.
EDIT: Your warning are from a different source: You have two classes named LibPD, and the C# compiler does not know, which one to use.
I am not sure, if these types are in the same namespace.
If they are in the same namespace, then change your namespace.
If they are in different namespaces, add a line using LibPd = LibPDBinding.LibPD; if you want to use the version from NuGet, or using LibPd = <yournamespace>.LibPD; if you want to use your version.
It's working. The uPD github code has an error as far as I can tell. So hopefully it gets fixed. In the meantime I just started the process from scratch and this is what I did to get the uPD test scene working in unity 5:
Get uPD from github. Theres a unity package in there
Get libPD from github.
Follow the instructions on the libPD github page to compile a x64
vresion of libpdcsharp.dll
replace the existing libpdcsharp.dll inside your unity project with
the x64 version you compiled
locate libpd\libs\mingw64\libwinthread-1.dll in the libpd files and
place this in the your unity project - the same place as the other dll is fine. I
don't think it matters where you put the Dll's (at least it made no
difference in my tests)
Run the project and notice you get an error in unity EntryPointNotFoundException: libpd_safe_init
double click the error to open up the problem script.
Find the following line of code: [DllImport("libpdcsharp", EntryPoint="libpd_safe_init")]
Change it to [DllImport("libpdcsharp", EntryPoint="libpd_init")] That's the real entry point for the init function in libpdcsharp. I don't know what libpd_safe_init is.
Thanks to the contributors who helped me to troubleshoot and find the issue!
I'm trying to compile a C# project using Mono and am getting the following exception during compilation using xbuild:
Error: Fody: An unhandled exception occurred:
Exception:
The requested feature is not implemented.
StackTrace:
at Mono.Cecil.Mdb.MdbReaderProvider.GetSymbolReader (Mono.Cecil.ModuleDefinition,System.IO.Stream) <0x00017>
at Mono.Cecil.ModuleReader.ReadSymbols (Mono.Cecil.ModuleDefinition,Mono.Cecil.ReaderParameters) <0x000cb>
at Mono.Cecil.ModuleReader.CreateModuleFrom (Mono.Cecil.PE.Image,Mono.Cecil.ReaderParameters) <0x000e7>
at Mono.Cecil.ModuleDefinition.ReadModule (System.IO.Stream,Mono.Cecil.ReaderParameters) <0x00063>
at ModuleWeaver.GetTemplateModuleDefinition () <0x00133>
at ModuleWeaver.ImportAssemblyLoader (bool) <0x00027>
at ModuleWeaver.Execute () <0x00113>
at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,object) <0x0004c>
at InnerWeaver.ExecuteWeavers () <0x002cb>
at InnerWeaver.Execute () <0x00123>
Source:
Mono.Cecil.Mdb
TargetSite:
Mono.Cecil.Cil.ISymbolReader GetSymbolReader(Mono.Cecil.ModuleDefinition, System.IO.Stream)
(Hybrasyl)
The project is using Fody, which appears to be what's throwing the error. From what I can tell Fody should support Mono. I'm using the latest version of Mono (v4.0.1.0), Fody (v1.28.3.0), and Costura Fody (v1.3.3.0).
Q1: What feature is the compiler claiming is not implemented? I'm having trouble understanding the error message.
Q2: Is this expected behavior? I'm having trouble finding references to Fody's status with respect to Mono support and am not sure if this is truly missing support or whether I'm doing something incorrectly. I'm new to the .NET + Mono world so let me know if I need to clarify further.
Thanks!
I'm using Xamarin Studio on a Mac, with clrzmq included via NuGet.
clrzmq references on libzmq.dll. My app compiles fine, but when I try to run it, I get this:
Unhandled Exception:
System.DllNotFoundException: libzmq
at (wrapper managed-to-native) ZMQ.C:zmq_init (int)
at ZMQ.Context..ctor (Int32 io_threads) [0x00000] in <filename unknown>:0
at FeatureSpike.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
libzmq.dll is definitely there in the build target directory. Does anyone know why it's not being found?
you might want to try using netmq (https://github.com/zeromq/netmq), is a 100% C# port of zeromq and might be easier to use then calling pinvoke.
if you're still looking for a solution...
You can use clrzmq4 and add statically linked libzmq and libsodium as native libraries to your xamarin solution.
Ensure you use the very latest version of clrzmq4,we just got it running today...
Is libzmq.dll next to your executable in the build output directory ?
The clrzmq NuGet package includes a PowerShell install script to set the build action for the libzmq.dll so it is always copied to the output directory. If you added the NuGet package on the Mac then the PowerShell script will not be run. So you will need to set the build action manually.
libzmq.dll seems to be a native dll compiled for Windows so it will not work on the Mac. So it looks like you will have to compile clrzmq on the Mac.
Have you created a dllmap in your config?
<configuration>
<dllmap dll="libzmq.dll" target="/usr/local/Cellar/zeromq/4.0.4/lib/libzmq.dylib"/>
</configuration>