LWRESULT Cx_PluginLoader::UnloadPlugin(const MODID& modid, bool force_unload) { int32_ moduleIndex = GetPluginIndex(modid); if(moduleIndex < 0) return LWDP_PLUGINMGR_NOT_FIND_MODULE; Ix_Module* pModule = m_modules[moduleIndex]->module; ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_POINTER_IS_NULL, pModule, "Cx_PluginLoader::UnloadPlugin Get Ix_Module Pointer Error"); if(!force_unload) { if(!pModule->CanUnloadPlugin()) return LWDP_PLUGINMGR_MODULE_USING; } else { lw_log_warning(LWDP_MODULE_LOG, "Force Unload Plugin(%s)", pModule->GetModuleName()); } pModule->UninitializePlugin(); MODID tmpModid = pModule->GetModuleMODID(); ClearModuleItems(tmpModid); ReleaseModule(tmpModid); return LWDP_OK; }
bool Cx_PluginLoader::UnloadPlugin(const wchar_t* name) { CLockCount locker(&m_unloading); int moduleIndex = GetPluginIndex(name); HMODULE hdll = moduleIndex < 0 ? NULL : m_modules[moduleIndex]->hdll; if (NULL == hdll) { return false; } typedef bool (*FUNC_CANUNLOAD)(); FUNC_CANUNLOAD pfnCan = (FUNC_CANUNLOAD)GetProcAddress( hdll, "x3CanUnloadPlugin"); if (pfnCan && !pfnCan()) { return false; } typedef void (*FUNC_UNLOAD)(); FUNC_UNLOAD pfnUnload = (FUNC_UNLOAD)GetProcAddress( hdll, "x3UninitializePlugin"); if (pfnUnload) { pfnUnload(); } VERIFY(ClearModuleItems(hdll)); ReleaseModule(hdll); return true; }
LWRESULT Cx_PluginLoader::RegisterPlugin(const Ix_Module* pModule) { ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_PARAMETER_ERROR, (pModule), "RegisterPlugin Para Error"); if (FindModule(pModule->GetModuleMODID()) >= 0) { return false; } MODULE moduleInfo; moduleInfo.modid = pModule->GetModuleMODID(); moduleInfo.module = (Ix_Module *)pModule; moduleInfo.owned = (pModule->GetModuleInstance())? true: false; moduleInfo.inited = false; int32_ moduleIndex = GetPluginIndex(pModule->GetModuleMODID()); if (moduleIndex >= 0) { ASSERT_CHECK_RET(LWDP_MODULE_LOG, false, (m_modules[moduleIndex] != NULL), "Get Plugin Index Error"); *m_modules[moduleIndex] = moduleInfo; } else { moduleIndex = m_modules.size(); MODULE* module = new MODULE; *module = moduleInfo; m_modules.push_back(module); } RegisterClassEntryTable(moduleIndex); return LWDP_OK; }
LWRESULT Cx_PluginLoader::RegisterFrameWork(Ix_Module* pModule) { ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_PARAMETER_ERROR, (pModule), "Cx_PluginLoader::RegisterFrameWork Para Error"); int32_ existIndex = GetPluginIndex(pModule->GetModuleMODID()); if (existIndex >= 0 && m_modules[existIndex]->modid.empty()) { return LWDP_PLUGINMGR_MODULE_ALREADY_LOAD; } LWRESULT stat = LWDP_ERROR; if (pModule) { pModule->Initialize(this, 0); if(RegisterPlugin(pModule) == LWDP_OK) { int32_ moduleIndex = FindModule(pModule->GetModuleMODID()); ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_PLUGINMGR_MODULE_INDEX_ERROR, (moduleIndex >= 0), "Module Index Error"); ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_PLUGINMGR_MODULE_INDEX_ERROR, (existIndex < 0 || existIndex == moduleIndex), "Module Index Error"); } } else { return LWDP_PLUGINMGR_LOADMODULE_ERROR; } return LWDP_OK; }
bool Cx_PluginLoader::LoadPlugin(const wchar_t* filename) { if (!x3InMainThread()) { X3LOG_WARNING2(L"Can't load plugin in sub thread.", filename); return false; } CLockCount locker(&m_loading); int existIndex = GetPluginIndex(filename); if (existIndex >= 0 && m_modules[existIndex]->hdll) { if (_wcsicmp(filename, m_modules[existIndex]->filename) != 0) { X3LOG_DEBUG2(L"The plugin is already loaded.", filename << L", " << (m_modules[existIndex]->filename)); } return false; } HMODULE hdll = x3LoadLibrary(filename); DWORD errcode = GetLastError(); if (hdll) { if (RegisterPlugin(hdll)) { int moduleIndex = FindModule(hdll); ASSERT(moduleIndex >= 0); ASSERT(existIndex < 0 || existIndex == moduleIndex); m_modules[moduleIndex]->owned = true; #ifdef _WIN32 DisableThreadLibraryCalls(hdll); #endif } else { FreeLibrary(hdll); hdll = NULL; } } else if (PathFileExistsW(filename)) { X3LOG_WARNING2(L"Fail to load plugin.", x3::GetSystemErrorString(errcode) << L", " << filename); } return hdll != NULL; }
LWRESULT Cx_PluginLoader::LoadPlugin(const MODID& modid) { int32_ existIndex = GetPluginIndex(modid); if (existIndex >= 0 && m_modules[existIndex]->modid.empty()) { return LWDP_PLUGINMGR_MODULE_ALREADY_LOAD; } Cx_Interface<Ix_FwIntf> iFwIntf(CLSID_FwIntf); ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_OBJECT_POINTER_IS_NULL, !iFwIntf.IsNull(), "Cx_PluginLoader::LoadPlugin Get Object(FwIntf) Error"); Ix_Module* pModule = iFwIntf->Api_Fw_GetModulePtr(modid); if(!pModule) { return LWDP_PLUGINMGR_NOT_FIND_MODULE_ENTRY; } LWRESULT stat = LWDP_ERROR; if (pModule) { if((stat = CheckPlugin(pModule)) != LWDP_OK) { if(pModule->GetModuleInstance()) //!< 只有加载的外部模块才Unload { Cx_Interface<Ix_FwIntf> iFwIntf(CLSID_FwIntf); ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_OBJECT_POINTER_IS_NULL, !iFwIntf.IsNull(), "Cx_PluginLoader::LoadPlugin Get Object(FwIntf) Error"); iFwIntf->Api_Fw_UnLoadModule(pModule->GetModuleMODID()); } return stat; } pModule->Initialize(this, 0); if(RegisterPlugin(pModule) == LWDP_OK) { int32_ moduleIndex = FindModule(pModule->GetModuleMODID()); ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_PLUGINMGR_MODULE_INDEX_ERROR, (moduleIndex >= 0), "Module Index Error"); ASSERT_CHECK_RET(LWDP_MODULE_LOG, LWDP_PLUGINMGR_MODULE_INDEX_ERROR, (existIndex < 0 || existIndex == moduleIndex), "Module Index Error"); } } else { return LWDP_PLUGINMGR_LOADMODULE_ERROR; } return LWDP_OK; }
bool Cx_PluginLoader::RegisterPlugin(HMODULE instance) { if (FindModule(instance) >= 0) { return false; } Ix_Module* pModule = GetModule(instance); if (pModule != NULL) { MODULE moduleInfo; moduleInfo.hdll = instance; moduleInfo.module = pModule; moduleInfo.owned = false; moduleInfo.inited = false; GetModuleFileNameW(moduleInfo.hdll, moduleInfo.filename, MAX_PATH); int moduleIndex = GetPluginIndex(moduleInfo.filename); if (moduleIndex >= 0) { ASSERT(m_modules[moduleIndex] != NULL); *m_modules[moduleIndex] = moduleInfo; } else { moduleIndex = x3::GetSize(m_modules); MODULE* module = new MODULE; *module = moduleInfo; m_modules.push_back(module); } RegisterClassEntryTable(moduleIndex); return true; } return false; }