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
}
}
}
}
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
}
}
}
}
private static extern void Test1();
// 这个完全不需要,这是用来调用非托管dll的。
直接在项目里面引用需要的dll,就可以直接使用里面的东西了。
private static extern void Test1();并且说明目前不支持namespace,所以很郁闷。
我试一下你说的方法,就是不使用插件方式在项目里引用试试,不知道能行不。
如果能实现非托管的方式编译或者编程,那么可能就没有错误,调用也就正常了。
添加一個靜態Main方法。
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)
然后编译的时候,增加编译选项reference, 参见msdn:http://msdn.microsoft.com/en-us/library/s5bac5fx%28VS.80%29.aspx