S-function crashes when accessing external dll compiled with /CLR
4 views (last 30 days)
Show older comments
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);
}
0 Comments
Answers (1)
Maneet Kaur Bagga
on 13 Sep 2023
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
0 Comments
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!