windowscodecs: Add stub implementation of IWICImagingFactory.
[wine.git] / libs / wine / loader.c
blobd10a6e0b7f466b53493f12bb7a5b7dbb465b3e35
1 /*
2 * Win32 builtin dlls support
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wine/library.h"
48 #ifdef __APPLE__
49 #include <crt_externs.h>
50 #define environ (*_NSGetEnviron())
51 #else
52 extern char **environ;
53 #endif
55 /* argc/argv for the Windows application */
56 int __wine_main_argc = 0;
57 char **__wine_main_argv = NULL;
58 WCHAR **__wine_main_wargv = NULL;
59 char **__wine_main_environ = NULL;
61 struct dll_path_context
63 unsigned int index; /* current index in the dll path list */
64 char *buffer; /* buffer used for storing path names */
65 char *name; /* start of file name part in buffer (including leading slash) */
66 int namelen; /* length of file name without .so extension */
67 int win16; /* 16-bit dll search */
70 #define MAX_DLLS 100
72 static struct
74 const IMAGE_NT_HEADERS *nt; /* NT header */
75 const char *filename; /* DLL file name */
76 } builtin_dlls[MAX_DLLS];
78 static int nb_dlls;
80 static const IMAGE_NT_HEADERS *main_exe;
82 static load_dll_callback_t load_dll_callback;
84 static const char *build_dir;
85 static const char *default_dlldir;
86 static const char **dll_paths;
87 static unsigned int nb_dll_paths;
88 static int dll_path_maxlen;
90 extern void mmap_init(void);
91 extern const char *get_dlldir( const char **default_dlldir );
93 /* build the dll load path from the WINEDLLPATH variable */
94 static void build_dll_path(void)
96 int len, count = 0;
97 char *p, *path = getenv( "WINEDLLPATH" );
98 const char *dlldir = get_dlldir( &default_dlldir );
100 if (path)
102 /* count how many path elements we need */
103 path = strdup(path);
104 p = path;
105 while (*p)
107 while (*p == ':') p++;
108 if (!*p) break;
109 count++;
110 while (*p && *p != ':') p++;
114 dll_paths = malloc( (count+2) * sizeof(*dll_paths) );
115 nb_dll_paths = 0;
117 if (dlldir)
119 dll_path_maxlen = strlen(dlldir);
120 dll_paths[nb_dll_paths++] = dlldir;
122 else if ((build_dir = wine_get_build_dir()))
124 dll_path_maxlen = strlen(build_dir) + sizeof("/programs");
127 if (count)
129 p = path;
130 while (*p)
132 while (*p == ':') *p++ = 0;
133 if (!*p) break;
134 dll_paths[nb_dll_paths] = p;
135 while (*p && *p != ':') p++;
136 if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
137 dll_path_maxlen = p - dll_paths[nb_dll_paths];
138 nb_dll_paths++;
142 /* append default dll dir (if not empty) to path */
143 if ((len = strlen(default_dlldir)) > 0)
145 if (len > dll_path_maxlen) dll_path_maxlen = len;
146 dll_paths[nb_dll_paths++] = default_dlldir;
150 /* check if a given file can be opened */
151 static inline int file_exists( const char *name )
153 int fd = open( name, O_RDONLY );
154 if (fd != -1) close( fd );
155 return (fd != -1);
158 static inline char *prepend( char *buffer, const char *str, size_t len )
160 return memcpy( buffer - len, str, len );
163 /* get a filename from the next entry in the dll path */
164 static char *next_dll_path( struct dll_path_context *context )
166 unsigned int index = context->index++;
167 int namelen = context->namelen;
168 char *path = context->name;
170 switch(index)
172 case 0: /* try dlls dir with subdir prefix */
173 if (namelen > 4 && !memcmp( context->name + namelen - 4, ".dll", 4 )) namelen -= 4;
174 if (!context->win16) path = prepend( path, context->name, namelen );
175 path = prepend( path, "/dlls", sizeof("/dlls") - 1 );
176 path = prepend( path, build_dir, strlen(build_dir) );
177 return path;
178 case 1: /* try programs dir with subdir prefix */
179 if (!context->win16)
181 if (namelen > 4 && !memcmp( context->name + namelen - 4, ".exe", 4 )) namelen -= 4;
182 path = prepend( path, context->name, namelen );
183 path = prepend( path, "/programs", sizeof("/programs") - 1 );
184 path = prepend( path, build_dir, strlen(build_dir) );
185 return path;
187 context->index++;
188 /* fall through */
189 default:
190 index -= 2;
191 if (index < nb_dll_paths)
192 return prepend( context->name, dll_paths[index], strlen( dll_paths[index] ));
193 break;
195 return NULL;
199 /* get a filename from the first entry in the dll path */
200 static char *first_dll_path( const char *name, int win16, struct dll_path_context *context )
202 char *p;
203 int namelen = strlen( name );
204 const char *ext = win16 ? "16" : ".so";
206 context->buffer = malloc( dll_path_maxlen + 2 * namelen + strlen(ext) + 3 );
207 context->index = build_dir ? 0 : 2; /* if no build dir skip all the build dir magic cases */
208 context->name = context->buffer + dll_path_maxlen + namelen + 1;
209 context->namelen = namelen + 1;
210 context->win16 = win16;
212 /* store the name at the end of the buffer, followed by extension */
213 p = context->name;
214 *p++ = '/';
215 memcpy( p, name, namelen );
216 strcpy( p + namelen, ext );
217 return next_dll_path( context );
221 /* free the dll path context created by first_dll_path */
222 static inline void free_dll_path( struct dll_path_context *context )
224 free( context->buffer );
228 /* open a library for a given dll, searching in the dll path
229 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
230 static void *dlopen_dll( const char *name, char *error, int errorsize,
231 int test_only, int *exists )
233 struct dll_path_context context;
234 char *path;
235 void *ret = NULL;
237 *exists = 0;
238 for (path = first_dll_path( name, 0, &context ); path; path = next_dll_path( &context ))
240 if (!test_only && (ret = wine_dlopen( path, RTLD_NOW, error, errorsize ))) break;
241 if ((*exists = file_exists( path ))) break; /* exists but cannot be loaded, return the error */
243 free_dll_path( &context );
244 return ret;
248 /* adjust an array of pointers to make them into RVAs */
249 static inline void fixup_rva_ptrs( void *array, BYTE *base, unsigned int count )
251 void **src = (void **)array;
252 DWORD *dst = (DWORD *)array;
253 while (count--)
255 *dst++ = *src ? (BYTE *)*src - base : 0;
256 src++;
260 /* fixup an array of RVAs by adding the specified delta */
261 static inline void fixup_rva_dwords( DWORD *ptr, int delta, unsigned int count )
263 while (count--)
265 if (*ptr) *ptr += delta;
266 ptr++;
271 /* fixup RVAs in the import directory */
272 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, BYTE *base, int delta )
274 UINT_PTR *ptr;
276 while (dir->Name)
278 fixup_rva_dwords( &dir->u.OriginalFirstThunk, delta, 1 );
279 fixup_rva_dwords( &dir->Name, delta, 1 );
280 fixup_rva_dwords( &dir->FirstThunk, delta, 1 );
281 ptr = (UINT_PTR *)(base + (dir->u.OriginalFirstThunk ? dir->u.OriginalFirstThunk : dir->FirstThunk));
282 while (*ptr)
284 if (!(*ptr & IMAGE_ORDINAL_FLAG)) *ptr += delta;
285 ptr++;
287 dir++;
292 /* fixup RVAs in the export directory */
293 static void fixup_exports( IMAGE_EXPORT_DIRECTORY *dir, BYTE *base, int delta )
295 fixup_rva_dwords( &dir->Name, delta, 1 );
296 fixup_rva_dwords( &dir->AddressOfFunctions, delta, 1 );
297 fixup_rva_dwords( &dir->AddressOfNames, delta, 1 );
298 fixup_rva_dwords( &dir->AddressOfNameOrdinals, delta, 1 );
299 fixup_rva_dwords( (DWORD *)(base + dir->AddressOfNames), delta, dir->NumberOfNames );
300 fixup_rva_ptrs( (base + dir->AddressOfFunctions), base, dir->NumberOfFunctions );
304 /* fixup RVAs in the resource directory */
305 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, BYTE *root, int delta )
307 IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
308 int i;
310 entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
311 for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
313 void *ptr = root + entry->u2.s3.OffsetToDirectory;
314 if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, delta );
315 else
317 IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
318 fixup_rva_dwords( &data->OffsetToData, delta, 1 );
324 /* map a builtin dll in memory and fixup RVAs */
325 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
327 #ifdef HAVE_MMAP
328 IMAGE_DATA_DIRECTORY *dir;
329 IMAGE_DOS_HEADER *dos;
330 IMAGE_NT_HEADERS *nt;
331 IMAGE_SECTION_HEADER *sec;
332 BYTE *addr;
333 DWORD code_start, data_start, data_end;
334 const size_t page_size = getpagesize();
335 const size_t page_mask = page_size - 1;
336 int delta, nb_sections = 2; /* code + data */
337 unsigned int i;
339 size_t size = (sizeof(IMAGE_DOS_HEADER)
340 + sizeof(IMAGE_NT_HEADERS)
341 + nb_sections * sizeof(IMAGE_SECTION_HEADER));
343 assert( size <= page_size );
345 /* module address must be aligned on 64K boundary */
346 addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
347 if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
349 dos = (IMAGE_DOS_HEADER *)addr;
350 nt = (IMAGE_NT_HEADERS *)(dos + 1);
351 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
353 /* Build the DOS and NT headers */
355 dos->e_magic = IMAGE_DOS_SIGNATURE;
356 dos->e_cblp = 0x90;
357 dos->e_cp = 3;
358 dos->e_cparhdr = (sizeof(*dos)+0xf)/0x10;
359 dos->e_minalloc = 0;
360 dos->e_maxalloc = 0xffff;
361 dos->e_ss = 0x0000;
362 dos->e_sp = 0x00b8;
363 dos->e_lfarlc = sizeof(*dos);
364 dos->e_lfanew = sizeof(*dos);
366 *nt = *nt_descr;
368 delta = (const BYTE *)nt_descr - addr;
369 code_start = page_size;
370 data_start = delta & ~page_mask;
371 data_end = (nt->OptionalHeader.SizeOfImage + delta + page_mask) & ~page_mask;
373 fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
375 nt->FileHeader.NumberOfSections = nb_sections;
376 nt->OptionalHeader.BaseOfCode = code_start;
377 #ifndef _WIN64
378 nt->OptionalHeader.BaseOfData = data_start;
379 #endif
380 nt->OptionalHeader.SizeOfCode = data_start - code_start;
381 nt->OptionalHeader.SizeOfInitializedData = data_end - data_start;
382 nt->OptionalHeader.SizeOfUninitializedData = 0;
383 nt->OptionalHeader.SizeOfImage = data_end;
384 nt->OptionalHeader.ImageBase = (ULONG_PTR)addr;
386 /* Build the code section */
388 memcpy( sec->Name, ".text", sizeof(".text") );
389 sec->SizeOfRawData = data_start - code_start;
390 sec->Misc.VirtualSize = sec->SizeOfRawData;
391 sec->VirtualAddress = code_start;
392 sec->PointerToRawData = code_start;
393 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
394 sec++;
396 /* Build the data section */
398 memcpy( sec->Name, ".data", sizeof(".data") );
399 sec->SizeOfRawData = data_end - data_start;
400 sec->Misc.VirtualSize = sec->SizeOfRawData;
401 sec->VirtualAddress = data_start;
402 sec->PointerToRawData = data_start;
403 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
404 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
405 sec++;
407 for (i = 0; i < nt->OptionalHeader.NumberOfRvaAndSizes; i++)
408 fixup_rva_dwords( &nt->OptionalHeader.DataDirectory[i].VirtualAddress, delta, 1 );
410 /* Build the import directory */
412 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
413 if (dir->Size)
415 IMAGE_IMPORT_DESCRIPTOR *imports = (void *)(addr + dir->VirtualAddress);
416 fixup_imports( imports, addr, delta );
419 /* Build the resource directory */
421 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
422 if (dir->Size)
424 void *ptr = (void *)(addr + dir->VirtualAddress);
425 fixup_resources( ptr, ptr, delta );
428 /* Build the export directory */
430 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
431 if (dir->Size)
433 IMAGE_EXPORT_DIRECTORY *exports = (void *)(addr + dir->VirtualAddress);
434 fixup_exports( exports, addr, delta );
436 return addr;
437 #else /* HAVE_MMAP */
438 return NULL;
439 #endif /* HAVE_MMAP */
443 /***********************************************************************
444 * __wine_get_main_environment
446 * Return an environment pointer to work around lack of environ variable.
447 * Only exported on Mac OS.
449 char **__wine_get_main_environment(void)
451 return environ;
455 /***********************************************************************
456 * __wine_dll_register
458 * Register a built-in DLL descriptor.
460 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
462 if (load_dll_callback) load_dll_callback( map_dll(header), filename );
463 else
465 if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
466 main_exe = header;
467 else
469 assert( nb_dlls < MAX_DLLS );
470 builtin_dlls[nb_dlls].nt = header;
471 builtin_dlls[nb_dlls].filename = filename;
472 nb_dlls++;
478 /***********************************************************************
479 * wine_dll_set_callback
481 * Set the callback function for dll loading, and call it
482 * for all dlls that were implicitly loaded already.
484 void wine_dll_set_callback( load_dll_callback_t load )
486 int i;
487 load_dll_callback = load;
488 for (i = 0; i < nb_dlls; i++)
490 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
491 if (!nt) continue;
492 builtin_dlls[i].nt = NULL;
493 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
495 nb_dlls = 0;
496 if (main_exe) load_dll_callback( map_dll(main_exe), "" );
500 /***********************************************************************
501 * wine_dll_load
503 * Load a builtin dll.
505 void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists )
507 int i;
509 /* callback must have been set already */
510 assert( load_dll_callback );
512 /* check if we have it in the list */
513 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
514 for (i = 0; i < nb_dlls; i++)
516 if (!builtin_dlls[i].nt) continue;
517 if (!strcmp( builtin_dlls[i].filename, filename ))
519 const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
520 builtin_dlls[i].nt = NULL;
521 load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
522 *file_exists = 1;
523 return (void *)1;
526 return dlopen_dll( filename, error, errorsize, 0, file_exists );
530 /***********************************************************************
531 * wine_dll_unload
533 * Unload a builtin dll.
535 void wine_dll_unload( void *handle )
537 if (handle != (void *)1)
538 wine_dlclose( handle, NULL, 0 );
542 /***********************************************************************
543 * wine_dll_load_main_exe
545 * Try to load the .so for the main exe.
547 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
548 int test_only, int *file_exists )
550 return dlopen_dll( name, error, errorsize, test_only, file_exists );
554 /***********************************************************************
555 * wine_dll_enum_load_path
557 * Enumerate the dll load path.
559 const char *wine_dll_enum_load_path( unsigned int index )
561 if (index >= nb_dll_paths) return NULL;
562 return dll_paths[index];
566 /***********************************************************************
567 * wine_dll_get_owner
569 * Retrieve the name of the 32-bit owner dll for a 16-bit dll.
570 * Return 0 if OK, -1 on error.
572 int wine_dll_get_owner( const char *name, char *buffer, int size, int *exists )
574 int ret = -1;
575 char *path;
576 struct dll_path_context context;
578 *exists = 0;
580 for (path = first_dll_path( name, 1, &context ); path; path = next_dll_path( &context ))
582 int fd = open( path, O_RDONLY );
583 if (fd != -1)
585 int res = read( fd, buffer, size - 1 );
586 while (res > 0 && (buffer[res-1] == '\n' || buffer[res-1] == '\r')) res--;
587 buffer[res] = 0;
588 close( fd );
589 *exists = 1;
590 ret = 0;
591 break;
594 free_dll_path( &context );
595 return ret;
599 /***********************************************************************
600 * set_max_limit
602 * Set a user limit to the maximum allowed value.
604 static void set_max_limit( int limit )
606 #ifdef HAVE_SETRLIMIT
607 struct rlimit rlimit;
609 if (!getrlimit( limit, &rlimit ))
611 rlimit.rlim_cur = rlimit.rlim_max;
612 if (setrlimit( limit, &rlimit ) != 0)
614 #if defined(__APPLE__) && defined(RLIMIT_NOFILE) && defined(OPEN_MAX)
615 /* On Leopard, setrlimit(RLIMIT_NOFILE, ...) fails on attempts to set
616 * rlim_cur above OPEN_MAX (even if rlim_max > OPEN_MAX). */
617 if (limit == RLIMIT_NOFILE && rlimit.rlim_cur > OPEN_MAX)
619 rlimit.rlim_cur = OPEN_MAX;
620 setrlimit( limit, &rlimit );
622 #endif
625 #endif
629 /***********************************************************************
630 * wine_init
632 * Main Wine initialisation.
634 void wine_init( int argc, char *argv[], char *error, int error_size )
636 struct dll_path_context context;
637 char *path;
638 void *ntdll = NULL;
639 void (*init_func)(void);
641 /* force a few limits that are set too low on some platforms */
642 #ifdef RLIMIT_NOFILE
643 set_max_limit( RLIMIT_NOFILE );
644 #endif
645 #ifdef RLIMIT_AS
646 set_max_limit( RLIMIT_AS );
647 #endif
649 wine_init_argv0_path( argv[0] );
650 build_dll_path();
651 __wine_main_argc = argc;
652 __wine_main_argv = argv;
653 __wine_main_environ = __wine_get_main_environment();
654 mmap_init();
656 for (path = first_dll_path( "ntdll.dll", 0, &context ); path; path = next_dll_path( &context ))
658 if ((ntdll = wine_dlopen( path, RTLD_NOW, error, error_size )))
660 /* if we didn't use the default dll dir, remove it from the search path */
661 if (default_dlldir[0] && context.index < nb_dll_paths + 2) nb_dll_paths--;
662 break;
665 free_dll_path( &context );
667 if (!ntdll) return;
668 if (!(init_func = wine_dlsym( ntdll, "__wine_process_init", error, error_size ))) return;
669 init_func();
674 * These functions provide wrappers around dlopen() and associated
675 * functions. They work around a bug in glibc 2.1.x where calling
676 * a dl*() function after a previous dl*() function has failed
677 * without a dlerror() call between the two will cause a crash.
678 * They all take a pointer to a buffer that
679 * will receive the error description (from dlerror()). This
680 * parameter may be NULL if the error description is not required.
683 #ifndef RTLD_FIRST
684 #define RTLD_FIRST 0
685 #endif
687 /***********************************************************************
688 * wine_dlopen
690 void *wine_dlopen( const char *filename, int flag, char *error, size_t errorsize )
692 #ifdef HAVE_DLOPEN
693 void *ret;
694 const char *s;
696 #ifdef __APPLE__
697 /* the Mac OS loader pretends to be able to load PE files, so avoid them here */
698 unsigned char magic[2];
699 int fd = open( filename, O_RDONLY );
700 if (fd != -1)
702 if (pread( fd, magic, 2, 0 ) == 2 && magic[0] == 'M' && magic[1] == 'Z')
704 static const char msg[] = "MZ format";
705 size_t len = min( errorsize, sizeof(msg) );
706 memcpy( error, msg, len );
707 error[len - 1] = 0;
708 close( fd );
709 return NULL;
711 close( fd );
713 #endif
714 dlerror(); dlerror();
715 #ifdef __sun
716 if (strchr( filename, ':' ))
718 char path[PATH_MAX];
719 /* Solaris' brain damaged dlopen() treats ':' as a path separator */
720 realpath( filename, path );
721 ret = dlopen( path, flag | RTLD_FIRST );
723 else
724 #endif
725 ret = dlopen( filename, flag | RTLD_FIRST );
726 s = dlerror();
727 if (error && errorsize)
729 if (s)
731 size_t len = strlen(s);
732 if (len >= errorsize) len = errorsize - 1;
733 memcpy( error, s, len );
734 error[len] = 0;
736 else error[0] = 0;
738 dlerror();
739 return ret;
740 #else
741 if (error)
743 static const char msg[] = "dlopen interface not detected by configure";
744 size_t len = min( errorsize, sizeof(msg) );
745 memcpy( error, msg, len );
746 error[len - 1] = 0;
748 return NULL;
749 #endif
752 /***********************************************************************
753 * wine_dlsym
755 void *wine_dlsym( void *handle, const char *symbol, char *error, size_t errorsize )
757 #ifdef HAVE_DLOPEN
758 void *ret;
759 const char *s;
760 dlerror(); dlerror();
761 ret = dlsym( handle, symbol );
762 s = dlerror();
763 if (error && errorsize)
765 if (s)
767 size_t len = strlen(s);
768 if (len >= errorsize) len = errorsize - 1;
769 memcpy( error, s, len );
770 error[len] = 0;
772 else error[0] = 0;
774 dlerror();
775 return ret;
776 #else
777 if (error)
779 static const char msg[] = "dlopen interface not detected by configure";
780 size_t len = min( errorsize, sizeof(msg) );
781 memcpy( error, msg, len );
782 error[len - 1] = 0;
784 return NULL;
785 #endif
788 /***********************************************************************
789 * wine_dlclose
791 int wine_dlclose( void *handle, char *error, size_t errorsize )
793 #ifdef HAVE_DLOPEN
794 int ret;
795 const char *s;
796 dlerror(); dlerror();
797 ret = dlclose( handle );
798 s = dlerror();
799 if (error && errorsize)
801 if (s)
803 size_t len = strlen(s);
804 if (len >= errorsize) len = errorsize - 1;
805 memcpy( error, s, len );
806 error[len] = 0;
808 else error[0] = 0;
810 dlerror();
811 return ret;
812 #else
813 if (error)
815 static const char msg[] = "dlopen interface not detected by configure";
816 size_t len = min( errorsize, sizeof(msg) );
817 memcpy( error, msg, len );
818 error[len - 1] = 0;
820 return 1;
821 #endif