C#調(diào)用C++DLL-創(chuàng)新互聯(lián)

1.C++與C#對應(yīng)類型關(guān)系C#調(diào)用C++DLL

C/C++

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比萬源網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式萬源網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋萬源地區(qū)。費(fèi)用合理售后完善,十載實體公司更值得信賴。

C#

short

short

int

int

long

int

bool

bool

char(Ascii碼字符)

byte

float

float

double

double

short

short

wchar_t *

String/char[]

wchar_t

char

const float *

Float[]

2.C#聲明DLL方法

原C++方法:METISAPI double CalcTagSimilarities(const wchar_t * str_src, const wchar_t * str_dst,

      const wchar_t delimiter, const float * weights, const size_t weights_size);

C#聲明:

[DllImport("Metis_Maths.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CalcTagSimilarities", CharSet = CharSet.Unicode)]

       public static unsafe extern double CalcTagSimilarities(char* srcStr, char* targetStr, char _delimiter, float* weight, int weightCount);

       [DllImport("Metis_Maths.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CalcTagSimilarities", CharSet = CharSet.Unicode)]

       public static extern double CalcTagSimilarities(string srcStr, string targetStr, char _delimiter, float[] weight, int weightCount);

可選的 DllImportAttribute 屬性:

CharSet 指示用在入口點(diǎn)中的字符集,如:CharSet=CharSet.Ansi;

SetLastError 指示方法是否保留 Win32"上一錯誤",如:SetLastError=true;

ExactSpelling 指示 EntryPoint 是否必須與指示的入口點(diǎn)的拼寫完全匹配,如:ExactSpelling=false;

PreserveSig指示方法的簽名應(yīng)當(dāng)被保留還是被轉(zhuǎn)換,如:PreserveSig=true;

CallingConvention指示入口點(diǎn)的調(diào)用約定, 如:CallingConvention=CallingConvention.Winapi;

幾個注意點(diǎn):

1)指定接口入口數(shù)據(jù)集Chatset為Charset.Unicode,調(diào)動的Dll規(guī)定只處理unicode編碼的字符串,當(dāng)前環(huán)境默認(rèn)編碼是gb2312,需要顯示的指定編碼格式,否則會出現(xiàn)中文亂碼等現(xiàn)象。

  用extern c 來指明導(dǎo)出函數(shù)的時候使用C語言方式編譯和連接,這樣保證函數(shù)定義的名字和導(dǎo)出的名字相同,確保程序可以找到正確的入口點(diǎn)。

分享題目:C#調(diào)用C++DLL-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article40/pcheo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司關(guān)鍵詞優(yōu)化、軟件開發(fā)、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)