一、發生的背景
在開發新項目中使用了新的語言開發 C# 和新的技術方案 WEB Service,但是在新項目中,一些舊的模組需要繼續使用,一般是採用 C 或 C++ 或 Delphi 編寫的,如何利用舊模組對於開發人員來說,有三種可用方法供選擇:第一、將 C 或 C++ 函數用 C# 徹底改寫一遍,這樣整個項目代碼比較統一,維護也方便一些。但是儘管微軟以及某些書籍說,C# 和 C++ 如何接近,但是改寫起來還是很痛苦的事情,特別是 C++ 裡的指標和記憶體操作;第二、將 C 或 C++ 函數封裝成 COM,在 C# 中調用COM 比較方便,只是在封裝時需要處理 C 或 C++ 類型和 COM 類別型之間的轉換,也有一些麻煩,另外COM 還需要註冊,註冊次數多了又可能導致混亂;第三、將 C 或 C++ 函數封裝成動態連結程式庫,封裝的過程簡單,工作量不大。因此我決定採用載入動態連結程式庫的方法實現,於是產生了在 C# 中如何調用自訂的動態連結程式庫問題,我在網上搜尋相關主題,發現一篇調用系統 API 的文章,但是沒有說明如何解決此問題,在 MSDN 上也沒有相關詳細說明。基於此,我決定自己從簡單出發,逐步實驗,看看能否達到自己的目標。
(說明一點:我這裡改寫為什麼很怕麻煩,我改寫的代碼是變長密碼編譯演算法函數,代碼有600多行,對演算法本身不熟悉,演算法中指標和記憶體操作太多,要想保證演算法正確,最可行的方法就是少動代碼,否則只要有一點點差錯,就不能肯定演算法與以前相容)
LIBEXPORT_API int mySum(int a,int b){ return a+b;}
C# 匯入定義:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int mySum (int a,int b);
}
在C#中調用測試:
int iSum = RefComm.mySum(2,3);
運行查看結果iSum為5,調用正確。第一步實驗完成,說明在C#中能夠調用自訂的動態連結程式庫函數。
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern string mySum (string a, string b);
}
在C#中再調用測試:
LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;}
C#匯入的定義:
public class RefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint=" mySum ",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern int mySum (int a, int b,ref int c);
}
在C#中調用測試:
int c=0;
int iSum= RefComm. mySum(2,3, ref c);
運行查看結果iSum 和c均為5,調用正確。
經過以上幾個步驟的實驗,基本掌握了如何定義動態庫函數以及如何在 C# 定義匯入,有此基礎,很快我實現了變長加密函數在 C# 中的調用,至此目標實現。
三、結論
在 C# 中調用 C++ 編寫的動態連結程式庫函數,如果需要出口參數輸出,則需要使用指標,對於字串,則需要使用雙重指標,對於 C# 的匯入定義,則需要使用引用(ref)定義。
對於函數傳回值,C# 匯入定義和 C++ 動態庫函式宣告定義需要保持一致,否則會出現函數調用失敗。定義匯入時,一定注意 CharSet 和 CallingConvention 參數,否則導致調用失敗或結果異常。運行時,動態連結程式庫放在 C# 程式的目錄下即可,我這裡是一個 C# 的動態連結程式庫,兩個動態連結程式庫就在同一個目錄下運行。