简体   繁体   中英

AccessViolationException when Marshal.PtrToStructure fires

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)

请检查本地结构和托管结构的大小是否匹配

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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