/* * @implemented */ HINSTANCE WINAPI LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) { UNICODE_STRING DllName; HINSTANCE hInst; NTSTATUS Status; PWSTR SearchPath; ULONG DllCharacteristics = 0; BOOL FreeString = FALSE; /* Check for any flags LdrLoadDll might be interested in */ if (dwFlags & DONT_RESOLVE_DLL_REFERENCES) { /* Tell LDR to treat it as an EXE */ DllCharacteristics = IMAGE_FILE_EXECUTABLE_IMAGE; } /* Build up a unicode dll name from null-terminated string */ RtlInitUnicodeString(&DllName, (LPWSTR)lpLibFileName); /* Lazy-initialize BasepExeLdrEntry */ if (!BasepExeLdrEntry) LdrEnumerateLoadedModules(0, BasepLocateExeLdrEntry, NtCurrentPeb()->ImageBaseAddress); /* Check if that module is our exe*/ if (BasepExeLdrEntry && !(dwFlags & LOAD_LIBRARY_AS_DATAFILE) && DllName.Length == BasepExeLdrEntry->FullDllName.Length) { /* Lengths match and it's not a datafile, so perform name comparison */ if (RtlEqualUnicodeString(&DllName, &BasepExeLdrEntry->FullDllName, TRUE)) { /* That's us! */ return BasepExeLdrEntry->DllBase; } } /* Check for trailing spaces and remove them if necessary */ if (DllName.Buffer[DllName.Length/sizeof(WCHAR) - 1] == L' ') { RtlCreateUnicodeString(&DllName, (LPWSTR)lpLibFileName); while (DllName.Length > sizeof(WCHAR) && DllName.Buffer[DllName.Length/sizeof(WCHAR) - 1] == L' ') { DllName.Length -= sizeof(WCHAR); } DllName.Buffer[DllName.Length/sizeof(WCHAR)] = UNICODE_NULL; FreeString = TRUE; } /* Compute the load path */ SearchPath = BaseComputeProcessDllPath((dwFlags & LOAD_WITH_ALTERED_SEARCH_PATH) ? DllName.Buffer : NULL, NULL); if (!SearchPath) { /* Getting DLL path failed, so set last error, free mem and return */ BaseSetLastNTError(STATUS_NO_MEMORY); if (FreeString) RtlFreeUnicodeString(&DllName); return NULL; } _SEH2_TRY { if (dwFlags & LOAD_LIBRARY_AS_DATAFILE) { /* If the image is loaded as a datafile, try to get its handle */ Status = LdrGetDllHandleEx(0, SearchPath, NULL, &DllName, (PVOID*)&hInst); if (!NT_SUCCESS(Status)) { /* It's not loaded yet - so load it up */ Status = BasepLoadLibraryAsDatafile(SearchPath, DllName.Buffer, &hInst); } _SEH2_YIELD(goto done;) } /* Call the API Properly */ Status = LdrLoadDll(SearchPath, &DllCharacteristics, &DllName, (PVOID*)&hInst); }
int main(int argc, char** argv) { LdrEnumerateLoadedModules(FALSE, ModuleEnumCallback, NULL); return 0; }