Moved scrollbar tracking code to scroll.c.
[wine] / relay32 / builtin32.c
1 /*
2  * Win32 builtin functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  */
6
7 #include "config.h"
8
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
17
18 #include "windef.h"
19 #include "wine/winbase16.h"
20 #include "wine/library.h"
21 #include "module.h"
22 #include "file.h"
23 #include "winerror.h"
24 #include "wine/server.h"
25 #include "debugtools.h"
26
27 DEFAULT_DEBUG_CHANNEL(module);
28 DECLARE_DEBUG_CHANNEL(relay);
29
30 extern void RELAY_SetupDLL( const char *module );
31
32 static HMODULE main_module;
33
34 /***********************************************************************
35  *           BUILTIN32_dlopen
36  */
37 void *BUILTIN32_dlopen( const char *name )
38 {
39     void *handle;
40     char error[256];
41
42     if (!(handle = wine_dll_load( name, error, sizeof(error) )))
43     {
44         if (strstr(error, "cannot open") || strstr(error, "open failed")) {
45             /* The file does not exist -> WARN() */
46             WARN("cannot open .so lib for builtin %s: %s\n", name, error);
47         } else {
48             /* ERR() for all other errors (missing functions, ...) */
49             ERR("failed to load .so lib for builtin %s: %s\n", name, error );
50         }
51     }
52     return handle;
53 }
54
55 /***********************************************************************
56  *           BUILTIN32_dlclose
57  */
58 int BUILTIN32_dlclose( void *handle )
59 {
60     /* FIXME: should unregister descriptors first */
61     /* wine_dll_unload( handle ); */
62     return 0;
63 }
64
65
66 /***********************************************************************
67  *           load_library
68  *
69  * Load a library in memory; callback function for wine_dll_register
70  */
71 static void load_library( void *base, const char *filename )
72 {
73     HMODULE module = (HMODULE)base;
74     WINE_MODREF *wm;
75
76     if (!base)
77     {
78         ERR("could not map image for %s\n", filename ? filename : "main exe" );
79         return;
80     }
81
82     if (!(PE_HEADER(module)->FileHeader.Characteristics & IMAGE_FILE_DLL))
83     {
84         /* if we already have an executable, ignore this one */
85         if (!main_module) main_module = module;
86         return; /* don't create the modref here, will be done later on */
87     }
88
89     if (GetModuleHandleA( filename ))
90         MESSAGE( "Warning: loading builtin %s, but native version already present. Expect trouble.\n", filename );
91
92     /* Create 32-bit MODREF */
93     if (!(wm = PE_CreateModule( module, filename, 0, 0, TRUE )))
94     {
95         ERR( "can't load %s\n", filename );
96         SetLastError( ERROR_OUTOFMEMORY );
97         return;
98     }
99     TRACE( "loaded %s %p %x\n", filename, wm, module );
100     wm->refCount++;  /* we don't support freeing builtin dlls (FIXME)*/
101
102     /* setup relay debugging entry points */
103     if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
104 }
105
106
107 /***********************************************************************
108  *           BUILTIN32_LoadLibraryExA
109  *
110  * Partly copied from the original PE_ version.
111  *
112  */
113 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
114 {
115     WINE_MODREF   *wm;
116     char dllname[20], *p;
117     LPCSTR name;
118     void *handle;
119
120     /* Fix the name in case we have a full path and extension */
121     name = path;
122     if ((p = strrchr( name, '\\' ))) name = p + 1;
123     if ((p = strrchr( name, '/' ))) name = p + 1;
124
125     if (strlen(name) >= sizeof(dllname)-4) goto error;
126
127     strcpy( dllname, name );
128     p = strrchr( dllname, '.' );
129     if (!p) strcat( dllname, ".dll" );
130     for (p = dllname; *p; p++) *p = FILE_tolower(*p);
131
132     if (!(handle = BUILTIN32_dlopen( dllname ))) goto error;
133
134     if (!(wm = MODULE_FindModule( path ))) wm = MODULE_FindModule( dllname );
135     if (!wm)
136     {
137         ERR( "loaded .so but dll %s still not found\n", dllname );
138         /* wine_dll_unload( handle );*/
139         return NULL;
140     }
141     wm->dlhandle = handle;
142     return wm;
143
144  error:
145     SetLastError( ERROR_FILE_NOT_FOUND );
146     return NULL;
147 }
148
149 /***********************************************************************
150  *           BUILTIN32_Init
151  *
152  * Initialize loading callbacks and return HMODULE of main exe.
153  * 'main' is the main exe in case if was already loaded from a PE file.
154  */
155 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
156 {
157     main_module = main;
158     wine_dll_set_callback( load_library );
159     if (!main_module)
160         MESSAGE( "No built-in EXE module loaded!  Did you create a .spec file?\n" );
161     return main_module;
162 }
163
164
165 /***********************************************************************
166  *           BUILTIN32_RegisterDLL
167  *
168  * Register a built-in DLL descriptor.
169  */
170 void BUILTIN32_RegisterDLL( const IMAGE_NT_HEADERS *header, const char *filename )
171 {
172     extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
173     __wine_dll_register( header, filename );
174 }