2011-03-10 2 views
4

이 코드 라인이 실행되면이 예외가 발생합니다.System.ExecutionEngineException이 throw되고 있습니다.

retobj = Marshal.PtrToStructure(buffer, anytype); 

실행하려고하는 응용 프로그램이 다른 개발자 시스템에서 제대로 작동하기 때문에 원인을 알 수 없습니다.

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; 
} 

.NET Compact Framework를 여러 번 복구하려고 시도했지만 아무 것도 작동하지 않는 것 같습니다. 아무도 해결책을 알고 있습니까?

+0

예외 (및 모든 'InnerException')에 대한 추가 정보를 제공하십시오. – Jon

+0

예외가 InnerException에 더 많은 정보를 가지고 있습니까? –

+0

내부 예외가 null입니다. '공공 정적 오브젝트 RawDeserialize (바이트 [] rawdatas, 타입은 anyType) \t \t \t \t { \t INT rawsize = Marshal.SizeOf (anyType에); \t \t \t \t \t \t \t는 null (rawsize> rawdatas.Length 있으면); \t \t \t IntPtr 버퍼 = Marshal.AllocHGlobal (rawsize); \t \t \t \t \t \t object retobj = null; \t \t \t는 \t \t \t { \t \t \t \t 원수를보십시오.복사 (rawdatas, 0, buffer, rawsize); \t \t \t \t retobj = Marshal.PtrToStructure (buffer, anytype); \t \t \t \t \t} \t 마지막 \t \t \t \t { \t \t \t Marshal.FreeHGlobal (버퍼); \t \t \t} \t \t \t return retobj; \t \t} 이것은 줄이 들어있는 기능입니다. 그 외에는 더 이상 정보를 제공하지 않습니다. – user501211

답변

1

다음 줄이 예외를 throw 것을 확인할 수있는 것들 :

retobj = Marshal.PtrToStructure(buffer, anytype); 

주요 원인은 마샬링 도구는 형식을 마샬링하는 방법을 알고하지 않습니다. 구조체의

  1. 중첩 된 구조 (유형 anyType에의)

    • [StructLayout로 구조체를 앞에 붙여 해결이 내가 아는 가장 일반적인 두 가지가 있으며, 여러 가지 원인이있다 (LayoutKind.Sequential는 팩 = 1)]

  2. 중첩 배열.

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

이 도움 희망 접두어 어레이에 의해 해결했다.

관련 문제