C#调用matlab时,类型初始值设定项引发异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#调用matlab时,类型初始值设定项引发异常相关的知识,希望对你有一定的参考价值。

C#建立动态链接库dll后,生成是成功的,但是调用时,会出现类型初始值设定项引发异常,具体错误如下:
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.TypeInitializationException: “MathWorks.MATLAB.NET.Arrays.MWNumericArray”的类型初始值设定项引发异常。 ---> System.TypeInitializationException: “MathWorks.MATLAB.NET.Arrays.MWArray”的类型初始值设定项引发异常。 ---> System.TypeInitializationException: “MathWorks.MATLAB.NET.Utility.MWMCR”的类型初始值设定项引发异常。 ---> System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
在 MathWorks.MATLAB.NET.Utility.MWMCR.mclIsMCRInitialized()
在 MathWorks.MATLAB.NET.Utility.MWMCR..cctor()
--- 内部异常堆栈跟踪的结尾 ---
在 MathWorks.MATLAB.NET.Utility.MWMCR.SetResourceManager(ResourceManager resourceManager)
在 MathWorks.MATLAB.NET.Arrays.MWArray..cctor()
--- 内部异常堆栈跟踪的结尾 ---
在 MathWorks.MATLAB.NET.Arrays.MWNumericArray.get__Inf()
在 MathWorks.MATLAB.NET.Arrays.MWNumericArray..cctor()
--- 内部异常堆栈跟踪的结尾 ---
在 MathWorks.MATLAB.NET.Arrays.MWNumericArray.op_Implicit(Double[] values)
在 Demeter.SVM.button1_Click(Object sender, EventArgs e) 位置 D:\work\Demeter_svm\Demeter\SVM.cs:行号 120
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
求高手解答,谢谢。解决问题了,所有积分全部给你了。

参考技术A #include<stdio.h>
void printSeven(int n)
if(n==0)return;
printSeven(n/7);
printf("%d",n%7);

main()
char s[10];
int i;
int n=0;
scanf("%s",s);
n=0;
for(i=0;s[i];i++)n=n*5+s[i]-'0';
printSeven(n);
参考技术B 是不是权限问题 参考技术C 这个regsvr32 mwcomutil.dll注册了吗?
试试看追问

注册过了,环境变量什么的也改过了,但是每次调用都出现这问题,以前也没遇到过,真的是没办法了

追答

你可以将MCRInstaller.exe从新安装一下到C盘,然后将再将regsvr32 mwcomutil.dll注册,就行了!

本回答被提问者采纳

“Emgu.CV.CvInvoke”的类型初始值设定项引发异常。

RT,跪求解决方法

参考技术A 首先确定没有重复安装opencv和emgucv两种,这样有可能在配置path时会出错。emgucv是包含opencv的,使用C#开发的朋友们,可以放心的只安装emgucv。

1、如遇错误提示:tbb_debug.dll找不到
请下载安装Intel TBB库,需要将tbb库或dll文件所在路径,配置带系统环境变量path中。

2、如遇错误提示:“Emgu.CV.CvInvoke”的类型初始值设定项引发异常
(1)、注销或重启计算机,使得系统环境变量中的path生效;
(2)、通过path中设置的路径去寻找对应的dll文件,查看是否存在,是否被成功引用到VS中;
(3)、copy dll文件到debug和release目录中。
(4)、尝试将.Net FrameWork版本降低,如果是4.0的就降低到3.5,如果是3.5的就降低到2.0。

官方解决办法:
(1)安装时注意:
ou can run an AutoIt script that automates much of the process, including installing OpenCV if necessary, and lets you start developing with Visual Studio right away.
Getting the Dependency
Emgu CV
use WCF(Windows Communication Foundation) therefore requires .Net 3.0
For version 2.4+ the bundled OpenCV binary is build with Visual Studio 2010, you will needs to installed
MSVCRT 9.0 SP1 x86
or
MSVCRT 9.0 SP1 x64to resolve the dependency issue.
For Version 2.0+, the bundled OpenCV binary is build with Visual Studio 2008, you will needs to installed
MSVCRT 9.0 SP1
to resolve the dependency issue.
For Version 1.5, the bundled OpenCV pre1.1 binary is build with Visual Studio 2005, you will needs to installed
MSVCRT 8.0 SP1
to resolve the dependency issue.
Building the Examples
Follow
this link
to the file server on Source Forge.
Download and extract the windows installer.
Install the software
Go to the Solution folder
If you are using Visual Studio 2008, browse to VS2008
If you are using Visual Studio 2010, browse to VS2010
Open
Emgu.CV.Example.sln
and build the solution. At this point, you should be able to run the example programs.
Creating a New Project in Visual Studio
To use the framework in Visual Studio, you need to
Download and extract the binary files package
Emgu.CV.Windows.Binary-version.zip
For a Full guide to using the dependencies under Visual Studio and C# see the C# Tutorial.
Core Functionality
Managed Code
Create a new Visual Studio project or use an existing one
Add the two files
Emgu.Utils.dll
and
Emgu.CV.dll
to
References
of the project. as well.
Optionally put the following lines in the top of your code to include the Emgu.CV namespace.

