當前位置:編程學習大全網 - 編程語言 - 編程實現重載

編程實現重載

第壹步,我先從壹個簡單的調用定義壹個簡單的函數,只實現壹個整數加法和,用C#調用測試

嘗試:

int iSum = RefComm.mySum(,);

運行查看iSum的結果是5,調用是正確的。實驗第壹步完成,表明在C#中可以調用用戶自定義的動態鏈接庫函數。

第二步,我定義了字符串操作的函數(為了簡單起見,我還是用之前的函數名),將結果作為字符串返回,在C#中調用

測試:

string strDest =

string strTmp= RefComm.mySum("45 ",strDest);

StrTmp為“45”,但strDest為空。我修改動態鏈接庫的實現,返回結果是字符串b:

LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,“%s”,a)return b;}

修改C# import的定義,將字符串B改為ref模式:

公共類RefComm

{

[DllImport("LibEncrypt.dll ",

EntryPoint=" mySum ",

CharSet =字符集。Auto,calling convention = calling conventi on。StdCall)]

public static extern string mySum(string a,ref string b);

}

用C#再次調用測試:

string strDest =

string strTmp= RefComm.mySum("45 ",ref strDest);

strTmp和strDest都不正確,並且包含不可見的字符。然後修改C# import的定義,將CharSet從Auto改為。

Ansi:

公共類RefComm

{

[DllImport("LibEncrypt.dll ",

EntryPoint=" mySum ",

CharSet =字符集。Ansi,調用約定=調用約定。StdCall)]

public static extern string mySum(字符串a,字符串b);

}

用C#再次調用測試:

string strDest =

string strTmp= RefComm。mySum("45 ",ref strDest);

運行視圖的結果是strTmp“45”,但是字符串strDest沒有賦值。第二步,實現函數返回壹個字符串,但是函數處的參數存在。

無法在中輸出。再次修改C#導入定義,修改字符串B作為引用(ref):

公共類RefComm

{

[DllImport("LibEncrypt.dll ",

EntryPoint=" mySum ",

CharSet =字符集。Ansi,調用約定=調用約定。StdCall)]

public static extern string mySum(string a,ref string b);

}

運行時調用失敗,執行無法繼續。

第三步,修改動態鏈接庫的實現,將B改為雙指針,用C#調用測試:

string strDest =

string strTmp= RefComm。mySum("45 ",ref strDest);

運行和查看strTmp和strDest的結果都是“45”,調用是正確的。第三步實現函數出口參數的正確輸出結果。

第四步,修改動態鏈接庫的實現,實現整數參數的輸出,在C#中調用測試:

int c = 0;

int iSum= RefComm。mySum(,,ref c);

運行查看的結果是iSum和C都是5,調用正確。

  • 上一篇:初學者學Python需要參考什麽書
  • 下一篇:beckmancoulter離心機D510報錯
  • copyright 2024編程學習大全網