学习笔记_1_C#调用Dll

本文详细介绍了如何在C#中通过DllImport调用C++编写的动态链接库(DLL),包括创建C++项目、添加.h和.cpp文件、设置项目属性以及在C#控制台程序中实际调用函数的过程。
摘要由CSDN通过智能技术生成

C#调用Dll

创建c++空项目:添加.h文件与.cpp文件,并编写内容;

修改项目属性-配置类型为动态库(dll)

8470.h

#pragma once

extern "C" {
	_declspec(dllexport) int add(int a, int b);
}

8470.cpp

#include "8470.h"


int add(int a, int b) {
	return a + b;
}

生成动态链接库(dll)

创建C#控制台程序并编写内容

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

namespace Console8470
{
    internal class Program
    {
        [DllImport("8470.dll",CallingConvention = CallingConvention.Cdecl)]
        public static extern int add(int a,int b);

        static void Main(string[] args)
        {
            Console.WriteLine(add(4, 5));
            Console.ReadKey();
        }
    }
}

将生成的dll放入debug目录下,运行debug即可调用

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值