Added LOAD_WITH_ALTERED_SEARCH_PATH to LoadLibraryEx (including b uiltins)

Medland, Bill Bill.Medland at accpac.com
Wed Aug 22 17:28:00 CDT 2001


 <<diff15.txt>> 
-------------- next part --------------
Bill Medland (medbi01 at accpac.com)
Added "LOAD_WITH_ALTERED_SEARCH_PATH" support to LoadLibraryEx.

Index: wine/files/directory.c
===================================================================
RCS file: /home/wine/wine/files/directory.c,v
retrieving revision 1.40
diff -u -r1.40 directory.c
--- wine/files/directory.c	2001/07/02 19:59:48	1.40
+++ wine/files/directory.c	2001/08/22 21:20:25
@@ -582,8 +582,7 @@
     p = strrchr( name, '.' );
     if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
         ext = NULL;  /* Ignore the specified extension */
-    if ((*name && (name[1] == ':')) ||
-        strchr( name, '/' ) || strchr( name, '\\' ))
+    if (FILE_contains_path (name))
         path = NULL;  /* Ignore path if name already contains a path */
     if (path && !*path) path = NULL;  /* Ignore empty path */
 
@@ -604,47 +603,43 @@
 
     /* If the name contains an explicit path, everything's easy */
 
-    if ((*name && (name[1] == ':')) || strchr( name, '/' ) || strchr( name, '\\' ))
-    {
+    if (FILE_contains_path (name))
         ret = DOSFS_GetFullName( name, TRUE, full_name );
-        goto done;
-    }
 
     /* Search in the specified path */
 
-    if (path)
-    {
+    else if (path)
         ret = DIR_TryEnvironmentPath( name, full_name, path );
-        goto done;
-    }
 
     /* Try the path of the current executable (for Win32 search order) */
 
-    if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
+    else if (win32 && DIR_TryModulePath( name, full_name, win32 ))
+        ;
 
     /* Try the current directory */
 
-    if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
+    else if (DOSFS_GetFullName( name, TRUE, full_name ))
+        ;
 
     /* Try the Windows system directory */
 
-    if (DIR_TryPath( &DIR_System, name, full_name ))
-        goto done;
+    else if (DIR_TryPath( &DIR_System, name, full_name ))
+        ;
 
     /* Try the Windows directory */
 
-    if (DIR_TryPath( &DIR_Windows, name, full_name ))
-        goto done;
+    else if (DIR_TryPath( &DIR_Windows, name, full_name ))
+        ;
 
     /* Try the path of the current executable (for Win16 search order) */
 
-    if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
+    else if (!win32 && DIR_TryModulePath( name, full_name, win32 ))
+        ;
 
     /* Try all directories in path */
+    else 
+        ret = DIR_TryEnvironmentPath( name, full_name, NULL );
 
-    ret = DIR_TryEnvironmentPath( name, full_name, NULL );
-
-done:
     if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
     return ret;
 }
@@ -746,3 +741,103 @@
 }
 
 
+/***********************************************************************
+ *           DIR_SearchAlternatePath
+ *
+ *
+ * FIXME: should return long path names.?
+ */
+static DWORD DIR_SearchAlternatePath (LPCSTR dll_path, LPCSTR name, LPCSTR ext,
+                      DOS_FULL_NAME *full_name)
+{
+    LPCSTR p;
+    LPSTR tmp = NULL;
+    BOOL ret = TRUE;
+
+    /* First check the supplied parameters */
+
+    p = strrchr( name, '.' );
+    if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
+        ext = NULL;  /* Ignore the specified extension */
+
+    /* Allocate a buffer for the file name and extension */
+
+    if (ext)
+    {
+        DWORD len = strlen(name) + strlen(ext);
+        if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
+        {
+            SetLastError( ERROR_OUTOFMEMORY );
+            return 0;
+        }
+        strcpy( tmp, name );
+        strcat( tmp, ext );
+        name = tmp;
+    }
+
+    if (DIR_TryEnvironmentPath (name, full_name, dll_path))
+        ;
+    else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
+        ;
+    else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
+        ;
+    else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
+        ;
+    else
+        ret = DIR_TryEnvironmentPath( name, full_name, NULL );
+
+    if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
+    return ret;
+}
+
+/***********************************************************************
+ * SearchAlternatePathA 		internal
+ *
+ * Searches for a specified file in the search path.
+ *
+ * PARAMS
+ *    dll_path	[I] Path to search
+ *    name	[I] Filename to search for.
+ *    ext	[I] File extension to append to file name. The first
+ *		    character must be a period. This parameter is
+ *                  specified only if the filename given does not
+ *                  contain an extension.
+ *    buflen	[I] size of buffer, in characters
+ *    buffer	[O] buffer for found filename
+ *    lastpart  [O] address of pointer to last used character in
+ *                  buffer (the final '\') (May be NULL)
+ *
+ * RETURNS
+ *    Success: length of string copied into buffer, not including
+ *             terminating null character. If the filename found is
+ *             longer than the length of the buffer, the length of the
+ *             filename is returned.
+ *    Failure: Zero
+ * 
+ * NOTES
+ *    If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
+ */
+DWORD SearchAlternatePathA( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
+                            DWORD buflen, LPSTR buffer, LPSTR *lastpart )
+{
+    LPSTR p, res;
+    DOS_FULL_NAME full_name;
+
+    if (!DIR_SearchAlternatePath( dll_path, name, ext, &full_name))
+    {
+	SetLastError(ERROR_FILE_NOT_FOUND);
+	return 0;
+    }
+    lstrcpynA( buffer, full_name.short_name, buflen );
+    res = full_name.long_name +
+              strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
+    while (*res == '/') res++;
+    if (buflen)
+    {
+        if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
+        for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
+        if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
+    }
+    TRACE("Returning %d\n", strlen(res) + 3 );
+    return strlen(res) + 3;
+}
Index: wine/include/file.h
===================================================================
RCS file: /home/wine/wine/include/file.h,v
retrieving revision 1.28
diff -u -r1.28 file.h
--- wine/include/file.h	2001/07/12 22:29:42	1.28
+++ wine/include/file.h	2001/08/22 21:20:25
@@ -61,6 +61,12 @@
     return c;
 }
 
