简体   繁体   中英

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)

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.

如果您发布了matlab 64位dll,请更改任何CPU的VS项目构建设置,然后取消选中“首选32位”属性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM