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:

-I did copy the dll to the Assets\Plugins folder.

-I 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;         
  }
}

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;
	 }//

}

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?