+inline static int FILE_contains_path (LPCSTR name)
+{
+    return ((*name && (name[1] == ':')) ||
+            strchr (name, '/') || strchr (name, '\\'));
+}
+
 /* files/file.c */
 extern int FILE_strcasecmp( const char *str1, const char *str2 );
 extern int FILE_strncasecmp( const char *str1, const char *str2, int len );
@@ -80,6 +86,8 @@
 extern int DIR_Init(void);
 extern UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count );
 extern UINT DIR_GetSystemUnixDir( LPSTR path, UINT count );
+extern DWORD SearchAlternatePathA( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
+                                   DWORD buflen, LPSTR buffer, LPSTR *lastpart);
 extern DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
                              DOS_FULL_NAME *full_name, BOOL win32 );
 
Index: wine/include/module.h
===================================================================
RCS file: /home/wine/wine/include/module.h,v
retrieving revision 1.59
diff -u -r1.59 module.h
--- wine/include/module.h	2001/07/24 21:45:24	1.59
+++ wine/include/module.h	2001/08/22 21:20:25
@@ -234,6 +234,7 @@
                                      DWORD flags, HANDLE hFile, BOOL builtin );
 extern void PE_InitTls(void);
 extern BOOL PE_InitDLL( HMODULE module, DWORD type, LPVOID lpReserved );
+extern DWORD PE_fixup_imports(WINE_MODREF *wm);
 
 /* loader/loadorder.c */
 extern void MODULE_InitLoadOrder(void);
