blob: 5d7c3066f01979945b4784f7cfa775df4022a072 [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
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +000021#include <assert.h>
Alexandre Julliard594997c1995-04-30 10:05:20 +000022#include <fcntl.h>
23#include <stdlib.h>
Jeremy Whited3e22d92000-02-10 19:03:02 +000024#include <stdio.h>
Alexandre Julliard594997c1995-04-30 10:05:20 +000025#include <string.h>
26#include <sys/types.h>
27#include <unistd.h>
Jeremy Whited3e22d92000-02-10 19:03:02 +000028#include "wine/winbase16.h"
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000029#include "winerror.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000030#include "heap.h"
Aric Stewarte4d09322000-12-03 03:14:29 +000031#include "file.h"
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000032#include "module.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000033#include "wine/debug.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000034#include "wine/server.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000035
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000036WINE_DEFAULT_DEBUG_CHANNEL(module);
37WINE_DECLARE_DEBUG_CHANNEL(win32);
38WINE_DECLARE_DEBUG_CHANNEL(loaddll);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000039
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000040WINE_MODREF *MODULE_modref_list = NULL;
41
42static WINE_MODREF *exe_modref;
43static int free_lib_count; /* recursion depth of FreeLibrary calls */
Alexandre Julliard014a8bb2000-12-20 18:41:34 +000044static int process_detaching; /* set on process detach to avoid deadlocks with thread detach */
Alexandre Julliard594997c1995-04-30 10:05:20 +000045
Alexandre Julliard64781642002-02-02 18:13:50 +000046static CRITICAL_SECTION loader_section = CRITICAL_SECTION_INIT( "loader_section" );
47
Alexandre Julliard5edf4e12001-07-26 20:12:54 +000048/***********************************************************************
49 * wait_input_idle
50 *
51 * Wrapper to call WaitForInputIdle USER function
52 */
53typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
54
55static DWORD wait_input_idle( HANDLE process, DWORD timeout )
56{
57 HMODULE mod = GetModuleHandleA( "user32.dll" );
58 if (mod)
59 {
60 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
61 if (ptr) return ptr( process, timeout );
62 }
63 return 0;
64}
65
66
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000067/*************************************************************************
68 * MODULE32_LookupHMODULE
69 * looks for the referenced HMODULE in the current process
Alexandre Julliard081ee942000-08-07 04:12:41 +000070 * NOTE: Assumes that the process critical section is held!
Alexandre Julliard329f0681996-04-14 13:21:20 +000071 */
Alexandre Julliard081ee942000-08-07 04:12:41 +000072static WINE_MODREF *MODULE32_LookupHMODULE( HMODULE hmod )
Ulrich Weigand1d90d691999-02-24 14:27:07 +000073{
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000074 WINE_MODREF *wm;
Alexandre Julliard77b99181997-09-14 17:17:23 +000075
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000076 if (!hmod)
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000077 return exe_modref;
Ulrich Weigand1d90d691999-02-24 14:27:07 +000078
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000079 if (!HIWORD(hmod)) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +000080 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod);
Alexandre Julliard081ee942000-08-07 04:12:41 +000081 SetLastError( ERROR_INVALID_HANDLE );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000082 return NULL;
83 }
Alexandre Julliardbecb9a32000-12-11 03:48:15 +000084 for ( wm = MODULE_modref_list; wm; wm=wm->next )
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000085 if (wm->module == hmod)
86 return wm;
Alexandre Julliard081ee942000-08-07 04:12:41 +000087 SetLastError( ERROR_INVALID_HANDLE );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000088 return NULL;
89}
90
Ulrich Weiganda3527cf1998-10-11 19:31:10 +000091/*************************************************************************
Alexandre Julliard081ee942000-08-07 04:12:41 +000092 * MODULE_AllocModRef
93 *
94 * Allocate a WINE_MODREF structure and add it to the process list
95 * NOTE: Assumes that the process critical section is held!
96 */
97WINE_MODREF *MODULE_AllocModRef( HMODULE hModule, LPCSTR filename )
98{
99 WINE_MODREF *wm;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000100
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000101 DWORD long_len = strlen( filename );
102 DWORD short_len = GetShortPathNameA( filename, NULL, 0 );
103
104 if ((wm = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
105 sizeof(*wm) + long_len + short_len + 1 )))
Alexandre Julliard081ee942000-08-07 04:12:41 +0000106 {
107 wm->module = hModule;
108 wm->tlsindex = -1;
109
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000110 wm->filename = wm->data;
111 memcpy( wm->filename, filename, long_len + 1 );
Alexandre Julliard081ee942000-08-07 04:12:41 +0000112 if ((wm->modname = strrchr( wm->filename, '\\' ))) wm->modname++;
113 else wm->modname = wm->filename;
114
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000115 wm->short_filename = wm->filename + long_len + 1;
116 GetShortPathNameA( wm->filename, wm->short_filename, short_len + 1 );
Alexandre Julliard081ee942000-08-07 04:12:41 +0000117 if ((wm->short_modname = strrchr( wm->short_filename, '\\' ))) wm->short_modname++;
118 else wm->short_modname = wm->short_filename;
119
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000120 wm->next = MODULE_modref_list;
Alexandre Julliard081ee942000-08-07 04:12:41 +0000121 if (wm->next) wm->next->prev = wm;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000122 MODULE_modref_list = wm;
123
124 if (!(PE_HEADER(hModule)->FileHeader.Characteristics & IMAGE_FILE_DLL))
125 {
126 if (!exe_modref) exe_modref = wm;
127 else FIXME( "Trying to load second .EXE file: %s\n", filename );
128 }
Alexandre Julliard081ee942000-08-07 04:12:41 +0000129 }
130 return wm;
131}
132
133/*************************************************************************
Andreas Mohr96293d42000-07-08 18:28:03 +0000134 * MODULE_InitDLL
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000135 */
Andreas Mohr96293d42000-07-08 18:28:03 +0000136static BOOL MODULE_InitDLL( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000137{
138 BOOL retv = TRUE;
139
140 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
141 "THREAD_ATTACH", "THREAD_DETACH" };
142 assert( wm );
143
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000144 /* Skip calls for modules loaded with special load flags */
145
Alexandre Julliard081ee942000-08-07 04:12:41 +0000146 if (wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) return TRUE;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000147
Alexandre Julliardf93eb3e2000-04-28 20:26:35 +0000148 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000149
150 /* Call the initialization routine */
Alexandre Julliard081ee942000-08-07 04:12:41 +0000151 retv = PE_InitDLL( wm->module, type, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000152
Alexandre Julliardf93eb3e2000-04-28 20:26:35 +0000153 /* The state of the module list may have changed due to the call
154 to PE_InitDLL. We cannot assume that this module has not been
155 deleted. */
156 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000157
158 return retv;
159}
160
161/*************************************************************************
162 * MODULE_DllProcessAttach
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000163 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000164 * Send the process attach notification to all DLLs the given module
165 * depends on (recursively). This is somewhat complicated due to the fact that
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000166 *
167 * - we have to respect the module dependencies, i.e. modules implicitly
168 * referenced by another module have to be initialized before the module
169 * itself can be initialized
170 *
171 * - the initialization routine of a DLL can itself call LoadLibrary,
172 * thereby introducing a whole new set of dependencies (even involving
173 * the 'old' modules) at any time during the whole process
174 *
175 * (Note that this routine can be recursively entered not only directly
176 * from itself, but also via LoadLibrary from one of the called initialization
177 * routines.)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000178 *
179 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
180 * the process *detach* notifications to be sent in the correct order.
181 * This must not only take into account module dependencies, but also
182 * 'hidden' dependencies created by modules calling LoadLibrary in their
183 * attach notification routine.
184 *
185 * The strategy is rather simple: we move a WINE_MODREF to the head of the
186 * list after the attach notification has returned. This implies that the
187 * detach notifications are called in the reverse of the sequence the attach
188 * notifications *returned*.
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000189 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000190BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000191{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000192 BOOL retv = TRUE;
193 int i;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000194
Alexandre Julliard64781642002-02-02 18:13:50 +0000195 RtlEnterCriticalSection( &loader_section );
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000196
Alexandre Julliard64781642002-02-02 18:13:50 +0000197 if (!wm)
198 {
199 wm = exe_modref;
200 PE_InitTls();
201 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000202 assert( wm );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000203
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000204 /* prevent infinite recursion in case of cyclical dependencies */
205 if ( ( wm->flags & WINE_MODREF_MARKER )
206 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000207 goto done;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000208
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000209 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000210
211 /* Tag current MODREF to prevent recursive loop */
Ulrich Weigande469a581999-03-27 16:45:57 +0000212 wm->flags |= WINE_MODREF_MARKER;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000213
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000214 /* Recursively attach all DLLs this one depends on */
215 for ( i = 0; retv && i < wm->nDeps; i++ )
216 if ( wm->deps[i] )
217 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
218
219 /* Call DLL entry point */
220 if ( retv )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000221 {
Andreas Mohr96293d42000-07-08 18:28:03 +0000222 retv = MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000223 if ( retv )
Ulrich Weigande469a581999-03-27 16:45:57 +0000224 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000225 }
Ulrich Weigande469a581999-03-27 16:45:57 +0000226
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000227 /* Re-insert MODREF at head of list */
228 if ( retv && wm->prev )
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000229 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000230 wm->prev->next = wm->next;
231 if ( wm->next ) wm->next->prev = wm->prev;
232
233 wm->prev = NULL;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000234 wm->next = MODULE_modref_list;
235 MODULE_modref_list = wm->next->prev = wm;
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000236 }
Ulrich Weigande469a581999-03-27 16:45:57 +0000237
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000238 /* Remove recursion flag */
239 wm->flags &= ~WINE_MODREF_MARKER;
Ulrich Weigande469a581999-03-27 16:45:57 +0000240
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000241 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
Ulrich Weigande469a581999-03-27 16:45:57 +0000242
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000243 done:
Alexandre Julliard64781642002-02-02 18:13:50 +0000244 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000245 return retv;
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000246}
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000247
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000248/*************************************************************************
249 * MODULE_DllProcessDetach
250 *
251 * Send DLL process detach notifications. See the comment about calling
252 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
253 * is set, only DLLs with zero refcount are notified.
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000254 */
255void MODULE_DllProcessDetach( BOOL bForceDetach, LPVOID lpReserved )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000256{
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000257 WINE_MODREF *wm;
258
Alexandre Julliard64781642002-02-02 18:13:50 +0000259 RtlEnterCriticalSection( &loader_section );
Alexandre Julliard014a8bb2000-12-20 18:41:34 +0000260 if (bForceDetach) process_detaching = 1;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000261 do
262 {
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000263 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000264 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000265 /* Check whether to detach this DLL */
266 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
267 continue;
268 if ( wm->refCount > 0 && !bForceDetach )
269 continue;
270
271 /* Call detach notification */
272 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
Andreas Mohr96293d42000-07-08 18:28:03 +0000273 MODULE_InitDLL( wm, DLL_PROCESS_DETACH, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000274
275 /* Restart at head of WINE_MODREF list, as entries might have
276 been added and/or removed while performing the call ... */
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000277 break;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000278 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000279 } while ( wm );
Alexandre Julliard12f29b52000-03-17 15:16:57 +0000280
Alexandre Julliard64781642002-02-02 18:13:50 +0000281 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000282}
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000283
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000284/*************************************************************************
285 * MODULE_DllThreadAttach
286 *
287 * Send DLL thread attach notifications. These are sent in the
288 * reverse sequence of process detach notification.
289 *
290 */
291void MODULE_DllThreadAttach( LPVOID lpReserved )
292{
293 WINE_MODREF *wm;
294
Alexandre Julliard014a8bb2000-12-20 18:41:34 +0000295 /* don't do any attach calls if process is exiting */
296 if (process_detaching) return;
297 /* FIXME: there is still a race here */
298
Alexandre Julliard64781642002-02-02 18:13:50 +0000299 RtlEnterCriticalSection( &loader_section );
300
301 PE_InitTls();
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000302
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000303 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000304 if ( !wm->next )
305 break;
306
307 for ( ; wm; wm = wm->prev )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000308 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000309 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
310 continue;
311 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
312 continue;
Ulrich Weigande469a581999-03-27 16:45:57 +0000313
Andreas Mohr96293d42000-07-08 18:28:03 +0000314 MODULE_InitDLL( wm, DLL_THREAD_ATTACH, lpReserved );
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000315 }
316
Alexandre Julliard64781642002-02-02 18:13:50 +0000317 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000318}
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000319
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000320/*************************************************************************
321 * MODULE_DllThreadDetach
322 *
323 * Send DLL thread detach notifications. These are sent in the
324 * same sequence as process detach notification.
325 *
326 */
327void MODULE_DllThreadDetach( LPVOID lpReserved )
328{
329 WINE_MODREF *wm;
330
Alexandre Julliard014a8bb2000-12-20 18:41:34 +0000331 /* don't do any detach calls if process is exiting */
332 if (process_detaching) return;
333 /* FIXME: there is still a race here */
334
Alexandre Julliard64781642002-02-02 18:13:50 +0000335 RtlEnterCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000336
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000337 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000338 {
339 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
340 continue;
341 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
342 continue;
343
Andreas Mohr96293d42000-07-08 18:28:03 +0000344 MODULE_InitDLL( wm, DLL_THREAD_DETACH, lpReserved );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000345 }
346
Alexandre Julliard64781642002-02-02 18:13:50 +0000347 RtlLeaveCriticalSection( &loader_section );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000348}
349
Ulrich Weigande469a581999-03-27 16:45:57 +0000350/****************************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000351 * DisableThreadLibraryCalls (KERNEL32.@)
Ulrich Weigande469a581999-03-27 16:45:57 +0000352 *
353 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
354 */
355BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
356{
Bertho Stultiens1b346971999-04-11 14:52:24 +0000357 WINE_MODREF *wm;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000358 BOOL retval = TRUE;
Ulrich Weigande469a581999-03-27 16:45:57 +0000359
Alexandre Julliard64781642002-02-02 18:13:50 +0000360 RtlEnterCriticalSection( &loader_section );
Ulrich Weigande469a581999-03-27 16:45:57 +0000361
Bertho Stultiens1b346971999-04-11 14:52:24 +0000362 wm = MODULE32_LookupHMODULE( hModule );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000363 if ( !wm )
364 retval = FALSE;
365 else
366 wm->flags |= WINE_MODREF_NO_DLL_CALLS;
367
Alexandre Julliard64781642002-02-02 18:13:50 +0000368 RtlLeaveCriticalSection( &loader_section );
Ulrich Weigande469a581999-03-27 16:45:57 +0000369
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000370 return retval;
Ulrich Weigande469a581999-03-27 16:45:57 +0000371}
372
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000373
374/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000375 * MODULE_CreateDummyModule
376 *
377 * Create a dummy NE module for Win32 or Winelib.
378 */
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000379HMODULE16 MODULE_CreateDummyModule( LPCSTR filename, HMODULE module32 )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000380{
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000381 HMODULE16 hModule;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000382 NE_MODULE *pModule;
383 SEGTABLEENTRY *pSegment;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000384 char *pStr,*s;
Francois Gougetbaa9bf91999-12-27 05:24:06 +0000385 unsigned int len;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000386 const char* basename;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000387 OFSTRUCT *ofs;
388 int of_size, size;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000389
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000390 /* Extract base filename */
391 basename = strrchr(filename, '\\');
392 if (!basename) basename = filename;
393 else basename++;
394 len = strlen(basename);
395 if ((s = strchr(basename, '.'))) len = s - basename;
396
397 /* Allocate module */
398 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
399 + strlen(filename) + 1;
400 size = sizeof(NE_MODULE) +
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000401 /* loaded file info */
Ulrich Weigandacefd162000-12-29 05:09:15 +0000402 ((of_size + 3) & ~3) +
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000403 /* segment table: DS,CS */
404 2 * sizeof(SEGTABLEENTRY) +
405 /* name table */
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000406 len + 2 +
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000407 /* several empty tables */
408 8;
409
410 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000411 if (!hModule) return (HMODULE16)11; /* invalid exe */
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000412
Alexandre Julliarda3960291999-02-26 11:11:13 +0000413 FarSetOwner16( hModule, hModule );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000414 pModule = (NE_MODULE *)GlobalLock16( hModule );
415
416 /* Set all used entries */
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000417 pModule->magic = IMAGE_OS2_SIGNATURE;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000418 pModule->count = 1;
419 pModule->next = 0;
420 pModule->flags = 0;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000421 pModule->dgroup = 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000422 pModule->ss = 1;
423 pModule->cs = 2;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000424 pModule->heap_size = 0;
425 pModule->stack_size = 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000426 pModule->seg_count = 2;
427 pModule->modref_count = 0;
428 pModule->nrname_size = 0;
429 pModule->fileinfo = sizeof(NE_MODULE);
430 pModule->os_flags = NE_OSFLAGS_WINDOWS;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000431 pModule->self = hModule;
Alexandre Julliardb4459522000-04-15 21:00:55 +0000432 pModule->module32 = module32;
433
434 /* Set version and flags */
435 if (module32)
436 {
437 pModule->expected_version =
438 ((PE_HEADER(module32)->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
439 (PE_HEADER(module32)->OptionalHeader.MinorSubsystemVersion & 0xff);
440 pModule->flags |= NE_FFLAGS_WIN32;
441 if (PE_HEADER(module32)->FileHeader.Characteristics & IMAGE_FILE_DLL)
442 pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
443 }
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000444
445 /* Set loaded file information */
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000446 ofs = (OFSTRUCT *)(pModule + 1);
447 memset( ofs, 0, of_size );
448 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
449 strcpy( ofs->szPathName, filename );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000450
Ulrich Weigandacefd162000-12-29 05:09:15 +0000451 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000452 pModule->seg_table = (int)pSegment - (int)pModule;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000453 /* Data segment */
454 pSegment->size = 0;
455 pSegment->flags = NE_SEGFLAGS_DATA;
456 pSegment->minsize = 0x1000;
457 pSegment++;
458 /* Code segment */
459 pSegment->flags = 0;
460 pSegment++;
461
462 /* Module name */
463 pStr = (char *)pSegment;
464 pModule->name_table = (int)pStr - (int)pModule;
Francois Gougetbaa9bf91999-12-27 05:24:06 +0000465 assert(len<256);
Alexandre Julliard17216f51997-10-12 16:30:17 +0000466 *pStr = len;
Francois Gougetbaa9bf91999-12-27 05:24:06 +0000467 lstrcpynA( pStr+1, basename, len+1 );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000468 pStr += len+2;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000469
470 /* All tables zero terminated */
471 pModule->res_table = pModule->import_table = pModule->entry_table =
472 (int)pStr - (int)pModule;
473
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000474 NE_RegisterModule( pModule );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000475 return hModule;
476}
477
478
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000479/**********************************************************************
Peter Gantenc7c42462000-08-28 21:33:28 +0000480 * MODULE_FindModule
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000481 *
482 * Find a (loaded) win32 module depending on path
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000483 *
484 * RETURNS
485 * the module handle if found
486 * 0 if not
487 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000488WINE_MODREF *MODULE_FindModule(
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000489 LPCSTR path /* [in] pathname of module/library to be found */
490) {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000491 WINE_MODREF *wm;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000492 char dllname[260], *p;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000493
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000494 /* Append .DLL to name if no extension present */
495 strcpy( dllname, path );
496 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
497 strcat( dllname, ".DLL" );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000498
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000499 for ( wm = MODULE_modref_list; wm; wm = wm->next )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000500 {
Aric Stewarte4d09322000-12-03 03:14:29 +0000501 if ( !FILE_strcasecmp( dllname, wm->modname ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000502 break;
Aric Stewarte4d09322000-12-03 03:14:29 +0000503 if ( !FILE_strcasecmp( dllname, wm->filename ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000504 break;
Aric Stewarte4d09322000-12-03 03:14:29 +0000505 if ( !FILE_strcasecmp( dllname, wm->short_modname ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000506 break;
Aric Stewarte4d09322000-12-03 03:14:29 +0000507 if ( !FILE_strcasecmp( dllname, wm->short_filename ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000508 break;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000509 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000510
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000511 return wm;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000512}
513
Andreas Mohrb021fe22000-07-26 18:02:28 +0000514
515/* Check whether a file is an OS/2 or a very old Windows executable
516 * by testing on import of KERNEL.
517 *
518 * FIXME: is reading the module imports the only way of discerning
519 * old Windows binaries from OS/2 ones ? At least it seems so...
520 */
521static DWORD MODULE_Decide_OS2_OldWin(HANDLE hfile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
522{
523 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
524 DWORD type = SCS_OS216_BINARY;
525 LPWORD modtab = NULL;
526 LPSTR nametab = NULL;
527 DWORD len;
528 int i;
529
530 /* read modref table */
531 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
532 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
533 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
534 || (len != ne->ne_cmod*sizeof(WORD)) )
535 goto broken;
536
537 /* read imported names table */
538 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
539 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
540 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
541 || (len != ne->ne_enttab - ne->ne_imptab) )
542 goto broken;
543
544 for (i=0; i < ne->ne_cmod; i++)
545 {
546 LPSTR module = &nametab[modtab[i]];
547 TRACE("modref: %.*s\n", module[0], &module[1]);
548 if (!(strncmp(&module[1], "KERNEL", module[0])))
549 { /* very old Windows file */
550 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
551 type = SCS_WOW_BINARY;
552 goto good;
553 }
554 }
555
556broken:
557 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
558
559good:
560 HeapFree( GetProcessHeap(), 0, modtab);
561 HeapFree( GetProcessHeap(), 0, nametab);
562 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
563 return type;
564}
565
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000566/***********************************************************************
567 * MODULE_GetBinaryType
568 *
569 * The GetBinaryType function determines whether a file is executable
570 * or not and if it is it returns what type of executable it is.
571 * The type of executable is a property that determines in which
572 * subsystem an executable file runs under.
573 *
574 * Binary types returned:
575 * SCS_32BIT_BINARY: A Win32 based application
576 * SCS_DOS_BINARY: An MS-Dos based application
577 * SCS_WOW_BINARY: A Win16 based application
578 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
579 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000580 * SCS_OS216_BINARY: A 16bit OS/2 based application
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000581 *
582 * Returns TRUE if the file is an executable in which case
583 * the value pointed by lpBinaryType is set.
584 * Returns FALSE if the file is not an executable or if the function fails.
585 *
586 * To do so it opens the file and reads in the header information
Andreas Mohr8952dea1999-12-12 20:16:42 +0000587 * if the extended header information is not present it will
588 * assume that the file is a DOS executable.
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000589 * If the extended header information is present it will
Andreas Mohr8952dea1999-12-12 20:16:42 +0000590 * determine if the file is a 16 or 32 bit Windows executable
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000591 * by check the flags in the header.
592 *
593 * Note that .COM and .PIF files are only recognized by their
594 * file name extension; but Windows does it the same way ...
Alexandre Julliard594997c1995-04-30 10:05:20 +0000595 */
Alexandre Julliarde0875082000-11-08 04:33:20 +0000596static BOOL MODULE_GetBinaryType( HANDLE hfile, LPCSTR filename, LPDWORD lpBinaryType )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000597{
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000598 IMAGE_DOS_HEADER mz_header;
599 char magic[4], *ptr;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000600 DWORD len;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000601
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000602 /* Seek to the start of the file and read the DOS header information.
603 */
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000604 if ( SetFilePointer( hfile, 0, NULL, SEEK_SET ) != -1
605 && ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL )
606 && len == sizeof(mz_header) )
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000607 {
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000608 /* Now that we have the header check the e_magic field
609 * to see if this is a dos image.
610 */
611 if ( mz_header.e_magic == IMAGE_DOS_SIGNATURE )
612 {
613 BOOL lfanewValid = FALSE;
614 /* We do have a DOS image so we will now try to seek into
615 * the file by the amount indicated by the field
616 * "Offset to extended header" and read in the
617 * "magic" field information at that location.
618 * This will tell us if there is more header information
619 * to read or not.
620 */
621 /* But before we do we will make sure that header
622 * structure encompasses the "Offset to extended header"
623 * field.
624 */
625 if ( (mz_header.e_cparhdr<<4) >= sizeof(IMAGE_DOS_HEADER) )
Alexandre Julliard8c81b7431999-10-13 15:47:38 +0000626 if ( ( mz_header.e_crlc == 0 ) ||
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000627 ( mz_header.e_lfarlc >= sizeof(IMAGE_DOS_HEADER) ) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000628 if ( mz_header.e_lfanew >= sizeof(IMAGE_DOS_HEADER)
629 && SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET ) != -1
630 && ReadFile( hfile, magic, sizeof(magic), &len, NULL )
631 && len == sizeof(magic) )
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000632 lfanewValid = TRUE;
633
634 if ( !lfanewValid )
635 {
636 /* If we cannot read this "extended header" we will
637 * assume that we have a simple DOS executable.
638 */
639 *lpBinaryType = SCS_DOS_BINARY;
640 return TRUE;
641 }
642 else
643 {
644 /* Reading the magic field succeeded so
645 * we will try to determine what type it is.
646 */
647 if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
648 {
649 /* This is an NT signature.
650 */
651 *lpBinaryType = SCS_32BIT_BINARY;
652 return TRUE;
653 }
654 else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
655 {
656 /* The IMAGE_OS2_SIGNATURE indicates that the
657 * "extended header is a Windows executable (NE)
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000658 * header." This can mean either a 16-bit OS/2
659 * or a 16-bit Windows or even a DOS program
660 * (running under a DOS extender). To decide
661 * which, we'll have to read the NE header.
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000662 */
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000663
664 IMAGE_OS2_HEADER ne;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000665 if ( SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET ) != -1
666 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
667 && len == sizeof(ne) )
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000668 {
Alexandre Julliard180a0882000-04-18 11:58:24 +0000669 switch ( ne.ne_exetyp )
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000670 {
671 case 2: *lpBinaryType = SCS_WOW_BINARY; return TRUE;
672 case 5: *lpBinaryType = SCS_DOS_BINARY; return TRUE;
Andreas Mohrb021fe22000-07-26 18:02:28 +0000673 default: *lpBinaryType =
674 MODULE_Decide_OS2_OldWin(hfile, &mz_header, &ne);
675 return TRUE;
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000676 }
677 }
678 /* Couldn't read header, so abort. */
679 return FALSE;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000680 }
681 else
682 {
Sergei Turchanovd14aea21999-12-04 04:17:37 +0000683 /* Unknown extended header, but this file is nonetheless
684 DOS-executable.
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000685 */
Sergei Turchanovd14aea21999-12-04 04:17:37 +0000686 *lpBinaryType = SCS_DOS_BINARY;
687 return TRUE;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000688 }
689 }
690 }
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000691 }
Alexandre Julliard2787be81995-05-22 18:23:01 +0000692
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000693 /* If we get here, we don't even have a correct MZ header.
694 * Try to check the file extension for known types ...
695 */
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000696 ptr = strrchr( filename, '.' );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000697 if ( ptr && !strchr( ptr, '\\' ) && !strchr( ptr, '/' ) )
698 {
Aric Stewarte4d09322000-12-03 03:14:29 +0000699 if ( !FILE_strcasecmp( ptr, ".COM" ) )
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000700 {
701 *lpBinaryType = SCS_DOS_BINARY;
702 return TRUE;
703 }
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000704
Aric Stewarte4d09322000-12-03 03:14:29 +0000705 if ( !FILE_strcasecmp( ptr, ".PIF" ) )
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000706 {
707 *lpBinaryType = SCS_PIF_BINARY;
708 return TRUE;
709 }
710 }
Alexandre Julliard1e37a181996-08-18 16:21:52 +0000711
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000712 return FALSE;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000713}
714
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000715/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000716 * GetBinaryTypeA [KERNEL32.@]
Patrik Stridvall044855c2001-07-11 18:56:41 +0000717 * GetBinaryType [KERNEL32.@]
Alexandre Julliard01d63461997-01-20 19:43:45 +0000718 */
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000719BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000720{
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000721 BOOL ret = FALSE;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000722 HANDLE hfile;
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000723
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000724 TRACE_(win32)("%s\n", lpApplicationName );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000725
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000726 /* Sanity check.
727 */
728 if ( lpApplicationName == NULL || lpBinaryType == NULL )
729 return FALSE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000730
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000731 /* Open the file indicated by lpApplicationName for reading.
732 */
Alexandre Julliarde3332122000-06-08 01:00:16 +0000733 hfile = CreateFileA( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
François Gougetda2b6a92001-01-06 01:29:18 +0000734 NULL, OPEN_EXISTING, 0, 0 );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000735 if ( hfile == INVALID_HANDLE_VALUE )
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000736 return FALSE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000737
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000738 /* Check binary type
739 */
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000740 ret = MODULE_GetBinaryType( hfile, lpApplicationName, lpBinaryType );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000741
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000742 /* Close the file.
743 */
744 CloseHandle( hfile );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000745
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000746 return ret;
747}
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000748
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000749/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000750 * GetBinaryTypeW [KERNEL32.@]
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000751 */
752BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
753{
754 BOOL ret = FALSE;
755 LPSTR strNew = NULL;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000756
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000757 TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000758
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000759 /* Sanity check.
760 */
761 if ( lpApplicationName == NULL || lpBinaryType == NULL )
762 return FALSE;
763
764 /* Convert the wide string to a ascii string.
765 */
766 strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
767
768 if ( strNew != NULL )
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000769 {
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000770 ret = GetBinaryTypeA( strNew, lpBinaryType );
771
772 /* Free the allocated string.
773 */
774 HeapFree( GetProcessHeap(), 0, strNew );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000775 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000776
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000777 return ret;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000778}
779
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000780
781/***********************************************************************
Patrik Stridvall044855c2001-07-11 18:56:41 +0000782 * WinExec (KERNEL.166)
783 * WinExec16 (KERNEL32.@)
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000784 */
785HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
786{
Andreas Mohr70963842000-09-22 22:08:28 +0000787 LPCSTR p, args = NULL;
788 LPCSTR name_beg, name_end;
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000789 LPSTR name, cmdline;
Andreas Mohr70963842000-09-22 22:08:28 +0000790 int arglen;
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000791 HINSTANCE16 ret;
792 char buffer[MAX_PATH];
Ulrich Weigand6ce40061999-05-03 09:22:55 +0000793
Andreas Mohr70963842000-09-22 22:08:28 +0000794 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000795 {
Andreas Mohr70963842000-09-22 22:08:28 +0000796 name_beg = lpCmdLine+1;
797 p = strchr ( lpCmdLine+1, '"' );
798 if (p)
799 {
800 name_end = p;
801 args = strchr ( p, ' ' );
802 }
803 else /* yes, even valid with trailing '"' missing */
804 name_end = lpCmdLine+strlen(lpCmdLine);
805 }
806 else
807 {
808 name_beg = lpCmdLine;
809 args = strchr( lpCmdLine, ' ' );
810 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
811 }
812
813 if ((name_beg == lpCmdLine) && (!args))
814 { /* just use the original cmdline string as file name */
815 name = (LPSTR)lpCmdLine;
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000816 }
817 else
818 {
Andreas Mohr70963842000-09-22 22:08:28 +0000819 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
820 return ERROR_NOT_ENOUGH_MEMORY;
821 memcpy( name, name_beg, name_end - name_beg );
822 name[name_end - name_beg] = '\0';
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000823 }
Ulrich Weigand6ce40061999-05-03 09:22:55 +0000824
Andreas Mohr70963842000-09-22 22:08:28 +0000825 if (args)
826 {
827 args++;
828 arglen = strlen(args);
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000829 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
Andreas Mohr70963842000-09-22 22:08:28 +0000830 cmdline[0] = (BYTE)arglen;
831 strcpy( cmdline + 1, args );
832 }
833 else
834 {
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000835 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
Andreas Mohr70963842000-09-22 22:08:28 +0000836 cmdline[0] = cmdline[1] = 0;
837 }
838
Andreas Mohrcabee392000-10-25 21:22:27 +0000839 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
Andreas Mohr70963842000-09-22 22:08:28 +0000840
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000841 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
842 {
843 LOADPARAMS16 params;
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000844 WORD showCmd[2];
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000845 showCmd[0] = 2;
846 showCmd[1] = nCmdShow;
847
848 params.hEnvironment = 0;
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000849 params.cmdLine = MapLS( cmdline );
850 params.showCmd = MapLS( showCmd );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000851 params.reserved = 0;
852
853 ret = LoadModule16( buffer, &params );
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000854 UnMapLS( params.cmdLine );
855 UnMapLS( params.showCmd );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000856 }
857 else ret = GetLastError();
858
Alexandre Julliardd7b76822001-12-20 00:19:40 +0000859 HeapFree( GetProcessHeap(), 0, cmdline );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000860 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
861
862 if (ret == 21) /* 32-bit module */
863 {
Alexandre Julliardab687972000-11-15 23:41:46 +0000864 DWORD count;
865 ReleaseThunkLock( &count );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000866 ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) );
Alexandre Julliardab687972000-11-15 23:41:46 +0000867 RestoreThunkLock( count );
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000868 }
869 return ret;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000870}
871
872/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000873 * WinExec (KERNEL32.@)
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000874 */
Francois Gouget2204b502002-05-19 22:21:45 +0000875UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000876{
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000877 PROCESS_INFORMATION info;
878 STARTUPINFOA startup;
879 HINSTANCE hInstance;
Alexandre Julliard596921d2000-06-24 20:53:47 +0000880 char *cmdline;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000881
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000882 memset( &startup, 0, sizeof(startup) );
883 startup.cb = sizeof(startup);
884 startup.dwFlags = STARTF_USESHOWWINDOW;
885 startup.wShowWindow = nCmdShow;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000886
Alexandre Julliard596921d2000-06-24 20:53:47 +0000887 /* cmdline needs to be writeable for CreateProcess */
Alexandre Julliard5f728ca2001-07-24 21:45:22 +0000888 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
889 strcpy( cmdline, lpCmdLine );
Alexandre Julliard596921d2000-06-24 20:53:47 +0000890
891 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000892 0, NULL, NULL, &startup, &info ))
893 {
894 /* Give 30 seconds to the app to come up */
Alexandre Julliard5edf4e12001-07-26 20:12:54 +0000895 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF)
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000896 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000897 hInstance = (HINSTANCE)33;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000898 /* Close off the handles */
899 CloseHandle( info.hThread );
900 CloseHandle( info.hProcess );
901 }
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000902 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000903 {
904 FIXME("Strange error set by CreateProcess: %d\n", hInstance );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000905 hInstance = (HINSTANCE)11;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000906 }
Alexandre Julliard596921d2000-06-24 20:53:47 +0000907 HeapFree( GetProcessHeap(), 0, cmdline );
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000908 return hInstance;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000909}
910
911/**********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000912 * LoadModule (KERNEL32.@)
Alexandre Julliard491502b1997-11-01 19:08:16 +0000913 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000914HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
Alexandre Julliard491502b1997-11-01 19:08:16 +0000915{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000916 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000917 PROCESS_INFORMATION info;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000918 STARTUPINFOA startup;
919 HINSTANCE hInstance;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000920 LPSTR cmdline, p;
921 char filename[MAX_PATH];
922 BYTE len;
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000923
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000924 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000925
926 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
927 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000928 return (HINSTANCE)GetLastError();
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000929
930 len = (BYTE)params->lpCmdLine[0];
931 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000932 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000933
934 strcpy( cmdline, filename );
935 p = cmdline + strlen(cmdline);
936 *p++ = ' ';
937 memcpy( p, params->lpCmdLine + 1, len );
938 p[len] = 0;
939
940 memset( &startup, 0, sizeof(startup) );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000941 startup.cb = sizeof(startup);
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000942 if (params->lpCmdShow)
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000943 {
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000944 startup.dwFlags = STARTF_USESHOWWINDOW;
945 startup.wShowWindow = params->lpCmdShow[1];
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000946 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000947
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000948 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
949 params->lpEnvAddress, NULL, &startup, &info ))
950 {
951 /* Give 30 seconds to the app to come up */
Alexandre Julliard5edf4e12001-07-26 20:12:54 +0000952 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF )
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000953 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000954 hInstance = (HINSTANCE)33;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000955 /* Close off the handles */
956 CloseHandle( info.hThread );
957 CloseHandle( info.hProcess );
958 }
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000959 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000960 {
961 FIXME("Strange error set by CreateProcess: %d\n", hInstance );
Alexandre Julliard8ff37b82001-06-06 20:24:12 +0000962 hInstance = (HINSTANCE)11;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000963 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000964
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000965 HeapFree( GetProcessHeap(), 0, cmdline );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000966 return hInstance;
Alexandre Julliard491502b1997-11-01 19:08:16 +0000967}
968
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000969
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000970/*************************************************************************
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000971 * get_file_name
Guy Albertelli38d7da81999-06-06 09:04:29 +0000972 *
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000973 * Helper for CreateProcess: retrieve the file name to load from the
974 * app name and command line. Store the file name in buffer, and
975 * return a possibly modified command line.
Guy Albertelli38d7da81999-06-06 09:04:29 +0000976 */
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000977static LPSTR get_file_name( LPCSTR appname, LPSTR cmdline, LPSTR buffer, int buflen )
Guy Albertelli38d7da81999-06-06 09:04:29 +0000978{
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000979 char *name, *pos, *ret = NULL;
980 const char *p;
Guy Albertelli38d7da81999-06-06 09:04:29 +0000981
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000982 /* if we have an app name, everything is easy */
983
984 if (appname)
985 {
986 /* use the unmodified app name as file name */
987 lstrcpynA( buffer, appname, buflen );
988 if (!(ret = cmdline))
989 {
990 /* no command-line, create one */
991 if ((ret = HeapAlloc( GetProcessHeap(), 0, strlen(appname) + 3 )))
992 sprintf( ret, "\"%s\"", appname );
993 }
994 return ret;
Guy Albertelli38d7da81999-06-06 09:04:29 +0000995 }
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +0000996
997 if (!cmdline)
998 {
999 SetLastError( ERROR_INVALID_PARAMETER );
1000 return NULL;
1001 }
1002
1003 /* first check for a quoted file name */
1004
1005 if ((cmdline[0] == '"') && ((p = strchr( cmdline + 1, '"' ))))
1006 {
1007 int len = p - cmdline - 1;
1008 /* extract the quoted portion as file name */
1009 if (!(name = HeapAlloc( GetProcessHeap(), 0, len + 1 ))) return NULL;
1010 memcpy( name, cmdline + 1, len );
1011 name[len] = 0;
1012
1013 if (SearchPathA( NULL, name, ".exe", buflen, buffer, NULL ) ||
1014 SearchPathA( NULL, name, NULL, buflen, buffer, NULL ))
1015 ret = cmdline; /* no change necessary */
1016 goto done;
1017 }
1018
1019 /* now try the command-line word by word */
1020
1021 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(cmdline) + 1 ))) return NULL;
1022 pos = name;
1023 p = cmdline;
1024
1025 while (*p)
1026 {
1027 do *pos++ = *p++; while (*p && *p != ' ');
1028 *pos = 0;
1029 TRACE("trying '%s'\n", name );
1030 if (SearchPathA( NULL, name, ".exe", buflen, buffer, NULL ) ||
1031 SearchPathA( NULL, name, NULL, buflen, buffer, NULL ))
1032 {
1033 ret = cmdline;
1034 break;
Guy Albertelli38d7da81999-06-06 09:04:29 +00001035 }
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001036 }
Guy Albertelli38d7da81999-06-06 09:04:29 +00001037
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001038 if (!ret || !strchr( name, ' ' )) goto done; /* no change necessary */
Guy Albertelli38d7da81999-06-06 09:04:29 +00001039
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001040 /* now build a new command-line with quotes */
Guy Albertelli38d7da81999-06-06 09:04:29 +00001041
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001042 if (!(ret = HeapAlloc( GetProcessHeap(), 0, strlen(cmdline) + 3 ))) goto done;
1043 sprintf( ret, "\"%s\"%s", name, p );
Guy Albertelli38d7da81999-06-06 09:04:29 +00001044
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001045 done:
1046 HeapFree( GetProcessHeap(), 0, name );
1047 return ret;
Guy Albertelli38d7da81999-06-06 09:04:29 +00001048}
1049
Andreas Mohre518f471999-02-28 19:28:44 +00001050
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001051/**********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001052 * CreateProcessA (KERNEL32.@)
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001053 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001054BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001055 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1056 LPSECURITY_ATTRIBUTES lpThreadAttributes,
1057 BOOL bInheritHandles, DWORD dwCreationFlags,
1058 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
1059 LPSTARTUPINFOA lpStartupInfo,
1060 LPPROCESS_INFORMATION lpProcessInfo )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001061{
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001062 BOOL retv = FALSE;
Ulrich Weigand237e8e91999-12-04 04:04:58 +00001063 HANDLE hFile;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001064 DWORD type;
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001065 char name[MAX_PATH];
Dave Picklesfec23291999-06-26 11:48:26 +00001066 LPSTR tidy_cmdline;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001067
Dave Picklesfec23291999-06-26 11:48:26 +00001068 /* Process the AppName and/or CmdLine to get module name and path */
Marcus Meissner1ab89071999-06-13 08:39:04 +00001069
Francois Gougetb60c4ce2001-10-14 16:15:05 +00001070 TRACE("app %s cmdline %s\n", debugstr_a(lpApplicationName), debugstr_a(lpCommandLine) );
Alexandre Julliardc192ba22000-05-29 21:25:10 +00001071
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001072 if (!(tidy_cmdline = get_file_name( lpApplicationName, lpCommandLine, name, sizeof(name) )))
Marcus Meissner1ab89071999-06-13 08:39:04 +00001073 return FALSE;
Dave Picklesfec23291999-06-26 11:48:26 +00001074
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001075 /* Warn if unsupported features are used */
1076
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001077 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001078 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001079 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001080 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001081 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001082 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001083 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001084 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001085 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001086 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001087 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001088 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001089 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001090 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001091 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001092 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001093 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001094 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001095 if (dwCreationFlags & CREATE_NO_WINDOW)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001096 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001097 if (dwCreationFlags & PROFILE_USER)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001098 FIXME("(%s,...): PROFILE_USER ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001099 if (dwCreationFlags & PROFILE_KERNEL)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001100 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001101 if (dwCreationFlags & PROFILE_SERVER)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001102 FIXME("(%s,...): PROFILE_SERVER ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001103 if (lpStartupInfo->lpDesktop)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001104 FIXME("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
Andreas Mohrf3598952001-10-02 17:49:20 +00001105 name, debugstr_a(lpStartupInfo->lpDesktop));
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001106 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001107 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001108 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001109 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001110 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001111 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001112 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001113 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001114
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001115 /* Open file and determine executable type */
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001116
François Gougetda2b6a92001-01-06 01:29:18 +00001117 hFile = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001118 if (hFile == INVALID_HANDLE_VALUE) goto done;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001119
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001120 if ( !MODULE_GetBinaryType( hFile, name, &type ) )
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001121 { /* unknown file, try as unix executable */
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001122 CloseHandle( hFile );
Alexandre Julliard8081e5a2001-01-05 04:08:07 +00001123 retv = PROCESS_Create( 0, name, tidy_cmdline, lpEnvironment,
Alexandre Julliardc192ba22000-05-29 21:25:10 +00001124 lpProcessAttributes, lpThreadAttributes,
1125 bInheritHandles, dwCreationFlags,
Marcus Meissnerdad70912000-07-16 15:42:22 +00001126 lpStartupInfo, lpProcessInfo, lpCurrentDirectory );
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001127 goto done;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001128 }
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001129
1130 /* Create process */
1131
1132 switch ( type )
1133 {
1134 case SCS_32BIT_BINARY:
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001135 case SCS_WOW_BINARY:
Alexandre Julliardc192ba22000-05-29 21:25:10 +00001136 case SCS_DOS_BINARY:
1137 retv = PROCESS_Create( hFile, name, tidy_cmdline, lpEnvironment,
1138 lpProcessAttributes, lpThreadAttributes,
1139 bInheritHandles, dwCreationFlags,
Marcus Meissnerdad70912000-07-16 15:42:22 +00001140 lpStartupInfo, lpProcessInfo, lpCurrentDirectory);
Alexandre Julliard8e8f0f52000-04-15 21:30:33 +00001141 break;
1142
1143 case SCS_PIF_BINARY:
1144 case SCS_POSIX_BINARY:
1145 case SCS_OS216_BINARY:
1146 FIXME("Unsupported executable type: %ld\n", type );
1147 /* fall through */
1148
1149 default:
1150 SetLastError( ERROR_BAD_FORMAT );
1151 break;
1152 }
1153 CloseHandle( hFile );
1154
1155 done:
1156 if (tidy_cmdline != lpCommandLine) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001157 return retv;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001158}
1159
1160/**********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001161 * CreateProcessW (KERNEL32.@)
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001162 * NOTES
1163 * lpReserved is not converted
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001164 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001165BOOL WINAPI CreateProcessW( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001166 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1167 LPSECURITY_ATTRIBUTES lpThreadAttributes,
Alexandre Julliarda3960291999-02-26 11:11:13 +00001168 BOOL bInheritHandles, DWORD dwCreationFlags,
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001169 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
Alexandre Julliarda3960291999-02-26 11:11:13 +00001170 LPSTARTUPINFOW lpStartupInfo,
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001171 LPPROCESS_INFORMATION lpProcessInfo )
Alexandre Julliarda3960291999-02-26 11:11:13 +00001172{ BOOL ret;
1173 STARTUPINFOA StartupInfoA;
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001174
1175 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
1176 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
1177 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001178
Alexandre Julliarda3960291999-02-26 11:11:13 +00001179 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFOA));
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001180 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
1181 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
1182
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001183 TRACE_(win32)("(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001184
1185 if (lpStartupInfo->lpReserved)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001186 FIXME_(win32)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001187
Alexandre Julliarda3960291999-02-26 11:11:13 +00001188 ret = CreateProcessA( lpApplicationNameA, lpCommandLineA,
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001189 lpProcessAttributes, lpThreadAttributes,
1190 bInheritHandles, dwCreationFlags,
1191 lpEnvironment, lpCurrentDirectoryA,
1192 &StartupInfoA, lpProcessInfo );
1193
1194 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
1195 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
1196 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
1197 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
1198
1199 return ret;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001200}
Alexandre Julliard01d63461997-01-20 19:43:45 +00001201
Alexandre Julliard77b99181997-09-14 17:17:23 +00001202/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001203 * GetModuleHandleA (KERNEL32.@)
Patrik Stridvall044855c2001-07-11 18:56:41 +00001204 * GetModuleHandle32 (KERNEL.488)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001205 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001206HMODULE WINAPI GetModuleHandleA(LPCSTR module)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001207{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001208 WINE_MODREF *wm;
1209
1210 if ( module == NULL )
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001211 wm = exe_modref;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001212 else
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001213 wm = MODULE_FindModule( module );
1214
1215 return wm? wm->module : 0;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001216}
1217
Patrik Stridvall2d6457c2000-03-28 20:22:59 +00001218/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001219 * GetModuleHandleW (KERNEL32.@)
Patrik Stridvall2d6457c2000-03-28 20:22:59 +00001220 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001221HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001222{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001223 HMODULE hModule;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001224 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001225 hModule = GetModuleHandleA( modulea );
Alexandre Julliard77b99181997-09-14 17:17:23 +00001226 HeapFree( GetProcessHeap(), 0, modulea );
1227 return hModule;
1228}
1229
Alexandre Julliard594997c1995-04-30 10:05:20 +00001230
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001231/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001232 * GetModuleFileNameA (KERNEL32.@)
Patrik Stridvall044855c2001-07-11 18:56:41 +00001233 * GetModuleFileName32 (KERNEL.487)
Andreas Mohr4654c322000-02-20 19:15:34 +00001234 *
1235 * GetModuleFileNameA seems to *always* return the long path;
1236 * it's only GetModuleFileName16 that decides between short/long path
1237 * by checking if exe version >= 4.0.
1238 * (SDK docu doesn't mention this)
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001239 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001240DWORD WINAPI GetModuleFileNameA(
1241 HMODULE hModule, /* [in] module handle (32bit) */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001242 LPSTR lpFileName, /* [out] filenamebuffer */
Alexandre Julliard081ee942000-08-07 04:12:41 +00001243 DWORD size ) /* [in] size of filenamebuffer */
1244{
1245 WINE_MODREF *wm;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001246
Alexandre Julliard64781642002-02-02 18:13:50 +00001247 RtlEnterCriticalSection( &loader_section );
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001248
Alexandre Julliard081ee942000-08-07 04:12:41 +00001249 lpFileName[0] = 0;
1250 if ((wm = MODULE32_LookupHMODULE( hModule )))
1251 lstrcpynA( lpFileName, wm->filename, size );
1252
Alexandre Julliard64781642002-02-02 18:13:50 +00001253 RtlLeaveCriticalSection( &loader_section );
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001254 TRACE("%s\n", lpFileName );
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001255 return strlen(lpFileName);
1256}
1257
1258
1259/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001260 * GetModuleFileNameW (KERNEL32.@)
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001261 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001262DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName,
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001263 DWORD size )
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001264{
1265 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001266 DWORD res = GetModuleFileNameA( hModule, fnA, size );
Alexandre Julliard24a62ab2000-11-28 22:40:56 +00001267 if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, fnA, -1, lpFileName, size ))
1268 lpFileName[size-1] = 0;
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001269 HeapFree( GetProcessHeap(), 0, fnA );
1270 return res;
1271}
1272
1273
Alexandre Julliard2787be81995-05-22 18:23:01 +00001274/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001275 * LoadLibraryExA (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001276 */
Jim Aston031f4fa1999-10-23 19:00:02 +00001277HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001278{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001279 WINE_MODREF *wm;
Marcus Meissner574ef761999-04-03 16:23:47 +00001280
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001281 if(!libname)
1282 {
1283 SetLastError(ERROR_INVALID_PARAMETER);
1284 return 0;
1285 }
Ulrich Weigandebc543c1998-10-23 09:37:23 +00001286
Alexandre Julliard081ee942000-08-07 04:12:41 +00001287 if (flags & LOAD_LIBRARY_AS_DATAFILE)
1288 {
1289 char filename[256];
Alexandre Julliard081ee942000-08-07 04:12:41 +00001290 HMODULE hmod = 0;
1291
Dmitry Timoshkovb77afe72001-03-21 03:38:03 +00001292 /* This method allows searching for the 'native' libraries only */
Ove Kaaven10b34022001-04-16 19:32:48 +00001293 if (SearchPathA( NULL, libname, ".dll", sizeof(filename), filename, NULL ))
Dmitry Timoshkovb77afe72001-03-21 03:38:03 +00001294 {
Ove Kaaven10b34022001-04-16 19:32:48 +00001295 /* FIXME: maybe we should use the hfile parameter instead */
1296 HANDLE hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
1297 NULL, OPEN_EXISTING, 0, 0 );
1298 if (hFile != INVALID_HANDLE_VALUE)
1299 {
Alexandre Julliardcd3afa82002-01-31 23:32:57 +00001300 DWORD type;
1301 MODULE_GetBinaryType( hFile, filename, &type );
1302 if (type == SCS_32BIT_BINARY)
1303 {
1304 HANDLE mapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY,
1305 0, 0, NULL );
1306 if (mapping)
1307 {
1308 hmod = (HMODULE)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1309 CloseHandle( mapping );
1310 }
1311 }
Ove Kaaven10b34022001-04-16 19:32:48 +00001312 CloseHandle( hFile );
1313 }
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001314 if (hmod) return (HMODULE)((ULONG_PTR)hmod + 1);
Dmitry Timoshkovb77afe72001-03-21 03:38:03 +00001315 }
Ove Kaaven10b34022001-04-16 19:32:48 +00001316 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
1317 /* Fallback to normal behaviour */
Alexandre Julliard081ee942000-08-07 04:12:41 +00001318 }
Ove Kaaven10b34022001-04-16 19:32:48 +00001319
Alexandre Julliard64781642002-02-02 18:13:50 +00001320 RtlEnterCriticalSection( &loader_section );
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001321
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001322 wm = MODULE_LoadLibraryExA( libname, hfile, flags );
Ulrich Weigand0106f851999-12-08 03:28:14 +00001323 if ( wm )
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001324 {
Ulrich Weigand0106f851999-12-08 03:28:14 +00001325 if ( !MODULE_DllProcessAttach( wm, NULL ) )
1326 {
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001327 WARN_(module)("Attach failed for module '%s'.\n", libname);
Ulrich Weigand0106f851999-12-08 03:28:14 +00001328 MODULE_FreeLibrary(wm);
1329 SetLastError(ERROR_DLL_INIT_FAILED);
1330 wm = NULL;
1331 }
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001332 }
1333
Alexandre Julliard64781642002-02-02 18:13:50 +00001334 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001335 return wm ? wm->module : 0;
Marcus Meissner8220bc91998-10-11 11:10:27 +00001336}
1337
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001338/***********************************************************************
Bill Medland65fc1c92001-08-24 21:13:02 +00001339 * allocate_lib_dir
1340 *
1341 * helper for MODULE_LoadLibraryExA. Allocate space to hold the directory
1342 * portion of the provided name and put the name in it.
1343 *
1344 */
1345static LPCSTR allocate_lib_dir(LPCSTR libname)
1346{
1347 LPCSTR p, pmax;
1348 LPSTR result;
1349 int length;
1350
1351 pmax = libname;
1352 if ((p = strrchr( pmax, '\\' ))) pmax = p + 1;
1353 if ((p = strrchr( pmax, '/' ))) pmax = p + 1; /* Naughty. MSDN says don't */
1354 if (pmax == libname && pmax[0] && pmax[1] == ':') pmax += 2;
1355
1356 length = pmax - libname;
1357
1358 result = HeapAlloc (GetProcessHeap(), 0, length+1);
1359
1360 if (result)
1361 {
1362 strncpy (result, libname, length);
1363 result [length] = '\0';
1364 }
1365
1366 return result;
1367}
1368
1369/***********************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001370 * MODULE_LoadLibraryExA (internal)
1371 *
1372 * Load a PE style module according to the load order.
1373 *
1374 * The HFILE parameter is not used and marked reserved in the SDK. I can
1375 * only guess that it should force a file to be mapped, but I rather
1376 * ignore the parameter because it would be extremely difficult to
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001377 * integrate this with different types of module representations.
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001378 *
Bill Medland65fc1c92001-08-24 21:13:02 +00001379 * libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion
1380 * on this function. When first called from LoadLibraryExA it will be
1381 * NULL but thereafter it may point to a buffer containing the path
1382 * portion of the library name. Note that the recursion all occurs
1383 * within a Critical section (see LoadLibraryExA) so the use of a
1384 * static is acceptable.
1385 * (We have to use a static variable at some point anyway, to pass the
1386 * information from BUILTIN32_dlopen through dlopen and the builtin's
1387 * init function into load_library).
1388 * allocated_libdir is TRUE in the stack frame that allocated libdir
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001389 */
1390WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
Marcus Meissner8220bc91998-10-11 11:10:27 +00001391{
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001392 DWORD err = GetLastError();
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001393 WINE_MODREF *pwm;
1394 int i;
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001395 enum loadorder_type loadorder[LOADORDER_NTYPES];
Peter Gantenc7c42462000-08-28 21:33:28 +00001396 LPSTR filename, p;
Gerard Patela77fd7d2001-01-10 22:54:02 +00001397 const char *filetype = "";
Bill Medland65fc1c92001-08-24 21:13:02 +00001398 DWORD found;
1399 BOOL allocated_libdir = FALSE;
1400 static LPCSTR libdir = NULL; /* See above */
Peter Gantenc7c42462000-08-28 21:33:28 +00001401
1402 if ( !libname ) return NULL;
1403
1404 filename = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1405 if ( !filename ) return NULL;
Bill Medlandca5b2012002-01-18 18:58:08 +00001406 *filename = 0; /* Just in case we don't set it before goto error */
Peter Gantenc7c42462000-08-28 21:33:28 +00001407
Alexandre Julliard64781642002-02-02 18:13:50 +00001408 RtlEnterCriticalSection( &loader_section );
Bill Medland65fc1c92001-08-24 21:13:02 +00001409
1410 if ((flags & LOAD_WITH_ALTERED_SEARCH_PATH) && FILE_contains_path(libname))
1411 {
1412 if (!(libdir = allocate_lib_dir(libname))) goto error;
1413 allocated_libdir = TRUE;
1414 }
1415
1416 if (!libdir || allocated_libdir)
1417 found = SearchPathA(NULL, libname, ".dll", MAX_PATH, filename, NULL);
1418 else
1419 found = DIR_SearchAlternatePath(libdir, libname, ".dll", MAX_PATH, filename, NULL);
1420
Peter Gantenc7c42462000-08-28 21:33:28 +00001421 /* build the modules filename */
Bill Medland65fc1c92001-08-24 21:13:02 +00001422 if (!found)
Peter Gantenc7c42462000-08-28 21:33:28 +00001423 {
1424 if ( ! GetSystemDirectoryA ( filename, MAX_PATH ) )
1425 goto error;
1426
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001427 /* if the library name contains a path and can not be found,
1428 * return an error.
1429 * exception: if the path is the system directory, proceed,
1430 * so that modules which are not PE modules can be loaded.
1431 * If the library name does not contain a path and can not
1432 * be found, assume the system directory is meant */
Peter Gantenc7c42462000-08-28 21:33:28 +00001433
Aric Stewarte4d09322000-12-03 03:14:29 +00001434 if ( ! FILE_strncasecmp ( filename, libname, strlen ( filename ) ))
Peter Gantenc7c42462000-08-28 21:33:28 +00001435 strcpy ( filename, libname );
1436 else
1437 {
Bill Medland65fc1c92001-08-24 21:13:02 +00001438 if (FILE_contains_path(libname)) goto error;
1439 strcat ( filename, "\\" );
1440 strcat ( filename, libname );
Peter Gantenc7c42462000-08-28 21:33:28 +00001441 }
1442
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001443 /* if the filename doesn't have an extension, append .DLL */
Peter Gantenc7c42462000-08-28 21:33:28 +00001444 if (!(p = strrchr( filename, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
Bill Medland65fc1c92001-08-24 21:13:02 +00001445 strcat( filename, ".dll" );
Peter Gantenc7c42462000-08-28 21:33:28 +00001446 }
Marcus Meissner8220bc91998-10-11 11:10:27 +00001447
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001448 /* Check for already loaded module */
Bill Medland65fc1c92001-08-24 21:13:02 +00001449 if (!(pwm = MODULE_FindModule(filename)) && !FILE_contains_path(libname))
Eric Pouech208955c2000-09-10 03:14:00 +00001450 {
1451 LPSTR fn = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1452 if (fn)
1453 {
1454 /* since the default loading mechanism uses a more detailed algorithm
1455 * than SearchPath (like using PATH, which can even be modified between
1456 * two attempts of loading the same DLL), the look-up above (with
1457 * SearchPath) can have put the file in system directory, whereas it
1458 * has already been loaded but with a different path. So do a specific
1459 * look-up with filename (without any path)
1460 */
1461 strcpy ( fn, libname );
1462 /* if the filename doesn't have an extension append .DLL */
1463 if (!strrchr( fn, '.')) strcat( fn, ".dll" );
1464 if ((pwm = MODULE_FindModule( fn )) != NULL)
1465 strcpy( filename, fn );
1466 HeapFree( GetProcessHeap(), 0, fn );
1467 }
1468 }
1469 if (pwm)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001470 {
Bertho Stultiensf4b6e821999-04-22 08:56:40 +00001471 if(!(pwm->flags & WINE_MODREF_MARKER))
1472 pwm->refCount++;
Alexandre Julliard081ee942000-08-07 04:12:41 +00001473
1474 if ((pwm->flags & WINE_MODREF_DONT_RESOLVE_REFS) &&
1475 !(flags & DONT_RESOLVE_DLL_REFERENCES))
1476 {
Alexandre Julliard081ee942000-08-07 04:12:41 +00001477 pwm->flags &= ~WINE_MODREF_DONT_RESOLVE_REFS;
Bill Medland65fc1c92001-08-24 21:13:02 +00001478 PE_fixup_imports( pwm );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001479 }
Francois Gouget070e7492001-11-06 21:01:32 +00001480 TRACE("Already loaded module '%s' at 0x%08x, count=%d\n", filename, pwm->module, pwm->refCount);
Bill Medland65fc1c92001-08-24 21:13:02 +00001481 if (allocated_libdir)
1482 {
1483 HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1484 libdir = NULL;
1485 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001486 RtlLeaveCriticalSection( &loader_section );
Peter Gantenc7c42462000-08-28 21:33:28 +00001487 HeapFree ( GetProcessHeap(), 0, filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001488 return pwm;
1489 }
1490
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001491 MODULE_GetLoadOrder( loadorder, filename, TRUE);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001492
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001493 for(i = 0; i < LOADORDER_NTYPES; i++)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001494 {
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001495 if (loadorder[i] == LOADORDER_INVALID) break;
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001496 SetLastError( ERROR_FILE_NOT_FOUND );
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001497
1498 switch(loadorder[i])
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001499 {
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001500 case LOADORDER_DLL:
Peter Gantenc7c42462000-08-28 21:33:28 +00001501 TRACE("Trying native dll '%s'\n", filename);
1502 pwm = PE_LoadLibraryExA(filename, flags);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001503 filetype = "native";
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001504 break;
1505
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001506 case LOADORDER_SO:
Peter Gantenc7c42462000-08-28 21:33:28 +00001507 TRACE("Trying so-library '%s'\n", filename);
Alexandre Julliarde441d3c2000-12-22 22:50:12 +00001508 pwm = ELF_LoadLibraryExA(filename, flags);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001509 filetype = "so";
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001510 break;
1511
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001512 case LOADORDER_BI:
Peter Gantenc7c42462000-08-28 21:33:28 +00001513 TRACE("Trying built-in '%s'\n", filename);
1514 pwm = BUILTIN32_LoadLibraryExA(filename, flags);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001515 filetype = "builtin";
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001516 break;
1517
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +00001518 default:
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001519 pwm = NULL;
1520 break;
1521 }
1522
1523 if(pwm)
1524 {
1525 /* Initialize DLL just loaded */
Francois Gouget070e7492001-11-06 21:01:32 +00001526 TRACE("Loaded module '%s' at 0x%08x\n", filename, pwm->module);
Gerard Patela77fd7d2001-01-10 22:54:02 +00001527 if (!TRACE_ON(module))
1528 TRACE_(loaddll)("Loaded module '%s' : %s\n", filename, filetype);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001529 /* Set the refCount here so that an attach failure will */
1530 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1531 pwm->refCount++;
1532
Bill Medland65fc1c92001-08-24 21:13:02 +00001533 if (allocated_libdir)
1534 {
1535 HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1536 libdir = NULL;
1537 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001538 RtlLeaveCriticalSection( &loader_section );
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001539 SetLastError( err ); /* restore last error */
Peter Gantenc7c42462000-08-28 21:33:28 +00001540 HeapFree ( GetProcessHeap(), 0, filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001541 return pwm;
1542 }
1543
Alexandre Julliard05f0b712000-03-09 18:18:41 +00001544 if(GetLastError() != ERROR_FILE_NOT_FOUND)
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001545 {
1546 ERR("Loading of %s DLL %s failed (error %ld), check this file.\n",
1547 filetype, filename, GetLastError());
1548 break;
1549 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001550 }
1551
Peter Gantenc7c42462000-08-28 21:33:28 +00001552 error:
Bill Medland65fc1c92001-08-24 21:13:02 +00001553 if (allocated_libdir)
1554 {
1555 HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1556 libdir = NULL;
1557 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001558 RtlLeaveCriticalSection( &loader_section );
Andreas Mohr01c8ec32002-04-02 19:47:30 +00001559 WARN("Failed to load module '%s'; error=%ld\n", filename, GetLastError());
Peter Gantenc7c42462000-08-28 21:33:28 +00001560 HeapFree ( GetProcessHeap(), 0, filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001561 return NULL;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001562}
1563
1564/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001565 * LoadLibraryA (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001566 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001567HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
1568 return LoadLibraryExA(libname,0,0);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001569}
1570
1571/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001572 * LoadLibraryW (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001573 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001574HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001575{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001576 return LoadLibraryExW(libnameW,0,0);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001577}
1578
1579/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001580 * LoadLibrary32 (KERNEL.452)
1581 * LoadSystemLibrary32 (KERNEL.482)
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001582 */
1583HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
1584{
1585 HMODULE hModule;
Alexandre Julliardab687972000-11-15 23:41:46 +00001586 DWORD count;
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001587
Alexandre Julliardab687972000-11-15 23:41:46 +00001588 ReleaseThunkLock( &count );
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001589 hModule = LoadLibraryA( libname );
Alexandre Julliardab687972000-11-15 23:41:46 +00001590 RestoreThunkLock( count );
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001591 return hModule;
1592}
1593
1594/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001595 * LoadLibraryExW (KERNEL32.@)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001596 */
Jim Aston031f4fa1999-10-23 19:00:02 +00001597HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HANDLE hfile,DWORD flags)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001598{
1599 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001600 HMODULE ret = LoadLibraryExA( libnameA , hfile, flags );
Alexandre Julliard77b99181997-09-14 17:17:23 +00001601
1602 HeapFree( GetProcessHeap(), 0, libnameA );
1603 return ret;
1604}
1605
Bertho Stultiens1b346971999-04-11 14:52:24 +00001606/***********************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001607 * MODULE_FlushModrefs
Bertho Stultiens1b346971999-04-11 14:52:24 +00001608 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001609 * NOTE: Assumes that the process critical section is held!
Bertho Stultiens1b346971999-04-11 14:52:24 +00001610 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001611 * Remove all unused modrefs and call the internal unloading routines
1612 * for the library type.
Bertho Stultiens1b346971999-04-11 14:52:24 +00001613 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001614static void MODULE_FlushModrefs(void)
Bertho Stultiens1b346971999-04-11 14:52:24 +00001615{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001616 WINE_MODREF *wm, *next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001617
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001618 for(wm = MODULE_modref_list; wm; wm = next)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001619 {
1620 next = wm->next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001621
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001622 if(wm->refCount)
1623 continue;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001624
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001625 /* Unlink this modref from the chain */
1626 if(wm->next)
1627 wm->next->prev = wm->prev;
1628 if(wm->prev)
1629 wm->prev->next = wm->next;
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001630 if(wm == MODULE_modref_list)
1631 MODULE_modref_list = wm->next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001632
Alexandre Julliard081ee942000-08-07 04:12:41 +00001633 TRACE(" unloading %s\n", wm->filename);
Alexandre Julliardc9cf68d2001-05-02 01:12:27 +00001634 if (wm->dlhandle) wine_dll_unload( wm->dlhandle );
Alexandre Julliard2418edb2001-05-10 19:17:54 +00001635 else UnmapViewOfFile( (LPVOID)wm->module );
Andreas Mohrcabee392000-10-25 21:22:27 +00001636 FreeLibrary16(wm->hDummyMod);
1637 HeapFree( GetProcessHeap(), 0, wm->deps );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001638 HeapFree( GetProcessHeap(), 0, wm );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001639 }
Bertho Stultiens1b346971999-04-11 14:52:24 +00001640}
1641
Alexandre Julliard77b99181997-09-14 17:17:23 +00001642/***********************************************************************
Patrik Stridvall044855c2001-07-11 18:56:41 +00001643 * FreeLibrary (KERNEL32.@)
1644 * FreeLibrary32 (KERNEL.486)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001645 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001646BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001647{
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001648 BOOL retv = FALSE;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001649 WINE_MODREF *wm;
Ulrich Weigande469a581999-03-27 16:45:57 +00001650
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001651 if (!hLibModule)
1652 {
1653 SetLastError( ERROR_INVALID_HANDLE );
1654 return FALSE;
1655 }
1656
1657 if ((ULONG_PTR)hLibModule & 1)
1658 {
1659 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
1660 char *ptr = (char *)hLibModule - 1;
1661 UnmapViewOfFile( ptr );
1662 return TRUE;
1663 }
1664
Alexandre Julliard64781642002-02-02 18:13:50 +00001665 RtlEnterCriticalSection( &loader_section );
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001666 free_lib_count++;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001667
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001668 if ((wm = MODULE32_LookupHMODULE( hLibModule ))) retv = MODULE_FreeLibrary( wm );
Bertho Stultiens1b346971999-04-11 14:52:24 +00001669
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001670 free_lib_count--;
Alexandre Julliard64781642002-02-02 18:13:50 +00001671 RtlLeaveCriticalSection( &loader_section );
Bertho Stultiens1b346971999-04-11 14:52:24 +00001672
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001673 return retv;
1674}
1675
1676/***********************************************************************
1677 * MODULE_DecRefCount
1678 *
1679 * NOTE: Assumes that the process critical section is held!
1680 */
1681static void MODULE_DecRefCount( WINE_MODREF *wm )
1682{
1683 int i;
1684
1685 if ( wm->flags & WINE_MODREF_MARKER )
1686 return;
1687
1688 if ( wm->refCount <= 0 )
1689 return;
1690
1691 --wm->refCount;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001692 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001693
1694 if ( wm->refCount == 0 )
1695 {
1696 wm->flags |= WINE_MODREF_MARKER;
1697
1698 for ( i = 0; i < wm->nDeps; i++ )
1699 if ( wm->deps[i] )
1700 MODULE_DecRefCount( wm->deps[i] );
1701
1702 wm->flags &= ~WINE_MODREF_MARKER;
1703 }
1704}
1705
1706/***********************************************************************
1707 * MODULE_FreeLibrary
1708 *
1709 * NOTE: Assumes that the process critical section is held!
1710 */
1711BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
1712{
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001713 TRACE("(%s) - START\n", wm->modname );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001714
1715 /* Recursively decrement reference counts */
1716 MODULE_DecRefCount( wm );
1717
1718 /* Call process detach notifications */
Alexandre Julliardbecb9a32000-12-11 03:48:15 +00001719 if ( free_lib_count <= 1 )
Alexandre Julliardd131a171999-05-23 20:02:04 +00001720 {
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001721 MODULE_DllProcessDetach( FALSE, NULL );
Alexandre Julliard67a74992001-02-27 02:09:16 +00001722 SERVER_START_REQ( unload_dll )
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001723 {
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001724 req->base = (void *)wm->module;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001725 wine_server_call( req );
Alexandre Julliard9c2370b2000-08-30 00:00:48 +00001726 }
1727 SERVER_END_REQ;
Alexandre Julliardf93eb3e2000-04-28 20:26:35 +00001728 MODULE_FlushModrefs();
Alexandre Julliardd131a171999-05-23 20:02:04 +00001729 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001730
Ulrich Czekallacc279982000-03-08 18:41:22 +00001731 TRACE("END\n");
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001732
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001733 return TRUE;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001734}
1735
Bertho Stultiens1b346971999-04-11 14:52:24 +00001736
Sergey Turchanov5ffd2df1999-03-22 12:35:48 +00001737/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001738 * FreeLibraryAndExitThread (KERNEL32.@)
Sergey Turchanov5ffd2df1999-03-22 12:35:48 +00001739 */
1740VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
1741{
1742 FreeLibrary(hLibModule);
1743 ExitThread(dwExitCode);
1744}
Alexandre Julliard2787be81995-05-22 18:23:01 +00001745
1746/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001747 * PrivateLoadLibrary (KERNEL32.@)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001748 *
1749 * FIXME: rough guesswork, don't know what "Private" means
1750 */
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001751HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001752{
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001753 return LoadLibrary16(libname);
Alexandre Julliarde658d821997-11-30 17:45:40 +00001754}
1755
1756
Alexandre Julliard2787be81995-05-22 18:23:01 +00001757
1758/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00001759 * PrivateFreeLibrary (KERNEL32.@)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001760 *
1761 * FIXME: rough guesswork, don't know what "Private" means
1762 */
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001763void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001764{
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001765 FreeLibrary16(handle);
Alexandre Julliarde658d821997-11-30 17:45:40 +00001766}
1767
1768
1769/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001770 * GetProcAddress16 (KERNEL32.37)
Alexandre Julliard641ee761997-08-04 16:34:36 +00001771 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1772 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001773FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
Alexandre Julliard641ee761997-08-04 16:34:36 +00001774{
Alexandre Julliard641ee761997-08-04 16:34:36 +00001775 if (!hModule) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001776 WARN("hModule may not be 0!\n");
Alexandre Julliard641ee761997-08-04 16:34:36 +00001777 return (FARPROC16)0;
1778 }
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001779 if (HIWORD(hModule))
1780 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001781 WARN("hModule is Win32 handle (%08x)\n", hModule );
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001782 return (FARPROC16)0;
1783 }
Alexandre Julliard8ff37b82001-06-06 20:24:12 +00001784 return GetProcAddress16( LOWORD(hModule), name );
Alexandre Julliard641ee761997-08-04 16:34:36 +00001785}
1786
1787/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001788 * GetProcAddress (KERNEL.50)
Alexandre Julliard2787be81995-05-22 18:23:01 +00001789 */
Alexandre Julliardac7efef2000-11-27 21:54:01 +00001790FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
Alexandre Julliard2787be81995-05-22 18:23:01 +00001791{
1792 WORD ordinal;
Alexandre Julliardca22b331996-07-12 19:02:39 +00001793 FARPROC16 ret;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001794
1795 if (!hModule) hModule = GetCurrentTask();
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001796 hModule = GetExePtr( hModule );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001797
1798 if (HIWORD(name) != 0)
1799 {
Alexandre Julliardac7efef2000-11-27 21:54:01 +00001800 ordinal = NE_GetOrdinal( hModule, name );
1801 TRACE("%04x '%s'\n", hModule, name );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001802 }
1803 else
1804 {
1805 ordinal = LOWORD(name);
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001806 TRACE("%04x %04x\n", hModule, ordinal );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001807 }
Alexandre Julliardca22b331996-07-12 19:02:39 +00001808 if (!ordinal) return (FARPROC16)0;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001809
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001810 ret = NE_GetEntryPoint( hModule, ordinal );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001811
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001812 TRACE("returning %08x\n", (UINT)ret );
Alexandre Julliardca22b331996-07-12 19:02:39 +00001813 return ret;
1814}
1815
1816
1817/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +00001818 * GetProcAddress (KERNEL32.@)
Alexandre Julliardca22b331996-07-12 19:02:39 +00001819 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001820FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
Alexandre Julliardca22b331996-07-12 19:02:39 +00001821{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001822 return MODULE_GetProcAddress( hModule, function, TRUE );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001823}
1824
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001825/***********************************************************************
Patrik Stridvall54fe8382000-04-06 20:21:16 +00001826 * GetProcAddress32 (KERNEL.453)
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001827 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001828FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001829{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001830 return MODULE_GetProcAddress( hModule, function, FALSE );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001831}
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001832
1833/***********************************************************************
Patrik Stridvall2d6457c2000-03-28 20:22:59 +00001834 * MODULE_GetProcAddress (internal)
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001835 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001836FARPROC MODULE_GetProcAddress(
1837 HMODULE hModule, /* [in] current module handle */
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001838 LPCSTR function, /* [in] function to be looked up */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001839 BOOL snoop )
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001840{
Alexandre Julliard081ee942000-08-07 04:12:41 +00001841 WINE_MODREF *wm;
1842 FARPROC retproc = 0;
Alexandre Julliardca22b331996-07-12 19:02:39 +00001843
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001844 if (HIWORD(function))
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001845 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001846 else
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001847 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
Alexandre Julliard081ee942000-08-07 04:12:41 +00001848
Alexandre Julliard64781642002-02-02 18:13:50 +00001849 RtlEnterCriticalSection( &loader_section );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001850 if ((wm = MODULE32_LookupHMODULE( hModule )))
Alexandre Julliarde658d821997-11-30 17:45:40 +00001851 {
Alexandre Julliard081ee942000-08-07 04:12:41 +00001852 retproc = wm->find_export( wm, function, snoop );
1853 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001854 }
Alexandre Julliard64781642002-02-02 18:13:50 +00001855 RtlLeaveCriticalSection( &loader_section );
Alexandre Julliard081ee942000-08-07 04:12:41 +00001856 return retproc;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001857}
1858
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001859
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001860/***************************************************************************
1861 * HasGPHandler (KERNEL.338)
1862 */
1863
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001864#include "pshpack1.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001865typedef struct _GPHANDLERDEF
1866{
1867 WORD selector;
1868 WORD rangeStart;
1869 WORD rangeEnd;
1870 WORD handler;
1871} GPHANDLERDEF;
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001872#include "poppack.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001873
Alexandre Julliarda3960291999-02-26 11:11:13 +00001874SEGPTR WINAPI HasGPHandler16( SEGPTR address )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001875{
1876 HMODULE16 hModule;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001877 int gpOrdinal;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001878 SEGPTR gpPtr;
1879 GPHANDLERDEF *gpHandler;
1880
Alexandre Julliarda3960291999-02-26 11:11:13 +00001881 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001882 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1883 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001884 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
Alexandre Julliard982a2232000-12-13 20:20:09 +00001885 && (gpHandler = MapSL( gpPtr )) != NULL )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001886 {
1887 while (gpHandler->selector)
1888 {
1889 if ( SELECTOROF(address) == gpHandler->selector
1890 && OFFSETOF(address) >= gpHandler->rangeStart
1891 && OFFSETOF(address) < gpHandler->rangeEnd )
Alexandre Julliard982a2232000-12-13 20:20:09 +00001892 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001893 gpHandler++;
1894 }
1895 }
1896
1897 return 0;
1898}
1899