Nitay
Nitay

Reputation: 4690

Creating a shared library (.NET Assembly) in Matlab and using it in C#

I've created a .NET assembly in Matlab (2014a) using the Application Compiler, and I'm trying to use it under C#.

The matlab module has only 1 function:

function [ val ] = AnalyzePicture( arg1 )

val = 5;

end

The exported .NET DLL is named AnalyzePicture.dll and exports Class1 (as defined in the Matlab application compiler). However, when I try to initialize it, I get an exception saying:

The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWArray' threw an exception

With the inner exception saying:

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Here's the code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AnalyzePicture;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;

namespace Analyzer
{
    public class MatlabWrapper
    {
        AnalyzePicture.Class1 analyzer = null;

        public MatlabWrapper()
        {
            try
            {
               // The exception is raised here
                analyzer = new AnalyzePicture.Class1();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("f");
            }
        }

        ...

My project references MWArray (8.3, the current version) and the AnalyzePicture dll (and DirectShowLib-2010).

I tried to find solutions online but i couldn't find a decent example on how to properly use a Matlab .NET Assembly in C#. I did exactly as described in this article besides that my assembly does not start with com.

Any ideas on what could be causing the problem? Any code examples will be greatly appriciated

(I'm using Windows 7 64bit, with Matlab 2014a 8.3 64bit)

Upvotes: 2

Views: 4191

Answers (2)

user5435578
user5435578

Reputation: 21

IF you published matlab 64 bit dll , Change your VS Project Build Settings Any CPU and uncheck the "Prefer 32-Bit" property

Upvotes: 2

Zaba
Zaba

Reputation: 29

Just in case someone else will encounter the same issue. I had this problem some time ago, so I figured out complete solution to this.

You need to set project settings to use .Net Framework 4.0 (instead of 4.5 VS 2013 sets as default version) and change target platform to x64. This set works beautifully on Win 8.1 (x64), MatLab R2013a (x64, runtime version 8.1) and Visual Studio 2013.

Upvotes: 3

Related Questions