blob: f0173231bbab61a4c355cab7489e182aec26dbfb [file] [log] [blame]
Alexandre Julliard594997c1995-04-30 10:05:20 +00001/*
2 * Modules
3 *
4 * Copyright 1995 Alexandre Julliard
5 */
6
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +00007#include <assert.h>
Alexandre Julliard594997c1995-04-30 10:05:20 +00008#include <fcntl.h>
9#include <stdlib.h>
Alexandre Julliard594997c1995-04-30 10:05:20 +000010#include <string.h>
11#include <sys/types.h>
12#include <unistd.h>
Michael Vekslerca1bc861999-01-01 18:57:33 +000013#include "wine/winuser16.h"
Marcus Meissner317af321999-02-17 13:51:06 +000014#include "wine/winbase16.h"
Ulrich Weigand6e0d3861999-02-28 11:14:32 +000015#include "windef.h"
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000016#include "winerror.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000017#include "file.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000018#include "global.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000019#include "heap.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000020#include "module.h"
Ulrich Weigandc50a1d01999-08-15 12:45:01 +000021#include "snoop.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000022#include "neexe.h"
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000023#include "pe_image.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000024#include "dosexe.h"
Alexandre Julliard02e90081998-01-04 17:49:09 +000025#include "process.h"
Alexandre Julliarda69b88b1998-03-15 20:29:56 +000026#include "thread.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000027#include "selectors.h"
Alexandre Julliard2787be81995-05-22 18:23:01 +000028#include "stackframe.h"
29#include "task.h"
Alexandre Julliard06c275a1999-05-02 14:32:27 +000030#include "debugtools.h"
Alexandre Julliardff8331e1995-09-18 11:19:54 +000031#include "callback.h"
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +000032#include "loadorder.h"
33#include "elfdll.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000034
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000035DECLARE_DEBUG_CHANNEL(module)
36DECLARE_DEBUG_CHANNEL(win32)
37
Guy Albertelli0e65b381999-06-12 10:49:36 +000038/*************************************************************************
39 * MODULE_WalkModref
40 * Walk MODREFs for input process ID
41 */
42void MODULE_WalkModref( DWORD id )
43{
44 int i;
45 WINE_MODREF *zwm, *prev = NULL;
46 PDB *pdb = PROCESS_IdToPDB( id );
47
48 if (!pdb) {
49 MESSAGE("Invalid process id (pid)\n");
50 return;
51 }
52
53 MESSAGE("Modref list for process pdb=%p\n", pdb);
54 MESSAGE("Modref next prev handle deps flags name\n");
55 for ( zwm = pdb->modref_list; zwm; zwm = zwm->next) {
56 MESSAGE("%p %p %p %04x %5d %04x %s\n", zwm, zwm->next, zwm->prev,
57 zwm->module, zwm->nDeps, zwm->flags, zwm->modname);
58 for ( i = 0; i < zwm->nDeps; i++ ) {
59 if ( zwm->deps[i] )
60 MESSAGE(" %d %p %s\n", i, zwm->deps[i], zwm->deps[i]->modname);
61 }
62 if (prev != zwm->prev)
63 MESSAGE(" --> modref corrupt, previous pointer wrong!!\n");
64 prev = zwm;
65 }
66}
Alexandre Julliard594997c1995-04-30 10:05:20 +000067
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000068/*************************************************************************
69 * MODULE32_LookupHMODULE
70 * looks for the referenced HMODULE in the current process
Alexandre Julliard329f0681996-04-14 13:21:20 +000071 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000072WINE_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)
Ulrich Weigand1d90d691999-02-24 14:27:07 +000077 return PROCESS_Current()->exe_modref;
78
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000079 if (!HIWORD(hmod)) {
Alexandre Julliard06c275a1999-05-02 14:32:27 +000080 ERR_(module)("tried to lookup 0x%04x in win32 module handler!\n",hmod);
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000081 return NULL;
82 }
Ulrich Weigand1d90d691999-02-24 14:27:07 +000083 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next )
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000084 if (wm->module == hmod)
85 return wm;
86 return NULL;
87}
88
Ulrich Weiganda3527cf1998-10-11 19:31:10 +000089/*************************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +000090 * MODULE_InitDll
91 */
92static BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
93{
94 BOOL retv = TRUE;
95
96 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
97 "THREAD_ATTACH", "THREAD_DETACH" };
98 assert( wm );
99
100
101 /* Skip calls for modules loaded with special load flags */
102
103 if ( ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
104 || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
105 return TRUE;
106
107
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000108 TRACE_(module)("(%s,%s,%p) - CALL\n",
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000109 wm->modname, typeName[type], lpReserved );
110
111 /* Call the initialization routine */
112 switch ( wm->type )
113 {
114 case MODULE32_PE:
115 retv = PE_InitDLL( wm, type, lpReserved );
116 break;
117
118 case MODULE32_ELF:
119 /* no need to do that, dlopen() already does */
120 break;
121
122 default:
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000123 ERR_(module)("wine_modref type %d not handled.\n", wm->type );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000124 retv = FALSE;
125 break;
126 }
127
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000128 TRACE_(module)("(%s,%s,%p) - RETURN %d\n",
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000129 wm->modname, typeName[type], lpReserved, retv );
130
131 return retv;
132}
133
134/*************************************************************************
135 * MODULE_DllProcessAttach
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000136 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000137 * Send the process attach notification to all DLLs the given module
138 * depends on (recursively). This is somewhat complicated due to the fact that
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000139 *
140 * - we have to respect the module dependencies, i.e. modules implicitly
141 * referenced by another module have to be initialized before the module
142 * itself can be initialized
143 *
144 * - the initialization routine of a DLL can itself call LoadLibrary,
145 * thereby introducing a whole new set of dependencies (even involving
146 * the 'old' modules) at any time during the whole process
147 *
148 * (Note that this routine can be recursively entered not only directly
149 * from itself, but also via LoadLibrary from one of the called initialization
150 * routines.)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000151 *
152 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
153 * the process *detach* notifications to be sent in the correct order.
154 * This must not only take into account module dependencies, but also
155 * 'hidden' dependencies created by modules calling LoadLibrary in their
156 * attach notification routine.
157 *
158 * The strategy is rather simple: we move a WINE_MODREF to the head of the
159 * list after the attach notification has returned. This implies that the
160 * detach notifications are called in the reverse of the sequence the attach
161 * notifications *returned*.
162 *
163 * NOTE: Assumes that the process critical section is held!
164 *
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000165 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000166BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000167{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000168 BOOL retv = TRUE;
169 int i;
170 assert( wm );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000171
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000172 /* prevent infinite recursion in case of cyclical dependencies */
173 if ( ( wm->flags & WINE_MODREF_MARKER )
174 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
175 return retv;
176
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000177 TRACE_(module)("(%s,%p) - START\n",
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000178 wm->modname, lpReserved );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000179
180 /* Tag current MODREF to prevent recursive loop */
Ulrich Weigande469a581999-03-27 16:45:57 +0000181 wm->flags |= WINE_MODREF_MARKER;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000182
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000183 /* Recursively attach all DLLs this one depends on */
184 for ( i = 0; retv && i < wm->nDeps; i++ )
185 if ( wm->deps[i] )
186 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
187
188 /* Call DLL entry point */
189 if ( retv )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000190 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000191 retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved );
192 if ( retv )
Ulrich Weigande469a581999-03-27 16:45:57 +0000193 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000194 }
Ulrich Weigande469a581999-03-27 16:45:57 +0000195
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000196 /* Re-insert MODREF at head of list */
197 if ( retv && wm->prev )
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000198 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000199 wm->prev->next = wm->next;
200 if ( wm->next ) wm->next->prev = wm->prev;
201
202 wm->prev = NULL;
203 wm->next = PROCESS_Current()->modref_list;
204 PROCESS_Current()->modref_list = wm->next->prev = wm;
Kevin Holbrooka8f8bef1999-04-18 09:33:20 +0000205 }
Ulrich Weigande469a581999-03-27 16:45:57 +0000206
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000207 /* Remove recursion flag */
208 wm->flags &= ~WINE_MODREF_MARKER;
Ulrich Weigande469a581999-03-27 16:45:57 +0000209
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000210 TRACE_(module)("(%s,%p) - END\n",
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000211 wm->modname, lpReserved );
Ulrich Weigande469a581999-03-27 16:45:57 +0000212
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000213 return retv;
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000214}
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000215
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000216/*************************************************************************
217 * MODULE_DllProcessDetach
218 *
219 * Send DLL process detach notifications. See the comment about calling
220 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
221 * is set, only DLLs with zero refcount are notified.
222 *
223 * NOTE: Assumes that the process critical section is held!
224 *
225 */
226void MODULE_DllProcessDetach( BOOL bForceDetach, LPVOID lpReserved )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000227{
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000228 WINE_MODREF *wm;
229
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000230 do
231 {
232 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000233 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000234 /* Check whether to detach this DLL */
235 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
236 continue;
237 if ( wm->refCount > 0 && !bForceDetach )
238 continue;
239
240 /* Call detach notification */
241 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
242 MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved );
243
244 /* Restart at head of WINE_MODREF list, as entries might have
245 been added and/or removed while performing the call ... */
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000246 break;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000247 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000248 } while ( wm );
249}
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000250
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000251/*************************************************************************
252 * MODULE_DllThreadAttach
253 *
254 * Send DLL thread attach notifications. These are sent in the
255 * reverse sequence of process detach notification.
256 *
257 */
258void MODULE_DllThreadAttach( LPVOID lpReserved )
259{
260 WINE_MODREF *wm;
261
262 EnterCriticalSection( &PROCESS_Current()->crit_section );
263
264 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
265 if ( !wm->next )
266 break;
267
268 for ( ; wm; wm = wm->prev )
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000269 {
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000270 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
271 continue;
272 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
273 continue;
Ulrich Weigande469a581999-03-27 16:45:57 +0000274
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000275 MODULE_InitDll( wm, DLL_THREAD_ATTACH, lpReserved );
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000276 }
277
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000278 LeaveCriticalSection( &PROCESS_Current()->crit_section );
279}
Ulrich Weigandebc543c1998-10-23 09:37:23 +0000280
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000281/*************************************************************************
282 * MODULE_DllThreadDetach
283 *
284 * Send DLL thread detach notifications. These are sent in the
285 * same sequence as process detach notification.
286 *
287 */
288void MODULE_DllThreadDetach( LPVOID lpReserved )
289{
290 WINE_MODREF *wm;
291
292 EnterCriticalSection( &PROCESS_Current()->crit_section );
293
294 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
295 {
296 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
297 continue;
298 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
299 continue;
300
301 MODULE_InitDll( wm, DLL_THREAD_DETACH, lpReserved );
302 }
303
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000304 LeaveCriticalSection( &PROCESS_Current()->crit_section );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +0000305}
306
Ulrich Weigande469a581999-03-27 16:45:57 +0000307/****************************************************************************
308 * DisableThreadLibraryCalls (KERNEL32.74)
309 *
310 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
311 */
312BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
313{
Bertho Stultiens1b346971999-04-11 14:52:24 +0000314 WINE_MODREF *wm;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000315 BOOL retval = TRUE;
Ulrich Weigande469a581999-03-27 16:45:57 +0000316
317 EnterCriticalSection( &PROCESS_Current()->crit_section );
Ulrich Weigande469a581999-03-27 16:45:57 +0000318
Bertho Stultiens1b346971999-04-11 14:52:24 +0000319 wm = MODULE32_LookupHMODULE( hModule );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000320 if ( !wm )
321 retval = FALSE;
322 else
323 wm->flags |= WINE_MODREF_NO_DLL_CALLS;
324
Ulrich Weigande469a581999-03-27 16:45:57 +0000325 LeaveCriticalSection( &PROCESS_Current()->crit_section );
326
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000327 return retval;
Ulrich Weigande469a581999-03-27 16:45:57 +0000328}
329
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000330
331/***********************************************************************
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000332 * MODULE_CreateDummyModule
333 *
334 * Create a dummy NE module for Win32 or Winelib.
335 */
Ulrich Weigande7957d61999-11-10 19:45:56 +0000336HMODULE MODULE_CreateDummyModule( const OFSTRUCT *ofs, LPCSTR modName, WORD version )
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000337{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000338 HMODULE hModule;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000339 NE_MODULE *pModule;
340 SEGTABLEENTRY *pSegment;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000341 char *pStr,*s;
Alexandre Julliard17216f51997-10-12 16:30:17 +0000342 int len;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000343 const char* basename;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000344
Alexandre Julliarda3960291999-02-26 11:11:13 +0000345 INT of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000346 + strlen(ofs->szPathName) + 1;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000347 INT size = sizeof(NE_MODULE) +
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000348 /* loaded file info */
349 of_size +
350 /* segment table: DS,CS */
351 2 * sizeof(SEGTABLEENTRY) +
352 /* name table */
353 9 +
354 /* several empty tables */
355 8;
356
357 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000358 if (!hModule) return (HMODULE)11; /* invalid exe */
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000359
Alexandre Julliarda3960291999-02-26 11:11:13 +0000360 FarSetOwner16( hModule, hModule );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000361 pModule = (NE_MODULE *)GlobalLock16( hModule );
362
363 /* Set all used entries */
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000364 pModule->magic = IMAGE_OS2_SIGNATURE;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000365 pModule->count = 1;
366 pModule->next = 0;
367 pModule->flags = 0;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000368 pModule->dgroup = 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000369 pModule->ss = 1;
370 pModule->cs = 2;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000371 pModule->heap_size = 0;
372 pModule->stack_size = 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000373 pModule->seg_count = 2;
374 pModule->modref_count = 0;
375 pModule->nrname_size = 0;
376 pModule->fileinfo = sizeof(NE_MODULE);
377 pModule->os_flags = NE_OSFLAGS_WINDOWS;
Ulrich Weigande7957d61999-11-10 19:45:56 +0000378 pModule->expected_version = version;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000379 pModule->self = hModule;
380
381 /* Set loaded file information */
382 memcpy( pModule + 1, ofs, of_size );
383 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
384
385 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000386 pModule->seg_table = (int)pSegment - (int)pModule;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000387 /* Data segment */
388 pSegment->size = 0;
389 pSegment->flags = NE_SEGFLAGS_DATA;
390 pSegment->minsize = 0x1000;
391 pSegment++;
392 /* Code segment */
393 pSegment->flags = 0;
394 pSegment++;
395
396 /* Module name */
397 pStr = (char *)pSegment;
398 pModule->name_table = (int)pStr - (int)pModule;
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000399 if ( modName )
400 basename = modName;
401 else
402 {
403 basename = strrchr(ofs->szPathName,'\\');
404 if (!basename) basename = ofs->szPathName;
405 else basename++;
406 }
Alexandre Julliard17216f51997-10-12 16:30:17 +0000407 len = strlen(basename);
408 if ((s = strchr(basename,'.'))) len = s - basename;
409 if (len > 8) len = 8;
410 *pStr = len;
411 strncpy( pStr+1, basename, len );
412 if (len < 8) pStr[len+1] = 0;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000413 pStr += 9;
414
415 /* All tables zero terminated */
416 pModule->res_table = pModule->import_table = pModule->entry_table =
417 (int)pStr - (int)pModule;
418
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000419 NE_RegisterModule( pModule );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000420 return hModule;
421}
422
423
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000424/**********************************************************************
425 * MODULE_FindModule32
426 *
427 * Find a (loaded) win32 module depending on path
428 * The handling of '.' is a bit weird, but we need it that way,
429 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
430 * this is the only way to differentiate. (mainly hypertrm.exe)
431 *
432 * RETURNS
433 * the module handle if found
434 * 0 if not
435 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000436WINE_MODREF *MODULE_FindModule(
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000437 LPCSTR path /* [in] pathname of module/library to be found */
438) {
439 LPSTR filename;
440 LPSTR dotptr;
441 WINE_MODREF *wm;
442
443 if (!(filename = strrchr( path, '\\' )))
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000444 filename = HEAP_strdupA( GetProcessHeap(), 0, path );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000445 else
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000446 filename = HEAP_strdupA( GetProcessHeap(), 0, filename+1 );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000447 dotptr=strrchr(filename,'.');
448
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000449 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000450 LPSTR xmodname,xdotptr;
451
452 assert (wm->modname);
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000453 xmodname = HEAP_strdupA( GetProcessHeap(), 0, wm->modname );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000454 xdotptr=strrchr(xmodname,'.');
455 if ( (xdotptr && !dotptr) ||
456 (!xdotptr && dotptr)
457 ) {
458 if (dotptr) *dotptr = '\0';
459 if (xdotptr) *xdotptr = '\0';
460 }
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +0000461 if (!strcasecmp( filename, xmodname)) {
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000462 HeapFree( GetProcessHeap(), 0, filename );
463 HeapFree( GetProcessHeap(), 0, xmodname );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000464 return wm;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000465 }
466 if (dotptr) *dotptr='.';
467 /* FIXME: add paths, shortname */
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000468 HeapFree( GetProcessHeap(), 0, xmodname );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000469 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000470 /* if that fails, try looking for the filename... */
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000471 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
Alexandre Julliard642d3131998-07-12 19:29:36 +0000472 LPSTR xlname,xdotptr;
473
474 assert (wm->longname);
Alexander Larssond235b131998-10-11 13:59:08 +0000475 xlname = strrchr(wm->longname,'\\');
Alexandre Julliard642d3131998-07-12 19:29:36 +0000476 if (!xlname)
477 xlname = wm->longname;
478 else
479 xlname++;
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000480 xlname = HEAP_strdupA( GetProcessHeap(), 0, xlname );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000481 xdotptr=strrchr(xlname,'.');
482 if ( (xdotptr && !dotptr) ||
483 (!xdotptr && dotptr)
484 ) {
485 if (dotptr) *dotptr = '\0';
486 if (xdotptr) *xdotptr = '\0';
487 }
488 if (!strcasecmp( filename, xlname)) {
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000489 HeapFree( GetProcessHeap(), 0, filename );
490 HeapFree( GetProcessHeap(), 0, xlname );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000491 return wm;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000492 }
493 if (dotptr) *dotptr='.';
494 /* FIXME: add paths, shortname */
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000495 HeapFree( GetProcessHeap(), 0, xlname );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000496 }
Ulrich Weigand1d90d691999-02-24 14:27:07 +0000497 HeapFree( GetProcessHeap(), 0, filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000498 return NULL;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000499}
500
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000501/***********************************************************************
502 * MODULE_GetBinaryType
503 *
504 * The GetBinaryType function determines whether a file is executable
505 * or not and if it is it returns what type of executable it is.
506 * The type of executable is a property that determines in which
507 * subsystem an executable file runs under.
508 *
509 * Binary types returned:
510 * SCS_32BIT_BINARY: A Win32 based application
511 * SCS_DOS_BINARY: An MS-Dos based application
512 * SCS_WOW_BINARY: A Win16 based application
513 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
514 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000515 * SCS_OS216_BINARY: A 16bit OS/2 based application
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000516 *
517 * Returns TRUE if the file is an executable in which case
518 * the value pointed by lpBinaryType is set.
519 * Returns FALSE if the file is not an executable or if the function fails.
520 *
521 * To do so it opens the file and reads in the header information
522 * if the extended header information is not presend it will
523 * assume that that the file is a DOS executable.
524 * If the extended header information is present it will
525 * determine if the file is an 16 or 32 bit Windows executable
526 * by check the flags in the header.
527 *
528 * Note that .COM and .PIF files are only recognized by their
529 * file name extension; but Windows does it the same way ...
Alexandre Julliard594997c1995-04-30 10:05:20 +0000530 */
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000531static BOOL MODULE_GetBinaryType( HFILE hfile, OFSTRUCT *ofs,
532 LPDWORD lpBinaryType )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000533{
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000534 IMAGE_DOS_HEADER mz_header;
535 char magic[4], *ptr;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000536
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000537 /* Seek to the start of the file and read the DOS header information.
538 */
539 if ( _llseek( hfile, 0, SEEK_SET ) >= 0 &&
540 _lread( hfile, &mz_header, sizeof(mz_header) ) == sizeof(mz_header) )
Ulrich Weigand7df1fbb1998-11-01 18:01:53 +0000541 {
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000542 /* Now that we have the header check the e_magic field
543 * to see if this is a dos image.
544 */
545 if ( mz_header.e_magic == IMAGE_DOS_SIGNATURE )
546 {
547 BOOL lfanewValid = FALSE;
548 /* We do have a DOS image so we will now try to seek into
549 * the file by the amount indicated by the field
550 * "Offset to extended header" and read in the
551 * "magic" field information at that location.
552 * This will tell us if there is more header information
553 * to read or not.
554 */
555 /* But before we do we will make sure that header
556 * structure encompasses the "Offset to extended header"
557 * field.
558 */
559 if ( (mz_header.e_cparhdr<<4) >= sizeof(IMAGE_DOS_HEADER) )
Alexandre Julliard8c81b7431999-10-13 15:47:38 +0000560 if ( ( mz_header.e_crlc == 0 ) ||
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000561 ( mz_header.e_lfarlc >= sizeof(IMAGE_DOS_HEADER) ) )
562 if ( mz_header.e_lfanew >= sizeof(IMAGE_DOS_HEADER) &&
563 _llseek( hfile, mz_header.e_lfanew, SEEK_SET ) >= 0 &&
564 _lread( hfile, magic, sizeof(magic) ) == sizeof(magic) )
565 lfanewValid = TRUE;
566
567 if ( !lfanewValid )
568 {
569 /* If we cannot read this "extended header" we will
570 * assume that we have a simple DOS executable.
571 */
572 *lpBinaryType = SCS_DOS_BINARY;
573 return TRUE;
574 }
575 else
576 {
577 /* Reading the magic field succeeded so
578 * we will try to determine what type it is.
579 */
580 if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
581 {
582 /* This is an NT signature.
583 */
584 *lpBinaryType = SCS_32BIT_BINARY;
585 return TRUE;
586 }
587 else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
588 {
589 /* The IMAGE_OS2_SIGNATURE indicates that the
590 * "extended header is a Windows executable (NE)
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000591 * header." This can mean either a 16-bit OS/2
592 * or a 16-bit Windows or even a DOS program
593 * (running under a DOS extender). To decide
594 * which, we'll have to read the NE header.
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000595 */
Ulrich Weigandd523e4d1999-06-07 17:37:43 +0000596
597 IMAGE_OS2_HEADER ne;
598 if ( _llseek( hfile, mz_header.e_lfanew, SEEK_SET ) >= 0 &&
599 _lread( hfile, &ne, sizeof(ne) ) == sizeof(ne) )
600 {
601 switch ( ne.operating_system )
602 {
603 case 2: *lpBinaryType = SCS_WOW_BINARY; return TRUE;
604 case 5: *lpBinaryType = SCS_DOS_BINARY; return TRUE;
605 default: *lpBinaryType = SCS_OS216_BINARY; return TRUE;
606 }
607 }
608 /* Couldn't read header, so abort. */
609 return FALSE;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000610 }
611 else
612 {
613 /* Unknown extended header, so abort.
614 */
615 return FALSE;
616 }
617 }
618 }
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000619 }
Alexandre Julliard2787be81995-05-22 18:23:01 +0000620
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000621 /* If we get here, we don't even have a correct MZ header.
622 * Try to check the file extension for known types ...
623 */
624 ptr = strrchr( ofs->szPathName, '.' );
625 if ( ptr && !strchr( ptr, '\\' ) && !strchr( ptr, '/' ) )
626 {
627 if ( !lstrcmpiA( ptr, ".COM" ) )
628 {
629 *lpBinaryType = SCS_DOS_BINARY;
630 return TRUE;
631 }
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000632
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000633 if ( !lstrcmpiA( ptr, ".PIF" ) )
634 {
635 *lpBinaryType = SCS_PIF_BINARY;
636 return TRUE;
637 }
638 }
Alexandre Julliard1e37a181996-08-18 16:21:52 +0000639
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000640 return FALSE;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000641}
642
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000643/***********************************************************************
644 * GetBinaryTypeA [KERNEL32.280]
Alexandre Julliard01d63461997-01-20 19:43:45 +0000645 */
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000646BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000647{
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000648 BOOL ret = FALSE;
649 HFILE hfile;
650 OFSTRUCT ofs;
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000651
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000652 TRACE_(win32)("%s\n", lpApplicationName );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000653
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000654 /* Sanity check.
655 */
656 if ( lpApplicationName == NULL || lpBinaryType == NULL )
657 return FALSE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000658
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000659 /* Open the file indicated by lpApplicationName for reading.
660 */
661 if ( (hfile = OpenFile( lpApplicationName, &ofs, OF_READ )) == HFILE_ERROR )
662 return FALSE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000663
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000664 /* Check binary type
665 */
666 ret = MODULE_GetBinaryType( hfile, &ofs, lpBinaryType );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000667
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000668 /* Close the file.
669 */
670 CloseHandle( hfile );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000671
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000672 return ret;
673}
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000674
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000675/***********************************************************************
676 * GetBinaryTypeW [KERNEL32.281]
677 */
678BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
679{
680 BOOL ret = FALSE;
681 LPSTR strNew = NULL;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000682
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000683 TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000684
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000685 /* Sanity check.
686 */
687 if ( lpApplicationName == NULL || lpBinaryType == NULL )
688 return FALSE;
689
690 /* Convert the wide string to a ascii string.
691 */
692 strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
693
694 if ( strNew != NULL )
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000695 {
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000696 ret = GetBinaryTypeA( strNew, lpBinaryType );
697
698 /* Free the allocated string.
699 */
700 HeapFree( GetProcessHeap(), 0, strNew );
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000701 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000702
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000703 return ret;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000704}
705
Alexandre Julliard491502b1997-11-01 19:08:16 +0000706/**********************************************************************
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000707 * MODULE_CreateUnixProcess
708 */
709static BOOL MODULE_CreateUnixProcess( LPCSTR filename, LPCSTR lpCmdLine,
710 LPSTARTUPINFOA lpStartupInfo,
711 LPPROCESS_INFORMATION lpProcessInfo,
712 BOOL useWine )
713{
714 DOS_FULL_NAME full_name;
715 const char *unixfilename = filename;
716 const char *argv[256], **argptr;
Uwe Bonnes20f7ef71999-09-28 13:08:36 +0000717 char *cmdline = NULL;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000718 BOOL iconic = FALSE;
719
720 /* Get Unix file name and iconic flag */
721
722 if ( lpStartupInfo->dwFlags & STARTF_USESHOWWINDOW )
723 if ( lpStartupInfo->wShowWindow == SW_SHOWMINIMIZED
724 || lpStartupInfo->wShowWindow == SW_SHOWMINNOACTIVE )
725 iconic = TRUE;
726
Dave Picklesfec23291999-06-26 11:48:26 +0000727 /* Build argument list */
728
729 argptr = argv;
730 if ( !useWine )
731 {
Uwe Bonnes20f7ef71999-09-28 13:08:36 +0000732 char *p;
733 p = cmdline = strdup(lpCmdLine);
Alexandre Julliard94822421999-09-22 15:18:52 +0000734 if (strchr(filename, '/') || strchr(filename, ':') || strchr(filename, '\\'))
735 {
736 if ( DOSFS_GetFullName( filename, TRUE, &full_name ) )
737 unixfilename = full_name.long_name;
738 }
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000739 *argptr++ = unixfilename;
740 if (iconic) *argptr++ = "-iconic";
741 while (1)
742 {
743 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
744 if (!*p) break;
745 *argptr++ = p;
746 while (*p && *p != ' ' && *p != '\t') p++;
747 }
748 }
749 else
750 {
751 *argptr++ = "wine";
752 if (iconic) *argptr++ = "-iconic";
753 *argptr++ = lpCmdLine;
754 }
755 *argptr++ = 0;
756
757 /* Fork and execute */
758
759 if ( !fork() )
760 {
761 /* Note: don't use Wine routines here, as this process
762 has not been correctly initialized! */
763
764 execvp( argv[0], (char**)argv );
765
766 /* Failed ! */
767 if ( useWine )
768 fprintf( stderr, "CreateProcess: can't exec 'wine %s'\n",
769 lpCmdLine );
770 exit( 1 );
771 }
772
773 /* Fake success return value */
774
775 memset( lpProcessInfo, '\0', sizeof( *lpProcessInfo ) );
776 lpProcessInfo->hProcess = INVALID_HANDLE_VALUE;
777 lpProcessInfo->hThread = INVALID_HANDLE_VALUE;
Uwe Bonnes20f7ef71999-09-28 13:08:36 +0000778 if (cmdline) free(cmdline);
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000779
780 SetLastError( ERROR_SUCCESS );
781 return TRUE;
782}
783
784/***********************************************************************
785 * WinExec16 (KERNEL.166)
786 */
787HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
788{
Ulrich Weigand6ce40061999-05-03 09:22:55 +0000789 HINSTANCE16 hInst;
790
791 SYSLEVEL_ReleaseWin16Lock();
792 hInst = WinExec( lpCmdLine, nCmdShow );
793 SYSLEVEL_RestoreWin16Lock();
794
795 return hInst;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000796}
797
798/***********************************************************************
799 * WinExec (KERNEL32.566)
800 */
801HINSTANCE WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
802{
803 LOADPARAMS params;
804 UINT16 paramCmdShow[2];
805
806 if (!lpCmdLine)
807 return 2; /* File not found */
808
809 /* Set up LOADPARAMS buffer for LoadModule */
810
811 memset( &params, '\0', sizeof(params) );
812 params.lpCmdLine = (LPSTR)lpCmdLine;
813 params.lpCmdShow = paramCmdShow;
814 params.lpCmdShow[0] = 2;
815 params.lpCmdShow[1] = nCmdShow;
816
817 /* Now load the executable file */
818
819 return LoadModule( NULL, &params );
820}
821
822/**********************************************************************
823 * LoadModule (KERNEL32.499)
Alexandre Julliard491502b1997-11-01 19:08:16 +0000824 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000825HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
Alexandre Julliard491502b1997-11-01 19:08:16 +0000826{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000827 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000828 PROCESS_INFORMATION info;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000829 STARTUPINFOA startup;
830 HINSTANCE hInstance;
831 PDB *pdb;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000832 TDB *tdb;
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000833
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000834 memset( &startup, '\0', sizeof(startup) );
835 startup.cb = sizeof(startup);
836 startup.dwFlags = STARTF_USESHOWWINDOW;
837 startup.wShowWindow = params->lpCmdShow? params->lpCmdShow[1] : 0;
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000838
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000839 if ( !CreateProcessA( name, params->lpCmdLine,
840 NULL, NULL, FALSE, 0, params->lpEnvAddress,
841 NULL, &startup, &info ) )
842 {
843 hInstance = GetLastError();
844 if ( hInstance < 32 ) return hInstance;
845
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000846 FIXME_(module)("Strange error set by CreateProcess: %d\n", hInstance );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000847 return 11;
848 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000849
Ulrich Weigand2aad33e1998-12-10 08:30:30 +0000850 /* Get 16-bit hInstance/hTask from process */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000851 pdb = PROCESS_IdToPDB( info.dwProcessId );
Ulrich Weigand2aad33e1998-12-10 08:30:30 +0000852 tdb = pdb? (TDB *)GlobalLock16( pdb->task ) : NULL;
853 hInstance = tdb && tdb->hInstance? tdb->hInstance : pdb? pdb->task : 0;
Alexandre Julliard481a8e21999-05-03 08:58:25 +0000854 /* If there is no hInstance (32-bit process) return a dummy value
855 * that must be > 31
856 * FIXME: should do this in all cases and fix Win16 callers */
857 if (!hInstance) hInstance = 33;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000858
859 /* Close off the handles */
860 CloseHandle( info.hThread );
861 CloseHandle( info.hProcess );
862
863 return hInstance;
Alexandre Julliard491502b1997-11-01 19:08:16 +0000864}
865
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000866/*************************************************************************
Guy Albertelli38d7da81999-06-06 09:04:29 +0000867 * get_makename_token
868 *
869 * Get next blank delimited token from input string. If quoted then
870 * process till matching quote and then till blank.
871 *
872 * Returns number of characters in token (not including \0). On
873 * end of string (EOS), returns a 0.
874 *
875 * from (IO) address of start of input string to scan, updated to
876 * next non-processed character.
877 * to (IO) address of start of output string (previous token \0
878 * char), updated to end of new output string (the \0
879 * char).
880 */
881static int get_makename_token(LPCSTR *from, LPSTR *to )
882{
883 int len = 0;
884 LPCSTR to_old = *to; /* only used for tracing */
885
886 while ( **from == ' ') {
887 /* Copy leading blanks (separators between previous */
888 /* token and this token). */
889 **to = **from;
890 (*from)++;
891 (*to)++;
892 len++;
893 }
894 do {
895 while ( (**from != 0) && (**from != ' ') && (**from != '"') ) {
896 **to = **from; (*from)++; (*to)++; len++;
897 }
898 if ( **from == '"' ) {
899 /* Handle quoted string. */
900 (*from)++;
901 if ( !strchr(*from, '"') ) {
902 /* fail - no closing quote. Return entire string */
903 while ( **from != 0 ) {
904 **to = **from; (*from)++; (*to)++; len++;
905 }
906 break;
907 }
908 while( **from != '"') {
909 **to = **from;
910 len++;
911 (*to)++;
912 (*from)++;
913 }
914 (*from)++;
915 continue;
916 }
917
918 /* either EOS or ' ' */
919 break;
920
921 } while (1);
922
923 **to = 0; /* terminate output string */
924
925 TRACE_(module)("returning token len=%d, string=%s\n",
926 len, to_old);
927
928 return len;
929}
930
931/*************************************************************************
932 * make_lpCommandLine_name
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000933 *
934 * Try longer and longer strings from "line" to find an existing
935 * file name. Each attempt is delimited by a blank outside of quotes.
936 * Also will attempt to append ".exe" if requested and not already
937 * present. Returns the address of the remaining portion of the
938 * input line.
939 *
940 */
Andreas Mohre518f471999-02-28 19:28:44 +0000941
Guy Albertelli38d7da81999-06-06 09:04:29 +0000942static BOOL make_lpCommandLine_name( LPCSTR line, LPSTR name, int namelen,
943 LPCSTR *after )
Andreas Mohre518f471999-02-28 19:28:44 +0000944{
Guy Albertelli38d7da81999-06-06 09:04:29 +0000945 BOOL found = TRUE;
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000946 LPCSTR from;
Guy Albertelli38d7da81999-06-06 09:04:29 +0000947 char buffer[260];
948 DWORD retlen;
949 LPSTR to, lastpart;
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000950
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000951 from = line;
Guy Albertelli38d7da81999-06-06 09:04:29 +0000952 to = name;
953
954 /* scan over initial blanks if any */
955 while ( *from == ' ') from++;
956
957 /* get a token and append to previous data the check for existance */
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000958 do {
Guy Albertelli38d7da81999-06-06 09:04:29 +0000959 if ( !get_makename_token( &from, &to ) ) {
960 /* EOS has occured and not found - exit */
961 retlen = 0;
962 found = FALSE;
963 break;
Jess Haas1ff60f31999-03-28 15:00:44 +0000964 }
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000965 TRACE_(module)("checking if file exists '%s'\n", name);
Guy Albertelli38d7da81999-06-06 09:04:29 +0000966 retlen = SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, &lastpart);
967 if ( retlen && (retlen < sizeof(buffer)) ) break;
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000968 } while (1);
969
Guy Albertelli38d7da81999-06-06 09:04:29 +0000970 /* if we have a non-null full path name in buffer then move to output */
Marcus Meissner1ab89071999-06-13 08:39:04 +0000971 if ( retlen ) {
972 if ( strlen(buffer) <= namelen ) {
Guy Albertelli38d7da81999-06-06 09:04:29 +0000973 strcpy( name, buffer );
Marcus Meissner1ab89071999-06-13 08:39:04 +0000974 } else {
Guy Albertelli38d7da81999-06-06 09:04:29 +0000975 /* not enough space to return full path string */
976 FIXME_(module)("internal string not long enough, need %d\n",
977 strlen(buffer) );
978 }
Marcus Meissner1ab89071999-06-13 08:39:04 +0000979 }
Guy Albertelli38d7da81999-06-06 09:04:29 +0000980
981 /* all done, indicate end of module name and then trace and exit */
982 if (after) *after = from;
983 TRACE_(module)("%i, selected file name '%s'\n and cmdline as %s\n",
984 found, name, debugstr_a(from));
985 return found;
Andreas Mohre518f471999-02-28 19:28:44 +0000986 }
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000987
988/*************************************************************************
Guy Albertelli38d7da81999-06-06 09:04:29 +0000989 * make_lpApplicationName_name
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000990 *
991 * Scan input string (the lpApplicationName) and remove any quotes
Guy Albertelli38d7da81999-06-06 09:04:29 +0000992 * if they are balanced.
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000993 *
994 */
995
Guy Albertelli38d7da81999-06-06 09:04:29 +0000996static BOOL make_lpApplicationName_name( LPCSTR line, LPSTR name, int namelen)
Guy Albertelli4f6d7f31999-05-02 10:17:31 +0000997{
998 LPCSTR from;
999 LPSTR to, to_end, to_old;
Guy Albertelli38d7da81999-06-06 09:04:29 +00001000 DOS_FULL_NAME full_name;
Guy Albertelli4f6d7f31999-05-02 10:17:31 +00001001
1002 to = name;
1003 to_end = to + namelen - 1;
1004 to_old = to;
1005
1006 while ( *line == ' ' ) line++; /* point to beginning of string */
1007 from = line;
1008 do {
Guy Albertelli38d7da81999-06-06 09:04:29 +00001009 /* Copy all input till end, or quote */
Guy Albertelli4f6d7f31999-05-02 10:17:31 +00001010 while((*from != 0) && (*from != '"') && (to < to_end))
1011 *to++ = *from++;
1012 if (to >= to_end) { *to = 0; break; }
1013
1014 if (*from == '"')
1015 {
1016 /* Handle quoted string. If there is a closing quote, copy all */
1017 /* that is inside. */
1018 from++;
1019 if (!strchr(from, '"'))
1020 {
1021 /* fail - no closing quote */
1022 to = to_old; /* restore to previous attempt */
1023 *to = 0; /* end string */
1024 break; /* exit with previous attempt */
1025 }
1026 while((*from != '"') && (to < to_end)) *to++ = *from++;
1027 if (to >= to_end) { *to = 0; break; }
1028 from++;
1029 continue; /* past quoted string, so restart from top */
1030 }
1031
1032 *to = 0; /* terminate output string */
1033 to_old = to; /* save for possible use in unmatched quote case */
1034
1035 /* loop around keeping the blank as part of file name */
1036 if (!*from)
1037 break; /* exit if out of input string */
Guy Albertelli4f6d7f31999-05-02 10:17:31 +00001038 } while (1);
1039
Guy Albertelli38d7da81999-06-06 09:04:29 +00001040 if (!DOSFS_GetFullName(name, TRUE, &full_name)) {
1041 TRACE_(module)("file not found '%s'\n", name );
1042 return FALSE;
Guy Albertelli4f6d7f31999-05-02 10:17:31 +00001043 }
1044
Guy Albertelli38d7da81999-06-06 09:04:29 +00001045 if (strlen(full_name.long_name) >= namelen ) {
1046 FIXME_(module)("name longer than buffer (len=%d), file=%s\n",
1047 namelen, full_name.long_name);
1048 return FALSE;
1049 }
1050 strcpy(name, full_name.long_name);
1051
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001052 TRACE_(module)("selected as file name '%s'\n", name );
Guy Albertelli38d7da81999-06-06 09:04:29 +00001053 return TRUE;
Andreas Mohre518f471999-02-28 19:28:44 +00001054}
1055
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001056/**********************************************************************
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001057 * CreateProcessA (KERNEL32.171)
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001058 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001059BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001060 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1061 LPSECURITY_ATTRIBUTES lpThreadAttributes,
1062 BOOL bInheritHandles, DWORD dwCreationFlags,
1063 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
1064 LPSTARTUPINFOA lpStartupInfo,
1065 LPPROCESS_INFORMATION lpProcessInfo )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001066{
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001067 BOOL retv = FALSE;
Guy Albertelli38d7da81999-06-06 09:04:29 +00001068 BOOL found_file = FALSE;
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001069 HFILE hFile;
1070 OFSTRUCT ofs;
1071 DWORD type;
Dave Picklesfec23291999-06-26 11:48:26 +00001072 char name[256], dummy[256];
Guy Albertelli4f6d7f31999-05-02 10:17:31 +00001073 LPCSTR cmdline = NULL;
Dave Picklesfec23291999-06-26 11:48:26 +00001074 LPSTR tidy_cmdline;
1075 int len = 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001076
1077 /* Get name and command line */
1078
1079 if (!lpApplicationName && !lpCommandLine)
1080 {
1081 SetLastError( ERROR_FILE_NOT_FOUND );
1082 return FALSE;
1083 }
1084
Dave Picklesfec23291999-06-26 11:48:26 +00001085 /* Process the AppName and/or CmdLine to get module name and path */
Marcus Meissner1ab89071999-06-13 08:39:04 +00001086
1087 name[0] = '\0';
1088
1089 if (lpApplicationName) {
1090 found_file = make_lpApplicationName_name( lpApplicationName, name, sizeof(name) );
Dave Picklesfec23291999-06-26 11:48:26 +00001091 if (lpCommandLine) {
1092 make_lpCommandLine_name( lpCommandLine, dummy, sizeof ( dummy ), &cmdline );
Marcus Meissner1ab89071999-06-13 08:39:04 +00001093 }
Dave Picklesfec23291999-06-26 11:48:26 +00001094 else {
1095 cmdline = lpApplicationName;
1096 }
1097 len += strlen(lpApplicationName);
1098 }
1099 else {
Marcus Meissner1ab89071999-06-13 08:39:04 +00001100 found_file = make_lpCommandLine_name( lpCommandLine, name, sizeof ( name ), &cmdline );
Dave Picklesfec23291999-06-26 11:48:26 +00001101 if (lpCommandLine) len = strlen(lpCommandLine);
1102 }
Marcus Meissner1ab89071999-06-13 08:39:04 +00001103
1104 if ( !found_file ) {
1105 /* make an early exit if file not found - save second pass */
1106 SetLastError( ERROR_FILE_NOT_FOUND );
1107 return FALSE;
1108 }
1109
Dave Picklesfec23291999-06-26 11:48:26 +00001110 len += strlen(name) + 2;
1111 tidy_cmdline = HeapAlloc( GetProcessHeap(), 0, len );
1112 sprintf( tidy_cmdline, "\"%s\"%s", name, cmdline);
1113
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001114 /* Warn if unsupported features are used */
1115
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001116 if (dwCreationFlags & DETACHED_PROCESS)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001117 FIXME_(module)("(%s,...): DETACHED_PROCESS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001118 if (dwCreationFlags & CREATE_NEW_CONSOLE)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001119 FIXME_(module)("(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001120 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001121 FIXME_(module)("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001122 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001123 FIXME_(module)("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001124 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001125 FIXME_(module)("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001126 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001127 FIXME_(module)("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001128 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001129 FIXME_(module)("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001130 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001131 FIXME_(module)("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001132 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001133 FIXME_(module)("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001134 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001135 FIXME_(module)("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001136 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001137 FIXME_(module)("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001138 if (dwCreationFlags & CREATE_NO_WINDOW)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001139 FIXME_(module)("(%s,...): CREATE_NO_WINDOW ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001140 if (dwCreationFlags & PROFILE_USER)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001141 FIXME_(module)("(%s,...): PROFILE_USER ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001142 if (dwCreationFlags & PROFILE_KERNEL)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001143 FIXME_(module)("(%s,...): PROFILE_KERNEL ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001144 if (dwCreationFlags & PROFILE_SERVER)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001145 FIXME_(module)("(%s,...): PROFILE_SERVER ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001146 if (lpCurrentDirectory)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001147 FIXME_(module)("(%s,...): lpCurrentDirectory %s ignored\n",
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001148 name, lpCurrentDirectory);
1149 if (lpStartupInfo->lpDesktop)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001150 FIXME_(module)("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001151 name, lpStartupInfo->lpDesktop);
1152 if (lpStartupInfo->lpTitle)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001153 FIXME_(module)("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001154 name, lpStartupInfo->lpTitle);
1155 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001156 FIXME_(module)("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001157 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
1158 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001159 FIXME_(module)("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001160 name, lpStartupInfo->dwFillAttribute);
1161 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001162 FIXME_(module)("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001163 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001164 FIXME_(module)("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001165 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001166 FIXME_(module)("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001167 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001168 FIXME_(module)("(%s,...): STARTF_USEHOTKEY ignored\n", name);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001169
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001170 /* Check for special case: second instance of NE module */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001171
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001172 lstrcpynA( ofs.szPathName, name, sizeof( ofs.szPathName ) );
Dave Picklesfec23291999-06-26 11:48:26 +00001173 retv = NE_CreateProcess( HFILE_ERROR, &ofs, tidy_cmdline, lpEnvironment,
Ulrich Weigand892627b1999-03-16 16:29:26 +00001174 lpProcessAttributes, lpThreadAttributes,
Alexandre Julliardd131a171999-05-23 20:02:04 +00001175 bInheritHandles, dwCreationFlags,
1176 lpStartupInfo, lpProcessInfo );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001177
1178 /* Load file and create process */
1179
1180 if ( !retv )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001181 {
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001182 /* Open file and determine executable type */
1183
1184 if ( (hFile = OpenFile( name, &ofs, OF_READ )) == HFILE_ERROR )
1185 {
1186 SetLastError( ERROR_FILE_NOT_FOUND );
Dave Picklesfec23291999-06-26 11:48:26 +00001187 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001188 return FALSE;
1189 }
1190
1191 if ( !MODULE_GetBinaryType( hFile, &ofs, &type ) )
1192 {
1193 CloseHandle( hFile );
1194
1195 /* FIXME: Try Unix executable only when appropriate! */
Dave Picklesfec23291999-06-26 11:48:26 +00001196 if ( MODULE_CreateUnixProcess( name, tidy_cmdline,
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001197 lpStartupInfo, lpProcessInfo, FALSE ) )
Dave Picklesfec23291999-06-26 11:48:26 +00001198 {
1199 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001200 return TRUE;
Dave Picklesfec23291999-06-26 11:48:26 +00001201 }
1202 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001203 SetLastError( ERROR_BAD_FORMAT );
1204 return FALSE;
1205 }
1206
1207
1208 /* Create process */
1209
1210 switch ( type )
1211 {
1212 case SCS_32BIT_BINARY:
Dave Picklesfec23291999-06-26 11:48:26 +00001213 retv = PE_CreateProcess( hFile, &ofs, tidy_cmdline, lpEnvironment,
Ulrich Weigand892627b1999-03-16 16:29:26 +00001214 lpProcessAttributes, lpThreadAttributes,
Alexandre Julliardd131a171999-05-23 20:02:04 +00001215 bInheritHandles, dwCreationFlags,
1216 lpStartupInfo, lpProcessInfo );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001217 break;
1218
1219 case SCS_DOS_BINARY:
Dave Picklesfec23291999-06-26 11:48:26 +00001220 retv = MZ_CreateProcess( hFile, &ofs, tidy_cmdline, lpEnvironment,
Ulrich Weigand892627b1999-03-16 16:29:26 +00001221 lpProcessAttributes, lpThreadAttributes,
Alexandre Julliardd131a171999-05-23 20:02:04 +00001222 bInheritHandles, dwCreationFlags,
1223 lpStartupInfo, lpProcessInfo );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001224 break;
1225
1226 case SCS_WOW_BINARY:
Dave Picklesfec23291999-06-26 11:48:26 +00001227 retv = NE_CreateProcess( hFile, &ofs, tidy_cmdline, lpEnvironment,
Ulrich Weigand892627b1999-03-16 16:29:26 +00001228 lpProcessAttributes, lpThreadAttributes,
Alexandre Julliardd131a171999-05-23 20:02:04 +00001229 bInheritHandles, dwCreationFlags,
1230 lpStartupInfo, lpProcessInfo );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001231 break;
1232
1233 case SCS_PIF_BINARY:
1234 case SCS_POSIX_BINARY:
1235 case SCS_OS216_BINARY:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001236 FIXME_(module)("Unsupported executable type: %ld\n", type );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001237 /* fall through */
1238
1239 default:
1240 SetLastError( ERROR_BAD_FORMAT );
1241 retv = FALSE;
1242 break;
1243 }
1244
1245 CloseHandle( hFile );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001246 }
Dave Picklesfec23291999-06-26 11:48:26 +00001247 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001248 return retv;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001249}
1250
1251/**********************************************************************
Ulrich Weigand6e0d3861999-02-28 11:14:32 +00001252 * CreateProcessW (KERNEL32.172)
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001253 * NOTES
1254 * lpReserved is not converted
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001255 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001256BOOL WINAPI CreateProcessW( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001257 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1258 LPSECURITY_ATTRIBUTES lpThreadAttributes,
Alexandre Julliarda3960291999-02-26 11:11:13 +00001259 BOOL bInheritHandles, DWORD dwCreationFlags,
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001260 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
Alexandre Julliarda3960291999-02-26 11:11:13 +00001261 LPSTARTUPINFOW lpStartupInfo,
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001262 LPPROCESS_INFORMATION lpProcessInfo )
Alexandre Julliarda3960291999-02-26 11:11:13 +00001263{ BOOL ret;
1264 STARTUPINFOA StartupInfoA;
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001265
1266 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
1267 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
1268 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001269
Alexandre Julliarda3960291999-02-26 11:11:13 +00001270 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFOA));
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001271 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
1272 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
1273
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001274 TRACE_(win32)("(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001275
1276 if (lpStartupInfo->lpReserved)
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001277 FIXME_(win32)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001278
Alexandre Julliarda3960291999-02-26 11:11:13 +00001279 ret = CreateProcessA( lpApplicationNameA, lpCommandLineA,
Juergen Schmiedd0fc60a1998-11-22 15:46:05 +00001280 lpProcessAttributes, lpThreadAttributes,
1281 bInheritHandles, dwCreationFlags,
1282 lpEnvironment, lpCurrentDirectoryA,
1283 &StartupInfoA, lpProcessInfo );
1284
1285 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
1286 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
1287 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
1288 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
1289
1290 return ret;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001291}
Alexandre Julliard01d63461997-01-20 19:43:45 +00001292
Alexandre Julliard77b99181997-09-14 17:17:23 +00001293/***********************************************************************
1294 * GetModuleHandle (KERNEL32.237)
1295 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001296HMODULE WINAPI GetModuleHandleA(LPCSTR module)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001297{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001298 WINE_MODREF *wm;
1299
1300 if ( module == NULL )
1301 wm = PROCESS_Current()->exe_modref;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001302 else
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001303 wm = MODULE_FindModule( module );
1304
1305 return wm? wm->module : 0;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001306}
1307
Alexandre Julliarda3960291999-02-26 11:11:13 +00001308HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001309{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001310 HMODULE hModule;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001311 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001312 hModule = GetModuleHandleA( modulea );
Alexandre Julliard77b99181997-09-14 17:17:23 +00001313 HeapFree( GetProcessHeap(), 0, modulea );
1314 return hModule;
1315}
1316
Alexandre Julliard594997c1995-04-30 10:05:20 +00001317
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001318/***********************************************************************
1319 * GetModuleFileName32A (KERNEL32.235)
1320 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001321DWORD WINAPI GetModuleFileNameA(
1322 HMODULE hModule, /* [in] module handle (32bit) */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001323 LPSTR lpFileName, /* [out] filenamebuffer */
1324 DWORD size /* [in] size of filenamebuffer */
1325) {
Ulrich Weigand1d90d691999-02-24 14:27:07 +00001326 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001327
1328 if (!wm) /* can happen on start up or the like */
1329 return 0;
1330
Alexander Larssond235b131998-10-11 13:59:08 +00001331 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
Alexandre Julliarda3960291999-02-26 11:11:13 +00001332 lstrcpynA( lpFileName, wm->longname, size );
Alexander Larssond235b131998-10-11 13:59:08 +00001333 else
Alexandre Julliarda3960291999-02-26 11:11:13 +00001334 lstrcpynA( lpFileName, wm->shortname, size );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001335
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001336 TRACE_(module)("%s\n", lpFileName );
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001337 return strlen(lpFileName);
1338}
1339
1340
1341/***********************************************************************
1342 * GetModuleFileName32W (KERNEL32.236)
1343 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001344DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName,
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001345 DWORD size )
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001346{
1347 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001348 DWORD res = GetModuleFileNameA( hModule, fnA, size );
Alexandre Julliardb1bac321996-12-15 19:45:59 +00001349 lstrcpynAtoW( lpFileName, fnA, size );
1350 HeapFree( GetProcessHeap(), 0, fnA );
1351 return res;
1352}
1353
1354
Alexandre Julliard2787be81995-05-22 18:23:01 +00001355/***********************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001356 * LoadLibraryExA (KERNEL32)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001357 */
Jim Aston031f4fa1999-10-23 19:00:02 +00001358HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001359{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001360 WINE_MODREF *wm;
Marcus Meissner574ef761999-04-03 16:23:47 +00001361
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001362 if(!libname)
1363 {
1364 SetLastError(ERROR_INVALID_PARAMETER);
1365 return 0;
1366 }
Ulrich Weigandebc543c1998-10-23 09:37:23 +00001367
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001368 EnterCriticalSection(&PROCESS_Current()->crit_section);
1369
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001370 wm = MODULE_LoadLibraryExA( libname, hfile, flags );
Ulrich Weigandebc543c1998-10-23 09:37:23 +00001371
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001372 if(wm && !MODULE_DllProcessAttach(wm, NULL))
1373 {
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001374 WARN_(module)("Attach failed for module '%s', \n", libname);
Bertho Stultiensaf5745f1999-04-19 16:32:31 +00001375 MODULE_FreeLibrary(wm);
1376 SetLastError(ERROR_DLL_INIT_FAILED);
1377 wm = NULL;
1378 }
1379
1380 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1381
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001382 return wm ? wm->module : 0;
Marcus Meissner8220bc91998-10-11 11:10:27 +00001383}
1384
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001385/***********************************************************************
1386 * MODULE_LoadLibraryExA (internal)
1387 *
1388 * Load a PE style module according to the load order.
1389 *
1390 * The HFILE parameter is not used and marked reserved in the SDK. I can
1391 * only guess that it should force a file to be mapped, but I rather
1392 * ignore the parameter because it would be extremely difficult to
1393 * integrate this with different types of module represenations.
1394 *
1395 */
1396WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
Marcus Meissner8220bc91998-10-11 11:10:27 +00001397{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001398 DWORD err;
1399 WINE_MODREF *pwm;
1400 int i;
1401 module_loadorder_t *plo;
Marcus Meissner8220bc91998-10-11 11:10:27 +00001402
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001403 EnterCriticalSection(&PROCESS_Current()->crit_section);
Bertho Stultiens1b346971999-04-11 14:52:24 +00001404
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001405 /* Check for already loaded module */
1406 if((pwm = MODULE_FindModule(libname)))
1407 {
Bertho Stultiensf4b6e821999-04-22 08:56:40 +00001408 if(!(pwm->flags & WINE_MODREF_MARKER))
1409 pwm->refCount++;
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001410 TRACE_(module)("Already loaded module '%s' at 0x%08x, count=%d, \n", libname, pwm->module, pwm->refCount);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001411 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1412 return pwm;
1413 }
1414
1415 plo = MODULE_GetLoadOrder(libname);
1416
1417 for(i = 0; i < MODULE_LOADORDER_NTYPES; i++)
1418 {
1419 switch(plo->loadorder[i])
1420 {
1421 case MODULE_LOADORDER_DLL:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001422 TRACE_(module)("Trying native dll '%s'\n", libname);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001423 pwm = PE_LoadLibraryExA(libname, flags, &err);
1424 break;
1425
1426 case MODULE_LOADORDER_ELFDLL:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001427 TRACE_(module)("Trying elfdll '%s'\n", libname);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001428 pwm = ELFDLL_LoadLibraryExA(libname, flags, &err);
1429 break;
1430
1431 case MODULE_LOADORDER_SO:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001432 TRACE_(module)("Trying so-library '%s'\n", libname);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001433 pwm = ELF_LoadLibraryExA(libname, flags, &err);
1434 break;
1435
1436 case MODULE_LOADORDER_BI:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001437 TRACE_(module)("Trying built-in '%s'\n", libname);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001438 pwm = BUILTIN32_LoadLibraryExA(libname, flags, &err);
1439 break;
1440
1441 default:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001442 ERR_(module)("Got invalid loadorder type %d (%s index %d)\n", plo->loadorder[i], plo->modulename, i);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001443 /* Fall through */
1444
1445 case MODULE_LOADORDER_INVALID: /* We ignore this as it is an empty entry */
1446 pwm = NULL;
1447 break;
1448 }
1449
1450 if(pwm)
1451 {
1452 /* Initialize DLL just loaded */
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001453 TRACE_(module)("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001454
1455 /* Set the refCount here so that an attach failure will */
1456 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1457 pwm->refCount++;
1458
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001459 LeaveCriticalSection(&PROCESS_Current()->crit_section);
Alexandre Julliardd131a171999-05-23 20:02:04 +00001460
1461 if (PROCESS_Current()->flags & PDB32_DEBUGGED)
Ulrich Weigandd024d5e1999-11-10 20:15:04 +00001462 DEBUG_SendLoadDLLEvent( -1 /*FIXME*/, pwm->module, &pwm->modname );
Alexandre Julliardd131a171999-05-23 20:02:04 +00001463
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001464 return pwm;
1465 }
1466
1467 if(err != ERROR_FILE_NOT_FOUND)
1468 break;
1469 }
1470
Ulrich Weigande7957d61999-11-10 19:45:56 +00001471 WARN_(module)("Failed to load module '%s'; error=0x%08lx, \n", libname, err);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001472 SetLastError(err);
1473 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1474 return NULL;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001475}
1476
1477/***********************************************************************
1478 * LoadLibraryA (KERNEL32)
1479 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001480HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
1481 return LoadLibraryExA(libname,0,0);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001482}
1483
1484/***********************************************************************
1485 * LoadLibraryW (KERNEL32)
1486 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001487HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001488{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001489 return LoadLibraryExW(libnameW,0,0);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001490}
1491
1492/***********************************************************************
Ulrich Weigandc3d9f281999-08-18 18:31:26 +00001493 * LoadLibrary32_16 (KERNEL.452)
1494 */
1495HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
1496{
1497 HMODULE hModule;
1498
1499 SYSLEVEL_ReleaseWin16Lock();
1500 hModule = LoadLibraryA( libname );
1501 SYSLEVEL_RestoreWin16Lock();
1502
1503 return hModule;
1504}
1505
1506/***********************************************************************
Alexandre Julliard77b99181997-09-14 17:17:23 +00001507 * LoadLibraryExW (KERNEL32)
1508 */
Jim Aston031f4fa1999-10-23 19:00:02 +00001509HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HANDLE hfile,DWORD flags)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001510{
1511 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001512 HMODULE ret = LoadLibraryExA( libnameA , hfile, flags );
Alexandre Julliard77b99181997-09-14 17:17:23 +00001513
1514 HeapFree( GetProcessHeap(), 0, libnameA );
1515 return ret;
1516}
1517
Bertho Stultiens1b346971999-04-11 14:52:24 +00001518/***********************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001519 * MODULE_FlushModrefs
Bertho Stultiens1b346971999-04-11 14:52:24 +00001520 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001521 * NOTE: Assumes that the process critical section is held!
Bertho Stultiens1b346971999-04-11 14:52:24 +00001522 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001523 * Remove all unused modrefs and call the internal unloading routines
1524 * for the library type.
Bertho Stultiens1b346971999-04-11 14:52:24 +00001525 */
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001526static void MODULE_FlushModrefs(void)
Bertho Stultiens1b346971999-04-11 14:52:24 +00001527{
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001528 WINE_MODREF *wm, *next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001529
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001530 for(wm = PROCESS_Current()->modref_list; wm; wm = next)
1531 {
1532 next = wm->next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001533
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001534 if(wm->refCount)
1535 continue;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001536
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001537 /* Unlink this modref from the chain */
1538 if(wm->next)
1539 wm->next->prev = wm->prev;
1540 if(wm->prev)
1541 wm->prev->next = wm->next;
1542 if(wm == PROCESS_Current()->modref_list)
1543 PROCESS_Current()->modref_list = wm->next;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001544
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001545 /*
1546 * The unloaders are also responsible for freeing the modref itself
1547 * because the loaders were responsible for allocating it.
1548 */
1549 switch(wm->type)
1550 {
1551 case MODULE32_PE: PE_UnloadLibrary(wm); break;
1552 case MODULE32_ELF: ELF_UnloadLibrary(wm); break;
1553 case MODULE32_ELFDLL: ELFDLL_UnloadLibrary(wm); break;
1554 case MODULE32_BI: BUILTIN32_UnloadLibrary(wm); break;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001555
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001556 default:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001557 ERR_(module)("Invalid or unhandled MODREF type %d encountered (wm=%p)\n", wm->type, wm);
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001558 }
1559 }
Bertho Stultiens1b346971999-04-11 14:52:24 +00001560}
1561
Alexandre Julliard77b99181997-09-14 17:17:23 +00001562/***********************************************************************
1563 * FreeLibrary
1564 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001565BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
Alexandre Julliard77b99181997-09-14 17:17:23 +00001566{
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001567 BOOL retv = FALSE;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001568 WINE_MODREF *wm;
Ulrich Weigande469a581999-03-27 16:45:57 +00001569
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001570 EnterCriticalSection( &PROCESS_Current()->crit_section );
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001571 PROCESS_Current()->free_lib_count++;
Bertho Stultiens1b346971999-04-11 14:52:24 +00001572
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001573 wm = MODULE32_LookupHMODULE( hLibModule );
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001574 if ( !wm || !hLibModule )
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001575 SetLastError( ERROR_INVALID_HANDLE );
Bertho Stultiens1b346971999-04-11 14:52:24 +00001576 else
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001577 retv = MODULE_FreeLibrary( wm );
Bertho Stultiens1b346971999-04-11 14:52:24 +00001578
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001579 PROCESS_Current()->free_lib_count--;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001580 LeaveCriticalSection( &PROCESS_Current()->crit_section );
Bertho Stultiens1b346971999-04-11 14:52:24 +00001581
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001582 return retv;
1583}
1584
1585/***********************************************************************
1586 * MODULE_DecRefCount
1587 *
1588 * NOTE: Assumes that the process critical section is held!
1589 */
1590static void MODULE_DecRefCount( WINE_MODREF *wm )
1591{
1592 int i;
1593
1594 if ( wm->flags & WINE_MODREF_MARKER )
1595 return;
1596
1597 if ( wm->refCount <= 0 )
1598 return;
1599
1600 --wm->refCount;
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001601 TRACE_(module)("(%s) refCount: %d\n", wm->modname, wm->refCount );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001602
1603 if ( wm->refCount == 0 )
1604 {
1605 wm->flags |= WINE_MODREF_MARKER;
1606
1607 for ( i = 0; i < wm->nDeps; i++ )
1608 if ( wm->deps[i] )
1609 MODULE_DecRefCount( wm->deps[i] );
1610
1611 wm->flags &= ~WINE_MODREF_MARKER;
1612 }
1613}
1614
1615/***********************************************************************
1616 * MODULE_FreeLibrary
1617 *
1618 * NOTE: Assumes that the process critical section is held!
1619 */
1620BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
1621{
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001622 TRACE_(module)("(%s) - START\n", wm->modname );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001623
1624 /* Recursively decrement reference counts */
1625 MODULE_DecRefCount( wm );
1626
1627 /* Call process detach notifications */
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001628 if ( PROCESS_Current()->free_lib_count <= 1 )
Alexandre Julliardd131a171999-05-23 20:02:04 +00001629 {
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001630 MODULE_DllProcessDetach( FALSE, NULL );
Alexandre Julliardd131a171999-05-23 20:02:04 +00001631 if (PROCESS_Current()->flags & PDB32_DEBUGGED)
1632 DEBUG_SendUnloadDLLEvent( wm->module );
1633 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001634
1635 MODULE_FlushModrefs();
1636
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001637 TRACE_(module)("(%s) - END\n", wm->modname );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +00001638
Ulrich Weigand6315a7f1999-05-22 10:44:39 +00001639 return TRUE;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001640}
1641
Bertho Stultiens1b346971999-04-11 14:52:24 +00001642
Sergey Turchanov5ffd2df1999-03-22 12:35:48 +00001643/***********************************************************************
1644 * FreeLibraryAndExitThread
1645 */
1646VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
1647{
1648 FreeLibrary(hLibModule);
1649 ExitThread(dwExitCode);
1650}
Alexandre Julliard2787be81995-05-22 18:23:01 +00001651
1652/***********************************************************************
Alexandre Julliarde658d821997-11-30 17:45:40 +00001653 * PrivateLoadLibrary (KERNEL32)
1654 *
1655 * FIXME: rough guesswork, don't know what "Private" means
1656 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001657HINSTANCE WINAPI PrivateLoadLibrary(LPCSTR libname)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001658{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001659 return (HINSTANCE)LoadLibrary16(libname);
Alexandre Julliarde658d821997-11-30 17:45:40 +00001660}
1661
1662
Alexandre Julliard2787be81995-05-22 18:23:01 +00001663
1664/***********************************************************************
Alexandre Julliarde658d821997-11-30 17:45:40 +00001665 * PrivateFreeLibrary (KERNEL32)
1666 *
1667 * FIXME: rough guesswork, don't know what "Private" means
1668 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001669void WINAPI PrivateFreeLibrary(HINSTANCE handle)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001670{
Alexandre Julliard44ed71f1997-12-21 19:17:50 +00001671 FreeLibrary16((HINSTANCE16)handle);
Alexandre Julliarde658d821997-11-30 17:45:40 +00001672}
1673
1674
1675/***********************************************************************
Alexandre Julliard641ee761997-08-04 16:34:36 +00001676 * WIN32_GetProcAddress16 (KERNEL32.36)
1677 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1678 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001679FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
Alexandre Julliard641ee761997-08-04 16:34:36 +00001680{
1681 WORD ordinal;
1682 FARPROC16 ret;
1683
1684 if (!hModule) {
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001685 WARN_(module)("hModule may not be 0!\n");
Alexandre Julliard641ee761997-08-04 16:34:36 +00001686 return (FARPROC16)0;
1687 }
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001688 if (HIWORD(hModule))
1689 {
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001690 WARN_(module)("hModule is Win32 handle (%08x)\n", hModule );
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001691 return (FARPROC16)0;
1692 }
1693 hModule = GetExePtr( hModule );
Alexandre Julliard641ee761997-08-04 16:34:36 +00001694 if (HIWORD(name)) {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001695 ordinal = NE_GetOrdinal( hModule, name );
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001696 TRACE_(module)("%04x '%s'\n",
Alexandre Julliard641ee761997-08-04 16:34:36 +00001697 hModule, name );
1698 } else {
1699 ordinal = LOWORD(name);
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001700 TRACE_(module)("%04x %04x\n",
Alexandre Julliard641ee761997-08-04 16:34:36 +00001701 hModule, ordinal );
1702 }
1703 if (!ordinal) return (FARPROC16)0;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001704 ret = NE_GetEntryPoint( hModule, ordinal );
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001705 TRACE_(module)("returning %08x\n",(UINT)ret);
Alexandre Julliard641ee761997-08-04 16:34:36 +00001706 return ret;
1707}
1708
1709/***********************************************************************
Alexandre Julliardca22b331996-07-12 19:02:39 +00001710 * GetProcAddress16 (KERNEL.50)
Alexandre Julliard2787be81995-05-22 18:23:01 +00001711 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001712FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
Alexandre Julliard2787be81995-05-22 18:23:01 +00001713{
1714 WORD ordinal;
Alexandre Julliardca22b331996-07-12 19:02:39 +00001715 FARPROC16 ret;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001716
1717 if (!hModule) hModule = GetCurrentTask();
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001718 hModule = GetExePtr( hModule );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001719
1720 if (HIWORD(name) != 0)
1721 {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001722 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001723 TRACE_(module)("%04x '%s'\n",
Alexandre Julliard2787be81995-05-22 18:23:01 +00001724 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1725 }
1726 else
1727 {
1728 ordinal = LOWORD(name);
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001729 TRACE_(module)("%04x %04x\n",
Alexandre Julliard2787be81995-05-22 18:23:01 +00001730 hModule, ordinal );
1731 }
Alexandre Julliardca22b331996-07-12 19:02:39 +00001732 if (!ordinal) return (FARPROC16)0;
Alexandre Julliard2787be81995-05-22 18:23:01 +00001733
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001734 ret = NE_GetEntryPoint( hModule, ordinal );
Alexandre Julliard2787be81995-05-22 18:23:01 +00001735
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001736 TRACE_(module)("returning %08x\n", (UINT)ret );
Alexandre Julliardca22b331996-07-12 19:02:39 +00001737 return ret;
1738}
1739
1740
1741/***********************************************************************
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001742 * GetProcAddress32 (KERNEL32.257)
Alexandre Julliardca22b331996-07-12 19:02:39 +00001743 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001744FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
Alexandre Julliardca22b331996-07-12 19:02:39 +00001745{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001746 return MODULE_GetProcAddress( hModule, function, TRUE );
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001747}
1748
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001749/***********************************************************************
1750 * WIN16_GetProcAddress32 (KERNEL.453)
1751 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001752FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001753{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001754 return MODULE_GetProcAddress( hModule, function, FALSE );
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001755}
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001756
1757/***********************************************************************
1758 * MODULE_GetProcAddress32 (internal)
1759 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001760FARPROC MODULE_GetProcAddress(
1761 HMODULE hModule, /* [in] current module handle */
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001762 LPCSTR function, /* [in] function to be looked up */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001763 BOOL snoop )
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001764{
Ulrich Weigand1d90d691999-02-24 14:27:07 +00001765 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
Marcus Meissner574ef761999-04-03 16:23:47 +00001766 FARPROC retproc;
Alexandre Julliardca22b331996-07-12 19:02:39 +00001767
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001768 if (HIWORD(function))
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001769 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001770 else
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001771 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
Marcus Meissner574ef761999-04-03 16:23:47 +00001772 if (!wm) {
1773 SetLastError(ERROR_INVALID_HANDLE);
Alexandre Julliarda3960291999-02-26 11:11:13 +00001774 return (FARPROC)0;
Marcus Meissner574ef761999-04-03 16:23:47 +00001775 }
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001776 switch (wm->type)
Alexandre Julliarde658d821997-11-30 17:45:40 +00001777 {
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001778 case MODULE32_PE:
Marcus Meissner574ef761999-04-03 16:23:47 +00001779 retproc = PE_FindExportedFunction( wm, function, snoop );
1780 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1781 return retproc;
Marcus Meissner8220bc91998-10-11 11:10:27 +00001782 case MODULE32_ELF:
Marcus Meissner574ef761999-04-03 16:23:47 +00001783 retproc = ELF_FindExportedFunction( wm, function);
1784 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1785 return retproc;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001786 default:
Alexandre Julliard06c275a1999-05-02 14:32:27 +00001787 ERR_(module)("wine_modref type %d not handled.\n",wm->type);
Marcus Meissner574ef761999-04-03 16:23:47 +00001788 SetLastError(ERROR_INVALID_HANDLE);
Alexandre Julliarda3960291999-02-26 11:11:13 +00001789 return (FARPROC)0;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001790 }
Alexandre Julliard2787be81995-05-22 18:23:01 +00001791}
1792
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001793
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001794/***********************************************************************
1795 * RtlImageNtHeaders (NTDLL)
1796 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001797PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001798{
Alexandre Julliard77b99181997-09-14 17:17:23 +00001799 /* basically:
1800 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1801 * but we could get HMODULE16 or the like (think builtin modules)
1802 */
1803
Ulrich Weigand1d90d691999-02-24 14:27:07 +00001804 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
Uwe Bonnes73d6c131998-10-16 09:30:33 +00001805 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
Alexandre Julliard46ea8b31998-05-03 19:01:20 +00001806 return PE_HEADER(wm->module);
Alexandre Julliard84c70f51997-05-09 08:40:27 +00001807}
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001808
1809
1810/***************************************************************************
1811 * HasGPHandler (KERNEL.338)
1812 */
1813
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001814#include "pshpack1.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001815typedef struct _GPHANDLERDEF
1816{
1817 WORD selector;
1818 WORD rangeStart;
1819 WORD rangeEnd;
1820 WORD handler;
1821} GPHANDLERDEF;
Patrik Stridvallc7a8dde1999-04-25 12:36:53 +00001822#include "poppack.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001823
Alexandre Julliarda3960291999-02-26 11:11:13 +00001824SEGPTR WINAPI HasGPHandler16( SEGPTR address )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001825{
1826 HMODULE16 hModule;
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001827 int gpOrdinal;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001828 SEGPTR gpPtr;
1829 GPHANDLERDEF *gpHandler;
1830
Alexandre Julliarda3960291999-02-26 11:11:13 +00001831 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
Ulrich Weiganda3527cf1998-10-11 19:31:10 +00001832 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1833 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001834 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1835 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1836 {
1837 while (gpHandler->selector)
1838 {
1839 if ( SELECTOROF(address) == gpHandler->selector
1840 && OFFSETOF(address) >= gpHandler->rangeStart
1841 && OFFSETOF(address) < gpHandler->rangeEnd )
1842 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1843 gpHandler->handler );
1844 gpHandler++;
1845 }
1846 }
1847
1848 return 0;
1849}
1850