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

网站最下端怎么做/石家庄网站建设案例

网站最下端怎么做,石家庄网站建设案例,简单的企业网站的主页,身份证被别人做网站备案目录 前言 一、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://www.whsansanxincailiao.cn/news/31969794.html

相关文章:

  • dedecms 网站首页/百度app下载最新版本
  • 17做网站广州起做网店/网络营销的用户创造价值
  • 如何能进腾讯做游戏视频网站/网络营销是什么意思?
  • 亚马逊图书官网/seo网络营销公司
  • 网站编程培训班/微指数查询入口
  • 泉州建行 网站/软文发布门户网站
  • 网站建设开发有什么好处/武汉搜索推广
  • 湖南外贸网站建设/自媒体发布软件app
  • 网批做衣服的网站/海外新闻发布
  • c#网站开发+pdf/全球网站排名查询网
  • 新乡商城网站建设哪家优惠/东莞关键词优化推广
  • 西安网站建设是什么/广东网络优化推广
  • 南昌做任务的网站/com域名多少钱一年
  • 青岛胶南做网站的/托管竞价推广公司
  • 东莞有哪些做推广的网站/友情链接建立遵循的原则包括
  • 厦门湖里区建设局网站/最好的推广平台排名
  • 四川建设安全监督管理局网站/百度一下 你就知道官网
  • 百度云做网站/微信营销策略
  • 网上做二建题那个网站好/青岛网站排名推广
  • 佛山网站上排名/搜索网
  • 企业公司网站制作/提升神马关键词排名报价
  • 深圳人为什么不想去龙岗/郑州seo排名哪有
  • 西宁公司做网站/百度网站的网址
  • 做外贸的免费网站有哪些/提高工作效率
  • 邵阳县网站建设公司沙河网站建设公司/网络营销形式
  • 网站建设与管理怎么做/徐州百度推广
  • wordpress 导入discuz/宁波seo深度优化平台有哪些
  • 动易网站中添加邮箱/电商平台排行榜前十名
  • 如何做制作头像的网站/网站流量分析报告
  • 东莞微信网站建设更好/活动推广方案怎么写