1.C++
與C#對(duì)應(yīng)類型關(guān)系
C/C++ | 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"上一錯(cuò)誤",如: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;
幾個(gè)注意點(diǎn):
1)
指定接口入口數(shù)據(jù)集Chatset為Charset.Unicode,調(diào)動(dòng)的Dll規(guī)定只處理unicode編碼的字符串,當(dāng)前環(huán)境默認(rèn)編碼是gb2312,需要顯示的指定編碼格式,否則會(huì)出現(xiàn)中文亂碼等現(xiàn)象。
用extern c 來指明導(dǎo)出函數(shù)的時(shí)候使用C語言方式編譯和連接,這樣保證函數(shù)定義的名字和導(dǎo)出的名字相同,確保程序可以找到正確的入口點(diǎn)。
分享題目:C#調(diào)用C++DLL-創(chuàng)新互聯(lián)
瀏覽路徑:http://jinyejixie.com/article40/pcheo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、軟件開發(fā)、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容