-1

I´m trying to use Pinvoke to call Matlab functions from C#.

My project configuration is:

  • C++ Project that uses "mex.h" to call "mexCallMATLAB" interface

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;
}
  • C# Project that uses PInvoke to call previous c++ project

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.

enter image description here

Does anyone know whats missing? What should I do to call mexCallMATLAB from c#?

guilhermecgs
  • 2,913
  • 11
  • 39
  • 69

2 Answers2

2

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.

SCFrench
  • 8,244
  • 2
  • 31
  • 61
  • I was afraid you are going to say that... :-) After several tests, I came to same conclusion. About the alternatives, whats your opinion about MATLAB COM speed and compatibility with Simulink (reading models, setting parameters, etc)? – guilhermecgs Aug 06 '15 at 12:29
  • 1
    Compatibility-wise, there shouldn't be any problem. The Feval function is basically the same thing as mexCallMATLAB. Speed-wise, I don't have enough experience with it to be able to give you a helpful answer. – SCFrench Aug 06 '15 at 14:22
0

The error is telling you that not all dependencies can be found. Your description suggests that your C++ DLL is located. But it in turn has dependencies on, at least, libmex, libmx and msvcr90. Some or all of these dependencies cannot be resolved.

One cheap (and dirty) way to resolve the issue would be to add the directories containing the MATLAB libraries to the PATH. A better way would be to amend the DLL search path with a call SetDllDirectory or AddDllDirectory before the first p/invoke call that leads to MATLAB.

The issue of MSVCR90 can be resolved by installing the MSVC redistributable package for that version of MSVC.

Finally though, MATLAB exposes its capabilities through .net assemblies. Why aren't you using those to avoid all this complexity.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • MATLAB Builder NE needs MCR installed on the final user machine to work . Besisdes that, I also need a 20k license. – guilhermecgs Jul 29 '15 at 15:46
  • Ops, my fault... Actually I really liked your advice... I am not an english speaker so my comments tend to be too direct and unpolite, althought I try to avoid it. It wasn't my inttention – guilhermecgs Jul 29 '15 at 16:12
  • Well, you know what to do now I think. – David Heffernan Jul 29 '15 at 16:25
  • I'm surprised by the downvote here. I think I explained what the error means and how you should fix it. Do you understand what I wrote? Do you appreciate what I mean when I explain that the dependencies cannot be resolved? – David Heffernan Aug 05 '15 at 19:39
  • It wasn't me who downvoted. You answer is well constructed and addresses part of the problem – guilhermecgs Aug 06 '15 at 12:36
  • 1
    My answer address the entirity of the question you asked. But I had not realised that even when you fix the dependencies, it is doomed to fail. – David Heffernan Aug 06 '15 at 12:36
  • Also, I realize that my question is very very specific on how Matlab works and it was very difficult to anyone to get it all – guilhermecgs Aug 06 '15 at 12:44
  • It's not that hard. I just answered the question that you asked. Which is what this site is all about. It seems you wanted more. – David Heffernan Aug 06 '15 at 12:45