Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by volkswagenb · Jun 20, 2013 at 02:59 PM · pluginreferencedllexternal

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;         
   }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AmoralAckbar · Jun 21, 2013 at 03:19 PM 0
Share

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:

  1. I am using C# in Visual Studio.

  2. I am using a C# DLL, so I don't have to do any DLLImport interop stuff.

  3. $$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:

  1. I added a reference to the first DLL in Visual Studio. I have this DLL in my Assets folder.

  2. I added my namespaces in code.

  3. I put the second DLL in my assets folder (which didn't work)

  4. 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
      }//
 
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AmoralAckbar · Jun 21, 2013 at 06:14 PM 0
Share

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.

avatar image volkswagenb · Jun 27, 2013 at 07:40 PM 0
Share

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.

avatar image volkswagenb · Jun 28, 2013 at 08:52 PM 0
Share

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

avatar image AmoralAckbar · Jun 30, 2013 at 05:46 PM 0
Share

Ah cool, thanks for co$$anonymous$$g back to post it when you figured it out.

avatar image gau098 · Nov 27, 2015 at 11:09 AM 0
Share

@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 )

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

External DLL + Unityscript = Not Implemented 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges