【问题标题】:DllImport function pointer errorDllImport 函数指针错误
【发布时间】:2014-05-05 20:02:58
【问题描述】:

我有这样的 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);

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

【问题讨论】:

    标签: c# callback delegates pinvoke dllimport


    【解决方案1】:

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

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

    接下来是代表。 array 参数是一个指向字节的指针数组。它不是字节数组。并且结构必须由 ref 传递。也许这样声明。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-16
      • 2017-03-11
      • 1970-01-01
      • 2016-02-19
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多