- Home /
External dll: namespace recognized by mono, not by UT
Hello all,
I'm trying to use a third party IO hardware board to read pots and turn on lights in my game. This board comes with a dll that lets you get and set the data you need, however the functions are not immediately available at the surface, but within a namespace (Automation.BDaq.)
I went into mono develop project->edit references and and added those. Mono now recognizes the namespace and even the auto-complete works. However, the Unity editor still gives me the fault "namespace 'Automation' could not be found. are you missing a using directive or an assembly reference?".
Does anyone of you know how to add the reference to Unity itself?
Additional notes:
did copy the dll to the Assets\Plugins folder.
started the project using JS and later found out that only C# can be used to access dlls. Thus I made a single C# file as a wrapper, which I believe can be accessed by the other js scripts (still haven't attempted that though).
-The board manufacturer provides lots of examples, but they're aimed at stand-alone applications not based in unity, so that has been of limited use.
.
Thanks for any hint you guys can provide, here's my C# script:
.
plugin_interface.cs---------------------------------------------------------------------------
using UnityEngine;
using System.Runtime.InteropServices;
//using Automation.BDaq;
class IoBoard : MonoBehaviour {
[DllImport ("BioDaq.dll")] //dll must be in AssetsPlugins folder
//private static extern Automation.BDaq.InstantAiCtrl instantAiCtrl1; private
static extern int Read(int ch,int dataRaw);
Automation.BDaq.InstantAiCtrl instantAiCtrl1 = new Automation.BDaq.InstantAiCtrl();
void Start(){
}//
float get_AD(float channel){
//return AI_Instant(channel);
return 0;
}
}
I am not doing exactly what you are doing, but I can tell you what I had to do to get some external DLLs working. Couple things:
I am using C# in Visual Studio.
I am using a C# DLL, so I don't have to do any DLLImport interop stuff.
$$anonymous$$y DLL is actually using a second DLL that my code doesn't use directly (which I think the solution for is what you have to do).
Here is what I did to get this working:
I added a reference to the first DLL in Visual Studio. I have this DLL in my Assets folder.
I added my namespaces in code.
I put the second DLL in my assets folder (which didn't work)
I put the second DLL in the C:\Program Files (x86)\Unity\Editor folder.
Here is a thread that helped me out a bunch and might help you:
http://forum.unity3d.com/threads/31083-DllNotFoundException-when-depend-on-another-dll
Answer by volkswagenb · Jun 21, 2013 at 06:11 PM
Hi Amoral, thanks for your answer.
That makes sense, and after playing around and digging some more I was reaching the same conclusions. I made a little test dll in VS C#, and put it in the Assets-Plugins folder. Unity succesfully ran it and received a return test value.
When it comes to C++ dlls (unmanaged), it seems the [DllImport ("name")] scheme is broken in Unity, especially if the dll has the functions within namespaces and classes internally, as opposed to bare-metal. I'm dropping this route.
Instead, like you say I will make a wrapper C# dll to interface with the actual dll needed. It seems this will do the trick. I'm glad you pointed out the "C:Program Files (x86)UnityEditor" folder curveball.
Now I just have to wait for the actual hardware board to arrive so I can test; I'll follow up with results.
Thanks again!!
EDIT:
Ok here's what I ended up doing:
It turns out the managed dll from my board supplier wants to access System.Drawing, which is a no-no in Unity ARGHHH as confirmed by this http://va.lent.in/blog/2013/01/06/unity3d-and-interactive-installations/.
So I had to go back to the unmanaged version of the dll and make a VS C++ interface dll between the target and the C# unity script.
The interface dll instantiates the target class and exports its own bridge functions that can be called by the Unity C# script, using [DllImport]. I got the insane syntax for exporting from another answer thread, but I don't have the link anymore.
Here's the code:
VS C++ interface dll (simplified) ---------------------------------------------------------------
#include <stdafx.h>
#include <stdlib.h>
#include <stdio.h>
#include "bdaqctrl.h"
using namespace Automation::BDaq;
//Analog input object
InstantAiCtrl* instantAiCtrl;
//Declare our functions for export
extern "C" __declspec(dllexport) int initialize();
extern "C" __declspec(dllexport) double get_instant_analog_data(int channel);
/////////////////////////////////////////////////////////////////////////////
__declspec(dllexport) int initialize(){
//Instantiate Advantech IO-board instant-analog control from BDaq.dll
instantAiCtrl = AdxInstantAiCtrlCreate();
return result;
//
}//
/////////////////////////////////////////////////////////////////
__declspec(dllexport) double get_instant_analog_data(int channel){
double value;
if(instantAiCtrl!=0){
instantAiCtrl->Read(channel,value);
}//
else{
value=0;//result=1;
}
//}
return value;
//
}
Unity C# script (simplified) --------------------------------------------------------------------------------------------------------------------
Note:You must carefully specify the "entry point' with the name of the function, which is kind of stupid if you ask me.
using UnityEngine;
using System.Runtime.InteropServices;
public class IoBoard{//
public int state; //0=disconnected. 1= connected
public double[] Ai_data;
int result=0;
//Import the functions from our IOdll wrapper
[DllImport ("IOdll.dll", EntryPoint = "initialize")] //dll must be in Unity\editor folder //XXAssets\Plugins folder
static extern int initialize();
//
[DllImport ("IOdll.dll", EntryPoint = "get_instant_analog_data")]
static extern double get_instant_analog_data(int channel);
//-----------------------------------------------------
public int init(){
result=initialize();
Ai_data = new double[8]; //initialize array
Di_data = new int[8]; //initialize array
Do_data = new int[8]; //initialize array
//
state=result;
return result;
}//
//------------------------------------------------------------------
public double get_instant_analog_input(int channel){
double val;
//get data
val=get_instant_analog_data(channel);
return val;
}//
}
Cool, glad to hear you are making progress. It had me tricked for a while until I figured out the right search terms to find it in Google.
NEXT WALL:
Now the issue I have is that the program runs fine from the editor but the published version will not work properly, it will either freeze my game thread (while still being able to see particles flying around for example). Or the line where I call one of the imported functions being outright ignored.
I think it must have something to do with the dll paths and dependencies.
I've copied both dlls (interface and target) to Unity/Editor, Publish/data/Assets, etc. without success. Has anyone seen this before??. any help is super welcome.
I can't believe it's the 21st century and we still have to deal with this kind of BS. When I have the time and money, I'll make my own Computer + OS + Language and call it the Commodore-64Gig, centered around developers and real-time IO signaling. Sorry, that's my rant for today.
Alright then, I had to put the dlls next to the published .exe file. Now it works.
So much for the 'Plugins' folder...
Thanks everyone
Ah cool, thanks for co$$anonymous$$g back to post it when you figured it out.
@volkswagenb: ..working on the same dll of " automation.bdaq.dll " and with the same error ... ...please help ...as the post seems very old ..don't know whether you are active on the forum or not you can also ping me on gau098@gmail.com (as working on similar project and need your utmost help )
Answer by juan.biondi · Jan 09, 2014 at 08:49 PM
Hi Everyone! I Have the same problem but i can't fix it... My C code goes like these:
extern "C"
{
...
__declspec(dllexport) int close_connection()
{...}
}
I've to put the dll in the "Editor" folder, because if not, it not work. From the unity editor works like a charm but when i compile the program ti can't load the dll, i got an exception that it could not be found. I've tried putting it in the same folder than the .exe, in "system", "system32" and so on, but nothing.
Anyone can help me?
Your answer
Follow this Question
Related Questions
dllimport of C++ class or member functions 1 Answer
Unity5 Set Placeholder and CPU 0 Answers
How to add a custom module to Unity? 0 Answers
Windows Phone 8.1 Assembly 0 Answers