C# 创建和引入动态链接库dll文件

一.创建动态链接库dll文件

新建 -> 项目->类库

名称为:dlltest11
在这里插入图片描述
添加函数:

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

namespace dlltest11
{
    public class Class1
    {
        public string ShowMessage(string text)
        {

            if (text == "123")
            {
                return "PASS";
            }
            else
            {
                return "NG";
            }
        }
    }
}

生成解决方案:名称为dlltest11.dll的文件

二:引用动态链接库dll文件

新建项目,以控制台应用程序为例
添加引用:浏览,添加创建好的dlltest11.dll文件在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Reflection;
using dlltest11;

namespace DLLtest
{
    class Program
    {
        //引入动态链接库
        [DllImport("dlltest.dll")]

        public static extern void ShowMessage(string text);//动态链接库中方法

        static void Main(string[] args)
        {

            Console.WriteLine("请输入数字:\n");

            string content = Console.ReadLine();

            //实例化

            dlltest11.Class1 i = new dlltest11.Class1();

            //调用动态链接库的方法
            
            string ReturnValue = i.ShowMessage(content);
            Console.WriteLine("{0}", ReturnValue);
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值