blob: 9f409522b5b5af5e04f7841ad489b89c308c2ccb [file] [log] [blame]
Alexandre Julliard594997c1995-04-30 10:05:20 +00001/*
2 * Modules
3 *
4 * Copyright 1995 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard594997c1995-04-30 10:05:20 +000019 */
20
Patrik Stridvalld016f812002-08-17 00:43:16 +000021#include "config.h"
22
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000023#include <assert.h>
Alexandre Julliard594997c1995-04-30 10:05:20 +000024#include <fcntl.h>
25#include <stdlib.h>
Jeremy Whited3e22d92000-02-10 19:03:02 +000026#include <stdio.h>
Alexandre Julliard594997c1995-04-30 10:05:20 +000027#include <string.h>
28#include <sys/types.h>
Patrik Stridvalld016f812002-08-17 00:43:16 +000029#ifdef HAVE_UNISTD_H
30# include <unistd.h>
31#endif
Jeremy Whited3e22d92000-02-10 19:03:02 +000032#include "wine/winbase16.h"
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000033#include "winerror.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000034#include "heap.h"
Aric Stewarte4d09322000-12-03 03:14:29 +000035#include "file.h"
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000036#include "module.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000037#include "wine/debug.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000038#include "wine/server.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000039
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000040WINE_DEFAULT_DEBUG_CHANNEL(module);
41WINE_DECLARE_DEBUG_CHANNEL(win32);
42WINE_DECLARE_DEBUG_CHANNEL(loaddll);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000043
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000044WINE_MODREF *MODULE_modref_list = NULL;
45
46static WINE_MODREF *exe_modref;
47static int free_lib_count; /* recursion depth of FreeLibrary calls */
Alexandre Julliard014a8bb2000-12-20 18:41:34 +000048static int process_detaching; /* set on process detach to avoid deadlocks with thread detach */
Alexandre Julliard594997c1995-04-30 10:05:20 +000049
Alexandre Julliard64781642002-02-02 18:13:50 +000050static CRITICAL_SECTION loader_section = CRITICAL_SECTION_INIT( "loader_section" );
51
Alexandre Julliard5edf4e12001-07-26 20:12:54 +000052/***********************************************************************
53 * wait_input_idle
54 *
55 * Wrapper to call WaitForInputIdle USER function
56 */
57typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
58
59static DWORD wait_input_idle( HANDLE process, DWORD timeout )
60{
61 HMODULE mod = GetModuleHandleA( "user32.dll" );
62 if (mod)
63 {
64 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
65 if (ptr) return ptr( process, timeout );
66 }
67 return 0;
68}
69
70
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000071/*************************************************************************
72 * MODULE32_LookupHMODULE
73 * looks for the referenced HMODULE in the current process
Alexandre Julliard081ee942000-08-07 04:12:41 +000074 * NOTE: Assumes that the process critical section is held!
Alexandre Julliard329f0681996-04-14 13:21:20 +000075 */
Alexandre Julliard081ee942000-08-07 04:12:41 +000076static WINE_MODREF *MODULE32_LookupHMODULE( HMODULE hmod )
Ulrich Weigand1d90d691999-02-24 14:27:07 +000077{
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000078 WINE_MODREF *wm;
Alexandre Julliard77b99181997-09-14 17:17:23 +000079
Vincent Béron9a624912002-05-31 23:06:46 +000080 if (!hmod)
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000081 return exe_modref;
Ulrich Weigand1d90d691999-02-24 14:27:07 +000082
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000083 if (!HIWORD(hmod)) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +000084 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod);
Alexandre Julliard081ee942000-08-07 04:12:41 +000085 SetLastError( ERROR_INVALID_HANDLE );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000086 return NULL;
87 }
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000088 for ( wm = MODULE_modref_list; wm; wm=wm->next )
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000089 if (wm->module == hmod)
90 return wm;
Alexandre Julliard081ee942000-08-07 04:12:41 +000091 SetLastError( ERROR_INVALID_HANDLE );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000092 return NULL;
93}
94
Ulrich Weiganda3527cf1998-10-11 19:31:10 +000095/*************************************************************************
Alexandre Julliard081ee942000-08-07 04:12:41 +000096 * MODULE_AllocModRef
97 *
98 * Allocate a WINE_MODREF structure and add it to the process list
99 * NOTE: Assumes that the process critical section is held!
100 */
101WINE_MODREF *MODULE_AllocModRef( HMODULE hModule, LPCSTR filename )
102{
103 WINE_MODREF *wm;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000104
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000105 DWORD long_len = strlen( filename );
106 DWORD short_len = GetShortPathNameA( filename, NULL, 0 );
107
108 if ((wm = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
109 sizeof(*wm) + long_len + short_len + 1 )))
Alexandre Julliard081ee942000-08-07 04:12:41 +0000110 {
111 wm->module = hModule;
112 wm->tlsindex = -1;
113
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000114 wm->filename = wm->data;
115 memcpy( wm->filename, filename, long_len + 1 );
Alexandre Julliard081ee942000-08-07 04:12:41 +0000116 if ((wm->modname = strrchr( wm->filename, '\\' ))) wm->modname++;
117 else wm->modname = wm->filename;
118
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000119 wm->short_filename = wm->filename + long_len + 1;
120 GetShortPathNameA( wm->filename, wm->short_filename, short_len + 1 );
Alexandre Julliard081ee942000-08-07 04:12:41 +0000121 if ((wm->short_modname = strrchr( wm->short_filename, '\\' ))) wm->short_modname++;
122 else wm->short_modname = wm->short_filename;
123
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000124 wm->next = MODULE_modref_list;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000125 if (wm->next) wm->next->prev = wm;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000126 MODULE_modref_list = wm;
127
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000128 if (!(RtlImageNtHeader(hModule)->FileHeader.Characteristics & IMAGE_FILE_DLL))
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000129 {
130 if (!exe_modref) exe_modref = wm;
131 else FIXME( "Trying to load second .EXE file: %s\n", filename );
132 }
Alexandre Julliard081ee942000-08-07 04:12:41 +0000133 }
134 return wm;
135}
136
137/*************************************************************************
Andreas Mohr96293d42000-07-08 18:28:03 +0000138 * MODULE_InitDLL
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000139 */
Andreas Mohr96293d42000-07-08 18:28:03 +0000140static BOOL MODULE_InitDLL( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000141{
142 BOOL retv = TRUE;
143
Vincent Béron9a624912002-05-31 23:06:46 +0000144 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000145 "THREAD_ATTACH", "THREAD_DETACH" };
146 assert( wm );
147
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000148 /* Skip calls for modules loaded with special load flags */
149
Alexandre Julliard081ee942000-08-07 04:12:41 +0000150 if (wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) return TRUE;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000151
Alexandre Julliardf93eb3e2000-04-28 20:26:35 +0000152 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000153
154 /* Call the initialization routine */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000155 retv = PE_InitDLL( wm->module, type, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000156
Alexandre Julliardf93eb3e2000-04-28 20:26:35 +0000157 /* The state of the module list may have changed due to the call
158 to PE_InitDLL. We cannot assume that this module has not been
159 deleted. */
160 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000161
162 return retv;
163}
164
165/*************************************************************************
166 * MODULE_DllProcessAttach
Vincent Béron9a624912002-05-31 23:06:46 +0000167 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000168 * Send the process attach notification to all DLLs the given module
169 * depends on (recursively). This is somewhat complicated due to the fact that
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000170 *
171 * - we have to respect the module dependencies, i.e. modules implicitly
172 * referenced by another module have to be initialized before the module
173 * itself can be initialized
Vincent Béron9a624912002-05-31 23:06:46 +0000174 *
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000175 * - the initialization routine of a DLL can itself call LoadLibrary,
176 * thereby introducing a whole new set of dependencies (even involving
177 * the 'old' modules) at any time during the whole process
178 *
179 * (Note that this routine can be recursively entered not only directly
180 * from itself, but also via LoadLibrary from one of the called initialization
181 * routines.)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000182 *
183 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
184 * the process *detach* notifications to be sent in the correct order.
Vincent Béron9a624912002-05-31 23:06:46 +0000185 * This must not only take into account module dependencies, but also
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000186 * 'hidden' dependencies created by modules calling LoadLibrary in their
187 * attach notification routine.
188 *
189 * The strategy is rather simple: we move a WINE_MODREF to the head of the
190 * list after the attach notification has returned. This implies that the
191 * detach notifications are called in the reverse of the sequence the attach
192 * notifications *returned*.
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000193 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000194BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000195{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000196 BOOL retv = TRUE;
197 int i;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000198
Alexandre Julliard64781642002-02-02 18:13:50 +0000199 RtlEnterCriticalSection( &loader_section );
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000200
Alexandre Julliard64781642002-02-02 18:13:50 +0000201 if (!wm)
202 {
203 wm = exe_modref;
204 PE_InitTls();
205 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000206 assert( wm );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000207
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000208 /* prevent infinite recursion in case of cyclical dependencies */
209 if ( ( wm->flags & WINE_MODREF_MARKER )
210 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000211 goto done;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000212
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000213 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000214
215 /* Tag current MODREF to prevent recursive loop */
Ulrich Weigande469a581999-03-27 16:45:57 +0000216 wm->flags |= WINE_MODREF_MARKER;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000217
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000218 /* Recursively attach all DLLs this one depends on */
219 for ( i = 0; retv && i < wm->nDeps; i++ )
220 if ( wm->deps[i] )
221 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
222
223 /* Call DLL entry point */
224 if ( retv )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000225 {
Andreas Mohr96293d42000-07-08 18:28:03 +0000226 retv = MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000227 if ( retv )
Ulrich Weigande469a581999-03-27 16:45:57 +0000228 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000229 }
Ulrich Weigande469a581999-03-27 16:45:57 +0000230
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000231 /* Re-insert MODREF at head of list */
232 if ( retv && wm->prev )
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000233 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000234 wm->prev->next = wm->next;
235 if ( wm->next ) wm->next->prev = wm->prev;
236
237 wm->prev = NULL;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000238 wm->next = MODULE_modref_list;
239 MODULE_modref_list = wm->next->prev = wm;
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000240 }
Ulrich Weigande469a581999-03-27 16:45:57 +0000241
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000242 /* Remove recursion flag */
243 wm->flags &= ~WINE_MODREF_MARKER;
Ulrich Weigande469a581999-03-27 16:45:57 +0000244
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000245 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
Ulrich Weigande469a581999-03-27 16:45:57 +0000246
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000247 done:
Alexandre Julliard64781642002-02-02 18:13:50 +0000248 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000249 return retv;
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000250}
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000251
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000252/*************************************************************************
253 * MODULE_DllProcessDetach
Vincent Béron9a624912002-05-31 23:06:46 +0000254 *
255 * Send DLL process detach notifications. See the comment about calling
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000256 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
257 * is set, only DLLs with zero refcount are notified.
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000258 */
259void MODULE_DllProcessDetach( BOOL bForceDetach, LPVOID lpReserved )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000260{
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000261 WINE_MODREF *wm;
262
Alexandre Julliard64781642002-02-02 18:13:50 +0000263 RtlEnterCriticalSection( &loader_section );
Alexandre Julliard014a8bb2000-12-20 18:41:34 +0000264 if (bForceDetach) process_detaching = 1;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000265 do
266 {
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000267 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000268 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000269 /* Check whether to detach this DLL */
270 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
271 continue;
272 if ( wm->refCount > 0 && !bForceDetach )
273 continue;
274
275 /* Call detach notification */
276 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
Andreas Mohr96293d42000-07-08 18:28:03 +0000277 MODULE_InitDLL( wm, DLL_PROCESS_DETACH, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000278
279 /* Restart at head of WINE_MODREF list, as entries might have
280 been added and/or removed while performing the call ... */
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000281 break;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000282 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000283 } while ( wm );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000284
Alexandre Julliard64781642002-02-02 18:13:50 +0000285 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000286}
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000287
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000288/*************************************************************************
289 * MODULE_DllThreadAttach
Vincent Béron9a624912002-05-31 23:06:46 +0000290 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000291 * Send DLL thread attach notifications. These are sent in the
292 * reverse sequence of process detach notification.
293 *
294 */
295void MODULE_DllThreadAttach( LPVOID lpReserved )
296{
297 WINE_MODREF *wm;
298
Alexandre Julliard014a8bb2000-12-20 18:41:34 +0000299 /* don't do any attach calls if process is exiting */
300 if (process_detaching) return;
301 /* FIXME: there is still a race here */
302
Alexandre Julliard64781642002-02-02 18:13:50 +0000303 RtlEnterCriticalSection( &loader_section );
304
305 PE_InitTls();
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000306
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000307 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000308 if ( !wm->next )
309 break;
310
311 for ( ; wm; wm = wm->prev )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000312 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000313 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
314 continue;
315 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
316 continue;
Ulrich Weigande469a581999-03-27 16:45:57 +0000317
Andreas Mohr96293d42000-07-08 18:28:03 +0000318 MODULE_InitDLL( wm, DLL_THREAD_ATTACH, lpReserved );
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000319 }
320
Alexandre Julliard64781642002-02-02 18:13:50 +0000321 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000322}
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000323
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000324/*************************************************************************
325 * MODULE_DllThreadDetach
Vincent Béron9a624912002-05-31 23:06:46 +0000326 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000327 * Send DLL thread detach notifications. These are sent in the
328 * same sequence as process detach notification.
329 *
330 */
331void MODULE_DllThreadDetach( LPVOID lpReserved )
332{
333 WINE_MODREF *wm;
334
Alexandre Julliard014a8bb2000-12-20 18:41:34 +0000335 /* don't do any detach calls if process is exiting */
336 if (process_detaching) return;
337 /* FIXME: there is still a race here */
338
Alexandre Julliard64781642002-02-02 18:13:50 +0000339 RtlEnterCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000340
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000341 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000342 {
343 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
344 continue;
345 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
346 continue;
347
Andreas Mohr96293d42000-07-08 18:28:03 +0000348 MODULE_InitDLL( wm, DLL_THREAD_DETACH, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000349 }
350
Alexandre Julliard64781642002-02-02 18:13:50 +0000351 RtlLeaveCriticalSection( &loader_section );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000352}
353
Ulrich Weigande469a581999-03-27 16:45:57 +0000354/****************************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000355 * DisableThreadLibraryCalls (KERNEL32.@)
Ulrich Weigande469a581999-03-27 16:45:57 +0000356 *
357 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
358 */
359BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
360{
Bertho Stultiens1b346971999-04-11 14:52:24 +0000361 WINE_MODREF *wm;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000362 BOOL retval = TRUE;
Ulrich Weigande469a581999-03-27 16:45:57 +0000363
Alexandre Julliard64781642002-02-02 18:13:50 +0000364 RtlEnterCriticalSection( &loader_section );
Ulrich Weigande469a581999-03-27 16:45:57 +0000365
Bertho Stultiens1b346971999-04-11 14:52:24 +0000366 wm = MODULE32_LookupHMODULE( hModule );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000367 if ( !wm )
368 retval = FALSE;
369 else
370 wm->flags |= WINE_MODREF_NO_DLL_CALLS;
371
Alexandre Julliard64781642002-02-02 18:13:50 +0000372 RtlLeaveCriticalSection( &loader_section );
Ulrich Weigande469a581999-03-27 16:45:57 +0000373
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000374 return retval;
Ulrich Weigande469a581999-03-27 16:45:57 +0000375}
376
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000377
378/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000379 * MODULE_CreateDummyModule
380 *
381 * Create a dummy NE module for Win32 or Winelib.
382 */
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000383HMODULE16 MODULE_CreateDummyModule( LPCSTR filename, HMODULE module32 )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000384{
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000385 HMODULE16 hModule;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000386 NE_MODULE *pModule;
387 SEGTABLEENTRY *pSegment;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000388 char *pStr,*s;
Francois Gougetbaa9bf91999-12-27 05:24:06 +0000389 unsigned int len;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000390 const char* basename;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000391 OFSTRUCT *ofs;
392 int of_size, size;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000393
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000394 /* Extract base filename */
395 basename = strrchr(filename, '\\');
396 if (!basename) basename = filename;
397 else basename++;
398 len = strlen(basename);
399 if ((s = strchr(basename, '.'))) len = s - basename;
400
401 /* Allocate module */
402 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
403 + strlen(filename) + 1;
404 size = sizeof(NE_MODULE) +
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000405 /* loaded file info */
Vincent Béron9a624912002-05-31 23:06:46 +0000406 ((of_size + 3) & ~3) +
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000407 /* segment table: DS,CS */
408 2 * sizeof(SEGTABLEENTRY) +
409 /* name table */
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000410 len + 2 +
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000411 /* several empty tables */
412 8;
413
414 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000415 if (!hModule) return (HMODULE16)11; /* invalid exe */
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000416
Alexandre Julliarda3960291999-02-26 11:11:13 +0000417 FarSetOwner16( hModule, hModule );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000418 pModule = (NE_MODULE *)GlobalLock16( hModule );
419
420 /* Set all used entries */
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000421 pModule->magic = IMAGE_OS2_SIGNATURE;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000422 pModule->count = 1;
423 pModule->next = 0;
424 pModule->flags = 0;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000425 pModule->dgroup = 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000426 pModule->ss = 1;
427 pModule->cs = 2;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000428 pModule->heap_size = 0;
429 pModule->stack_size = 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000430 pModule->seg_count = 2;
431 pModule->modref_count = 0;
432 pModule->nrname_size = 0;
433 pModule->fileinfo = sizeof(NE_MODULE);
434 pModule->os_flags = NE_OSFLAGS_WINDOWS;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000435 pModule->self = hModule;
Alexandre Julliardb4459522000-04-15 21:00:55 +0000436 pModule->module32 = module32;
437
438 /* Set version and flags */
439 if (module32)
440 {
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000441 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
442 pModule->expected_version = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
443 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
Alexandre Julliardb4459522000-04-15 21:00:55 +0000444 pModule->flags |= NE_FFLAGS_WIN32;
Alexandre Julliarda5dea212002-08-09 19:57:38 +0000445 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
Alexandre Julliardb4459522000-04-15 21:00:55 +0000446 pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
447 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000448
449 /* Set loaded file information */
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000450 ofs = (OFSTRUCT *)(pModule + 1);
451 memset( ofs, 0, of_size );
452 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
453 strcpy( ofs->szPathName, filename );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000454
Ulrich Weigandacefd162000-12-29 05:09:15 +0000455 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000456 pModule->seg_table = (int)pSegment - (int)pModule;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000457 /* Data segment */
458 pSegment->size = 0;
459 pSegment->flags = NE_SEGFLAGS_DATA;
460 pSegment->minsize = 0x1000;
461 pSegment++;
462 /* Code segment */
463 pSegment->flags = 0;
464 pSegment++;
465
466 /* Module name */
467 pStr = (char *)pSegment;
468 pModule->name_table = (int)pStr - (int)pModule;
Francois Gougetbaa9bf91999-12-27 05:24:06 +0000469 assert(len<256);
Alexandre Julliard17216f51997-10-12 16:30:17 +0000470 *pStr = len;
Francois Gougetbaa9bf91999-12-27 05:24:06 +0000471 lstrcpynA( pStr+1, basename, len+1 );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000472 pStr += len+2;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000473
474 /* All tables zero terminated */
475 pModule->res_table = pModule->import_table = pModule->entry_table =
476 (int)pStr - (int)pModule;
477
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000478 NE_RegisterModule( pModule );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000479 return hModule;
480}
481
482
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000483/**********************************************************************
Peter Gantenc7c42462000-08-28 21:33:28 +0000484 * MODULE_FindModule
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000485 *
486 * Find a (loaded) win32 module depending on path
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000487 *
488 * RETURNS
489 * the module handle if found
490 * 0 if not
491 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000492WINE_MODREF *MODULE_FindModule(
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000493 LPCSTR path /* [in] pathname of module/library to be found */
494) {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000495 WINE_MODREF *wm;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000496 char dllname[260], *p;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000497
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000498 /* Append .DLL to name if no extension present */
499 strcpy( dllname, path );
500 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
501 strcat( dllname, ".DLL" );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000502
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000503 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000504 {
Aric Stewarte4d09322000-12-03 03:14:29 +0000505 if ( !FILE_strcasecmp( dllname, wm->modname ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000506 break;
Aric Stewarte4d09322000-12-03 03:14:29 +0000507 if ( !FILE_strcasecmp( dllname, wm->filename ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000508 break;
Aric Stewarte4d09322000-12-03 03:14:29 +0000509 if ( !FILE_strcasecmp( dllname, wm->short_modname ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000510 break;
Aric Stewarte4d09322000-12-03 03:14:29 +0000511 if ( !FILE_strcasecmp( dllname, wm->short_filename ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000512 break;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000513 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000514
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000515 return wm;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000516}
517
Andreas Mohrb021fe22000-07-26 18:02:28 +0000518
519/* Check whether a file is an OS/2 or a very old Windows executable
520 * by testing on import of KERNEL.
521 *
522 * FIXME: is reading the module imports the only way of discerning
523 * old Windows binaries from OS/2 ones ? At least it seems so...
524 */
Alexandre Julliard35363162002-05-22 21:32:49 +0000525static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
526 const IMAGE_OS2_HEADER *ne)
Andreas Mohrb021fe22000-07-26 18:02:28 +0000527{
528 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
Alexandre Julliard35363162002-05-22 21:32:49 +0000529 enum binary_type ret = BINARY_OS216;
Andreas Mohrb021fe22000-07-26 18:02:28 +0000530 LPWORD modtab = NULL;
531 LPSTR nametab = NULL;
532 DWORD len;
533 int i;
534
535 /* read modref table */
536 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
537 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
538 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
539 || (len != ne->ne_cmod*sizeof(WORD)) )
540 goto broken;
541
542 /* read imported names table */
543 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
544 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
545 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
546 || (len != ne->ne_enttab - ne->ne_imptab) )
547 goto broken;
548
549 for (i=0; i < ne->ne_cmod; i++)
550 {
551 LPSTR module = &nametab[modtab[i]];
552 TRACE("modref: %.*s\n", module[0], &module[1]);
553 if (!(strncmp(&module[1], "KERNEL", module[0])))
554 { /* very old Windows file */
555 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
Alexandre Julliard35363162002-05-22 21:32:49 +0000556 ret = BINARY_WIN16;
Andreas Mohrb021fe22000-07-26 18:02:28 +0000557 goto good;
558 }
559 }
560
561broken:
562 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
563
564good:
565 HeapFree( GetProcessHeap(), 0, modtab);
566 HeapFree( GetProcessHeap(), 0, nametab);
567 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
Alexandre Julliard35363162002-05-22 21:32:49 +0000568 return ret;
Andreas Mohrb021fe22000-07-26 18:02:28 +0000569}
570
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000571/***********************************************************************
572 * MODULE_GetBinaryType
Alexandre Julliard35363162002-05-22 21:32:49 +0000573 */
574enum binary_type MODULE_GetBinaryType( HANDLE hfile )
575{
576 union
577 {
578 struct
579 {
580 unsigned char magic[4];
581 unsigned char ignored[12];
582 unsigned short type;
583 } elf;
584 IMAGE_DOS_HEADER mz;
585 } header;
586
587 char magic[4];
588 DWORD len;
589
590 /* Seek to the start of the file and read the header information. */
591 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
592 return BINARY_UNKNOWN;
593 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
594 return BINARY_UNKNOWN;
595
596 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
597 {
598 /* FIXME: we don't bother to check byte order, architecture, etc. */
599 switch(header.elf.type)
600 {
601 case 2: return BINARY_UNIX_EXE;
602 case 3: return BINARY_UNIX_LIB;
603 }
604 return BINARY_UNKNOWN;
605 }
606
607 /* Not ELF, try DOS */
608
609 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
610 {
611 /* We do have a DOS image so we will now try to seek into
612 * the file by the amount indicated by the field
613 * "Offset to extended header" and read in the
614 * "magic" field information at that location.
615 * This will tell us if there is more header information
616 * to read or not.
617 */
618 /* But before we do we will make sure that header
619 * structure encompasses the "Offset to extended header"
620 * field.
621 */
622 if ((header.mz.e_cparhdr << 4) < sizeof(IMAGE_DOS_HEADER))
623 return BINARY_DOS;
624 if (header.mz.e_crlc && (header.mz.e_lfarlc < sizeof(IMAGE_DOS_HEADER)))
625 return BINARY_DOS;
626 if (header.mz.e_lfanew < sizeof(IMAGE_DOS_HEADER))
627 return BINARY_DOS;
628 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
629 return BINARY_DOS;
630 if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic))
631 return BINARY_DOS;
632
633 /* Reading the magic field succeeded so
634 * we will try to determine what type it is.
635 */
636 if (!memcmp( magic, "PE\0\0", 4 ))
637 {
Dmitry Timoshkov1467bbd2002-08-27 00:34:41 +0000638 IMAGE_FILE_HEADER FileHeader;
Alexandre Julliard35363162002-05-22 21:32:49 +0000639
Dmitry Timoshkov1467bbd2002-08-27 00:34:41 +0000640 if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader))
Alexandre Julliard35363162002-05-22 21:32:49 +0000641 {
Dmitry Timoshkov1467bbd2002-08-27 00:34:41 +0000642 if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
Alexandre Julliard35363162002-05-22 21:32:49 +0000643 return BINARY_PE_EXE;
644 }
Dmitry Timoshkov1467bbd2002-08-27 00:34:41 +0000645 return BINARY_DOS;
Alexandre Julliard35363162002-05-22 21:32:49 +0000646 }
647
648 if (!memcmp( magic, "NE", 2 ))
649 {
650 /* This is a Windows executable (NE) header. This can
651 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
652 * DOS program (running under a DOS extender). To decide
653 * which, we'll have to read the NE header.
654 */
655 IMAGE_OS2_HEADER ne;
656 if ( SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1
657 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
658 && len == sizeof(ne) )
659 {
660 switch ( ne.ne_exetyp )
661 {
662 case 2: return BINARY_WIN16;
663 case 5: return BINARY_DOS;
664 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne);
665 }
666 }
667 /* Couldn't read header, so abort. */
Dmitry Timoshkov1467bbd2002-08-27 00:34:41 +0000668 return BINARY_DOS;
Alexandre Julliard35363162002-05-22 21:32:49 +0000669 }
670
671 /* Unknown extended header, but this file is nonetheless DOS-executable. */
672 return BINARY_DOS;
673 }
674
675 return BINARY_UNKNOWN;
676}
677
678/***********************************************************************
679 * GetBinaryTypeA [KERNEL32.@]
680 * GetBinaryType [KERNEL32.@]
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000681 *
682 * The GetBinaryType function determines whether a file is executable
683 * or not and if it is it returns what type of executable it is.
684 * The type of executable is a property that determines in which
685 * subsystem an executable file runs under.
686 *
687 * Binary types returned:
688 * SCS_32BIT_BINARY: A Win32 based application
689 * SCS_DOS_BINARY: An MS-Dos based application
690 * SCS_WOW_BINARY: A Win16 based application
691 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
692 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000693 * SCS_OS216_BINARY: A 16bit OS/2 based application
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000694 *
695 * Returns TRUE if the file is an executable in which case
696 * the value pointed by lpBinaryType is set.
697 * Returns FALSE if the file is not an executable or if the function fails.
698 *
699 * To do so it opens the file and reads in the header information
Andreas Mohr8952dea1999-12-12 20:16:42 +0000700 * if the extended header information is not present it will
701 * assume that the file is a DOS executable.
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000702 * If the extended header information is present it will
Andreas Mohr8952dea1999-12-12 20:16:42 +0000703 * determine if the file is a 16 or 32 bit Windows executable
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000704 * by check the flags in the header.
705 *
706 * Note that .COM and .PIF files are only recognized by their
707 * file name extension; but Windows does it the same way ...
Alexandre Julliard594997c1995-04-30 10:05:20 +0000708 */
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000709BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000710{
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000711 BOOL ret = FALSE;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000712 HANDLE hfile;
Alexandre Julliard35363162002-05-22 21:32:49 +0000713 char *ptr;
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000714
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000715 TRACE_(win32)("%s\n", lpApplicationName );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000716
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000717 /* Sanity check.
718 */
719 if ( lpApplicationName == NULL || lpBinaryType == NULL )
720 return FALSE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000721
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000722 /* Open the file indicated by lpApplicationName for reading.
723 */
Alexandre Julliarde3332122000-06-08 01:00:16 +0000724 hfile = CreateFileA( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
François Gougetda2b6a92001-01-06 01:29:18 +0000725 NULL, OPEN_EXISTING, 0, 0 );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000726 if ( hfile == INVALID_HANDLE_VALUE )
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000727 return FALSE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000728
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000729 /* Check binary type
730 */
Alexandre Julliard35363162002-05-22 21:32:49 +0000731 switch(MODULE_GetBinaryType( hfile ))
732 {
733 case BINARY_UNKNOWN:
734 /* try to determine from file name */
735 ptr = strrchr( lpApplicationName, '.' );
736 if (!ptr) break;
737 if (!FILE_strcasecmp( ptr, ".COM" ))
738 {
739 *lpBinaryType = SCS_DOS_BINARY;
740 ret = TRUE;
741 }
742 else if (!FILE_strcasecmp( ptr, ".PIF" ))
743 {
744 *lpBinaryType = SCS_PIF_BINARY;
745 ret = TRUE;
746 }
747 break;
748 case BINARY_PE_EXE:
749 case BINARY_PE_DLL:
750 *lpBinaryType = SCS_32BIT_BINARY;
751 ret = TRUE;
752 break;
753 case BINARY_WIN16:
754 *lpBinaryType = SCS_WOW_BINARY;
755 ret = TRUE;
756 break;
757 case BINARY_OS216:
758 *lpBinaryType = SCS_OS216_BINARY;
759 ret = TRUE;
760 break;
761 case BINARY_DOS:
762 *lpBinaryType = SCS_DOS_BINARY;
763 ret = TRUE;
764 break;
765 case BINARY_UNIX_EXE:
766 case BINARY_UNIX_LIB:
767 ret = FALSE;
768 break;
769 }
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000770
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000771 CloseHandle( hfile );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000772 return ret;
773}
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000774
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000775/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000776 * GetBinaryTypeW [KERNEL32.@]
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000777 */
778BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
779{
780 BOOL ret = FALSE;
781 LPSTR strNew = NULL;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000782
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000783 TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000784
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000785 /* Sanity check.
786 */
787 if ( lpApplicationName == NULL || lpBinaryType == NULL )
788 return FALSE;
789
790 /* Convert the wide string to a ascii string.
791 */
792 strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
793
794 if ( strNew != NULL )
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000795 {
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000796 ret = GetBinaryTypeA( strNew, lpBinaryType );
797
798 /* Free the allocated string.
799 */
800 HeapFree( GetProcessHeap(), 0, strNew );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000801 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000802
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000803 return ret;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000804}
805
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000806
807/***********************************************************************
Patrik Stridvall044855c2001-07-11 18:56:41 +0000808 * WinExec (KERNEL.166)
809 * WinExec16 (KERNEL32.@)
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000810 */
811HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
812{
Andreas Mohr70963842000-09-22 22:08:28 +0000813 LPCSTR p, args = NULL;
814 LPCSTR name_beg, name_end;
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000815 LPSTR name, cmdline;
Andreas Mohr70963842000-09-22 22:08:28 +0000816 int arglen;
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000817 HINSTANCE16 ret;
818 char buffer[MAX_PATH];
Ulrich Weigand6ce40061999-05-03 09:22:55 +0000819
Andreas Mohr70963842000-09-22 22:08:28 +0000820 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000821 {
Andreas Mohr70963842000-09-22 22:08:28 +0000822 name_beg = lpCmdLine+1;
823 p = strchr ( lpCmdLine+1, '"' );
824 if (p)
825 {
826 name_end = p;
827 args = strchr ( p, ' ' );
828 }
829 else /* yes, even valid with trailing '"' missing */
830 name_end = lpCmdLine+strlen(lpCmdLine);
831 }
Vincent Béron9a624912002-05-31 23:06:46 +0000832 else
Andreas Mohr70963842000-09-22 22:08:28 +0000833 {
834 name_beg = lpCmdLine;
835 args = strchr( lpCmdLine, ' ' );
836 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
837 }
838
839 if ((name_beg == lpCmdLine) && (!args))
840 { /* just use the original cmdline string as file name */
841 name = (LPSTR)lpCmdLine;
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000842 }
843 else
844 {
Andreas Mohr70963842000-09-22 22:08:28 +0000845 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
846 return ERROR_NOT_ENOUGH_MEMORY;
847 memcpy( name, name_beg, name_end - name_beg );
848 name[name_end - name_beg] = '\0';
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000849 }
Ulrich Weigand6ce40061999-05-03 09:22:55 +0000850
Andreas Mohr70963842000-09-22 22:08:28 +0000851 if (args)
852 {
853 args++;
854 arglen = strlen(args);
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000855 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
Andreas Mohr70963842000-09-22 22:08:28 +0000856 cmdline[0] = (BYTE)arglen;
857 strcpy( cmdline + 1, args );
858 }
859 else
860 {
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000861 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
Andreas Mohr70963842000-09-22 22:08:28 +0000862 cmdline[0] = cmdline[1] = 0;
863 }
864
Andreas Mohrcabee392000-10-25 21:22:27 +0000865 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
Andreas Mohr70963842000-09-22 22:08:28 +0000866
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000867 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
868 {
869 LOADPARAMS16 params;
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000870 WORD showCmd[2];
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000871 showCmd[0] = 2;
872 showCmd[1] = nCmdShow;
873
874 params.hEnvironment = 0;
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000875 params.cmdLine = MapLS( cmdline );
876 params.showCmd = MapLS( showCmd );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000877 params.reserved = 0;
878
879 ret = LoadModule16( buffer, &params );
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000880 UnMapLS( params.cmdLine );
881 UnMapLS( params.showCmd );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000882 }
883 else ret = GetLastError();
884
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000885 HeapFree( GetProcessHeap(), 0, cmdline );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000886 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
887
888 if (ret == 21) /* 32-bit module */
889 {
Alexandre Julliardab687972000-11-15 23:41:46 +0000890 DWORD count;
891 ReleaseThunkLock( &count );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000892 ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) );
Alexandre Julliardab687972000-11-15 23:41:46 +0000893 RestoreThunkLock( count );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000894 }
895 return ret;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000896}
897
898/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000899 * WinExec (KERNEL32.@)
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000900 */
Francois Gouget2204b502002-05-19 22:21:45 +0000901UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000902{
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000903 PROCESS_INFORMATION info;
904 STARTUPINFOA startup;
Alexandre Julliard596921d2000-06-24 20:53:47 +0000905 char *cmdline;
Alexandre Julliard267ca682002-07-31 17:20:00 +0000906 UINT ret;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000907
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000908 memset( &startup, 0, sizeof(startup) );
909 startup.cb = sizeof(startup);
910 startup.dwFlags = STARTF_USESHOWWINDOW;
911 startup.wShowWindow = nCmdShow;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000912
Alexandre Julliard596921d2000-06-24 20:53:47 +0000913 /* cmdline needs to be writeable for CreateProcess */
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000914 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
915 strcpy( cmdline, lpCmdLine );
Alexandre Julliard596921d2000-06-24 20:53:47 +0000916
917 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000918 0, NULL, NULL, &startup, &info ))
919 {
920 /* Give 30 seconds to the app to come up */
Alexandre Julliard5edf4e12001-07-26 20:12:54 +0000921 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF)
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000922 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
Alexandre Julliard267ca682002-07-31 17:20:00 +0000923 ret = 33;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000924 /* Close off the handles */
925 CloseHandle( info.hThread );
926 CloseHandle( info.hProcess );
927 }
Alexandre Julliard267ca682002-07-31 17:20:00 +0000928 else if ((ret = GetLastError()) >= 32)
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000929 {
Alexandre Julliard267ca682002-07-31 17:20:00 +0000930 FIXME("Strange error set by CreateProcess: %d\n", ret );
931 ret = 11;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000932 }
Alexandre Julliard596921d2000-06-24 20:53:47 +0000933 HeapFree( GetProcessHeap(), 0, cmdline );
Alexandre Julliard267ca682002-07-31 17:20:00 +0000934 return ret;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000935}
936
937/**********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000938 * LoadModule (KERNEL32.@)
Alexandre Julliard491502b1997-11-01 19:08:16 +0000939 */
Vincent Béron9a624912002-05-31 23:06:46 +0000940HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
Alexandre Julliard491502b1997-11-01 19:08:16 +0000941{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000942 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000943 PROCESS_INFORMATION info;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000944 STARTUPINFOA startup;
945 HINSTANCE hInstance;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000946 LPSTR cmdline, p;
947 char filename[MAX_PATH];
948 BYTE len;
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000949
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000950 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000951
952 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
953 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000954 return (HINSTANCE)GetLastError();
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000955
956 len = (BYTE)params->lpCmdLine[0];
957 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000958 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000959
960 strcpy( cmdline, filename );
961 p = cmdline + strlen(cmdline);
962 *p++ = ' ';
963 memcpy( p, params->lpCmdLine + 1, len );
964 p[len] = 0;
965
966 memset( &startup, 0, sizeof(startup) );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000967 startup.cb = sizeof(startup);
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000968 if (params->lpCmdShow)
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000969 {
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000970 startup.dwFlags = STARTF_USESHOWWINDOW;
971 startup.wShowWindow = params->lpCmdShow[1];
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000972 }
Vincent Béron9a624912002-05-31 23:06:46 +0000973
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000974 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
975 params->lpEnvAddress, NULL, &startup, &info ))
976 {
977 /* Give 30 seconds to the app to come up */
Alexandre Julliard5edf4e12001-07-26 20:12:54 +0000978 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF )
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000979 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000980 hInstance = (HINSTANCE)33;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000981 /* Close off the handles */
982 CloseHandle( info.hThread );
983 CloseHandle( info.hProcess );
984 }
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000985 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000986 {
987 FIXME("Strange error set by CreateProcess: %d\n", hInstance );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000988 hInstance = (HINSTANCE)11;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000989 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000990
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000991 HeapFree( GetProcessHeap(), 0, cmdline );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000992 return hInstance;
Alexandre Julliard491502b1997-11-01 19:08:16 +0000993}
994
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000995
Alexandre Julliard77b99181997-09-14 17:17:23 +0000996/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000997 * GetModuleHandleA (KERNEL32.@)
Patrik Stridvall044855c2001-07-11 18:56:41 +0000998 * GetModuleHandle32 (KERNEL.488)
Alexandre Julliard77b99181997-09-14 17:17:23 +0000999 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001000HMODULE WINAPI GetModuleHandleA(LPCSTR module)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001001{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001002 WINE_MODREF *wm;
1003
1004 if ( module == NULL )
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001005 wm = exe_modref;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001006 else
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001007 wm = MODULE_FindModule( module );
1008
1009 return wm? wm->module : 0;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001010}
1011
Patrik Stridvall2d6457c2000-03-28 20:22:59 +00001012/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001013 * GetModuleHandleW (KERNEL32.@)
Patrik Stridvall2d6457c2000-03-28 20:22:59 +00001014 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001015HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001016{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001017 HMODULE hModule;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001018 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001019 hModule = GetModuleHandleA( modulea );
Alexandre Julliard77b99181997-09-14 17:17:23 +00001020 HeapFree( GetProcessHeap(), 0, modulea );
1021 return hModule;
1022}
1023
Alexandre Julliard594997c1995-04-30 10:05:20 +00001024
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001025/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001026 * GetModuleFileNameA (KERNEL32.@)
Patrik Stridvall044855c2001-07-11 18:56:41 +00001027 * GetModuleFileName32 (KERNEL.487)
Andreas Mohr4654c322000-02-20 19:15:34 +00001028 *
1029 * GetModuleFileNameA seems to *always* return the long path;
1030 * it's only GetModuleFileName16 that decides between short/long path
1031 * by checking if exe version >= 4.0.
1032 * (SDK docu doesn't mention this)
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001033 */
Vincent Béron9a624912002-05-31 23:06:46 +00001034DWORD WINAPI GetModuleFileNameA(
Alexandre Julliarda3960291999-02-26 11:11:13 +00001035 HMODULE hModule, /* [in] module handle (32bit) */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001036 LPSTR lpFileName, /* [out] filenamebuffer */
Alexandre Julliard081ee942000-08-07 04:12:41 +00001037 DWORD size ) /* [in] size of filenamebuffer */
1038{
Alexandre Julliard64781642002-02-02 18:13:50 +00001039 RtlEnterCriticalSection( &loader_section );
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001040
Alexandre Julliard081ee942000-08-07 04:12:41 +00001041 lpFileName[0] = 0;
Joshua Thielen1cef2972002-07-05 00:16:41 +00001042 if (!hModule && !(NtCurrentTeb()->tibflags & TEBF_WIN32))
1043 {
1044 /* 16-bit task - get current NE module name */
1045 NE_MODULE *pModule = NE_GetPtr( GetCurrentTask() );
1046 if (pModule) GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, size);
1047 }
1048 else
1049 {
1050 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1051 if (wm) lstrcpynA( lpFileName, wm->filename, size );
1052 }
Alexandre Julliard081ee942000-08-07 04:12:41 +00001053
Alexandre Julliard64781642002-02-02 18:13:50 +00001054 RtlLeaveCriticalSection( &loader_section );
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001055 TRACE("%s\n", lpFileName );
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001056 return strlen(lpFileName);
Vincent Béron9a624912002-05-31 23:06:46 +00001057}
1058
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001059
1060/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001061 * GetModuleFileNameW (KERNEL32.@)
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001062 */
Alexandre Julliard83886f22002-07-05 01:27:19 +00001063DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001064{
Alexandre Julliard83886f22002-07-05 01:27:19 +00001065 LPSTR fnA = HeapAlloc( GetProcessHeap(), 0, size * 2 );
1066 if (!fnA) return 0;
1067 GetModuleFileNameA( hModule, fnA, size * 2 );
Alexandre Julliard24a62ab2000-11-28 22:40:56 +00001068 if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, fnA, -1, lpFileName, size ))
1069 lpFileName[size-1] = 0;
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001070 HeapFree( GetProcessHeap(), 0, fnA );
Alexandre Julliard83886f22002-07-05 01:27:19 +00001071 return strlenW(lpFileName);
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001072}
1073
1074
Alexandre Julliard2787be81995-05-22 18:23:01 +00001075/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001076 * LoadLibraryExA (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001077 */
Jim Aston031f4fa1999-10-23 19:00:02 +00001078HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001079{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001080 WINE_MODREF *wm;
Marcus Meissner574ef761999-04-03 16:23:47 +00001081
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001082 if(!libname)
1083 {
1084 SetLastError(ERROR_INVALID_PARAMETER);
1085 return 0;
1086 }
Ulrich Weigandebc543c1998-10-23 09:37:23 +00001087
Alexandre Julliard081ee942000-08-07 04:12:41 +00001088 if (flags & LOAD_LIBRARY_AS_DATAFILE)
1089 {
1090 char filename[256];
Alexandre Julliard081ee942000-08-07 04:12:41 +00001091 HMODULE hmod = 0;
1092
Dmitry Timoshkovb77afe72001-03-21 03:38:03 +00001093 /* This method allows searching for the 'native' libraries only */
Ove Kaaven10b34022001-04-16 19:32:48 +00001094 if (SearchPathA( NULL, libname, ".dll", sizeof(filename), filename, NULL ))
Dmitry Timoshkovb77afe72001-03-21 03:38:03 +00001095 {
Ove Kaaven10b34022001-04-16 19:32:48 +00001096 /* FIXME: maybe we should use the hfile parameter instead */
1097 HANDLE hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
1098 NULL, OPEN_EXISTING, 0, 0 );
1099 if (hFile != INVALID_HANDLE_VALUE)
1100 {
Alexandre Julliard35363162002-05-22 21:32:49 +00001101 HANDLE mapping;
1102 switch (MODULE_GetBinaryType( hFile ))
Alexandre Julliardcd3afa82002-01-31 23:32:57 +00001103 {
Alexandre Julliard35363162002-05-22 21:32:49 +00001104 case BINARY_PE_EXE:
1105 case BINARY_PE_DLL:
1106 mapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
Alexandre Julliardcd3afa82002-01-31 23:32:57 +00001107 if (mapping)
1108 {
1109 hmod = (HMODULE)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1110 CloseHandle( mapping );
1111 }
Alexandre Julliard35363162002-05-22 21:32:49 +00001112 break;
1113 default:
1114 break;
Alexandre Julliardcd3afa82002-01-31 23:32:57 +00001115 }
Ove Kaaven10b34022001-04-16 19:32:48 +00001116 CloseHandle( hFile );
1117 }
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001118 if (hmod) return (HMODULE)((ULONG_PTR)hmod + 1);
Dmitry Timoshkovb77afe72001-03-21 03:38:03 +00001119 }
Ove Kaaven10b34022001-04-16 19:32:48 +00001120 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
1121 /* Fallback to normal behaviour */
Alexandre Julliard081ee942000-08-07 04:12:41 +00001122 }
Ove Kaaven10b34022001-04-16 19:32:48 +00001123
Alexandre Julliard64781642002-02-02 18:13:50 +00001124 RtlEnterCriticalSection( &loader_section );
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001125
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001126 wm = MODULE_LoadLibraryExA( libname, hfile, flags );
Ulrich Weigand0106f851999-12-08 03:28:14 +00001127 if ( wm )
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001128 {
Ulrich Weigand0106f851999-12-08 03:28:14 +00001129 if ( !MODULE_DllProcessAttach( wm, NULL ) )
1130 {
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001131 WARN_(module)("Attach failed for module '%s'.\n", libname);
Ulrich Weigand0106f851999-12-08 03:28:14 +00001132 MODULE_FreeLibrary(wm);
1133 SetLastError(ERROR_DLL_INIT_FAILED);
1134 wm = NULL;
1135 }
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001136 }
1137
Alexandre Julliard64781642002-02-02 18:13:50 +00001138 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001139 return wm ? wm->module : 0;
Marcus Meissner8220bc91998-10-11 11:10:27 +00001140}
1141
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001142/***********************************************************************
Bill Medland65fc1c92001-08-24 21:13:02 +00001143 * allocate_lib_dir
1144 *
1145 * helper for MODULE_LoadLibraryExA. Allocate space to hold the directory
1146 * portion of the provided name and put the name in it.
Vincent Béron9a624912002-05-31 23:06:46 +00001147 *
Bill Medland65fc1c92001-08-24 21:13:02 +00001148 */
1149static LPCSTR allocate_lib_dir(LPCSTR libname)
1150{
1151 LPCSTR p, pmax;
1152 LPSTR result;
1153 int length;
1154
1155 pmax = libname;
1156 if ((p = strrchr( pmax, '\\' ))) pmax = p + 1;
1157 if ((p = strrchr( pmax, '/' ))) pmax = p + 1; /* Naughty. MSDN says don't */
1158 if (pmax == libname && pmax[0] && pmax[1] == ':') pmax += 2;
1159
1160 length = pmax - libname;
1161
1162 result = HeapAlloc (GetProcessHeap(), 0, length+1);
1163
1164 if (result)
1165 {
1166 strncpy (result, libname, length);
1167 result [length] = '\0';
1168 }
1169
1170 return result;
1171}
1172
1173/***********************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001174 * MODULE_LoadLibraryExA (internal)
1175 *
1176 * Load a PE style module according to the load order.
1177 *
1178 * The HFILE parameter is not used and marked reserved in the SDK. I can
1179 * only guess that it should force a file to be mapped, but I rather
1180 * ignore the parameter because it would be extremely difficult to
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001181 * integrate this with different types of module representations.
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001182 *
Bill Medland65fc1c92001-08-24 21:13:02 +00001183 * libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion
1184 * on this function. When first called from LoadLibraryExA it will be
Vincent Béron9a624912002-05-31 23:06:46 +00001185 * NULL but thereafter it may point to a buffer containing the path
1186 * portion of the library name. Note that the recursion all occurs
1187 * within a Critical section (see LoadLibraryExA) so the use of a
Bill Medland65fc1c92001-08-24 21:13:02 +00001188 * static is acceptable.
1189 * (We have to use a static variable at some point anyway, to pass the
1190 * information from BUILTIN32_dlopen through dlopen and the builtin's
1191 * init function into load_library).
1192 * allocated_libdir is TRUE in the stack frame that allocated libdir
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001193 */
Alexandre Julliard267ca682002-07-31 17:20:00 +00001194WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HANDLE hfile, DWORD flags )
Marcus Meissner8220bc91998-10-11 11:10:27 +00001195{
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001196 DWORD err = GetLastError();
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001197 WINE_MODREF *pwm;
1198 int i;
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001199 enum loadorder_type loadorder[LOADORDER_NTYPES];
Alexandre Julliardbefbb0e2002-08-14 21:12:58 +00001200 LPSTR filename;
Gerard Patela77fd7d2001-01-10 22:54:02 +00001201 const char *filetype = "";
Bill Medland65fc1c92001-08-24 21:13:02 +00001202 DWORD found;
1203 BOOL allocated_libdir = FALSE;
1204 static LPCSTR libdir = NULL; /* See above */
Peter Gantenc7c42462000-08-28 21:33:28 +00001205
1206 if ( !libname ) return NULL;
1207
1208 filename = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1209 if ( !filename ) return NULL;
Bill Medlandca5b2012002-01-18 18:58:08 +00001210 *filename = 0; /* Just in case we don't set it before goto error */
Peter Gantenc7c42462000-08-28 21:33:28 +00001211
Alexandre Julliard64781642002-02-02 18:13:50 +00001212 RtlEnterCriticalSection( &loader_section );
Bill Medland65fc1c92001-08-24 21:13:02 +00001213
1214 if ((flags & LOAD_WITH_ALTERED_SEARCH_PATH) && FILE_contains_path(libname))
1215 {
1216 if (!(libdir = allocate_lib_dir(libname))) goto error;
1217 allocated_libdir = TRUE;
1218 }
1219
1220 if (!libdir || allocated_libdir)
1221 found = SearchPathA(NULL, libname, ".dll", MAX_PATH, filename, NULL);
1222 else
1223 found = DIR_SearchAlternatePath(libdir, libname, ".dll", MAX_PATH, filename, NULL);
1224
Peter Gantenc7c42462000-08-28 21:33:28 +00001225 /* build the modules filename */
Bill Medland65fc1c92001-08-24 21:13:02 +00001226 if (!found)
Peter Gantenc7c42462000-08-28 21:33:28 +00001227 {
Alexandre Julliardbefbb0e2002-08-14 21:12:58 +00001228 if (!MODULE_GetBuiltinPath( libname, ".dll", filename, MAX_PATH )) goto error;
Peter Gantenc7c42462000-08-28 21:33:28 +00001229 }
Marcus Meissner8220bc91998-10-11 11:10:27 +00001230
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001231 /* Check for already loaded module */
Bill Medland65fc1c92001-08-24 21:13:02 +00001232 if (!(pwm = MODULE_FindModule(filename)) && !FILE_contains_path(libname))
Eric Pouech208955c2000-09-10 03:14:00 +00001233 {
1234 LPSTR fn = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1235 if (fn)
1236 {
Vincent Béron9a624912002-05-31 23:06:46 +00001237 /* since the default loading mechanism uses a more detailed algorithm
Eric Pouech208955c2000-09-10 03:14:00 +00001238 * than SearchPath (like using PATH, which can even be modified between
1239 * two attempts of loading the same DLL), the look-up above (with
1240 * SearchPath) can have put the file in system directory, whereas it
1241 * has already been loaded but with a different path. So do a specific
1242 * look-up with filename (without any path)
1243 */
1244 strcpy ( fn, libname );
1245 /* if the filename doesn't have an extension append .DLL */
1246 if (!strrchr( fn, '.')) strcat( fn, ".dll" );
1247 if ((pwm = MODULE_FindModule( fn )) != NULL)
1248 strcpy( filename, fn );
1249 HeapFree( GetProcessHeap(), 0, fn );
1250 }
1251 }
1252 if (pwm)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001253 {
Bertho Stultiensf4b6e821999-04-22 08:56:40 +00001254 if(!(pwm->flags & WINE_MODREF_MARKER))
1255 pwm->refCount++;
Alexandre Julliard081ee942000-08-07 04:12:41 +00001256
1257 if ((pwm->flags & WINE_MODREF_DONT_RESOLVE_REFS) &&
1258 !(flags & DONT_RESOLVE_DLL_REFERENCES))
1259 {
Alexandre Julliard081ee942000-08-07 04:12:41 +00001260 pwm->flags &= ~WINE_MODREF_DONT_RESOLVE_REFS;
Bill Medland65fc1c92001-08-24 21:13:02 +00001261 PE_fixup_imports( pwm );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001262 }
Francois Gouget070e7492001-11-06 21:01:32 +00001263 TRACE("Already loaded module '%s' at 0x%08x, count=%d\n", filename, pwm->module, pwm->refCount);
Bill Medland65fc1c92001-08-24 21:13:02 +00001264 if (allocated_libdir)
1265 {
1266 HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1267 libdir = NULL;
1268 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001269 RtlLeaveCriticalSection( &loader_section );
Peter Gantenc7c42462000-08-28 21:33:28 +00001270 HeapFree ( GetProcessHeap(), 0, filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001271 return pwm;
1272 }
1273
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001274 MODULE_GetLoadOrder( loadorder, filename, TRUE);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001275
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001276 for(i = 0; i < LOADORDER_NTYPES; i++)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001277 {
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001278 if (loadorder[i] == LOADORDER_INVALID) break;
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001279 SetLastError( ERROR_FILE_NOT_FOUND );
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001280
1281 switch(loadorder[i])
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001282 {
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001283 case LOADORDER_DLL:
Peter Gantenc7c42462000-08-28 21:33:28 +00001284 TRACE("Trying native dll '%s'\n", filename);
1285 pwm = PE_LoadLibraryExA(filename, flags);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001286 filetype = "native";
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001287 break;
1288
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001289 case LOADORDER_SO:
Peter Gantenc7c42462000-08-28 21:33:28 +00001290 TRACE("Trying so-library '%s'\n", filename);
Alexandre Julliarde441d3c2000-12-22 22:50:12 +00001291 pwm = ELF_LoadLibraryExA(filename, flags);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001292 filetype = "so";
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001293 break;
1294
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001295 case LOADORDER_BI:
Peter Gantenc7c42462000-08-28 21:33:28 +00001296 TRACE("Trying built-in '%s'\n", filename);
1297 pwm = BUILTIN32_LoadLibraryExA(filename, flags);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001298 filetype = "builtin";
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001299 break;
1300
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001301 default:
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001302 pwm = NULL;
1303 break;
1304 }
1305
1306 if(pwm)
1307 {
1308 /* Initialize DLL just loaded */
Francois Gouget070e7492001-11-06 21:01:32 +00001309 TRACE("Loaded module '%s' at 0x%08x\n", filename, pwm->module);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001310 if (!TRACE_ON(module))
1311 TRACE_(loaddll)("Loaded module '%s' : %s\n", filename, filetype);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001312 /* Set the refCount here so that an attach failure will */
1313 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1314 pwm->refCount++;
1315
Bill Medland65fc1c92001-08-24 21:13:02 +00001316 if (allocated_libdir)
1317 {
1318 HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1319 libdir = NULL;
1320 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001321 RtlLeaveCriticalSection( &loader_section );
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001322 SetLastError( err ); /* restore last error */
Peter Gantenc7c42462000-08-28 21:33:28 +00001323 HeapFree ( GetProcessHeap(), 0, filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001324 return pwm;
1325 }
1326
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001327 if(GetLastError() != ERROR_FILE_NOT_FOUND)
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001328 {
Alexandre Julliard83886f22002-07-05 01:27:19 +00001329 WARN("Loading of %s DLL %s failed (error %ld).\n",
1330 filetype, filename, GetLastError());
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001331 break;
1332 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001333 }
1334
Peter Gantenc7c42462000-08-28 21:33:28 +00001335 error:
Bill Medland65fc1c92001-08-24 21:13:02 +00001336 if (allocated_libdir)
1337 {
1338 HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1339 libdir = NULL;
1340 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001341 RtlLeaveCriticalSection( &loader_section );
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001342 WARN("Failed to load module '%s'; error=%ld\n", filename, GetLastError());
Peter Gantenc7c42462000-08-28 21:33:28 +00001343 HeapFree ( GetProcessHeap(), 0, filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001344 return NULL;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001345}
1346
1347/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001348 * LoadLibraryA (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001349 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001350HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
1351 return LoadLibraryExA(libname,0,0);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001352}
1353
1354/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001355 * LoadLibraryW (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001356 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001357HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001358{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001359 return LoadLibraryExW(libnameW,0,0);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001360}
1361
1362/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001363 * LoadLibrary32 (KERNEL.452)
1364 * LoadSystemLibrary32 (KERNEL.482)
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001365 */
1366HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
1367{
1368 HMODULE hModule;
Alexandre Julliardab687972000-11-15 23:41:46 +00001369 DWORD count;
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001370
Alexandre Julliardab687972000-11-15 23:41:46 +00001371 ReleaseThunkLock( &count );
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001372 hModule = LoadLibraryA( libname );
Alexandre Julliardab687972000-11-15 23:41:46 +00001373 RestoreThunkLock( count );
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001374 return hModule;
1375}
1376
1377/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001378 * LoadLibraryExW (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001379 */
Jim Aston031f4fa1999-10-23 19:00:02 +00001380HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HANDLE hfile,DWORD flags)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001381{
1382 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001383 HMODULE ret = LoadLibraryExA( libnameA , hfile, flags );
Alexandre Julliard77b99181997-09-14 17:17:23 +00001384
1385 HeapFree( GetProcessHeap(), 0, libnameA );
1386 return ret;
1387}
1388
Bertho Stultiens1b346971999-04-11 14:52:24 +00001389/***********************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001390 * MODULE_FlushModrefs
Bertho Stultiens1b346971999-04-11 14:52:24 +00001391 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001392 * NOTE: Assumes that the process critical section is held!
Bertho Stultiens1b346971999-04-11 14:52:24 +00001393 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001394 * Remove all unused modrefs and call the internal unloading routines
1395 * for the library type.
Bertho Stultiens1b346971999-04-11 14:52:24 +00001396 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001397static void MODULE_FlushModrefs(void)
Bertho Stultiens1b346971999-04-11 14:52:24 +00001398{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001399 WINE_MODREF *wm, *next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001400
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001401 for(wm = MODULE_modref_list; wm; wm = next)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001402 {
1403 next = wm->next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001404
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001405 if(wm->refCount)
1406 continue;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001407
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001408 /* Unlink this modref from the chain */
1409 if(wm->next)
1410 wm->next->prev = wm->prev;
1411 if(wm->prev)
1412 wm->prev->next = wm->next;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001413 if(wm == MODULE_modref_list)
1414 MODULE_modref_list = wm->next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001415
Alexandre Julliard081ee942000-08-07 04:12:41 +00001416 TRACE(" unloading %s\n", wm->filename);
Alexandre Julliard69622db2002-06-25 00:23:23 +00001417 if (!TRACE_ON(module))
1418 TRACE_(loaddll)("Unloaded module '%s' : %s\n", wm->filename,
1419 wm->dlhandle ? "builtin" : "native" );
1420
Alexandre Julliardc9cf68d2001-05-02 01:12:27 +00001421 if (wm->dlhandle) wine_dll_unload( wm->dlhandle );
Alexandre Julliard2418edb2001-05-10 19:17:54 +00001422 else UnmapViewOfFile( (LPVOID)wm->module );
Andreas Mohrcabee392000-10-25 21:22:27 +00001423 FreeLibrary16(wm->hDummyMod);
1424 HeapFree( GetProcessHeap(), 0, wm->deps );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001425 HeapFree( GetProcessHeap(), 0, wm );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001426 }
Bertho Stultiens1b346971999-04-11 14:52:24 +00001427}
1428
Alexandre Julliard77b99181997-09-14 17:17:23 +00001429/***********************************************************************
Patrik Stridvall044855c2001-07-11 18:56:41 +00001430 * FreeLibrary (KERNEL32.@)
1431 * FreeLibrary32 (KERNEL.486)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001432 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001433BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001434{
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001435 BOOL retv = FALSE;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001436 WINE_MODREF *wm;
Ulrich Weigande469a581999-03-27 16:45:57 +00001437
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001438 if (!hLibModule)
1439 {
1440 SetLastError( ERROR_INVALID_HANDLE );
1441 return FALSE;
1442 }
1443
1444 if ((ULONG_PTR)hLibModule & 1)
1445 {
1446 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
1447 char *ptr = (char *)hLibModule - 1;
1448 UnmapViewOfFile( ptr );
1449 return TRUE;
1450 }
1451
Alexandre Julliard64781642002-02-02 18:13:50 +00001452 RtlEnterCriticalSection( &loader_section );
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001453 free_lib_count++;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001454
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001455 if ((wm = MODULE32_LookupHMODULE( hLibModule ))) retv = MODULE_FreeLibrary( wm );
Bertho Stultiens1b346971999-04-11 14:52:24 +00001456
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001457 free_lib_count--;
Alexandre Julliard64781642002-02-02 18:13:50 +00001458 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiens1b346971999-04-11 14:52:24 +00001459
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001460 return retv;
1461}
1462
1463/***********************************************************************
1464 * MODULE_DecRefCount
1465 *
1466 * NOTE: Assumes that the process critical section is held!
1467 */
1468static void MODULE_DecRefCount( WINE_MODREF *wm )
1469{
1470 int i;
1471
1472 if ( wm->flags & WINE_MODREF_MARKER )
1473 return;
1474
1475 if ( wm->refCount <= 0 )
1476 return;
1477
1478 --wm->refCount;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001479 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001480
1481 if ( wm->refCount == 0 )
1482 {
1483 wm->flags |= WINE_MODREF_MARKER;
1484
1485 for ( i = 0; i < wm->nDeps; i++ )
1486 if ( wm->deps[i] )
1487 MODULE_DecRefCount( wm->deps[i] );
1488
1489 wm->flags &= ~WINE_MODREF_MARKER;
1490 }
1491}
1492
1493/***********************************************************************
1494 * MODULE_FreeLibrary
1495 *
1496 * NOTE: Assumes that the process critical section is held!
1497 */
1498BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
1499{
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001500 TRACE("(%s) - START\n", wm->modname );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001501
1502 /* Recursively decrement reference counts */
1503 MODULE_DecRefCount( wm );
1504
1505 /* Call process detach notifications */
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001506 if ( free_lib_count <= 1 )
Alexandre Julliardd131a171999-05-23 20:02:04 +00001507 {
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001508 MODULE_DllProcessDetach( FALSE, NULL );
Alexandre Julliard67a74992001-02-27 02:09:16 +00001509 SERVER_START_REQ( unload_dll )
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001510 {
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001511 req->base = (void *)wm->module;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001512 wine_server_call( req );
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001513 }
1514 SERVER_END_REQ;
Alexandre Julliardf93eb3e2000-04-28 20:26:35 +00001515 MODULE_FlushModrefs();
Alexandre Julliardd131a171999-05-23 20:02:04 +00001516 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001517
Ulrich Czekallacc279982000-03-08 18:41:22 +00001518 TRACE("END\n");
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001519
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001520 return TRUE;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001521}
1522
Bertho Stultiens1b346971999-04-11 14:52:24 +00001523
Sergey Turchanov5ffd2df1999-03-22 12:35:48 +00001524/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001525 * FreeLibraryAndExitThread (KERNEL32.@)
Sergey Turchanov5ffd2df1999-03-22 12:35:48 +00001526 */
1527VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
1528{
1529 FreeLibrary(hLibModule);
1530 ExitThread(dwExitCode);
1531}
Alexandre Julliard2787be81995-05-22 18:23:01 +00001532
1533/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001534 * PrivateLoadLibrary (KERNEL32.@)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001535 *
1536 * FIXME: rough guesswork, don't know what "Private" means
1537 */
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001538HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001539{
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001540 return LoadLibrary16(libname);
Alexandre Julliarde658d821997-11-30 17:45:40 +00001541}
1542
1543
Alexandre Julliard2787be81995-05-22 18:23:01 +00001544
1545/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001546 * PrivateFreeLibrary (KERNEL32.@)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001547 *
1548 * FIXME: rough guesswork, don't know what "Private" means
1549 */
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001550void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001551{
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001552 FreeLibrary16(handle);
Alexandre Julliarde658d821997-11-30 17:45:40 +00001553}
1554
1555
1556/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001557 * GetProcAddress16 (KERNEL32.37)
Alexandre Julliard641ee761997-08-04 16:34:36 +00001558 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1559 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001560FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
Alexandre Julliard641ee761997-08-04 16:34:36 +00001561{
Alexandre Julliard641ee761997-08-04 16:34:36 +00001562 if (!hModule) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001563 WARN("hModule may not be 0!\n");
Alexandre Julliard641ee761997-08-04 16:34:36 +00001564 return (FARPROC16)0;
1565 }
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001566 if (HIWORD(hModule))
1567 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001568 WARN("hModule is Win32 handle (%08x)\n", hModule );
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001569 return (FARPROC16)0;
1570 }
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001571 return GetProcAddress16( LOWORD(hModule), name );
Alexandre Julliard641ee761997-08-04 16:34:36 +00001572}
1573
1574/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001575 * GetProcAddress (KERNEL.50)
Alexandre Julliard2787be81995-05-22 18:23:01 +00001576 */
Alexandre Julliardac7efef2000-11-27 21:54:01 +00001577FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
Alexandre Julliard2787be81995-05-22 18:23:01 +00001578{
1579 WORD ordinal;
Alexandre Julliardca22b331996-07-12 19:02:39 +00001580 FARPROC16 ret;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001581
1582 if (!hModule) hModule = GetCurrentTask();
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001583 hModule = GetExePtr( hModule );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001584
1585 if (HIWORD(name) != 0)
1586 {
Alexandre Julliardac7efef2000-11-27 21:54:01 +00001587 ordinal = NE_GetOrdinal( hModule, name );
1588 TRACE("%04x '%s'\n", hModule, name );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001589 }
1590 else
1591 {
1592 ordinal = LOWORD(name);
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001593 TRACE("%04x %04x\n", hModule, ordinal );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001594 }
Alexandre Julliardca22b331996-07-12 19:02:39 +00001595 if (!ordinal) return (FARPROC16)0;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001596
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001597 ret = NE_GetEntryPoint( hModule, ordinal );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001598
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001599 TRACE("returning %08x\n", (UINT)ret );
Alexandre Julliardca22b331996-07-12 19:02:39 +00001600 return ret;
1601}
1602
1603
1604/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001605 * GetProcAddress (KERNEL32.@)
Alexandre Julliardca22b331996-07-12 19:02:39 +00001606 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001607FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
Alexandre Julliardca22b331996-07-12 19:02:39 +00001608{
Alexandre Julliard891d23e2002-07-24 19:04:41 +00001609 return MODULE_GetProcAddress( hModule, function, -1, TRUE );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001610}
1611
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001612/***********************************************************************
Patrik Stridvall54fe8382000-04-06 20:21:16 +00001613 * GetProcAddress32 (KERNEL.453)
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001614 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001615FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001616{
Alexandre Julliard891d23e2002-07-24 19:04:41 +00001617 return MODULE_GetProcAddress( hModule, function, -1, FALSE );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001618}
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001619
1620/***********************************************************************
Patrik Stridvall2d6457c2000-03-28 20:22:59 +00001621 * MODULE_GetProcAddress (internal)
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001622 */
Vincent Béron9a624912002-05-31 23:06:46 +00001623FARPROC MODULE_GetProcAddress(
Alexandre Julliarda3960291999-02-26 11:11:13 +00001624 HMODULE hModule, /* [in] current module handle */
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001625 LPCSTR function, /* [in] function to be looked up */
Alexandre Julliard891d23e2002-07-24 19:04:41 +00001626 int hint,
Alexandre Julliarda3960291999-02-26 11:11:13 +00001627 BOOL snoop )
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001628{
Alexandre Julliard081ee942000-08-07 04:12:41 +00001629 WINE_MODREF *wm;
1630 FARPROC retproc = 0;
Alexandre Julliardca22b331996-07-12 19:02:39 +00001631
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001632 if (HIWORD(function))
Alexandre Julliard891d23e2002-07-24 19:04:41 +00001633 TRACE_(win32)("(%08lx,%s (%d))\n",(DWORD)hModule,function,hint);
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001634 else
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001635 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
Alexandre Julliard081ee942000-08-07 04:12:41 +00001636
Alexandre Julliard64781642002-02-02 18:13:50 +00001637 RtlEnterCriticalSection( &loader_section );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001638 if ((wm = MODULE32_LookupHMODULE( hModule )))
Alexandre Julliarde658d821997-11-30 17:45:40 +00001639 {
Alexandre Julliard891d23e2002-07-24 19:04:41 +00001640 retproc = wm->find_export( wm, function, hint, snoop );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001641 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001642 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001643 RtlLeaveCriticalSection( &loader_section );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001644 return retproc;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001645}
1646
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001647
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001648/***************************************************************************
1649 * HasGPHandler (KERNEL.338)
1650 */
1651
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001652#include "pshpack1.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001653typedef struct _GPHANDLERDEF
1654{
1655 WORD selector;
1656 WORD rangeStart;
1657 WORD rangeEnd;
1658 WORD handler;
1659} GPHANDLERDEF;
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001660#include "poppack.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001661
Alexandre Julliarda3960291999-02-26 11:11:13 +00001662SEGPTR WINAPI HasGPHandler16( SEGPTR address )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001663{
1664 HMODULE16 hModule;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001665 int gpOrdinal;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001666 SEGPTR gpPtr;
1667 GPHANDLERDEF *gpHandler;
Vincent Béron9a624912002-05-31 23:06:46 +00001668
Alexandre Julliarda3960291999-02-26 11:11:13 +00001669 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001670 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1671 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001672 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
Alexandre Julliard982a2232000-12-13 20:20:09 +00001673 && (gpHandler = MapSL( gpPtr )) != NULL )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001674 {
1675 while (gpHandler->selector)
1676 {
1677 if ( SELECTOROF(address) == gpHandler->selector
1678 && OFFSETOF(address) >= gpHandler->rangeStart
1679 && OFFSETOF(address) < gpHandler->rangeEnd )
Alexandre Julliard982a2232000-12-13 20:20:09 +00001680 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001681 gpHandler++;
1682 }
1683 }
1684
1685 return 0;
1686}