C#调用C/C++动态库


前言

在项目开发中经常会遇到C/C++的动态链接库,而上位机采用C#的方式进行开发。对于某些应用不适用直接使用非托管的方式调用C/C++中的接口,本文就介绍在C#中采用中间层调用C++动态库,并将其封装成C#的库文件,将符合C#的库文件用于发布使用。


一、创建C/C++动态库

1.新建工程

在这里插入图片描述
设置向导
在这里插入图片描述

2.添加模拟测试驱动

#include "stdafx.h"
#include "board_driver.h"

#define DEFAULT_HANDLE	0x2000

int board_open(unsigned int* handle)
{
	*handle = DEFAULT_HANDLE;
	return 0;
}

int board_close(unsigned int handle)
{
	return handle;
}

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

2.头文件声明

#ifndef __BOARD_DRIVER_H
#define __BOARD_DRIVER_H

int board_open(unsigned int* handle);
int board_close(unsigned int handle);
int board_calc(int a, int b);

#endif

3.模块定义声明

在这里插入图片描述
模块声明需要导出的函数。

LIBRARY	"board_driver"
EXPORTS
	board_open @1
	board_close @2
	board_calc @3

4.编译生成库文件

选择重新生成;
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、创建C#库

1.新建工程

在同一解决方案下新建C#库工程。

在这里插入图片描述
在这里插入图片描述

2.添加源码

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

namespace CBoardDriver
{
    public class BoardDriver
    {
        [DllImport("board_driver.dll", EntryPoint = "board_open", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern int board_open(ref UInt32 handle);

        [DllImport("board_driver.dll", EntryPoint = "board_close", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern int board_close(UInt32 handle);

        [DllImport("board_driver.dll", EntryPoint = "board_calc", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern int board_calc(int a,int b);

        /// <summary>
        /// 重写接口
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public int BoardOpen(ref UInt32 handle)
        {
            return board_open(ref handle);
        }


        /// <summary>
        /// 重写接口
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public int BoardClose(UInt32 handle)
        {
            return board_close(handle);
        }


        /// <summary>
        /// 重写接口
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public int BoardCalc(int a,int b)
        {
            return board_calc(a, b);
        }
    }
}

3.编译生成库文件

在这里插入图片描述
在这里插入图片描述

三、应用测试

1.新建WinForm工程

在这里插入图片描述
在这里插入图片描述

2.引用库

在这里插入图片描述
在这里插入图片描述

3.应用测试

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CBoardDriver;

namespace CBoardTest
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 
        /// </summary>
        BoardDriver boardDriver;

        /// <summary>
        /// 
        /// </summary>
        UInt32 handle;

        public Form1()
        {
            InitializeComponent();
            //
            handle = 0;
            //实例
            boardDriver = new BoardDriver();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            boardDriver.BoardOpen(ref handle);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int status = 0;
            status = boardDriver.BoardClose(handle);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int result = 0;
            result = boardDriver.BoardCalc(4, 5);
        }
    }
}

4.设置运行

设置测试工程为启动项目;
在这里插入图片描述
将C/C++库拷贝到可执行程序路径;
运行所需库

5.测试

在Open中读取到在C/C++库中设置的默认值;
在这里插入图片描述
在Calc中实现两数相加;
在这里插入图片描述

总结

以上就是本次记录的内容,本文仅仅简单介绍了C#采用中间件调用C/C++库的实现流程;

  • 4
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值