2 * Dlls load order support
4 * Copyright 1999 Bertho Stultiens
5 * Copyright 2003 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #define WIN32_NO_STATUS
31 #include "ntdll_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(module
);
37 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
39 typedef struct module_loadorder
41 const WCHAR
*modulename
;
42 enum loadorder loadorder
;
49 module_loadorder_t
*order
;
52 static const WCHAR separatorsW
[] = L
", \t";
54 static BOOL init_done
;
55 static struct loadorder_list env_list
;
58 /***************************************************************************
59 * cmp_sort_func (internal, static)
61 * Sorting and comparing function used in sort and search of loadorder
64 static int __cdecl
cmp_sort_func(const void *s1
, const void *s2
)
66 return wcsicmp(((const module_loadorder_t
*)s1
)->modulename
, ((const module_loadorder_t
*)s2
)->modulename
);
70 /***************************************************************************
73 * Return the base name of a file name (i.e. remove the path components).
75 static const WCHAR
*get_basename( const WCHAR
*name
)
79 if (name
[0] && name
[1] == ':') name
+= 2; /* strip drive specification */
80 if ((ptr
= wcsrchr( name
, '\\' ))) name
= ptr
+ 1;
81 if ((ptr
= wcsrchr( name
, '/' ))) name
= ptr
+ 1;
85 /***************************************************************************
88 * Remove extension if it is ".dll".
90 static inline void remove_dll_ext( WCHAR
*name
)
92 WCHAR
*p
= wcsrchr( name
, '.' );
94 if (p
&& !wcsicmp( p
, L
".dll" )) *p
= 0;
98 /***************************************************************************
101 * Return a loadorder in printable form.
103 static const char *debugstr_loadorder( enum loadorder lo
)
107 case LO_DISABLED
: return "";
108 case LO_NATIVE
: return "n";
109 case LO_BUILTIN
: return "b";
110 case LO_NATIVE_BUILTIN
: return "n,b";
111 case LO_BUILTIN_NATIVE
: return "b,n";
112 case LO_DEFAULT
: return "default";
113 default: return "??";
118 /***************************************************************************
121 * Parses the loadorder options from the configuration and puts it into
124 static enum loadorder
parse_load_order( const WCHAR
*order
)
126 enum loadorder ret
= LO_DISABLED
;
130 order
+= wcsspn( order
, separatorsW
);
133 case 'N': /* native */
135 if (ret
== LO_DISABLED
) ret
= LO_NATIVE
;
136 else if (ret
== LO_BUILTIN
) return LO_BUILTIN_NATIVE
;
138 case 'B': /* builtin */
140 if (ret
== LO_DISABLED
) ret
= LO_BUILTIN
;
141 else if (ret
== LO_NATIVE
) return LO_NATIVE_BUILTIN
;
144 order
+= wcscspn( order
, separatorsW
);
150 /***************************************************************************
153 * Adds an entry in the list of environment overrides.
155 static void add_load_order( const module_loadorder_t
*plo
)
159 for(i
= 0; i
< env_list
.count
; i
++)
161 if(!cmp_sort_func(plo
, &env_list
.order
[i
] ))
163 /* replace existing option */
164 env_list
.order
[i
].loadorder
= plo
->loadorder
;
169 if (i
>= env_list
.alloc
)
171 /* No space in current array, make it larger */
172 env_list
.alloc
+= LOADORDER_ALLOC_CLUSTER
;
174 env_list
.order
= RtlReAllocateHeap(GetProcessHeap(), 0, env_list
.order
,
175 env_list
.alloc
* sizeof(module_loadorder_t
));
177 env_list
.order
= RtlAllocateHeap(GetProcessHeap(), 0,
178 env_list
.alloc
* sizeof(module_loadorder_t
));
181 MESSAGE("Virtual memory exhausted\n");
182 NtTerminateProcess( GetCurrentProcess(), 1 );
185 env_list
.order
[i
].loadorder
= plo
->loadorder
;
186 env_list
.order
[i
].modulename
= plo
->modulename
;
191 /***************************************************************************
194 * Adds a set of entries in the list of command-line overrides from the key parameter.
196 static void add_load_order_set( WCHAR
*entry
)
198 module_loadorder_t ldo
;
199 WCHAR
*end
= wcschr( entry
, '=' );
203 ldo
.loadorder
= parse_load_order( end
);
207 entry
+= wcsspn( entry
, separatorsW
);
208 end
= entry
+ wcscspn( entry
, separatorsW
);
209 if (*end
) *end
++ = 0;
212 remove_dll_ext( entry
);
213 ldo
.modulename
= entry
;
214 add_load_order( &ldo
);
221 /***************************************************************************
224 static void init_load_order(void)
226 WCHAR
*entry
, *next
, *order
;
234 order
= RtlAllocateHeap( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
235 status
= RtlQueryEnvironmentVariable( NULL
, L
"WINEDLLOVERRIDES", wcslen(L
"WINEDLLOVERRIDES"),
236 order
, len
- 1, &len
);
242 RtlFreeHeap( GetProcessHeap(), 0, order
);
243 if (status
!= STATUS_BUFFER_TOO_SMALL
) return;
249 while (*entry
== ';') entry
++;
251 next
= wcschr( entry
, ';' );
252 if (next
) *next
++ = 0;
253 else next
= entry
+ wcslen(entry
);
254 add_load_order_set( entry
);
258 /* sort the array for quick lookup */
260 qsort(env_list
.order
, env_list
.count
, sizeof(env_list
.order
[0]), cmp_sort_func
);
262 /* note: we don't free the string because the stored module names point inside it */
266 /***************************************************************************
269 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
271 static inline enum loadorder
get_env_load_order( const WCHAR
*module
)
273 module_loadorder_t tmp
, *res
;
275 tmp
.modulename
= module
;
276 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
277 if (env_list
.count
&&
278 (res
= bsearch(&tmp
, env_list
.order
, env_list
.count
, sizeof(env_list
.order
[0]), cmp_sort_func
)))
279 return res
->loadorder
;
284 /***************************************************************************
287 * Return a handle to the standard DllOverrides registry section.
289 static HANDLE
get_standard_key(void)
291 static HANDLE std_key
= (HANDLE
)-1;
293 if (std_key
== (HANDLE
)-1)
295 OBJECT_ATTRIBUTES attr
;
296 UNICODE_STRING nameW
;
299 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
300 attr
.Length
= sizeof(attr
);
301 attr
.RootDirectory
= root
;
302 attr
.ObjectName
= &nameW
;
304 attr
.SecurityDescriptor
= NULL
;
305 attr
.SecurityQualityOfService
= NULL
;
306 RtlInitUnicodeString( &nameW
, L
"Software\\Wine\\DllOverrides" );
308 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
309 if (NtOpenKey( &std_key
, KEY_ALL_ACCESS
, &attr
)) std_key
= 0;
316 /***************************************************************************
319 * Get the registry key for the app-specific DllOverrides list.
321 static HANDLE
get_app_key( const WCHAR
*app_name
)
323 OBJECT_ATTRIBUTES attr
;
324 UNICODE_STRING nameW
;
327 static HANDLE app_key
= (HANDLE
)-1;
329 if (app_key
!= (HANDLE
)-1) return app_key
;
331 str
= RtlAllocateHeap( GetProcessHeap(), 0,
332 sizeof(L
"Software\\Wine\\AppDefaults\\") + sizeof(L
"\\DllOverrides") +
333 wcslen(app_name
) * sizeof(WCHAR
) );
335 wcscpy( str
, L
"Software\\Wine\\AppDefaults\\" );
336 wcscat( str
, app_name
);
337 wcscat( str
, L
"\\DllOverrides" );
339 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
340 attr
.Length
= sizeof(attr
);
341 attr
.RootDirectory
= root
;
342 attr
.ObjectName
= &nameW
;
344 attr
.SecurityDescriptor
= NULL
;
345 attr
.SecurityQualityOfService
= NULL
;
346 RtlInitUnicodeString( &nameW
, str
);
348 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
349 if (NtOpenKey( &app_key
, KEY_ALL_ACCESS
, &attr
)) app_key
= 0;
351 RtlFreeHeap( GetProcessHeap(), 0, str
);
356 /***************************************************************************
359 * Load the registry loadorder value for a given module.
361 static enum loadorder
get_registry_value( HANDLE hkey
, const WCHAR
*module
)
363 UNICODE_STRING valueW
;
367 RtlInitUnicodeString( &valueW
, module
);
369 if (!NtQueryValueKey( hkey
, &valueW
, KeyValuePartialInformation
,
370 buffer
, sizeof(buffer
), &count
))
372 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
373 return parse_load_order( str
);
379 /***************************************************************************
380 * get_load_order_value
382 * Get the load order for the exact specified module string, looking in:
383 * 1. The WINEDLLOVERRIDES environment variable
384 * 2. The per-application DllOverrides key
385 * 3. The standard DllOverrides key
387 static enum loadorder
get_load_order_value( HANDLE std_key
, HANDLE app_key
, const WCHAR
*module
)
391 if ((ret
= get_env_load_order( module
)) != LO_INVALID
)
393 TRACE( "got environment %s for %s\n", debugstr_loadorder(ret
), debugstr_w(module
) );
397 if (app_key
&& ((ret
= get_registry_value( app_key
, module
)) != LO_INVALID
))
399 TRACE( "got app defaults %s for %s\n", debugstr_loadorder(ret
), debugstr_w(module
) );
403 if (std_key
&& ((ret
= get_registry_value( std_key
, module
)) != LO_INVALID
))
405 TRACE( "got standard key %s for %s\n", debugstr_loadorder(ret
), debugstr_w(module
) );
413 /***************************************************************************
414 * get_load_order (internal)
416 * Return the loadorder of a module.
417 * The system directory and '.dll' extension is stripped from the path.
419 enum loadorder
get_load_order( const WCHAR
*app_name
, const UNICODE_STRING
*nt_name
)
421 enum loadorder ret
= LO_INVALID
;
422 HANDLE std_key
, app_key
= 0;
423 const WCHAR
*path
= nt_name
->Buffer
;
424 WCHAR
*module
, *basename
;
427 if (!init_done
) init_load_order();
428 std_key
= get_standard_key();
429 if (app_name
) app_key
= get_app_key( app_name
);
430 if (!wcsncmp( path
, L
"\\??\\", 4 )) path
+= 4;
432 TRACE("looking for %s\n", debugstr_w(path
));
434 /* Strip path information if the module resides in the system directory
436 if (!wcsnicmp( system_dir
, path
, wcslen( system_dir
)))
438 const WCHAR
*p
= path
+ wcslen( system_dir
);
439 while (*p
== '\\' || *p
== '/') p
++;
440 if (!wcschr( p
, '\\' ) && !wcschr( p
, '/' )) path
= p
;
443 if (!(len
= wcslen(path
))) return ret
;
444 if (!(module
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 2) * sizeof(WCHAR
) ))) return ret
;
445 wcscpy( module
+1, path
); /* reserve module[0] for the wildcard char */
446 remove_dll_ext( module
+ 1 );
447 basename
= (WCHAR
*)get_basename( module
+1 );
449 /* first explicit module name */
450 if ((ret
= get_load_order_value( std_key
, app_key
, module
+1 )) != LO_INVALID
)
453 /* then module basename preceded by '*' */
455 if ((ret
= get_load_order_value( std_key
, app_key
, basename
-1 )) != LO_INVALID
)
458 /* then module basename without '*' (only if explicit path) */
459 if (basename
!= module
+1 && ((ret
= get_load_order_value( std_key
, app_key
, basename
)) != LO_INVALID
))
462 /* if loading the main exe with an explicit path, try native first */
463 if (!app_name
&& basename
!= module
+1)
465 ret
= LO_NATIVE_BUILTIN
;
466 TRACE( "got main exe default %s for %s\n", debugstr_loadorder(ret
), debugstr_w(path
) );
470 /* and last the hard-coded default */
472 TRACE( "got hardcoded %s for %s\n", debugstr_loadorder(ret
), debugstr_w(path
) );
475 RtlFreeHeap( GetProcessHeap(), 0, module
);