2

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)

3
  • 1
    Maybe it helps to compile your C# program for x86. Commented Nov 13, 2014 at 14:11
  • Well! Your hunch was right - Setting the compiler from "Any CPU" to "x64" fixed it Commented Nov 13, 2014 at 14:17
  • 1
    I just wanted to say that you should try x64 as well ;-) I'm glad I could help Commented Nov 13, 2014 at 14:19

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.