前言
接下来介绍影像处理提取特徵,所以首先要先做好环境设定,这次介绍如何呼叫dll来Debug。
创建C++ DLL
1.选择DLL和空专案。
2.设定支援CLR让.NET可呼叫。
3.创建一个class和一个写入档案的函数。
Library.h
#pragma once#ifndef LIBRARY_H#define LIBRARY_H#include <fstream>#include <string>class Library{public:Library();~Library();void Write(const char* str);private:const char* LOG_FILE;std::fstream _fwLog;};#endif // !LIBRARY_H
4.实作函数。
Library.cpp
#include "Library.h"Library::Library(){LOG_FILE = "D:\\Log.txt";_fwLog.open(LOG_FILE, std::ios::app);}Library::~Library(){_fwLog.close();}void Library::Write(const char* str){_fwLog << str;}
5.汇出DLL函数,呼叫时为mndtWrite。
MNDTLibrary.cpp
#ifdef MNDTLIBRARY_EXPORTS#define MNDTLIBRARY_API __declspec(dllexport)#include "Library.h"extern "C" MNDTLIBRARY_API void mndtWrite(const char* msg){Library lib;lib.Write(msg);}#else#define MNDTLIBRARY_API __declspec(dllimport)#endif
6.我这边使用x64编译。
创建C#
1.设定unsafe(后面会使用到指标来传资料),和x64编译。
2.创建class呼叫C++汇入函数使用,这边我指定路径读取C++编译的DLL。
MNDTLibrary.cs
class MNDTLibrary{ private const string DLL_PATH = @"D:\Visual Studio 2013 Project\Projects\MNDTLibrary\x64\Debug\MNDTLibrary.dll"; [DllImport(DLL_PATH)] private static extern void mndtWrite(string msg); public void Write(string msg) { mndtWrite(msg); }}
3.呼叫Write写入D:/Log.txt。
Form1
public partial class Form1 : Form{ public Form1() { InitializeComponent(); MNDTLibrary mndt = new MNDTLibrary(); mndt.Write("Hello World"); }}
4.我这边使用x64编译。
Debug设定
Debug设定只需要在C++ Dll档那边设定C#编译出来的exe路径,按下Debug即可。
1.设定位置。
结语
接下来要介绍一些影像的色彩转换,这篇文献因为有点久远忘记参考哪篇所以这次没打文献请见谅。