1、本人是在Mono开发环境下,使用MonoDevelop编辑和编译;
2、最近需要写一个C#调用另外一个C#生成DLL文件的函数;调用Test.dll中的Test1函数时报错说:
EntryPointNotFoundException: Test1
TestA.Start ()   (at Assets\TestA.cs:10)Test.cs源文件内容为:
using System;
using UnityEngine;public class Test
{public void Test1()
 {Debug.Log(Time.time);
 }
}调用方式为:[DllImport("Test")]
 private static extern void Test1();
 
 void Start()
 {Test1();
 }问:我查了一下资料,这种情况好像与受管类与非受管类有关,但本人是C#新手,根本不知道如何在受管模式和非受管模式下编程;希望大家能给个出路,谢谢。注——我使用C#调用C++编译DLL类的函数却没有错误,其源代码如下:#if _MSC_VER // this is defined when compiling with Visual Studio
#define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this
#else
#define EXPORT_API // XCode does not need annotating exported functions, so define is empty
#endif#include <math.h>// ------------------------------------------------------------------------
// Plugin itself// Link following functions C-style (required for plugins)
extern "C"
{void EXPORT_API UpdateTexture( void* colors, int width, int height, float time )
 {if(!colors )return;
  
  float* data = reinterpret_cast<float*>(colors);
  
  for (int y=0;y<height;y++)
  {for (int x=0;x<width;x++)
   {float* pixel = data + (y * width + x) * 4;
    // just a very simple scrolling sine waves pattern
    pixel[0] = sinf( x * 0.13f + time * 2.1f ) * 0.5f + 0.5f; // R
    pixel[1] = sinf( y * 0.17f + time * 1.9f ) * 0.5f + 0.5f; // G
    pixel[2] = cosf( x * 0.21f + time * 2.3f ) * 0.5f + 0.5f; // B
    pixel[3] = 1.0f; // A
   }
  }
 }
}

解决方案 »

  1.   

    你这个是用调用非托管dll的方法来调用托管dll,肯定是不成的。其实非常简单:[DllImport("Test")]
     private static extern void Test1();
    // 这个完全不需要,这是用来调用非托管dll的。
    直接在项目里面引用需要的dll,就可以直接使用里面的东西了。
      

  2.   

    我的开发环境限制的有点死,插件使用方法指明使用这种方式的:[DllImport("Test")]
     private static extern void Test1();并且说明目前不支持namespace,所以很郁闷。
    我试一下你说的方法,就是不使用插件方式在项目里引用试试,不知道能行不。
      

  3.   

    我的程序就是调用出错,原来说DllNoFound,后来改了一下位置,就报错:EntryPointNotFoundException:Test1;即类的函数Test没有发现入口。
    如果能实现非托管的方式编译或者编程,那么可能就没有错误,调用也就正常了。
      

  4.   

    沒有入口嘛
    添加一個靜態Main方法。
      

  5.   

    使用静态Main方法没有解决,我可能没说清楚,现在将两个C#文件源文件全部放上来,供大家出个方法:1、被调用的C#文件,使用MonoDevelop编译。using UnityEngine;
    using System;
    namespace VRNo1
    {public class Test
     {static void Main(string[] sStrs)
      {Debug.Log(sStrs[0]);
      }
      public static void MyClass(string sStr)
      {Debug.Log(sStr+"\t|\t"+Time.time);//此类是UnityEngine中的类
      }
     }
    }2、调用DLL的C#文件,默认是被Mono编译的,但编译方式不清楚;此C#文件能正常调用User32.dll文件,但是调用上面编译的VRNo1.dll文件时却报错,报错内容在最下面。using UnityEngine;
    using System.Collections;
    using System.Runtime.InteropServices;public class TestA:MonoBehaviour
    {[DllImport("User32")]
     public static extern int MessageBox(int Handle,string Message,string Caption,int Type);
     
     void Start()
     {MainAbs("Do't spport double-byte chars.");
     }
     
     void MainAbs(string sStr)
     {MessageBox(0,sStr,"My Message Box",0);
     }
     //================================================================
     [DllImport("VRNo1")]
     private static extern void MyClass(string sStr);
     
     void Start()
     {MyClass("ABS");
     }
    }3、报错内容:EntryPointNotFoundException: MyClass
    TestA.Start ()   (at Assets\TestA.cs:21)
      

  6.   

    首先,您的方法完全错误了,这个dll不是unmanaged dll,不能这么掉然后,正确的方法是,代码里面不用写任何东西,直接using 相应 的namespace之后,就用就是了。不过引入的不可能是个光秃秃的函数,因为c#是以类为基本单位的。
    然后编译的时候,增加编译选项reference, 参见msdn:http://msdn.microsoft.com/en-us/library/s5bac5fx%28VS.80%29.aspx
      

  7.   

    谢谢大家,尤其是7楼和2楼的兄弟,终于搞定了;没有使用DllImport,始终不成功;是使用Namespace+引用解决的。