创建一个 DLL 项目并指定预处理器指令,例如 /DIFLOOR_EXPORTS_COMMONPLUGINBASE(此预处理器变量仅在 DLL 项目中)
然后创建一个标头,指定您的类是导入还是导出:
CommonPluing.h
#ifndef _COMMONPLUGIN_H
#define _COMMONPLUGIN_H
#if defined(__WXMSW__)
#ifdef IFLOOR_EXPORTS_COMMONPLUGINBASE
#define IFLOOR_API_COMMONPLUGINBASE __declspec(dllexport)
#else
#define IFLOOR_API_COMMONPLUGINBASE __declspec(dllimport)
#endif
#else
#define IFLOOR_API_COMMONPLUGINBASE
#endif
#endif // _COMMONPLUGIN_H
然后创建导出的类并从第一个标题添加说明符:
CommonConfigWindowBase.h
class IFLOOR_API_COMMONPLUGINBASE CommonConfigWindowBase : public wxPanel
{
DECLARE_DYNAMIC_CLASS(CommonConfigWindowBase)
public:
/// Constructors
CommonConfigWindowBase();
CommonConfigWindowBase(wxWindow *parent,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL | wxNO_BORDER,
const wxString& name = wxPanelNameStr);
/// Pseudo ctor
bool Create(wxWindow *parent,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL | wxNO_BORDER,
const wxString& name = wxPanelNameStr);
virtual ~CommonConfigWindowBase();
/// Reads config from the effect
virtual bool ReadConfig(){return true;}
/// Saves config to the effect
virtual bool SaveConfig(){return true;}
};
创建可从主可执行文件调用的导出函数(您可能希望创建一个包装类并调用返回 wxWindow * 的方法)。您需要一个导出的方法来创建插件对象和删除它。你还需要一个!!!虚拟析构函数!!!对于导出的对象和您的窗口。所以假设 SportEffectPlugin 包含一个 wxWindow * CreateConfigWindow(wxWindow * parent) 方法:
出口.cpp
#include "stdwx.h"
#include "CommonConfigWindowBase.h"
IFLOOR_API_COMMONPLUGINBASE IFloorEffectPluginBase * CreatePlugin(const wxString& sBasePath, iFloorBlobVector * blobs)
{
return new SportEffectPlugin(sBasePath, blobs);
}
IFLOOR_API_COMMONPLUGINBASE void DeletePlugin(IFloorEffectPluginBase * plugin)
{
wxDELETE(plugin);
}
然后在主应用程序中加载 DLL(您需要根据需要采用以下代码):
加载器.cpp
bool IFloorSystem::LoadPlugins(bool forceProgramPath)
{
if (!m_DefaultPlugin)
{
m_DefaultPlugin = new DefaultEffectPlugin(GetDefaultGraphicsPath());
RegisterEffectPlugin(m_DefaultPlugin);
}
wxFileName fn;
fn.AssignDir(GetPluginsPath(forceProgramPath));
wxLogDebug(wxT("%s"), fn.GetFullPath().data());
fn.AppendDir(wxT("effects"));
wxLogDebug(wxT("%s"), fn.GetFullPath().data());
if (!fn.DirExists())
return false;
wxDir dir(fn.GetFullPath());
if (!dir.IsOpened())
return false;
// scan for plugins
wxString filename;
wxString ext = wxT("*.dll"); // TODO: change ext for different platforms
bool bFound = dir.GetFirst(&filename, ext, wxDIR_FILES);
while (bFound)
{
fn.SetFullName(filename);
wxDynamicLibrary * dll = new wxDynamicLibrary(fn.GetFullPath());
if (dll->IsLoaded())
{
wxDYNLIB_FUNCTION(CreatePlugin_function, CreatePlugin, *dll);
if (pfnCreatePlugin)
{
IFloorEffectPluginBase* plugin = pfnCreatePlugin(GetDefaultGraphicsPath(), &IFloorStorage::Instance().GetBlobs());
RegisterEffectPlugin(plugin);
m_DllList.Append(dll);
m_MapPluginsDll[plugin] = dll;
}
else
wxDELETE(dll);
}
bFound = dir.GetNext(&filename);
}
return true;
}
最后,您需要通过调用 DLL 中的函数来卸载插件并删除所有加载的对象:
bool IFloorSystem::UnRegisterEffectPlugin(IFloorEffectPluginBase * plugin)
{
IFloorEffectPluginBaseList::compatibility_iterator it = m_Plugins.Find(plugin);
if (it == NULL)
return false;
do
{
wxDynamicLibrary * dll = m_MapPluginsDll[plugin];
if (!dll) // Probably plugin was not loaded from dll
break;
wxDYNLIB_FUNCTION(DeletePlugin_function, DeletePlugin, *dll);
if (pfnDeletePlugin)
{
pfnDeletePlugin(plugin);
m_Plugins.Erase(it);
m_MapPluginsDll.erase(plugin);
return true;
}
} while (false);
// If plugin is not loaded from DLL
wxDELETE(plugin);
m_Plugins.Erase(it);
return true;
}