x


Unity Plugins for .DLL

Now I writing a simple program with c# and bulding .DLL (This is the c#)

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

namespace TestDll { public class TestClass { Queue myQueue = new Queue(); private System.Timers.Timer timerA; int direction = 0;

    public TestClass()
    {
        timerA = new System.Timers.Timer(100);
        timerA.Elapsed += new System.Timers.ElapsedEventHandler(timerEvent);
        timerA.Enabled = true;
    }

    private void timerEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        myQueue.Enqueue(direction);
        direction = (direction + 1) % 4;
        if (myQueue.Count > 10)
        {
            myQueue.Dequeue();
        }
    }

    public int GetD()
    {
        int r = -1;
        if (myQueue.Count > 0)
        {
            return Convert.ToInt32(myQueue.Dequeue());
        }
        else
        {
            return -1;
        }
    }

    public int iNowMiliSec()
    {

        int iRtn = 0;
        try 
        {
            iRtn = DateTime.Now.Millisecond;
        }
        catch (Exception errMsg)
        {
            Debug.WriteLine(errMsg.ToString());
        }
        return iRtn;
    }
}

}

And I put it(DLL) to Unity to test plugins. I create a c# to import the function in Unity.

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

public class PluginsTest : MonoBehaviour {

[DllImport ("TestDll")]
private static extern int iNowMiliSec();

[DllImport ("TestDll")]
private static extern int GetD();

void Start () {
Debug.Log(iNowMiliSec());
Debug.Log(GetD());
}

}

But when I play...Unity say.... EntryPointNotFoundException: iNowMiliSec PluginsTest.Start () (at Assets/PluginsTest/Script/PluginsTest.cs:12)

so...where I miss or wrong??

more ▼

asked Dec 13 '11 at 02:28 PM

ZroeX gravatar image

ZroeX
77 14 17 21

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

You shouldn't have to use DllImport for managed dlls, but unmanaged dlls.

In order to use your TestDll.dll, copy it to Unity as you've done. Then your code that uses it should look something like this:

using UnityEngine; 
using System.Collections;
using TestDll;

void Start()
{
    TestClass obj = new TestClass();
    Debug.Log(obj.iNowMiliSec());
    Debug.Log(obj.GetD());    
}
more ▼

answered Dec 13 '11 at 03:06 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

wow!! It's working!! Thank you a lot!!

And can I ask something about you say?

What is you say "managed dlls"? Why it can't be use? And what is unmanaged dlls?

Sorry to ask so many... You can teach me more?

Dec 13 '11 at 03:19 PM ZroeX

Managed code is code that runs in .NET or MONO framework. It's code that is compiled into IL. Basically VB .NET, C#, Managed C++ and other ".NET languages" are managed. Unmanaged code is also known as native code, and is code that have been compiled into machine instructions (Native C++/C and other languages). You are using your managed DLL by including it in the project and using the namespace declared inside it. Unmanaged code has to be dynamically loaded, bound to and conform to special calling conventions unless defined otherwise. Long story short (and simplified): Only use DllImport if you have a typical C++ dll that you want to use.

Dec 13 '11 at 03:23 PM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x395
x233

asked: Dec 13 '11 at 02:28 PM

Seen: 2572 times

Last Updated: Dec 13 '11 at 03:24 PM