5,530
社区成员
发帖
与我相关
我的任务
分享
#ifndef PLUGIN_API
#define PLUGIN_API __declspec(dllexport)
#endif
extern "C" PLUGIN_API void RegisterPlugin(VPFactory& factory){
process... results;
factory.push_back(results);
}
extern "C" PLUGIN_API void UnReisterPlugin(VPFactory& factory){
factory.erase(results);
}
// 遍历指定文件夹下的dll插件。
void MainWindow::setupFactory()
{
//get the program's directory
wchar_t dir [MAX_PATH];
::GetModuleFileName (NULL, dir, MAX_PATH);
//eliminate the file name (to get just the directory)
wchar_t* p = ::wcsrchr (dir, '\\');
*(p + 1) = 0;
//find all DLLs in the plugins subdirectory
wchar_t search_parms [MAX_PATH];
::wcscpy_s (search_parms, MAX_PATH, dir);
::wcscat_s (search_parms, MAX_PATH, L"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
wchar_t plugin_full_name [MAX_PATH];
::wcscpy_s (plugin_full_name, MAX_PATH, dir);
::wcscat_s (plugin_full_name, MAX_PATH, L"plugins\\");
::wcscat_s (plugin_full_name, MAX_PATH, find_data.cFileName);
HMODULE h_mod = ::LoadLibrary(plugin_full_name);
if (h_mod != NULL)
{
// 保存插件列表
loadedPlugins.push_back(h_mod);
// 注册插件
PLUGIN_FUNC_PTR p_register_function =
(PLUGIN_FUNC_PTR) ::GetProcAddress (h_mod, "RegisterPlugin");
if (p_register_function != NULL)
{
// 将结果放到factory单件实例中。
(*p_register_function)( VPFactorySingleton::Instance() );
}
}
//go for the next DLL
f_ok = ::FindNextFile (h_find, &find_data);
}
}