0
votes

I'm using C# to call methods from an dll file to record video.

This is the Marshal methods file http://pastebin.com/YrVvBfZ9

This is my CameraUtilities file http://pastebin.com/0AZNtnhk

This is my camera file http://pastebin.com/ZE3HD1zq

When I call StartRecord method (in camera file) to start Record video.

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

It throw an exception

- 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

I can't find any reason for this error. Can somebody help me to fix this.

1

1 Answers

0
votes

I think the callback you pass to create the STREAM_READ_CALLBACK object should be static. Otherwise, the thispointer is lost during the marshalling.

Instead of:

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

try:

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

And you'll need to somehow put your instance of CameraUtility into the context parameter.