当前位置: 首页 > news >正文

C++ 封装成DLL,C#调用

目录

前言

一、C++ DLL 封装

二、C# 调用 DLL

1、创建 C# 控制台项目,调用

三、注意事项


        

前言

        在实际工程开发中,跨语言调用是常见的需求,尤其是在性能要求较高的模块中,常常采用 C++ 实现核心算法逻辑,并通过封装为 DLL(动态链接库)的形式提供给其他语言调用,例如 C#。这种方式既能充分发挥 C++ 在执行效率、底层控制方面的优势,又可以借助 C# 在界面开发、快速迭代和平台整合方面的便利性,从而实现高效开发与运行的平衡。

        本项目以“C++ 封装成 DLL,然后在 C# 中调用”为核心,详细介绍了如何封装成标准的 Windows 动态链接库(DLL),并在 C# 环境中成功调用

一、C++ DLL 封装

//ImageProcessing.h
#pragma once#include <string>
#include <vector>#ifdef IMAGEPROCESSING_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endifextern "C" {/*** @brief 加载图像并进行反色处理* @param inputPath 输入图像路径* @param outputPath 输出图像路径* @return 处理成功返回1,失败返回0*/DLL_API int InvertImage(const char* inputPath, const char* outputPath);
}

cpp源文件:

#include "ImageProcessing.h"
#include <opencv2/opencv.hpp>void interpolateMat(const cv::Mat& input, cv::Mat& output)
{//*******//****
}Mat fitPlane(const std::vector<cv::Point>& points, const cv::Mat& depthMap)
{//*******//****
}int InvertImage(const char* inputPath, const char* outputPath)
{//interpolateMat();//****//fitPlane();
}

二、C# 调用 DLL

1、创建 C# 控制台项目,完成调用,结束。

using System;
using System.Runtime.InteropServices;namespace UseImageProcessingDll
{class Program{[DllImport("MyTest.dll", CallingConvention = CallingConvention.Cdecl)]public static extern int InvertImage(string inputPath, string outputPath);static void Main(string[] args){string input = "test.jpg";string output = "inverted.jpg";int result = InvertImage(input, output);Console.WriteLine(result == 1 ? "图像处理成功!" : "图像处理失败!");}}
}

三、注意事项

  • 确保 xxx.dllopencv_worldXXX.dll(如 opencv_world455.dll)位于 bin\Releasebin\Debug 目录下。

  • 推荐设置为 x64 平台(C++ 和 C# 都必须一致)。

2、另外如果遇到 C# 中没有 std::string& 这样的类型,这属于 C++ 的标准库类型,不能直接通过 P/Invoke(DllImport)跨语言传递。则:

  • 在 C++ 侧提供一个 const char* 的 C 接口包装(因为直接 P/Invoke std::string& 不稳定)。

  • 在 C# 中通过 [DllImport] 调用这个包装函数

extern "C"
{DLL_API void ExtractionPathAndSave(std::string& FileName, double minThreshold = 0.15);// 新增:接受 C 字符串DLL_API void ExtractionPathAndSave_Ansi(const char* fileName, double minThreshold = 0.15);
}
void ExtractionPathAndSave(std::string& FileName, double minThreshold)
{// 你的实际处理逻辑
}extern "C" void ExtractionPathAndSave_Ansi(const char* fileName, double minThreshold)
{std::string strFile(fileName);ExtractionPathAndSave(strFile, minThreshold);
}

调用时C# 的 string 会自动通过 [MarshalAs(UnmanagedType.LPStr)] 转换为 const char*,只要设置 CharSet = CharSet.Ansi

using System;
using System.Runtime.InteropServices;namespace UseDLL_Test
{class Program{// 方法1:导入 DLL 函数,注意命名要和导出的函数一致[DllImport("DLL_API .dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]public static extern void ExtractionPathAndSave_Ansi(string fileName, double minThreshold);方法2:P/Invoke 声明:调用 C++ 的 Ansi 包装函数//[DllImport("DLL_API.dll",EntryPoint = "ExtractionPathAndSave_Ansi", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]//public static extern void ExtractionPathAndSave(string fileName, double minThreshold);static void Main(string[] args){string filePath = @"C:\data\scan_data.bin";double threshold = 0.2;try{Console.WriteLine("调用 DLL 提取路径...");ExtractionPathAndSave_Ansi(filePath, threshold); //方法1//ExtractionPathAndSave(binFile, threshold);//方法2Console.WriteLine("完成!");}catch (Exception ex){Console.WriteLine("发生错误: " + ex.Message);}Console.ReadKey();}}
}

http://www.xdnf.cn/news/68275.html

相关文章:

  • 数字虹膜:无网时代的视觉密语 | 讨论
  • 第1 篇:你好,时间序列!—— 开启时间数据探索之旅
  • liunx中常用操作
  • 深入探索Qt异步编程--从信号槽到Future
  • 483. 找到字符串中所有的字母异位词
  • Linux 进程与线程间通信方式及应用分析
  • 分布式数据库TiDB:架构、核心特性与生产实践(分库分表)
  • 基于selenium框架的web应用自动化测试系统的设计与实现 毕业论文开题
  • Linux-网络基础
  • Spring_MVC 高级特性详解与实战应用
  • HTTP参数污染
  • Cribl 利用表向event 中插入相应的字段-example-01
  • 为零基础及不同背景学习者设计的 人工智能全栈学习路线图
  • git 版本提交规范
  • Linux 网络基础(三) TCP/IP协议
  • AI大模型 —— 国产大模型 —— 华为大模型
  • 卸载工具:IObit Uninstaller Pro v14.3.0 中文绿色专业便携版
  • IO流--字节流详解
  • linux安装mysql数据库
  • 如何下载适用于超宽屏显示的Google Chrome浏览器
  • CMake笔记:find_package工作原理
  • 如何清理Windows系统中已失效或已删除应用的默认打开方式设置
  • 国产DTU!工业DTU“性能翻倍+功耗减半”双突破!
  • 中科院数据生成赋能具身导航!WCGEN:基于世界一致性数据生成的视觉语言导航
  • 退役淘汰的硬盘数据安全处置不可忽视-硬盘数据抹除清零
  • 目标检测:视觉系统中的CNN-Transformer融合网络
  • LeetCode面试经典 150 题(Java题解)
  • c++_csp-j算法 (3)
  • HXBC编译相关错误
  • 【数论】快速幂