首页 > 编程语言 >c#开发和学习(c#调用dll)

c#开发和学习(c#调用dll)

时间:2022-11-23 15:03:21浏览次数:32  
标签:调用 sub c# System dll int add CallingConvention



        c和c++作为早期的开发语言,积累了大量的可用库。后续的开发语言,虽然在易用性和容易程度上面有了很大的提高,但是对于曾经的开发库,是无法做到弃之不用的。因此,对于c#语言来说,也是一样的。今天就来讨论下如何用c#调用dll这个问题。

1、首先编写一个c++代码

        注意这里的c++代码是生成动态库,不是生成exe文件。后续c#就是使用这个动态库里面的函数。假设函数的内容是这样的,

extern  "C" __declspec(dllexport) int add(int x, int y)
{
return x + y;
}

extern "C" __declspec(dllexport) int sub(int x, int y)
{
return x - y;
}

2、生成动态库之后,准备c#代码

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}

        初始创建工程后,一般什么内容也没有。这时候,需要做的第一步,就是添加InteropServices,即,

using System.Runtime.InteropServices;

        接着,第二步就是,从dll中引用导出来的函数。从上面c++的内容看,导出的函数有两个,一个是add,一个是sub,

[DllImport("Dll1.dll", EntryPoint = "add", CallingConvention = CallingConvention.Cdecl)]
public static extern int add(int a, int b);
[DllImport("Dll1.dll", EntryPoint = "sub", CallingConvention = CallingConvention.Cdecl)]
public static extern int sub(int a, int b);

        注意,这两个声明最好放在class里面。有了这两个声明之后,就可以开始准备使用这两个函数了,这也是所谓的第三步动作,

static void Main(string[] args)
{
Console.WriteLine(add(1,2));
Console.WriteLine(sub(1,2));
}

        有了上面的三步操作,不出意外的话,其实是可以看到3和-1这样的打印了,如下图所示,

c#开发和学习(c#调用dll)_学习

3、总结

        前面说到了c#调用了c++。总结一下主要有这么两点,第一,调用的时候传参和出参最好是基本的数据,比如char、int、char*、float、double这样的数据。第二,动态库一定要是extern "C" __declspec(dllexport)这样来定义,只有这样,才能将不确定性降到最低。最后为了方便大家复现这个问题,给出完整的c#参考代码,希望对大家有所裨益。

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

namespace ConsoleApp1
{
class Program
{
[DllImport("Dll1.dll", EntryPoint = "add", CallingConvention = CallingConvention.Cdecl)]
public static extern int add(int a, int b);
[DllImport("Dll1.dll", EntryPoint = "sub", CallingConvention = CallingConvention.Cdecl)]
public static extern int sub(int a, int b);

static void Main(string[] args)
{
Console.WriteLine(add(1,2));
Console.WriteLine(sub(1,2));
}
}
}

标签:调用,sub,c#,System,dll,int,add,CallingConvention
From: https://blog.51cto.com/feixiaoxing/5881300

相关文章

  • cpu设计和实现(取指)
        cpu设计的本质是数字电路的设计。要是没有verilog、vhdl这些语言,那么剩下来使用的方法基本只有卡诺图这一种了。在数字电路中,有两种基本的电路,一种是逻辑电路,一......
  • scrapy爬取后中文乱码,解决word转为html 时cp1252编码问题
    解决思路1、循环暴力寻找编码,但是不如思路3defparse(self,response):print(response.text[:100])body=response.body#直接是bytes,response.tex......
  • Codeforces Round #835 (Div.4) A-G题解
    原题链接:https://codeforces.com/contest/1744A.MediumNumber题意:给定三个数,求中间那个数(倒数第二小or倒数第二大)。题解:直接用数组存,sort一下输出中间的数即可。#in......
  • c#开发和学习(基础)
        原先对c#了解不多,后来进入非标领域,才知道原来大多数非标上位机软件都是用c#开发的。之前在研究所的时候,曾经想过用qt开发上位机软件,但是qt是基于c++语言的,这对......
  • 常用js库和框架(echarts)
    前端库,不管是饼图、柱状图,都可以很轻松的画出来。所需要的准备,就是把数据准备好就可以了。下面一个简单的例子来说明下,1、准备demo文件<head><metacharset="utf-8"/>......
  • Spring Cache + Redis;用Spring Cache的注解自动管理Redis缓存
    https://blog.csdn.net/qq_45839663/article/details/127209491?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHu......
  • composer 创建项目错误
    Problem1-league/flysystem[1.1.0,...,1.1.10]requireext-fileinfo*->itismissingfromyoursystem.InstallorenablePHP'sfileinfoextension.......
  • springcloud -nacos-配置中心-接入
    1.nacaosClient接入:pom.xml引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator</artifactId>......
  • Android gradle依赖:implementation 和compile以及其他详解
    2017年google后,Androidstudio版本更新至3.0,更新中,连带着com.android.tools.build:gradle工具也升级到了3.0.0,在3.0.0中使用了最新的Gralde4.0里程碑版本作为gradl......
  • Vue和Electron分离开发,一起打包
    分别安装Vue和Electron参照地址:https://cn.vuejs.org/guide/quick-start.html#creating-a-vue-applicationhttps://www.electronforge.io/npminitvue@latestnpmin......