0
点赞
收藏
分享

微信扫一扫

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

49路末班车 2022-04-25 阅读 62
c++c#
  • 用到了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 条评论