From c307f4990fe89b4a958efcc9107bf25aef6c01d0 Mon Sep 17 00:00:00 2001
From: Alexandre Julliard <julliard@winehq.org>
Date: Tue, 21 Mar 2006 15:10:22 +0100
Subject: [PATCH] ntdll: Added a DLL_WINE_PREATTACH call to DllMain for
 builtins.

By returning FALSE from this call, a builtin dll can specify that we
should try to load the native one instead.
---
 dlls/ntdll/loader.c | 16 +++++++++++++---
 include/winnt.h     |  4 +++-
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
index 967c2989c9..88878c43a4 100644
--- a/dlls/ntdll/loader.c
+++ b/dlls/ntdll/loader.c
@@ -58,7 +58,9 @@ static const char * const reason_names[] =
     "PROCESS_DETACH",
     "PROCESS_ATTACH",
     "THREAD_ATTACH",
-    "THREAD_DETACH"
+    "THREAD_DETACH",
+    NULL, NULL, NULL, NULL,
+    "WINE_PREATTACH"
 };
 
 static const WCHAR dllW[] = {'.','d','l','l',0};
@@ -1749,10 +1751,18 @@ static NTSTATUS load_dll( LPCWSTR load_path, LPCWSTR libname, DWORD flags, WINE_
     case LO_BUILTIN_NATIVE:
     case LO_DEFAULT:  /* default is builtin,native */
         nts = load_builtin_dll( load_path, filename, handle, flags, pwm );
-        if (nts == STATUS_SUCCESS) break;
         if (!handle) break;  /* nothing else we can try */
         /* file is not a builtin library, try without using the specified file */
-        nts = load_builtin_dll( load_path, filename, 0, flags, pwm );
+        if (nts != STATUS_SUCCESS)
+            nts = load_builtin_dll( load_path, filename, 0, flags, pwm );
+        if (nts == STATUS_SUCCESS && loadorder == LO_DEFAULT &&
+            !MODULE_InitDLL( *pwm, DLL_WINE_PREATTACH, NULL ))
+        {
+            /* stub-only dll, try native */
+            TRACE( "%s pre-attach returned FALSE, preferring native\n", debugstr_w(filename) );
+            LdrUnloadDll( (*pwm)->ldr.BaseAddress );
+            nts = STATUS_DLL_NOT_FOUND;
+        }
         if (nts == STATUS_DLL_NOT_FOUND && loadorder != LO_BUILTIN)
             nts = load_native_dll( load_path, filename, handle, flags, pwm );
         break;
diff --git a/include/winnt.h b/include/winnt.h
index 199383bb5a..053f01445a 100644
--- a/include/winnt.h
+++ b/include/winnt.h
@@ -469,7 +469,9 @@ typedef DWORD FLONG;
 #define	DLL_PROCESS_ATTACH	1	/* attach process (load library) */
 #define	DLL_THREAD_ATTACH	2	/* attach new thread */
 #define	DLL_THREAD_DETACH	3	/* detach thread */
-
+#ifdef __WINESRC__
+#define DLL_WINE_PREATTACH      8       /* called before process attach for Wine builtins */
+#endif
 
 /* u.x.wProcessorArchitecture (NT) */
 #define PROCESSOR_ARCHITECTURE_INTEL	0
-- 
2.32.0.93.g670b81a890