C#调用Dll文件中方法的简单应用

参考:http://www.cnblogs.com/Asuphy/p/4206623.html

 直接看代码,最简单的引入,只需要3步:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 //第一部,开启引入的功能
10 //这个命名空间必须引入,否则使用不了DllImport
11 using System.Runtime.InteropServices;
12 
13 namespace ImportDllFile
14 {
15     public partial class Form1 : Form
16     {
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void button1_Click(object sender, EventArgs e)
23         {
24            //第三步,使用方法
25             MsgBox(0,"C#调用DLL文件","这是标题",0x30);
26         }
27 
28        //第二步,引入所要使用的dll文件,及要调用的函数
29        //中括号,直接使用user32.dll即可,EntryPoint说明引用Dll中的函数名,
30       //这里MsgBox是它的别名
31         [DllImport("user32.dll", EntryPoint = "MessageBoxA")]
32         public static extern int MsgBox(int hWnd, string msg, string caption, int type);
33     }
34 }
View Code

DLL中部分的参数类型所对应C#中的参数类型如下:

http://www.cnblogs.com/ysharp/archive/2012/05/25/2517803.html

附:常用的Dll集合,介绍与例子

原文地址:https://www.cnblogs.com/tommy-huang/p/4259761.html