将参数从c#传递给c ++

时间:2012-08-06 11:59:32

标签: c# c++ .net com-interop

正如主题所说,尝试将结构从c#environnement传递给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触发的异常是:

  

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

感谢您的帮助!!

1 个答案:

答案 0 :(得分: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;