- Home /
DllNotFound when the plugin c++ dll links another c++ dll
I want to call c++ function in C# script. So I put the c++ dll in Assert/Plugins. However, if this dll (call it MainDll) dynamically links to another dll (call it SecondaryDll, which is also put into the Plugins folder), I will always get a DllNotFoundException.
I even write a VS C# project and calls MainDll linked with SecondaryDll in the same way as above, and everything is fine. Only not work in Unity C# script...
EDITED: No, just the MainDll as posted:
[DllImport("MainDll")] public static extern void FunctionMain1();
And in .cpp file which defines FunctionMain1, FunctionSec1 will be referenced, which I think is a purely c++ dynamic link:
// MainDll.cpp
include "SecondaryDll.h" extern "C" { DLL_EXPORT void FunctionMain1() { FunctionSec1(); } }
Also, SecondaryDll may exports C++ class since it is intended to be referenced only by another c++ project rather than unity c# script, like:
// MainDll.cpp
include "SecondaryDll.h" CSecondaryDll gObj;
extern "C" { DLL_EXPORT void FunctionMain1() { gObj.fun(); } }
Are you declaring both libraries and their functions? Like this:
[DllImport("$$anonymous$$ainDll")] public static extern void Function$$anonymous$$ain1(); [DllImport("$$anonymous$$ainDll")] public static extern void Function$$anonymous$$ain2(); ... [DllImport("SecondaryDll")] public static extern void FunctionSec1(); [DllImport("SecondaryDll")] public static extern void FunctionSec2(); ...
As @Sister$$anonymous$$y said in a comment, the Your answer box must be used only to answer the question - comments and replies must be posted using the add new comment button. I added the text to your question, and deleted the answer to make things clearer.
Answer by aldonaletto · Nov 21, 2011 at 03:46 AM
I never worked with more than one DLL in Unity, but I suspect that the problem is the SecondaryDll library location: since you've not declared it, Unity doesn't store this library wherever it's storing MainDll, and MainDll can't find it. You could try to declare any of the SecondaryDll functions in the same script you're declaring MainDll - I suppose Unity will store both in the same folder, allowing MainDll to find SecondaryDll. Another possibility would be to place SecondaryDll in the folder System32, the default DLL location - but this would complicate things when installing your game in another machine.
aldonaletto, thanks for your suggestions. But I wonder it may be not so easy to declare SecondaryDll's functions, since it can be from 3rd part, only export c++ classes and have no source.
If you know at least one function declaration, you can declare it to test this. If not, try to store the DLL in the Windows/System32 folder - if this works, you will know that it's a location problem.
You could of course also link the secondary Dll dynamically. Every 3rd party worth it's salt also delivers .lib files together with their .dlls
aldonaletto, I've tried storing dll in System32, but this doesn't work. I also tried export a dummy method in SecondaryDll and declare it in C# script; but only when C# code has called this dummy method, then it can successfully load Function$$anonymous$$ain1() which relies on the SecondaryDll. As you said, this is truly a path-searching problem. I find this post in forum (http://forum.unity3d.com/threads/28069-Plugin-consisting-of-multiple-dll-s-(F$$anonymous$$OD)) presents exactly the same issue, but also no better solution...
The solution of dummy method has drawbacks, since the SecondaryDll may depends on the third dll or even more, so it is a nightmare if all needs a dummy method...
i hope this issue has been fixed ..if not in my case i just placed the secondary dlls in unity application's root directory and it worked fine..
Answer by mdeletrain · Nov 21, 2011 at 01:53 PM
A temporary solution could be to pre-merge your dll's using library tools and to put the merged dll into Unity. This way you only have one library and problem is gone... but yeah, it's quite an awful solution !
Actually, even for just one custom dll, it is not alone but depends on several windows system dlls. Unity can successfully load the first dll, which proves it can find the system dlls. Now the problem is unity did not know the path of secondary custom dlls, is it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613050637im_/https://answers.unity.com/themes/thub/images/avi.jpg)