Plug-in framework using DLLS

xiaoxiao2021-03-06  15

From: http://www.codeproject.com/dll/plugin.asp

DLLs are the most effective way to implement plug-in functionality. Even Windows itself uses DLLs extensively for most of its tasks. DLLs can be delivered easily as a patch or as an upgrade version, without worrying about re-compiling the main application again and again. Once installed / copied they can be loaded at runtime by the main application to use the functionality provided using exported functions / classes within the DLL. Now many of you would question, that how would the application know what all functions are to be called ? The answer to this would Be That,

all the DLLs must conform to some basic, generic, pre-defined interface. The call to the function (inside the DLL) can be issued only after knowing the function name. So the main app should, beforehand, know what all the functions that Can Be Called. So this is the reason why a generic / pre-determined interface shop be developed.

Use DLL to do a plugin is one of the most effective ways. The main program and plug-in must be communicated by the prior "agreement" interface.

SO Howings We go About Making One ??? The Basic Steps Are As Follows: Large process of loading plugins:

The main app is initialized (ie started) The DLLs / Plug-Ins are searched in the directory (may be pre-determined) Once "valid" DLLs are found, all or selected DLLs are loaded "Init" function is invoked on all loaded DLLsThe instance and the DLL name / path are cached for future use (method invocation) User uses the functionality of the DLLs (using GUI interfaces provided by the DLLs) Once done, the main application invokes "Destroy" function of the DLLs one by one To Free ResourceSthe Main Application EXITS

The validation of the DLL can be done in various ways. I used a method to extract the name of the DLL / plug-in by invoking an exportedGetPlugInName function. If this function exists and return a name then I consider it as a valid Plug- Users CAN Use Different Methods That Suite Their Needs.

How to verify the "legitimacy" of the DLL plug-in: You can try to call an implementation convention (such as getPluginName ()), if the execution is successful and returns a string, the DLL can be considered as legal.

Now, with all this done. The user should be able to use the plug-in at runtime by clicking on the toolbar buttons just added. By default the buttons are disabled, since the compiler can not find any "MESSAGE MAP" entries for it in the application. and also we can not add the message map entry beforehand as the "main" application doest not know even if the plug-ins existed or not beforehand, and also what resource IDs they use. After all, the main application has to be Designed to Be Generic and flexible. So this Must Be Done At Runtime (Enabling The Buttons and Handling Events on Theb).

Solving the issue of Message Map: Since the main program cannot know the resource ID usage in advance, the Message Map cannot be created with a conventional method. Solution is to overload the cmainframe :: oncmdmsg () method:

BOOL CMainFrame :: OnCmdMsg (UINT nID, int nCode, void * pExtra, AFX_CMDHANDLERINFO * pHandlerInfo) {// If pHandlerInfo is NULL, then handle the message if (pHandlerInfo == NULL) {// If Plug-Ins have been loaded if (m_bIsPlugInLoaded) {// Iterate through all loaded Plug-Ins for (int nNumDlls = 0; nNumDlls Enable (TRUE); return TRUE;} else // If it is CN_COMMAND Generated by clicking the button if (nCode == CN_COMMAND) {// Handle CN_COMMAND Message // Call OnCallPlugInManager (). To Redirect the Call // To The Plug-Ins Event Handler Method OncallPluginManager (NID, NCODE, PEXTRA, PHANDLERINFO); RETURN TRUE

}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }.Com So the DLL handles these messages in its own way.

The problem of DLL processing message is currently existing:

I Would Really Like To Handle DLL-Specific Events Wtem, Such AS

WM_LButtondown,

ON_COMMAND,

WM_MESSAGE, ETC. I Tried It Before, But The Message Handlers Are Never Called, May Be Due To some Silly Mistake I Had Made.

转载请注明原文地址:https://www.9cbs.com/read-46531.html

New Post(0)