Remove references to PCVOID and LPUINT.
[wine] / loader / module.c
1 /*
2  * Modules
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include "wine/winbase16.h"
34 #include "winerror.h"
35 #include "winternl.h"
36 #include "heap.h"
37 #include "file.h"
38 #include "module.h"
39 #include "wine/debug.h"
40 #include "wine/server.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(module);
43 WINE_DECLARE_DEBUG_CHANNEL(win32);
44 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
45
46 WINE_MODREF *MODULE_modref_list = NULL;
47
48 static WINE_MODREF *exe_modref;
49 static int free_lib_count;   /* recursion depth of FreeLibrary calls */
50 static int process_detaching;  /* set on process detach to avoid deadlocks with thread detach */
51
52 static CRITICAL_SECTION loader_section = CRITICAL_SECTION_INIT( "loader_section" );
53
54 /***********************************************************************
55  *           wait_input_idle
56  *
57  * Wrapper to call WaitForInputIdle USER function
58  */
59 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
60
61 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
62 {
63     HMODULE mod = GetModuleHandleA( "user32.dll" );
64     if (mod)
65     {
66         WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
67         if (ptr) return ptr( process, timeout );
68     }
69     return 0;
70 }
71
72
73 /*************************************************************************
74  *              MODULE32_LookupHMODULE
75  * looks for the referenced HMODULE in the current process
76  * NOTE: Assumes that the process critical section is held!
77  */
78 static WINE_MODREF *MODULE32_LookupHMODULE( HMODULE hmod )
79 {
80     WINE_MODREF *wm;
81
82     if (!hmod)
83         return exe_modref;
84
85     if (!HIWORD(hmod)) {
86         ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod);
87         SetLastError( ERROR_INVALID_HANDLE );
88         return NULL;
89     }
90     for ( wm = MODULE_modref_list; wm; wm=wm->next )
91         if (wm->module == hmod)
92             return wm;
93     SetLastError( ERROR_INVALID_HANDLE );
94     return NULL;
95 }
96
97 /*************************************************************************
98  *              MODULE_AllocModRef
99  *
100  * Allocate a WINE_MODREF structure and add it to the process list
101  * NOTE: Assumes that the process critical section is held!
102  */
103 WINE_MODREF *MODULE_AllocModRef( HMODULE hModule, LPCSTR filename )
104 {
105     WINE_MODREF *wm;
106
107     DWORD long_len = strlen( filename );
108     DWORD short_len = GetShortPathNameA( filename, NULL, 0 );
109
110     if ((wm = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
111                          sizeof(*wm) + long_len + short_len + 1 )))
112     {
113         wm->module = hModule;
114         wm->tlsindex = -1;
115
116         wm->filename = wm->data;
117         memcpy( wm->filename, filename, long_len + 1 );
118         if ((wm->modname = strrchr( wm->filename, '\\' ))) wm->modname++;
119         else wm->modname = wm->filename;
120
121         wm->short_filename = wm->filename + long_len + 1;
122         GetShortPathNameA( wm->filename, wm->short_filename, short_len + 1 );
123         if ((wm->short_modname = strrchr( wm->short_filename, '\\' ))) wm->short_modname++;
124         else wm->short_modname = wm->short_filename;
125
126         wm->next = MODULE_modref_list;
127         if (wm->next) wm->next->prev = wm;
128         MODULE_modref_list = wm;
129
130         if (!(RtlImageNtHeader(hModule)->FileHeader.Characteristics & IMAGE_FILE_DLL))
131         {
132             if (!exe_modref) exe_modref = wm;
133             else FIXME( "Trying to load second .EXE file: %s\n", filename );
134         }
135     }
136     return wm;
137 }
138
139 /*************************************************************************
140  *              MODULE_InitDLL
141  */
142 static BOOL MODULE_InitDLL( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
143 {
144     BOOL retv = TRUE;
145
146     static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
147                                  "THREAD_ATTACH", "THREAD_DETACH" };
148     assert( wm );
149
150     /* Skip calls for modules loaded with special load flags */
151
152     if (wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) return TRUE;
153
154     TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
155
156     /* Call the initialization routine */
157     retv = PE_InitDLL( wm->module, type, lpReserved );
158
159     /* The state of the module list may have changed due to the call
160        to PE_InitDLL. We cannot assume that this module has not been
161        deleted.  */
162     TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
163
164     return retv;
165 }
166
167 /*************************************************************************
168  *              MODULE_DllProcessAttach
169  *
170  * Send the process attach notification to all DLLs the given module
171  * depends on (recursively). This is somewhat complicated due to the fact that
172  *
173  * - we have to respect the module dependencies, i.e. modules implicitly
174  *   referenced by another module have to be initialized before the module
175  *   itself can be initialized
176  *
177  * - the initialization routine of a DLL can itself call LoadLibrary,
178  *   thereby introducing a whole new set of dependencies (even involving
179  *   the 'old' modules) at any time during the whole process
180  *
181  * (Note that this routine can be recursively entered not only directly
182  *  from itself, but also via LoadLibrary from one of the called initialization
183  *  routines.)
184  *
185  * Furthermore, we need to rearrange the main WINE_MODREF list to allow
186  * the process *detach* notifications to be sent in the correct order.
187  * This must not only take into account module dependencies, but also
188  * 'hidden' dependencies created by modules calling LoadLibrary in their
189  * attach notification routine.
190  *
191  * The strategy is rather simple: we move a WINE_MODREF to the head of the
192  * list after the attach notification has returned.  This implies that the
193  * detach notifications are called in the reverse of the sequence the attach
194  * notifications *returned*.
195  */
196 BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
197 {
198     BOOL retv = TRUE;
199     int i;
200
201     RtlEnterCriticalSection( &loader_section );
202
203     if (!wm)
204     {
205         wm = exe_modref;
206         PE_InitTls();
207     }
208     assert( wm );
209
210     /* prevent infinite recursion in case of cyclical dependencies */
211     if (    ( wm->flags & WINE_MODREF_MARKER )
212          || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
213         goto done;
214
215     TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
216
217     /* Tag current MODREF to prevent recursive loop */
218     wm->flags |= WINE_MODREF_MARKER;
219
220     /* Recursively attach all DLLs this one depends on */
221     for ( i = 0; retv && i < wm->nDeps; i++ )
222         if ( wm->deps[i] )
223             retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
224
225     /* Call DLL entry point */
226     if ( retv )
227     {
228         retv = MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved );
229         if ( retv )
230             wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
231     }
232
233     /* Re-insert MODREF at head of list */
234     if ( retv && wm->prev )
235     {
236         wm->prev->next = wm->next;
237         if ( wm->next ) wm->next->prev = wm->prev;
238
239         wm->prev = NULL;
240         wm->next = MODULE_modref_list;
241         MODULE_modref_list = wm->next->prev = wm;
242     }
243
244     /* Remove recursion flag */
245     wm->flags &= ~WINE_MODREF_MARKER;
246
247     TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
248
249  done:
250     RtlLeaveCriticalSection( &loader_section );
251     return retv;
252 }
253
254 /*************************************************************************
255  *              MODULE_DllProcessDetach
256  *
257  * Send DLL process detach notifications.  See the comment about calling
258  * sequence at MODULE_DllProcessAttach.  Unless the bForceDetach flag
259  * is set, only DLLs with zero refcount are notified.
260  */
261 void MODULE_DllProcessDetach( BOOL bForceDetach, LPVOID lpReserved )
262 {
263     WINE_MODREF *wm;
264
265     RtlEnterCriticalSection( &loader_section );
266     if (bForceDetach) process_detaching = 1;
267     do
268     {
269         for ( wm = MODULE_modref_list; wm; wm = wm->next )
270         {
271             /* Check whether to detach this DLL */
272             if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
273                 continue;
274             if ( wm->refCount > 0 && !bForceDetach )
275                 continue;
276
277             /* Call detach notification */
278             wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
279             MODULE_InitDLL( wm, DLL_PROCESS_DETACH, lpReserved );
280
281             /* Restart at head of WINE_MODREF list, as entries might have
282                been added and/or removed while performing the call ... */
283             break;
284         }
285     } while ( wm );
286
287     RtlLeaveCriticalSection( &loader_section );
288 }
289
290 /*************************************************************************
291  *              MODULE_DllThreadAttach
292  *
293  * Send DLL thread attach notifications. These are sent in the
294  * reverse sequence of process detach notification.
295  *
296  */
297 void MODULE_DllThreadAttach( LPVOID lpReserved )
298 {
299     WINE_MODREF *wm;
300
301     /* don't do any attach calls if process is exiting */
302     if (process_detaching) return;
303     /* FIXME: there is still a race here */
304
305     RtlEnterCriticalSection( &loader_section );
306
307     PE_InitTls();
308
309     for ( wm = MODULE_modref_list; wm; wm = wm->next )
310         if ( !wm->next )
311             break;
312
313     for ( ; wm; wm = wm->prev )
314     {
315         if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
316             continue;
317         if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
318             continue;
319
320         MODULE_InitDLL( wm, DLL_THREAD_ATTACH, lpReserved );
321     }
322
323     RtlLeaveCriticalSection( &loader_section );
324 }
325
326 /*************************************************************************
327  *              MODULE_DllThreadDetach
328  *
329  * Send DLL thread detach notifications. These are sent in the
330  * same sequence as process detach notification.
331  *
332  */
333 void MODULE_DllThreadDetach( LPVOID lpReserved )
334 {
335     WINE_MODREF *wm;
336
337     /* don't do any detach calls if process is exiting */
338     if (process_detaching) return;
339     /* FIXME: there is still a race here */
340
341     RtlEnterCriticalSection( &loader_section );
342
343     for ( wm = MODULE_modref_list; wm; wm = wm->next )
344     {
345         if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
346             continue;
347         if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
348             continue;
349
350         MODULE_InitDLL( wm, DLL_THREAD_DETACH, lpReserved );
351     }
352
353     RtlLeaveCriticalSection( &loader_section );
354 }
355
356 /****************************************************************************
357  *              DisableThreadLibraryCalls (KERNEL32.@)
358  *
359  * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
360  */
361 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
362 {
363     WINE_MODREF *wm;
364     BOOL retval = TRUE;
365
366     RtlEnterCriticalSection( &loader_section );
367
368     wm = MODULE32_LookupHMODULE( hModule );
369     if ( !wm )
370         retval = FALSE;
371     else
372         wm->flags |= WINE_MODREF_NO_DLL_CALLS;
373
374     RtlLeaveCriticalSection( &loader_section );
375
376     return retval;
377 }
378
379
380 /***********************************************************************
381  *           MODULE_CreateDummyModule
382  *
383  * Create a dummy NE module for Win32 or Winelib.
384  */
385 HMODULE16 MODULE_CreateDummyModule( LPCSTR filename, HMODULE module32 )
386 {
387     HMODULE16 hModule;
388     NE_MODULE *pModule;
389     SEGTABLEENTRY *pSegment;
390     char *pStr,*s;
391     unsigned int len;
392     const char* basename;
393     OFSTRUCT *ofs;
394     int of_size, size;
395
396     /* Extract base filename */
397     basename = strrchr(filename, '\\');
398     if (!basename) basename = filename;
399     else basename++;
400     len = strlen(basename);
401     if ((s = strchr(basename, '.'))) len = s - basename;
402
403     /* Allocate module */
404     of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
405                     + strlen(filename) + 1;
406     size = sizeof(NE_MODULE) +
407                  /* loaded file info */
408                  ((of_size + 3) & ~3) +
409                  /* segment table: DS,CS */
410                  2 * sizeof(SEGTABLEENTRY) +
411                  /* name table */
412                  len + 2 +
413                  /* several empty tables */
414                  8;
415
416     hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
417     if (!hModule) return (HMODULE16)11;  /* invalid exe */
418
419     FarSetOwner16( hModule, hModule );
420     pModule = (NE_MODULE *)GlobalLock16( hModule );
421
422     /* Set all used entries */
423     pModule->magic            = IMAGE_OS2_SIGNATURE;
424     pModule->count            = 1;
425     pModule->next             = 0;
426     pModule->flags            = 0;
427     pModule->dgroup           = 0;
428     pModule->ss               = 1;
429     pModule->cs               = 2;
430     pModule->heap_size        = 0;
431     pModule->stack_size       = 0;
432     pModule->seg_count        = 2;
433     pModule->modref_count     = 0;
434     pModule->nrname_size      = 0;
435     pModule->fileinfo         = sizeof(NE_MODULE);
436     pModule->os_flags         = NE_OSFLAGS_WINDOWS;
437     pModule->self             = hModule;
438     pModule->module32         = module32;
439
440     /* Set version and flags */
441     if (module32)
442     {
443         IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
444         pModule->expected_version = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
445                                      (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
446         pModule->flags |= NE_FFLAGS_WIN32;
447         if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
448             pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
449     }
450
451     /* Set loaded file information */
452     ofs = (OFSTRUCT *)(pModule + 1);
453     memset( ofs, 0, of_size );
454     ofs->cBytes = of_size < 256 ? of_size : 255;   /* FIXME */
455     strcpy( ofs->szPathName, filename );
456
457     pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
458     pModule->seg_table = (int)pSegment - (int)pModule;
459     /* Data segment */
460     pSegment->size    = 0;
461     pSegment->flags   = NE_SEGFLAGS_DATA;
462     pSegment->minsize = 0x1000;
463     pSegment++;
464     /* Code segment */
465     pSegment->flags   = 0;
466     pSegment++;
467
468     /* Module name */
469     pStr = (char *)pSegment;
470     pModule->name_table = (int)pStr - (int)pModule;
471     assert(len<256);
472     *pStr = len;
473     lstrcpynA( pStr+1, basename, len+1 );
474     pStr += len+2;
475
476     /* All tables zero terminated */
477     pModule->res_table = pModule->import_table = pModule->entry_table =
478                 (int)pStr - (int)pModule;
479
480     NE_RegisterModule( pModule );
481     return hModule;
482 }
483
484
485 /**********************************************************************
486  *          MODULE_FindModule
487  *
488  * Find a (loaded) win32 module depending on path
489  *
490  * RETURNS
491  *      the module handle if found
492  *      0 if not
493  */
494 WINE_MODREF *MODULE_FindModule(
495         LPCSTR path     /* [in] pathname of module/library to be found */
496 ) {
497     WINE_MODREF *wm;
498     char dllname[260], *p;
499
500     /* Append .DLL to name if no extension present */
501     strcpy( dllname, path );
502     if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
503             strcat( dllname, ".DLL" );
504
505     for ( wm = MODULE_modref_list; wm; wm = wm->next )
506     {
507         if ( !FILE_strcasecmp( dllname, wm->modname ) )
508             break;
509         if ( !FILE_strcasecmp( dllname, wm->filename ) )
510             break;
511         if ( !FILE_strcasecmp( dllname, wm->short_modname ) )
512             break;
513         if ( !FILE_strcasecmp( dllname, wm->short_filename ) )
514             break;
515     }
516
517     return wm;
518 }
519
520
521 /* Check whether a file is an OS/2 or a very old Windows executable
522  * by testing on import of KERNEL.
523  *
524  * FIXME: is reading the module imports the only way of discerning
525  *        old Windows binaries from OS/2 ones ? At least it seems so...
526  */
527 static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
528                                                  const IMAGE_OS2_HEADER *ne)
529 {
530     DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
531     enum binary_type ret = BINARY_OS216;
532     LPWORD modtab = NULL;
533     LPSTR nametab = NULL;
534     DWORD len;
535     int i;
536
537     /* read modref table */
538     if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
539       || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
540       || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
541       || (len != ne->ne_cmod*sizeof(WORD)) )
542         goto broken;
543
544     /* read imported names table */
545     if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
546       || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
547       || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
548       || (len != ne->ne_enttab - ne->ne_imptab) )
549         goto broken;
550
551     for (i=0; i < ne->ne_cmod; i++)
552     {
553         LPSTR module = &nametab[modtab[i]];
554         TRACE("modref: %.*s\n", module[0], &module[1]);
555         if (!(strncmp(&module[1], "KERNEL", module[0])))
556         { /* very old Windows file */
557             MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
558             ret = BINARY_WIN16;
559             goto good;
560         }
561     }
562
563 broken:
564     ERR("Hmm, an error occurred. Is this binary file broken ?\n");
565
566 good:
567     HeapFree( GetProcessHeap(), 0, modtab);
568     HeapFree( GetProcessHeap(), 0, nametab);
569     SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
570     return ret;
571 }
572
573 /***********************************************************************
574  *           MODULE_GetBinaryType
575  */
576 enum binary_type MODULE_GetBinaryType( HANDLE hfile )
577 {
578     union
579     {
580         struct
581         {
582             unsigned char magic[4];
583             unsigned char ignored[12];
584             unsigned short type;
585         } elf;
586         IMAGE_DOS_HEADER mz;
587     } header;
588
589     char magic[4];
590     DWORD len;
591
592     /* Seek to the start of the file and read the header information. */
593     if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
594         return BINARY_UNKNOWN;
595     if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
596         return BINARY_UNKNOWN;
597
598     if (!memcmp( header.elf.magic, "\177ELF", 4 ))
599     {
600         /* FIXME: we don't bother to check byte order, architecture, etc. */
601         switch(header.elf.type)
602         {
603         case 2: return BINARY_UNIX_EXE;
604         case 3: return BINARY_UNIX_LIB;
605         }
606         return BINARY_UNKNOWN;
607     }
608
609     /* Not ELF, try DOS */
610
611     if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
612     {
613         /* We do have a DOS image so we will now try to seek into
614          * the file by the amount indicated by the field
615          * "Offset to extended header" and read in the
616          * "magic" field information at that location.
617          * This will tell us if there is more header information
618          * to read or not.
619          */
620         /* But before we do we will make sure that header
621          * structure encompasses the "Offset to extended header"
622          * field.
623          */
624         if ((header.mz.e_cparhdr << 4) < sizeof(IMAGE_DOS_HEADER))
625             return BINARY_DOS;
626         if (header.mz.e_crlc && (header.mz.e_lfarlc < sizeof(IMAGE_DOS_HEADER)))
627             return BINARY_DOS;
628         if (header.mz.e_lfanew < sizeof(IMAGE_DOS_HEADER))
629             return BINARY_DOS;
630         if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
631             return BINARY_DOS;
632         if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic))
633             return BINARY_DOS;
634
635         /* Reading the magic field succeeded so
636          * we will try to determine what type it is.
637          */
638         if (!memcmp( magic, "PE\0\0", 4 ))
639         {
640             IMAGE_FILE_HEADER FileHeader;
641
642             if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader))
643             {
644                 if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
645                 return BINARY_PE_EXE;
646             }
647             return BINARY_DOS;
648         }
649
650         if (!memcmp( magic, "NE", 2 ))
651         {
652             /* This is a Windows executable (NE) header.  This can
653              * mean either a 16-bit OS/2 or a 16-bit Windows or even a
654              * DOS program (running under a DOS extender).  To decide
655              * which, we'll have to read the NE header.
656              */
657             IMAGE_OS2_HEADER ne;
658             if (    SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1
659                     && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
660                     && len == sizeof(ne) )
661             {
662                 switch ( ne.ne_exetyp )
663                 {
664                 case 2:  return BINARY_WIN16;
665                 case 5:  return BINARY_DOS;
666                 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne);
667                 }
668             }
669             /* Couldn't read header, so abort. */
670             return BINARY_DOS;
671         }
672
673         /* Unknown extended header, but this file is nonetheless DOS-executable. */
674         return BINARY_DOS;
675     }
676
677     return BINARY_UNKNOWN;
678 }
679
680 /***********************************************************************
681  *             GetBinaryTypeA                     [KERNEL32.@]
682  *             GetBinaryType                      [KERNEL32.@]
683  *
684  * The GetBinaryType function determines whether a file is executable
685  * or not and if it is it returns what type of executable it is.
686  * The type of executable is a property that determines in which
687  * subsystem an executable file runs under.
688  *
689  * Binary types returned:
690  * SCS_32BIT_BINARY: A Win32 based application
691  * SCS_DOS_BINARY: An MS-Dos based application
692  * SCS_WOW_BINARY: A Win16 based application
693  * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
694  * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
695  * SCS_OS216_BINARY: A 16bit OS/2 based application
696  *
697  * Returns TRUE if the file is an executable in which case
698  * the value pointed by lpBinaryType is set.
699  * Returns FALSE if the file is not an executable or if the function fails.
700  *
701  * To do so it opens the file and reads in the header information
702  * if the extended header information is not present it will
703  * assume that the file is a DOS executable.
704  * If the extended header information is present it will
705  * determine if the file is a 16 or 32 bit Windows executable
706  * by check the flags in the header.
707  *
708  * Note that .COM and .PIF files are only recognized by their
709  * file name extension; but Windows does it the same way ...
710  */
711 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
712 {
713     BOOL ret = FALSE;
714     HANDLE hfile;
715     char *ptr;
716
717     TRACE_(win32)("%s\n", lpApplicationName );
718
719     /* Sanity check.
720      */
721     if ( lpApplicationName == NULL || lpBinaryType == NULL )
722         return FALSE;
723
724     /* Open the file indicated by lpApplicationName for reading.
725      */
726     hfile = CreateFileA( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
727                          NULL, OPEN_EXISTING, 0, 0 );
728     if ( hfile == INVALID_HANDLE_VALUE )
729         return FALSE;
730
731     /* Check binary type
732      */
733     switch(MODULE_GetBinaryType( hfile ))
734     {
735     case BINARY_UNKNOWN:
736         /* try to determine from file name */
737         ptr = strrchr( lpApplicationName, '.' );
738         if (!ptr) break;
739         if (!FILE_strcasecmp( ptr, ".COM" ))
740         {
741             *lpBinaryType = SCS_DOS_BINARY;
742             ret = TRUE;
743         }
744         else if (!FILE_strcasecmp( ptr, ".PIF" ))
745         {
746             *lpBinaryType = SCS_PIF_BINARY;
747             ret = TRUE;
748         }
749         break;
750     case BINARY_PE_EXE:
751     case BINARY_PE_DLL:
752         *lpBinaryType = SCS_32BIT_BINARY;
753         ret = TRUE;
754         break;
755     case BINARY_WIN16:
756         *lpBinaryType = SCS_WOW_BINARY;
757         ret = TRUE;
758         break;
759     case BINARY_OS216:
760         *lpBinaryType = SCS_OS216_BINARY;
761         ret = TRUE;
762         break;
763     case BINARY_DOS:
764         *lpBinaryType = SCS_DOS_BINARY;
765         ret = TRUE;
766         break;
767     case BINARY_UNIX_EXE:
768     case BINARY_UNIX_LIB:
769         ret = FALSE;
770         break;
771     }
772
773     CloseHandle( hfile );
774     return ret;
775 }
776
777 /***********************************************************************
778  *             GetBinaryTypeW                      [KERNEL32.@]
779  */
780 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
781 {
782     BOOL ret = FALSE;
783     LPSTR strNew = NULL;
784
785     TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) );
786
787     /* Sanity check.
788      */
789     if ( lpApplicationName == NULL || lpBinaryType == NULL )
790         return FALSE;
791
792     /* Convert the wide string to a ascii string.
793      */
794     strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
795
796     if ( strNew != NULL )
797     {
798         ret = GetBinaryTypeA( strNew, lpBinaryType );
799
800         /* Free the allocated string.
801          */
802         HeapFree( GetProcessHeap(), 0, strNew );
803     }
804
805     return ret;
806 }
807
808
809 /***********************************************************************
810  *           WinExec     (KERNEL.166)
811  *           WinExec16   (KERNEL32.@)
812  */
813 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
814 {
815     LPCSTR p, args = NULL;
816     LPCSTR name_beg, name_end;
817     LPSTR name, cmdline;
818     int arglen;
819     HINSTANCE16 ret;
820     char buffer[MAX_PATH];
821
822     if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
823     {
824       name_beg = lpCmdLine+1;
825       p = strchr ( lpCmdLine+1, '"' );
826       if (p)
827       {
828           name_end = p;
829           args = strchr ( p, ' ' );
830       }
831       else /* yes, even valid with trailing '"' missing */
832           name_end = lpCmdLine+strlen(lpCmdLine);
833     }
834     else
835     {
836       name_beg = lpCmdLine;
837       args = strchr( lpCmdLine, ' ' );
838       name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
839     }
840
841     if ((name_beg == lpCmdLine) && (!args))
842     { /* just use the original cmdline string as file name */
843         name = (LPSTR)lpCmdLine;
844     }
845     else
846     {
847         if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
848             return ERROR_NOT_ENOUGH_MEMORY;
849         memcpy( name, name_beg, name_end - name_beg );
850         name[name_end - name_beg] = '\0';
851     }
852
853     if (args)
854     {
855         args++;
856         arglen = strlen(args);
857         cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
858         cmdline[0] = (BYTE)arglen;
859         strcpy( cmdline + 1, args );
860     }
861     else
862     {
863         cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
864         cmdline[0] = cmdline[1] = 0;
865     }
866
867     TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
868
869     if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
870     {
871         LOADPARAMS16 params;
872         WORD showCmd[2];
873         showCmd[0] = 2;
874         showCmd[1] = nCmdShow;
875
876         params.hEnvironment = 0;
877         params.cmdLine = MapLS( cmdline );
878         params.showCmd = MapLS( showCmd );
879         params.reserved = 0;
880
881         ret = LoadModule16( buffer, &params );
882         UnMapLS( params.cmdLine );
883         UnMapLS( params.showCmd );
884     }
885     else ret = GetLastError();
886
887     HeapFree( GetProcessHeap(), 0, cmdline );
888     if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
889
890     if (ret == 21)  /* 32-bit module */
891     {
892         DWORD count;
893         ReleaseThunkLock( &count );
894         ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) );
895         RestoreThunkLock( count );
896     }
897     return ret;
898 }
899
900 /***********************************************************************
901  *           WinExec   (KERNEL32.@)
902  */
903 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
904 {
905     PROCESS_INFORMATION info;
906     STARTUPINFOA startup;
907     char *cmdline;
908     UINT ret;
909
910     memset( &startup, 0, sizeof(startup) );
911     startup.cb = sizeof(startup);
912     startup.dwFlags = STARTF_USESHOWWINDOW;
913     startup.wShowWindow = nCmdShow;
914
915     /* cmdline needs to be writeable for CreateProcess */
916     if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
917     strcpy( cmdline, lpCmdLine );
918
919     if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
920                         0, NULL, NULL, &startup, &info ))
921     {
922         /* Give 30 seconds to the app to come up */
923         if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF)
924             WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
925         ret = 33;
926         /* Close off the handles */
927         CloseHandle( info.hThread );
928         CloseHandle( info.hProcess );
929     }
930     else if ((ret = GetLastError()) >= 32)
931     {
932         FIXME("Strange error set by CreateProcess: %d\n", ret );
933         ret = 11;
934     }
935     HeapFree( GetProcessHeap(), 0, cmdline );
936     return ret;
937 }
938
939 /**********************************************************************
940  *          LoadModule    (KERNEL32.@)
941  */
942 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
943 {
944     LOADPARAMS *params = (LOADPARAMS *)paramBlock;
945     PROCESS_INFORMATION info;
946     STARTUPINFOA startup;
947     HINSTANCE hInstance;
948     LPSTR cmdline, p;
949     char filename[MAX_PATH];
950     BYTE len;
951
952     if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
953
954     if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
955         !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
956         return (HINSTANCE)GetLastError();
957
958     len = (BYTE)params->lpCmdLine[0];
959     if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
960         return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
961
962     strcpy( cmdline, filename );
963     p = cmdline + strlen(cmdline);
964     *p++ = ' ';
965     memcpy( p, params->lpCmdLine + 1, len );
966     p[len] = 0;
967
968     memset( &startup, 0, sizeof(startup) );
969     startup.cb = sizeof(startup);
970     if (params->lpCmdShow)
971     {
972         startup.dwFlags = STARTF_USESHOWWINDOW;
973         startup.wShowWindow = params->lpCmdShow[1];
974     }
975
976     if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
977                         params->lpEnvAddress, NULL, &startup, &info ))
978     {
979         /* Give 30 seconds to the app to come up */
980         if (wait_input_idle( info.hProcess, 30000 ) ==  0xFFFFFFFF )
981             WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
982         hInstance = (HINSTANCE)33;
983         /* Close off the handles */
984         CloseHandle( info.hThread );
985         CloseHandle( info.hProcess );
986     }
987     else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
988     {
989         FIXME("Strange error set by CreateProcess: %d\n", hInstance );
990         hInstance = (HINSTANCE)11;
991     }
992
993     HeapFree( GetProcessHeap(), 0, cmdline );
994     return hInstance;
995 }
996
997
998 /***********************************************************************
999  *              GetModuleHandleA         (KERNEL32.@)
1000  *              GetModuleHandle32        (KERNEL.488)
1001  */
1002 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
1003 {
1004     WINE_MODREF *wm;
1005
1006     if ( module == NULL )
1007         wm = exe_modref;
1008     else
1009         wm = MODULE_FindModule( module );
1010
1011     return wm? wm->module : 0;
1012 }
1013
1014 /***********************************************************************
1015  *              GetModuleHandleW (KERNEL32.@)
1016  */
1017 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
1018 {
1019     HMODULE hModule;
1020     LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
1021     hModule = GetModuleHandleA( modulea );
1022     HeapFree( GetProcessHeap(), 0, modulea );
1023     return hModule;
1024 }
1025
1026
1027 /***********************************************************************
1028  *              GetModuleFileNameA      (KERNEL32.@)
1029  *              GetModuleFileName32     (KERNEL.487)
1030  *
1031  * GetModuleFileNameA seems to *always* return the long path;
1032  * it's only GetModuleFileName16 that decides between short/long path
1033  * by checking if exe version >= 4.0.
1034  * (SDK docu doesn't mention this)
1035  */
1036 DWORD WINAPI GetModuleFileNameA(
1037         HMODULE hModule,        /* [in] module handle (32bit) */
1038         LPSTR lpFileName,       /* [out] filenamebuffer */
1039         DWORD size )            /* [in] size of filenamebuffer */
1040 {
1041     RtlEnterCriticalSection( &loader_section );
1042
1043     lpFileName[0] = 0;
1044     if (!hModule && !(NtCurrentTeb()->tibflags & TEBF_WIN32))
1045     {
1046         /* 16-bit task - get current NE module name */
1047         NE_MODULE *pModule = NE_GetPtr( GetCurrentTask() );
1048         if (pModule) GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, size);
1049     }
1050     else
1051     {
1052         WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1053         if (wm) lstrcpynA( lpFileName, wm->filename, size );
1054     }
1055
1056     RtlLeaveCriticalSection( &loader_section );
1057     TRACE("%s\n", lpFileName );
1058     return strlen(lpFileName);
1059 }
1060
1061
1062 /***********************************************************************
1063  *              GetModuleFileNameW      (KERNEL32.@)
1064  */
1065 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
1066 {
1067     LPSTR fnA = HeapAlloc( GetProcessHeap(), 0, size * 2 );
1068     if (!fnA) return 0;
1069     GetModuleFileNameA( hModule, fnA, size * 2 );
1070     if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, fnA, -1, lpFileName, size ))
1071         lpFileName[size-1] = 0;
1072     HeapFree( GetProcessHeap(), 0, fnA );
1073     return strlenW(lpFileName);
1074 }
1075
1076
1077 /***********************************************************************
1078  *           LoadLibraryExA   (KERNEL32.@)
1079  */
1080 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
1081 {
1082         WINE_MODREF *wm;
1083
1084         if(!libname)
1085         {
1086                 SetLastError(ERROR_INVALID_PARAMETER);
1087                 return 0;
1088         }
1089
1090         if (flags & LOAD_LIBRARY_AS_DATAFILE)
1091         {
1092             char filename[256];
1093             HMODULE hmod = 0;
1094
1095             /* This method allows searching for the 'native' libraries only */
1096             if (SearchPathA( NULL, libname, ".dll", sizeof(filename), filename, NULL ))
1097             {
1098                 /* FIXME: maybe we should use the hfile parameter instead */
1099                 HANDLE hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
1100                                             NULL, OPEN_EXISTING, 0, 0 );
1101                 if (hFile != INVALID_HANDLE_VALUE)
1102                 {
1103                     HANDLE mapping;
1104                     switch (MODULE_GetBinaryType( hFile ))
1105                     {
1106                     case BINARY_PE_EXE:
1107                     case BINARY_PE_DLL:
1108                         mapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
1109                         if (mapping)
1110                         {
1111                             hmod = (HMODULE)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
1112                             CloseHandle( mapping );
1113                         }
1114                         break;
1115                     default:
1116                         break;
1117                     }
1118                     CloseHandle( hFile );
1119                 }
1120                 if (hmod) return (HMODULE)((ULONG_PTR)hmod + 1);
1121             }
1122             flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
1123             /* Fallback to normal behaviour */
1124         }
1125
1126         RtlEnterCriticalSection( &loader_section );
1127
1128         wm = MODULE_LoadLibraryExA( libname, hfile, flags );
1129         if ( wm )
1130         {
1131                 if ( !MODULE_DllProcessAttach( wm, NULL ) )
1132                 {
1133                         WARN_(module)("Attach failed for module '%s'.\n", libname);
1134                         MODULE_FreeLibrary(wm);
1135                         SetLastError(ERROR_DLL_INIT_FAILED);
1136                         wm = NULL;
1137                 }
1138         }
1139
1140         RtlLeaveCriticalSection( &loader_section );
1141         return wm ? wm->module : 0;
1142 }
1143
1144 /***********************************************************************
1145  *      allocate_lib_dir
1146  *
1147  * helper for MODULE_LoadLibraryExA.  Allocate space to hold the directory
1148  * portion of the provided name and put the name in it.
1149  *
1150  */
1151 static LPCSTR allocate_lib_dir(LPCSTR libname)
1152 {
1153     LPCSTR p, pmax;
1154     LPSTR result;
1155     int length;
1156
1157     pmax = libname;
1158     if ((p = strrchr( pmax, '\\' ))) pmax = p + 1;
1159     if ((p = strrchr( pmax, '/' ))) pmax = p + 1; /* Naughty.  MSDN says don't */
1160     if (pmax == libname && pmax[0] && pmax[1] == ':') pmax += 2;
1161
1162     length = pmax - libname;
1163
1164     result = HeapAlloc (GetProcessHeap(), 0, length+1);
1165
1166     if (result)
1167     {
1168         strncpy (result, libname, length);
1169         result [length] = '\0';
1170     }
1171
1172     return result;
1173 }
1174
1175 /***********************************************************************
1176  *      MODULE_LoadLibraryExA   (internal)
1177  *
1178  * Load a PE style module according to the load order.
1179  *
1180  * The HFILE parameter is not used and marked reserved in the SDK. I can
1181  * only guess that it should force a file to be mapped, but I rather
1182  * ignore the parameter because it would be extremely difficult to
1183  * integrate this with different types of module representations.
1184  *
1185  * libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion
1186  *        on this function.  When first called from LoadLibraryExA it will be
1187  *        NULL but thereafter it may point to a buffer containing the path
1188  *        portion of the library name.  Note that the recursion all occurs
1189  *        within a Critical section (see LoadLibraryExA) so the use of a
1190  *        static is acceptable.
1191  *        (We have to use a static variable at some point anyway, to pass the
1192  *        information from BUILTIN32_dlopen through dlopen and the builtin's
1193  *        init function into load_library).
1194  * allocated_libdir is TRUE in the stack frame that allocated libdir
1195  */
1196 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HANDLE hfile, DWORD flags )
1197 {
1198         DWORD err = GetLastError();
1199         WINE_MODREF *pwm;
1200         int i;
1201         enum loadorder_type loadorder[LOADORDER_NTYPES];
1202         LPSTR filename;
1203         const char *filetype = "";
1204         DWORD found;
1205         BOOL allocated_libdir = FALSE;
1206         static LPCSTR libdir = NULL; /* See above */
1207
1208         if ( !libname ) return NULL;
1209
1210         filename = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1211         if ( !filename ) return NULL;
1212         *filename = 0; /* Just in case we don't set it before goto error */
1213
1214         RtlEnterCriticalSection( &loader_section );
1215
1216         if ((flags & LOAD_WITH_ALTERED_SEARCH_PATH) && FILE_contains_path(libname))
1217         {
1218             if (!(libdir = allocate_lib_dir(libname))) goto error;
1219             allocated_libdir = TRUE;
1220         }
1221
1222         if (!libdir || allocated_libdir)
1223             found = SearchPathA(NULL, libname, ".dll", MAX_PATH, filename, NULL);
1224         else
1225             found = DIR_SearchAlternatePath(libdir, libname, ".dll", MAX_PATH, filename, NULL);
1226
1227         /* build the modules filename */
1228         if (!found)
1229         {
1230             if (!MODULE_GetBuiltinPath( libname, ".dll", filename, MAX_PATH )) goto error;
1231         }
1232
1233         /* Check for already loaded module */
1234         if (!(pwm = MODULE_FindModule(filename)) && !FILE_contains_path(libname))
1235         {
1236             LPSTR       fn = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1237             if (fn)
1238             {
1239                 /* since the default loading mechanism uses a more detailed algorithm
1240                  * than SearchPath (like using PATH, which can even be modified between
1241                  * two attempts of loading the same DLL), the look-up above (with
1242                  * SearchPath) can have put the file in system directory, whereas it
1243                  * has already been loaded but with a different path. So do a specific
1244                  * look-up with filename (without any path)
1245                  */
1246                 strcpy ( fn, libname );
1247                 /* if the filename doesn't have an extension append .DLL */
1248                 if (!strrchr( fn, '.')) strcat( fn, ".dll" );
1249                 if ((pwm = MODULE_FindModule( fn )) != NULL)
1250                    strcpy( filename, fn );
1251                 HeapFree( GetProcessHeap(), 0, fn );
1252             }
1253         }
1254         if (pwm)
1255         {
1256                 if(!(pwm->flags & WINE_MODREF_MARKER))
1257                         pwm->refCount++;
1258
1259                 if ((pwm->flags & WINE_MODREF_DONT_RESOLVE_REFS) &&
1260                     !(flags & DONT_RESOLVE_DLL_REFERENCES))
1261                 {
1262                     pwm->flags &= ~WINE_MODREF_DONT_RESOLVE_REFS;
1263                     PE_fixup_imports( pwm );
1264                 }
1265                 TRACE("Already loaded module '%s' at 0x%08x, count=%d\n", filename, pwm->module, pwm->refCount);
1266                 if (allocated_libdir)
1267                 {
1268                     HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1269                     libdir = NULL;
1270                 }
1271                 RtlLeaveCriticalSection( &loader_section );
1272                 HeapFree ( GetProcessHeap(), 0, filename );
1273                 return pwm;
1274         }
1275
1276         MODULE_GetLoadOrder( loadorder, filename, TRUE);
1277
1278         for(i = 0; i < LOADORDER_NTYPES; i++)
1279         {
1280                 if (loadorder[i] == LOADORDER_INVALID) break;
1281                 SetLastError( ERROR_FILE_NOT_FOUND );
1282
1283                 switch(loadorder[i])
1284                 {
1285                 case LOADORDER_DLL:
1286                         TRACE("Trying native dll '%s'\n", filename);
1287                         pwm = PE_LoadLibraryExA(filename, flags);
1288                         filetype = "native";
1289                         break;
1290
1291                 case LOADORDER_SO:
1292                         TRACE("Trying so-library '%s'\n", filename);
1293                         pwm = ELF_LoadLibraryExA(filename, flags);
1294                         filetype = "so";
1295                         break;
1296
1297                 case LOADORDER_BI:
1298                         TRACE("Trying built-in '%s'\n", filename);
1299                         pwm = BUILTIN32_LoadLibraryExA(filename, flags);
1300                         filetype = "builtin";
1301                         break;
1302
1303                 default:
1304                         pwm = NULL;
1305                         break;
1306                 }
1307
1308                 if(pwm)
1309                 {
1310                         /* Initialize DLL just loaded */
1311                         TRACE("Loaded module '%s' at 0x%08x\n", filename, pwm->module);
1312                         if (!TRACE_ON(module))
1313                             TRACE_(loaddll)("Loaded module '%s' : %s\n", filename, filetype);
1314                         /* Set the refCount here so that an attach failure will */
1315                         /* decrement the dependencies through the MODULE_FreeLibrary call. */
1316                         pwm->refCount++;
1317
1318                         if (allocated_libdir)
1319                         {
1320                             HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1321                             libdir = NULL;
1322                         }
1323                         RtlLeaveCriticalSection( &loader_section );
1324                         SetLastError( err );  /* restore last error */
1325                         HeapFree ( GetProcessHeap(), 0, filename );
1326                         return pwm;
1327                 }
1328
1329                 if(GetLastError() != ERROR_FILE_NOT_FOUND)
1330                 {
1331                     WARN("Loading of %s DLL %s failed (error %ld).\n",
1332                          filetype, filename, GetLastError());
1333                     break;
1334                 }
1335         }
1336
1337  error:
1338         if (allocated_libdir)
1339         {
1340             HeapFree ( GetProcessHeap(), 0, (LPSTR)libdir );
1341             libdir = NULL;
1342         }
1343         RtlLeaveCriticalSection( &loader_section );
1344         WARN("Failed to load module '%s'; error=%ld\n", filename, GetLastError());
1345         HeapFree ( GetProcessHeap(), 0, filename );
1346         return NULL;
1347 }
1348
1349 /***********************************************************************
1350  *           LoadLibraryA         (KERNEL32.@)
1351  */
1352 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
1353         return LoadLibraryExA(libname,0,0);
1354 }
1355
1356 /***********************************************************************
1357  *           LoadLibraryW         (KERNEL32.@)
1358  */
1359 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
1360 {
1361     return LoadLibraryExW(libnameW,0,0);
1362 }
1363
1364 /***********************************************************************
1365  *           LoadLibrary32        (KERNEL.452)
1366  *           LoadSystemLibrary32  (KERNEL.482)
1367  */
1368 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
1369 {
1370     HMODULE hModule;
1371     DWORD count;
1372
1373     ReleaseThunkLock( &count );
1374     hModule = LoadLibraryA( libname );
1375     RestoreThunkLock( count );
1376     return hModule;
1377 }
1378
1379 /***********************************************************************
1380  *           LoadLibraryExW       (KERNEL32.@)
1381  */
1382 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HANDLE hfile,DWORD flags)
1383 {
1384     LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
1385     HMODULE ret = LoadLibraryExA( libnameA , hfile, flags );
1386
1387     HeapFree( GetProcessHeap(), 0, libnameA );
1388     return ret;
1389 }
1390
1391 /***********************************************************************
1392  *           MODULE_FlushModrefs
1393  *
1394  * NOTE: Assumes that the process critical section is held!
1395  *
1396  * Remove all unused modrefs and call the internal unloading routines
1397  * for the library type.
1398  */
1399 static void MODULE_FlushModrefs(void)
1400 {
1401         WINE_MODREF *wm, *next;
1402
1403         for(wm = MODULE_modref_list; wm; wm = next)
1404         {
1405                 next = wm->next;
1406
1407                 if(wm->refCount)
1408                         continue;
1409
1410                 /* Unlink this modref from the chain */
1411                 if(wm->next)
1412                         wm->next->prev = wm->prev;
1413                 if(wm->prev)
1414                         wm->prev->next = wm->next;
1415                 if(wm == MODULE_modref_list)
1416                         MODULE_modref_list = wm->next;
1417
1418                 TRACE(" unloading %s\n", wm->filename);
1419                 if (!TRACE_ON(module))
1420                     TRACE_(loaddll)("Unloaded module '%s' : %s\n", wm->filename,
1421                                     wm->dlhandle ? "builtin" : "native" );
1422
1423                 if (wm->dlhandle) wine_dll_unload( wm->dlhandle );
1424                 else UnmapViewOfFile( (LPVOID)wm->module );
1425                 FreeLibrary16(wm->hDummyMod);
1426                 HeapFree( GetProcessHeap(), 0, wm->deps );
1427                 HeapFree( GetProcessHeap(), 0, wm );
1428         }
1429 }
1430
1431 /***********************************************************************
1432  *           FreeLibrary   (KERNEL32.@)
1433  *           FreeLibrary32 (KERNEL.486)
1434  */
1435 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
1436 {
1437     BOOL retv = FALSE;
1438     WINE_MODREF *wm;
1439
1440     if (!hLibModule)
1441     {
1442         SetLastError( ERROR_INVALID_HANDLE );
1443         return FALSE;
1444     }
1445
1446     if ((ULONG_PTR)hLibModule & 1)
1447     {
1448         /* this is a LOAD_LIBRARY_AS_DATAFILE module */
1449         char *ptr = (char *)hLibModule - 1;
1450         UnmapViewOfFile( ptr );
1451         return TRUE;
1452     }
1453
1454     RtlEnterCriticalSection( &loader_section );
1455     free_lib_count++;
1456
1457     if ((wm = MODULE32_LookupHMODULE( hLibModule ))) retv = MODULE_FreeLibrary( wm );
1458
1459     free_lib_count--;
1460     RtlLeaveCriticalSection( &loader_section );
1461
1462     return retv;
1463 }
1464
1465 /***********************************************************************
1466  *           MODULE_DecRefCount
1467  *
1468  * NOTE: Assumes that the process critical section is held!
1469  */
1470 static void MODULE_DecRefCount( WINE_MODREF *wm )
1471 {
1472     int i;
1473
1474     if ( wm->flags & WINE_MODREF_MARKER )
1475         return;
1476
1477     if ( wm->refCount <= 0 )
1478         return;
1479
1480     --wm->refCount;
1481     TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
1482
1483     if ( wm->refCount == 0 )
1484     {
1485         wm->flags |= WINE_MODREF_MARKER;
1486
1487         for ( i = 0; i < wm->nDeps; i++ )
1488             if ( wm->deps[i] )
1489                 MODULE_DecRefCount( wm->deps[i] );
1490
1491         wm->flags &= ~WINE_MODREF_MARKER;
1492     }
1493 }
1494
1495 /***********************************************************************
1496  *           MODULE_FreeLibrary
1497  *
1498  * NOTE: Assumes that the process critical section is held!
1499  */
1500 BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
1501 {
1502     TRACE("(%s) - START\n", wm->modname );
1503
1504     /* Recursively decrement reference counts */
1505     MODULE_DecRefCount( wm );
1506
1507     /* Call process detach notifications */
1508     if ( free_lib_count <= 1 )
1509     {
1510         MODULE_DllProcessDetach( FALSE, NULL );
1511         SERVER_START_REQ( unload_dll )
1512         {
1513             req->base = (void *)wm->module;
1514             wine_server_call( req );
1515         }
1516         SERVER_END_REQ;
1517         MODULE_FlushModrefs();
1518     }
1519
1520     TRACE("END\n");
1521
1522     return TRUE;
1523 }
1524
1525
1526 /***********************************************************************
1527  *           FreeLibraryAndExitThread (KERNEL32.@)
1528  */
1529 VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
1530 {
1531     FreeLibrary(hLibModule);
1532     ExitThread(dwExitCode);
1533 }
1534
1535 /***********************************************************************
1536  *           PrivateLoadLibrary       (KERNEL32.@)
1537  *
1538  * FIXME: rough guesswork, don't know what "Private" means
1539  */
1540 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
1541 {
1542     return LoadLibrary16(libname);
1543 }
1544
1545
1546
1547 /***********************************************************************
1548  *           PrivateFreeLibrary       (KERNEL32.@)
1549  *
1550  * FIXME: rough guesswork, don't know what "Private" means
1551  */
1552 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
1553 {
1554     FreeLibrary16(handle);
1555 }
1556
1557
1558 /***********************************************************************
1559  *           GetProcAddress16   (KERNEL32.37)
1560  * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1561  */
1562 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1563 {
1564     if (!hModule) {
1565         WARN("hModule may not be 0!\n");
1566         return (FARPROC16)0;
1567     }
1568     if (HIWORD(hModule))
1569     {
1570         WARN("hModule is Win32 handle (%08x)\n", hModule );
1571         return (FARPROC16)0;
1572     }
1573     return GetProcAddress16( LOWORD(hModule), name );
1574 }
1575
1576 /***********************************************************************
1577  *           GetProcAddress   (KERNEL.50)
1578  */
1579 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1580 {
1581     WORD ordinal;
1582     FARPROC16 ret;
1583
1584     if (!hModule) hModule = GetCurrentTask();
1585     hModule = GetExePtr( hModule );
1586
1587     if (HIWORD(name) != 0)
1588     {
1589         ordinal = NE_GetOrdinal( hModule, name );
1590         TRACE("%04x '%s'\n", hModule, name );
1591     }
1592     else
1593     {
1594         ordinal = LOWORD(name);
1595         TRACE("%04x %04x\n", hModule, ordinal );
1596     }
1597     if (!ordinal) return (FARPROC16)0;
1598
1599     ret = NE_GetEntryPoint( hModule, ordinal );
1600
1601     TRACE("returning %08x\n", (UINT)ret );
1602     return ret;
1603 }
1604
1605
1606 /***********************************************************************
1607  *           GetProcAddress             (KERNEL32.@)
1608  */
1609 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
1610 {
1611     return MODULE_GetProcAddress( hModule, function, -1, TRUE );
1612 }
1613
1614 /***********************************************************************
1615  *           GetProcAddress32                   (KERNEL.453)
1616  */
1617 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
1618 {
1619     return MODULE_GetProcAddress( hModule, function, -1, FALSE );
1620 }
1621
1622 /***********************************************************************
1623  *           MODULE_GetProcAddress              (internal)
1624  */
1625 FARPROC MODULE_GetProcAddress(
1626         HMODULE hModule,        /* [in] current module handle */
1627         LPCSTR function,        /* [in] function to be looked up */
1628         int hint,
1629         BOOL snoop )
1630 {
1631     WINE_MODREF *wm;
1632     FARPROC     retproc = 0;
1633
1634     if (HIWORD(function))
1635         TRACE_(win32)("(%08lx,%s (%d))\n",(DWORD)hModule,function,hint);
1636     else
1637         TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
1638
1639     RtlEnterCriticalSection( &loader_section );
1640     if ((wm = MODULE32_LookupHMODULE( hModule )))
1641     {
1642         retproc = wm->find_export( wm, function, hint, snoop );
1643         if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1644     }
1645     RtlLeaveCriticalSection( &loader_section );
1646     return retproc;
1647 }
1648
1649
1650 /***************************************************************************
1651  *              HasGPHandler                    (KERNEL.338)
1652  */
1653
1654 #include "pshpack1.h"
1655 typedef struct _GPHANDLERDEF
1656 {
1657     WORD selector;
1658     WORD rangeStart;
1659     WORD rangeEnd;
1660     WORD handler;
1661 } GPHANDLERDEF;
1662 #include "poppack.h"
1663
1664 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1665 {
1666     HMODULE16 hModule;
1667     int gpOrdinal;
1668     SEGPTR gpPtr;
1669     GPHANDLERDEF *gpHandler;
1670
1671     if (    (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1672          && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1673          && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1674          && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1675          && (gpHandler = MapSL( gpPtr )) != NULL )
1676     {
1677         while (gpHandler->selector)
1678         {
1679             if (    SELECTOROF(address) == gpHandler->selector
1680                  && OFFSETOF(address)   >= gpHandler->rangeStart
1681                  && OFFSETOF(address)   <  gpHandler->rangeEnd  )
1682                 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1683             gpHandler++;
1684         }
1685     }
1686
1687     return 0;
1688 }