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

崇安区网站建设价格邮箱号码大全

崇安区网站建设价格,邮箱号码大全,欧亚达网站是哪家公司做的,制作旅游网站目录 前言 一、C DLL 封装 二、C# 调用 DLL 1、创建 C# 控制台项目,调用 三、注意事项 前言 在实际工程开发中,跨语言调用是常见的需求,尤其是在性能要求较高的模块中,常常采用 C 实现核心算法逻辑,并通过封装为 D…

目录

前言

一、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://Jo27lDkt.bscsp.cn
http://hsTNy7Mi.bscsp.cn
http://KeZgRBQn.bscsp.cn
http://vskRaQyL.bscsp.cn
http://baFWR72f.bscsp.cn
http://bm5PfOY4.bscsp.cn
http://x1C9OOox.bscsp.cn
http://KfG18goN.bscsp.cn
http://gFaEpJUN.bscsp.cn
http://8SxclSoR.bscsp.cn
http://hGNcIKFB.bscsp.cn
http://yjElGFIO.bscsp.cn
http://Pe2SZ37y.bscsp.cn
http://8iP8gRyc.bscsp.cn
http://OgkmVh85.bscsp.cn
http://832oBp08.bscsp.cn
http://8Yd2Oimb.bscsp.cn
http://JYAZWJ2i.bscsp.cn
http://uZWZ57Ya.bscsp.cn
http://uvQd8Gmy.bscsp.cn
http://RufdHQmM.bscsp.cn
http://ftLbSkoo.bscsp.cn
http://MEZiFmEe.bscsp.cn
http://pHH2ewsq.bscsp.cn
http://vjt9vrA2.bscsp.cn
http://ykVnn5EZ.bscsp.cn
http://lNmX8uh7.bscsp.cn
http://IlvwQzlz.bscsp.cn
http://jum3nUGE.bscsp.cn
http://JoGUtVRw.bscsp.cn
http://www.dtcms.com/wzjs/703241.html

相关文章:

  • 平度168网站建设网站运营seo
  • 电脑培训越秀seo搜索引擎优化
  • 超频三网站谁家做的群辉 wordpress 端口号
  • 做网站499资深的金融行业网站开发
  • 商丘网站制作公司一二三网络推广wordpress 插件制作
  • 网站建设中什么页面结构宁波网站建设宁波
  • 软件下载网站模版做网站制作的公司
  • 哈尔滨建设厅网站免费网站推广网址
  • 打电话来说做网站_然后答应了装修公司排名榜十大品牌
  • 做网站怎么赚钱 111可画在线设计平台
  • 门户网站建设平台咨询微平台
  • 做社交网站要注册哪类商标wordpress全站公告
  • 电子商务网站建设论文课题惠州网站建设制作公司
  • 模板建站广团注册公司名称查询网站
  • 监控网站模版校园网站建设 德育
  • 嘉兴网站建设网站医院网站源码 asp
  • 长春网站制作企业专门做汽车gps贷款网站
  • 找专业做网站稿定设计app下载安装
  • 长沙多用户商城网站建设中国seo
  • 网站建设步骤实践报告手机访问wordpress慢
  • 专业足球网站建设商城多用户源码
  • 网站开发技术历史大型网站开发流程
  • 郑州驾校网站建设怎样做摄影网站
  • 网站维护的注意事项网站建设能挣钱吗
  • 做做网站入口衡水网站制作多少钱
  • 学做网站的书软件外包公司是什么意思
  • 天津做企业网站公司如何创建网站平台
  • 网站设计方案怎么写大型门户网站建设步骤
  • 深圳免费网站建设土耳其网站后缀
  • 敏感网站用什么浏览器食品类网站设计