DllNotFoundException in a single 3D plugin for C ++ dll

I am working on a Unity Plugin project and trying to import a native C ++ dll from a C # file. But I keep getting dllnotfoundexception.

C ++ dll code:

extern "C" { extern __declspec( dllexport ) bool IGP_IsActivated(); } 

C # code:

 [DllImport("mydll")] private static extern bool IGP_IsActivated(); 

Dll is in place, and FIle.Exists is working correctly. All dependent dlls are in the same hierarchy, but I still get the dllnotfound exception.

Any help very valuable!

+7
source share
6 answers

Thanks to this, Unity Forum Post I came up with a nice solution that modifies the PATH -environment variable at runtime:

  • Insert all the DLLs (both DLLs that Unity interacts with and their dependent DLLs) into Project\Assets\Wherever\Works\Best\Plugins .
  • Put the following static constructor in the class that the plugin uses:

     static MyClassWhichUsesPlugin() // static Constructor { var currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process); #if UNITY_EDITOR_32 var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "SomePath" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x86"; #elif UNITY_EDITOR_64 var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "SomePath" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x86_64"; #else // Player var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins"; #endif if (currentPath != null && currentPath.Contains(dllPath) == false) Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process); } 
  • Add [InitializeOnLoad] to the class to ensure that the constructor starts when the editor starts :

      [InitializeOnLoad] public class MyClassWhichUsesPlugin { ... static MyClassWhichUsesPlugin() // static Constructor { ... } } 

With this script, there is no need to copy the DLL. The Unity editor finds them in the Assets/.../Plugins/... folder, and the executable finds them in the ..._Data/Plugins directory (where they are automatically copied when created).

+5
source

Put the Unity DLL (s) interfaces in Project \ Assets \ Wherever \ Works \ Best \ Plugins.

Put any dependency libraries that do not have direct access to your scripts in Project. This will allow your program to work in the editor.

When you create, copy the dependency DLL files again, this time to the root directory of the assembly (next to the generated executable). This should allow your application to load them at runtime.

(Tip: you can use Dependency Walker to view your DLL files and see what they depend on.)

+4
source

Well, it works for me. For others who may run into this problem, if you have multiple DLLs, you need to place the secondary DLLs at the root level of the Unity editor (e.g. C: \ Program Files \ Unity \ Editor) and the actual dll link from the script in the plugins folder . It worked for me.

+3
source
  • Plugins should be in the Plugins folder.
  • The architecture of your dll library for (x86 or x86_64) should match the architecture version of Unity Editor. Unity Editor 32-bit will not load 64-bit plugins and vice versa.
  • If you are targeting 32-bit and 64-bit architectures, you should put your DLLs in special named folders inside the Plugins folder. The names are / x 86 plugins for 32-bit DLLs and / x 86_64 plugins (x64 also works) for 64-bit dlls.
  • Visual C ++ redistributable components must be installed. I have everything since 2008.
  • When you create all your DLL files, they must be copied to the same path of your executable file (and built again for the correct x86 / x64 architecture).

These streams are a bit outdated, but still relevant.

DLLNotFoundException - Unity3D Plugin

Unity internal compiler error with custom dll

+1
source

I spent one day with this error. My problem was that Android does not get the library and always gets a DDLNotFound error. My solution was:

1.- Make sure you have the libraries for the correct architecture in the Plugins folder.

Plugins / Android / x86 and plugins / Android / armeabi -v7a if your FAT build settings (x86 and arm)

2.- Make sure Unity recognizes them as libraries. If you select them on the Project tab, you will see them as a library associated with the platform and architecture.

3.- After the build (do not close the Unity editor!), You can check Temp / StagingArea / libs if your libraries are there. If there are any, the libraries will be in the APK. As a double check, you can open the APK (change for the zip extension) and browse the libraries in the lib folder.

4.- In C #, you must remove any lib prefix in the name of your library, for example:

If your library name is "libdosomething.so", you should call it

[DllImport ("dosomething")]

I hope this work is for you :)

Greetings.

+1
source

just put the dll in the plugins folder and it works for me

0
source

All Articles