using Emgu.CV; using Emgu.CV.Structure;

Open CV unmanaged dll
Copy the OpenCV dll files: opencv_calib3dXXX.dll, opencv_contribXXX.dll, opencv_coreXXX.dll, opencv_features2dXXX.dll, opencv_ffmpegXXX.dll,opencv_flannXXX.dll, opencv_gpuXXX.dll, opencv_highguiXXX.dll, opencv_imgprocXXX.dll, opencv_legacyXXX.dll, opencv_mlXXX.dll,opencv_objdetectXXX.dll, opencv_videoXXX.dll and cvextern.dll to the execution directory, where XXX is the OpenCV version number.
(2)The type initializer for 'Emgu.CV.CvInvoke' threw an exception.(Emgu.CV.CvInvoke 的类型初始值设定项引发异常)
If you see this exception, please check the following
Have you installed MSVCRT?
For Version 2.4+, the bundled OpenCV binary is build with Visual Studio 2010, you will needs to installed
MSVCRT 10.0 SP1 x86
or
MSVCRT 10.0 SP1 x64
to resolve the dependency issue.
For Version 2.0+, the bundled OpenCV binary is build with Visual Studio 2008, you will needs to installed
MSVCRT 9.0 SP1
to resolve the dependency issue.
For Version 1.5, the bundled OpenCV pre1.1 binary is build with Visual Studio 2005, you will needs to installed
MSVCRT 8.0 SP1
to resolve the dependency issue.
Have you copied the OpenCV dlls to the execution directory?
Make sure the unmanaged DLLs are in the execution directory.
For Emgu CV version <= 2.1, this means the following dlls:cvXXX.dll, cvauxXXX.dll, cxcoreXXX.dll, highguiXXX.dll, opencv_ffmpegXXX.dll, mlXXX.dllcvextern.dll
where
XXX
is the OpenCV version number.
For Emgu CV version 2.2, 2.3 this means the following dlls:
opencv_calib3dXXX.dll, opencv_contribXXX.dll, opencv_coreXXX.dll, opencv_features2dXXX.dll, opencv_highguiXXX.dll, opencv_imgprocXXX.dll, opencv_legacyXXX.dll, opencv_mlXXX.dll, opencv_objectdetectXXX.dll, opencv_videoXXX.dll
where
XXX
is the OpenCV version number.
For EMGU CV version >=2.4
cudart64_42_9.dll, cvextern.dll, npp64_42_9.dll, opencv_calib3dXXX.dll, opencv_contribXXX.dll, opencv_coreXXX.dll, opencv_features2dXXX.dll, opencv_flannXXX.dll, opencv_highguiXXX.dll, opencv_imgprocXXX.dll, opencv_legacyXXX.dll, opencv_mlXXX.dll, opencv_nonfreXXX.dll, opencv_objectdetectXXX.dll, opencv_videoXXX.dll,
where
XXX
is the OpenCV version number.
The best way to set up your project is:
Copy the unmanaged DLLs to your project folder
Right click on the project, click Add->Existing Item and select all unmanaged DLLs. Add them to the project.
For each of the included Dlls, left click on it, find the "Copy to Output Directory" option and select "Copy if newer"

以上是关于C#调用matlab时,类型初始值设定项引发异常的主要内容,如果未能解决你的问题,请参考以下文章

C#使用NLOG System.TypeInitializationException,类型初始值设定项引发异常

类型初始值设定项引发异常

“Emgu.CV.CvInvoke”的类型初始值设定项引发异常。

Memcached Memcached.ClientLibrary.SockIOPool”的类型初始值设定项引发异常

Emgu.CV.CvInvoke”的类型初始值设定项引发异常

Emgu.CV.CvInvoke”的类型初始值设定项引发异常。