Simulink and DLLs

Simulink and DLLs

本文关键字:DLLs and Simulink      更新时间:2023-10-16

我需要将.EXE与simulink集成。我正在尝试在Simulink中创建一个C++DLL。该DLL稍后应合并到simulink中的S函数调用中。

信息流将是:

  1. Simulink执行S功能块
  2. 此块从C++DLL调用一个方法
  3. C++方法执行EXE,并返回一个结果
  4. 结果一直返回到simulink

ps:我没有EXE的源代码,它是一个黑盒。这就是为什么我要创建一个c++包装器,以便从simulink执行它。

到目前为止,C++DLL包装器无法工作。代码如下。

#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport) 
#else
#define MATHFUNCSDLL_API __declspec(dllimport) 
#endif
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
namespace MathFuncs
{
// This class is exported from the MathFuncsDll.dll
class MyMathFuncs
{
public: 
// Returns a + b
static __declspec(dllexport)  double Add(double a, double b); 
};
}
#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif

*.cpp

#include "stdafx.h"
#include <stdexcept>
#include "MathFuncsDll.h"
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{  return a + b;   }
}

请注意,我正在使用(extern"c")命令来生成一个可以在Matlab c风格中使用的DLL。

当我尝试加载DLL:时

  • 加载库('myDLL','myDLL.h')
  • 库函数MathFuncsDll

"没有类lib.MathFuncsDll的方法或没有类lib.MathFuncsDll.">

所以,我的问题是:

  1. 我的dll项目出了什么问题
  2. 使用这种方法可以将exe与simulink集成吗

obs:类似的问题在这里

给出一些提示:

你想要一个2级的matlab函数,在这里解释:http://www.mathworks.de/de/help/simulink/sfg/writing-level-2-matlab-s-functions.html从msfuntmpl_basic.m模板开始。

您可以完全跳过级别1的函数,它们的存在只是为了向后兼容。

我不是在解决dll错误,而是您从Simulink运行exe的原始问题。

  1. 为什么不像Daniel建议的那样使用system命令直接从MATALB代码运行exe?

  2. 如果你写一个C++s函数,它的接口更容易实现,你可以从C++s函数调用你的exe。即,您将使用C++s函数作为Simulink和exe之间的接口,而不是DLL。