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
23 #include "wine/port.h"
32 #include "ntdll_misc.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(module
);
39 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
41 typedef struct module_loadorder
43 const WCHAR
*modulename
;
44 enum loadorder loadorder
;
51 module_loadorder_t
*order
;
54 static const WCHAR separatorsW
[] = {',',' ','\t',0};
57 static struct loadorder_list env_list
;
60 /***************************************************************************
61 * cmp_sort_func (internal, static)
63 * Sorting and comparing function used in sort and search of loadorder
66 static int cmp_sort_func(const void *s1
, const void *s2
)
68 return strcmpiW(((const module_loadorder_t
*)s1
)->modulename
, ((const module_loadorder_t
*)s2
)->modulename
);
72 /***************************************************************************
75 * Return the base name of a file name (i.e. remove the path components).
77 static const WCHAR
*get_basename( const WCHAR
*name
)
81 if (name
[0] && name
[1] == ':') name
+= 2; /* strip drive specification */
82 if ((ptr
= strrchrW( name
, '\\' ))) name
= ptr
+ 1;
83 if ((ptr
= strrchrW( name
, '/' ))) name
= ptr
+ 1;
87 /***************************************************************************
90 * Remove extension if it is ".dll".
92 static inline void remove_dll_ext( WCHAR
*ext
)
95 toupperW(ext
[1]) == 'D' &&
96 toupperW(ext
[2]) == 'L' &&
97 toupperW(ext
[3]) == 'L' &&
102 /***************************************************************************
105 * Return a loadorder in printable form.
107 static const char *debugstr_loadorder( enum loadorder lo
)
111 case LO_DISABLED
: return "";
112 case LO_NATIVE
: return "n";
113 case LO_BUILTIN
: return "b";
114 case LO_NATIVE_BUILTIN
: return "n,b";
115 case LO_BUILTIN_NATIVE
: return "b,n";
116 case LO_DEFAULT
: return "default";
117 default: return "??";
122 /***************************************************************************
125 * Parses the loadorder options from the configuration and puts it into
128 static enum loadorder
parse_load_order( const WCHAR
*order
)
130 enum loadorder ret
= LO_DISABLED
;
134 order
+= strspnW( order
, separatorsW
);
137 case 'N': /* native */
139 if (ret
== LO_DISABLED
) ret
= LO_NATIVE
;
140 else if (ret
== LO_BUILTIN
) return LO_BUILTIN_NATIVE
;
142 case 'B': /* builtin */
144 if (ret
== LO_DISABLED
) ret
= LO_BUILTIN
;
145 else if (ret
== LO_NATIVE
) return LO_NATIVE_BUILTIN
;
148 order
+= strcspnW( order
, separatorsW
);
154 /***************************************************************************
157 * Adds an entry in the list of environment overrides.
159 static void add_load_order( const module_loadorder_t
*plo
)
163 for(i
= 0; i
< env_list
.count
; i
++)
165 if(!cmp_sort_func(plo
, &env_list
.order
[i
] ))
167 /* replace existing option */
168 env_list
.order
[i
].loadorder
= plo
->loadorder
;
173 if (i
>= env_list
.alloc
)
175 /* No space in current array, make it larger */
176 env_list
.alloc
+= LOADORDER_ALLOC_CLUSTER
;
178 env_list
.order
= RtlReAllocateHeap(GetProcessHeap(), 0, env_list
.order
,
179 env_list
.alloc
* sizeof(module_loadorder_t
));
181 env_list
.order
= RtlAllocateHeap(GetProcessHeap(), 0,
182 env_list
.alloc
* sizeof(module_loadorder_t
));
185 MESSAGE("Virtual memory exhausted\n");
189 env_list
.order
[i
].loadorder
= plo
->loadorder
;
190 env_list
.order
[i
].modulename
= plo
->modulename
;
195 /***************************************************************************
198 * Adds a set of entries in the list of command-line overrides from the key parameter.
200 static void add_load_order_set( WCHAR
*entry
)
202 module_loadorder_t ldo
;
203 WCHAR
*end
= strchrW( entry
, '=' );
207 ldo
.loadorder
= parse_load_order( end
);
211 entry
+= strspnW( entry
, separatorsW
);
212 end
= entry
+ strcspnW( entry
, separatorsW
);
213 if (*end
) *end
++ = 0;
216 WCHAR
*ext
= strrchrW(entry
, '.');
217 if (ext
) remove_dll_ext( ext
);
218 ldo
.modulename
= entry
;
219 add_load_order( &ldo
);
226 /***************************************************************************
229 static void init_load_order(void)
231 const char *order
= getenv( "WINEDLLOVERRIDES" );
238 if (!strcmp( order
, "help" ))
241 " WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
242 " where each entry is of the form:\n"
243 " module[,module...]={native|builtin}[,{b|n}]\n"
245 " Only the first letter of the override (native or builtin)\n"
246 " is significant.\n\n"
248 " WINEDLLOVERRIDES=\"comdlg32=n,b;shell32,shlwapi=b\"\n" );
252 RtlCreateUnicodeStringFromAsciiz( &strW
, order
);
256 while (*entry
&& *entry
== ';') entry
++;
258 next
= strchrW( entry
, ';' );
259 if (next
) *next
++ = 0;
260 else next
= entry
+ strlenW(entry
);
261 add_load_order_set( entry
);
265 /* sort the array for quick lookup */
267 qsort(env_list
.order
, env_list
.count
, sizeof(env_list
.order
[0]), cmp_sort_func
);
269 /* Note: we don't free the Unicode string because the
270 * stored module names point inside it */
274 /***************************************************************************
277 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
279 static inline enum loadorder
get_env_load_order( const WCHAR
*module
)
281 module_loadorder_t tmp
, *res
;
283 tmp
.modulename
= module
;
284 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
285 if (env_list
.count
&&
286 (res
= bsearch(&tmp
, env_list
.order
, env_list
.count
, sizeof(env_list
.order
[0]), cmp_sort_func
)))
287 return res
->loadorder
;
292 /***************************************************************************
295 * Return a handle to the standard DllOverrides registry section.
297 static HANDLE
get_standard_key(void)
299 static const WCHAR DllOverridesW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
300 'D','l','l','O','v','e','r','r','i','d','e','s',0};
301 static HANDLE std_key
= (HANDLE
)-1;
303 if (std_key
== (HANDLE
)-1)
305 OBJECT_ATTRIBUTES attr
;
306 UNICODE_STRING nameW
;
309 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
310 attr
.Length
= sizeof(attr
);
311 attr
.RootDirectory
= root
;
312 attr
.ObjectName
= &nameW
;
314 attr
.SecurityDescriptor
= NULL
;
315 attr
.SecurityQualityOfService
= NULL
;
316 RtlInitUnicodeString( &nameW
, DllOverridesW
);
318 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
319 if (NtOpenKey( &std_key
, KEY_ALL_ACCESS
, &attr
)) std_key
= 0;
326 /***************************************************************************
329 * Get the registry key for the app-specific DllOverrides list.
331 static HANDLE
get_app_key( const WCHAR
*app_name
)
333 OBJECT_ATTRIBUTES attr
;
334 UNICODE_STRING nameW
;
337 static const WCHAR AppDefaultsW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
338 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
339 static const WCHAR DllOverridesW
[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
340 static HANDLE app_key
= (HANDLE
)-1;
342 if (app_key
!= (HANDLE
)-1) return app_key
;
344 str
= RtlAllocateHeap( GetProcessHeap(), 0,
345 sizeof(AppDefaultsW
) + sizeof(DllOverridesW
) +
346 strlenW(app_name
) * sizeof(WCHAR
) );
348 strcpyW( str
, AppDefaultsW
);
349 strcatW( str
, app_name
);
350 strcatW( str
, DllOverridesW
);
352 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
353 attr
.Length
= sizeof(attr
);
354 attr
.RootDirectory
= root
;
355 attr
.ObjectName
= &nameW
;
357 attr
.SecurityDescriptor
= NULL
;
358 attr
.SecurityQualityOfService
= NULL
;
359 RtlInitUnicodeString( &nameW
, str
);
361 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
362 if (NtOpenKey( &app_key
, KEY_ALL_ACCESS
, &attr
)) app_key
= 0;
364 RtlFreeHeap( GetProcessHeap(), 0, str
);
369 /***************************************************************************
372 * Load the registry loadorder value for a given module.
374 static enum loadorder
get_registry_value( HANDLE hkey
, const WCHAR
*module
)
376 UNICODE_STRING valueW
;
381 RtlInitUnicodeString( &valueW
, module
);
383 if ((ret
= !NtQueryValueKey( hkey
, &valueW
, KeyValuePartialInformation
,
384 buffer
, sizeof(buffer
), &count
)))
386 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
387 return parse_load_order( str
);
393 /***************************************************************************
394 * get_load_order_value
396 * Get the load order for the exact specified module string, looking in:
397 * 1. The WINEDLLOVERRIDES environment variable
398 * 2. The per-application DllOverrides key
399 * 3. The standard DllOverrides key
401 static enum loadorder
get_load_order_value( HANDLE std_key
, HANDLE app_key
, const WCHAR
*module
)
405 if ((ret
= get_env_load_order( module
)) != LO_INVALID
)
407 TRACE( "got environment %s for %s\n", debugstr_loadorder(ret
), debugstr_w(module
) );
411 if (app_key
&& ((ret
= get_registry_value( app_key
, module
)) != LO_INVALID
))
413 TRACE( "got app defaults %s for %s\n", debugstr_loadorder(ret
), debugstr_w(module
) );
417 if (std_key
&& ((ret
= get_registry_value( std_key
, module
)) != LO_INVALID
))
419 TRACE( "got standard key %s for %s\n", debugstr_loadorder(ret
), debugstr_w(module
) );
427 /***************************************************************************
428 * get_load_order (internal)
430 * Return the loadorder of a module.
431 * The system directory and '.dll' extension is stripped from the path.
433 enum loadorder
get_load_order( const WCHAR
*app_name
, const WCHAR
*path
)
435 enum loadorder ret
= LO_INVALID
;
436 HANDLE std_key
, app_key
= 0;
437 WCHAR
*module
, *basename
;
438 UNICODE_STRING path_str
;
441 if (!init_done
) init_load_order();
442 std_key
= get_standard_key();
443 if (app_name
) app_key
= get_app_key( app_name
);
445 TRACE("looking for %s\n", debugstr_w(path
));
447 /* Strip path information if the module resides in the system directory
449 RtlInitUnicodeString( &path_str
, path
);
450 if (RtlPrefixUnicodeString( &system_dir
, &path_str
, TRUE
))
452 const WCHAR
*p
= path
+ system_dir
.Length
/ sizeof(WCHAR
);
453 while (*p
== '\\' || *p
== '/') p
++;
454 if (!strchrW( p
, '\\' ) && !strchrW( p
, '/' )) path
= p
;
457 if (!(len
= strlenW(path
))) return ret
;
458 if (!(module
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 2) * sizeof(WCHAR
) ))) return ret
;
459 strcpyW( module
+1, path
); /* reserve module[0] for the wildcard char */
460 basename
= (WCHAR
*)get_basename( module
+1 );
462 if (len
>= 4) remove_dll_ext( module
+ 1 + len
- 4 );
464 /* first explicit module name */
465 if ((ret
= get_load_order_value( std_key
, app_key
, module
+1 )) != LO_INVALID
)
468 /* then module basename preceded by '*' */
470 if ((ret
= get_load_order_value( std_key
, app_key
, basename
-1 )) != LO_INVALID
)
473 /* then module basename without '*' (only if explicit path) */
474 if (basename
!= module
+1 && ((ret
= get_load_order_value( std_key
, app_key
, basename
)) != LO_INVALID
))
477 /* if loading the main exe with an explicit path, try native first */
478 if (!app_name
&& basename
!= module
+1)
480 ret
= LO_NATIVE_BUILTIN
;
481 TRACE( "got main exe default %s for %s\n", debugstr_loadorder(ret
), debugstr_w(path
) );
485 /* and last the hard-coded default */
487 TRACE( "got hardcoded %s for %s\n", debugstr_loadorder(ret
), debugstr_w(path
) );
490 RtlFreeHeap( GetProcessHeap(), 0, module
);