Native plugin DllNotFoundExeption in Unity 4 (works in 5)

Hi,

I am trying to include a native C plugin for use in the Editor but cannot get it going in Unity 4 on Windows 8. I can compile it into a bundle and include it in the project in Unity 4 on Mac OS X. I can also compile it into a 64 bit shared dll and include it in Unity 5 on Windows. However, Unity 4 on Windows spits out a DllNotFoundException when I try to use either a 32 or 64 bit version of the shared dll.

I have tried placing the dll in: ‘Assets/’, ‘Assets/Editor’, ‘Assets/Plugins’, ‘Assets/Plugins/Editor’, and ‘Assets/Plugins/x86’, all to no avail (always spits out ‘DllNotFounddException: PLUGIN_NAME’, except when in the x86 folder, which produces:
‘DllNotFounddException: FULL/PATH/TO/x86/PLUGIN_NAME’).

Since the code is c99, I can’t compile using VisualStudio. Instead, I’m using MinGW (tried on Linux, MacOSX and Windows) by running:

i686-w64-mingw32-gcc -m32 -mwindows -c --std=c99 -DBUILDING_EXAMPLE_DLL *.c *.h
i686-w64-mingw32-gcc -m32 -mwindows -shared -o myplugin.dll *.o

Has anybody encountered this before?
Is there a reason that the plugin would work in Unity 5 but not Unity 4?

Thanks in advance…

//=======================
EDIT - adding more info:

I’m using:
Windows 8 64-bit

  • running in Bootcamp on a recent
    Macbook Pro 13
  • latest MinGW Unity
  • 4.6.5f1.

Also running the file command on the dlls in cygwin produces:

  • 32 bit: "PE32 executable (DLL) (console) Intel 80386, for MS Windows"
  • 64 bit: "PE32+ executable (DLL) (console) x86-64, for MS Windows"

so it seems to me that the produced dlls are compiled for the correct OS and architecture.

//======================= EDIT 2:

I’ve successfully compiled a test plugin (containing a function that adds two numbers) using the same commands as above and was surprised to see it works from Unity 4 when I DllImport the absolute file path (as opposed to just the dll name). Unfortunately, Unity still doesn’t find the actual plugin I’m interested in even when using the absolute file path.

(Also, to preempt any such suggestions, switching the project to Unity 5 to get this going is not an option).

I had this same problem using a 32-bit version of a dll on 32-bit Unity 5. I believe Unity 4 editor only supports 32-bit so this may be the same issue.

It looks like this is a misleading message and Unity can’t find one of the dll’s your dll has a dependency on. I finally got Unity to load mine by using the -static-libgcc flag to prevent mingw from linking with the shared version of the standard libraries.

However, I couldn’t figure out how to get Unity to find/load the other dll so this may still be a problem for you if you have other dependencies and you can’t link to them statically.

You can use http://www.dependencywalker.com/ to help figure out what your dll’s dependencies are.