title: Using DLL in Unity, MonoBehaviour
body: <p>I have a library of some wing geometry and flow solver code written in C. I made a simple test project for debugging some integration problems between the library, my C# wrapper class, and my main project. In this test project I have a test script:</p>
  
  <pre><code>using UnityEngine;
  using System;
  using System.Collections.Generic;
  using System.Collections;
  using System.Runtime.InteropServices;
  
  public class testScript2 : MonoBehaviour {
      // external functions
      [DllImport( "TestLib" )]
      public static extern void makeWing( ref stripCalcStruct wing, 
          int n_sections, bool isSwept, double sweep );
      [DllImport( "TestLib" )]
      public static extern void makeComp( ref stripCalcStruct wing, 
          ref compNILLStruct wingComp );
      [DllImport( "TestLib" )]
      public static extern void NILLWingEx( ref stripCalcStruct wing,
          ref compNILLStruct comp, ref outNILL results, bool simpsons );
  
      // member variables to store computation results
      public double calculation1 = 0.0;
      public double calculation2 = 0.0;
      public double calculation3 = 0.0;
      public double calculation4 = 0.0;
      public double calculation5 = 0.0;
      public double calculation6 = 0.0;
  
      // Use this for initialization
      void Start () {
          // this is my "main" program
          // initialize some geometric parameters
          int numCS = 2;
          int numControlStations = 2;
          int numAirfoils = 1;
          int numAiCoeffs = 10;
          int n_sections = 40;
  
          // Create a StripController
          StripController controller1 = new StripController(
              n_sections, numCS, numControlStations, numAirfoils, numAiCoeffs );
          controller1.tryStuff();
          // Fill the wing geometry with a default wing from the library
          makeWing( ref controller1.wing, 40, false, 0.0 );
          // save a piece to see that it worked
          calculation1 = controller1.wing.area;
          // Fill computational state parameters with default values from the library
          makeComp( ref controller1.wing, ref controller1.comp );
          // save a piece to see that it worked
          calculation2 = controller1.getAlphaAtIndex( 0 );
          // Call the flow solver from the library
          NILLWingEx( ref controller1.wing, ref controller1.comp, 
              ref controller1.results, false );
          // save the required computation time
          calculation3 = controller1.results.time;
          // free pinned memory in the controller
          controller1.freeStuff();
  
          /* Try this again, but this time call the NILLWingEX function from
           * within the StripController file */
          StripController controller2 = new StripController(
              n_sections, numCS, numControlStations, numAirfoils, numAiCoeffs );
          controller2.tryStuff();
          makeWing( ref controller2.wing, 40, false, 0.0 );
          calculation4 = controller2.wing.area;
          makeComp( ref controller2.wing, ref controller2.comp );
          calculation5 = controller2.getAlphaAtIndex( 0 );
          controller2.EvalNILL(); // This fails
          calculation6 = controller2.results.time;
          controller2.freeStuff();
      }
  
      // Update is called once per frame
      void Update () {
  
      }
  
      void OnGUI()
      {
          // display our computation results on the screen
          GUI.Label( new Rect( 100f, 100f, 500f, 20f ), calculation1.ToString() );
          GUI.Label( new Rect( 100f, 120f, 500f, 20f ), calculation2.ToString() );
          GUI.Label( new Rect( 100f, 140f, 500f, 20f ), calculation3.ToString() );
          GUI.Label( new Rect( 100f, 160f, 500f, 20f ), calculation4.ToString() );
          GUI.Label( new Rect( 100f, 180f, 500f, 20f ), calculation5.ToString() );
          GUI.Label( new Rect( 100f, 200f, 500f, 20f ), calculation6.ToString() );
      }
  }
  </code></pre>
  
  <p>There is a StripController.cs file that contains the <code>StripController</code> class. The top of the class looks like this</p>
  
  <pre><code>public class StripController
  {
      [DllImport( "TestLib" )]
      public static extern void NILLWingEX( ref stripCalcStruc wing,
           ref compNILLStruct comp, ref outNILL results, bool simpsons );
  </code></pre>
  
  <p>So it has the same external <code>NILLWingEX</code> definition as the other file. The <code>EvalNILL()</code> method just looks like this</p>
  
  <pre><code>public void EvalNILL()
  {
      NILLWingEX( ref wing, ref comp, ref results, false );
  }
  </code></pre>
  
  <p>In the test script file, in the first block using the <code>StripController</code> and calling <code>NILLWingEX</code> locally works fine. But in the second block, where I have the <code>StripController</code> call <code>NILLWingEX</code> from that file, I get an <code>EntryPointNotFoundException: NILLWingEX</code>. So I can call the external functions from the test script file, but not my other files? Does this have something to do with inheriting from MonoBehaviour? Does this have something to do with the fact that the test script is attached to a game object (the Main Camera)?</p>
  
  <p>Any help clarifying when/where you can and cannot call library functions in Unity would be appreciated.</p>