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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
35 #include "ntdll_misc.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(module);
43 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
45 typedef struct module_loadorder
47 const WCHAR *modulename;
48 enum loadorder_type loadorder[LOADORDER_NTYPES];
55 module_loadorder_t *order;
58 /* dll to load as builtins if not explicitly specified otherwise */
59 /* the list must remain sorted by dll name */
60 static const WCHAR default_builtins[][16] =
62 { 'g','d','i','3','2',0 },
63 { 'i','c','m','p',0 },
64 { 'k','e','r','n','e','l','3','2',0 },
65 { 'm','m','s','y','s','t','e','m',0 },
66 { 'n','t','d','l','l',0 },
67 { 'o','d','b','c','3','2',0 },
68 { 's','o','u','n','d',0 },
69 { 't','t','y','d','r','v',0 },
70 { 'u','s','e','r','3','2',0 },
71 { 'w','3','2','s','k','r','n','l',0 },
72 { 'w','3','2','s','y','s',0 },
73 { 'w','i','n','3','2','s','1','6',0 },
74 { 'w','i','n','a','s','p','i',0 },
75 { 'w','i','n','e','d','o','s',0 },
76 { 'w','i','n','e','p','s','1','6','.','d','r','v',0 },
77 { 'w','i','n','e','p','s',0 },
78 { 'w','i','n','m','m',0 },
79 { 'w','i','n','s','o','c','k',0 },
80 { 'w','n','a','s','p','i','3','2',0 },
81 { 'w','o','w','3','2',0 },
82 { 'w','s','2','_','3','2',0 },
83 { 'w','s','o','c','k','3','2',0 },
84 { 'x','1','1','d','r','v',0 }
87 /* default if nothing else specified */
88 static const enum loadorder_type default_loadorder[LOADORDER_NTYPES] =
90 LOADORDER_BI, LOADORDER_DLL, 0
93 /* default for modules with an explicit path */
94 static const enum loadorder_type default_path_loadorder[LOADORDER_NTYPES] =
96 LOADORDER_DLL, LOADORDER_BI, 0
99 static const WCHAR separatorsW[] = {',',' ','\t',0};
101 static int init_done;
102 static struct loadorder_list env_list;
105 /***************************************************************************
106 * cmp_sort_func (internal, static)
108 * Sorting and comparing function used in sort and search of loadorder
111 static int cmp_sort_func(const void *s1, const void *s2)
113 return strcmpiW(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
117 /***************************************************************************
120 static int strcmp_func(const void *s1, const void *s2)
122 return strcmpiW( (WCHAR *)s1, (WCHAR *)s2 );
126 /***************************************************************************
129 * Return the base name of a file name (i.e. remove the path components).
131 static const WCHAR *get_basename( const WCHAR *name )
135 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
136 if ((ptr = strrchrW( name, '\\' ))) name = ptr + 1;
137 if ((ptr = strrchrW( name, '/' ))) name = ptr + 1;
141 /***************************************************************************
144 * Remove extension if it is ".dll".
146 static inline void remove_dll_ext( WCHAR *ext )
149 toupperW(ext[1]) == 'D' &&
150 toupperW(ext[2]) == 'L' &&
151 toupperW(ext[3]) == 'L' &&
156 /***************************************************************************
159 * Return a loadorder in printable form.
161 static const char *debugstr_loadorder( enum loadorder_type lo[] )
164 char buffer[LOADORDER_NTYPES*3+1];
167 for(i = 0; i < LOADORDER_NTYPES; i++)
169 if (lo[i] == LOADORDER_INVALID) break;
172 case LOADORDER_DLL: strcat( buffer, "n," ); break;
173 case LOADORDER_BI: strcat( buffer, "b," ); break;
174 default: strcat( buffer, "?," ); break;
177 if (buffer[0]) buffer[strlen(buffer)-1] = 0;
178 return debugstr_a(buffer);
182 /***************************************************************************
185 * Append a load order to the list if necessary.
187 static void append_load_order(enum loadorder_type lo[], enum loadorder_type append)
191 for (i = 0; i < LOADORDER_NTYPES; i++)
193 if (lo[i] == LOADORDER_INVALID) /* append it here */
196 lo[i] = LOADORDER_INVALID;
199 if (lo[i] == append) return; /* already in the list */
201 assert(0); /* cannot get here */
205 /***************************************************************************
208 * Parses the loadorder options from the configuration and puts it into
211 static void parse_load_order( const WCHAR *order, enum loadorder_type lo[] )
213 lo[0] = LOADORDER_INVALID;
216 order += strspnW( order, separatorsW );
219 case 'N': /* Native */
221 append_load_order( lo, LOADORDER_DLL );
223 case 'B': /* Builtin */
225 append_load_order( lo, LOADORDER_BI );
228 order += strcspnW( order, separatorsW );
233 /***************************************************************************
236 * Adds an entry in the list of environment overrides.
238 static void add_load_order( const module_loadorder_t *plo )
242 for(i = 0; i < env_list.count; i++)
244 if(!cmp_sort_func(plo, &env_list.order[i] ))
246 /* replace existing option */
247 memcpy( env_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
252 if (i >= env_list.alloc)
254 /* No space in current array, make it larger */
255 env_list.alloc += LOADORDER_ALLOC_CLUSTER;
257 env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
258 env_list.alloc * sizeof(module_loadorder_t));
260 env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
261 env_list.alloc * sizeof(module_loadorder_t));
264 MESSAGE("Virtual memory exhausted\n");
268 memcpy(env_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
269 env_list.order[i].modulename = plo->modulename;
274 /***************************************************************************
277 * Adds a set of entries in the list of command-line overrides from the key parameter.
279 static void add_load_order_set( WCHAR *entry )
281 module_loadorder_t ldo;
282 WCHAR *end = strchrW( entry, '=' );
286 parse_load_order( end, ldo.loadorder );
290 entry += strspnW( entry, separatorsW );
291 end = entry + strcspnW( entry, separatorsW );
292 if (*end) *end++ = 0;
295 WCHAR *ext = strrchrW(entry, '.');
296 if (ext) remove_dll_ext( ext );
297 ldo.modulename = entry;
298 add_load_order( &ldo );
305 /***************************************************************************
308 static void init_load_order(void)
310 const char *order = getenv( "WINEDLLOVERRIDES" );
317 if (!strcmp( order, "help" ))
320 " WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
321 " where each entry is of the form:\n"
322 " module[,module...]={native|builtin}[,{b|n}]\n"
324 " Only the first letter of the override (native or builtin)\n"
325 " is significant.\n\n"
327 " WINEDLLOVERRIDES=\"comdlg32,commdlg=n,b;shell,shell32=b\"\n" );
331 RtlCreateUnicodeStringFromAsciiz( &strW, order );
335 while (*entry && *entry == ';') entry++;
337 next = strchrW( entry, ';' );
338 if (next) *next++ = 0;
339 else next = entry + strlenW(entry);
340 add_load_order_set( entry );
344 /* sort the array for quick lookup */
346 qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
348 /* Note: we don't free the Unicode string because the
349 * stored module names point inside it */
353 /***************************************************************************
356 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
358 static inline BOOL get_env_load_order( const WCHAR *module, enum loadorder_type lo[] )
360 module_loadorder_t tmp, *res = NULL;
362 tmp.modulename = module;
363 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
364 if (env_list.count &&
365 (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
366 memcpy( lo, res->loadorder, sizeof(res->loadorder) );
367 return (res != NULL);
371 /***************************************************************************
372 * get_default_load_order
374 * Get the load order for a given module from the default list.
376 static inline BOOL get_default_load_order( const WCHAR *module, enum loadorder_type lo[] )
378 const int count = sizeof(default_builtins) / sizeof(default_builtins[0]);
379 if (!bsearch( module, default_builtins, count, sizeof(default_builtins[0]), strcmp_func ))
381 lo[0] = LOADORDER_BI;
382 lo[1] = LOADORDER_INVALID;
387 /***************************************************************************
390 * Open the registry key to the app-specific DllOverrides list.
392 static HKEY open_app_key( const WCHAR *app_name, const WCHAR *module )
394 OBJECT_ATTRIBUTES attr;
395 UNICODE_STRING nameW;
398 static const WCHAR AppDefaultsW[] = {'M','a','c','h','i','n','e','\\',
399 'S','o','f','t','w','a','r','e','\\',
400 'W','i','n','e','\\',
401 'W','i','n','e','\\',
402 'C','o','n','f','i','g','\\',
403 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
404 static const WCHAR DllOverridesW[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
406 str = RtlAllocateHeap( GetProcessHeap(), 0,
407 sizeof(AppDefaultsW) + sizeof(DllOverridesW) +
408 strlenW(app_name) * sizeof(WCHAR) );
410 strcpyW( str, AppDefaultsW );
411 strcatW( str, app_name );
412 strcatW( str, DllOverridesW );
414 TRACE( "searching %s in %s\n", debugstr_w(module), debugstr_w(str) );
416 attr.Length = sizeof(attr);
417 attr.RootDirectory = 0;
418 attr.ObjectName = &nameW;
420 attr.SecurityDescriptor = NULL;
421 attr.SecurityQualityOfService = NULL;
422 RtlInitUnicodeString( &nameW, str );
424 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
425 RtlFreeHeap( GetProcessHeap(), 0, str );
430 /***************************************************************************
433 * Load the registry loadorder value for a given module.
435 static BOOL get_registry_value( HKEY hkey, const WCHAR *module, enum loadorder_type lo[] )
437 UNICODE_STRING valueW;
442 RtlInitUnicodeString( &valueW, module );
444 if ((ret = !NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
445 buffer, sizeof(buffer), &count )))
448 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
452 enum loadorder_type type = LOADORDER_INVALID;
454 while (*str == ',' || isspaceW(*str)) str++;
457 switch(tolowerW(*str))
459 case 'n': type = LOADORDER_DLL; break;
460 case 'b': type = LOADORDER_BI; break;
461 case 's': break; /* no longer supported, ignore */
462 case 0: break; /* end of string */
464 ERR("Invalid load order module-type %s, ignored\n", debugstr_w(str));
467 if (type != LOADORDER_INVALID)
469 for (i = 0; i < n; i++) if (lo[i] == type) break; /* already specified */
470 if (i == n) lo[n++] = type;
472 while (*str && *str != ',' && !isspaceW(*str)) str++;
474 lo[n] = LOADORDER_INVALID;
480 /***************************************************************************
481 * MODULE_GetSystemDirectory
483 * Retrieve the system directory. The string must be freed by the caller.
485 BOOL MODULE_GetSystemDirectory( UNICODE_STRING *sysdir )
487 static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
490 RtlInitUnicodeString( &name, winsysdirW );
491 sysdir->MaximumLength = 0;
492 if (RtlQueryEnvironmentVariable_U( NULL, &name, sysdir ) != STATUS_BUFFER_TOO_SMALL)
494 sysdir->MaximumLength = sysdir->Length + sizeof(WCHAR);
495 if (!(sysdir->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, sysdir->MaximumLength )))
497 if (RtlQueryEnvironmentVariable_U( NULL, &name, sysdir ) == STATUS_SUCCESS)
499 RtlFreeUnicodeString( sysdir );
504 /***************************************************************************
505 * MODULE_GetLoadOrderW (internal)
507 * Locate the loadorder of a module.
508 * Any path is stripped from the path-argument and so are the extension
509 * '.dll' and '.exe'. A lookup in the table can yield an override for
510 * the specific dll. Otherwise the default load order is returned.
512 void MODULE_GetLoadOrderW( enum loadorder_type loadorder[], const WCHAR *app_name,
513 const WCHAR *path, BOOL win32 )
515 static const WCHAR DllOverridesW[] = {'M','a','c','h','i','n','e','\\',
516 'S','o','f','t','w','a','r','e','\\',
517 'W','i','n','e','\\',
518 'W','i','n','e','\\',
519 'C','o','n','f','i','g','\\',
520 'D','l','l','O','v','e','r','r','i','d','e','s',0};
522 static HKEY std_key = (HKEY)-1; /* key to standard section, cached */
524 WCHAR *module, *basename;
527 if (!init_done) init_load_order();
529 TRACE("looking for %s\n", debugstr_w(path));
531 loadorder[0] = LOADORDER_INVALID; /* in case something bad happens below */
533 /* Strip path information if the module resides in the system directory
534 * (path is already stripped by caller for 16-bit modules)
538 UNICODE_STRING path_str, sysdir;
540 if (!MODULE_GetSystemDirectory( &sysdir )) return;
541 RtlInitUnicodeString( &path_str, path );
542 if (RtlPrefixUnicodeString( &sysdir, &path_str, TRUE ))
544 const WCHAR *p = path + sysdir.Length / sizeof(WCHAR);
545 while (*p == '\\' || *p == '/') p++;
546 if (!strchrW( p, '\\' ) && !strchrW( p, '/' )) path = p;
548 RtlFreeUnicodeString( &sysdir );
551 if (!(len = strlenW(path))) return;
552 if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return;
553 strcpyW( module+1, path ); /* reserve module[0] for the wildcard char */
555 if (len >= 4) remove_dll_ext( module + 1 + len - 4 );
557 /* check environment variable first */
558 if (get_env_load_order( module+1, loadorder ))
560 TRACE( "got environment %s for %s\n",
561 debugstr_loadorder(loadorder), debugstr_w(path) );
565 /* then explicit module name in AppDefaults */
568 app_key = open_app_key( app_name, module+1 );
569 if (app_key && get_registry_value( app_key, module+1, loadorder ))
571 TRACE( "got app defaults %s for %s\n",
572 debugstr_loadorder(loadorder), debugstr_w(path) );
577 /* then explicit module name in standard section */
578 if (std_key == (HKEY)-1)
580 OBJECT_ATTRIBUTES attr;
581 UNICODE_STRING nameW;
583 attr.Length = sizeof(attr);
584 attr.RootDirectory = 0;
585 attr.ObjectName = &nameW;
587 attr.SecurityDescriptor = NULL;
588 attr.SecurityQualityOfService = NULL;
589 RtlInitUnicodeString( &nameW, DllOverridesW );
591 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
594 if (std_key && get_registry_value( std_key, module+1, loadorder ))
596 TRACE( "got standard entry %s for %s\n",
597 debugstr_loadorder(loadorder), debugstr_w(path) );
601 /* then module basename preceded by '*' in AppDefaults */
602 basename = (WCHAR *)get_basename( module+1 );
604 if (app_key && get_registry_value( app_key, basename-1, loadorder ))
606 TRACE( "got app defaults basename %s for %s\n",
607 debugstr_loadorder(loadorder), debugstr_w(path) );
611 /* then module name preceded by '*' in standard section */
612 if (std_key && get_registry_value( std_key, basename-1, loadorder ))
614 TRACE( "got standard base name %s for %s\n",
615 debugstr_loadorder(loadorder), debugstr_w(path) );
619 /* then base name matching compiled-in defaults */
620 if (get_default_load_order( basename, loadorder ))
622 TRACE( "got compiled-in default %s for %s\n",
623 debugstr_loadorder(loadorder), debugstr_w(path) );
627 if (basename == module+1)
629 static const WCHAR wildcardW[] = {'*',0};
631 /* then wildcard entry in AppDefaults (only if no explicit path) */
632 if (app_key && get_registry_value( app_key, wildcardW, loadorder ))
634 TRACE( "got app defaults wildcard %s for %s\n",
635 debugstr_loadorder(loadorder), debugstr_w(path) );
639 /* then wildcard entry in standard section (only if no explicit path) */
640 if (std_key && get_registry_value( std_key, wildcardW, loadorder ))
642 TRACE( "got standard wildcard %s for %s\n",
643 debugstr_loadorder(loadorder), debugstr_w(path) );
647 /* and last the hard-coded default */
648 memcpy( loadorder, default_loadorder, sizeof(default_loadorder) );
649 TRACE( "got hardcoded default %s for %s\n",
650 debugstr_loadorder(loadorder), debugstr_w(path) );
652 else /* module contains an explicit path */
654 memcpy( loadorder, default_path_loadorder, sizeof(default_path_loadorder) );
655 TRACE( "got hardcoded path default %s for %s\n",
656 debugstr_loadorder(loadorder), debugstr_w(path) );
660 if (app_key) NtClose( app_key );
661 RtlFreeHeap( GetProcessHeap(), 0, module );
665 /***************************************************************************
666 * MODULE_GetLoadOrderA (internal)
668 * Locate the loadorder of a module.
669 * Any path is stripped from the path-argument and so are the extension
670 * '.dll' and '.exe'. A lookup in the table can yield an override for
671 * the specific dll. Otherwise the default load order is returned.
673 * FIXME: should be removed, everything should be Unicode.
675 void MODULE_GetLoadOrderA( enum loadorder_type loadorder[], const WCHAR *app_name,
676 const char *path, BOOL win32 )
678 UNICODE_STRING pathW;
680 RtlCreateUnicodeStringFromAsciiz( &pathW, path );
681 MODULE_GetLoadOrderW( loadorder, app_name, pathW.Buffer, win32 );
682 RtlFreeUnicodeString( &pathW );