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. }

 

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