David Hoyle just blogged about Adding Menu Items to the IDE Editor’s Context Menu. As always his article is quite insightful, in particular the last part, because many people forget this:
That’s it, all done, or is it…
Well for a DLL yes as you would never get a situation where the Editor Popup Menu is called when you DLL is not in memory any more however this is not true for a BPL based plug-in so we need to reverse our hook.
… because a BPL based plugin can be loaded and unloaded at any time.
For which he then gives the following code:
Procedure TDDTWizard.UnhookEditorPopupMenu; Var EditorPopupMenu : TPopupActionBar; Begin EditorPopupMenu := FindEditorPopup; If Assigned(EditorPopupMenu) And Assigned(EditorPopupMenu.OnPopup) Then EditorPopupMenu.OnPopup := TNotifyEvent(FEditorPopupMethod); End;
And here we have a problem: How do you know that you can safely remove your hook? Some other plugin might have changed it and you will never know. If you simply change the OnPopup event back to the value you saved when you installed it, you might disable another plugin which was installed/initialized after yours. Or even worse, what if the plugin whose event you saved also has been unloaded? What do you do then? Simply reinstalling its original event handler will potenially crash the IDE the next time the event is called.
That’s exactly the reason why I wrote this article (years ago):
Safe event hooking for Delphi IDE plugins
I use this in my own plugins (Delphi IDE Explorer and parts of GExperts) and it works fine if both plugins adhere to this “standard”. Unfortunately I know of no other plugins who do. If you write Delphi IDE plugins, you might want to consider also tyring to play nice with other plugins.
(Btw David, your blog still says “Copyright David Hoyle 2018” in the footer.)