简体   繁体   English

在Matlab中创建共享库(.NET Assembly)并在C#中使用它

[英]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#. 我已经使用Application Compiler在Matlab(2014a)中创建了一个.NET程序集,我正在尝试在C#下使用它。

The matlab module has only 1 function: matlab模块只有一项功能:

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). 导出的.NET DLL名为AnalyzePicture.dll,并导出Class1(在Matlab应用程序编译器中定义)。 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). 我的项目引用了MWArray(当前版本为8.3)和AnalyzePicture dll(以及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#. 我试图在线查找解决方案,但找不到如何在C#中正确使用Matlab .NET程序集的良好示例。 I did exactly as described in this article besides that my assembly does not start with com. 我没有完全按照描述的这个文章,除了我的装配不启动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) (我使用的是Windows 7 64位,使用Matlab 2014a 8.3 64位)

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. 您需要设置项目设置以使用.Net Framework 4.0(而不是将4.5 VS 2013设置为默认版本),并将目标平台更改为x64。 This set works beautifully on Win 8.1 (x64), MatLab R2013a (x64, runtime version 8.1) and Visual Studio 2013. 此设置在Win 8.1(x64),MatLab R2013a(x64,运行时版本8.1)和Visual Studio 2013上可以很好地工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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