Moved DCX_* constants to winuser.h.
[wine/multimedia.git] / relay32 / builtin32.c
blob47aa37e0849a5b1f3cb2589c75b2f07054667ac6
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 #include <sys/types.h>
14 #ifdef HAVE_SYS_MMAN_H
15 #include <sys/mman.h>
16 #endif
18 #include "windef.h"
19 #include "wine/winbase16.h"
20 #include "wine/library.h"
21 #include "module.h"
22 #include "file.h"
23 #include "heap.h"
24 #include "winerror.h"
25 #include "server.h"
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(module);
29 DECLARE_DEBUG_CHANNEL(relay);
31 extern void RELAY_SetupDLL( const char *module );
33 static HMODULE main_module;
35 /***********************************************************************
36 * BUILTIN32_dlopen
38 void *BUILTIN32_dlopen( const char *name )
40 void *handle;
41 char error[256];
43 if (!(handle = wine_dll_load( name, error, sizeof(error) )))
45 if (strstr(error, "cannot open")) /* cannot open -> WARN() */
46 WARN("cannot open .so lib for builtin %s: %s\n", name, error);
47 else /* ERR() for all other errors (missing functions, ...) */
48 ERR("failed to load .so lib for builtin %s: %s\n", name, error );
50 return handle;
53 /***********************************************************************
54 * BUILTIN32_dlclose
56 int BUILTIN32_dlclose( void *handle )
58 /* FIXME: should unregister descriptors first */
59 /* wine_dll_unload( handle ); */
60 return 0;
64 /***********************************************************************
65 * load_library
67 * Load a library in memory; callback function for wine_dll_register
69 static void load_library( void *base, const char *filename )
71 HMODULE module = (HMODULE)base;
72 WINE_MODREF *wm;
74 if (!base)
76 ERR("could not map image for %s\n", filename ? filename : "main exe" );
77 return;
80 if (!(PE_HEADER(module)->FileHeader.Characteristics & IMAGE_FILE_DLL))
82 /* if we already have an executable, ignore this one */
83 if (!main_module) main_module = module;
84 return; /* don't create the modref here, will be done later on */
87 if (GetModuleHandleA( filename ))
88 MESSAGE( "Warning: loading builtin %s, but native version already present. Expect trouble.\n", filename );
90 /* Create 32-bit MODREF */
91 if (!(wm = PE_CreateModule( module, filename, 0, 0, TRUE )))
93 ERR( "can't load %s\n", filename );
94 SetLastError( ERROR_OUTOFMEMORY );
95 return;
97 TRACE( "loaded %s %p %x\n", filename, wm, module );
98 wm->refCount++; /* we don't support freeing builtin dlls (FIXME)*/
100 /* setup relay debugging entry points */
101 if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
105 /***********************************************************************
106 * BUILTIN32_LoadLibraryExA
108 * Partly copied from the original PE_ version.
111 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
113 WINE_MODREF *wm;
114 char dllname[20], *p;
115 LPCSTR name;
116 void *handle;
118 /* Fix the name in case we have a full path and extension */
119 name = path;
120 if ((p = strrchr( name, '\\' ))) name = p + 1;
121 if ((p = strrchr( name, '/' ))) name = p + 1;
123 if (strlen(name) >= sizeof(dllname)-4) goto error;
125 strcpy( dllname, name );
126 p = strrchr( dllname, '.' );
127 if (!p) strcat( dllname, ".dll" );
128 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
130 if (!(handle = BUILTIN32_dlopen( dllname ))) goto error;
132 if (!(wm = MODULE_FindModule( path ))) wm = MODULE_FindModule( dllname );
133 if (!wm)
135 ERR( "loaded .so but dll %s still not found\n", dllname );
136 /* wine_dll_unload( handle );*/
137 return NULL;
139 wm->dlhandle = handle;
140 return wm;
142 error:
143 SetLastError( ERROR_FILE_NOT_FOUND );
144 return NULL;
147 /***********************************************************************
148 * BUILTIN32_Init
150 * Initialize loading callbacks and return HMODULE of main exe.
151 * 'main' is the main exe in case if was already loaded from a PE file.
153 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
155 main_module = main;
156 wine_dll_set_callback( load_library );
157 if (!main_module)
158 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
159 return main_module;
163 /***********************************************************************
164 * BUILTIN32_RegisterDLL
166 * Register a built-in DLL descriptor.
168 void BUILTIN32_RegisterDLL( const IMAGE_NT_HEADERS *header, const char *filename )
170 extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
171 __wine_dll_register( header, filename );