0

正如主题所说,尝试将结构从 c# 环境传递给 c++。

定义结构和接口的 c++ 代码:

#pragma pack(push, 4)
    struct CEA708CONFIG 
    {
        BYTE                b608Service;            
        BYTE                bCompactStream;         
        BYTE                pActiveServices[63];    
        LONG                lActiveServiceCount;    //
        POINT               ptAlignmentPosition;    
    };
    #pragma pack(pop)


        interface
        __declspec(uuid("{some clsid}"))
        ICEA708Decoder : IUnknown {
            virtual HRESULT SetConfig(IN const CEA708CONFIG* pConfig) = 0;
            virtual HRESULT GetConfig(OUT CEA708CONFIG* pConfig) = 0;
        };

现在到 c# 代码,我在 c# 中定义了相同的结构

  [StructLayout(LayoutKind.Sequential, Pack = 4), Serializable]
    public struct CEA708CONFIG
    {
        public byte is608Service;
        public byte isCompactStream;
        //[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 63)]
        public IntPtr activeServices;
        public long activeServiceCount;
        public Point alignmentPosition;
    };

以及接受配置结构的相应接口

[ComVisible(true), ComImport, Guid("same clsid as above"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICEA708Decoder
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int SetConfig([In, MarshalAs(UnmanagedType.Struct)] ref CEA708CONFIG config);
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int GetConfig([Out, MarshalAs(UnmanagedType.Struct)] out CEA708CONFIG config);
    }

每当我尝试传递结构时,我的问题就会出现,我可以清楚地看到,在执行 c# 代码时,整个结构被初始化为“合理”的值,但是一旦传递给 c++,我看到在事务期间发生了一些事情。

使魔术发生的 c# 代码:

            CEA708CONFIG   cc708Config;
            ICEA708Decoder CC708DecoderConfig = CC708Filter as ICEA708Decoder;

            if (CC708DecoderConfig == null)
            {
                throw new ApplicationException("Couldn't get ICEA708Decoder structure");
            }

            byte[] dataByte = new byte[63];
            int    size     = Marshal.SizeOf(dataByte[0]) * dataByte.Length;
            IntPtr pnt      = Marshal.AllocHGlobal(size);

            dataByte[0] = 1;
            Marshal.Copy(dataByte, 0, pnt, dataByte.Length);
            cc708Config.activeServices = pnt;
            if (0 != (hr = CC708DecoderConfig.SetConfig(ref cc708Config)))
            {
                throw new ApplicationException("Couldn't SetConfig() because: " + DirectShowLib.DsError.GetErrorText(hr));
            }

SetConfig 触发的异常是:

{“无法编组‘CCReIndexer.Graphs.CEA708CONFIG’类型的字段‘activeServices’:托管/非托管类型组合无效(Int/UInt 必须与 SysInt 或 SysUInt 配对)。”:“”}

感谢您的帮助!!

4

1 回答 1

2

您是否尝试过将数组作为数组传输?

[StructLayout(LayoutKind.Sequential, Pack = 4), Serializable]
public struct CEA708CONFIG
{
    public byte is608Service;
    public byte isCompactStream;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 63)]
    public byte[] activeServices;
    public long activeServiceCount;
    public Point alignmentPosition;
};

byte[] dataByte = new byte[63];
cc708Config.activeServices = dataByte;
于 2012-08-06T12:32:14.180 回答