AccessViolationException when Marshal.PtrToStructure fires

▼魔方 西西 提交于 2019-12-13 04:33:59

问题


i am trying to use a native dll from VS2012 C# env. i have a weird scenario: case1:

        IntPtr p_stat = IntPtr.Zero;
        dcUe.dcUeGetMacStats(_helper.Handle, out p_stat);//native function call thatallocates memory and points the pointer to it
        MacStatistics stats = (MacStatistics)Marshal.PtrToStructure(p_stat,   typeof(MacStatistics));

with the following Pinvoke wrapper:

        [DllImport("dcc.dll",CharSet=CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern UInt32 dcUeGetMacStats(thpHandle_t handle, /* MacStatistics*/out IntPtr stats);

this is the class:

                [StructLayout(LayoutKind.Sequential)]
            public class MacStatistics {
        public u32 activeDemultFailNumber;
        public u32 activeDemultySuccessNumber;
        public u32 pdschTotalDataNumber;
        public u16 taTimerLength;
        public u32 activePdschCrcCorrectNumber;
        public u32 activePdschCrcErrorNumber;
        public u8 antennaPortNumber;
        public u32 dlSystemRbNumber;
        public u32 parseDci0SuccessNumber;
        public u32 pdschCrcCorrectNumber;
        public u32 pdschCrcErrorNumber;
        public u32 pdschDynamicNumber;
        public u32 pdschSemiStaticNumber;
        public u32 receiveDci0Number;
        public u32 sendPucchSuccessNumber;
        public u32 sendPuschSuccessNumber;
        public u32 ulSubCarrierRBNumber;
        public u32 ulSymbolNumber;
        public u32 ulSystemRbNumber;
    };

this case works fine case 2:

        IntPtr p_stats = IntPtr.Zero;
        st = dcUe.dcUeGetNasInfo(_helper.Handle, out p_stats);//nativs function call that allocates memory and points the pointer to it
        NasInfo ueNasState = (NasInfo)Marshal.PtrToStructure(p_stats, typeof(NasInfo));

with the following Pinvoke wrapper:

        [DllImport("dcc.dll",CharSet=CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    public static extern UInt32 dcUeGetNasInfo(thpHandle_t handle, /* NasInfo*/out IntPtr info);

this is the class:

        [StructLayout(LayoutKind.Sequential)]
    public class NasInfo {
        public  EmmPlmnSelectMode plmnSelectMode;//enum
        public u32 pdnQty;
        public EpsPdnAddress pdnArray;
        public Bool_t emmRegistred;
        public Bool_t rrcConnected;
        public EpsIntegrityAlgorithm integrityAlgo;//enum
        public EpsCipheringAlgorithm cipheringAlgo;//enum
    };

    [StructLayout(LayoutKind.Sequential)]
public class EpsPdnAddress {
    sqnBool_t epsIpv4AddressPresent;
    u8 [] epsIpv4Address = new u8[4];
    sqnBool_t epsIpv6AddressPresent;
    u8 [] epsIpv6Address = new u8[8];

}

this case throws AccessViolationException in the line:

NasInfo ueNasState = (NasInfo)Marshal.PtrToStructure(p_stats, typeof(NasInfo));

i am really baffled, the native functions does change the ptr value so it seems to do the allocation but the marshalling fails.

please helps thanks mosh.

[UPDATE RESOLVED]

public class NasInfo {
    public  EmmPlmnSelectMode plmnSelectMode;//enum
    public u32 pdnQty;
    public EpsPdnAddress pdnArray;
    public Bool_t emmRegistred;
    public Bool_t rrcConnected;
    public EpsIntegrityAlgorithm integrityAlgo;//enum
    public EpsCipheringAlgorithm cipheringAlgo;//enum
};

was changed to

    public class NasInfo {
    public  EmmPlmnSelectMode plmnSelectMode;//enum
    public u32 pdnQty;
    public /*EpsPdnAddress*/ IntPtr pdnArray;
    public Bool_t emmRegistred;
    public Bool_t rrcConnected;
    public EpsIntegrityAlgorithm integrityAlgo;//enum
    public EpsCipheringAlgorithm cipheringAlgo;//enum
};

and the marshelling works but i have lost the type safe, is there a way to wrap the IntPtr and still have some detail about the original struct/class (EpsPdnAddress in this case)


回答1:


Please check the size of the native and managed structs are matched



来源:https://stackoverflow.com/questions/19817883/accessviolationexception-when-marshal-ptrtostructure-fires

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!