About one implementation of the plug-in function in the software

xiaoxiao2021-03-06  45

Most commercial software support features extension. Compare the general method is to use plugins, more practical forms are to provide a DLL or similar file, which automatically detects these DLL files when loaded, and implements the extension, in Among the company's product development, I also used similar technologies, but I didn't have a reference to the online existing code, but I thought of a way, in principle, may be similar to this technology. (I didn't really study the method of the plugin implementation, huh, so I can't confirm that it is consistent)

The main principle is that commercial software automatically detects a directory clip in the current directory, such as an extra directory, enumerate all DLL files (if confidential, can change the DLL suffix to other names, and even Encrypt this file, then decrypt it in memory, but this is not within the scope of this article), and perform function call according to a predefined function name (note: It is important to provide all DLL files. The function interface name must be the same), I define this two function interface, one is the getInfo function, mainly returning this expansion module version number and some related information, the second is the DOWORK function, that is, the real need to be called by us. Work function, here, we can envisage, we can fully use the DOWORK function as an entry, implement all of our needs in this function, such as draw a dialog, or some other things we want, thereby commercial software The function will be infinitely extended by these extensions.

Terminology: Win32 DLL Compilation Interface Specification / WIN32 DLL Call Specification (Specific Specification Requirements, I can refer to Windows in-depth programming class)

Development Tools: Visual C 6.0 (SP5)

The following is listed below some of the code in the relevant implementation: ------------------------------------- -------------------- Extended module function definition module: (2 function definitions exported in the DLL can be called by other DLL / EXE files) Public: Bool getInfo (char * myinfo_data); // Returns the DLL version number and other information, the result will be saved to myinfo, allowing this char * to force this char * to a custom Structlong Dowork (Char * name, int ities, char * work_para); / / Actual working function

Commercial software calls DLL module section implementation: // Retrieves all extended DLL libraries Hinstance HDLL; // Function DLL handle char * myinfo_tmp; myinfo info; // I have custom A Structure reference Myinfo_TMP = (char *) Malloc SIZEOF (MyInfo); // Distribution of Memory

TypeDef Bool (Callback * lpfndllfunc1) (CHAR *); // Callback function declares lpfndllfunc1 getInfo; // function pointer lpfndllfunc1 DOWORK; / / function pointer

INT PST = 0; // Extended DLL function number count CString path, filename, othermsg; win32_find_data wfd; // MFC one Struct reference path.Format ("extra // *. dll"); handle handle = findfirstfile (); Handle Handle = FINDFIRSTFILE PATH, & WFD); // Open enumeration session / / The following is the code IF ("! = INVALID_HANDLE_VALUE) {DO {filename.format (" extra //% s "{DO {filename.format (" extra //% s "{filename.format (" extra //% s " Wfd.cfilename); HDLL = loadLibrary (filename); // Load the retrieved DLL file to memory if (HDLL! = null) {getInfo = (lpfndllfunc1) getProcaddress (HDLL, "getInfo"); // Get GetInfo The entry address IF (GetInfo) {getInfo (MyInfo_TMP); MYINFO_TMP); MYINFO, MYINFO_TMP, SIZEOF (INFO)); m_list.addstring (info.name); // Add NAME to the list control OtherMsg.Format "Manufacturer:% S / N / N file name:% S / T version number:% S / N / N]:% s", info.name, info.dllname, info.ter, fwinfo.othes, DOWORK = (Lpfndllfunc2) getProcAddress (HDLL, "DOWORK"); // get the entry address if the DOWORK function in the memory is (! "," Name ", 3," now ")) AFXMessageBox (" When calling the DOWORK function! " );} PST ;}} Freeelibrary (HDLL); // Release handle} While (FindNextFile (Handle, & WFD)); Findclose (Handle); // Close enumeration handle Free (MyInfo_TMP); // Release memory --- ----------------------------------- -------------------

The above is the realization code of the relevant call. If you have any questions, you can send an email on me, or leave a message, huh, you may have a more clever way to achieve extended features, I hope I can throw the jade, lead to a better implementation method.

Note: This article is original, written in 2001, to get a collection of articles in this blog, but turn out of the posted, laughing!

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

New Post(0)