void *dllLoadLibrary(char *filename,char *portname) { int (*Entry)(void *, long, void *); void *hinst; bug("[DynLink] %s('%s','%s')\n", __PRETTY_FUNCTION__, filename, portname); hinst = dllInternalLoadLibrary(filename, portname, 1L); if (!hinst) return NULL; // Check for an entry point Entry = dllGetProcAddress(hinst, "DllEntryPoint"); if (Entry) { int ret = Entry(hinst, 0, NULL); if (ret) { // if we get non-null here, assume the initialisation worked return hinst; } else { // the entry point reported an error dllFreeLibrary(hinst); return NULL; } } return hinst; }
extern "C" BOOL __stdcall track_FreeLibrary(HINSTANCE hLibModule) { uintptr_t loc = (uintptr_t)_ReturnAddress(); tracker_library_free(loc, hLibModule); return dllFreeLibrary(hLibModule); }
extern "C" void tracker_library_free_all(DllTrackInfo* pInfo) { // unloading unloaded dll's if (!pInfo->dllList.empty()) { CSingleLock lock(g_trackerLock); CLog::Log(LOGDEBUG,"%s: Detected %"PRIdS" unloaded dll's", pInfo->pDll->GetFileName(), pInfo->dllList.size()); for (DllListIter it = pInfo->dllList.begin(); it != pInfo->dllList.end(); ++it) { LibraryLoader* pDll = DllLoaderContainer::GetModule((HMODULE)*it); if( !pDll) { CLog::Log(LOGERROR, "%s - Invalid module in tracker", __FUNCTION__); return; } if (!pDll->IsSystemDll()) { if (strlen(pDll->GetFileName()) > 0) CLog::Log(LOGDEBUG," : %s", pDll->GetFileName()); } } // now unload the dlls for (DllListIter it = pInfo->dllList.begin(); it != pInfo->dllList.end(); ++it) { LibraryLoader* pDll = DllLoaderContainer::GetModule((HMODULE)*it); if( !pDll) { CLog::Log(LOGERROR, "%s - Invalid module in tracker", __FUNCTION__); return; } if (!pDll->IsSystemDll()) { dllFreeLibrary((HMODULE)pDll->GetHModule()); } } } }
void Win32DllLoader::RestoreImports() { // first unhook any referenced dll's for (unsigned int i = 0; i < m_referencedDlls.size(); i++) { HMODULE module = m_referencedDlls[i]; dllFreeLibrary(module); // should unhook things for us } m_referencedDlls.clear(); for (unsigned int i = 0; i < m_overriddenImports.size(); i++) { Import &import = m_overriddenImports[i]; // change to protection settings so we can write to memory area DWORD old_prot = 0; VirtualProtect(import.table, 4, PAGE_EXECUTE_READWRITE, &old_prot); *(DWORD *)import.table = import.function; // reset to old settings VirtualProtect(import.table, 4, old_prot, &old_prot); } }