**EDIT: SOLUTION IS BELOW! - look for the header "**SOLUTION**"**
Hello,
So I'm trying to use a C++ native plugin (balls.dll) with a C# script in my project (Unity 5.5.0f3). The dll is inside my assets/plugins folder.
My code (simplified) looks something like this:
public class Bigballs : Monobehaviour
{
[DllImport("balls")]
public static extern int Init(); //returns 0 if initialized, else returns -1
int x;
void Start()
{
x = Init();
}
}
While in editor, I hit run and Unity responds with "EntryPointNotFoundException: Init" at the line "x=Init()". What can I do to fix this? I have tried searching Unity forums and googled, it seems no one is really having the same issue in regards to native plugins.
If it is worth mentioning, I have worked with native plugins in the past, and initially, I tried [DllImport("balls.dll")], which is how I was able to access my dll methods in an older project. However, this time I got a "DllNotFoundException". After some quick googling, it was suggested that dropping the .dll would get rid of the DllNotFoundException in the DllImport.
I greatly appreciate your help in advance.
**SOLUTION**
So here is what I did that FINALLY got it to work. I created a constructor to find my plugin, solving the DllNotFoundException. What it does is set the current path to the location of my plugin, which is in Assets/Plugins/x64.
static Bigballs() //constructor
{
String currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
String dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x64";
if (currentPath.Contains(dllPath) == false)
{
Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
}
}
This resulted in the Dll being found, but still returns EntryPointNotFoundException. So here is the annoying part. You have to crack open the .dll, or if you're lucky and have a .lib, you can use that. What you want to do is find the Entry Point of your function inside the .dll or .lib, I am going to describe how I did it using Visual Studio and a .lib, but this should work for another program that can open .dlls and with a .dll.
So to see the contents of Balls.lib, I dragged it into Visual Studio's solution explorer and then opened it. Inside balls.lib, there is a column of what seems like jibberish, but these are actually entry points. You have to pinpoint the exact function you are trying to import, so for me it is Init(), and inside the .lib, the entry point looked like this:
?Init@@YA?AW4variable@@XZ
One thing I noticed, all the functions' entry points seem to be within a pair of periods ('**.**'), so just copy and paste the entry point, format it to have no spaces, and add it to your DllImport call, like so:
[DllImport("SimballMedicalHID.dll", EntryPoint = "?Init@@YA?AW4variable@@XZ")]
And that's how I eliminated these two annoying problems. I spent days stuck on my project and I hope that at least some of this is helpful for anyone else experiencing similar issues.
↧