一般在業務開發中,要分很多個模塊來進行協作,模塊多了之後就會有依賴問題,包括模塊的啓動順序,統一初始化,釋放資源等等,所以就需要一個來管理模塊的框架,插件框架。
外部接口:
插件管理啓動所有註冊插件,並初始化
//啓動框架
#define STARTCOMPONENTSERVICE StartComponent();
註冊模塊(dll), 並在dll中實現以下虛接口供插件管理調用
const std::wstring components[] = {
L"Update_d.dll"
};
#endif
struct IComponent
{
virtual bool Start() = 0;
virtual bool Stop() = 0;
};
extern "C" __declspec(dllexport) IComponent* GetComponent();
IComponent::Start用於插件內部的一些初始化
Stop用於插件卸載時的一些資源釋放
插件管理模塊啓動實現:
typedef IComponent*(*GetComponentFunc)();
void StartComponent()
{
for (auto x : components)
{
HMODULE hMod = ::LoadLibrary(x.c_str());
if (hMod)
{
GetComponentFunc pFunc = (GetComponentFunc)GetProcAddress(hMod, "GetComponent");
if (pFunc)
{
IComponent* pComponent = pFunc();
if (nullptr != pComponent)
{
pComponent->Start();
}
}
}
}
}