关于c#:“ MathWorks.MATLAB.NET.Utility.MWMCR”的类型初始值设定项引发了异常

The type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception

" MathWorks.MATLAB.NET.Utility.MWMCR"的类型初始值设定项引发了异常:

我从c#调用,这是MatLab库编译器编译的DLL中的函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using AddVidToDB;

namespace AddToDb
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 obj = null;
            // MWNumericArray input = null;
            MWNumericArray output = null;
            MWArray[] result = null;
            MWArray input = null;

            try
            {
                // Instantiate your component class.
                obj = new Class1();

                // Invoke your component.
                string VidName ="080626439d8809181a1857685bdc645.mp4";
                input[0] = VidName;
                obj.AddVidToDB(input);
            }
            catch
            {
                throw;
            }
        }
    }
}

当我运行代码时,它给出了一个例外:

"The type initializer for 'AddVidToDB.Class1' threw an exception.".
The inner exception says:"The type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.".

堆栈跟踪说:

at AddVidToDB.Class1..ctor()
at AddToDb.Program.Main(String[] args) in d:\\MSc\\Project\\SurfSearchVid\\AddToDb\\AddToDb\\AddToDb\\Program.cs:line 40
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

那么我该如何解决呢?


您必须在计算机上安装运行时库才能调用MCR dll。
如果有,请检查PATH(环境变量)是否包含运行时路径。
您可以使用以下命令检查运行代码的用户是否具有正确的路径变量:

1
Environment.GetEnvironmentVariable("Path");

该变量必须包含以下内容:

C:\\Program Files\\MATLAB\\MATLAB Compiler Runtime\\v79\
untime\\{win32|win64};