user32: 0 vs. INFINITE timeout.
[wine.git] / dlls / ntdll / loadorder.c
blobac4dcb871cdb3315c6245ba39d54ad62810c1094
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
30 #include "windef.h"
31 #include "winternl.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;
45 } module_loadorder_t;
47 struct loadorder_list
49 int count;
50 int alloc;
51 module_loadorder_t *order;
54 /* dll to load as builtins if not explicitly specified otherwise */
55 /* the list must remain sorted by dll name */
56 static const WCHAR default_builtins[][10] =
58 { 'g','d','i','3','2',0 },
59 { 'i','c','m','p',0 },
60 { 'k','e','r','n','e','l','3','2',0 },
61 { 'n','t','d','l','l',0 },
62 { 'o','d','b','c','3','2',0 },
63 { 't','t','y','d','r','v',0 },
64 { 'u','s','e','r','3','2',0 },
65 { 'w','3','2','s','k','r','n','l',0 },
66 { 'w','i','n','e','d','o','s',0 },
67 { 'w','i','n','e','p','s',0 },
68 { 'w','i','n','m','m',0 },
69 { 'w','n','a','s','p','i','3','2',0 },
70 { 'w','o','w','3','2',0 },
71 { 'w','s','2','_','3','2',0 },
72 { 'w','s','o','c','k','3','2',0 },
73 { 'x','1','1','d','r','v',0 }
76 static const WCHAR separatorsW[] = {',',' ','\t',0};
78 static int init_done;
79 static struct loadorder_list env_list;
82 /***************************************************************************
83 * cmp_sort_func (internal, static)
85 * Sorting and comparing function used in sort and search of loadorder
86 * entries.
88 static int cmp_sort_func(const void *s1, const void *s2)
90 return strcmpiW(((const module_loadorder_t *)s1)->modulename, ((const module_loadorder_t *)s2)->modulename);
94 /***************************************************************************
95 * strcmp_func
97 static int strcmp_func(const void *s1, const void *s2)
99 return strcmpiW( (const WCHAR *)s1, (const WCHAR *)s2 );
103 /***************************************************************************
104 * get_basename
106 * Return the base name of a file name (i.e. remove the path components).
108 static const WCHAR *get_basename( const WCHAR *name )
110 const WCHAR *ptr;
112 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
113 if ((ptr = strrchrW( name, '\\' ))) name = ptr + 1;
114 if ((ptr = strrchrW( name, '/' ))) name = ptr + 1;
115 return name;
118 /***************************************************************************
119 * remove_dll_ext
121 * Remove extension if it is ".dll".
123 static inline void remove_dll_ext( WCHAR *ext )
125 if (ext[0] == '.' &&
126 toupperW(ext[1]) == 'D' &&
127 toupperW(ext[2]) == 'L' &&
128 toupperW(ext[3]) == 'L' &&
129 !ext[4]) ext[0] = 0;
133 /***************************************************************************
134 * debugstr_loadorder
136 * Return a loadorder in printable form.
138 static const char *debugstr_loadorder( enum loadorder lo )
140 switch(lo)
142 case LO_DISABLED: return "";
143 case LO_NATIVE: return "n";
144 case LO_BUILTIN: return "b";
145 case LO_NATIVE_BUILTIN: return "n,b";
146 case LO_BUILTIN_NATIVE: return "b,n";
147 case LO_DEFAULT: return "default";
148 default: return "??";
153 /***************************************************************************
154 * parse_load_order
156 * Parses the loadorder options from the configuration and puts it into
157 * a structure.
159 static enum loadorder parse_load_order( const WCHAR *order )
161 enum loadorder ret = LO_DISABLED;
163 while (*order)
165 order += strspnW( order, separatorsW );
166 switch(*order)
168 case 'N': /* native */
169 case 'n':
170 if (ret == LO_DISABLED) ret = LO_NATIVE;
171 else if (ret == LO_BUILTIN) return LO_BUILTIN_NATIVE;
172 break;
173 case 'B': /* builtin */
174 case 'b':
175 if (ret == LO_DISABLED) ret = LO_BUILTIN;
176 else if (ret == LO_NATIVE) return LO_NATIVE_BUILTIN;
177 break;
179 order += strcspnW( order, separatorsW );
181 return ret;
185 /***************************************************************************
186 * add_load_order
188 * Adds an entry in the list of environment overrides.
190 static void add_load_order( const module_loadorder_t *plo )
192 int i;
194 for(i = 0; i < env_list.count; i++)
196 if(!cmp_sort_func(plo, &env_list.order[i] ))
198 /* replace existing option */
199 env_list.order[i].loadorder = plo->loadorder;
200 return;
204 if (i >= env_list.alloc)
206 /* No space in current array, make it larger */
207 env_list.alloc += LOADORDER_ALLOC_CLUSTER;
208 if (env_list.order)
209 env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
210 env_list.alloc * sizeof(module_loadorder_t));
211 else
212 env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
213 env_list.alloc * sizeof(module_loadorder_t));
214 if(!env_list.order)
216 MESSAGE("Virtual memory exhausted\n");
217 exit(1);
220 env_list.order[i].loadorder = plo->loadorder;
221 env_list.order[i].modulename = plo->modulename;
222 env_list.count++;
226 /***************************************************************************
227 * add_load_order_set
229 * Adds a set of entries in the list of command-line overrides from the key parameter.
231 static void add_load_order_set( WCHAR *entry )
233 module_loadorder_t ldo;
234 WCHAR *end = strchrW( entry, '=' );
236 if (!end) return;
237 *end++ = 0;
238 ldo.loadorder = parse_load_order( end );
240 while (*entry)
242 entry += strspnW( entry, separatorsW );
243 end = entry + strcspnW( entry, separatorsW );
244 if (*end) *end++ = 0;
245 if (*entry)
247 WCHAR *ext = strrchrW(entry, '.');
248 if (ext) remove_dll_ext( ext );
249 ldo.modulename = entry;
250 add_load_order( &ldo );
251 entry = end;
257 /***************************************************************************
258 * init_load_order
260 static void init_load_order(void)
262 const char *order = getenv( "WINEDLLOVERRIDES" );
263 UNICODE_STRING strW;
264 WCHAR *entry, *next;
266 init_done = 1;
267 if (!order) return;
269 if (!strcmp( order, "help" ))
271 MESSAGE( "Syntax:\n"
272 " WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
273 " where each entry is of the form:\n"
274 " module[,module...]={native|builtin}[,{b|n}]\n"
275 "\n"
276 " Only the first letter of the override (native or builtin)\n"
277 " is significant.\n\n"
278 "Example:\n"
279 " WINEDLLOVERRIDES=\"comdlg32=n,b;shell32,shlwapi=b\"\n" );
280 exit(0);
283 RtlCreateUnicodeStringFromAsciiz( &strW, order );
284 entry = strW.Buffer;
285 while (*entry)
287 while (*entry && *entry == ';') entry++;
288 if (!*entry) break;
289 next = strchrW( entry, ';' );
290 if (next) *next++ = 0;
291 else next = entry + strlenW(entry);
292 add_load_order_set( entry );
293 entry = next;
296 /* sort the array for quick lookup */
297 if (env_list.count)
298 qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
300 /* Note: we don't free the Unicode string because the
301 * stored module names point inside it */
305 /***************************************************************************
306 * get_env_load_order
308 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
310 static inline enum loadorder get_env_load_order( const WCHAR *module )
312 module_loadorder_t tmp, *res;
314 tmp.modulename = module;
315 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
316 if (env_list.count &&
317 (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
318 return res->loadorder;
319 return LO_INVALID;
323 /***************************************************************************
324 * get_default_load_order
326 * Get the load order for a given module from the default list.
328 static inline enum loadorder get_default_load_order( const WCHAR *module )
330 const int count = sizeof(default_builtins) / sizeof(default_builtins[0]);
331 if (!bsearch( module, default_builtins, count, sizeof(default_builtins[0]), strcmp_func ))
332 return FALSE;
333 TRACE( "got compiled-in builtin default for %s\n", debugstr_w(module) );
334 return LO_BUILTIN;
338 /***************************************************************************
339 * get_standard_key
341 * Return a handle to the standard DllOverrides registry section.
343 static HANDLE get_standard_key(void)
345 static const WCHAR DllOverridesW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
346 'D','l','l','O','v','e','r','r','i','d','e','s',0};
347 static HANDLE std_key = (HANDLE)-1;
349 if (std_key == (HANDLE)-1)
351 OBJECT_ATTRIBUTES attr;
352 UNICODE_STRING nameW;
353 HANDLE root;
355 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
356 attr.Length = sizeof(attr);
357 attr.RootDirectory = root;
358 attr.ObjectName = &nameW;
359 attr.Attributes = 0;
360 attr.SecurityDescriptor = NULL;
361 attr.SecurityQualityOfService = NULL;
362 RtlInitUnicodeString( &nameW, DllOverridesW );
364 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
365 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
366 NtClose( root );
368 return std_key;
372 /***************************************************************************
373 * get_app_key
375 * Get the registry key for the app-specific DllOverrides list.
377 static HANDLE get_app_key( const WCHAR *app_name )
379 OBJECT_ATTRIBUTES attr;
380 UNICODE_STRING nameW;
381 HANDLE root;
382 WCHAR *str;
383 static const WCHAR AppDefaultsW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
384 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
385 static const WCHAR DllOverridesW[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
386 static HANDLE app_key = (HANDLE)-1;
388 if (app_key != (HANDLE)-1) return app_key;
390 str = RtlAllocateHeap( GetProcessHeap(), 0,
391 sizeof(AppDefaultsW) + sizeof(DllOverridesW) +
392 strlenW(app_name) * sizeof(WCHAR) );
393 if (!str) return 0;
394 strcpyW( str, AppDefaultsW );
395 strcatW( str, app_name );
396 strcatW( str, DllOverridesW );
398 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
399 attr.Length = sizeof(attr);
400 attr.RootDirectory = root;
401 attr.ObjectName = &nameW;
402 attr.Attributes = 0;
403 attr.SecurityDescriptor = NULL;
404 attr.SecurityQualityOfService = NULL;
405 RtlInitUnicodeString( &nameW, str );
407 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
408 if (NtOpenKey( &app_key, KEY_ALL_ACCESS, &attr )) app_key = 0;
409 NtClose( root );
410 RtlFreeHeap( GetProcessHeap(), 0, str );
411 return app_key;
415 /***************************************************************************
416 * get_registry_value
418 * Load the registry loadorder value for a given module.
420 static enum loadorder get_registry_value( HANDLE hkey, const WCHAR *module )
422 UNICODE_STRING valueW;
423 char buffer[80];
424 DWORD count;
425 BOOL ret;
427 RtlInitUnicodeString( &valueW, module );
429 if ((ret = !NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
430 buffer, sizeof(buffer), &count )))
432 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
433 return parse_load_order( str );
435 return LO_INVALID;
439 /***************************************************************************
440 * get_load_order_value
442 * Get the load order for the exact specified module string, looking in:
443 * 1. The WINEDLLOVERRIDES environment variable
444 * 2. The per-application DllOverrides key
445 * 3. The standard DllOverrides key
447 static enum loadorder get_load_order_value( HANDLE std_key, HANDLE app_key, const WCHAR *module )
449 enum loadorder ret;
451 if ((ret = get_env_load_order( module )) != LO_INVALID)
453 TRACE( "got environment %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
454 return ret;
457 if (app_key && ((ret = get_registry_value( app_key, module )) != LO_INVALID))
459 TRACE( "got app defaults %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
460 return ret;
463 if (std_key && ((ret = get_registry_value( std_key, module )) != LO_INVALID))
465 TRACE( "got standard key %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
466 return ret;
469 return ret;
473 /***************************************************************************
474 * get_load_order (internal)
476 * Return the loadorder of a module.
477 * The system directory and '.dll' extension is stripped from the path.
479 enum loadorder get_load_order( const WCHAR *app_name, const WCHAR *path )
481 static const WCHAR wildcardW[] = {'*',0};
483 enum loadorder ret = LO_INVALID;
484 HANDLE std_key, app_key = 0;
485 WCHAR *module, *basename;
486 UNICODE_STRING path_str;
487 int len;
489 if (!init_done) init_load_order();
490 std_key = get_standard_key();
491 if (app_name) app_key = get_app_key( app_name );
493 TRACE("looking for %s\n", debugstr_w(path));
495 /* Strip path information if the module resides in the system directory
497 RtlInitUnicodeString( &path_str, path );
498 if (RtlPrefixUnicodeString( &system_dir, &path_str, TRUE ))
500 const WCHAR *p = path + system_dir.Length / sizeof(WCHAR);
501 while (*p == '\\' || *p == '/') p++;
502 if (!strchrW( p, '\\' ) && !strchrW( p, '/' )) path = p;
505 if (!(len = strlenW(path))) return ret;
506 if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return ret;
507 strcpyW( module+1, path ); /* reserve module[0] for the wildcard char */
508 basename = (WCHAR *)get_basename( module+1 );
510 if (len >= 4) remove_dll_ext( module + 1 + len - 4 );
512 /* first explicit module name */
513 if ((ret = get_load_order_value( std_key, app_key, module+1 )) != LO_INVALID)
514 goto done;
516 /* then module basename preceded by '*' */
517 basename[-1] = '*';
518 if ((ret = get_load_order_value( std_key, app_key, basename-1 )) != LO_INVALID)
519 goto done;
521 /* then module basename without '*' (only if explicit path) */
522 if (basename != module+1 && ((ret = get_load_order_value( std_key, app_key, basename )) != LO_INVALID))
523 goto done;
525 /* then compiled-in defaults */
526 if ((ret = get_default_load_order( basename )) != LO_INVALID)
527 goto done;
529 /* then wildcard entry (only if no explicit path) */
530 if (basename == module+1 && ((ret = get_load_order_value( std_key, app_key, wildcardW )) != LO_INVALID))
531 goto done;
533 /* and last the hard-coded default */
534 ret = LO_DEFAULT;
535 TRACE( "got hardcoded default %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
537 done:
538 RtlFreeHeap( GetProcessHeap(), 0, module );
539 return ret;