简体   繁体   English

仅在调试时才发生System.ExecutionEngineException

[英]System.ExecutionEngineException only when debugging

I am a C++ developer so I do not have much experience with C#, however, I have to fix this problem anyway. 我是C ++开发人员,所以我对C#没有太多经验,但是无论如何,我都必须解决此问题。

There is a C# GUI which is using some C++ DLLs. 有一个使用某些C ++ DLL的C#GUI。 When calling one of the methods from one of the DLLs a System.ExecutionEngineException occurs. 从DLL之一调用方法之一时,会发生System.ExecutionEngineException。 Obviously I cant step into the C++ code to see if there is a problem with this code. 显然,我无法进入C ++代码以查看此代码是否存在问题。 So I build a small C++ testing application and this one worked fine. 因此,我构建了一个小型C ++测试应用程序,该应用程序运行良好。 The parameters the method is called with in the C# code also look fine to me (used the same values in my C++ testing app). 在C#代码中使用该方法调用的参数对我来说也很好(在我的C ++测试应用程序中使用了相同的值)。 Although there is a problem somewhere in the GUI which may or may not be related to this issue, the same executable started without Visual Studio works just fine. 尽管在GUI某处存在一个问题,该问题可能与此问题相关,也可能不相关,但是在没有Visual Studio的情况下启动的同一可执行文件也可以正常工作。

I have currently no idea on how to find out what causes the problem. 我目前不知道如何找出导致问题的原因。 Any hints will be highly appreciated. 任何提示将不胜感激。

Call to method in C# code where caseID is of type int, paramID is of type System.Int32 and paramData is a custom struct. 用C#代码调用caseID为int类型,paramID为System.Int32类型,paramData为自定义结构的方法。

getParameter(caseID, dataID, paramID, ref paramData)

The signature of the method in the C# code looks like this: C#代码中方法的签名如下所示:

[ DllImport ("helper.dll", CallingConvention=CallingConvention.Cdecl/*, CharSet = CharSet.Auto*/ )]
public static extern ERROR getParameter(System.Int32 caseID, System.Int32 dataID, System.Int32 paramID, ref PARAMETER_DATA data);

The signature of the corresponding method in the C++ DLL looks like this: C ++ DLL中相应方法的签名如下所示:

ERROR _stdcall getParameter(const long caseID, const long dataID, long number, PARAMETER_DATA *data);

What does look odd to me is that the C++ code explicitly states that the method should be called with calling convention "stdcall" whereas the C# code want to use Cdecl. 在我看来,奇怪的是,C ++代码明确声明应使用调用约定“ stdcall”来调用该方法,而C#代码要使用Cdecl。 However, this code seems to have worked for quite a while now. 但是,这段代码似乎已经工作了很长时间了。 I also tried to change both calling conventions to stdcall but that did not work either. 我还尝试将两种调用约定都更改为stdcall,但这也不起作用。

The problem was that a struct member which is a char[] in C++ changed its size. 问题是在C ++中为char []的struct成员更改了其大小。 In the C# code there was this attribute 在C#代码中有此属性

[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=11)]

So I had to change the value of the SizeConst property. 因此,我不得不更改SizeConst属性的值。 Don't know why I didn' see this when I first checked the structs for differences... 不知道为什么当我第一次检查结构差异时为什么没有看到这个...

I also fixed the calling convention issue and now everything seems to work fine. 我还解决了调用约定问题,现在一切似乎都正常。

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

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