DllNotFoundException in unity3d plugin for c++ dll

Go To StackoverFlow.com

7

I am working on the Unity Plugin project and try to import the c++ native dll from 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 work properly. All dependent dlls are present at same hierarchy, but I still end up in dllnotfound exception.

Any help, much appreciated!!

2012-04-03 23:40
by Raj
is the plugin available somewhere - Alex 2013-12-18 16:09


6

Thanks to this Unity forum post I came up with a nice solution which modifies the PATH-environment variable at runtime:

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

    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 make sure that the constructor is run at editor launch:

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

With this script there is no need to copy around DLLs. The Unity editor finds them in the Assets/.../Plugins/...-folder and the executable finds them in ..._Data/Plugins-directory (where they get automatically copied when building).

2015-10-14 11:35
by Lars Bilke
I couldn't get the static to work in Unity 2017.3.1f1. At runtime I got: "UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead." I switched it to use Awake() and it worked fine & the editor & in my build - rusty 2018-03-22 16:30


4

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

Place any dependency DLLs that are not directly accessed by your scripts in Project. This will allow your program to run in the editor.

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

(Tip: you can use Dependency Walker look at you DLLs and see what they depends on.)

2014-01-25 03:18
by sirbrialliance


3

Well I got it working. For others who may face this problem, if you have more than one dll, you need to put the secondary dlls at root level of the Unity editor (e.g. C:\Program Files\Unity\Editor) and the actual referenced dll from script into plugins folder. This worked for me.

2012-04-04 20:24
by Raj
A better place for the dll files would be in the project folder. Just place the dll files in the same folder as the "Assets" and "Library" folders - TrolleFar 2012-07-04 00:25
@TrolleFar this worked for me, saved me hours of headaches thank you guys for posting the question, answers, and comments - hsmith 2013-03-06 06:06
I keep get those Exception even I place dll file in project directory and in folder same as exe file after build. Are Visual C++ Redistributable needed? I build dll from Visual C++ 2008 SP1 and installed all Visual C++ 2008 Redistributable available in microsoft website still get exception dll not found in other machine - Akirayjin 2013-09-05 03:53
I tried all of these solutions for hours... turned out my external DLL was a 32 bit one, and my Unity project was 64-bit. Was getting DllNotFoundException so got led down the garden path... another thing to watch out for - jozzas 2014-08-11 04:21
Tried everything, nothing worked. Please help https://stackoverflow.com/questions/54898629/unable-to-find-type-or-namespace-of-imported-dl - vipin8169 2019-02-28 03:02


2

I spent one day with this error. My issue was that Android doesn't get the library and always get and DDLNotFound error. My solution was:

1.- Be sure that you have the libraries for the proper architecture in the Plugins folder.

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

2.- Check that Unity recognizes them as libraries. If you select them in the Project tab you should see them as a library and the platform and architecture related.

3.- After the build (don't close Unity Editor!), you can check in the Temp/StagingArea/libs if your libraries are there. If there are there for sure the libraries are going to be in the APK. As a double check, you can open your APK (change to zip extension) and see the libraries in the lib folder.

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

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

[DllImport ("dosomething")]

I hope this work for you :)

Cheers.

2017-09-27 18:00
by Alexis Barra
This helped me. I am using Unity 2018.3 and the proper architecture is now Plugins/Androidx86 and Plugins/Androidarmeabi-v7a. I also put a library in Plugins/Androidarm64-v8a but while Unity recognises it as arm64 it is not used.. Androidx86_64 is not supporte - Adrian G 2019-02-14 10:09


1

Make sure the following chacklist is satisfied:

  • Plugins should all stay in a folder called Plugins.
  • The architecture your dll is built for (x86 or x86_64) must correspond to the architecture version of Unity Editor. Unity Editor 32-bit will not load 64 bit plugins and viceversa.
  • If you are targeting both 32 and 64 bit architectures you should put your dlls in special named folders inside the Plugins folder. The names are Plugins/x86 for 32 bit dlls and Plugins/x86_64 (x64 also works) for 64 bit dlls.
  • Visual C++ Redistributables must be installed. I have all from 2008.
  • When you build all your dlls should be copied into the root where your executable is (and again built for the correct x86/x64 architecture)

If you keep getting a namespace error it means the dll you are importing has unmanaged code and it must be wrapped into another managed dll Pugin in order to work.

These threads are a bit outdated but still relevant

DLLNotFoundException - Unity3D Plugin

Unity internal compiler error with custom dll

2016-01-26 21:31
by SteakOverflow
Hello, could you please help me with this - https://stackoverflow.com/questions/54898629/unable-to-find-type-or-namespace-of-imported-dl - vipin8169 2019-02-28 02:56


0

just put the dlls under Plugins folder and that works for me

2014-10-08 10:40
by fieldChao