C# 从C调用Matlab编译函数时出现访问冲突错误#

C# 从C调用Matlab编译函数时出现访问冲突错误#,c#,matlab,C#,Matlab,我在从C#调用Matlab函数时遇到问题。调用Matlab函数时,会出现访问冲突错误(0x00000005) 创建了简单的Matlab函数: function fcnHelloStrIn(str) fprintf(sprintf('Hello world ... %s\n\n', (str))); 包含此函数的dll库是由Matlab编译器(4.17)创建的 我试图实现简单的C#wrapper如下(只包括相关的代码行) 调用函数mlxFcnHelloStrIn时,会出现访问冲突错误(0xc0

我在从C#调用Matlab函数时遇到问题。调用Matlab函数时,会出现访问冲突错误(0x00000005)

创建了简单的Matlab函数

function fcnHelloStrIn(str)

fprintf(sprintf('Hello world ... %s\n\n', (str)));
包含此函数的dll库是由Matlab编译器(4.17)创建的

我试图实现简单的C#wrapper如下(只包括相关的代码行)

调用函数mlxFcnHelloStrIn时,会出现访问冲突错误(0xc0000005)。有人知道如何解决这个问题吗

提前谢谢


Martin

下载示例,看看哪里出了问题。感谢您的回答,但我认为这个示例适用于使用Matlab Builder的情况。我改用Matlab编译器(和MCR)。
bool MW_CALL_CONV mlxFcnHelloStrIn(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
...

// ----- DLL import -----

[DllImport("HelloDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]

public static extern bool mlxFcnHelloStrIn([In] int nlhs, ref IntPtr outParams, [In] int nrhs, [In] IntPtr inParams);

...

MCR and Dll initialization function // OK

...

// ----- Main part -----

IntPtr outputPtr = IntPtr.Zero;

IntPtr stringPtr = IntPtr.Zero;

string helloString = "Hello world!";

stringPtr = mxCreateString(helloString);

bool result = mlxFcnHelloStrIn(0, ref outputPtr, 1, stringPtr);

....