System.ExecutionEngineException exception

This exception occurs when this line of code is executed.

retobj = Marshal.PtrToStructure( buffer, anytype ); 

I don't know what causes this because the application I'm trying to run works fine on other development machines.

 public static object RawDeserialize(byte[] rawdatas, Type anytype) { int rawsize = Marshal.SizeOf(anytype); if (rawsize > rawdatas.Length) { return null; } IntPtr buffer = Marshal.AllocHGlobal(rawsize); object retobj = null; try { Marshal.Copy(rawdatas, 0, buffer, rawsize); retobj = Marshal.PtrToStructure(buffer, anytype); } finally { Marshal.FreeHGlobal(buffer); } return retobj; } 

I tried to fix the .NET Compact Framework several times and nothing works, does anyone know about this solution?

+4
source share
1 answer

If you are debugging your program, you will find that the following line throws an exception:

  retobj = Marshal.PtrToStructure(buffer, anytype); 

The main reason is because the sorting tool does not know how to marshal your type. There are many possible reasons for this, two of the most common I know:

  • Nested structures in a structure (of anytype type)

    • solved the prefix of your structure using

      [StructLayout (LayoutKind.Sequential, Pack = 1)]

  • Nested Arrays.

    • resolved by prefixing the array using

      [MarshalAs (UnmanagedType.ByValArray, SizeConst = 512)]

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1343182/


All Articles