DllImport函数指针错误

时间:2014-03-27 03:17:49

标签: c# callback delegates pinvoke dllimport

我有这样的C ++代码:

typedef void (CALLBACK * VideoCaptureCB_Ptr)(PVOID pContext, BYTE * apData[3],
VideoSampleInfo_T * pVSI);

typedef struct _VideoSampleInfo_T
{
    ULONG   idFormat; // 
    ULONG   lSignalState;
    int     nLen; // not used for raw video data(e.g. YUV420)
    int     nWidth;
    int     nHeight;
    int     anPitchs[3]; // only used for raw video data(e.g. YUV420)
    ULONG   dwMicrosecsPerFrame; // 1000*1000/FPS
    ULONG   field;
    int     iSerial;

} VideoSampleInfo_T;

WD_RegisterVideoPreviewCB(HANDLE hChannel, PVOID pContext, VideoCaptureCB_Ptr pCB);

我编写C#代码以在C#中使用WD_RegisterVideoPreviewCB()

public delegate void VideoCaptureCB_Ptr(IntPtr pContext, byte[] apData, VideoSampleInfo_T pVSI);
 public static void HandleVideoStatic(IntPtr pContext, byte[] apData, VideoSampleInfo_T pVSI)
        {

        }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct VideoSampleInfo_T
    {

        public uint idFormat;
        public uint lSignalState;
        public int nLen; // not used for raw video data(e.g. YUV420)
        public int nWidth;
        public int nHeight;
        public int[] anPitchs ; // only used for raw video data(e.g. YUV420)
        public uint dwMicrosecsPerFrame; // 1000*1000/FPS
        public uint field;
        public int iSerial;

    }
WD_RegisterVideoPreviewCB(m_ahChannels[i],   m_aMediaHandler[i], HandleVideoStatic);

在Irun我的C#代码之前,我在HandleVideoStatic()中放了一个debbugging点。当我运行我的代码。 VS 2013并没有停在调试点:(

1 个答案:

答案 0 :(得分:0)

您的翻译中存在一些错误。首先是结构。内联数组声明不正确。你需要这样:

public struct VideoSampleInfo_T
{
    public uint idFormat;
    public uint lSignalState;
    public int nLen; 
    public int nWidth;
    public int nHeight;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
    public int[] anPitchs;
    public uint dwMicrosecsPerFrame;
    public uint field;
    public int iSerial;
}

接下来的代表。数组参数是指向byte的指针数组。它不是字节数组。结构必须由ref传递。或许可以这样声明。

public delegate void VideoCaptureCB_Ptr(
    IntPtr pContext, 
    [MarshalAs(UnmanagedType.LPArray, SizeConst=3)]
    IntPtr[] apData, 
    ref VideoSampleInfo_T pVSI
);