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