gpt4 book ai didi

c# - 如何在 C# 中使用由 MATLAB 编译的 C++ DLL

转载 作者:行者123 更新时间:2023-11-28 06:49:35 24 4
gpt4 key购买 nike

我需要在我的 C# 代码中使用一个在 MATLAB 中编译的 C++ DLL 中的方法。

有几个 DLL,每个都有我需要的方法。

我的任务是制作一个 C# UI(Win 窗体会很好),它将调用这两个当然需要给定参数的方法:图像和矩阵。

extern LIB_alignment_C_API 
bool MW_CALL_CONV mlxAlignLine(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

extern LIB_libfindLines_C_API
bool MW_CALL_CONV mlxFindLines(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

文件列表: https://drive.google.com/file/d/0B-5Uh7wgKQPzYV8wbEFLSjBfbTA/edit?usp=sharing


H 文件代码如下:

对齐.h:

//
// MATLAB Compiler: 5.1 (R2014a)
// Date: Thu Jun 12 08:45:08 2014
// Arguments: "-B" "macro_default" "-v" "-W" "cpplib:alignment" "-T" "link:lib"
// "alignLine"
//

#ifndef __alignment_h
#define __alignment_h 1

#if defined(__cplusplus) && !defined(mclmcrrt_h) && defined(__linux__)
# pragma implementation "mclmcrrt.h"
#endif
#include "mclmcrrt.h"
#include "mclcppclass.h"
#ifdef __cplusplus
extern "C" {
#endif

#if defined(__SUNPRO_CC)
/* Solaris shared libraries use __global, rather than mapfiles
* to define the API exported from a shared library. __global is
* only necessary when building the library -- files including
* this header file to use the library do not need the __global
* declaration; hence the EXPORTING_<library> logic.
*/

#ifdef EXPORTING_alignment
#define PUBLIC_alignment_C_API __global
#else
#define PUBLIC_alignment_C_API /* No import statement needed. */
#endif

#define LIB_alignment_C_API PUBLIC_alignment_C_API

#elif defined(_HPUX_SOURCE)

#ifdef EXPORTING_alignment
#define PUBLIC_alignment_C_API __declspec(dllexport)
#else
#define PUBLIC_alignment_C_API __declspec(dllimport)
#endif

#define LIB_alignment_C_API PUBLIC_alignment_C_API


#else

#define LIB_alignment_C_API

#endif

/* This symbol is defined in shared libraries. Define it here
* (to nothing) in case this isn't a shared library.
*/
#ifndef LIB_alignment_C_API
#define LIB_alignment_C_API /* No special import/export declaration */
#endif

extern LIB_alignment_C_API
bool MW_CALL_CONV alignmentInitializeWithHandlers(
mclOutputHandlerFcn error_handler,
mclOutputHandlerFcn print_handler);

extern LIB_alignment_C_API
bool MW_CALL_CONV alignmentInitialize(void);

extern LIB_alignment_C_API
void MW_CALL_CONV alignmentTerminate(void);



extern LIB_alignment_C_API
void MW_CALL_CONV alignmentPrintStackTrace(void);

extern LIB_alignment_C_API
bool MW_CALL_CONV mlxAlignLine(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);


#ifdef __cplusplus
}
#endif

#ifdef __cplusplus

/* On Windows, use __declspec to control the exported API */
#if defined(_MSC_VER) || defined(__BORLANDC__)

#ifdef EXPORTING_alignment
#define PUBLIC_alignment_CPP_API __declspec(dllexport)
#else
#define PUBLIC_alignment_CPP_API __declspec(dllimport)
#endif

#define LIB_alignment_CPP_API PUBLIC_alignment_CPP_API

#else

#if !defined(LIB_alignment_CPP_API)
#if defined(LIB_alignment_C_API)
#define LIB_alignment_CPP_API LIB_alignment_C_API
#else
#define LIB_alignment_CPP_API /* empty! */
#endif
#endif

#endif

extern LIB_alignment_CPP_API void MW_CALL_CONV alignLine(int nargout, mwArray& globalOffsets, mwArray& warped_ref, const mwArray& scriptImgLine, const mwArray& textImgLine, const mwArray& rectData);

#endif
#endif

libfindLines.h:

//
// MATLAB Compiler: 5.1 (R2014a)
// Date: Thu Jun 12 08:46:31 2014
// Arguments: "-B" "macro_default" "-v" "-W" "cpplib:libfindLines" "-T"
// "link:lib" "findLines"
//

#ifndef __libfindLines_h
#define __libfindLines_h 1

#if defined(__cplusplus) && !defined(mclmcrrt_h) && defined(__linux__)
# pragma implementation "mclmcrrt.h"
#endif
#include "mclmcrrt.h"
#include "mclcppclass.h"
#ifdef __cplusplus
extern "C" {
#endif

#if defined(__SUNPRO_CC)
/* Solaris shared libraries use __global, rather than mapfiles
* to define the API exported from a shared library. __global is
* only necessary when building the library -- files including
* this header file to use the library do not need the __global
* declaration; hence the EXPORTING_<library> logic.
*/

#ifdef EXPORTING_libfindLines
#define PUBLIC_libfindLines_C_API __global
#else
#define PUBLIC_libfindLines_C_API /* No import statement needed. */
#endif

#define LIB_libfindLines_C_API PUBLIC_libfindLines_C_API

#elif defined(_HPUX_SOURCE)

#ifdef EXPORTING_libfindLines
#define PUBLIC_libfindLines_C_API __declspec(dllexport)
#else
#define PUBLIC_libfindLines_C_API __declspec(dllimport)
#endif

#define LIB_libfindLines_C_API PUBLIC_libfindLines_C_API


#else

#define LIB_libfindLines_C_API

#endif

/* This symbol is defined in shared libraries. Define it here
* (to nothing) in case this isn't a shared library.
*/
#ifndef LIB_libfindLines_C_API
#define LIB_libfindLines_C_API /* No special import/export declaration */
#endif

extern LIB_libfindLines_C_API
bool MW_CALL_CONV libfindLinesInitializeWithHandlers(
mclOutputHandlerFcn error_handler,
mclOutputHandlerFcn print_handler);

extern LIB_libfindLines_C_API
bool MW_CALL_CONV libfindLinesInitialize(void);

extern LIB_libfindLines_C_API
void MW_CALL_CONV libfindLinesTerminate(void);



extern LIB_libfindLines_C_API
void MW_CALL_CONV libfindLinesPrintStackTrace(void);

extern LIB_libfindLines_C_API
bool MW_CALL_CONV mlxFindLines(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);


#ifdef __cplusplus
}
#endif

#ifdef __cplusplus

/* On Windows, use __declspec to control the exported API */
#if defined(_MSC_VER) || defined(__BORLANDC__)

#ifdef EXPORTING_libfindLines
#define PUBLIC_libfindLines_CPP_API __declspec(dllexport)
#else
#define PUBLIC_libfindLines_CPP_API __declspec(dllimport)
#endif

#define LIB_libfindLines_CPP_API PUBLIC_libfindLines_CPP_API

#else

#if !defined(LIB_libfindLines_CPP_API)
#if defined(LIB_libfindLines_C_API)
#define LIB_libfindLines_CPP_API LIB_libfindLines_C_API
#else
#define LIB_libfindLines_CPP_API /* empty! */
#endif
#endif

#endif

extern LIB_libfindLines_CPP_API void MW_CALL_CONV findLines(int nargout, mwArray& linesFound, mwArray& locs, const mwArray& imageMat);

#endif
#endif

最佳答案

有一篇关于如何执行此操作的帖子和一个项目,但我相信它已被删除,因为 mathworks 有一个关于它的产品。

您在 C# 项目中要做的第一件事是使用 PInvoke 初始化 mcr(无论您将使用哪个库):

[DllImport(@"mclmcrrt7_17.dll", EntryPoint = "mclInitializeApplication_proxy", CallingConvention = CallingConvention.Cdecl)]
private static extern bool mclInitializeApplication(string options, Int32 count);

当然,最后终止mcr

[DllImport(@"mclmcrrt7_17.dll", EntryPoint = "mclTerminateApplication_proxy", CallingConvention = CallingConvention.Cdecl)]
private static extern void mclTerminateApplication();

然后看你的c文件,找到一个方法叫“你的库名”+Initialize,你的mcr初始化后就用它:

[DllImport("libmcc.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool _libmccInitialize();

注意:根据您使用的编译器,纯 mcc 命令行编译或 deploytool,方法名称略有不同,我建议使用 dll export viewer 来查找真正的方法名称:http://www.nirsoft.net/utils/dll_export_viewer.html

两种方法初始化后,您可以使用您的 matlab 函数:

[DllImport("libmcc.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool _mlfCreat_smallsample (int nargout, ref IntPtr sample);

注意:如果你想将任何参数传递给你的matlab函数,你应该在c#中创建一个指向这个参数的指针,这里有一个方法可以得到一个double number的指针

[DllImport(@"libmx.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr mxCreateDoubleScalar([In]double value);

关于c# - 如何在 C# 中使用由 MATLAB 编译的 C++ DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com