RegisterStreamReadCallback时的Marshal错误

时间:2015-05-29 09:04:18

标签: c++ marshalling

我使用C#从dll文件调用方法来录制视频。

这是Marshal方法文件     http://pastebin.com/YrVvBfZ9

这是我的CameraUtilities文件     http://pastebin.com/0AZNtnhk

这是我的相机文件    http://pastebin.com/ZE3HD1zq

当我调用StartRecord方法(在相机文件中)开始录制视频时。

 public void StartRecord()
    {
        if (this.ListCameras != null)
        {
            bool bRes = true;
            try
            {
                int tmpError = -1;
                m_tmpContext = new IntPtr();
                m_handle = new GCHandle();
                m_callBackChannels = new ulong[m_ListCameras.Length];

                for (int i = 0; i < m_ListCameras.Length; i++)
                {
                    m_callBackChannels[i] = 10;
                    cameraCurrentIndex = 0;
                    IntPtr channelHandle = T1800.T18_ChannelOpen(cameraCurrentIndex);
                    m_ListCameras[cameraCurrentIndex].ChannelHandle = channelHandle;
                    T1800.T18_CaptureIFrame(m_ListCameras[i].ChannelHandle);



                    m_ListCameras[i].BeginRecord();
                    if (AllowRecordVideo)
                    {
                        m_del = new T1800.STREAM_READ_CALLBACK(StreamReadCallBack);
                        m_tmpContext = m_ListCameras[i].ChannelHandle;
                        tmpError = T1800.T18_RegisterStreamReadCallback(m_del, ref m_tmpContext);
                    }
                }




                if (tmpError == -1) bRes = false;
            }
            catch (Exception ex)
            {
                Log.Logger.Error(ex);
                bRes = false;
            }
        }
    }

抛出异常

- System.Runtime.InteropServices.MarshalDirectiveException: Invalid PInvoke calling convention.
 Thiscall requires that the first parameter is present and can be enregistered.
   at System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegateInternal(Delegate d)
   at System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(Delegate d)
   at TH.Parking.Wrapper.HBCamera.T1800.dll_T18_RegisterStreamReadCallback(STREAM_READ_CALLBACK STREAM_READ_CALLBACK, IntPtr& context)
   at TH.Parking.Wrapper.HBCamera.CameraUtilities.StartRecord() in d:\TH.Parking\TH.Parking\Wrapper\HBCamera\CameraUtilities.cs:line 93

我无法找到此错误的原因。有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为您传递给创建STREAM_READ_CALLBACK对象的回调应该是静态的。否则,在编组期间this指针会丢失。

而不是:

private int StreamReadCallBack(ulong channelHandle, IntPtr context)
{
   ...
}

尝试:

private static int StreamReadCallBack(ulong channelHandle, IntPtr context)
{
   ...
}

您需要以某种方式将CameraUtility的实例放入context参数中。