4 * Copyright 1995 Alexandre Julliard
12 #include <sys/types.h>
14 #include "wine/winbase16.h"
19 #include "debugtools.h"
20 #include "wine/server.h"
22 DEFAULT_DEBUG_CHANNEL(module
);
23 DECLARE_DEBUG_CHANNEL(win32
);
24 DECLARE_DEBUG_CHANNEL(loaddll
);
26 WINE_MODREF
*MODULE_modref_list
= NULL
;
28 static WINE_MODREF
*exe_modref
;
29 static int free_lib_count
; /* recursion depth of FreeLibrary calls */
30 static int process_detaching
; /* set on process detach to avoid deadlocks with thread detach */
32 static CRITICAL_SECTION loader_section
= CRITICAL_SECTION_INIT( "loader_section" );
34 /***********************************************************************
37 * Wrapper to call WaitForInputIdle USER function
39 typedef DWORD (WINAPI
*WaitForInputIdle_ptr
)( HANDLE hProcess
, DWORD dwTimeOut
);
41 static DWORD
wait_input_idle( HANDLE process
, DWORD timeout
)
43 HMODULE mod
= GetModuleHandleA( "user32.dll" );
46 WaitForInputIdle_ptr ptr
= (WaitForInputIdle_ptr
)GetProcAddress( mod
, "WaitForInputIdle" );
47 if (ptr
) return ptr( process
, timeout
);
53 /*************************************************************************
54 * MODULE32_LookupHMODULE
55 * looks for the referenced HMODULE in the current process
56 * NOTE: Assumes that the process critical section is held!
58 static WINE_MODREF
*MODULE32_LookupHMODULE( HMODULE hmod
)
66 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod
);
67 SetLastError( ERROR_INVALID_HANDLE
);
70 for ( wm
= MODULE_modref_list
; wm
; wm
=wm
->next
)
71 if (wm
->module
== hmod
)
73 SetLastError( ERROR_INVALID_HANDLE
);
77 /*************************************************************************
80 * Allocate a WINE_MODREF structure and add it to the process list
81 * NOTE: Assumes that the process critical section is held!
83 WINE_MODREF
*MODULE_AllocModRef( HMODULE hModule
, LPCSTR filename
)
87 DWORD long_len
= strlen( filename
);
88 DWORD short_len
= GetShortPathNameA( filename
, NULL
, 0 );
90 if ((wm
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
91 sizeof(*wm
) + long_len
+ short_len
+ 1 )))
96 wm
->filename
= wm
->data
;
97 memcpy( wm
->filename
, filename
, long_len
+ 1 );
98 if ((wm
->modname
= strrchr( wm
->filename
, '\\' ))) wm
->modname
++;
99 else wm
->modname
= wm
->filename
;
101 wm
->short_filename
= wm
->filename
+ long_len
+ 1;
102 GetShortPathNameA( wm
->filename
, wm
->short_filename
, short_len
+ 1 );
103 if ((wm
->short_modname
= strrchr( wm
->short_filename
, '\\' ))) wm
->short_modname
++;
104 else wm
->short_modname
= wm
->short_filename
;
106 wm
->next
= MODULE_modref_list
;
107 if (wm
->next
) wm
->next
->prev
= wm
;
108 MODULE_modref_list
= wm
;
110 if (!(PE_HEADER(hModule
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
112 if (!exe_modref
) exe_modref
= wm
;
113 else FIXME( "Trying to load second .EXE file: %s\n", filename
);
119 /*************************************************************************
122 static BOOL
MODULE_InitDLL( WINE_MODREF
*wm
, DWORD type
, LPVOID lpReserved
)
126 static LPCSTR typeName
[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
127 "THREAD_ATTACH", "THREAD_DETACH" };
130 /* Skip calls for modules loaded with special load flags */
132 if (wm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
) return TRUE
;
134 TRACE("(%s,%s,%p) - CALL\n", wm
->modname
, typeName
[type
], lpReserved
);
136 /* Call the initialization routine */
137 retv
= PE_InitDLL( wm
->module
, type
, lpReserved
);
139 /* The state of the module list may have changed due to the call
140 to PE_InitDLL. We cannot assume that this module has not been
142 TRACE("(%p,%s,%p) - RETURN %d\n", wm
, typeName
[type
], lpReserved
, retv
);
147 /*************************************************************************
148 * MODULE_DllProcessAttach
150 * Send the process attach notification to all DLLs the given module
151 * depends on (recursively). This is somewhat complicated due to the fact that
153 * - we have to respect the module dependencies, i.e. modules implicitly
154 * referenced by another module have to be initialized before the module
155 * itself can be initialized
157 * - the initialization routine of a DLL can itself call LoadLibrary,
158 * thereby introducing a whole new set of dependencies (even involving
159 * the 'old' modules) at any time during the whole process
161 * (Note that this routine can be recursively entered not only directly
162 * from itself, but also via LoadLibrary from one of the called initialization
165 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
166 * the process *detach* notifications to be sent in the correct order.
167 * This must not only take into account module dependencies, but also
168 * 'hidden' dependencies created by modules calling LoadLibrary in their
169 * attach notification routine.
171 * The strategy is rather simple: we move a WINE_MODREF to the head of the
172 * list after the attach notification has returned. This implies that the
173 * detach notifications are called in the reverse of the sequence the attach
174 * notifications *returned*.
176 BOOL
MODULE_DllProcessAttach( WINE_MODREF
*wm
, LPVOID lpReserved
)
181 RtlEnterCriticalSection( &loader_section
);
190 /* prevent infinite recursion in case of cyclical dependencies */
191 if ( ( wm
->flags
& WINE_MODREF_MARKER
)
192 || ( wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
195 TRACE("(%s,%p) - START\n", wm
->modname
, lpReserved
);
197 /* Tag current MODREF to prevent recursive loop */
198 wm
->flags
|= WINE_MODREF_MARKER
;
200 /* Recursively attach all DLLs this one depends on */
201 for ( i
= 0; retv
&& i
< wm
->nDeps
; i
++ )
203 retv
= MODULE_DllProcessAttach( wm
->deps
[i
], lpReserved
);
205 /* Call DLL entry point */
208 retv
= MODULE_InitDLL( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
210 wm
->flags
|= WINE_MODREF_PROCESS_ATTACHED
;
213 /* Re-insert MODREF at head of list */
214 if ( retv
&& wm
->prev
)
216 wm
->prev
->next
= wm
->next
;
217 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
220 wm
->next
= MODULE_modref_list
;
221 MODULE_modref_list
= wm
->next
->prev
= wm
;
224 /* Remove recursion flag */
225 wm
->flags
&= ~WINE_MODREF_MARKER
;
227 TRACE("(%s,%p) - END\n", wm
->modname
, lpReserved
);
230 RtlLeaveCriticalSection( &loader_section
);
234 /*************************************************************************
235 * MODULE_DllProcessDetach
237 * Send DLL process detach notifications. See the comment about calling
238 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
239 * is set, only DLLs with zero refcount are notified.
241 void MODULE_DllProcessDetach( BOOL bForceDetach
, LPVOID lpReserved
)
245 RtlEnterCriticalSection( &loader_section
);
246 if (bForceDetach
) process_detaching
= 1;
249 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
251 /* Check whether to detach this DLL */
252 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
254 if ( wm
->refCount
> 0 && !bForceDetach
)
257 /* Call detach notification */
258 wm
->flags
&= ~WINE_MODREF_PROCESS_ATTACHED
;
259 MODULE_InitDLL( wm
, DLL_PROCESS_DETACH
, lpReserved
);
261 /* Restart at head of WINE_MODREF list, as entries might have
262 been added and/or removed while performing the call ... */
267 RtlLeaveCriticalSection( &loader_section
);
270 /*************************************************************************
271 * MODULE_DllThreadAttach
273 * Send DLL thread attach notifications. These are sent in the
274 * reverse sequence of process detach notification.
277 void MODULE_DllThreadAttach( LPVOID lpReserved
)
281 /* don't do any attach calls if process is exiting */
282 if (process_detaching
) return;
283 /* FIXME: there is still a race here */
285 RtlEnterCriticalSection( &loader_section
);
289 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
293 for ( ; wm
; wm
= wm
->prev
)
295 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
297 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
300 MODULE_InitDLL( wm
, DLL_THREAD_ATTACH
, lpReserved
);
303 RtlLeaveCriticalSection( &loader_section
);
306 /*************************************************************************
307 * MODULE_DllThreadDetach
309 * Send DLL thread detach notifications. These are sent in the
310 * same sequence as process detach notification.
313 void MODULE_DllThreadDetach( LPVOID lpReserved
)
317 /* don't do any detach calls if process is exiting */
318 if (process_detaching
) return;
319 /* FIXME: there is still a race here */
321 RtlEnterCriticalSection( &loader_section
);
323 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
325 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
327 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
330 MODULE_InitDLL( wm
, DLL_THREAD_DETACH
, lpReserved
);
333 RtlLeaveCriticalSection( &loader_section
);
336 /****************************************************************************
337 * DisableThreadLibraryCalls (KERNEL32.@)
339 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
341 BOOL WINAPI
DisableThreadLibraryCalls( HMODULE hModule
)
346 RtlEnterCriticalSection( &loader_section
);
348 wm
= MODULE32_LookupHMODULE( hModule
);
352 wm
->flags
|= WINE_MODREF_NO_DLL_CALLS
;
354 RtlLeaveCriticalSection( &loader_section
);
360 /***********************************************************************
361 * MODULE_CreateDummyModule
363 * Create a dummy NE module for Win32 or Winelib.
365 HMODULE16
MODULE_CreateDummyModule( LPCSTR filename
, HMODULE module32
)
369 SEGTABLEENTRY
*pSegment
;
372 const char* basename
;
376 /* Extract base filename */
377 basename
= strrchr(filename
, '\\');
378 if (!basename
) basename
= filename
;
380 len
= strlen(basename
);
381 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
383 /* Allocate module */
384 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
385 + strlen(filename
) + 1;
386 size
= sizeof(NE_MODULE
) +
387 /* loaded file info */
388 ((of_size
+ 3) & ~3) +
389 /* segment table: DS,CS */
390 2 * sizeof(SEGTABLEENTRY
) +
393 /* several empty tables */
396 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
397 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
399 FarSetOwner16( hModule
, hModule
);
400 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
402 /* Set all used entries */
403 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
410 pModule
->heap_size
= 0;
411 pModule
->stack_size
= 0;
412 pModule
->seg_count
= 2;
413 pModule
->modref_count
= 0;
414 pModule
->nrname_size
= 0;
415 pModule
->fileinfo
= sizeof(NE_MODULE
);
416 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
417 pModule
->self
= hModule
;
418 pModule
->module32
= module32
;
420 /* Set version and flags */
423 pModule
->expected_version
=
424 ((PE_HEADER(module32
)->OptionalHeader
.MajorSubsystemVersion
& 0xff) << 8 ) |
425 (PE_HEADER(module32
)->OptionalHeader
.MinorSubsystemVersion
& 0xff);
426 pModule
->flags
|= NE_FFLAGS_WIN32
;
427 if (PE_HEADER(module32
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
428 pModule
->flags
|= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
;
431 /* Set loaded file information */
432 ofs
= (OFSTRUCT
*)(pModule
+ 1);
433 memset( ofs
, 0, of_size
);
434 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
435 strcpy( ofs
->szPathName
, filename
);
437 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + ((of_size
+ 3) & ~3));
438 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
441 pSegment
->flags
= NE_SEGFLAGS_DATA
;
442 pSegment
->minsize
= 0x1000;
449 pStr
= (char *)pSegment
;
450 pModule
->name_table
= (int)pStr
- (int)pModule
;
453 lstrcpynA( pStr
+1, basename
, len
+1 );
456 /* All tables zero terminated */
457 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
=
458 (int)pStr
- (int)pModule
;
460 NE_RegisterModule( pModule
);
465 /**********************************************************************
468 * Find a (loaded) win32 module depending on path
471 * the module handle if found
474 WINE_MODREF
*MODULE_FindModule(
475 LPCSTR path
/* [in] pathname of module/library to be found */
478 char dllname
[260], *p
;
480 /* Append .DLL to name if no extension present */
481 strcpy( dllname
, path
);
482 if (!(p
= strrchr( dllname
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
483 strcat( dllname
, ".DLL" );
485 for ( wm
= MODULE_modref_list
; wm
; wm
= wm
->next
)
487 if ( !FILE_strcasecmp( dllname
, wm
->modname
) )
489 if ( !FILE_strcasecmp( dllname
, wm
->filename
) )
491 if ( !FILE_strcasecmp( dllname
, wm
->short_modname
) )
493 if ( !FILE_strcasecmp( dllname
, wm
->short_filename
) )
501 /* Check whether a file is an OS/2 or a very old Windows executable
502 * by testing on import of KERNEL.
504 * FIXME: is reading the module imports the only way of discerning
505 * old Windows binaries from OS/2 ones ? At least it seems so...
507 static DWORD
MODULE_Decide_OS2_OldWin(HANDLE hfile
, IMAGE_DOS_HEADER
*mz
, IMAGE_OS2_HEADER
*ne
)
509 DWORD currpos
= SetFilePointer( hfile
, 0, NULL
, SEEK_CUR
);
510 DWORD type
= SCS_OS216_BINARY
;
511 LPWORD modtab
= NULL
;
512 LPSTR nametab
= NULL
;
516 /* read modref table */
517 if ( (SetFilePointer( hfile
, mz
->e_lfanew
+ ne
->ne_modtab
, NULL
, SEEK_SET
) == -1)
518 || (!(modtab
= HeapAlloc( GetProcessHeap(), 0, ne
->ne_cmod
*sizeof(WORD
))))
519 || (!(ReadFile(hfile
, modtab
, ne
->ne_cmod
*sizeof(WORD
), &len
, NULL
)))
520 || (len
!= ne
->ne_cmod
*sizeof(WORD
)) )
523 /* read imported names table */
524 if ( (SetFilePointer( hfile
, mz
->e_lfanew
+ ne
->ne_imptab
, NULL
, SEEK_SET
) == -1)
525 || (!(nametab
= HeapAlloc( GetProcessHeap(), 0, ne
->ne_enttab
- ne
->ne_imptab
)))
526 || (!(ReadFile(hfile
, nametab
, ne
->ne_enttab
- ne
->ne_imptab
, &len
, NULL
)))
527 || (len
!= ne
->ne_enttab
- ne
->ne_imptab
) )
530 for (i
=0; i
< ne
->ne_cmod
; i
++)
532 LPSTR module
= &nametab
[modtab
[i
]];
533 TRACE("modref: %.*s\n", module
[0], &module
[1]);
534 if (!(strncmp(&module
[1], "KERNEL", module
[0])))
535 { /* very old Windows file */
536 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
537 type
= SCS_WOW_BINARY
;
543 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
546 HeapFree( GetProcessHeap(), 0, modtab
);
547 HeapFree( GetProcessHeap(), 0, nametab
);
548 SetFilePointer( hfile
, currpos
, NULL
, SEEK_SET
); /* restore filepos */
552 /***********************************************************************
553 * MODULE_GetBinaryType
555 * The GetBinaryType function determines whether a file is executable
556 * or not and if it is it returns what type of executable it is.
557 * The type of executable is a property that determines in which
558 * subsystem an executable file runs under.
560 * Binary types returned:
561 * SCS_32BIT_BINARY: A Win32 based application
562 * SCS_DOS_BINARY: An MS-Dos based application
563 * SCS_WOW_BINARY: A Win16 based application
564 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
565 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
566 * SCS_OS216_BINARY: A 16bit OS/2 based application
568 * Returns TRUE if the file is an executable in which case
569 * the value pointed by lpBinaryType is set.
570 * Returns FALSE if the file is not an executable or if the function fails.
572 * To do so it opens the file and reads in the header information
573 * if the extended header information is not present it will
574 * assume that the file is a DOS executable.
575 * If the extended header information is present it will
576 * determine if the file is a 16 or 32 bit Windows executable
577 * by check the flags in the header.
579 * Note that .COM and .PIF files are only recognized by their
580 * file name extension; but Windows does it the same way ...
582 static BOOL
MODULE_GetBinaryType( HANDLE hfile
, LPCSTR filename
, LPDWORD lpBinaryType
)
584 IMAGE_DOS_HEADER mz_header
;
588 /* Seek to the start of the file and read the DOS header information.
590 if ( SetFilePointer( hfile
, 0, NULL
, SEEK_SET
) != -1
591 && ReadFile( hfile
, &mz_header
, sizeof(mz_header
), &len
, NULL
)
592 && len
== sizeof(mz_header
) )
594 /* Now that we have the header check the e_magic field
595 * to see if this is a dos image.
597 if ( mz_header
.e_magic
== IMAGE_DOS_SIGNATURE
)
599 BOOL lfanewValid
= FALSE
;
600 /* We do have a DOS image so we will now try to seek into
601 * the file by the amount indicated by the field
602 * "Offset to extended header" and read in the
603 * "magic" field information at that location.
604 * This will tell us if there is more header information
607 /* But before we do we will make sure that header
608 * structure encompasses the "Offset to extended header"
611 if ( (mz_header
.e_cparhdr
<<4) >= sizeof(IMAGE_DOS_HEADER
) )
612 if ( ( mz_header
.e_crlc
== 0 ) ||
613 ( mz_header
.e_lfarlc
>= sizeof(IMAGE_DOS_HEADER
) ) )
614 if ( mz_header
.e_lfanew
>= sizeof(IMAGE_DOS_HEADER
)
615 && SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
616 && ReadFile( hfile
, magic
, sizeof(magic
), &len
, NULL
)
617 && len
== sizeof(magic
) )
622 /* If we cannot read this "extended header" we will
623 * assume that we have a simple DOS executable.
625 *lpBinaryType
= SCS_DOS_BINARY
;
630 /* Reading the magic field succeeded so
631 * we will try to determine what type it is.
633 if ( *(DWORD
*)magic
== IMAGE_NT_SIGNATURE
)
635 /* This is an NT signature.
637 *lpBinaryType
= SCS_32BIT_BINARY
;
640 else if ( *(WORD
*)magic
== IMAGE_OS2_SIGNATURE
)
642 /* The IMAGE_OS2_SIGNATURE indicates that the
643 * "extended header is a Windows executable (NE)
644 * header." This can mean either a 16-bit OS/2
645 * or a 16-bit Windows or even a DOS program
646 * (running under a DOS extender). To decide
647 * which, we'll have to read the NE header.
651 if ( SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
652 && ReadFile( hfile
, &ne
, sizeof(ne
), &len
, NULL
)
653 && len
== sizeof(ne
) )
655 switch ( ne
.ne_exetyp
)
657 case 2: *lpBinaryType
= SCS_WOW_BINARY
; return TRUE
;
658 case 5: *lpBinaryType
= SCS_DOS_BINARY
; return TRUE
;
659 default: *lpBinaryType
=
660 MODULE_Decide_OS2_OldWin(hfile
, &mz_header
, &ne
);
664 /* Couldn't read header, so abort. */
669 /* Unknown extended header, but this file is nonetheless
672 *lpBinaryType
= SCS_DOS_BINARY
;
679 /* If we get here, we don't even have a correct MZ header.
680 * Try to check the file extension for known types ...
682 ptr
= strrchr( filename
, '.' );
683 if ( ptr
&& !strchr( ptr
, '\\' ) && !strchr( ptr
, '/' ) )
685 if ( !FILE_strcasecmp( ptr
, ".COM" ) )
687 *lpBinaryType
= SCS_DOS_BINARY
;
691 if ( !FILE_strcasecmp( ptr
, ".PIF" ) )
693 *lpBinaryType
= SCS_PIF_BINARY
;
701 /***********************************************************************
702 * GetBinaryTypeA [KERNEL32.@]
703 * GetBinaryType [KERNEL32.@]
705 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
710 TRACE_(win32
)("%s\n", lpApplicationName
);
714 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
717 /* Open the file indicated by lpApplicationName for reading.
719 hfile
= CreateFileA( lpApplicationName
, GENERIC_READ
, FILE_SHARE_READ
,
720 NULL
, OPEN_EXISTING
, 0, 0 );
721 if ( hfile
== INVALID_HANDLE_VALUE
)
726 ret
= MODULE_GetBinaryType( hfile
, lpApplicationName
, lpBinaryType
);
730 CloseHandle( hfile
);
735 /***********************************************************************
736 * GetBinaryTypeW [KERNEL32.@]
738 BOOL WINAPI
GetBinaryTypeW( LPCWSTR lpApplicationName
, LPDWORD lpBinaryType
)
743 TRACE_(win32
)("%s\n", debugstr_w(lpApplicationName
) );
747 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
750 /* Convert the wide string to a ascii string.
752 strNew
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName
);
754 if ( strNew
!= NULL
)
756 ret
= GetBinaryTypeA( strNew
, lpBinaryType
);
758 /* Free the allocated string.
760 HeapFree( GetProcessHeap(), 0, strNew
);
767 /***********************************************************************
768 * WinExec (KERNEL.166)
769 * WinExec16 (KERNEL32.@)
771 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
773 LPCSTR p
, args
= NULL
;
774 LPCSTR name_beg
, name_end
;
778 char buffer
[MAX_PATH
];
780 if (*lpCmdLine
== '"') /* has to be only one and only at beginning ! */
782 name_beg
= lpCmdLine
+1;
783 p
= strchr ( lpCmdLine
+1, '"' );
787 args
= strchr ( p
, ' ' );
789 else /* yes, even valid with trailing '"' missing */
790 name_end
= lpCmdLine
+strlen(lpCmdLine
);
794 name_beg
= lpCmdLine
;
795 args
= strchr( lpCmdLine
, ' ' );
796 name_end
= args
? args
: lpCmdLine
+strlen(lpCmdLine
);
799 if ((name_beg
== lpCmdLine
) && (!args
))
800 { /* just use the original cmdline string as file name */
801 name
= (LPSTR
)lpCmdLine
;
805 if (!(name
= HeapAlloc( GetProcessHeap(), 0, name_end
- name_beg
+ 1 )))
806 return ERROR_NOT_ENOUGH_MEMORY
;
807 memcpy( name
, name_beg
, name_end
- name_beg
);
808 name
[name_end
- name_beg
] = '\0';
814 arglen
= strlen(args
);
815 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 + arglen
);
816 cmdline
[0] = (BYTE
)arglen
;
817 strcpy( cmdline
+ 1, args
);
821 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 );
822 cmdline
[0] = cmdline
[1] = 0;
825 TRACE("name: '%s', cmdline: '%.*s'\n", name
, cmdline
[0], &cmdline
[1]);
827 if (SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, NULL
))
832 showCmd
[1] = nCmdShow
;
834 params
.hEnvironment
= 0;
835 params
.cmdLine
= MapLS( cmdline
);
836 params
.showCmd
= MapLS( showCmd
);
839 ret
= LoadModule16( buffer
, ¶ms
);
840 UnMapLS( params
.cmdLine
);
841 UnMapLS( params
.showCmd
);
843 else ret
= GetLastError();
845 HeapFree( GetProcessHeap(), 0, cmdline
);
846 if (name
!= lpCmdLine
) HeapFree( GetProcessHeap(), 0, name
);
848 if (ret
== 21) /* 32-bit module */
851 ReleaseThunkLock( &count
);
852 ret
= LOWORD( WinExec( lpCmdLine
, nCmdShow
) );
853 RestoreThunkLock( count
);
858 /***********************************************************************
859 * WinExec (KERNEL32.@)
861 HINSTANCE WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
863 PROCESS_INFORMATION info
;
864 STARTUPINFOA startup
;
868 memset( &startup
, 0, sizeof(startup
) );
869 startup
.cb
= sizeof(startup
);
870 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
871 startup
.wShowWindow
= nCmdShow
;
873 /* cmdline needs to be writeable for CreateProcess */
874 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine
)+1 ))) return 0;
875 strcpy( cmdline
, lpCmdLine
);
877 if (CreateProcessA( NULL
, cmdline
, NULL
, NULL
, FALSE
,
878 0, NULL
, NULL
, &startup
, &info
))
880 /* Give 30 seconds to the app to come up */
881 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF)
882 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
883 hInstance
= (HINSTANCE
)33;
884 /* Close off the handles */
885 CloseHandle( info
.hThread
);
886 CloseHandle( info
.hProcess
);
888 else if ((hInstance
= (HINSTANCE
)GetLastError()) >= (HINSTANCE
)32)
890 FIXME("Strange error set by CreateProcess: %d\n", hInstance
);
891 hInstance
= (HINSTANCE
)11;
893 HeapFree( GetProcessHeap(), 0, cmdline
);
897 /**********************************************************************
898 * LoadModule (KERNEL32.@)
900 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
902 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
903 PROCESS_INFORMATION info
;
904 STARTUPINFOA startup
;
907 char filename
[MAX_PATH
];
910 if (!name
) return (HINSTANCE
)ERROR_FILE_NOT_FOUND
;
912 if (!SearchPathA( NULL
, name
, ".exe", sizeof(filename
), filename
, NULL
) &&
913 !SearchPathA( NULL
, name
, NULL
, sizeof(filename
), filename
, NULL
))
914 return (HINSTANCE
)GetLastError();
916 len
= (BYTE
)params
->lpCmdLine
[0];
917 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + len
+ 2 )))
918 return (HINSTANCE
)ERROR_NOT_ENOUGH_MEMORY
;
920 strcpy( cmdline
, filename
);
921 p
= cmdline
+ strlen(cmdline
);
923 memcpy( p
, params
->lpCmdLine
+ 1, len
);
926 memset( &startup
, 0, sizeof(startup
) );
927 startup
.cb
= sizeof(startup
);
928 if (params
->lpCmdShow
)
930 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
931 startup
.wShowWindow
= params
->lpCmdShow
[1];
934 if (CreateProcessA( filename
, cmdline
, NULL
, NULL
, FALSE
, 0,
935 params
->lpEnvAddress
, NULL
, &startup
, &info
))
937 /* Give 30 seconds to the app to come up */
938 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF )
939 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
940 hInstance
= (HINSTANCE
)33;
941 /* Close off the handles */
942 CloseHandle( info
.hThread
);
943 CloseHandle( info
.hProcess
);
945 else if ((hInstance
= (HINSTANCE
)GetLastError()) >= (HINSTANCE
)32)
947 FIXME("Strange error set by CreateProcess: %d\n", hInstance
);
948 hInstance
= (HINSTANCE
)11;
951 HeapFree( GetProcessHeap(), 0, cmdline
);
956 /*************************************************************************
959 * Helper for CreateProcess: retrieve the file name to load from the
960 * app name and command line. Store the file name in buffer, and
961 * return a possibly modified command line.
963 static LPSTR
get_file_name( LPCSTR appname
, LPSTR cmdline
, LPSTR buffer
, int buflen
)
965 char *name
, *pos
, *ret
= NULL
;
968 /* if we have an app name, everything is easy */
972 /* use the unmodified app name as file name */
973 lstrcpynA( buffer
, appname
, buflen
);
974 if (!(ret
= cmdline
))
976 /* no command-line, create one */
977 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(appname
) + 3 )))
978 sprintf( ret
, "\"%s\"", appname
);
985 SetLastError( ERROR_INVALID_PARAMETER
);
989 /* first check for a quoted file name */
991 if ((cmdline
[0] == '"') && ((p
= strchr( cmdline
+ 1, '"' ))))
993 int len
= p
- cmdline
- 1;
994 /* extract the quoted portion as file name */
995 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 ))) return NULL
;
996 memcpy( name
, cmdline
+ 1, len
);
999 if (SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) ||
1000 SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
1001 ret
= cmdline
; /* no change necessary */
1005 /* now try the command-line word by word */
1007 if (!(name
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 1 ))) return NULL
;
1013 do *pos
++ = *p
++; while (*p
&& *p
!= ' ');
1015 TRACE("trying '%s'\n", name
);
1016 if (SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) ||
1017 SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
1024 if (!ret
|| !strchr( name
, ' ' )) goto done
; /* no change necessary */
1026 /* now build a new command-line with quotes */
1028 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 3 ))) goto done
;
1029 sprintf( ret
, "\"%s\"%s", name
, p
);
1032 HeapFree( GetProcessHeap(), 0, name
);
1037 /**********************************************************************
1038 * CreateProcessA (KERNEL32.@)
1040 BOOL WINAPI
CreateProcessA( LPCSTR lpApplicationName
, LPSTR lpCommandLine
,
1041 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1042 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1043 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1044 LPVOID lpEnvironment
, LPCSTR lpCurrentDirectory
,
1045 LPSTARTUPINFOA lpStartupInfo
,
1046 LPPROCESS_INFORMATION lpProcessInfo
)
1051 char name
[MAX_PATH
];
1054 /* Process the AppName and/or CmdLine to get module name and path */
1056 TRACE("app %s cmdline %s\n", debugstr_a(lpApplicationName
), debugstr_a(lpCommandLine
) );
1058 if (!(tidy_cmdline
= get_file_name( lpApplicationName
, lpCommandLine
, name
, sizeof(name
) )))
1061 /* Warn if unsupported features are used */
1063 if (dwCreationFlags
& NORMAL_PRIORITY_CLASS
)
1064 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1065 if (dwCreationFlags
& IDLE_PRIORITY_CLASS
)
1066 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1067 if (dwCreationFlags
& HIGH_PRIORITY_CLASS
)
1068 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1069 if (dwCreationFlags
& REALTIME_PRIORITY_CLASS
)
1070 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1071 if (dwCreationFlags
& CREATE_NEW_PROCESS_GROUP
)
1072 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1073 if (dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
)
1074 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1075 if (dwCreationFlags
& CREATE_SEPARATE_WOW_VDM
)
1076 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1077 if (dwCreationFlags
& CREATE_SHARED_WOW_VDM
)
1078 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1079 if (dwCreationFlags
& CREATE_DEFAULT_ERROR_MODE
)
1080 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1081 if (dwCreationFlags
& CREATE_NO_WINDOW
)
1082 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1083 if (dwCreationFlags
& PROFILE_USER
)
1084 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1085 if (dwCreationFlags
& PROFILE_KERNEL
)
1086 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1087 if (dwCreationFlags
& PROFILE_SERVER
)
1088 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1089 if (lpStartupInfo
->lpDesktop
)
1090 FIXME("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
1091 name
, debugstr_a(lpStartupInfo
->lpDesktop
));
1092 if (lpStartupInfo
->lpTitle
)
1093 FIXME("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
1094 name
, lpStartupInfo
->lpTitle
);
1095 if (lpStartupInfo
->dwFlags
& STARTF_USECOUNTCHARS
)
1096 FIXME("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
1097 name
, lpStartupInfo
->dwXCountChars
, lpStartupInfo
->dwYCountChars
);
1098 if (lpStartupInfo
->dwFlags
& STARTF_USEFILLATTRIBUTE
)
1099 FIXME("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
1100 name
, lpStartupInfo
->dwFillAttribute
);
1101 if (lpStartupInfo
->dwFlags
& STARTF_RUNFULLSCREEN
)
1102 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1103 if (lpStartupInfo
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1104 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1105 if (lpStartupInfo
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1106 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1107 if (lpStartupInfo
->dwFlags
& STARTF_USEHOTKEY
)
1108 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1110 /* Open file and determine executable type */
1112 hFile
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1113 if (hFile
== INVALID_HANDLE_VALUE
) goto done
;
1115 if ( !MODULE_GetBinaryType( hFile
, name
, &type
) )
1117 CloseHandle( hFile
);
1118 retv
= PROCESS_Create( 0, name
, tidy_cmdline
, lpEnvironment
,
1119 lpProcessAttributes
, lpThreadAttributes
,
1120 bInheritHandles
, dwCreationFlags
,
1121 lpStartupInfo
, lpProcessInfo
, lpCurrentDirectory
);
1125 /* Create process */
1129 case SCS_32BIT_BINARY
:
1130 case SCS_WOW_BINARY
:
1131 case SCS_DOS_BINARY
:
1132 retv
= PROCESS_Create( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1133 lpProcessAttributes
, lpThreadAttributes
,
1134 bInheritHandles
, dwCreationFlags
,
1135 lpStartupInfo
, lpProcessInfo
, lpCurrentDirectory
);
1138 case SCS_PIF_BINARY
:
1139 case SCS_POSIX_BINARY
:
1140 case SCS_OS216_BINARY
:
1141 FIXME("Unsupported executable type: %ld\n", type
);
1145 SetLastError( ERROR_BAD_FORMAT
);
1148 CloseHandle( hFile
);
1151 if (tidy_cmdline
!= lpCommandLine
) HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1155 /**********************************************************************
1156 * CreateProcessW (KERNEL32.@)
1158 * lpReserved is not converted
1160 BOOL WINAPI
CreateProcessW( LPCWSTR lpApplicationName
, LPWSTR lpCommandLine
,
1161 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1162 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1163 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1164 LPVOID lpEnvironment
, LPCWSTR lpCurrentDirectory
,
1165 LPSTARTUPINFOW lpStartupInfo
,
1166 LPPROCESS_INFORMATION lpProcessInfo
)
1168 STARTUPINFOA StartupInfoA
;
1170 LPSTR lpApplicationNameA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName
);
1171 LPSTR lpCommandLineA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine
);
1172 LPSTR lpCurrentDirectoryA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory
);
1174 memcpy (&StartupInfoA
, lpStartupInfo
, sizeof(STARTUPINFOA
));
1175 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpDesktop
);
1176 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpTitle
);
1178 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(lpApplicationName
), debugstr_w(lpCommandLine
));
1180 if (lpStartupInfo
->lpReserved
)
1181 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo
->lpReserved
));
1183 ret
= CreateProcessA( lpApplicationNameA
, lpCommandLineA
,
1184 lpProcessAttributes
, lpThreadAttributes
,
1185 bInheritHandles
, dwCreationFlags
,
1186 lpEnvironment
, lpCurrentDirectoryA
,
1187 &StartupInfoA
, lpProcessInfo
);
1189 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA
);
1190 HeapFree( GetProcessHeap(), 0, lpCommandLineA
);
1191 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1192 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1197 /***********************************************************************
1198 * GetModuleHandleA (KERNEL32.@)
1199 * GetModuleHandle32 (KERNEL.488)
1201 HMODULE WINAPI
GetModuleHandleA(LPCSTR module
)
1205 if ( module
== NULL
)
1208 wm
= MODULE_FindModule( module
);
1210 return wm
? wm
->module
: 0;
1213 /***********************************************************************
1214 * GetModuleHandleW (KERNEL32.@)
1216 HMODULE WINAPI
GetModuleHandleW(LPCWSTR module
)
1219 LPSTR modulea
= HEAP_strdupWtoA( GetProcessHeap(), 0, module
);
1220 hModule
= GetModuleHandleA( modulea
);
1221 HeapFree( GetProcessHeap(), 0, modulea
);
1226 /***********************************************************************
1227 * GetModuleFileNameA (KERNEL32.@)
1228 * GetModuleFileName32 (KERNEL.487)
1230 * GetModuleFileNameA seems to *always* return the long path;
1231 * it's only GetModuleFileName16 that decides between short/long path
1232 * by checking if exe version >= 4.0.
1233 * (SDK docu doesn't mention this)
1235 DWORD WINAPI
GetModuleFileNameA(
1236 HMODULE hModule
, /* [in] module handle (32bit) */
1237 LPSTR lpFileName
, /* [out] filenamebuffer */
1238 DWORD size
) /* [in] size of filenamebuffer */
1242 RtlEnterCriticalSection( &loader_section
);
1245 if ((wm
= MODULE32_LookupHMODULE( hModule
)))
1246 lstrcpynA( lpFileName
, wm
->filename
, size
);
1248 RtlLeaveCriticalSection( &loader_section
);
1249 TRACE("%s\n", lpFileName
);
1250 return strlen(lpFileName
);
1254 /***********************************************************************
1255 * GetModuleFileNameW (KERNEL32.@)
1257 DWORD WINAPI
GetModuleFileNameW( HMODULE hModule
, LPWSTR lpFileName
,
1260 LPSTR fnA
= (char*)HeapAlloc( GetProcessHeap(), 0, size
);
1261 DWORD res
= GetModuleFileNameA( hModule
, fnA
, size
);
1262 if (size
> 0 && !MultiByteToWideChar( CP_ACP
, 0, fnA
, -1, lpFileName
, size
))
1263 lpFileName
[size
-1] = 0;
1264 HeapFree( GetProcessHeap(), 0, fnA
);
1269 /***********************************************************************
1270 * LoadLibraryExA (KERNEL32.@)
1272 HMODULE WINAPI
LoadLibraryExA(LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1278 SetLastError(ERROR_INVALID_PARAMETER
);
1282 if (flags
& LOAD_LIBRARY_AS_DATAFILE
)
1287 /* This method allows searching for the 'native' libraries only */
1288 if (SearchPathA( NULL
, libname
, ".dll", sizeof(filename
), filename
, NULL
))
1290 /* FIXME: maybe we should use the hfile parameter instead */
1291 HANDLE hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
1292 NULL
, OPEN_EXISTING
, 0, 0 );
1293 if (hFile
!= INVALID_HANDLE_VALUE
)
1296 MODULE_GetBinaryType( hFile
, filename
, &type
);
1297 if (type
== SCS_32BIT_BINARY
)
1299 HANDLE mapping
= CreateFileMappingA( hFile
, NULL
, PAGE_READONLY
,
1303 hmod
= (HMODULE
)MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
1304 CloseHandle( mapping
);
1307 CloseHandle( hFile
);
1309 if (hmod
) return (HMODULE
)((ULONG_PTR
)hmod
+ 1);
1311 flags
|= DONT_RESOLVE_DLL_REFERENCES
; /* Just in case */
1312 /* Fallback to normal behaviour */
1315 RtlEnterCriticalSection( &loader_section
);
1317 wm
= MODULE_LoadLibraryExA( libname
, hfile
, flags
);
1320 if ( !MODULE_DllProcessAttach( wm
, NULL
) )
1322 WARN_(module
)("Attach failed for module '%s', \n", libname
);
1323 MODULE_FreeLibrary(wm
);
1324 SetLastError(ERROR_DLL_INIT_FAILED
);
1329 RtlLeaveCriticalSection( &loader_section
);
1330 return wm
? wm
->module
: 0;
1333 /***********************************************************************
1336 * helper for MODULE_LoadLibraryExA. Allocate space to hold the directory
1337 * portion of the provided name and put the name in it.
1340 static LPCSTR
allocate_lib_dir(LPCSTR libname
)
1347 if ((p
= strrchr( pmax
, '\\' ))) pmax
= p
+ 1;
1348 if ((p
= strrchr( pmax
, '/' ))) pmax
= p
+ 1; /* Naughty. MSDN says don't */
1349 if (pmax
== libname
&& pmax
[0] && pmax
[1] == ':') pmax
+= 2;
1351 length
= pmax
- libname
;
1353 result
= HeapAlloc (GetProcessHeap(), 0, length
+1);
1357 strncpy (result
, libname
, length
);
1358 result
[length
] = '\0';
1364 /***********************************************************************
1365 * MODULE_LoadLibraryExA (internal)
1367 * Load a PE style module according to the load order.
1369 * The HFILE parameter is not used and marked reserved in the SDK. I can
1370 * only guess that it should force a file to be mapped, but I rather
1371 * ignore the parameter because it would be extremely difficult to
1372 * integrate this with different types of module represenations.
1374 * libdir is used to support LOAD_WITH_ALTERED_SEARCH_PATH during the recursion
1375 * on this function. When first called from LoadLibraryExA it will be
1376 * NULL but thereafter it may point to a buffer containing the path
1377 * portion of the library name. Note that the recursion all occurs
1378 * within a Critical section (see LoadLibraryExA) so the use of a
1379 * static is acceptable.
1380 * (We have to use a static variable at some point anyway, to pass the
1381 * information from BUILTIN32_dlopen through dlopen and the builtin's
1382 * init function into load_library).
1383 * allocated_libdir is TRUE in the stack frame that allocated libdir
1385 WINE_MODREF
*MODULE_LoadLibraryExA( LPCSTR libname
, HFILE hfile
, DWORD flags
)
1387 DWORD err
= GetLastError();
1390 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
1392 const char *filetype
= "";
1394 BOOL allocated_libdir
= FALSE
;
1395 static LPCSTR libdir
= NULL
; /* See above */
1397 if ( !libname
) return NULL
;
1399 filename
= HeapAlloc ( GetProcessHeap(), 0, MAX_PATH
+ 1 );
1400 if ( !filename
) return NULL
;
1401 *filename
= 0; /* Just in case we don't set it before goto error */
1403 RtlEnterCriticalSection( &loader_section
);
1405 if ((flags
& LOAD_WITH_ALTERED_SEARCH_PATH
) && FILE_contains_path(libname
))
1407 if (!(libdir
= allocate_lib_dir(libname
))) goto error
;
1408 allocated_libdir
= TRUE
;
1411 if (!libdir
|| allocated_libdir
)
1412 found
= SearchPathA(NULL
, libname
, ".dll", MAX_PATH
, filename
, NULL
);
1414 found
= DIR_SearchAlternatePath(libdir
, libname
, ".dll", MAX_PATH
, filename
, NULL
);
1416 /* build the modules filename */
1419 if ( ! GetSystemDirectoryA ( filename
, MAX_PATH
) )
1422 /* if the library name contains a path and can not be found, return an error.
1423 exception: if the path is the system directory, proceed, so that modules,
1424 which are not PE-modules can be loaded
1426 if the library name does not contain a path and can not be found, assume the
1427 system directory is meant */
1429 if ( ! FILE_strncasecmp ( filename
, libname
, strlen ( filename
) ))
1430 strcpy ( filename
, libname
);
1433 if (FILE_contains_path(libname
)) goto error
;
1434 strcat ( filename
, "\\" );
1435 strcat ( filename
, libname
);
1438 /* if the filename doesn't have an extension append .DLL */
1439 if (!(p
= strrchr( filename
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
1440 strcat( filename
, ".dll" );
1443 /* Check for already loaded module */
1444 if (!(pwm
= MODULE_FindModule(filename
)) && !FILE_contains_path(libname
))
1446 LPSTR fn
= HeapAlloc ( GetProcessHeap(), 0, MAX_PATH
+ 1 );
1449 /* since the default loading mechanism uses a more detailed algorithm
1450 * than SearchPath (like using PATH, which can even be modified between
1451 * two attempts of loading the same DLL), the look-up above (with
1452 * SearchPath) can have put the file in system directory, whereas it
1453 * has already been loaded but with a different path. So do a specific
1454 * look-up with filename (without any path)
1456 strcpy ( fn
, libname
);
1457 /* if the filename doesn't have an extension append .DLL */
1458 if (!strrchr( fn
, '.')) strcat( fn
, ".dll" );
1459 if ((pwm
= MODULE_FindModule( fn
)) != NULL
)
1460 strcpy( filename
, fn
);
1461 HeapFree( GetProcessHeap(), 0, fn
);
1466 if(!(pwm
->flags
& WINE_MODREF_MARKER
))
1469 if ((pwm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
) &&
1470 !(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1472 pwm
->flags
&= ~WINE_MODREF_DONT_RESOLVE_REFS
;
1473 PE_fixup_imports( pwm
);
1475 TRACE("Already loaded module '%s' at 0x%08x, count=%d\n", filename
, pwm
->module
, pwm
->refCount
);
1476 if (allocated_libdir
)
1478 HeapFree ( GetProcessHeap(), 0, (LPSTR
)libdir
);
1481 RtlLeaveCriticalSection( &loader_section
);
1482 HeapFree ( GetProcessHeap(), 0, filename
);
1486 MODULE_GetLoadOrder( loadorder
, filename
, TRUE
);
1488 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
1490 if (loadorder
[i
] == LOADORDER_INVALID
) break;
1491 SetLastError( ERROR_FILE_NOT_FOUND
);
1493 switch(loadorder
[i
])
1496 TRACE("Trying native dll '%s'\n", filename
);
1497 pwm
= PE_LoadLibraryExA(filename
, flags
);
1498 filetype
= "native";
1502 TRACE("Trying so-library '%s'\n", filename
);
1503 pwm
= ELF_LoadLibraryExA(filename
, flags
);
1508 TRACE("Trying built-in '%s'\n", filename
);
1509 pwm
= BUILTIN32_LoadLibraryExA(filename
, flags
);
1510 filetype
= "builtin";
1520 /* Initialize DLL just loaded */
1521 TRACE("Loaded module '%s' at 0x%08x\n", filename
, pwm
->module
);
1522 if (!TRACE_ON(module
))
1523 TRACE_(loaddll
)("Loaded module '%s' : %s\n", filename
, filetype
);
1524 /* Set the refCount here so that an attach failure will */
1525 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1528 if (allocated_libdir
)
1530 HeapFree ( GetProcessHeap(), 0, (LPSTR
)libdir
);
1533 RtlLeaveCriticalSection( &loader_section
);
1534 SetLastError( err
); /* restore last error */
1535 HeapFree ( GetProcessHeap(), 0, filename
);
1539 if(GetLastError() != ERROR_FILE_NOT_FOUND
)
1544 if (allocated_libdir
)
1546 HeapFree ( GetProcessHeap(), 0, (LPSTR
)libdir
);
1549 RtlLeaveCriticalSection( &loader_section
);
1550 WARN("Failed to load module '%s'; error=0x%08lx\n", filename
, GetLastError());
1551 HeapFree ( GetProcessHeap(), 0, filename
);
1555 /***********************************************************************
1556 * LoadLibraryA (KERNEL32.@)
1558 HMODULE WINAPI
LoadLibraryA(LPCSTR libname
) {
1559 return LoadLibraryExA(libname
,0,0);
1562 /***********************************************************************
1563 * LoadLibraryW (KERNEL32.@)
1565 HMODULE WINAPI
LoadLibraryW(LPCWSTR libnameW
)
1567 return LoadLibraryExW(libnameW
,0,0);
1570 /***********************************************************************
1571 * LoadLibrary32 (KERNEL.452)
1572 * LoadSystemLibrary32 (KERNEL.482)
1574 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
1579 ReleaseThunkLock( &count
);
1580 hModule
= LoadLibraryA( libname
);
1581 RestoreThunkLock( count
);
1585 /***********************************************************************
1586 * LoadLibraryExW (KERNEL32.@)
1588 HMODULE WINAPI
LoadLibraryExW(LPCWSTR libnameW
,HANDLE hfile
,DWORD flags
)
1590 LPSTR libnameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW
);
1591 HMODULE ret
= LoadLibraryExA( libnameA
, hfile
, flags
);
1593 HeapFree( GetProcessHeap(), 0, libnameA
);
1597 /***********************************************************************
1598 * MODULE_FlushModrefs
1600 * NOTE: Assumes that the process critical section is held!
1602 * Remove all unused modrefs and call the internal unloading routines
1603 * for the library type.
1605 static void MODULE_FlushModrefs(void)
1607 WINE_MODREF
*wm
, *next
;
1609 for(wm
= MODULE_modref_list
; wm
; wm
= next
)
1616 /* Unlink this modref from the chain */
1618 wm
->next
->prev
= wm
->prev
;
1620 wm
->prev
->next
= wm
->next
;
1621 if(wm
== MODULE_modref_list
)
1622 MODULE_modref_list
= wm
->next
;
1624 TRACE(" unloading %s\n", wm
->filename
);
1625 if (wm
->dlhandle
) wine_dll_unload( wm
->dlhandle
);
1626 else UnmapViewOfFile( (LPVOID
)wm
->module
);
1627 FreeLibrary16(wm
->hDummyMod
);
1628 HeapFree( GetProcessHeap(), 0, wm
->deps
);
1629 HeapFree( GetProcessHeap(), 0, wm
);
1633 /***********************************************************************
1634 * FreeLibrary (KERNEL32.@)
1635 * FreeLibrary32 (KERNEL.486)
1637 BOOL WINAPI
FreeLibrary(HINSTANCE hLibModule
)
1644 SetLastError( ERROR_INVALID_HANDLE
);
1648 if ((ULONG_PTR
)hLibModule
& 1)
1650 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
1651 char *ptr
= (char *)hLibModule
- 1;
1652 UnmapViewOfFile( ptr
);
1656 RtlEnterCriticalSection( &loader_section
);
1659 if ((wm
= MODULE32_LookupHMODULE( hLibModule
))) retv
= MODULE_FreeLibrary( wm
);
1662 RtlLeaveCriticalSection( &loader_section
);
1667 /***********************************************************************
1668 * MODULE_DecRefCount
1670 * NOTE: Assumes that the process critical section is held!
1672 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
1676 if ( wm
->flags
& WINE_MODREF_MARKER
)
1679 if ( wm
->refCount
<= 0 )
1683 TRACE("(%s) refCount: %d\n", wm
->modname
, wm
->refCount
);
1685 if ( wm
->refCount
== 0 )
1687 wm
->flags
|= WINE_MODREF_MARKER
;
1689 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1691 MODULE_DecRefCount( wm
->deps
[i
] );
1693 wm
->flags
&= ~WINE_MODREF_MARKER
;
1697 /***********************************************************************
1698 * MODULE_FreeLibrary
1700 * NOTE: Assumes that the process critical section is held!
1702 BOOL
MODULE_FreeLibrary( WINE_MODREF
*wm
)
1704 TRACE("(%s) - START\n", wm
->modname
);
1706 /* Recursively decrement reference counts */
1707 MODULE_DecRefCount( wm
);
1709 /* Call process detach notifications */
1710 if ( free_lib_count
<= 1 )
1712 MODULE_DllProcessDetach( FALSE
, NULL
);
1713 SERVER_START_REQ( unload_dll
)
1715 req
->base
= (void *)wm
->module
;
1716 wine_server_call( req
);
1719 MODULE_FlushModrefs();
1728 /***********************************************************************
1729 * FreeLibraryAndExitThread (KERNEL32.@)
1731 VOID WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
1733 FreeLibrary(hLibModule
);
1734 ExitThread(dwExitCode
);
1737 /***********************************************************************
1738 * PrivateLoadLibrary (KERNEL32.@)
1740 * FIXME: rough guesswork, don't know what "Private" means
1742 HINSTANCE16 WINAPI
PrivateLoadLibrary(LPCSTR libname
)
1744 return LoadLibrary16(libname
);
1749 /***********************************************************************
1750 * PrivateFreeLibrary (KERNEL32.@)
1752 * FIXME: rough guesswork, don't know what "Private" means
1754 void WINAPI
PrivateFreeLibrary(HINSTANCE16 handle
)
1756 FreeLibrary16(handle
);
1760 /***********************************************************************
1761 * GetProcAddress16 (KERNEL32.37)
1762 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1764 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1767 WARN("hModule may not be 0!\n");
1768 return (FARPROC16
)0;
1770 if (HIWORD(hModule
))
1772 WARN("hModule is Win32 handle (%08x)\n", hModule
);
1773 return (FARPROC16
)0;
1775 return GetProcAddress16( LOWORD(hModule
), name
);
1778 /***********************************************************************
1779 * GetProcAddress (KERNEL.50)
1781 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, LPCSTR name
)
1786 if (!hModule
) hModule
= GetCurrentTask();
1787 hModule
= GetExePtr( hModule
);
1789 if (HIWORD(name
) != 0)
1791 ordinal
= NE_GetOrdinal( hModule
, name
);
1792 TRACE("%04x '%s'\n", hModule
, name
);
1796 ordinal
= LOWORD(name
);
1797 TRACE("%04x %04x\n", hModule
, ordinal
);
1799 if (!ordinal
) return (FARPROC16
)0;
1801 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1803 TRACE("returning %08x\n", (UINT
)ret
);
1808 /***********************************************************************
1809 * GetProcAddress (KERNEL32.@)
1811 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
1813 return MODULE_GetProcAddress( hModule
, function
, TRUE
);
1816 /***********************************************************************
1817 * GetProcAddress32 (KERNEL.453)
1819 FARPROC WINAPI
GetProcAddress32_16( HMODULE hModule
, LPCSTR function
)
1821 return MODULE_GetProcAddress( hModule
, function
, FALSE
);
1824 /***********************************************************************
1825 * MODULE_GetProcAddress (internal)
1827 FARPROC
MODULE_GetProcAddress(
1828 HMODULE hModule
, /* [in] current module handle */
1829 LPCSTR function
, /* [in] function to be looked up */
1833 FARPROC retproc
= 0;
1835 if (HIWORD(function
))
1836 TRACE_(win32
)("(%08lx,%s)\n",(DWORD
)hModule
,function
);
1838 TRACE_(win32
)("(%08lx,%p)\n",(DWORD
)hModule
,function
);
1840 RtlEnterCriticalSection( &loader_section
);
1841 if ((wm
= MODULE32_LookupHMODULE( hModule
)))
1843 retproc
= wm
->find_export( wm
, function
, snoop
);
1844 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1846 RtlLeaveCriticalSection( &loader_section
);
1851 /***************************************************************************
1852 * HasGPHandler (KERNEL.338)
1855 #include "pshpack1.h"
1856 typedef struct _GPHANDLERDEF
1863 #include "poppack.h"
1865 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1870 GPHANDLERDEF
*gpHandler
;
1872 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1873 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1874 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1875 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1876 && (gpHandler
= MapSL( gpPtr
)) != NULL
)
1878 while (gpHandler
->selector
)
1880 if ( SELECTOROF(address
) == gpHandler
->selector
1881 && OFFSETOF(address
) >= gpHandler
->rangeStart
1882 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1883 return MAKESEGPTR( gpHandler
->selector
, gpHandler
->handler
);