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

Passing parameters from c# to c++

本文关键字:c++ 参数      更新时间:2023-10-16

正如主题所说,试图将一个结构从c#environmentment传递到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成对出现)。":"}

谢谢你的帮助!!

您尝试过将数组转换为数组吗?

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