4 * Copyright 1995 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
29 #include <sys/types.h>
33 #include "wine/winbase16.h"
39 #include "wine/debug.h"
40 #include "wine/server.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(module
);
43 WINE_DECLARE_DEBUG_CHANNEL(win32
);
44 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
46 WINE_MODREF
*MODULE_modref_list
= NULL
;
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 */
52 static CRITICAL_SECTION loader_section
= CRITICAL_SECTION_INIT( "loader_section" );
54 /***********************************************************************
57 * Wrapper to call WaitForInputIdle USER function
59 typedef DWORD (WINAPI
*WaitForInputIdle_ptr
)( HANDLE hProcess
, DWORD dwTimeOut
);
61 static DWORD
wait_input_idle( HANDLE process
, DWORD timeout
)
63 HMODULE mod
= GetModuleHandleA( "user32.dll" );
66 WaitForInputIdle_ptr ptr
= (WaitForInputIdle_ptr
)GetProcAddress( mod
, "WaitForInputIdle" );
67 if (ptr
) return ptr( process
, timeout
);
73 /*************************************************************************
74 * MODULE32_LookupHMODULE
75 * looks for the referenced HMODULE in the current process
76 * NOTE: Assumes that the process critical section is held!
78 static WINE_MODREF
*MODULE32_LookupHMODULE( HMODULE hmod
)
86 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod
);
87 SetLastError( ERROR_INVALID_HANDLE
);
90 for ( wm
= MODULE_modref_list
; wm
; wm
=wm
->next
)
91 if (wm
->module
== hmod
)
93 SetLastError( ERROR_INVALID_HANDLE
);
97 /*************************************************************************
100 * Allocate a WINE_MODREF structure and add it to the process list
101 * NOTE: Assumes that the process critical section is held!
103 WINE_MODREF
*MODULE_AllocModRef( HMODULE hModule
, LPCSTR filename
)
107 DWORD long_len
= strlen( filename
);
108 DWORD short_len
= GetShortPathNameA( filename
, NULL
, 0 );
110 if ((wm
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
111 sizeof(*wm
) + long_len
+ short_len
+ 1 )))
113 wm
->module
= hModule
;
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
;
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
;
126 wm
->next
= MODULE_modref_list
;
127 if (wm
->next
) wm
->next
->prev
= wm
;
128 MODULE_modref_list
= wm
;
130 if (!(RtlImageNtHeader(hModule
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
132 if (!exe_modref
) exe_modref
= wm
;
133 else FIXME( "Trying to load second .EXE file: %s\n", filename
);
139 /*************************************************************************
142 static BOOL
MODULE_InitDLL( WINE_MODREF
*wm
, DWORD type
, LPVOID lpReserved
)
146 static LPCSTR typeName
[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
147 "THREAD_ATTACH", "THREAD_DETACH" };
150 /* Skip calls for modules loaded with special load flags */
152 if (wm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
) return TRUE
;
154 TRACE("(%s,%s,%p) - CALL\n", wm
->modname
, typeName
[type
], lpReserved
);
156 /* Call the initialization routine */
157 retv
= PE_InitDLL( wm
->module
, type
, lpReserved
);
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
162 TRACE("(%p,%s,%p) - RETURN %d\n", wm
, typeName
[type
], lpReserved
, retv
);
167 /*************************************************************************
168 * MODULE_DllProcessAttach
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
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
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
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
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.
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*.
196 BOOL
MODULE_DllProcessAttach( WINE_MODREF
*wm
, LPVOID lpReserved
)
201 RtlEnterCriticalSection( &loader_section
);
210 /* prevent infinite recursion in case of cyclical dependencies */
211 if ( ( wm
->flags
& WINE_MODREF_MARKER
)
212 || ( wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
215 TRACE("(%s,%p) - START\n", wm
->modname
, lpReserved
);
217 /* Tag current MODREF to prevent recursive loop */
218 wm
->flags
|= WINE_MODREF_MARKER
;
220 /* Recursively attach all DLLs this one depends on */
221 for ( i
= 0; retv
&& i
< wm
->nDeps
; i
++ )
223 retv
= MODULE_DllProcessAttach( wm
->deps
[i
], lpReserved
);
225 /* Call DLL entry point */
228 retv
= MODULE_InitDLL( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
230 wm
->flags
|= WINE_MODREF_PROCESS_ATTACHED
;
233 /* Re-insert MODREF at head of list */
234 if ( retv
&& wm
->prev
)
236 wm
->prev
->next
= wm
->next
;
237 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
240 wm
->next
= MODULE_modref_list
;
241 MODULE_modref_list
= wm
->next
->prev
= wm
;
244 /* Remove recursion flag */
245 wm
->flags
&= ~WINE_MODREF_MARKER
;
247 TRACE("(%s,%p) - END\n", wm
->modname
, lpReserved
);
250 RtlLeaveCriticalSection( &loader_section
);
254 /*************************************************************************
255 * MODULE_DllProcessDetach
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.
261 void MODULE_DllProcessDetach( BOOL bForceDetach
, LPVOID lpReserved
)
265 RtlEnterCriticalSection( &loader_section
);
266 if (bForceDetach
) process_detaching
= 1;
269 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
271 /* Check whether to detach this DLL */
272 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
274 if ( wm
->refCount
> 0 && !bForceDetach
)
277 /* Call detach notification */
278 wm
->flags
&= ~WINE_MODREF_PROCESS_ATTACHED
;
279 MODULE_InitDLL( wm
, DLL_PROCESS_DETACH
, lpReserved
);
281 /* Restart at head of WINE_MODREF list, as entries might have
282 been added and/or removed while performing the call ... */
287 RtlLeaveCriticalSection( &loader_section
);
290 /*************************************************************************
291 * MODULE_DllThreadAttach
293 * Send DLL thread attach notifications. These are sent in the
294 * reverse sequence of process detach notification.
297 void MODULE_DllThreadAttach( LPVOID lpReserved
)
301 /* don't do any attach calls if process is exiting */
302 if (process_detaching
) return;
303 /* FIXME: there is still a race here */
305 RtlEnterCriticalSection( &loader_section
);
309 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
313 for ( ; wm
; wm
= wm
->prev
)
315 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
317 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
320 MODULE_InitDLL( wm
, DLL_THREAD_ATTACH
, lpReserved
);
323 RtlLeaveCriticalSection( &loader_section
);
326 /*************************************************************************
327 * MODULE_DllThreadDetach
329 * Send DLL thread detach notifications. These are sent in the
330 * same sequence as process detach notification.
333 void MODULE_DllThreadDetach( LPVOID lpReserved
)
337 /* don't do any detach calls if process is exiting */
338 if (process_detaching
) return;
339 /* FIXME: there is still a race here */
341 RtlEnterCriticalSection( &loader_section
);
343 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
345 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
347 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
350 MODULE_InitDLL( wm
, DLL_THREAD_DETACH
, lpReserved
);
353 RtlLeaveCriticalSection( &loader_section
);
356 /****************************************************************************
357 * DisableThreadLibraryCalls (KERNEL32.@)
359 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
361 BOOL WINAPI
DisableThreadLibraryCalls( HMODULE hModule
)
366 RtlEnterCriticalSection( &loader_section
);
368 wm
= MODULE32_LookupHMODULE( hModule
);
372 wm
->flags
|= WINE_MODREF_NO_DLL_CALLS
;
374 RtlLeaveCriticalSection( &loader_section
);
380 /***********************************************************************
381 * MODULE_CreateDummyModule
383 * Create a dummy NE module for Win32 or Winelib.
385 HMODULE16
MODULE_CreateDummyModule( LPCSTR filename
, HMODULE module32
)
389 SEGTABLEENTRY
*pSegment
;
392 const char* basename
;
396 /* Extract base filename */
397 basename
= strrchr(filename
, '\\');
398 if (!basename
) basename
= filename
;
400 len
= strlen(basename
);
401 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
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
) +
413 /* several empty tables */
416 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
417 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
419 FarSetOwner16( hModule
, hModule
);
420 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
422 /* Set all used entries */
423 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
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
;
440 /* Set version and flags */
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
;
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
);
457 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + ((of_size
+ 3) & ~3));
458 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
461 pSegment
->flags
= NE_SEGFLAGS_DATA
;
462 pSegment
->minsize
= 0x1000;
469 pStr
= (char *)pSegment
;
470 pModule
->name_table
= (int)pStr
- (int)pModule
;
473 lstrcpynA( pStr
+1, basename
, len
+1 );
476 /* All tables zero terminated */
477 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
=
478 (int)pStr
- (int)pModule
;
480 NE_RegisterModule( pModule
);
485 /**********************************************************************
488 * Find a (loaded) win32 module depending on path
491 * the module handle if found
494 WINE_MODREF
*MODULE_FindModule(
495 LPCSTR path
/* [in] pathname of module/library to be found */
498 char dllname
[260], *p
;
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" );
505 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
507 if ( !FILE_strcasecmp( dllname
, wm
->modname
) )
509 if ( !FILE_strcasecmp( dllname
, wm
->filename
) )
511 if ( !FILE_strcasecmp( dllname
, wm
->short_modname
) )
513 if ( !FILE_strcasecmp( dllname
, wm
->short_filename
) )
521 /* Check whether a file is an OS/2 or a very old Windows executable
522 * by testing on import of KERNEL.
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...
527 static enum binary_type
MODULE_Decide_OS2_OldWin(HANDLE hfile
, const IMAGE_DOS_HEADER
*mz
,
528 const IMAGE_OS2_HEADER
*ne
)
530 DWORD currpos
= SetFilePointer( hfile
, 0, NULL
, SEEK_CUR
);
531 enum binary_type ret
= BINARY_OS216
;
532 LPWORD modtab
= NULL
;
533 LPSTR nametab
= NULL
;
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
)) )
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
) )
551 for (i
=0; i
< ne
->ne_cmod
; i
++)
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");
564 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
567 HeapFree( GetProcessHeap(), 0, modtab
);
568 HeapFree( GetProcessHeap(), 0, nametab
);
569 SetFilePointer( hfile
, currpos
, NULL
, SEEK_SET
); /* restore filepos */
573 /***********************************************************************
574 * MODULE_GetBinaryType
576 enum binary_type
MODULE_GetBinaryType( HANDLE hfile
)
582 unsigned char magic
[4];
583 unsigned char ignored
[12];
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
;
598 if (!memcmp( header
.elf
.magic
, "\177ELF", 4 ))
600 /* FIXME: we don't bother to check byte order, architecture, etc. */
601 switch(header
.elf
.type
)
603 case 2: return BINARY_UNIX_EXE
;
604 case 3: return BINARY_UNIX_LIB
;
606 return BINARY_UNKNOWN
;
609 /* Not ELF, try DOS */
611 if (header
.mz
.e_magic
== IMAGE_DOS_SIGNATURE
)
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
620 /* But before we do we will make sure that header
621 * structure encompasses the "Offset to extended header"
624 if ((header
.mz
.e_cparhdr
<< 4) < sizeof(IMAGE_DOS_HEADER
))
626 if (header
.mz
.e_crlc
&& (header
.mz
.e_lfarlc
< sizeof(IMAGE_DOS_HEADER
)))
628 if (header
.mz
.e_lfanew
< sizeof(IMAGE_DOS_HEADER
))
630 if (SetFilePointer( hfile
, header
.mz
.e_lfanew
, NULL
, SEEK_SET
) == -1)
632 if (!ReadFile( hfile
, magic
, sizeof(magic
), &len
, NULL
) || len
!= sizeof(magic
))
635 /* Reading the magic field succeeded so
636 * we will try to determine what type it is.
638 if (!memcmp( magic
, "PE\0\0", 4 ))
640 IMAGE_FILE_HEADER FileHeader
;
642 if (ReadFile( hfile
, &FileHeader
, sizeof(FileHeader
), &len
, NULL
) && len
== sizeof(FileHeader
))
644 if (FileHeader
.Characteristics
& IMAGE_FILE_DLL
) return BINARY_PE_DLL
;
645 return BINARY_PE_EXE
;
650 if (!memcmp( magic
, "NE", 2 ))
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.
658 if ( SetFilePointer( hfile
, header
.mz
.e_lfanew
, NULL
, SEEK_SET
) != -1
659 && ReadFile( hfile
, &ne
, sizeof(ne
), &len
, NULL
)
660 && len
== sizeof(ne
) )
662 switch ( ne
.ne_exetyp
)
664 case 2: return BINARY_WIN16
;
665 case 5: return BINARY_DOS
;
666 default: return MODULE_Decide_OS2_OldWin(hfile
, &header
.mz
, &ne
);
669 /* Couldn't read header, so abort. */
673 /* Unknown extended header, but this file is nonetheless DOS-executable. */
677 return BINARY_UNKNOWN
;
680 /***********************************************************************
681 * GetBinaryTypeA [KERNEL32.@]
682 * GetBinaryType [KERNEL32.@]
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.
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
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.
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.
708 * Note that .COM and .PIF files are only recognized by their
709 * file name extension; but Windows does it the same way ...
711 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
717 TRACE_(win32
)("%s\n", lpApplicationName
);
721 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
724 /* Open the file indicated by lpApplicationName for reading.
726 hfile
= CreateFileA( lpApplicationName
, GENERIC_READ
, FILE_SHARE_READ
,
727 NULL
, OPEN_EXISTING
, 0, 0 );
728 if ( hfile
== INVALID_HANDLE_VALUE
)
733 switch(MODULE_GetBinaryType( hfile
))
736 /* try to determine from file name */
737 ptr
= strrchr( lpApplicationName
, '.' );
739 if (!FILE_strcasecmp( ptr
, ".COM" ))
741 *lpBinaryType
= SCS_DOS_BINARY
;
744 else if (!FILE_strcasecmp( ptr
, ".PIF" ))
746 *lpBinaryType
= SCS_PIF_BINARY
;
752 *lpBinaryType
= SCS_32BIT_BINARY
;
756 *lpBinaryType
= SCS_WOW_BINARY
;
760 *lpBinaryType
= SCS_OS216_BINARY
;
764 *lpBinaryType
= SCS_DOS_BINARY
;
767 case BINARY_UNIX_EXE
:
768 case BINARY_UNIX_LIB
:
773 CloseHandle( hfile
);
777 /***********************************************************************
778 * GetBinaryTypeW [KERNEL32.@]
780 BOOL WINAPI
GetBinaryTypeW( LPCWSTR lpApplicationName
, LPDWORD lpBinaryType
)
785 TRACE_(win32
)("%s\n", debugstr_w(lpApplicationName
) );
789 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
792 /* Convert the wide string to a ascii string.
794 strNew
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName
);
796 if ( strNew
!= NULL
)
798 ret
= GetBinaryTypeA( strNew
, lpBinaryType
);
800 /* Free the allocated string.
802 HeapFree( GetProcessHeap(), 0, strNew
);
809 /***********************************************************************
810 * WinExec (KERNEL.166)
812 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
814 LPCSTR p
, args
= NULL
;
815 LPCSTR name_beg
, name_end
;
819 char buffer
[MAX_PATH
];
821 if (*lpCmdLine
== '"') /* has to be only one and only at beginning ! */
823 name_beg
= lpCmdLine
+1;
824 p
= strchr ( lpCmdLine
+1, '"' );
828 args
= strchr ( p
, ' ' );
830 else /* yes, even valid with trailing '"' missing */
831 name_end
= lpCmdLine
+strlen(lpCmdLine
);
835 name_beg
= lpCmdLine
;
836 args
= strchr( lpCmdLine
, ' ' );
837 name_end
= args
? args
: lpCmdLine
+strlen(lpCmdLine
);
840 if ((name_beg
== lpCmdLine
) && (!args
))
841 { /* just use the original cmdline string as file name */
842 name
= (LPSTR
)lpCmdLine
;
846 if (!(name
= HeapAlloc( GetProcessHeap(), 0, name_end
- name_beg
+ 1 )))
847 return ERROR_NOT_ENOUGH_MEMORY
;
848 memcpy( name
, name_beg
, name_end
- name_beg
);
849 name
[name_end
- name_beg
] = '\0';
855 arglen
= strlen(args
);
856 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 + arglen
);
857 cmdline
[0] = (BYTE
)arglen
;
858 strcpy( cmdline
+ 1, args
);
862 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 );
863 cmdline
[0] = cmdline
[1] = 0;
866 TRACE("name: '%s', cmdline: '%.*s'\n", name
, cmdline
[0], &cmdline
[1]);
868 if (SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, NULL
))
873 showCmd
[1] = nCmdShow
;
875 params
.hEnvironment
= 0;
876 params
.cmdLine
= MapLS( cmdline
);
877 params
.showCmd
= MapLS( showCmd
);
880 ret
= LoadModule16( buffer
, ¶ms
);
881 UnMapLS( params
.cmdLine
);
882 UnMapLS( params
.showCmd
);
884 else ret
= GetLastError();
886 HeapFree( GetProcessHeap(), 0, cmdline
);
887 if (name
!= lpCmdLine
) HeapFree( GetProcessHeap(), 0, name
);
889 if (ret
== 21) /* 32-bit module */
892 ReleaseThunkLock( &count
);
893 ret
= LOWORD( WinExec( lpCmdLine
, nCmdShow
) );
894 RestoreThunkLock( count
);
899 /***********************************************************************
900 * WinExec (KERNEL32.@)
902 UINT WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
904 PROCESS_INFORMATION info
;
905 STARTUPINFOA startup
;
909 memset( &startup
, 0, sizeof(startup
) );
910 startup
.cb
= sizeof(startup
);
911 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
912 startup
.wShowWindow
= nCmdShow
;
914 /* cmdline needs to be writeable for CreateProcess */
915 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine
)+1 ))) return 0;
916 strcpy( cmdline
, lpCmdLine
);
918 if (CreateProcessA( NULL
, cmdline
, NULL
, NULL
, FALSE
,
919 0, NULL
, NULL
, &startup
, &info
))
921 /* Give 30 seconds to the app to come up */
922 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF)
923 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
925 /* Close off the handles */
926 CloseHandle( info
.hThread
);
927 CloseHandle( info
.hProcess
);
929 else if ((ret
= GetLastError()) >= 32)
931 FIXME("Strange error set by CreateProcess: %d\n", ret
);
934 HeapFree( GetProcessHeap(), 0, cmdline
);
938 /**********************************************************************
939 * LoadModule (KERNEL32.@)
941 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
943 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
944 PROCESS_INFORMATION info
;
945 STARTUPINFOA startup
;
948 char filename
[MAX_PATH
];
951 if (!name
) return (HINSTANCE
)ERROR_FILE_NOT_FOUND
;
953 if (!SearchPathA( NULL
, name
, ".exe", sizeof(filename
), filename
, NULL
) &&
954 !SearchPathA( NULL
, name
, NULL
, sizeof(filename
), filename
, NULL
))
955 return (HINSTANCE
)GetLastError();
957 len
= (BYTE
)params
->lpCmdLine
[0];
958 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + len
+ 2 )))
959 return (HINSTANCE
)ERROR_NOT_ENOUGH_MEMORY
;
961 strcpy( cmdline
, filename
);
962 p
= cmdline
+ strlen(cmdline
);
964 memcpy( p
, params
->lpCmdLine
+ 1, len
);
967 memset( &startup
, 0, sizeof(startup
) );
968 startup
.cb
= sizeof(startup
);
969 if (params
->lpCmdShow
)
971 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
972 startup
.wShowWindow
= params
->lpCmdShow
[1];
975 if (CreateProcessA( filename
, cmdline
, NULL
, NULL
, FALSE
, 0,
976 params
->lpEnvAddress
, NULL
, &startup
, &info
))
978 /* Give 30 seconds to the app to come up */
979 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF )
980 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
981 hInstance
= (HINSTANCE
)33;
982 /* Close off the handles */
983 CloseHandle( info
.hThread
);
984 CloseHandle( info
.hProcess
);
986 else if ((hInstance
= (HINSTANCE
)GetLastError()) >= (HINSTANCE
)32)
988 FIXME("Strange error set by CreateProcess: %d\n", hInstance
);
989 hInstance
= (HINSTANCE
)11;
992 HeapFree( GetProcessHeap(), 0, cmdline
);
997 /***********************************************************************
998 * GetModuleHandleA (KERNEL32.@)
999 * GetModuleHandle32 (KERNEL.488)
1001 HMODULE WINAPI
GetModuleHandleA(LPCSTR module
)
1005 if ( module
== NULL
)
1008 wm
= MODULE_FindModule( module
);
1010 return wm
? wm
->module
: 0;
1013 /***********************************************************************
1014 * GetModuleHandleW (KERNEL32.@)
1016 HMODULE WINAPI
GetModuleHandleW(LPCWSTR module
)
1019 LPSTR modulea
= HEAP_strdupWtoA( GetProcessHeap(), 0, module
);
1020 hModule
= GetModuleHandleA( modulea
);
1021 HeapFree( GetProcessHeap(), 0, modulea
);
1026 /***********************************************************************
1027 * GetModuleFileNameA (KERNEL32.@)
1028 * GetModuleFileName32 (KERNEL.487)
1030 * GetModuleFileNameA seems to *always* return the long path;
1031 * it's only GetModuleFileName16 that decides between short/long path
1032 * by checking if exe version >= 4.0.
1033 * (SDK docu doesn't mention this)
1035 DWORD WINAPI
GetModuleFileNameA(
1036 HMODULE hModule
, /* [in] module handle (32bit) */
1037 LPSTR lpFileName
, /* [out] filenamebuffer */
1038 DWORD size
) /* [in] size of filenamebuffer */
1040 RtlEnterCriticalSection( &loader_section
);
1043 if (!hModule
&& !(NtCurrentTeb()->tibflags
& TEBF_WIN32
))
1045 /* 16-bit task - get current NE module name */
1046 NE_MODULE
*pModule
= NE_GetPtr( GetCurrentTask() );
1047 if (pModule
) GetLongPathNameA(NE_MODULE_NAME(pModule
), lpFileName
, size
);
1051 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1052 if (wm
) lstrcpynA( lpFileName
, wm
->filename
, size
);
1055 RtlLeaveCriticalSection( &loader_section
);
1056 TRACE("%s\n", lpFileName
);
1057 return strlen(lpFileName
);
1061 /***********************************************************************
1062 * GetModuleFileNameW (KERNEL32.@)
1064 DWORD WINAPI
GetModuleFileNameW( HMODULE hModule
, LPWSTR lpFileName
, DWORD size
)
1066 LPSTR fnA
= HeapAlloc( GetProcessHeap(), 0, size
* 2 );
1068 GetModuleFileNameA( hModule
, fnA
, size
* 2 );
1069 if (size
> 0 && !MultiByteToWideChar( CP_ACP
, 0, fnA
, -1, lpFileName
, size
))
1070 lpFileName
[size
-1] = 0;
1071 HeapFree( GetProcessHeap(), 0, fnA
);
1072 return strlenW(lpFileName
);
1076 /***********************************************************************
1077 * LoadLibraryExA (KERNEL32.@)
1079 HMODULE WINAPI
LoadLibraryExA(LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1085 SetLastError(ERROR_INVALID_PARAMETER
);
1089 if (flags
& LOAD_LIBRARY_AS_DATAFILE
)
1094 /* This method allows searching for the 'native' libraries only */
1095 if (SearchPathA( NULL
, libname
, ".dll", sizeof(filename
), filename
, NULL
))
1097 /* FIXME: maybe we should use the hfile parameter instead */
1098 HANDLE hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
1099 NULL
, OPEN_EXISTING
, 0, 0 );
1100 if (hFile
!= INVALID_HANDLE_VALUE
)
1103 switch (MODULE_GetBinaryType( hFile
))
1107 mapping
= CreateFileMappingA( hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
1110 hmod
= (HMODULE
)MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
1111 CloseHandle( mapping
);
1117 CloseHandle( hFile
);
1119 if (hmod
) return (HMODULE
)((ULONG_PTR
)hmod
+ 1);
1121 flags
|= DONT_RESOLVE_DLL_REFERENCES
; /* Just in case */
1122 /* Fallback to normal behaviour */
1125 RtlEnterCriticalSection( &loader_section
);
1127 wm
= MODULE_LoadLibraryExA( libname
, hfile
, flags
);
1130 if ( !MODULE_DllProcessAttach( wm
, NULL
) )
1132 WARN_(module
)("Attach failed for module '%s'.\n", libname
);
1133 MODULE_FreeLibrary(wm
);
1134 SetLastError(ERROR_DLL_INIT_FAILED
);
1139 RtlLeaveCriticalSection( &loader_section
);
1140 return wm
? wm
->module
: 0;
1143 /***********************************************************************
1146 * helper for MODULE_LoadLibraryExA. Allocate space to hold the directory
1147 * portion of the provided name and put the name in it.
1150 static LPCSTR
allocate_lib_dir(LPCSTR libname
)
1157 if ((p
= strrchr( pmax
, '\\' ))) pmax
= p
+ 1;
1158 if ((p
= strrchr( pmax
, '/' ))) pmax
= p
+ 1; /* Naughty. MSDN says don't */
1159 if (pmax
== libname
&& pmax
[0] && pmax
[1] == ':') pmax
+= 2;
1161 length
= pmax
- libname
;
1163 result
= HeapAlloc (GetProcessHeap(), 0, length
+1);
1167 strncpy (result
, libname
, length
);
1168 result
[length
] = '\0';
1174 /***********************************************************************
1175 * MODULE_LoadLibraryExA (internal)
1177 * Load a PE style module according to the load order.
1179 * The HFILE parameter is not used and marked reserved in the SDK. I can
1180 * only guess that it should force a file to be mapped, but I rather
1181 * ignore the parameter because it would be extremely difficult to
1182 * integrate this with different types of module representations.
1184 * libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion
1185 * on this function. When first called from LoadLibraryExA it will be
1186 * NULL but thereafter it may point to a buffer containing the path
1187 * portion of the library name. Note that the recursion all occurs
1188 * within a Critical section (see LoadLibraryExA) so the use of a
1189 * static is acceptable.
1190 * (We have to use a static variable at some point anyway, to pass the
1191 * information from BUILTIN32_dlopen through dlopen and the builtin's
1192 * init function into load_library).
1193 * allocated_libdir is TRUE in the stack frame that allocated libdir
1195 WINE_MODREF
*MODULE_LoadLibraryExA( LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1197 DWORD err
= GetLastError();
1200 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
1202 const char *filetype
= "";
1204 BOOL allocated_libdir
= FALSE
;
1205 static LPCSTR libdir
= NULL
; /* See above */
1207 if ( !libname
) return NULL
;
1209 filename
= HeapAlloc ( GetProcessHeap(), 0, MAX_PATH
+ 1 );
1210 if ( !filename
) return NULL
;
1211 *filename
= 0; /* Just in case we don't set it before goto error */
1213 RtlEnterCriticalSection( &loader_section
);
1215 if ((flags
& LOAD_WITH_ALTERED_SEARCH_PATH
) && FILE_contains_path(libname
))
1217 if (!(libdir
= allocate_lib_dir(libname
))) goto error
;
1218 allocated_libdir
= TRUE
;
1221 if (!libdir
|| allocated_libdir
)
1222 found
= SearchPathA(NULL
, libname
, ".dll", MAX_PATH
, filename
, NULL
);
1224 found
= DIR_SearchAlternatePath(libdir
, libname
, ".dll", MAX_PATH
, filename
, NULL
);
1226 /* build the modules filename */
1229 if (!MODULE_GetBuiltinPath( libname
, ".dll", filename
, MAX_PATH
)) goto error
;
1232 /* Check for already loaded module */
1233 if (!(pwm
= MODULE_FindModule(filename
)) && !FILE_contains_path(libname
))
1235 LPSTR fn
= HeapAlloc ( GetProcessHeap(), 0, MAX_PATH
+ 1 );
1238 /* since the default loading mechanism uses a more detailed algorithm
1239 * than SearchPath (like using PATH, which can even be modified between
1240 * two attempts of loading the same DLL), the look-up above (with
1241 * SearchPath) can have put the file in system directory, whereas it
1242 * has already been loaded but with a different path. So do a specific
1243 * look-up with filename (without any path)
1245 strcpy ( fn
, libname
);
1246 /* if the filename doesn't have an extension append .DLL */
1247 if (!strrchr( fn
, '.')) strcat( fn
, ".dll" );
1248 if ((pwm
= MODULE_FindModule( fn
)) != NULL
)
1249 strcpy( filename
, fn
);
1250 HeapFree( GetProcessHeap(), 0, fn
);
1257 if ((pwm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
) &&
1258 !(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1260 pwm
->flags
&= ~WINE_MODREF_DONT_RESOLVE_REFS
;
1261 PE_fixup_imports( pwm
);
1263 TRACE("Already loaded module '%s' at 0x%08x, count=%d\n", filename
, pwm
->module
, pwm
->refCount
);
1264 if (allocated_libdir
)
1266 HeapFree ( GetProcessHeap(), 0, (LPSTR
)libdir
);
1269 RtlLeaveCriticalSection( &loader_section
);
1270 HeapFree ( GetProcessHeap(), 0, filename
);
1274 MODULE_GetLoadOrder( loadorder
, filename
, TRUE
);
1276 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
1278 if (loadorder
[i
] == LOADORDER_INVALID
) break;
1279 SetLastError( ERROR_FILE_NOT_FOUND
);
1281 switch(loadorder
[i
])
1284 TRACE("Trying native dll '%s'\n", filename
);
1285 pwm
= PE_LoadLibraryExA(filename
, flags
);
1286 filetype
= "native";
1290 TRACE("Trying so-library '%s'\n", filename
);
1291 pwm
= ELF_LoadLibraryExA(filename
, flags
);
1296 TRACE("Trying built-in '%s'\n", filename
);
1297 pwm
= BUILTIN32_LoadLibraryExA(filename
, flags
);
1298 filetype
= "builtin";
1308 /* Initialize DLL just loaded */
1309 TRACE("Loaded module '%s' at 0x%08x\n", filename
, pwm
->module
);
1310 if (!TRACE_ON(module
))
1311 TRACE_(loaddll
)("Loaded module '%s' : %s\n", filename
, filetype
);
1312 /* Set the refCount here so that an attach failure will */
1313 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1316 if (allocated_libdir
)
1318 HeapFree ( GetProcessHeap(), 0, (LPSTR
)libdir
);
1321 RtlLeaveCriticalSection( &loader_section
);
1322 SetLastError( err
); /* restore last error */
1323 HeapFree ( GetProcessHeap(), 0, filename
);
1327 if(GetLastError() != ERROR_FILE_NOT_FOUND
)
1329 WARN("Loading of %s DLL %s failed (error %ld).\n",
1330 filetype
, filename
, GetLastError());
1336 if (allocated_libdir
)
1338 HeapFree ( GetProcessHeap(), 0, (LPSTR
)libdir
);
1341 RtlLeaveCriticalSection( &loader_section
);
1342 WARN("Failed to load module '%s'; error=%ld\n", filename
, GetLastError());
1343 HeapFree ( GetProcessHeap(), 0, filename
);
1347 /***********************************************************************
1348 * LoadLibraryA (KERNEL32.@)
1350 HMODULE WINAPI
LoadLibraryA(LPCSTR libname
) {
1351 return LoadLibraryExA(libname
,0,0);
1354 /***********************************************************************
1355 * LoadLibraryW (KERNEL32.@)
1357 HMODULE WINAPI
LoadLibraryW(LPCWSTR libnameW
)
1359 return LoadLibraryExW(libnameW
,0,0);
1362 /***********************************************************************
1363 * LoadLibrary32 (KERNEL.452)
1364 * LoadSystemLibrary32 (KERNEL.482)
1366 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
1371 ReleaseThunkLock( &count
);
1372 hModule
= LoadLibraryA( libname
);
1373 RestoreThunkLock( count
);
1377 /***********************************************************************
1378 * LoadLibraryExW (KERNEL32.@)
1380 HMODULE WINAPI
LoadLibraryExW(LPCWSTR libnameW
,HANDLE hfile
,DWORD flags
)
1382 LPSTR libnameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW
);
1383 HMODULE ret
= LoadLibraryExA( libnameA
, hfile
, flags
);
1385 HeapFree( GetProcessHeap(), 0, libnameA
);
1389 /***********************************************************************
1390 * MODULE_FlushModrefs
1392 * NOTE: Assumes that the process critical section is held!
1394 * Remove all unused modrefs and call the internal unloading routines
1395 * for the library type.
1397 static void MODULE_FlushModrefs(void)
1399 WINE_MODREF
*wm
, *next
;
1401 for(wm
= MODULE_modref_list
; wm
; wm
= next
)
1408 /* Unlink this modref from the chain */
1410 wm
->next
->prev
= wm
->prev
;
1412 wm
->prev
->next
= wm
->next
;
1413 if(wm
== MODULE_modref_list
)
1414 MODULE_modref_list
= wm
->next
;
1416 TRACE(" unloading %s\n", wm
->filename
);
1417 if (!TRACE_ON(module
))
1418 TRACE_(loaddll
)("Unloaded module '%s' : %s\n", wm
->filename
,
1419 wm
->dlhandle
? "builtin" : "native" );
1421 if (wm
->dlhandle
) wine_dll_unload( wm
->dlhandle
);
1422 else UnmapViewOfFile( (LPVOID
)wm
->module
);
1423 FreeLibrary16(wm
->hDummyMod
);
1424 HeapFree( GetProcessHeap(), 0, wm
->deps
);
1425 HeapFree( GetProcessHeap(), 0, wm
);
1429 /***********************************************************************
1430 * FreeLibrary (KERNEL32.@)
1431 * FreeLibrary32 (KERNEL.486)
1433 BOOL WINAPI
FreeLibrary(HINSTANCE hLibModule
)
1440 SetLastError( ERROR_INVALID_HANDLE
);
1444 if ((ULONG_PTR
)hLibModule
& 1)
1446 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
1447 char *ptr
= (char *)hLibModule
- 1;
1448 UnmapViewOfFile( ptr
);
1452 RtlEnterCriticalSection( &loader_section
);
1455 if ((wm
= MODULE32_LookupHMODULE( hLibModule
))) retv
= MODULE_FreeLibrary( wm
);
1458 RtlLeaveCriticalSection( &loader_section
);
1463 /***********************************************************************
1464 * MODULE_DecRefCount
1466 * NOTE: Assumes that the process critical section is held!
1468 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
1472 if ( wm
->flags
& WINE_MODREF_MARKER
)
1475 if ( wm
->refCount
<= 0 )
1479 TRACE("(%s) refCount: %d\n", wm
->modname
, wm
->refCount
);
1481 if ( wm
->refCount
== 0 )
1483 wm
->flags
|= WINE_MODREF_MARKER
;
1485 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1487 MODULE_DecRefCount( wm
->deps
[i
] );
1489 wm
->flags
&= ~WINE_MODREF_MARKER
;
1493 /***********************************************************************
1494 * MODULE_FreeLibrary
1496 * NOTE: Assumes that the process critical section is held!
1498 BOOL
MODULE_FreeLibrary( WINE_MODREF
*wm
)
1500 TRACE("(%s) - START\n", wm
->modname
);
1502 /* Recursively decrement reference counts */
1503 MODULE_DecRefCount( wm
);
1505 /* Call process detach notifications */
1506 if ( free_lib_count
<= 1 )
1508 MODULE_DllProcessDetach( FALSE
, NULL
);
1509 SERVER_START_REQ( unload_dll
)
1511 req
->base
= (void *)wm
->module
;
1512 wine_server_call( req
);
1515 MODULE_FlushModrefs();
1524 /***********************************************************************
1525 * FreeLibraryAndExitThread (KERNEL32.@)
1527 VOID WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
1529 FreeLibrary(hLibModule
);
1530 ExitThread(dwExitCode
);
1533 /***********************************************************************
1534 * PrivateLoadLibrary (KERNEL32.@)
1536 * FIXME: rough guesswork, don't know what "Private" means
1538 HINSTANCE16 WINAPI
PrivateLoadLibrary(LPCSTR libname
)
1540 return LoadLibrary16(libname
);
1545 /***********************************************************************
1546 * PrivateFreeLibrary (KERNEL32.@)
1548 * FIXME: rough guesswork, don't know what "Private" means
1550 void WINAPI
PrivateFreeLibrary(HINSTANCE16 handle
)
1552 FreeLibrary16(handle
);
1556 /***********************************************************************
1557 * GetProcAddress16 (KERNEL32.37)
1558 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1560 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1563 WARN("hModule may not be 0!\n");
1564 return (FARPROC16
)0;
1566 if (HIWORD(hModule
))
1568 WARN("hModule is Win32 handle (%08x)\n", hModule
);
1569 return (FARPROC16
)0;
1571 return GetProcAddress16( LOWORD(hModule
), name
);
1574 /***********************************************************************
1575 * GetProcAddress (KERNEL.50)
1577 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, LPCSTR name
)
1582 if (!hModule
) hModule
= GetCurrentTask();
1583 hModule
= GetExePtr( hModule
);
1585 if (HIWORD(name
) != 0)
1587 ordinal
= NE_GetOrdinal( hModule
, name
);
1588 TRACE("%04x '%s'\n", hModule
, name
);
1592 ordinal
= LOWORD(name
);
1593 TRACE("%04x %04x\n", hModule
, ordinal
);
1595 if (!ordinal
) return (FARPROC16
)0;
1597 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1599 TRACE("returning %08x\n", (UINT
)ret
);
1604 /***********************************************************************
1605 * GetProcAddress (KERNEL32.@)
1607 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
1609 return MODULE_GetProcAddress( hModule
, function
, -1, TRUE
);
1612 /***********************************************************************
1613 * GetProcAddress32 (KERNEL.453)
1615 FARPROC WINAPI
GetProcAddress32_16( HMODULE hModule
, LPCSTR function
)
1617 return MODULE_GetProcAddress( hModule
, function
, -1, FALSE
);
1620 /***********************************************************************
1621 * MODULE_GetProcAddress (internal)
1623 FARPROC
MODULE_GetProcAddress(
1624 HMODULE hModule
, /* [in] current module handle */
1625 LPCSTR function
, /* [in] function to be looked up */
1630 FARPROC retproc
= 0;
1632 if (HIWORD(function
))
1633 TRACE_(win32
)("(%08lx,%s (%d))\n",(DWORD
)hModule
,function
,hint
);
1635 TRACE_(win32
)("(%08lx,%p)\n",(DWORD
)hModule
,function
);
1637 RtlEnterCriticalSection( &loader_section
);
1638 if ((wm
= MODULE32_LookupHMODULE( hModule
)))
1640 retproc
= wm
->find_export( wm
, function
, hint
, snoop
);
1641 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1643 RtlLeaveCriticalSection( &loader_section
);
1648 /***************************************************************************
1649 * HasGPHandler (KERNEL.338)
1652 #include "pshpack1.h"
1653 typedef struct _GPHANDLERDEF
1660 #include "poppack.h"
1662 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1667 GPHANDLERDEF
*gpHandler
;
1669 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1670 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1671 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1672 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1673 && (gpHandler
= MapSL( gpPtr
)) != NULL
)
1675 while (gpHandler
->selector
)
1677 if ( SELECTOROF(address
) == gpHandler
->selector
1678 && OFFSETOF(address
) >= gpHandler
->rangeStart
1679 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1680 return MAKESEGPTR( gpHandler
->selector
, gpHandler
->handler
);