I´m trying to use Pinvoke to call Matlab functions from C#.
My project configuration is:
Example:
#include "stdafx.h"
#include <stdarg.h>
#include <string>
#include "matrix.h"
#include "mex.h"
extern "C" _declspec(dllexport) bool blockExists()
{
std::string blockPath = "model/myblockpath";
mxArray *pin[1];
int nin = 1;
mxArray *pout[1];
int nout = 1;
pin[0] = mxCreateString( blockPath.c_str() );
if ( mexCallMATLAB( nout, pout, nin, pin, "find_system" ) != 0 ) {
callStatus = false;
}
mxDestroyArray( pin[0] );
return callStatus;
}
Example:
using System.Runtime.InteropServices;
namespace ManagedMatlabWrapper
{
public class MatlabWrapper
{
[DllImport(@"MatlabAPI.dll")]
private static extern bool blockExists();
public static bool blockExistsAPI()
{
bool result = blockExists();
return result;
}
}
}
Everything compiles OK
But when I run the code, it gives me an error of DLL not found exception.
I checked the DLL generated using a dependency walker and it shows that some Matlab DLL are not found. If i change the code and remove any reference to Matlab, Pinvoke runs just fine.
Does anyone know whats missing? What should I do to call mexCallMATLAB from c#?
mexCallMATLAB won't work unless it is called from a MATLAB process via a MEX file. There is too much uninitialized context without that. So even if you get the right DLLs on the search path, it will crash when you try to execute it.
It sounds like maybe you would be better off looking at either the MATLAB Engine API or the MATLAB COM Automation Server.