MATLAB Coder: Including a DLL function in C# which returns dynamic sized data
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I have got a simple method
function res = returnArray(a)
res = zeros(a, a);
end
which returns a 2D array of doubles. How can I include it in my C# client?
I exported it in a DLL and tried to import it like this (using the method signature created by the Coder):
using System.Runtime.InteropServices;
class Programm
{
[DllImport("returnArray.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void returnArray(double a, emxArray_real_T resArr);
static void Main()
{
double a = 1;
emxArray_real_T resArr = new emxArray_real_T();
returnArray(a, resArr);
}
}
This produces System.AccessViolationException: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
2 comentarios
Sumit Ghosh
el 12 de Feb. de 2024
You can try to avoid generation of emxArray types in the DLL by not relying on variable size arrays. This will result in generation primitive types like double arrays, which you can interface with C#.
To avoid using variable-size arrays, you can change the config settings related to Dynamic Memory Allocation. for this, you can use coder.EmbeddedCodeConfig
Option 1: Set the DynamicMemoryAllocation property to Off to disable variable-size arrays for all arrays in your code
Option 2: By default, DynamicMemoryAllocation is set to Threshold, and you can adjust the threhold value to something within which your array will not use variable-size. To do this, you can use the property DynamicMemoryAllocationThreshold (default vaue is 65536).
You can look at examples in https://www.mathworks.com/help/coder/ug/use-c-arrays-in-the-generated-function-interfaces.html to see how to generate the C function interface using primitive types instead of emxArrays.
Respuestas (1)
Sumit Ghosh
el 15 de Feb. de 2024
You can try to avoid generation of emxArray types in the DLL by not relying on variable size arrays. This will result in generation primitive types like double arrays, which you can interface with C#.
To avoid using variable-size arrays, you can change the config settings related to Dynamic Memory Allocation. for this, you can use coder.EmbeddedCodeConfig
Option 1: Set the DynamicMemoryAllocation property to Off to disable variable-size arrays for all arrays in your code
Option 2: By default, DynamicMemoryAllocation is set to Threshold, and you can adjust the threhold value to something within which your array will not use variable-size. To do this, you can use the property DynamicMemoryAllocationThreshold (default vaue is 65536).
You can look at examples in https://www.mathworks.com/help/coder/ug/use-c-arrays-in-the-generated-function-interfaces.html to see how to generate the C function interface using primitive types instead of emxArrays.
0 comentarios
Ver también
Categorías
Más información sobre MATLAB Coder en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!