S-function crashes when accessing external dll compiled with /CLR

4 次查看(过去 30 天)
Hello, I have been trying to use .NET libraries in simulink. I am testing it with the c++/cli dll that calls the .NET assembly and am trying to include this dll into the s-function. The code for the dll is below.
Inside the s-function I use #include "ManagedDll.dll" ,and then call the two exposed functions in the mdlOutputs.
The s-function compiles normally (with with -lManagedDll) , but when running it causes a crash with "Fatal Exception" and the stack trace seems to point to the CLR.dll
If I remove the call to the dll functions everything runs normally, no crashes.
Is simulink not compatible with C++/CLI?
#include "stdafx.h"
#include "ManagedDll.h"
// ManagedDll.h
#pragma once
using namespace System;
using namespace System::Reflection;
namespace ManagedDll {
public ref class DoWork
{
public:int CSharpIntSum(int arg1, int arg2)
{
ManagedCSharp::ManagedClass^ mcs = gcnew ManagedCSharp::ManagedClass(1, 2);
return mcs->IntSum(arg1, arg2);
}
public:float CSharpFloatSum(float arg1, float arg2)
{
ManagedCSharp::ManagedClass^ mcs = gcnew ManagedCSharp::ManagedClass(1, 2);
return mcs->FloatSum(arg1, arg2);
}
};
}
__declspec(dllexport) int GetIntResult(int arg1, int arg2)
{
ManagedDll::DoWork work;
return work.CSharpIntSum(arg1, arg2);
}
__declspec(dllexport) float GetFloatResult(float arg1, float arg2)
{
ManagedDll::DoWork work;
return work.CSharpFloatSum(arg1, arg2);
}

回答(1 个)

Maneet Kaur Bagga
Maneet Kaur Bagga 2023-9-13
Hi Pavel,
As per my understanding of the question the code provided is an example of a C++/CLI DLL that wraps around a .NET assembly called "ManagedDll". The DLL exposes two functions, "GetIntResult" and "GetFloatResult", which internally call functions from the "ManagedDll" assembly. This code demonstrates how to create a C++/CLI DLL that acts as a bridge between native C++ code and managed .NET code.
The issue being faced is an S-Function feature.Simulink is primarily designed to work with C and C++ code, and it may not have direct support for C++/CLI (Common Language Infrastructure). C++/CLI is a specific technology that allows to mix native C++ code with managed .NET code.The crash might be caused due to the custom .NET file and not due to the MATLAB generated files.
For better understanding of the S-Function block please refer to the MATLAB Documentation below.
Thank You!
Maneet Bagga

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

产品


版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by