[英]How to call a simple MATLAB function in a MATLAB generated DLL from C#
从MATLAB构建DLL并从C#调用其函数时遇到问题
这是MATLAB函数
function [success_code] = ENGINE_PING()
success_code = 42;
end
这是生成的C ++标头
extern LIB_ENGINE_C_API
bool MW_CALL_CONV mlxENGINE_PING(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
我的问题分为两部分:我应该使用什么PInvoke声明? 以及如何封送传递和返回值的值?
我已将其简化为我能想到的一个简单示例-由于许可原因,我无法使用MATLAB .NET Builder等。
我非常感谢您的帮助。
谢谢!
第一部分:
[DllImport(@"mclmcrrt7_17.dll", EntryPoint = "mclInitializeApplication_proxy", CallingConvention = CallingConvention.Cdecl)]
private static extern bool mclInitializeApplication(string options, Int32 count);
[DllImport(@"mclmcrrt7_17.dll", EntryPoint = "mclTerminateApplication_proxy", CallingConvention = CallingConvention.Cdecl)]
private static extern void mclTerminateApplication();
[DllImport("libmcc.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern bool _mlfCreat_smallsample (int nargout, ref IntPtr sample);
第二部分:(最好将其包装在函数中)
IntPtr num_ptr = MxCreateDoubleScalar((double)number);
IntPtr return_ptr = IntPtr.Zero;
double[] ans = new double[1];
_mlfFcn_add(1, ref return_ptr, sample, num_ptr);
Marshal.Copy(MxGetPr(return_ptr), ans, 0, 1);
return ans[0].ToString();
[DllImport(@"libmx.dll", CallingConvention = CallingConvention.Cdecl)]
rivate static extern IntPtr mxGetPr([In]IntPtr mxArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.