+HMODULE
+WINAPI
+GetModuleHandleForUnicodeString(PUNICODE_STRING ModuleName)
+{
+ NTSTATUS Status;
+ PVOID Module;
+ LPWSTR DllPath;
+
+ /* Try to get a handle with a magic value of 1 for DllPath */
+ Status = LdrGetDllHandle((LPWSTR)1, NULL, ModuleName, &Module);
+
+ /* If that succeeded - we're done */
+ if (NT_SUCCESS(Status)) return Module;
+
+ /* If not, then the path should be computed */
+ DllPath = BasepGetDllPath(NULL, 0);
+
+ /* Call LdrGetHandle() again providing the computed DllPath
+ and wrapped into SEH */
+ _SEH2_TRY
+ {
+ Status = LdrGetDllHandle(DllPath, NULL, ModuleName, &Module);
+ }
+ _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+ {
+ /* Fail with the SEH error */
+ Status = _SEH2_GetExceptionCode();
+ }
+ _SEH2_END;
+
+ /* Free the DllPath */
+ RtlFreeHeap(RtlGetProcessHeap(), 0, DllPath);
+
+ /* In case of error set last win32 error and return NULL */
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT("Failure acquiring DLL module '%wZ' handle, Status 0x%08X\n", ModuleName, Status);
+ SetLastErrorByStatus(Status);
+ Module = 0;
+ }
+
+ /* Return module */
+ return (HMODULE)Module;
+}
+
+BOOLEAN
+WINAPI
+BasepGetModuleHandleExW(BOOLEAN NoLock, DWORD dwPublicFlags, LPCWSTR lpwModuleName, HMODULE *phModule)
+{
+ DWORD Cookie;
+ NTSTATUS Status = STATUS_SUCCESS, Status2;
+ HANDLE hModule = 0;
+ UNICODE_STRING ModuleNameU;
+ DWORD dwValid;
+ BOOLEAN Redirected = FALSE; // FIXME
+
+ /* Validate parameters */
+ dwValid = BasepGetModuleHandleExParameterValidation(dwPublicFlags, lpwModuleName, phModule);
+ ASSERT(dwValid == BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_CONTINUE);
+
+ /* Acquire lock if necessary */
+ if (!NoLock)
+ {
+ Status = LdrLockLoaderLock(0, NULL, &Cookie);
+ if (!NT_SUCCESS(Status))
+ {
+ /* Fail */
+ SetLastErrorByStatus(Status);
+ if (phModule) *phModule = 0;
+ return Status;
+ }
+ }
+
+ if (!(dwPublicFlags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS))
+ {
+ /* Create a unicode string out of module name */
+ RtlInitUnicodeString(&ModuleNameU, lpwModuleName);
+
+ // FIXME: Do some redirected DLL stuff?
+ if (Redirected)
+ {
+ UNIMPLEMENTED;
+ }
+
+ if (!hModule)
+ {
+ hModule = GetModuleHandleForUnicodeString(&ModuleNameU);
+ if (!hModule)
+ {
+ /* Last error is already set, so just return failure by setting status */
+ Status = STATUS_DLL_NOT_FOUND;
+ goto quickie;
+ }
+ }
+ }
+ else
+ {
+ /* Perform Pc to file header to get module instance */
+ hModule = (HMODULE)RtlPcToFileHeader((PVOID)lpwModuleName,
+ (PVOID*)&hModule);
+
+ /* Check if it succeeded */
+ if (!hModule)
+ {
+ /* Set "dll not found" status and quit */
+ Status = STATUS_DLL_NOT_FOUND;
+ goto quickie;
+ }
+ }
+
+ /* Check if changing reference is not forbidden */
+ if (!(dwPublicFlags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT))
+ {
+ /* Add reference to this DLL */
+ Status = LdrAddRefDll((dwPublicFlags & GET_MODULE_HANDLE_EX_FLAG_PIN) ? LDR_PIN_MODULE : 0,
+ hModule);
+ }
+
+ /* Set last error in case of failure */
+ if (!NT_SUCCESS(Status))
+ SetLastErrorByStatus(Status);
+
+quickie:
+ /* Unlock loader lock if it was acquired */
+ if (!NoLock)
+ {
+ Status2 = LdrUnlockLoaderLock(0, Cookie);
+ ASSERT(NT_SUCCESS(Status2));
+ }
+
+ /* Set the module handle to the caller */
+ if (phModule) *phModule = hModule;
+
+ /* Return TRUE on success and FALSE otherwise */
+ return NT_SUCCESS(Status);
+}