如何调用在MATLAB一个简单的MATLAB函数生成的DLL从C#
问题描述:
建立从MATLAB一个DLL和C#如何调用在MATLAB一个简单的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);
2RD部分:(最好是在一个功能被包裹)
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);