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"
32 #include "ntdll_misc.h"
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(module);
40 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
42 typedef struct module_loadorder
44 const WCHAR *modulename;
45 enum loadorder_type loadorder[LOADORDER_NTYPES];
52 module_loadorder_t *order;
55 /* dll to load as builtins if not explicitly specified otherwise */
56 /* the list must remain sorted by dll name */
57 static const WCHAR default_builtins[][10] =
59 { 'g','d','i','3','2',0 },
60 { 'i','c','m','p',0 },
61 { 'k','e','r','n','e','l','3','2',0 },
62 { 'n','t','d','l','l',0 },
63 { 'o','d','b','c','3','2',0 },
64 { 't','t','y','d','r','v',0 },
65 { 'u','s','e','r','3','2',0 },
66 { 'w','3','2','s','k','r','n','l',0 },
67 { 'w','i','n','e','d','o','s',0 },
68 { 'w','i','n','e','p','s',0 },
69 { 'w','i','n','m','m',0 },
70 { 'w','n','a','s','p','i','3','2',0 },
71 { 'w','o','w','3','2',0 },
72 { 'w','s','2','_','3','2',0 },
73 { 'w','s','o','c','k','3','2',0 },
74 { 'x','1','1','d','r','v',0 }
77 /* default if nothing else specified */
78 static const enum loadorder_type default_loadorder[LOADORDER_NTYPES] =
80 LOADORDER_BI, LOADORDER_DLL, 0
83 /* default for modules with an explicit path */
84 static const enum loadorder_type default_path_loadorder[LOADORDER_NTYPES] =
86 LOADORDER_DLL, LOADORDER_BI, 0
89 static const WCHAR separatorsW[] = {',',' ','\t',0};
92 static struct loadorder_list env_list;
95 /***************************************************************************
96 * cmp_sort_func (internal, static)
98 * Sorting and comparing function used in sort and search of loadorder
101 static int cmp_sort_func(const void *s1, const void *s2)
103 return strcmpiW(((const module_loadorder_t *)s1)->modulename, ((const module_loadorder_t *)s2)->modulename);
107 /***************************************************************************
110 static int strcmp_func(const void *s1, const void *s2)
112 return strcmpiW( (const WCHAR *)s1, (const WCHAR *)s2 );
116 /***************************************************************************
119 * Return the base name of a file name (i.e. remove the path components).
121 static const WCHAR *get_basename( const WCHAR *name )
125 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
126 if ((ptr = strrchrW( name, '\\' ))) name = ptr + 1;
127 if ((ptr = strrchrW( name, '/' ))) name = ptr + 1;
131 /***************************************************************************
134 * Remove extension if it is ".dll".
136 static inline void remove_dll_ext( WCHAR *ext )
139 toupperW(ext[1]) == 'D' &&
140 toupperW(ext[2]) == 'L' &&
141 toupperW(ext[3]) == 'L' &&
146 /***************************************************************************
149 * Return a loadorder in printable form.
151 static const char *debugstr_loadorder( enum loadorder_type lo[] )
154 char buffer[LOADORDER_NTYPES*3+1];
157 for(i = 0; i < LOADORDER_NTYPES; i++)
159 if (lo[i] == LOADORDER_INVALID) break;
162 case LOADORDER_DLL: strcat( buffer, "n," ); break;
163 case LOADORDER_BI: strcat( buffer, "b," ); break;
164 default: strcat( buffer, "?," ); break;
167 if (buffer[0]) buffer[strlen(buffer)-1] = 0;
168 return debugstr_a(buffer);
172 /***************************************************************************
175 * Append a load order to the list if necessary.
177 static void append_load_order(enum loadorder_type lo[], enum loadorder_type append)
181 for (i = 0; i < LOADORDER_NTYPES; i++)
183 if (lo[i] == LOADORDER_INVALID) /* append it here */
186 lo[i] = LOADORDER_INVALID;
189 if (lo[i] == append) return; /* already in the list */
191 assert(0); /* cannot get here */
195 /***************************************************************************
198 * Parses the loadorder options from the configuration and puts it into
201 static void parse_load_order( const WCHAR *order, enum loadorder_type lo[] )
203 lo[0] = LOADORDER_INVALID;
206 order += strspnW( order, separatorsW );
209 case 'N': /* Native */
211 append_load_order( lo, LOADORDER_DLL );
213 case 'B': /* Builtin */
215 append_load_order( lo, LOADORDER_BI );
218 order += strcspnW( order, separatorsW );
223 /***************************************************************************
226 * Adds an entry in the list of environment overrides.
228 static void add_load_order( const module_loadorder_t *plo )
232 for(i = 0; i < env_list.count; i++)
234 if(!cmp_sort_func(plo, &env_list.order[i] ))
236 /* replace existing option */
237 memcpy( env_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
242 if (i >= env_list.alloc)
244 /* No space in current array, make it larger */
245 env_list.alloc += LOADORDER_ALLOC_CLUSTER;
247 env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
248 env_list.alloc * sizeof(module_loadorder_t));
250 env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
251 env_list.alloc * sizeof(module_loadorder_t));
254 MESSAGE("Virtual memory exhausted\n");
258 memcpy(env_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
259 env_list.order[i].modulename = plo->modulename;
264 /***************************************************************************
267 * Adds a set of entries in the list of command-line overrides from the key parameter.
269 static void add_load_order_set( WCHAR *entry )
271 module_loadorder_t ldo;
272 WCHAR *end = strchrW( entry, '=' );
276 parse_load_order( end, ldo.loadorder );
280 entry += strspnW( entry, separatorsW );
281 end = entry + strcspnW( entry, separatorsW );
282 if (*end) *end++ = 0;
285 WCHAR *ext = strrchrW(entry, '.');
286 if (ext) remove_dll_ext( ext );
287 ldo.modulename = entry;
288 add_load_order( &ldo );
295 /***************************************************************************
298 static void init_load_order(void)
300 const char *order = getenv( "WINEDLLOVERRIDES" );
307 if (!strcmp( order, "help" ))
310 " WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
311 " where each entry is of the form:\n"
312 " module[,module...]={native|builtin}[,{b|n}]\n"
314 " Only the first letter of the override (native or builtin)\n"
315 " is significant.\n\n"
317 " WINEDLLOVERRIDES=\"comdlg32=n,b;shell32,shlwapi=b\"\n" );
321 RtlCreateUnicodeStringFromAsciiz( &strW, order );
325 while (*entry && *entry == ';') entry++;
327 next = strchrW( entry, ';' );
328 if (next) *next++ = 0;
329 else next = entry + strlenW(entry);
330 add_load_order_set( entry );
334 /* sort the array for quick lookup */
336 qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
338 /* Note: we don't free the Unicode string because the
339 * stored module names point inside it */
343 /***************************************************************************
346 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
348 static inline BOOL get_env_load_order( const WCHAR *module, enum loadorder_type lo[] )
350 module_loadorder_t tmp, *res = NULL;
352 tmp.modulename = module;
353 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
354 if (env_list.count &&
355 (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
356 memcpy( lo, res->loadorder, sizeof(res->loadorder) );
357 return (res != NULL);
361 /***************************************************************************
362 * get_default_load_order
364 * Get the load order for a given module from the default list.
366 static inline BOOL get_default_load_order( const WCHAR *module, enum loadorder_type lo[] )
368 const int count = sizeof(default_builtins) / sizeof(default_builtins[0]);
369 if (!bsearch( module, default_builtins, count, sizeof(default_builtins[0]), strcmp_func ))
371 lo[0] = LOADORDER_BI;
372 lo[1] = LOADORDER_INVALID;
377 /***************************************************************************
380 * Open the registry key to the app-specific DllOverrides list.
382 static HANDLE open_app_key( const WCHAR *app_name, const WCHAR *module )
384 OBJECT_ATTRIBUTES attr;
385 UNICODE_STRING nameW;
388 static const WCHAR AppDefaultsW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
389 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
390 static const WCHAR DllOverridesW[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
392 str = RtlAllocateHeap( GetProcessHeap(), 0,
393 sizeof(AppDefaultsW) + sizeof(DllOverridesW) +
394 strlenW(app_name) * sizeof(WCHAR) );
396 strcpyW( str, AppDefaultsW );
397 strcatW( str, app_name );
398 strcatW( str, DllOverridesW );
400 TRACE( "searching %s in %s\n", debugstr_w(module), debugstr_w(str) );
402 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
403 attr.Length = sizeof(attr);
404 attr.RootDirectory = root;
405 attr.ObjectName = &nameW;
407 attr.SecurityDescriptor = NULL;
408 attr.SecurityQualityOfService = NULL;
409 RtlInitUnicodeString( &nameW, str );
411 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
412 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
414 RtlFreeHeap( GetProcessHeap(), 0, str );
419 /***************************************************************************
422 * Load the registry loadorder value for a given module.
424 static BOOL get_registry_value( HANDLE hkey, const WCHAR *module, enum loadorder_type lo[] )
426 UNICODE_STRING valueW;
431 RtlInitUnicodeString( &valueW, module );
433 if ((ret = !NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
434 buffer, sizeof(buffer), &count )))
437 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
441 enum loadorder_type type = LOADORDER_INVALID;
443 while (*str == ',' || isspaceW(*str)) str++;
446 switch(tolowerW(*str))
448 case 'n': type = LOADORDER_DLL; break;
449 case 'b': type = LOADORDER_BI; break;
450 case 's': break; /* no longer supported, ignore */
451 case 0: break; /* end of string */
453 ERR("Invalid load order module-type %s, ignored\n", debugstr_w(str));
456 if (type != LOADORDER_INVALID)
458 for (i = 0; i < n; i++) if (lo[i] == type) break; /* already specified */
459 if (i == n) lo[n++] = type;
461 while (*str && *str != ',' && !isspaceW(*str)) str++;
463 lo[n] = LOADORDER_INVALID;
469 /***************************************************************************
470 * MODULE_GetLoadOrderW (internal)
472 * Return the loadorder of a module.
473 * The system directory and '.dll' extension is stripped from the path.
475 void MODULE_GetLoadOrderW( enum loadorder_type loadorder[], const WCHAR *app_name,
478 static const WCHAR DllOverridesW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
479 'D','l','l','O','v','e','r','r','i','d','e','s',0};
481 static HANDLE std_key = (HANDLE)-1; /* key to standard section, cached */
484 WCHAR *module, *basename;
485 UNICODE_STRING path_str;
488 if (!init_done) init_load_order();
490 TRACE("looking for %s\n", debugstr_w(path));
492 loadorder[0] = LOADORDER_INVALID; /* in case something bad happens below */
494 /* Strip path information if the module resides in the system directory
496 RtlInitUnicodeString( &path_str, path );
497 if (RtlPrefixUnicodeString( &system_dir, &path_str, TRUE ))
499 const WCHAR *p = path + system_dir.Length / sizeof(WCHAR);
500 while (*p == '\\' || *p == '/') p++;
501 if (!strchrW( p, '\\' ) && !strchrW( p, '/' )) path = p;
504 if (!(len = strlenW(path))) return;
505 if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return;
506 strcpyW( module+1, path ); /* reserve module[0] for the wildcard char */
508 if (len >= 4) remove_dll_ext( module + 1 + len - 4 );
510 /* check environment variable first */
511 if (get_env_load_order( module+1, loadorder ))
513 TRACE( "got environment %s for %s\n",
514 debugstr_loadorder(loadorder), debugstr_w(path) );
518 /* then explicit module name in AppDefaults */
521 app_key = open_app_key( app_name, module+1 );
522 if (app_key && get_registry_value( app_key, module+1, loadorder ))
524 TRACE( "got app defaults %s for %s\n",
525 debugstr_loadorder(loadorder), debugstr_w(path) );
530 /* then explicit module name in standard section */
531 if (std_key == (HANDLE)-1)
533 OBJECT_ATTRIBUTES attr;
534 UNICODE_STRING nameW;
537 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
538 attr.Length = sizeof(attr);
539 attr.RootDirectory = root;
540 attr.ObjectName = &nameW;
542 attr.SecurityDescriptor = NULL;
543 attr.SecurityQualityOfService = NULL;
544 RtlInitUnicodeString( &nameW, DllOverridesW );
546 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
547 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
551 if (std_key && get_registry_value( std_key, module+1, loadorder ))
553 TRACE( "got standard entry %s for %s\n",
554 debugstr_loadorder(loadorder), debugstr_w(path) );
558 /* then module basename preceded by '*' in environment */
559 basename = (WCHAR *)get_basename( module+1 );
561 if (get_env_load_order( basename-1, loadorder ))
563 TRACE( "got environment basename %s for %s\n",
564 debugstr_loadorder(loadorder), debugstr_w(path) );
568 /* then module basename preceded by '*' in AppDefaults */
569 if (app_key && get_registry_value( app_key, basename-1, loadorder ))
571 TRACE( "got app defaults basename %s for %s\n",
572 debugstr_loadorder(loadorder), debugstr_w(path) );
576 /* then module name preceded by '*' in standard section */
577 if (std_key && get_registry_value( std_key, basename-1, loadorder ))
579 TRACE( "got standard base name %s for %s\n",
580 debugstr_loadorder(loadorder), debugstr_w(path) );
584 if (basename == module+1) /* module doesn't contain a path */
586 static const WCHAR wildcardW[] = {'*',0};
588 /* then base name matching compiled-in defaults */
589 if (get_default_load_order( basename, loadorder ))
591 TRACE( "got compiled-in default %s for %s\n",
592 debugstr_loadorder(loadorder), debugstr_w(path) );
596 /* then wildcard entry in AppDefaults (only if no explicit path) */
597 if (app_key && get_registry_value( app_key, wildcardW, loadorder ))
599 TRACE( "got app defaults wildcard %s for %s\n",
600 debugstr_loadorder(loadorder), debugstr_w(path) );
604 /* then wildcard entry in standard section (only if no explicit path) */
605 if (std_key && get_registry_value( std_key, wildcardW, loadorder ))
607 TRACE( "got standard wildcard %s for %s\n",
608 debugstr_loadorder(loadorder), debugstr_w(path) );
612 /* and last the hard-coded default */
613 memcpy( loadorder, default_loadorder, sizeof(default_loadorder) );
614 TRACE( "got hardcoded default %s for %s\n",
615 debugstr_loadorder(loadorder), debugstr_w(path) );
617 else /* module contains an explicit path */
619 /* then base name without '*' in AppDefaults */
620 if (app_key && get_registry_value( app_key, basename, loadorder ))
622 TRACE( "got basename app defaults %s for %s\n",
623 debugstr_loadorder(loadorder), debugstr_w(path) );
627 /* then base name without '*' in standard section */
628 if (std_key && get_registry_value( std_key, basename, loadorder ))
630 TRACE( "got basename standard entry %s for %s\n",
631 debugstr_loadorder(loadorder), debugstr_w(path) );
635 /* then base name matching compiled-in defaults */
636 if (get_default_load_order( basename, loadorder ))
638 TRACE( "got compiled-in default %s for %s\n",
639 debugstr_loadorder(loadorder), debugstr_w(path) );
643 /* and last the hard-coded default */
644 memcpy( loadorder, default_path_loadorder, sizeof(default_path_loadorder) );
645 TRACE( "got hardcoded path default %s for %s\n",
646 debugstr_loadorder(loadorder), debugstr_w(path) );
650 if (app_key) NtClose( app_key );
651 RtlFreeHeap( GetProcessHeap(), 0, module );