0

我正在尝试使用来自 VS2012 C# env 的本机 dll。我有一个奇怪的场景: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));

使用以下 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
};

和编组工作,但我失去了类型安全,有没有办法包装 IntPtr 并且仍然有一些关于原始结构/类的细节(在这种情况下为 EpsPdnAddress)

4

1 回答 1

3

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

于 2013-11-07T13:08:14.263 回答