您的位置:首页 > 移动开发 > Unity3D

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

2015-05-04 16:29 399 查看

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.基本数据类型的传递

互调过程中,最基本要传递的无非是数值和字符,即:int,long,float,char等等,但是此类型非彼类型,C/C++与C#中有一些数据类型长度是不一样的,下表中列出常见数据类型的异同:

C/C++

C#
长度
short

short
2Bytes
int

int
4Bytes
long(该类型在传递的时候常常会弄混)

int
4Bytes
bool

bool
1Byte
char(Ascii码字符)

byte
1Byte
wchar_t(Unicode字符,该类型与C#中的Char兼容)

char
2Bytes
float

float
4Bytes
double

double
8Bytes
最容易弄混的是就是long,char两个类型,在C/C++中long和int都是4个字节,都对应着C#中的int类型,而C/C++中的char类型占一个字节,用来表示一个ASCII码字符,在C#中能够表示一个字节的是byte类型。与C#中char类型对应的应该是C/C++中的wchar_t类型,对应的是一个2字节的Unicode字符。
温馨提示,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++的交互 http://www.cnblogs.com/warensoft/archive/2011/12/09/warenosoft3d.html
[2]
C#_DllImport用法和路径问题 http://www.cnblogs.com/szytwo/archive/2011/12/11/2283780.html [3] Unity3d中C#使用指针(Unsafe)的办法 http://www.j2megame.com/html/xwzx/ty/3652.html
[4]Unity3D教程:调用C++的DLL方法 http://www.unitymanual.com/5291.html
[5] 数组MARSHALLING



http://www.kycis.com/blog/read.php?21
欢迎交流,批评指正,谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: