【转】Unity C# 调用C++ dll 问题集锦
1.調(diào)用約定
stdcall 、 cdecl、 fastcall等等
這個(gè)用來指定參數(shù)傳遞順序和函數(shù)返回時(shí)棧的清除方式。
可以通過以下
?
?| 1 2 3 4 5 6 7 8 9 10 11 12 | [AttributeUsage(AttributeTargets.Method)] public class DllImportAttribute: System.Attribute { public DllImportAttribute(string dllName) {…} //定位參數(shù)為dllName public CallingConvention CallingConvention; //入口點(diǎn)調(diào)用約定 public CharSet CharSet; //入口點(diǎn)采用的字符接 public string EntryPoint; //入口點(diǎn)名稱 public bool ExactSpelling; //是否必須與指示的入口點(diǎn)拼寫完全一致,默認(rèn)false public bool PreserveSig; //方法的簽名是被保留還是被轉(zhuǎn)換 public bool SetLastError; //FindLastError方法的返回值保存在這里 public string Value { get {…} } } |
屬性指定調(diào)用約定和字符集等等。
?
2.開啟x64支持
這個(gè)很容易更改,vs正常是win32 release、debug,可以新建x64 release、debug即可。
3.基本數(shù)據(jù)類型的傳遞
?
互調(diào)過程中,最基本要傳遞的無非是數(shù)值和字符,即:int,long,float,char等等,但是此類型非彼類型,C/C++與C#中有一些數(shù)據(jù)類型長(zhǎng)度是不一樣的,下表中列出常見數(shù)據(jù)類型的異同:
| C/C++ | C# | 長(zhǎng)度 |
| short | short | 2Bytes |
| int | int | 4Bytes |
| long(該類型在傳遞的時(shí)候常常會(huì)弄混) | int | 4Bytes |
| bool | bool | 1Byte |
| char(Ascii碼字符) | byte | 1Byte |
| wchar_t(Unicode字符,該類型與C#中的Char兼容) | char | 2Bytes |
| float | float | 4Bytes |
| double | double | 8Bytes |
最容易弄混的是就是long,char兩個(gè)類型,在C/C++中l(wèi)ong和int都是4個(gè)字節(jié),都對(duì)應(yīng)著C#中的int類型,而C/C++中的char類型占一個(gè)字節(jié),用來表示一個(gè)ASCII碼字符,在C#中能夠表示一個(gè)字節(jié)的是byte類型。與C#中char類型對(duì)應(yīng)的應(yīng)該是C/C++中的wchar_t類型,對(duì)應(yīng)的是一個(gè)2字節(jié)的Unicode字符。
溫馨提示,C++的char不是C#的char,這兩個(gè)類型不兼容的。并且wchar_t 在windows下2byte,在linux下4byte。
4.傳遞數(shù)組
----------------------------------------------------------
C#聲明形參如下:
char[] chs
C++聲明形參如下:
char chs[5]
此時(shí)數(shù)組時(shí)拷貝過去的,C++修改數(shù)組不會(huì)改變C#數(shù)組
----------------------------------------------------------
C#聲明形參如下:
([In, Out][MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] char[] chs
C++聲明形參如下:
wchar_t* chs
此時(shí)相當(dāng)于按引用傳遞,C++修改數(shù)組會(huì)改變C#數(shù)組
----------------------------------------------------------
C#聲明形參如下:
ref char ch(調(diào)用時(shí),ref char[0])
?
C++聲明形參如下:
wchar_t* chs
此時(shí)相當(dāng)于按引用傳遞,C++修改數(shù)組會(huì)改變C#數(shù)組
------------------------------------------------------------
如果需要使用指針,可能需要開啟unsafe。
5.開啟unsafe
?
Unity3d中C#使用指針(Unsafe)的辦法
近日由于在U3D項(xiàng)目中要使用到數(shù)據(jù)傳遞(C++ DLL的數(shù)據(jù)傳遞給U3D中的C#),其中涉及到需要使用C#的指針。直接編譯會(huì)出現(xiàn)以下錯(cuò)誤Unsafe code requires the 'unsafe' command line option to be specified。
?
下面是我總結(jié)的解決辦法:
1.去除MONO編輯器中的Unsafe錯(cuò)誤,Assembly-CSharp鼠標(biāo)右鍵 找到Options->Build->General 。Allow 'unsafe' code 打鉤。這個(gè)只能去除MONO報(bào)錯(cuò),但是依然無法運(yùn)行。
2.首先看下面一段比較長(zhǎng)的
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# | /Assets/smcs.rsp |
| C# - Editor Scripts | /Assets/gmcs.rsp |
| UnityScript | /Assets/us.rsp |
| Boo | /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, 因?yàn)檫@個(gè)預(yù)編譯是在啟動(dòng)U3D時(shí)候運(yùn)行的。工程文件名別帶中文。
原理是編輯器中的smcs.exe 添加編譯命令,也可以在CMD下運(yùn)行編輯器目錄下的smcs.exe 逐個(gè)添加,會(huì)很累的。
?
?
引用:
[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教程:調(diào)用C++的DLL方法 http://www.unitymanual.com/5291.html
[5] 數(shù)組MARSHALLINGseo=" />http://www.kycis.com/blog/read.php?21
總結(jié)
以上是生活随笔為你收集整理的【转】Unity C# 调用C++ dll 问题集锦的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#命名空间的嵌套
- 下一篇: C#多维数组与嵌套数组