static BOOL UnloadOneDLL(UnloadInfo *pui) { if (pui && pui->pidd->pUnloadIAT) { ImgDelayDescr * pidd = pui->pidd; HMODULE hmod = pidd->hmod; #if 0 { char Buff[128]; sprintf(Buff, "%08x: %s", UnloadOneDLL, pui->pidd->szName); MessageBox(NULL, Buff, "UnloadOneDLL", MB_OK); } #endif OverlayIAT(pidd->pIAT, pidd->pUnloadIAT); FreeLibrary(hmod); pidd->hmod = NULL; RemoveFromUnloadList(pui); return TRUE; } return FALSE; }
BOOL WINAPI __FUnloadDelayLoadedDLL2(LPCSTR szDll) { BOOL fRet = FALSE; PCImgDelayDescr pidd = PiddFromDllName(szDll); if ((pidd != NULL) && (pidd->rvaUnloadIAT != 0)) { HMODULE * phmod = PFromRva<HMODULE*>(pidd->rvaHmod); HMODULE hmod = *phmod; if (hmod != NULL) { DloadAcquireSectionWriteAccess(); OverlayIAT( PFromRva<PImgThunkData>(pidd->rvaIAT), PFromRva<PCImgThunkData>(pidd->rvaUnloadIAT) ); ::FreeLibrary(hmod); *phmod = NULL; DloadReleaseSectionWriteAccess(); fRet = TRUE; } } return fRet; }
FARPROC WINAPI _EXPFUNC __delayLoadHelper(ImgDelayDescr *pidd, FARPROC *ppfnIATEntry) { HMODULE hmod; unsigned iINT; FARPROC pfnRet; const IMAGE_THUNK_DATA * pitd; // Set up some data we use for the hook procs but also useful for // our own use // DelayLoadInfo dli; memset(&dli, 0, sizeof(dli)); dli.cb = sizeof (DelayLoadInfo); dli.pidd = pidd; dli.ppfn = ppfnIATEntry; dli.szDll = pidd->szName; hmod = pidd->hmod; // Calculate the index for the name in the import name table. // N.B. it is ordered the same as the IAT entries so the calculation // comes from the IAT side. // iINT = IndexFromPImgThunkData((const IMAGE_THUNK_DATA *)ppfnIATEntry, pidd->pIAT); pitd = &((pidd->pINT)[iINT]); if ((dli.dlp.fImportByName = ((pitd->u1.Ordinal & IMAGE_ORDINAL_FLAG) == 0)) != 0) { dli.dlp.szProcName = (LPCSTR)((IMAGE_IMPORT_BY_NAME*)pitd->u1.AddressOfData)->Name; } else { dli.dlp.dwOrdinal = IMAGE_ORDINAL(pitd->u1.Ordinal); } // Call the initial hook. If it exists and returns a function pointer, // abort the rest of the processing and just return it for the call. // pfnRet = NULL; if (__pfnDliNotifyHook) { if ((pfnRet = (*__pfnDliNotifyHook)(dliNoteStartProcessing, &dli)) != 0) { goto HookBypass; } } if (hmod == 0) { if (__pfnDliNotifyHook) { hmod = (HMODULE)(*__pfnDliNotifyHook)(dliNotePreLoadLibrary, &dli); } if (hmod == 0) { hmod = LoadLibrary(dli.szDll); } if (hmod == 0) { dli.dwLastError = GetLastError(); if (__pfnDliFailureHook) { // when the hook is called on LoadLibrary failure, it will // return 0 for failure and an hmod for the lib if it fixed // the problem. // hmod = (HMODULE)(*__pfnDliFailureHook)(dliFailLoadLibrary, &dli); } if (hmod == 0) { DelayLoadInfo * pdli = &dli; RaiseException( BCPPException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND), 0, 1, (PDWORD)&pdli ); // If we get to here, we blindly assume that the handler of the exception // has magically fixed everything up and left the function pointer in // dli.pfnCur. // return dli.pfnCur; } } // Store the library handle. If it is already there, we infer // that another thread got there first, and we need to do a // FreeLibrary() to reduce the refcount // EnterCriticalSection(&UnloadInfoCS); if (!pidd->hmod) { AddToUnloadList(pidd); pidd->hmod = hmod; } else { FreeLibrary(hmod); hmod = pidd->hmod; } LeaveCriticalSection(&UnloadInfoCS); } // Go for the procedure now. dli.hmodCur = hmod; if (__pfnDliNotifyHook) { pfnRet = (*__pfnDliNotifyHook)(dliNotePreGetProcAddress, &dli); } if (pfnRet == 0) { if (pidd->pBoundIAT && pidd->dwTimeStamp) { // bound imports exist...check the timestamp from the target image PIMAGE_NT_HEADERS pinh = PinhFromImageBase(hmod); if (pinh->Signature == IMAGE_NT_SIGNATURE && TimeStampOfImage(pinh) == pidd->dwTimeStamp && FLoadedAtPreferredAddress(pinh, hmod)) { OverlayIAT(pidd->pIAT, pidd->pBoundIAT); pfnRet = (FARPROC)pidd->pIAT[iINT].u1.Function; goto HookBypass; } } pfnRet = GetProcAddress(hmod, dli.dlp.szProcName); } if (pfnRet == 0) { dli.dwLastError = GetLastError(); if (__pfnDliFailureHook) { // when the hook is called on GetProcAddress failure, it will // return 0 on failure and a valid proc address on success // pfnRet = (*__pfnDliFailureHook)(dliFailGetProcAddress, &dli); } if (pfnRet == 0) { DelayLoadInfo * pdli = &dli; RaiseException( BCPPException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND), 0, 1, (PDWORD)&pdli ); // If we get to here, we blindly assume that the handler of the exception // has magically fixed everything up and left the function pointer in // dli.pfnCur. // pfnRet = dli.pfnCur; } } *ppfnIATEntry = pfnRet; HookBypass: if (__pfnDliNotifyHook) { dli.dwLastError = 0; dli.hmodCur = hmod; dli.pfnCur = pfnRet; (*__pfnDliNotifyHook)(dliNoteEndProcessing, &dli); } return pfnRet; }