English 中文(简体)
FatalExecutionEngineError, C# Marshalling
原标题:FatalExecutionEngineError during C# Marshalling

I m running into an issue when trying to read c++ structures from memofields in a number of DBase IV files into C# (.Net 4) and then insert them into MSSQL 2008. The data is being extracted ok from the DBase files but I seemed to be doing something wrong with managing the unsafe calls because I randomly get the following error:

FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the error was
at 0x791fa62c, on thread 0x16c0. The error code is 0xc0000005. This error may be
a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common
sources of this bug include user marshaling errors for COM-interop or PInvoke, 
which may corrupt the stack. 

下面是使用代码一读数据。 该法典成功完成,我从档案中获取的数据是正确的。 这一错误是在这项职能被称作几分钟之后发生的,当时这些物体正在使用原状输入数据库(由于我不认为该问题确实重要,我将其删除)。

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Event
{        
    public int      Number;
    public int      Month;
    public int      Day;
    public int      Year;
    public int      Hour;
    public int      Minute;
    public int      Second;
    public UInt32   UPCTime;
    public int      BlobSize;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = DataLengths.MAX_EVENT_TITLE_LENGTH)]
    public string   Title;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = DataLengths.MAX_TRIGGER_LENGTH)]
    public string Trigger;
 }

public struct Trigger
{
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Control;
    public int Index;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Mode;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string RecordFreq;
    public int Pre;
    public int Post;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string Source;
    public int Delay;
    public int EventUserNotify;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
    public int[] Spare;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = DataLengths.MAX_EVENT_SENSORS)]
    public int[] Sensors;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Value
{
    public Trigger Trigger;
    public string[] SensorLabels;
    public Point[] Point;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Point
{
    public Single Value;
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string State;
}

//The dbf is from the java xBasej library that I compiled into a dll using IKVM.net
public Dictionary<Event, Value> Read(DBF dbf)
{     
Dictionary<Event, Value> eventData = new Dictionary<Event, Value>();

try
{
    for (int i = 1; i <= dbf.getRecordCount(); i++)
    {
         dbf.gotoRecord(i);
         MemoField memofield = (MemoField)dbf.getField("MemoField");

         // Perform the conversion from one encoding to the other.
         byte[] blob = memofield.getBytes();

         if (blob.Length == 0)
         {
             continue;
         }

         MemoryStream memoryStream = null;
         BinaryReader binaryReader = null;
         GCHandle eventHandle = new GCHandle();
         GCHandle triggerHandle = new GCHandle();
         GCHandle sensorLabelHandle = new GCHandle();
         GCHandle pointHandle = new GCHandle();

         try
         {
             memoryStream = new MemoryStream(blob);
             binaryReader = new BinaryReader(memoryStream);

             //The data was orignally C++ structures so we read the bytes back into C# equivalent
             //structures.
             int eventDataSize = Marshal.SizeOf(typeof(Event));

             eventHandle = GCHandle.Alloc(binaryReader.ReadBytes(eventDataSize), GCHandleType.Pinned);
             Event @event = (Event)Marshal.PtrToStructure(eventHandle.AddrOfPinnedObject(), typeof(Event));

             //Read the event trigger data
             int triggerDataSize = Marshal.SizeOf(typeof(Trigger));
             triggerHandle = GCHandle.Alloc(binaryReader.ReadBytes(triggerDataSize), GCHandleType.Pinned);
             Trigger trigger = (Trigger)Marshal.PtrToStructure(triggerHandle.AddrOfPinnedObject(), typeof(Trigger));

             Value value = new Value();
             value.Trigger = trigger;

             triggerHandle.Free();

             //Read all the sensor labels
             List<string> sensorLableList = new List<string>();
             for (int sensorIndex = 0; sensorIndex < DataLengths.MAX_EVENT_SENSORS; sensorIndex++)
             {
                   int sensorLableDataSize = DataLengths.MAX_LABEL_LENGTH;
                   sensorLabelHandle = GCHandle.Alloc(binaryReader.ReadBytes(sensorLableDataSize), GCHandleType.Pinned);
                  string label = Marshal.PtrToStringAnsi(sensorLabelHandle.AddrOfPinnedObject(), sensorLableDataSize).Trim();
                   sensorLableList.Add(label);

                   sensorLabelHandle.Free();
             }
             value.SensorLabels = sensorLableList.ToArray();
             //Read all the recorded sensor data
             List<Point> pointList = new List<Point>();
             for (int pointIndex = 0; pointIndex < DataLengths.MAX_EVENT_SENSORS; pointIndex++)
             {
                  int pointDataSize = Marshal.SizeOf(typeof(Point));

                  pointHandle = GCHandle.Alloc(binaryReader.ReadBytes(pointDataSize), GCHandleType.Pinned);
                  Point point = (Point)Marshal.PtrToStructure(pointHandle.AddrOfPinnedObject(), typeof(Point));
                  pointList.Add(point);

                  pointHandle.Free();
             }

             value.Point = pointList.ToArray();

             eventData.Add(@event, value);
             eventHandle.Free();
        }
        finally
        {
             //Free all the resources used to get the data
             if (memoryStream != null) { memoryStream.Close(); }
             if (binaryReader != null) { binaryReader.Close(); }
             if (eventHandle.IsAllocated) { eventHandle.Free(); }
             if (triggerHandle.IsAllocated) { triggerHandle.Free(); }
             if (sensorLabelHandle.IsAllocated) { sensorLabelHandle.Free(); }
             if (pointHandle.IsAllocated) { pointHandle.Free(); }

             GC.Collect();
         }
    }

}
finally
{
     if (dbf != null)
     {
        dbf.close();
     }
}                

return eventData;
}
最佳回答

令人感兴趣的失败模式不应发生。 通常,由于垃圾收集的肥皂正在被销毁,你会获得欧洲常规能源论坛。 通常很容易解释这种错误的pin土功能,如过度的缓冲。 这里没有pin。

然而,这一错误的一位杰出候选人:

[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
public string Control;

为此,设计了一种由零污染特性组成的扼杀体。 SizeConst = 1不能通过设计妥善工作,只能为零确定者留有余地,而不是任何特性。 其中,根据实地申报,档案中没有任何被终止的胎体,是固定的。

越野车是否真是造成这种错失,这是一个公开的问题,但当试图找到零点时,这同样容易成为炸弹。 没有发现一页,并混为一谈。

Anyhoo, you re doing it wrong. You must use a char[] instead of a string in the structure declaration and use ByValArray in the [MarshalAs] attribute. Pretty painful coding btw.

问题回答

您 三角结构,你是否确信你有正确的包装? 如您本打算予以抵消,该指数成员将被抵消4。 1 ? • 与前和邮政局合用。

Also, what size is Control supposed to be? If it is a string, usually unmanaged strings are null-terminated. By specifying the length of 1, that would indicate to me a single character. If that is the case, why not use char for it instead of string?

Error 0xc0000005是一种出入侵犯。

你们是否试图根据夸张的手法行事,并允许无管理的偷偷窃行为? 那么,你就可以把侵犯进出权的行为定在被扔进去的时候。 NET 框架代码。 然后,你可以记忆,了解它可能做些什么。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签