How to implement a callback method within DLL (Delphi / TJVPluginManager + TJvPlugin)

早过忘川 提交于 2019-12-06 15:38:31

The basic concept is pretty simple. A callback method is a pointer to a method that you pass to some code so that it can call it at a specific time to allow you to customize its behavior. If you have any experience at all with Delphi, you're already familiar with callback methods under a different name: "event handlers".

Try something like this in your plugin:

type
   TMyEvent = procedure(param1, param2, param3: integer) of object;

procedure AddCallback(callback: TMyEvent);

This procedure would take the TMyEvent method pointer passed in and store it somewhere. Let's say in a variable called FCallback. When the time comes for it to call your app, the code would look like this:

if assigned(FCallback) then
   FCallback(param1, param2, param3);

You would call it from your app like this, when you're setting up the plugin:

MyPlugin.AddCallback(self.callbackProc);

Sometimes you'll need to put an @ in front of it (@self.callbackProc) so the compiler can tell that it's a method pointer and not a method call, but this is not always necessary.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!