触发Marshal.PtrToStructure时的AccessViolationException

时间:2013-11-06 16:49:44

标签: c# dll marshalling native

我正在尝试使用VS2012 C#env中的本机dll。 我有一个奇怪的场景: 情形1:

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

使用以下Pinvoke包装器:

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

这是班级:

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

这种情况很好 案例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));

使用以下Pinvoke包装器:

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

这是班级:

        [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];

}

这种情况在行中抛出AccessViolationException:

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

我真的很困惑,原生函数确实改变了ptr值,所以它似乎做了分配但是编组失败了。

请帮助 谢谢 MOSH。

[最新决议]

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

已更改为

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

和marshelling工作 但我丢失了类型安全,有没有办法包装IntPtr并仍然有一些关于原始struct / class的详细信息(在这种情况下为EpsPdnAddress)

1 个答案:

答案 0 :(得分:3)

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