C#調用動態鏈接庫

  動態鏈接庫的調用是使用動態鏈接庫的重要部分,下面主要通過一個示例來進行講解。

  創建DllDemo動態鏈接庫代碼如下。

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Text;
  4. namespaceDllDemo
  5. {
  6.   publicclassOperation
  7.   {
  8.     publicdoubleAdd(doublea,doubleb)
  9.     {
  10.       returna+b;
  11.     }
  12.     publicdoubleMinus(doublea,doubleb)
  13.     {
  14.       returna-b;
  15.     }
  16.     publicdoubleMultiplication(doublea,doubleb)
  17.     {
  18.       returna*b;
  19.     }
  20.     publicdoubleDivision(doublea,doubleb)
  21.     {
  22.       returna/b;
  23.     }
  24.   }
  25. }

  示例  通過調用類庫實現簡單計算器程序

  (1)在菜單欄中選擇“項目”/“添加引用”,彈出“添加引用”對話框,在“添加引用”對話框中選擇“瀏覽”選項卡。

  (2)通過瀏覽找到上一節中創建的DLL類庫,成功找到後,單擊【確定】按鈕,將類庫添加到項目中。

 

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Text;
  7. usingSystem.Windows.Forms;
  8. usingDllDemo;
  9.  
  10. namespaceCCDll
  11. {
  12.   publicpartialclassForm1:Form
  13.   {
  14.     publicForm1()
  15.     {
  16.       InitializeComponent();
  17.     }
  18.     Operationoper=newOperation();
  19.  
  20.     privatevoidbutton1_Click(objectsender,EventArgse)
  21.     {
  22.       switch(comboBox1.Text)
  23.       {
  24.         case"+":
  25.           textBox3.Text=oper.Add(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
  26.           break;
  27.         case"-":
  28.           textBox3.Text=oper.Minus(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
  29.           break;
  30.         case"*":
  31.           textBox3.Text=oper.Multiplication(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
  32.           break;
  33.         case"/":
  34.           textBox3.Text=oper.Division(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
  35.           break;
  36.       }
  37.     }
  38.   }
  39. }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章