C# 调用 c++ 动态链接库,浅学一下~

这篇博客介绍了如何在C++中利用OpenCV创建动态库,并展示了C#如何调用该动态库打开并显示图片。通过修改项目属性将C++项目转换为生成.dll文件,然后在C#中使用DllImport导入并调用相应方法完成跨语言交互。
摘要由CSDN通过智能技术生成
  • 用到了opencv,得安装opencv,不多叙述
  • 首先是c++动态库的生成,我这里最开始是没有选择动态链接库创建项目,从生成 .exe项目转换成 .dll 项目。改动两个地方项目属性——配置属性——常规——目标文件扩展名(改为 .dll)——配置类型(改为:动态库.dll)
    如下图:在这里插入图片描述
  • 详情代码 :
//  dll 生成 c++
// .h
#pragma once
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

extern "C" _declspec(dllexport) void openImg();


// .cpp
#include "ExternAPI.h"
extern "C" _declspec(dllexport) void openImg()
{
	string path = "D:/myProject/dog.png";   // 路径用'/'不用'\' 折磨~
	Mat img = imread(path, 1);
	imshow("HUSKY_DOG", img);
	waitKey(0);
	destroyAllWindows();
}




// c# 调用:把生成的.dll 文件拷贝至 c#生成的 .exe文件同级目录

using System;
using System.Runtime.InteropServices;

namespace CallDll
{
    class Program
    {
        [DllImport("DoanOpencv.dll")]
        private extern static void openImg();

        static void Main(string[] args)
        {
            Console.WriteLine("Test call CPlus DLL");
            openImg();
            Console.Read();
        }
    }
}
  • 结果如下:
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSharp 调用C++ DLL; 参数为指针类型导出函数 c# Csharp调用 c++库 参数为导入和导出指针两种 包含C++ DLL源码 如fun(cont char* A,char*B) A为输入参数,B为输出参数-C# CSharp call C++ DLL lib dll function param use export and import eg: fun(cont char* A,char*B) A IN,B OUT TestDll\Debug\TestCallDll.exe .......\.....\TestCallDll.vshost.exe .......\.....\TestCallDll.vshost.exe.manifest .......\.....\TestDll.dll .......\.....\TestDll.lib .......\TestCallDll\Form1.cs .......\...........\Form1.Designer.cs .......\...........\Form1.resx .......\...........\obj\Debug\TestCallDll.csproj.FileListAbsolute.txt .......\...........\...\.....\TestCallDll.csproj.GenerateResource.Cache .......\...........\...\.....\TestCallDll.exe .......\...........\...\.....\TestCallDll.Form1.resources .......\...........\...\.....\TestCallDll.pdb .......\...........\...\.....\TestCallDll.Properties.Resources.resources .......\...........\Program.cs .......\...........\...perties\AssemblyInfo.cs .......\...........\..........\Resources.Designer.cs .......\...........\..........\Resources.resx .......\...........\..........\Settings.Designer.cs .......\...........\..........\Settings.settings .......\...........\TestCallDll.csproj .......\....Dll\dllmain.cpp .......\.......\ReadMe.txt .......\.......\stdafx.cpp .......\.......\stdafx.h .......\.......\targetver.h .......\.......\TestDll.cpp .......\.......\TestDll.def .......\.......\TestDll.h .......\.......\TestDll.vcproj .......\.......\TestDll.vcproj.PC-201008261742.Administrator.user .......\TestDll.sln .......\TestDll.suo .......\....CallDll\obj\Debug\TempPE .......\...........\...\Debug .......\...........\obj .......\...........\Properties .......\Debug .......\TestCallDll .......\TestDll TestDll
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值