C#調用c++編譯的opencv文件生成的dll(VS2013+opencv3.1)

1.新建項目

2.源文件---右鍵 添加---新建項(.cpp)  /  頭文件---右鍵 添加---新建項(.h)

 

3.配置opencv3.1

4..cpp文件中放入自己的程序(只是一個方法,或者一個類,不要main),並且在函數前加如下代碼

extern "C" __declspec(dllexport)
#include "stdio.h"
#include "opencv2/highgui/highgui_c.h"
#include <opencv2/opencv.hpp>
#include <tchar.h>
#include <fstream>
#include <string>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;
using namespace cv;

// a.cpp : 定義 DLL 應用程序的導出函數。
extern "C" __declspec(dllexport)bool CutPic(char* path)
{
    //代碼段......
}

5..h文件中放入修改後的代碼如下(請參考修改)(注:PACKAGEDLL是自己創建的文件名稱大寫):

#ifdef PACKAGEDLL_EXPORTS
#define PACKAGEDLL_API __declspec(dllexport)
#else
#define PACKAGEDLL_API __declspec(dllimport)
#endif

#ifdef PACKAGEDLL_EXPORTS
#define PACKAGEDLL_API extern "C" __declspec(dllexport)
#else
#define v extern "C" __declspec(dllimport)
#endif

PACKAGEDLL_API bool CutPic(char* path);

 

6.配置clr

7.生成---生成***(U)(注意:自己選擇的模式,debug模式下編譯的dll,只能debug模式下使用)

8.在輸出中可以看見輸出的路徑

9.在C#程序中引入dll

先將生成的dll和opencv的dll,放到項目啓動路徑xxx\bin\Debug

10.在需要引入dll的文件中加入如下:

using System.Runtime.InteropServices;

[DllImport("PackageDll.dll")]//Dll名
public static extern bool CutPic(string path);//我得方法CutPic

11. C#環境配置(按如圖紅框配置--不勾選)

注意:自己調用的名字不要寫錯,dll放錯地方會報錯找不到dll

參考鏈接:

https://blog.csdn.net/liyuqian199695/article/details/53525178

https://blog.csdn.net/xiaolifeidaoer/article/details/81232666

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章