- Home /
How to properly use my own DLLs in Unity?
I have been working on an unity tool and at this point I need an external library to be used (DLL) but at the moment of using any referenced function:
[DllImport("soloud_x86.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr Soloud_create();
public Soloud()
{
objhandle = Soloud_create();
}
It return an error saying DLLNotFoundException, I supposed that the problem was the [Dllimport] but error is thrown in the fifth line that is where the function is used. Because of this I don't know if it finds the Dll and then it doesn't read properly the function or it doesn't even find the library.
I have tried putting the Dll with almost every build configuration and in every plugins/editor folder but it is always the same.
Answer by Legend_Bacon · Oct 09, 2018 at 12:22 PM
Hello there,
I think you should take out the ".dll" from the name of your file. Also, make sure it is under your Assets/Plugins folder, and that when you click on it all the settings are properly set (Editor vs standalone, 32 vs 64, etc...).
Hope that helps!
Cheers,
~LegendBacon
@Legend_Bacon Thanks for the response but I have tried doing the things you said and it keeps telling DLLNotFoundException.
Answer by Bunny83 · Oct 11, 2018 at 10:23 AM
Actually this seems to be a bug in Unity. I just downloaded the SoLoud audio library myself and tried a minimum setup. I created a standalone build and got this in the log:
Fallback handler could not load library path/Build_Data/Mono/soloud_x86.dll
Fallback handler could not load library path/Build_Data/Mono/.\soloud_x86.dll
Fallback handler could not load library path/Build_Data/Mono/soloud_x86.dll
Fallback handler could not load library path/Build_Data/Mono/soloud_x86
Fallback handler could not load library path/Build_Data/Mono/.\soloud_x86
Fallback handler could not load library path/Build_Data/Mono/soloud_x86
Fallback handler could not load library path/Build_Data/Mono/libsoloud_x86.dll
Fallback handler could not load library path/Build_Data/Mono/.\libsoloud_x86.dll
Fallback handler could not load library path/Build_Data/Mono/libsoloud_x86.dll
DllNotFoundException: soloud_x86.dll
The actual location of the dll is
path/Build_Data/Plugins/soloud_x86.dll
Which isn't ever checked as it seems. I know i used native plugins in the past, even some i've written myself but didn't test those with the current Unity version. Maybe they messed something up. Since the dll is actually copied into the plugins folder of the build Unity seems to correctly identify the dll as native plugin. Though the resolution of the path seems to fail for some reason.
I'll suggest you file a bug report with a repo project. Also you may include a link to this question.
Note if i manually copy the dll
to one of the locations that were checked
, the errors go away. I can't verify the dll works properly since i just create a Soloud instance and call init in start and deinit in OnDestroy.
edit
Actually i was wrong with the last sentence. The locations that were checked actually doesn't exist. The "Mono" folder is no longer inside the _Data folder but next to the exe. It did work when i placed the dll right next to the exe. Inside the _Data folder it did not work. To me this looks like they have some outdated search path information in the dll resolver.
So the only locations that did work are if i place the dll into the build folder, right next to the exe and when i create a Mono folder inside the _Data folder and place it there. This clearly need to be fixed by Unity. Especially when testing in the editor