程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Unity C# 調用C++ dll 問題集錦

Unity C# 調用C++ dll 問題集錦

編輯:C#入門知識

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.基本數據類型的傳遞

 

互調過程中,最基本要傳遞的無非是數值和字符,即: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

C++聲明形參如下:

char 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# /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, 因為這個預編譯是在啟動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 \seo=" /> 不指定 http://www.kycis.com/blog/read.php?21

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved