从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等出于许可的原因。
我非常感谢你的帮助。
谢谢!
答案 0 :(得分:0)
第一部分:
[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);
第2部分:(最好包装在一个函数中)
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);