c# 调用c++ sdk 传结构体指针 5C

本人菜鸟一枚
SDK函数 LIBBVCU_API BVCU_Result BVCU_Initialize( const BVCU_GlobalParam* pParam);

其中 BVCU_GlobalParam为结构体
typedef struct _BVCU_GlobalParam
{

int iSize;
void (*OnEvent)(int iEventCode, void* pParam);
int iReserved[4];
}BVCU_GlobalParam;

问题我是该直接传递一个指针,还是传一个BVCU_GlobalParam类型参数
public IntPtr m_bvcuSdkHandle;
BVCU_Initialize(ref Im_bvcuSdkHandle)(指针)
还是 BVCU_Initialize(ref _BVCU_GlobalParam bg) (结构体)

 [DllImport("libBVCU.dll", CallingConvention = CallingConvention.Cdecl)]
 public static extern int BVCU_Initialize(ref IntPtr handle);

[StructLayout(LayoutKind.Sequential)]
    public struct BVCU_GlobalParam
    {
        public int iSize;
        public CallbackDelegate OnEvent;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public int[] iReserved;

    };

这两种都试了,接口返回还是参数不符合,请大神上码指教

3个回答

看起来好像没有错,在结构体委托上加个特性试试 [MarshalAs(UnmanagedType.FunctionPtr)]
[StructLayout(LayoutKind.Sequential)]
public struct BVCU_GlobalParam
{
public int iSize;
[MarshalAs(UnmanagedType.FunctionPtr)]
public CallbackDelegate OnEvent;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public int[] iReserved;

};

返回值用IntPtr接收,然后再转回结构体

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