dllNotFoundException

I hope someone can help with this problem.

I am trying to get a dll plugin to work using a simple example.

My C# dll code is as follows and is saved as hello.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hello
{
   
    public class Class1
    {
        float Foo() { return 5.0F; }

    }
}

I have placed the dll in the Assets folder and created a GameObject with Script.

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class NewBehaviourScript : MonoBehaviour {
	
	[DllImport("hello")]
	private static extern float Foo();
	
	void Awake()
	{
		print("Starting dll");
		print(Foo());
	}

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

On running, the Console shows the Starting dll message but then a dllNotFoundException: hello.

Any help would be much appreciated.

Regards

Eric

A couple things that may be a problem - if the DLL is written in C# like this, you can actually just import it as an asset and treat it like any other C# script. So if you made Foo() a public static method you could use it in your script with:

print(hello.Class1.Foo());

If it’s not static, you’d need to instantiate the class (and Foo() would still need to be public).

hello.Class1 c = new hello.Class1();
print(c.Foo());

Or with a ‘using’ to shorten calls…

using hello;

...

Class1 c = new Class1();
print(c.Foo());

If you’re using Unity Pro, you can use non-.NET dlls too with the DllImport, but they need to be placed in the Plugin folder. Also I believe you need the .dll extension on the dllimport, but I may be mistaken.

Beezir is right, just drop it in.
DLLImport and interop are only needed for regular C dlls (which require pro).

What you need to keep in mind with your dll there though is that you are using .NET 3.0+ functionality and Unity is mono 1.2.5, so your users are forced to have it installed as well and you naturally can not use it for anything else than standalone distribution

The compiler won’t include any of the Linq stuff if it’s not called in the code outside of the using statement. If it does get compiled in to the dll, unity will throw an error when mono encounters the stuff it doesn’t understand.

Thank you for your replies.

Basically I am just trying to make sure I can get a basic dll plugin to work.

I do have the Pro Licence.

I have put the dll in a Plugins folder and the dll seems to be recognised but not the function.
I still get an error. This time:

EntryPointNotFoundException: Foo

I have also tried C++ code for the dll

extern "C" float Foo();

using namespace System;

namespace hello {

	public ref class Class1
	{
		
		 float Foo() { return 5.0F; }
	};
}

but get exactly the same error

EntryPointNotFoundException: Foo

You need to tell to compiler that you function should be visible outside, as MS does not seem to think that extern keyword is for that.

extern "C" {
   __declspec( dllexport ) float Foo(); 
}

And if you want your plugin to compile also in Mac you need to do something like this:

extern "C" {
#if (defined(WIN32) || defined(__WIN32__))
   __declspec( dllexport ) float Foo(); 
#else
   float Foo();
}

In addition, I think Foo() has to be accessible to the outside world. Use the extern block with the __declspec as shown by lankoski to get C linkage in the DLL and move Foo() up to the global namespace.

Or you could export the whole class like this:

class __declspec(dllexport) hello
{
   ...
}

And finally, if it’s compiled to managed code I don’t know if you can use any of this, even in C++. I think managed code needs to use the previously mentioned method of adding it as an asset and calling it like a .NET library. Managed C++ has it’s own way of externing things for DLLs, but I have no idea if this can be called in unity like a native DLL as I don’t have the proper compiler:

__gc public class hello
{
   ...
}

Thanks for all your help.

The C++ works if I remove the class and include

 extern "C" { 
   __declspec( dllexport ) float Foo(); 
}

as advised.

Many thanks, much appreciated.