Index: wine/loader/module.c
===================================================================
RCS file: /home/wine/wine/loader/module.c,v
retrieving revision 1.137
diff -u -r1.137 module.c
--- wine/loader/module.c	2001/07/26 20:12:55	1.137
+++ wine/loader/module.c	2001/08/22 21:20:26
@@ -1316,6 +1316,43 @@
 }
 
 /***********************************************************************
+ *      AllocateLibdir
+ *
+ * helper for MODULE_LoadLibraryExA.  Allocate space to hold the directory
+ * portion of the provided name and put the name in it.
+ * 
+ */
+static LPCSTR AllocateLibdir (LPCSTR libname)
+{
+    LPCSTR p1, p2, p3, pmax;
+    LPSTR result;
+    int length;
+
+    p1 = strrchr (libname, '\\');
+    p2 = strrchr (libname, '/'); /* Naughty.  MSDN says don't */
+    p3 = strrchr (libname, ':'); /* assert null or libname+1 */
+    /* assert (p1 || p2 || p3) */
+
+    /* assert null is less than all pointers */
+    pmax = p1;
+    if (p2 > pmax) pmax = p2;
+    if (p3 > pmax) pmax = p3;
+    /* assert pmax */
+    
+    length = (pmax - libname)+1;
+
+    result = HeapAlloc (GetProcessHeap(), 0, length+1);
+
+    if (result)
+    {
+        strncpy (result, libname, length);
+        result [length] = '\0';
+    }
+
+    return result;
+}
+
+/***********************************************************************
  *	MODULE_LoadLibraryExA	(internal)
  *
  * Load a PE style module according to the load order.
@@ -1325,6 +1362,16 @@
  * ignore the parameter because it would be extremely difficult to
  * integrate this with different types of module represenations.
  *
+ * libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion
+ *        on this function.  When first called from LoadLibraryExA it will be
+ *        NULL but thereafter it may point to a buffer containing the path 
+ *        portion of the library name.  Note that the recursion all occurs 
+ *        within a Critical section (see LoadLibrarayExA) so the use of a 
+ *        static is acceptable.
+ *        (We have to use a static variable at some point anyway, to pass the
+ *        information from BUILTIN32_dlopen through dlopen and the builtin's
+ *        init function into load_library).
+ * allocated_libdir is TRUE in the stack frame that allocated libdir
  */
 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
 {
@@ -1334,14 +1381,28 @@
 	enum loadorder_type loadorder[LOADORDER_NTYPES];
 	LPSTR filename, p;
         const char *filetype = "";
+        BOOL allocated_libdir = FALSE;
+        static LPCSTR libdir = NULL; /* See above */
 
 	if ( !libname ) return NULL;
 
 	filename = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
 	if ( !filename ) return NULL;
 
+        if ((flags & LOAD_WITH_ALTERED_SEARCH_PATH) &&
+            FILE_contains_path (libname))
+        {
+            if (!(libdir = AllocateLibdir (libname))) goto error;
+            allocated_libdir = TRUE;
+        }
+
 	/* build the modules filename */
-	if (!SearchPathA( NULL, libname, ".dll", MAX_PATH, filename, NULL ))
+        if (! ( (!libdir || allocated_libdir) ?
+                     SearchPathA          (NULL, libname, ".dll",
+                                           MAX_PATH, filename, NULL)
+                 :
+                     SearchAlternatePathA (libdir, libname, ".dll",
+                                           MAX_PATH, filename, NULL)))
 	{
 	    if ( ! GetSystemDirectoryA ( filename, MAX_PATH ) ) 
   	        goto error;
@@ -1404,13 +1465,14 @@
                 if ((pwm->flags & WINE_MODREF_DONT_RESOLVE_REFS) &&
 		    !(flags & DONT_RESOLVE_DLL_REFERENCES))
                 {
-                    extern DWORD fixup_imports(WINE_MODREF *wm); /*FIXME*/
                     pwm->flags &= ~WINE_MODREF_DONT_RESOLVE_REFS;
-                    fixup_imports( pwm );
+                    PE_fixup_imports( pwm );
 		}
 		TRACE("Already loaded module '%s' at 0x%08x, count=%d, \n", filename, pwm->module, pwm->refCount);
                 RtlReleasePebLock();
 		HeapFree ( GetProcessHeap(), 0, filename );
+                if (allocated_libdir)
+                    HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
 		return pwm;
 	}
 
@@ -1459,6 +1521,8 @@
                         RtlReleasePebLock();
                         SetLastError( err );  /* restore last error */
 			HeapFree ( GetProcessHeap(), 0, filename );
+                        if (allocated_libdir)
+                            HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
 			return pwm;
 		}
 
@@ -1470,6 +1534,8 @@
  error:
 	WARN("Failed to load module '%s'; error=0x%08lx, \n", filename, GetLastError());
 	HeapFree ( GetProcessHeap(), 0, filename );
+        if (allocated_libdir)
+            HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
 	return NULL;
 }
 
Index: wine/loader/pe_image.c
===================================================================
RCS file: /home/wine/wine/loader/pe_image.c,v
retrieving revision 1.97
diff -u -r1.97 pe_image.c
--- wine/loader/pe_image.c	2001/08/09 21:16:55	1.97
+++ wine/loader/pe_image.c	2001/08/22 21:20:27
@@ -17,7 +17,7 @@
  * - If you want to enhance, speed up or clean up something in here, think
  *   twice WHY it is implemented in that strange way. There is usually a reason.
  *   Though sometimes it might just be lazyness ;)
- * - In PE_MapImage, right before fixup_imports() all external and internal 
+ * - In PE_MapImage, right before PE_fixup_imports() all external and internal 
  *   state MUST be correct since this function can be called with the SAME image
  *   AGAIN. (Thats recursion for you.) That means MODREF.module and
  *   NE_MODULE.module32.
@@ -242,7 +242,10 @@
 	}
 }
 
-DWORD fixup_imports( WINE_MODREF *wm )
+/****************************************************************
+ * 	PE_fixup_imports
+ */
+DWORD PE_fixup_imports( WINE_MODREF *wm )
 {
     IMAGE_IMPORT_DESCRIPTOR	*pe_imp;
     unsigned int load_addr	= wm->module;
@@ -633,7 +636,8 @@
 
     /* Fixup Imports */
 
-    if (!(wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) && fixup_imports( wm ))
+    if (!(wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) &&
+        PE_fixup_imports( wm ))
     {
         /* remove entry from modref chain */
 


More information about the wine-patches mailing list