// MainProgram.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "IPlugin.h" //for the IPlugin abstract base
//convenience typedef for the pointers to the 2 functions we expect to find in the plugins
typedef IPlugin* (*PLUGIN_FACTORY)();
typedef void (*PLUGIN_CLEANUP)(IPlugin*);
int main(int argc, char* argv[])
{
//get the program's directory
char dir [MAX_PATH];
::GetModuleFileName (NULL, dir, MAX_PATH);
//eliminate the file name (to get just the directory)
char* p = ::strrchr (dir, '\\');
*(p + 1) = 0;
//find all DLLs in the plugins subdirectory
char search_parms [MAX_PATH];
::strcpy_s (search_parms, MAX_PATH, dir);
::strcat_s (search_parms, MAX_PATH, "plugins\\*.dll");
WIN32_FIND_DATA find_data;
HANDLE h_find = ::FindFirstFile (search_parms, &find_data);
BOOL f_ok = TRUE;
while (h_find != INVALID_HANDLE_VALUE && f_ok)
{
//load each DLL and determine whether it is exporting the functions we care about
char plugin_full_name [MAX_PATH];
::strcpy_s (plugin_full_name, MAX_PATH, dir);
::strcat_s (plugin_full_name, MAX_PATH, "plugins\\");
::strcat_s (plugin_full_name, MAX_PATH, find_data.cFileName);
HMODULE h_mod = ::LoadLibrary (plugin_full_name);
if (h_mod != NULL)
{
PLUGIN_FACTORY p_factory_function = (PLUGIN_FACTORY) ::GetProcAddress (h_mod, "Create_Plugin");
PLUGIN_CLEANUP p_cleanup_function = (PLUGIN_CLEANUP) ::GetProcAddress (h_mod, "Release_Plugin");
if (p_factory_function != NULL && p_cleanup_function != NULL)
{
//yes, this DLL exposes the 2 functions we need, it is a plugin we can use!
//invoke the factory to create the plugin object
IPlugin* p_plugin = (*p_factory_function) ();
//show which plugin it is, and let the plugin do the processing
printf ("Now working with plugin: %s\n", p_plugin ->Get_Name ());
p_plugin ->Process_Data ();
//done, cleanup the plugin by invoking its cleanup function
(*p_cleanup_function) (p_plugin);
}
::FreeLibrary (h_mod);
}
//go for the next DLL
f_ok = ::FindNextFile (h_find, &find_data);
}
return 0;
}
评论0
最新资源