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"
37 #include "ntdll_misc.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(module
);
44 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
46 typedef struct module_loadorder
48 const WCHAR
*modulename
;
49 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
56 module_loadorder_t
*order
;
59 /* dll to load as builtins if not explicitly specified otherwise */
60 /* the list must remain sorted by dll name */
61 static const WCHAR default_builtins
[][16] =
63 { 'g','d','i','3','2',0 },
64 { 'i','c','m','p',0 },
65 { 'k','e','r','n','e','l','3','2',0 },
66 { 'm','m','s','y','s','t','e','m',0 },
67 { 'n','t','d','l','l',0 },
68 { 'o','d','b','c','3','2',0 },
69 { 's','o','u','n','d',0 },
70 { 't','t','y','d','r','v',0 },
71 { 'u','s','e','r','3','2',0 },
72 { 'w','3','2','s','k','r','n','l',0 },
73 { 'w','3','2','s','y','s',0 },
74 { 'w','i','n','3','2','s','1','6',0 },
75 { 'w','i','n','a','s','p','i',0 },
76 { 'w','i','n','e','d','o','s',0 },
77 { 'w','i','n','e','p','s','1','6','.','d','r','v',0 },
78 { 'w','i','n','e','p','s',0 },
79 { 'w','i','n','m','m',0 },
80 { 'w','i','n','s','o','c','k',0 },
81 { 'w','n','a','s','p','i','3','2',0 },
82 { 'w','o','w','3','2',0 },
83 { 'w','s','2','_','3','2',0 },
84 { 'w','s','o','c','k','3','2',0 },
85 { 'x','1','1','d','r','v',0 }
88 /* default if nothing else specified */
89 static const enum loadorder_type default_loadorder
[LOADORDER_NTYPES
] =
91 LOADORDER_BI
, LOADORDER_DLL
, 0
94 /* default for modules with an explicit path */
95 static const enum loadorder_type default_path_loadorder
[LOADORDER_NTYPES
] =
97 LOADORDER_DLL
, LOADORDER_BI
, 0
100 static const WCHAR separatorsW
[] = {',',' ','\t',0};
102 static int init_done
;
103 static struct loadorder_list env_list
;
106 /***************************************************************************
107 * cmp_sort_func (internal, static)
109 * Sorting and comparing function used in sort and search of loadorder
112 static int cmp_sort_func(const void *s1
, const void *s2
)
114 return strcmpiW(((module_loadorder_t
*)s1
)->modulename
, ((module_loadorder_t
*)s2
)->modulename
);
118 /***************************************************************************
121 static int strcmp_func(const void *s1
, const void *s2
)
123 return strcmpiW( (WCHAR
*)s1
, (WCHAR
*)s2
);
127 /***************************************************************************
130 * Return the base name of a file name (i.e. remove the path components).
132 static const WCHAR
*get_basename( const WCHAR
*name
)
136 if (name
[0] && name
[1] == ':') name
+= 2; /* strip drive specification */
137 if ((ptr
= strrchrW( name
, '\\' ))) name
= ptr
+ 1;
138 if ((ptr
= strrchrW( name
, '/' ))) name
= ptr
+ 1;
142 /***************************************************************************
145 * Remove extension if it is ".dll".
147 static inline void remove_dll_ext( WCHAR
*ext
)
150 toupperW(ext
[1]) == 'D' &&
151 toupperW(ext
[2]) == 'L' &&
152 toupperW(ext
[3]) == 'L' &&
157 /***************************************************************************
160 * Return a loadorder in printable form.
162 static const char *debugstr_loadorder( enum loadorder_type lo
[] )
165 char buffer
[LOADORDER_NTYPES
*3+1];
168 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
170 if (lo
[i
] == LOADORDER_INVALID
) break;
173 case LOADORDER_DLL
: strcat( buffer
, "n," ); break;
174 case LOADORDER_BI
: strcat( buffer
, "b," ); break;
175 default: strcat( buffer
, "?," ); break;
178 if (buffer
[0]) buffer
[strlen(buffer
)-1] = 0;
179 return debugstr_a(buffer
);
183 /***************************************************************************
186 * Append a load order to the list if necessary.
188 static void append_load_order(enum loadorder_type lo
[], enum loadorder_type append
)
192 for (i
= 0; i
< LOADORDER_NTYPES
; i
++)
194 if (lo
[i
] == LOADORDER_INVALID
) /* append it here */
197 lo
[i
] = LOADORDER_INVALID
;
200 if (lo
[i
] == append
) return; /* already in the list */
202 assert(0); /* cannot get here */
206 /***************************************************************************
209 * Parses the loadorder options from the configuration and puts it into
212 static void parse_load_order( const WCHAR
*order
, enum loadorder_type lo
[] )
214 lo
[0] = LOADORDER_INVALID
;
217 order
+= strspnW( order
, separatorsW
);
220 case 'N': /* Native */
222 append_load_order( lo
, LOADORDER_DLL
);
224 case 'B': /* Builtin */
226 append_load_order( lo
, LOADORDER_BI
);
229 order
+= strcspnW( order
, separatorsW
);
234 /***************************************************************************
237 * Adds an entry in the list of environment overrides.
239 static void add_load_order( const module_loadorder_t
*plo
)
243 for(i
= 0; i
< env_list
.count
; i
++)
245 if(!cmp_sort_func(plo
, &env_list
.order
[i
] ))
247 /* replace existing option */
248 memcpy( env_list
.order
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
253 if (i
>= env_list
.alloc
)
255 /* No space in current array, make it larger */
256 env_list
.alloc
+= LOADORDER_ALLOC_CLUSTER
;
258 env_list
.order
= RtlReAllocateHeap(GetProcessHeap(), 0, env_list
.order
,
259 env_list
.alloc
* sizeof(module_loadorder_t
));
261 env_list
.order
= RtlAllocateHeap(GetProcessHeap(), 0,
262 env_list
.alloc
* sizeof(module_loadorder_t
));
265 MESSAGE("Virtual memory exhausted\n");
269 memcpy(env_list
.order
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
270 env_list
.order
[i
].modulename
= plo
->modulename
;
275 /***************************************************************************
278 * Adds a set of entries in the list of command-line overrides from the key parameter.
280 static void add_load_order_set( WCHAR
*entry
)
282 module_loadorder_t ldo
;
283 WCHAR
*end
= strchrW( entry
, '=' );
287 parse_load_order( end
, ldo
.loadorder
);
291 entry
+= strspnW( entry
, separatorsW
);
292 end
= entry
+ strcspnW( entry
, separatorsW
);
293 if (*end
) *end
++ = 0;
296 WCHAR
*ext
= strrchrW(entry
, '.');
297 if (ext
) remove_dll_ext( ext
);
298 ldo
.modulename
= entry
;
299 add_load_order( &ldo
);
306 /***************************************************************************
309 static void init_load_order(void)
311 const char *order
= getenv( "WINEDLLOVERRIDES" );
318 if (!strcmp( order
, "help" ))
321 " WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
322 " where each entry is of the form:\n"
323 " module[,module...]={native|builtin}[,{b|n}]\n"
325 " Only the first letter of the override (native or builtin)\n"
326 " is significant.\n\n"
328 " WINEDLLOVERRIDES=\"comdlg32,commdlg=n,b;shell,shell32=b\"\n" );
332 RtlCreateUnicodeStringFromAsciiz( &strW
, order
);
336 while (*entry
&& *entry
== ';') entry
++;
338 next
= strchrW( entry
, ';' );
339 if (next
) *next
++ = 0;
340 else next
= entry
+ strlenW(entry
);
341 add_load_order_set( entry
);
345 /* sort the array for quick lookup */
347 qsort(env_list
.order
, env_list
.count
, sizeof(env_list
.order
[0]), cmp_sort_func
);
349 /* Note: we don't free the Unicode string because the
350 * stored module names point inside it */
354 /***************************************************************************
357 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
359 static inline BOOL
get_env_load_order( const WCHAR
*module
, enum loadorder_type lo
[] )
361 module_loadorder_t tmp
, *res
= NULL
;
363 tmp
.modulename
= module
;
364 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
365 if (env_list
.count
&&
366 (res
= bsearch(&tmp
, env_list
.order
, env_list
.count
, sizeof(env_list
.order
[0]), cmp_sort_func
)))
367 memcpy( lo
, res
->loadorder
, sizeof(res
->loadorder
) );
368 return (res
!= NULL
);
372 /***************************************************************************
373 * get_default_load_order
375 * Get the load order for a given module from the default list.
377 static inline BOOL
get_default_load_order( const WCHAR
*module
, enum loadorder_type lo
[] )
379 const int count
= sizeof(default_builtins
) / sizeof(default_builtins
[0]);
380 if (!bsearch( module
, default_builtins
, count
, sizeof(default_builtins
[0]), strcmp_func
))
382 lo
[0] = LOADORDER_BI
;
383 lo
[1] = LOADORDER_INVALID
;
388 /***************************************************************************
391 * Open the registry key to the app-specific DllOverrides list.
393 static HKEY
open_app_key( const WCHAR
*app_name
, const WCHAR
*module
)
395 OBJECT_ATTRIBUTES attr
;
396 UNICODE_STRING nameW
;
399 static const WCHAR AppDefaultsW
[] = {'M','a','c','h','i','n','e','\\',
400 'S','o','f','t','w','a','r','e','\\',
401 'W','i','n','e','\\',
402 'W','i','n','e','\\',
403 'C','o','n','f','i','g','\\',
404 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
405 static const WCHAR DllOverridesW
[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
407 str
= RtlAllocateHeap( GetProcessHeap(), 0,
408 sizeof(AppDefaultsW
) + sizeof(DllOverridesW
) +
409 strlenW(app_name
) * sizeof(WCHAR
) );
411 strcpyW( str
, AppDefaultsW
);
412 strcatW( str
, app_name
);
413 strcatW( str
, DllOverridesW
);
415 TRACE( "searching %s in %s\n", debugstr_w(module
), debugstr_w(str
) );
417 attr
.Length
= sizeof(attr
);
418 attr
.RootDirectory
= 0;
419 attr
.ObjectName
= &nameW
;
421 attr
.SecurityDescriptor
= NULL
;
422 attr
.SecurityQualityOfService
= NULL
;
423 RtlInitUnicodeString( &nameW
, str
);
425 if (NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) hkey
= 0;
426 RtlFreeHeap( GetProcessHeap(), 0, str
);
431 /***************************************************************************
434 * Load the registry loadorder value for a given module.
436 static BOOL
get_registry_value( HKEY hkey
, const WCHAR
*module
, enum loadorder_type lo
[] )
438 UNICODE_STRING valueW
;
443 RtlInitUnicodeString( &valueW
, module
);
445 if ((ret
= !NtQueryValueKey( hkey
, &valueW
, KeyValuePartialInformation
,
446 buffer
, sizeof(buffer
), &count
)))
449 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)buffer
)->Data
;
453 enum loadorder_type type
= LOADORDER_INVALID
;
455 while (*str
== ',' || isspaceW(*str
)) str
++;
458 switch(tolowerW(*str
))
460 case 'n': type
= LOADORDER_DLL
; break;
461 case 'b': type
= LOADORDER_BI
; break;
462 case 's': break; /* no longer supported, ignore */
463 case 0: break; /* end of string */
465 ERR("Invalid load order module-type %s, ignored\n", debugstr_w(str
));
468 if (type
!= LOADORDER_INVALID
)
470 for (i
= 0; i
< n
; i
++) if (lo
[i
] == type
) break; /* already specified */
471 if (i
== n
) lo
[n
++] = type
;
473 while (*str
&& *str
!= ',' && !isspaceW(*str
)) str
++;
475 lo
[n
] = LOADORDER_INVALID
;
481 /***************************************************************************
482 * MODULE_GetBuiltinPath
484 * Get the path of a builtin module when the native file does not exist.
486 BOOL
MODULE_GetBuiltinPath( const char *libname
, const char *ext
, char *filename
, UINT size
)
490 UINT len
= GetSystemDirectoryA( filename
, size
);
492 if (FILE_contains_path( libname
))
496 /* if the library name contains a path and can not be found,
498 * exception: if the path is the system directory, proceed,
499 * so that modules which are not PE modules can be loaded.
500 * If the library name does not contain a path and can not
501 * be found, assume the system directory is meant */
503 if (strlen(libname
) >= size
) return FALSE
; /* too long */
504 if (strchr( libname
, '/' )) /* need to convert slashes */
506 if (!(tmp
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(libname
)+1 ))) return FALSE
;
507 strcpy( tmp
, libname
);
508 for (p
= tmp
; *p
; p
++) if (*p
== '/') *p
= '\\';
510 else tmp
= (char *)libname
;
512 if (!FILE_strncasecmp( filename
, tmp
, len
) && tmp
[len
] == '\\')
514 strcpy( filename
, tmp
);
517 if (tmp
!= libname
) RtlFreeHeap( GetProcessHeap(), 0, tmp
);
518 if (!ret
) return FALSE
;
522 if (strlen(libname
) >= size
- len
- 1) return FALSE
;
523 filename
[len
] = '\\';
524 strcpy( filename
+len
+1, libname
);
527 /* if the filename doesn't have an extension, append the default */
528 if (!(p
= strrchr( filename
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
530 if (strlen(filename
) + strlen(ext
) >= size
) return FALSE
;
531 strcat( filename
, ext
);
537 /***************************************************************************
538 * MODULE_GetLoadOrderW (internal)
540 * Locate the loadorder of a module.
541 * Any path is stripped from the path-argument and so are the extension
542 * '.dll' and '.exe'. A lookup in the table can yield an override for
543 * the specific dll. Otherwise the default load order is returned.
545 void MODULE_GetLoadOrderW( enum loadorder_type loadorder
[], const WCHAR
*app_name
,
546 const WCHAR
*path
, BOOL win32
)
548 static const WCHAR DllOverridesW
[] = {'M','a','c','h','i','n','e','\\',
549 'S','o','f','t','w','a','r','e','\\',
550 'W','i','n','e','\\',
551 'W','i','n','e','\\',
552 'C','o','n','f','i','g','\\',
553 'D','l','l','O','v','e','r','r','i','d','e','s',0};
555 static HKEY std_key
= (HKEY
)-1; /* key to standard section, cached */
557 WCHAR
*module
, *basename
;
560 if (!init_done
) init_load_order();
562 TRACE("looking for %s\n", debugstr_w(path
));
564 loadorder
[0] = LOADORDER_INVALID
; /* in case something bad happens below */
566 /* Strip path information if the module resides in the system directory
567 * (path is already stripped by caller for 16-bit modules)
571 WCHAR sysdir
[MAX_PATH
+1];
572 UNICODE_STRING path_str
, sysdir_str
;
573 if (!GetSystemDirectoryW( sysdir
, MAX_PATH
)) return;
575 RtlInitUnicodeString( &path_str
, path
);
576 RtlInitUnicodeString( &sysdir_str
, sysdir
);
577 if (RtlPrefixUnicodeString( &sysdir_str
, &path_str
, TRUE
))
579 path
+= sysdir_str
.Length
/ sizeof(WCHAR
);
580 while (*path
== '\\' || *path
== '/') path
++;
584 if (!(len
= strlenW(path
))) return;
585 if (!(module
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 2) * sizeof(WCHAR
) ))) return;
586 strcpyW( module
+1, path
); /* reserve module[0] for the wildcard char */
588 if (len
>= 4) remove_dll_ext( module
+ 1 + len
- 4 );
590 /* check environment variable first */
591 if (get_env_load_order( module
+1, loadorder
))
593 TRACE( "got environment %s for %s\n",
594 debugstr_loadorder(loadorder
), debugstr_w(path
) );
598 /* then explicit module name in AppDefaults */
601 app_key
= open_app_key( app_name
, module
+1 );
602 if (app_key
&& get_registry_value( app_key
, module
+1, loadorder
))
604 TRACE( "got app defaults %s for %s\n",
605 debugstr_loadorder(loadorder
), debugstr_w(path
) );
610 /* then explicit module name in standard section */
611 if (std_key
== (HKEY
)-1)
613 OBJECT_ATTRIBUTES attr
;
614 UNICODE_STRING nameW
;
616 attr
.Length
= sizeof(attr
);
617 attr
.RootDirectory
= 0;
618 attr
.ObjectName
= &nameW
;
620 attr
.SecurityDescriptor
= NULL
;
621 attr
.SecurityQualityOfService
= NULL
;
622 RtlInitUnicodeString( &nameW
, DllOverridesW
);
624 if (NtOpenKey( &std_key
, KEY_ALL_ACCESS
, &attr
)) std_key
= 0;
627 if (std_key
&& get_registry_value( std_key
, module
+1, loadorder
))
629 TRACE( "got standard entry %s for %s\n",
630 debugstr_loadorder(loadorder
), debugstr_w(path
) );
634 /* then module basename preceded by '*' in AppDefaults */
635 basename
= (WCHAR
*)get_basename( module
+1 );
637 if (app_key
&& get_registry_value( app_key
, basename
-1, loadorder
))
639 TRACE( "got app defaults basename %s for %s\n",
640 debugstr_loadorder(loadorder
), debugstr_w(path
) );
644 /* then module name preceded by '*' in standard section */
645 if (std_key
&& get_registry_value( std_key
, basename
-1, loadorder
))
647 TRACE( "got standard base name %s for %s\n",
648 debugstr_loadorder(loadorder
), debugstr_w(path
) );
652 /* then base name matching compiled-in defaults */
653 if (get_default_load_order( basename
, loadorder
))
655 TRACE( "got compiled-in default %s for %s\n",
656 debugstr_loadorder(loadorder
), debugstr_w(path
) );
660 if (basename
== module
+1)
662 static const WCHAR wildcardW
[] = {'*',0};
664 /* then wildcard entry in AppDefaults (only if no explicit path) */
665 if (app_key
&& get_registry_value( app_key
, wildcardW
, loadorder
))
667 TRACE( "got app defaults wildcard %s for %s\n",
668 debugstr_loadorder(loadorder
), debugstr_w(path
) );
672 /* then wildcard entry in standard section (only if no explicit path) */
673 if (std_key
&& get_registry_value( std_key
, wildcardW
, loadorder
))
675 TRACE( "got standard wildcard %s for %s\n",
676 debugstr_loadorder(loadorder
), debugstr_w(path
) );
680 /* and last the hard-coded default */
681 memcpy( loadorder
, default_loadorder
, sizeof(default_loadorder
) );
682 TRACE( "got hardcoded default %s for %s\n",
683 debugstr_loadorder(loadorder
), debugstr_w(path
) );
685 else /* module contains an explicit path */
687 memcpy( loadorder
, default_path_loadorder
, sizeof(default_path_loadorder
) );
688 TRACE( "got hardcoded path default %s for %s\n",
689 debugstr_loadorder(loadorder
), debugstr_w(path
) );
693 if (app_key
) NtClose( app_key
);
694 RtlFreeHeap( GetProcessHeap(), 0, module
);
698 /***************************************************************************
699 * MODULE_GetLoadOrderA (internal)
701 * Locate the loadorder of a module.
702 * Any path is stripped from the path-argument and so are the extension
703 * '.dll' and '.exe'. A lookup in the table can yield an override for
704 * the specific dll. Otherwise the default load order is returned.
706 * FIXME: should be removed, everything should be Unicode.
708 void MODULE_GetLoadOrderA( enum loadorder_type loadorder
[], const WCHAR
*app_name
,
709 const char *path
, BOOL win32
)
711 UNICODE_STRING pathW
;
713 RtlCreateUnicodeStringFromAsciiz( &pathW
, path
);
714 MODULE_GetLoadOrderW( loadorder
, app_name
, pathW
.Buffer
, win32
);
715 RtlFreeUnicodeString( &pathW
);