新聞中心
C#語言還是比較常見的東西,這里我們主要介紹C#實(shí)現(xiàn)修改動態(tài)鏈接庫,包括介紹在 C# 中調(diào)用 C++ 編寫的動態(tài)鏈接庫函數(shù)等方面。

C#實(shí)現(xiàn)修改動態(tài)鏈接庫,將b修改為雙重指針:
- public class RefComm
- {
- [DllImport("LibEncrypt.dll",
- EntryPoint=" mySum ",
- CharSetCharSet=CharSet.Ansi,
CallingConventionCallingConvention=CallingConvention.StdCall)]- public static extern string mySum (string a, ref string b);
- }
在C#中調(diào)用測試:
- string strDest="";
- string strTmp= RefComm. mySum("12345", ref strDest);
運(yùn)行查看結(jié)果 strTmp 和 strDest 均為"12345",調(diào)用正確。實(shí)現(xiàn)了函數(shù)出口參數(shù)正確輸出結(jié)果。
C#實(shí)現(xiàn)修改動態(tài)鏈接庫,實(shí)現(xiàn)整數(shù)參數(shù)的輸出:
- public class RefComm
- {
- [DllImport("LibEncrypt.dll",
- EntryPoint=" mySum ",
- CharSetCharSet=CharSet.Ansi,
CallingConventionCallingConvention=CallingConvention.StdCall)]- public static extern int mySum (int a, int b,ref int c);
- }
經(jīng)過以上的試驗(yàn),基本掌握了如何定義動態(tài)庫函數(shù)以及如何在 C# 定義導(dǎo)入,有此基礎(chǔ),很快我實(shí)現(xiàn)了變長加密函數(shù)在 C# 中的調(diào)用,至此目標(biāo)實(shí)現(xiàn)。
在 C# 中調(diào)用 C++ 編寫的動態(tài)鏈接庫函數(shù),如果需要出口參數(shù)輸出,則需要使用指針,對于字符串,則需要使用雙重指針,對于 C# 的導(dǎo)入定義,則需要使用引用(ref)定義。
對于函數(shù)返回值,C# 導(dǎo)入定義和 C++ 動態(tài)庫函數(shù)聲明定義需要保持一致,否則會出現(xiàn)函數(shù)調(diào)用失敗。定義導(dǎo)入時,一定注意 CharSet 和 CallingConvention 參數(shù),否則導(dǎo)致調(diào)用失敗或結(jié)果異常。運(yùn)行時,動態(tài)鏈接庫放在 C# 程序的目錄下即可,我這里是一個 C# 的動態(tài)鏈接庫,兩個動態(tài)鏈接庫就在同一個目錄下運(yùn)行。
網(wǎng)頁標(biāo)題:C#實(shí)現(xiàn)修改動態(tài)鏈接庫
本文地址:http://fisionsoft.com.cn/article/dhjcgdj.html


咨詢
建站咨詢
