Unity C# 调用C++ dll 问题集锦

1.调用约定

stdcall 、 cdecl、 fastcall等等

这个用来指定参数传递顺序和函数返回时栈的清除方式。

可以通过以下

[AttributeUsage(AttributeTargets.Method)]  public class DllImportAttribute: System.Attribute  {   public DllImportAttribute(string dllName) {…} //定位参数为dllName   public CallingConvention CallingConvention; //入口点调用约定   public CharSet CharSet; //入口点采用的字符接   public string EntryPoint; //入口点名称   public bool ExactSpelling; //是否必须与指示的入口点拼写完全一致,默认false   public bool PreserveSig; //方法的签名是被保留还是被转换   public bool SetLastError; //FindLastError方法的返回值保存在这里   public string Value { get {…} }  }属性指定调用约定和字符集等等。2.开启x64支持

这个很容易更改,vs正常是win32 release、debug,可以新建x64 release、debug即可。

3.基本数据类型的传递

C/C++

C#

长度

short

short

2Bytes

int

int

4Bytes

int

4Bytes

bool

bool

1Byte

byte

1Byte

char

2Bytes

float

float

4Bytes

double

double

8Bytes

温馨提示,C++的char不是C#的char,这两个类型不兼容的。并且wchar_t 在windows下2byte,,在linux下4byte。4.传递数组

———————————————————-

C#声明形参如下:

char[] chs 或者 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] char[] chs

C++声明形参如下:

wchar_t chs[5]

此时数组时拷贝过去的,C++修改数组不会改变C#数组

———————————————————-

C#声明形参如下:

([In, Out][MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] char[] chs

C++声明形参如下:

wchar_t* chs

此时相当于按引用传递,C++修改数组会改变C#数组

———————————————————-

C#声明形参如下:

ref char ch(调用时,ref char[0])

C++声明形参如下:

wchar_t* chs

此时相当于按引用传递,C++修改数组会改变C#数组

————————————————————

如果需要使用指针,可能需要开启unsafe。

5.开启unsafe

Unity3d中C#使用指针(Unsafe)的办法

近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针。直接编译会出现以下错误Unsafe code requires the ‘unsafe’ command line option to be specified。

下面是我总结的解决办法:

1.去除MONO编辑器中的Unsafe错误,Assembly-CSharp鼠标右键 找到Options->Build->General 。Allow ‘unsafe’ code 打钩。这个只能去除MONO报错,但是依然无法运行。

2.首先看下面一段比较长的

Custom Preprocessor Directives

It is also possible to define your own preprocessor directives to control which code gets included when compiling. To do this you must add in the "Assets/" folder a text file with the extra directives. The name of the file depends on the language you are using :

C#<Project Path>/Assets/smcs.rsp

C# – Editor Scripts<Project Path>/Assets/gmcs.rsp

UnityScript<Project Path>/Assets/us.rsp

Boo<Project Path>/Assets/boo.rsp

As an example, if you include the single line ‘-define:UNITY_DEBUG’ in your smcs.rsp file the define UNITY_DEBUG will exist as a global define for C# scripts, except for Editor scripts.

Every time you make make changes to the .rsp files a recompilation needs to be done for them to be effective. You can do this by updating or reimporting a single script (.js, .cs or .boo) file.

The usage of the .rsp files is described in the help of the smcs application, included in the Editor installation folder. You can get more information by running : "smcs -help".

在你的Assets目录下面添加smcs.rsp文件,里面只加一行字不要有空格 -unsafe。 OK搞定。记得一定要重启Unity3d, 因为这个预编译是在启动U3D时候运行的。工程文件名别带中文。

原理是编辑器中的smcs.exe 添加编译命令,也可以在CMD下运行编辑器目录下的smcs.exe 逐个添加,会很累的。

引用:

[1]C#与C/C++的交互

[2] C#_DllImport用法和路径问题

[3] Unity3d中C#使用指针(Unsafe)的办法

[4]Unity3D教程:调用C++的DLL方法

[5] 数组MARSHALLING ?21

欢迎交流,批评指正,谢谢

伟人之所以伟大,是因为他与别人共处逆境时,

Unity C# 调用C++ dll 问题集锦

相关文章:

你感兴趣的文章:

标签云: