c#调用DLL

1.创建DLL

文件->新建-》项目-》visual c#-》windows桌面-》类库,名称起一个,如DllTest

编写代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DllTest
{
    public class Class1
    {
        public void ShowMessage()
        {
            Console.WriteLine("你以成功调用了动态连接!");
            Console.ReadLine();
        }
    }
}

2.检查输出

在解决方案上右击——属性,在应用程序选卡中需要注意一下三个内容。
1.程序集名称2.默认命名空间3.输出类型
1.DllTest      2.DllTest           3.类库


3.生成DLL

生成-》生成DllTest

生成成功后可以在目录下能看到生成的Dll


4.调用DLL

新建一个控制台应用程序,来实现该dll的调用。例如新建一个DLLExample。

解决方案管理器选中,右键添加-》引用-》浏览,选择DllTest.dll,确定。
编辑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//下面两项必须添加
using System.Runtime.InteropServices;
using DllTest;
namespace DLLExample
{
    class Program
    {
        [DllImport("DllTest.dll")]
        public static extern void ShowMessage();
        static void Main(string[] args)
        {
            //实例化
            DllTest.Class1 i = new Class1();
            //调用动态链接库的方法
            i.ShowMessage();
        }
    }
}

运行即可成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值