Display an error message if loading a Winelib app failed (suggested by
[wine/multimedia.git] / relay32 / builtin32.c
blobe16398cac735dd54202466dd685208fc4aff79ba
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #ifdef HAVE_DL_API
14 #include <dlfcn.h>
15 #endif
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/mman.h>
19 #endif
21 #include "windef.h"
22 #include "wine/winbase16.h"
23 #include "wine/library.h"
24 #include "global.h"
25 #include "module.h"
26 #include "heap.h"
27 #include "main.h"
28 #include "winerror.h"
29 #include "server.h"
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(module);
33 DECLARE_DEBUG_CHANNEL(relay);
35 extern void RELAY_SetupDLL( const char *module );
37 static HMODULE main_module;
39 /***********************************************************************
40 * BUILTIN32_dlopen
42 void *BUILTIN32_dlopen( const char *name )
44 #ifdef HAVE_DL_API
45 void *handle;
47 if (!(handle = wine_dll_load( name )))
49 LPSTR pErr, p;
50 pErr = dlerror();
51 p = strchr(pErr, ':');
52 if ((p) &&
53 (!strncmp(p, ": undefined symbol", 18))) /* undef symbol -> ERR() */
54 ERR("failed to load %s: %s\n", name, pErr);
55 else /* WARN() for libraries that are supposed to be native */
56 WARN("failed to load %s: %s\n", name, pErr );
58 return handle;
59 #else
60 return NULL;
61 #endif
64 /***********************************************************************
65 * BUILTIN32_dlclose
67 int BUILTIN32_dlclose( void *handle )
69 #ifdef HAVE_DL_API
70 /* FIXME: should unregister descriptors first */
71 /* return dlclose( handle ); */
72 #endif
73 return 0;
77 /***********************************************************************
78 * load_library
80 * Load a library in memory; callback function for wine_dll_register
82 static void load_library( void *base, const char *filename )
84 HMODULE module = (HMODULE)base;
85 WINE_MODREF *wm;
87 if (!base)
89 ERR("could not map image for %s\n", filename ? filename : "main exe" );
90 return;
93 if (!(PE_HEADER(module)->FileHeader.Characteristics & IMAGE_FILE_DLL))
95 /* if we already have an executable, ignore this one */
96 if (!main_module) main_module = module;
97 return; /* don't create the modref here, will be done later on */
100 if (GetModuleHandleA( filename ))
101 MESSAGE( "Warning: loading builtin %s, but native version already present. Expect trouble.\n", filename );
103 /* Create 32-bit MODREF */
104 if (!(wm = PE_CreateModule( module, filename, 0, -1, TRUE )))
106 ERR( "can't load %s\n", filename );
107 SetLastError( ERROR_OUTOFMEMORY );
108 return;
110 TRACE( "loaded %s %p %x\n", filename, wm, module );
111 wm->refCount++; /* we don't support freeing builtin dlls (FIXME)*/
113 /* setup relay debugging entry points */
114 if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
118 /***********************************************************************
119 * BUILTIN32_LoadLibraryExA
121 * Partly copied from the original PE_ version.
124 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
126 WINE_MODREF *wm;
127 char dllname[20], *p;
128 LPCSTR name;
129 void *handle;
131 /* Fix the name in case we have a full path and extension */
132 name = path;
133 if ((p = strrchr( name, '\\' ))) name = p + 1;
134 if ((p = strrchr( name, '/' ))) name = p + 1;
136 if (strlen(name) >= sizeof(dllname)-4) goto error;
138 strcpy( dllname, name );
139 p = strrchr( dllname, '.' );
140 if (!p) strcat( dllname, ".dll" );
142 if (!(handle = BUILTIN32_dlopen( dllname ))) goto error;
144 if (!(wm = MODULE_FindModule( path ))) wm = MODULE_FindModule( dllname );
145 if (!wm)
147 ERR( "loaded .so but dll %s still not found\n", dllname );
148 /* wine_dll_unload( handle );*/
149 return NULL;
151 wm->dlhandle = handle;
152 return wm;
154 error:
155 SetLastError( ERROR_FILE_NOT_FOUND );
156 return NULL;
159 /***********************************************************************
160 * BUILTIN32_Init
162 * Initialize loading callbacks and return HMODULE of main exe.
163 * 'main' is the main exe in case if was already loaded from a PE file.
165 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
167 main_module = main;
168 wine_dll_set_callback( load_library );
169 if (!main_module)
170 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
171 return main_module;
175 /***********************************************************************
176 * BUILTIN32_RegisterDLL
178 * Register a built-in DLL descriptor.
180 void BUILTIN32_RegisterDLL( const IMAGE_NT_HEADERS *header, const char *filename )
182 extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
183 __wine_dll_register( header, filename );