我被要求在我的项目中整合网络摄像头ZoneTrigger。该站点提供的SDK是C++,也是示例。我已经能够使用一些功能。我被卡住的地方是回调函数。
示例C++中的代码回调函数

ZT_SetCallbackProc(ZoneTriggerCallbackProc);

//在头文件中
typedef int (WINAPI *f_ZT_SetCallbackProc) (void *proc);
/*
Sets up the callback function for your application.
The proc should be declared like this:

int WINAPI ZoneTriggerCallbackProc(int MessageType, ZT_TRIG_STRUCT *trigInfo);

MessageType may be one of the following:
0:  Zone Trigger sends a trig. The trigInfo contains data about the hot spots that generated the trig.
1:  Zone Trigger has started and is notifying us that it is ready. This only occurs when Zone Trigger starts after the interface DLL is loaded.
2:  Zone Trigger is shutting down. You application may need to know this. If Zone Trigger is started again, your application will get message 1.
3:  Zone Trigger's Hot spot scheme has changed (a Hot Spot was added or deleted)
*/

我的C#代码:
[DllImport("ZTcom.dll")]
    private static extern IntPtr ZT_SetCallbackProc(int ZoneTriggerCallbackProc);

 private unsafe int ZoneTriggerCallbackProc(int MessageType, ref ZT_TRIG_STRUCT trigInfo)
    {
        switch (MessageType)
        {

            case 0:     //Trig from a Zone Trigger hot spot
                // string s1 = new string(trigInfo.SpotName);
                MessageBox.Show("Got a trig from spot" + trigInfo.SpotIndex.ToString()+ s1 );
                break;

            case 1:     //Zone Trigger has started and is notifying us that it is ready
                MessageBox.Show("Zone Trigger issued a ready notification.\r\n");
                break;

            case 2:     //Zone Trigger is shutting down
                MessageBox.Show("Zone Trigger has left the building.\r\n");
                break;

            case 3:     //Hot spot scheme updated, you might want yo re-enumerate the hot spots
                MessageBox.Show("Zone Trigger's hot spots have been updated.\r\n");
                break;


        }
        return 0;
    }

我到现在为止...但是我不明白如何调用ZT_SetCallbackProc函数?
 IntPtr tg = IntPtr.Zero;
            tg = ZT_SetCallbackProc(ZoneTriggerCallbackProc);

这给出了ZoneTriggerCallbackProc是方法组的错误。请帮助...预先感谢。

最佳答案

丹尼尔(Daniel)的answer具有简单快捷的路线。如果您不希望遇到问题(由于GC可能会收集委托(delegate)而导致AccessViolationException),则需要执行以下操作。

Daniel的类型/个性声明(出于完整性考虑):

// int WINAPI ZoneTriggerCallbackProc(int MessageType, ZT_TRIG_STRUCT *trigInfo);
delegate int ZoneTriggerCallbackProc(int messageType, ref ZT_TRIG_STRUCT trigInfo);
[DllImport("ZTcom.dll")]
private static extern IntPtr ZT_SetCallbackProc(ZoneTriggerCallbackProc callbackProc);

在包装器类中,保存对委托(delegate)的引用,以便在设置回调时GC不会收集委托(delegate)。
public class ZoneTrigger : CriticalFinalizerObject
{
  private ZoneTriggerCallbackProc _zoneTriggerCallback;
  private IntPtr _zoneTriggerCallbackCookie;

  public ZoneTrigger()
  {
    _zoneTriggerCallback = ZoneTriggerCallback;
    // Why not just do it here?
    _zoneTriggerCallbackCookie = NativeMethods.ZT_SetCallbackProc(_zoneTriggerCallback);
    if (_zoneTriggerCallbackCookie == IntPtr.Zero)
       throw new Exception("Failed to set callback");
  }

   private unsafe int ZoneTriggerCallback(int MessageType, ref ZT_TRIG_STRUCT trigInfo)
   {
     // ...
   }

  ~ZoneTrigger()
  {
     var oldCookie = Interlocked.Exchange(ref _zoneTriggerCallback, IntPtr.Zero);
     if (oldCookie != IntPtr.Zero)
       ZT_ClearCallbackProc(oldCookie);
  }
}

注意:请接受Daniel的回答,这是对它的补充。

08-16 01:10