4 * Copyright 1995 Alexandre Julliard
12 #include <sys/types.h>
14 #include "wine/winbase16.h"
15 #include "wine/winestring.h"
21 #include "selectors.h"
22 #include "debugtools.h"
24 #include "loadorder.h"
28 DEFAULT_DEBUG_CHANNEL(module
);
29 DECLARE_DEBUG_CHANNEL(win32
);
32 /*************************************************************************
33 * MODULE32_LookupHMODULE
34 * looks for the referenced HMODULE in the current process
35 * NOTE: Assumes that the process critical section is held!
37 static WINE_MODREF
*MODULE32_LookupHMODULE( HMODULE hmod
)
42 return PROCESS_Current()->exe_modref
;
45 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod
);
46 SetLastError( ERROR_INVALID_HANDLE
);
49 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
=wm
->next
)
50 if (wm
->module
== hmod
)
52 SetLastError( ERROR_INVALID_HANDLE
);
56 /*************************************************************************
59 * Allocate a WINE_MODREF structure and add it to the process list
60 * NOTE: Assumes that the process critical section is held!
62 WINE_MODREF
*MODULE_AllocModRef( HMODULE hModule
, LPCSTR filename
)
67 if ((wm
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*wm
) )))
72 wm
->filename
= HEAP_strdupA( GetProcessHeap(), 0, filename
);
73 if ((wm
->modname
= strrchr( wm
->filename
, '\\' ))) wm
->modname
++;
74 else wm
->modname
= wm
->filename
;
76 len
= GetShortPathNameA( wm
->filename
, NULL
, 0 );
77 wm
->short_filename
= (char *)HeapAlloc( GetProcessHeap(), 0, len
+1 );
78 GetShortPathNameA( wm
->filename
, wm
->short_filename
, len
+1 );
79 if ((wm
->short_modname
= strrchr( wm
->short_filename
, '\\' ))) wm
->short_modname
++;
80 else wm
->short_modname
= wm
->short_filename
;
82 wm
->next
= PROCESS_Current()->modref_list
;
83 if (wm
->next
) wm
->next
->prev
= wm
;
84 PROCESS_Current()->modref_list
= wm
;
89 /*************************************************************************
92 static BOOL
MODULE_InitDLL( WINE_MODREF
*wm
, DWORD type
, LPVOID lpReserved
)
96 static LPCSTR typeName
[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
97 "THREAD_ATTACH", "THREAD_DETACH" };
100 /* Skip calls for modules loaded with special load flags */
102 if (wm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
) return TRUE
;
104 TRACE("(%s,%s,%p) - CALL\n", wm
->modname
, typeName
[type
], lpReserved
);
106 /* Call the initialization routine */
107 retv
= PE_InitDLL( wm
->module
, type
, lpReserved
);
109 /* The state of the module list may have changed due to the call
110 to PE_InitDLL. We cannot assume that this module has not been
112 TRACE("(%p,%s,%p) - RETURN %d\n", wm
, typeName
[type
], lpReserved
, retv
);
117 /*************************************************************************
118 * MODULE_DllProcessAttach
120 * Send the process attach notification to all DLLs the given module
121 * depends on (recursively). This is somewhat complicated due to the fact that
123 * - we have to respect the module dependencies, i.e. modules implicitly
124 * referenced by another module have to be initialized before the module
125 * itself can be initialized
127 * - the initialization routine of a DLL can itself call LoadLibrary,
128 * thereby introducing a whole new set of dependencies (even involving
129 * the 'old' modules) at any time during the whole process
131 * (Note that this routine can be recursively entered not only directly
132 * from itself, but also via LoadLibrary from one of the called initialization
135 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
136 * the process *detach* notifications to be sent in the correct order.
137 * This must not only take into account module dependencies, but also
138 * 'hidden' dependencies created by modules calling LoadLibrary in their
139 * attach notification routine.
141 * The strategy is rather simple: we move a WINE_MODREF to the head of the
142 * list after the attach notification has returned. This implies that the
143 * detach notifications are called in the reverse of the sequence the attach
144 * notifications *returned*.
146 * NOTE: Assumes that the process critical section is held!
149 BOOL
MODULE_DllProcessAttach( WINE_MODREF
*wm
, LPVOID lpReserved
)
155 /* prevent infinite recursion in case of cyclical dependencies */
156 if ( ( wm
->flags
& WINE_MODREF_MARKER
)
157 || ( wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
160 TRACE("(%s,%p) - START\n", wm
->modname
, lpReserved
);
162 /* Tag current MODREF to prevent recursive loop */
163 wm
->flags
|= WINE_MODREF_MARKER
;
165 /* Recursively attach all DLLs this one depends on */
166 for ( i
= 0; retv
&& i
< wm
->nDeps
; i
++ )
168 retv
= MODULE_DllProcessAttach( wm
->deps
[i
], lpReserved
);
170 /* Call DLL entry point */
173 retv
= MODULE_InitDLL( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
175 wm
->flags
|= WINE_MODREF_PROCESS_ATTACHED
;
178 /* Re-insert MODREF at head of list */
179 if ( retv
&& wm
->prev
)
181 wm
->prev
->next
= wm
->next
;
182 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
185 wm
->next
= PROCESS_Current()->modref_list
;
186 PROCESS_Current()->modref_list
= wm
->next
->prev
= wm
;
189 /* Remove recursion flag */
190 wm
->flags
&= ~WINE_MODREF_MARKER
;
192 TRACE("(%s,%p) - END\n", wm
->modname
, lpReserved
);
197 /*************************************************************************
198 * MODULE_DllProcessDetach
200 * Send DLL process detach notifications. See the comment about calling
201 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
202 * is set, only DLLs with zero refcount are notified.
204 void MODULE_DllProcessDetach( BOOL bForceDetach
, LPVOID lpReserved
)
208 EnterCriticalSection( &PROCESS_Current()->crit_section
);
212 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
214 /* Check whether to detach this DLL */
215 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
217 if ( wm
->refCount
> 0 && !bForceDetach
)
220 /* Call detach notification */
221 wm
->flags
&= ~WINE_MODREF_PROCESS_ATTACHED
;
222 MODULE_InitDLL( wm
, DLL_PROCESS_DETACH
, lpReserved
);
224 /* Restart at head of WINE_MODREF list, as entries might have
225 been added and/or removed while performing the call ... */
230 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
233 /*************************************************************************
234 * MODULE_DllThreadAttach
236 * Send DLL thread attach notifications. These are sent in the
237 * reverse sequence of process detach notification.
240 void MODULE_DllThreadAttach( LPVOID lpReserved
)
244 EnterCriticalSection( &PROCESS_Current()->crit_section
);
246 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
250 for ( ; wm
; wm
= wm
->prev
)
252 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
254 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
257 MODULE_InitDLL( wm
, DLL_THREAD_ATTACH
, lpReserved
);
260 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
263 /*************************************************************************
264 * MODULE_DllThreadDetach
266 * Send DLL thread detach notifications. These are sent in the
267 * same sequence as process detach notification.
270 void MODULE_DllThreadDetach( LPVOID lpReserved
)
274 EnterCriticalSection( &PROCESS_Current()->crit_section
);
276 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
278 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
280 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
283 MODULE_InitDLL( wm
, DLL_THREAD_DETACH
, lpReserved
);
286 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
289 /****************************************************************************
290 * DisableThreadLibraryCalls (KERNEL32.74)
292 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
294 BOOL WINAPI
DisableThreadLibraryCalls( HMODULE hModule
)
299 EnterCriticalSection( &PROCESS_Current()->crit_section
);
301 wm
= MODULE32_LookupHMODULE( hModule
);
305 wm
->flags
|= WINE_MODREF_NO_DLL_CALLS
;
307 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
313 /***********************************************************************
314 * MODULE_CreateDummyModule
316 * Create a dummy NE module for Win32 or Winelib.
318 HMODULE
MODULE_CreateDummyModule( LPCSTR filename
, HMODULE module32
)
322 SEGTABLEENTRY
*pSegment
;
325 const char* basename
;
329 /* Extract base filename */
330 basename
= strrchr(filename
, '\\');
331 if (!basename
) basename
= filename
;
333 len
= strlen(basename
);
334 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
336 /* Allocate module */
337 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
338 + strlen(filename
) + 1;
339 size
= sizeof(NE_MODULE
) +
340 /* loaded file info */
342 /* segment table: DS,CS */
343 2 * sizeof(SEGTABLEENTRY
) +
346 /* several empty tables */
349 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
350 if (!hModule
) return (HMODULE
)11; /* invalid exe */
352 FarSetOwner16( hModule
, hModule
);
353 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
355 /* Set all used entries */
356 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
363 pModule
->heap_size
= 0;
364 pModule
->stack_size
= 0;
365 pModule
->seg_count
= 2;
366 pModule
->modref_count
= 0;
367 pModule
->nrname_size
= 0;
368 pModule
->fileinfo
= sizeof(NE_MODULE
);
369 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
370 pModule
->self
= hModule
;
371 pModule
->module32
= module32
;
373 /* Set version and flags */
376 pModule
->expected_version
=
377 ((PE_HEADER(module32
)->OptionalHeader
.MajorSubsystemVersion
& 0xff) << 8 ) |
378 (PE_HEADER(module32
)->OptionalHeader
.MinorSubsystemVersion
& 0xff);
379 pModule
->flags
|= NE_FFLAGS_WIN32
;
380 if (PE_HEADER(module32
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
381 pModule
->flags
|= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
;
384 /* Set loaded file information */
385 ofs
= (OFSTRUCT
*)(pModule
+ 1);
386 memset( ofs
, 0, of_size
);
387 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
388 strcpy( ofs
->szPathName
, filename
);
390 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + of_size
);
391 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
394 pSegment
->flags
= NE_SEGFLAGS_DATA
;
395 pSegment
->minsize
= 0x1000;
402 pStr
= (char *)pSegment
;
403 pModule
->name_table
= (int)pStr
- (int)pModule
;
406 lstrcpynA( pStr
+1, basename
, len
+1 );
409 /* All tables zero terminated */
410 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
=
411 (int)pStr
- (int)pModule
;
413 NE_RegisterModule( pModule
);
418 /**********************************************************************
421 * Find a (loaded) win32 module depending on path
424 * the module handle if found
427 WINE_MODREF
*MODULE_FindModule(
428 LPCSTR path
/* [in] pathname of module/library to be found */
431 char dllname
[260], *p
;
433 /* Append .DLL to name if no extension present */
434 strcpy( dllname
, path
);
435 if (!(p
= strrchr( dllname
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
436 strcat( dllname
, ".DLL" );
438 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
440 if ( !strcasecmp( dllname
, wm
->modname
) )
442 if ( !strcasecmp( dllname
, wm
->filename
) )
444 if ( !strcasecmp( dllname
, wm
->short_modname
) )
446 if ( !strcasecmp( dllname
, wm
->short_filename
) )
454 /* Check whether a file is an OS/2 or a very old Windows executable
455 * by testing on import of KERNEL.
457 * FIXME: is reading the module imports the only way of discerning
458 * old Windows binaries from OS/2 ones ? At least it seems so...
460 static DWORD
MODULE_Decide_OS2_OldWin(HANDLE hfile
, IMAGE_DOS_HEADER
*mz
, IMAGE_OS2_HEADER
*ne
)
462 DWORD currpos
= SetFilePointer( hfile
, 0, NULL
, SEEK_CUR
);
463 DWORD type
= SCS_OS216_BINARY
;
464 LPWORD modtab
= NULL
;
465 LPSTR nametab
= NULL
;
469 /* read modref table */
470 if ( (SetFilePointer( hfile
, mz
->e_lfanew
+ ne
->ne_modtab
, NULL
, SEEK_SET
) == -1)
471 || (!(modtab
= HeapAlloc( GetProcessHeap(), 0, ne
->ne_cmod
*sizeof(WORD
))))
472 || (!(ReadFile(hfile
, modtab
, ne
->ne_cmod
*sizeof(WORD
), &len
, NULL
)))
473 || (len
!= ne
->ne_cmod
*sizeof(WORD
)) )
476 /* read imported names table */
477 if ( (SetFilePointer( hfile
, mz
->e_lfanew
+ ne
->ne_imptab
, NULL
, SEEK_SET
) == -1)
478 || (!(nametab
= HeapAlloc( GetProcessHeap(), 0, ne
->ne_enttab
- ne
->ne_imptab
)))
479 || (!(ReadFile(hfile
, nametab
, ne
->ne_enttab
- ne
->ne_imptab
, &len
, NULL
)))
480 || (len
!= ne
->ne_enttab
- ne
->ne_imptab
) )
483 for (i
=0; i
< ne
->ne_cmod
; i
++)
485 LPSTR module
= &nametab
[modtab
[i
]];
486 TRACE("modref: %.*s\n", module
[0], &module
[1]);
487 if (!(strncmp(&module
[1], "KERNEL", module
[0])))
488 { /* very old Windows file */
489 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
490 type
= SCS_WOW_BINARY
;
496 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
499 HeapFree( GetProcessHeap(), 0, modtab
);
500 HeapFree( GetProcessHeap(), 0, nametab
);
501 SetFilePointer( hfile
, currpos
, NULL
, SEEK_SET
); /* restore filepos */
505 /***********************************************************************
506 * MODULE_GetBinaryType
508 * The GetBinaryType function determines whether a file is executable
509 * or not and if it is it returns what type of executable it is.
510 * The type of executable is a property that determines in which
511 * subsystem an executable file runs under.
513 * Binary types returned:
514 * SCS_32BIT_BINARY: A Win32 based application
515 * SCS_DOS_BINARY: An MS-Dos based application
516 * SCS_WOW_BINARY: A Win16 based application
517 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
518 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
519 * SCS_OS216_BINARY: A 16bit OS/2 based application
521 * Returns TRUE if the file is an executable in which case
522 * the value pointed by lpBinaryType is set.
523 * Returns FALSE if the file is not an executable or if the function fails.
525 * To do so it opens the file and reads in the header information
526 * if the extended header information is not present it will
527 * assume that the file is a DOS executable.
528 * If the extended header information is present it will
529 * determine if the file is a 16 or 32 bit Windows executable
530 * by check the flags in the header.
532 * Note that .COM and .PIF files are only recognized by their
533 * file name extension; but Windows does it the same way ...
535 BOOL
MODULE_GetBinaryType( HANDLE hfile
, LPCSTR filename
, LPDWORD lpBinaryType
)
537 IMAGE_DOS_HEADER mz_header
;
541 /* Seek to the start of the file and read the DOS header information.
543 if ( SetFilePointer( hfile
, 0, NULL
, SEEK_SET
) != -1
544 && ReadFile( hfile
, &mz_header
, sizeof(mz_header
), &len
, NULL
)
545 && len
== sizeof(mz_header
) )
547 /* Now that we have the header check the e_magic field
548 * to see if this is a dos image.
550 if ( mz_header
.e_magic
== IMAGE_DOS_SIGNATURE
)
552 BOOL lfanewValid
= FALSE
;
553 /* We do have a DOS image so we will now try to seek into
554 * the file by the amount indicated by the field
555 * "Offset to extended header" and read in the
556 * "magic" field information at that location.
557 * This will tell us if there is more header information
560 /* But before we do we will make sure that header
561 * structure encompasses the "Offset to extended header"
564 if ( (mz_header
.e_cparhdr
<<4) >= sizeof(IMAGE_DOS_HEADER
) )
565 if ( ( mz_header
.e_crlc
== 0 ) ||
566 ( mz_header
.e_lfarlc
>= sizeof(IMAGE_DOS_HEADER
) ) )
567 if ( mz_header
.e_lfanew
>= sizeof(IMAGE_DOS_HEADER
)
568 && SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
569 && ReadFile( hfile
, magic
, sizeof(magic
), &len
, NULL
)
570 && len
== sizeof(magic
) )
575 /* If we cannot read this "extended header" we will
576 * assume that we have a simple DOS executable.
578 *lpBinaryType
= SCS_DOS_BINARY
;
583 /* Reading the magic field succeeded so
584 * we will try to determine what type it is.
586 if ( *(DWORD
*)magic
== IMAGE_NT_SIGNATURE
)
588 /* This is an NT signature.
590 *lpBinaryType
= SCS_32BIT_BINARY
;
593 else if ( *(WORD
*)magic
== IMAGE_OS2_SIGNATURE
)
595 /* The IMAGE_OS2_SIGNATURE indicates that the
596 * "extended header is a Windows executable (NE)
597 * header." This can mean either a 16-bit OS/2
598 * or a 16-bit Windows or even a DOS program
599 * (running under a DOS extender). To decide
600 * which, we'll have to read the NE header.
604 if ( SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
605 && ReadFile( hfile
, &ne
, sizeof(ne
), &len
, NULL
)
606 && len
== sizeof(ne
) )
608 switch ( ne
.ne_exetyp
)
610 case 2: *lpBinaryType
= SCS_WOW_BINARY
; return TRUE
;
611 case 5: *lpBinaryType
= SCS_DOS_BINARY
; return TRUE
;
612 default: *lpBinaryType
=
613 MODULE_Decide_OS2_OldWin(hfile
, &mz_header
, &ne
);
617 /* Couldn't read header, so abort. */
622 /* Unknown extended header, but this file is nonetheless
625 *lpBinaryType
= SCS_DOS_BINARY
;
632 /* If we get here, we don't even have a correct MZ header.
633 * Try to check the file extension for known types ...
635 ptr
= strrchr( filename
, '.' );
636 if ( ptr
&& !strchr( ptr
, '\\' ) && !strchr( ptr
, '/' ) )
638 if ( !strcasecmp( ptr
, ".COM" ) )
640 *lpBinaryType
= SCS_DOS_BINARY
;
644 if ( !strcasecmp( ptr
, ".PIF" ) )
646 *lpBinaryType
= SCS_PIF_BINARY
;
654 /***********************************************************************
655 * GetBinaryTypeA [KERNEL32.280]
657 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
662 TRACE_(win32
)("%s\n", lpApplicationName
);
666 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
669 /* Open the file indicated by lpApplicationName for reading.
671 hfile
= CreateFileA( lpApplicationName
, GENERIC_READ
, FILE_SHARE_READ
,
672 NULL
, OPEN_EXISTING
, 0, -1 );
673 if ( hfile
== INVALID_HANDLE_VALUE
)
678 ret
= MODULE_GetBinaryType( hfile
, lpApplicationName
, lpBinaryType
);
682 CloseHandle( hfile
);
687 /***********************************************************************
688 * GetBinaryTypeW [KERNEL32.281]
690 BOOL WINAPI
GetBinaryTypeW( LPCWSTR lpApplicationName
, LPDWORD lpBinaryType
)
695 TRACE_(win32
)("%s\n", debugstr_w(lpApplicationName
) );
699 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
702 /* Convert the wide string to a ascii string.
704 strNew
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName
);
706 if ( strNew
!= NULL
)
708 ret
= GetBinaryTypeA( strNew
, lpBinaryType
);
710 /* Free the allocated string.
712 HeapFree( GetProcessHeap(), 0, strNew
);
719 /***********************************************************************
720 * WinExec16 (KERNEL.166)
722 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
724 LPCSTR p
, args
= NULL
;
725 LPCSTR name_beg
, name_end
;
729 char buffer
[MAX_PATH
];
731 if (*lpCmdLine
== '"') /* has to be only one and only at beginning ! */
733 name_beg
= lpCmdLine
+1;
734 p
= strchr ( lpCmdLine
+1, '"' );
738 args
= strchr ( p
, ' ' );
740 else /* yes, even valid with trailing '"' missing */
741 name_end
= lpCmdLine
+strlen(lpCmdLine
);
745 name_beg
= lpCmdLine
;
746 args
= strchr( lpCmdLine
, ' ' );
747 name_end
= args
? args
: lpCmdLine
+strlen(lpCmdLine
);
750 if ((name_beg
== lpCmdLine
) && (!args
))
751 { /* just use the original cmdline string as file name */
752 name
= (LPSTR
)lpCmdLine
;
756 if (!(name
= HeapAlloc( GetProcessHeap(), 0, name_end
- name_beg
+ 1 )))
757 return ERROR_NOT_ENOUGH_MEMORY
;
758 memcpy( name
, name_beg
, name_end
- name_beg
);
759 name
[name_end
- name_beg
] = '\0';
765 arglen
= strlen(args
);
766 cmdline
= SEGPTR_ALLOC( 2 + arglen
);
767 cmdline
[0] = (BYTE
)arglen
;
768 strcpy( cmdline
+ 1, args
);
772 cmdline
= SEGPTR_ALLOC( 2 );
773 cmdline
[0] = cmdline
[1] = 0;
776 TRACE("name: '%s', cmdline: '%.*s'\n", name
, cmdline
[0], &cmdline
[1]);
778 if (SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, NULL
))
781 WORD
*showCmd
= SEGPTR_ALLOC( 2*sizeof(WORD
) );
783 showCmd
[1] = nCmdShow
;
785 params
.hEnvironment
= 0;
786 params
.cmdLine
= SEGPTR_GET(cmdline
);
787 params
.showCmd
= SEGPTR_GET(showCmd
);
790 ret
= LoadModule16( buffer
, ¶ms
);
792 SEGPTR_FREE( showCmd
);
793 SEGPTR_FREE( cmdline
);
795 else ret
= GetLastError();
797 if (name
!= lpCmdLine
) HeapFree( GetProcessHeap(), 0, name
);
799 if (ret
== 21) /* 32-bit module */
801 SYSLEVEL_ReleaseWin16Lock();
802 ret
= WinExec( lpCmdLine
, nCmdShow
);
803 SYSLEVEL_RestoreWin16Lock();
808 /***********************************************************************
809 * WinExec (KERNEL32.566)
811 HINSTANCE WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
813 PROCESS_INFORMATION info
;
814 STARTUPINFOA startup
;
818 memset( &startup
, 0, sizeof(startup
) );
819 startup
.cb
= sizeof(startup
);
820 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
821 startup
.wShowWindow
= nCmdShow
;
823 /* cmdline needs to be writeable for CreateProcess */
824 if (!(cmdline
= HEAP_strdupA( GetProcessHeap(), 0, lpCmdLine
))) return 0;
826 if (CreateProcessA( NULL
, cmdline
, NULL
, NULL
, FALSE
,
827 0, NULL
, NULL
, &startup
, &info
))
829 /* Give 30 seconds to the app to come up */
830 if (Callout
.WaitForInputIdle ( info
.hProcess
, 30000 ) == 0xFFFFFFFF)
831 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
833 /* Close off the handles */
834 CloseHandle( info
.hThread
);
835 CloseHandle( info
.hProcess
);
837 else if ((hInstance
= GetLastError()) >= 32)
839 FIXME("Strange error set by CreateProcess: %d\n", hInstance
);
842 HeapFree( GetProcessHeap(), 0, cmdline
);
846 /**********************************************************************
847 * LoadModule (KERNEL32.499)
849 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
851 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
852 PROCESS_INFORMATION info
;
853 STARTUPINFOA startup
;
856 char filename
[MAX_PATH
];
859 if (!name
) return ERROR_FILE_NOT_FOUND
;
861 if (!SearchPathA( NULL
, name
, ".exe", sizeof(filename
), filename
, NULL
) &&
862 !SearchPathA( NULL
, name
, NULL
, sizeof(filename
), filename
, NULL
))
863 return GetLastError();
865 len
= (BYTE
)params
->lpCmdLine
[0];
866 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + len
+ 2 )))
867 return ERROR_NOT_ENOUGH_MEMORY
;
869 strcpy( cmdline
, filename
);
870 p
= cmdline
+ strlen(cmdline
);
872 memcpy( p
, params
->lpCmdLine
+ 1, len
);
875 memset( &startup
, 0, sizeof(startup
) );
876 startup
.cb
= sizeof(startup
);
877 if (params
->lpCmdShow
)
879 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
880 startup
.wShowWindow
= params
->lpCmdShow
[1];
883 if (CreateProcessA( filename
, cmdline
, NULL
, NULL
, FALSE
, 0,
884 params
->lpEnvAddress
, NULL
, &startup
, &info
))
886 /* Give 30 seconds to the app to come up */
887 if ( Callout
.WaitForInputIdle ( info
.hProcess
, 30000 ) == 0xFFFFFFFF )
888 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
890 /* Close off the handles */
891 CloseHandle( info
.hThread
);
892 CloseHandle( info
.hProcess
);
894 else if ((hInstance
= GetLastError()) >= 32)
896 FIXME("Strange error set by CreateProcess: %d\n", hInstance
);
900 HeapFree( GetProcessHeap(), 0, cmdline
);
905 /*************************************************************************
908 * Helper for CreateProcess: retrieve the file name to load from the
909 * app name and command line. Store the file name in buffer, and
910 * return a possibly modified command line.
912 static LPSTR
get_file_name( LPCSTR appname
, LPSTR cmdline
, LPSTR buffer
, int buflen
)
914 char *name
, *pos
, *ret
= NULL
;
917 /* if we have an app name, everything is easy */
921 /* use the unmodified app name as file name */
922 lstrcpynA( buffer
, appname
, buflen
);
923 if (!(ret
= cmdline
))
925 /* no command-line, create one */
926 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(appname
) + 3 )))
927 sprintf( ret
, "\"%s\"", appname
);
934 SetLastError( ERROR_INVALID_PARAMETER
);
938 /* first check for a quoted file name */
940 if ((cmdline
[0] == '"') && ((p
= strchr( cmdline
+ 1, '"' ))))
942 int len
= p
- cmdline
- 1;
943 /* extract the quoted portion as file name */
944 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 ))) return NULL
;
945 memcpy( name
, cmdline
+ 1, len
);
948 if (SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) ||
949 SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
950 ret
= cmdline
; /* no change necessary */
954 /* now try the command-line word by word */
956 if (!(name
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 1 ))) return NULL
;
962 do *pos
++ = *p
++; while (*p
&& *p
!= ' ');
964 TRACE("trying '%s'\n", name
);
965 if (SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) ||
966 SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
973 if (!ret
|| !strchr( name
, ' ' )) goto done
; /* no change necessary */
975 /* now build a new command-line with quotes */
977 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 3 ))) goto done
;
978 sprintf( ret
, "\"%s\"%s", name
, p
);
981 HeapFree( GetProcessHeap(), 0, name
);
986 /**********************************************************************
987 * CreateProcessA (KERNEL32.171)
989 BOOL WINAPI
CreateProcessA( LPCSTR lpApplicationName
, LPSTR lpCommandLine
,
990 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
991 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
992 BOOL bInheritHandles
, DWORD dwCreationFlags
,
993 LPVOID lpEnvironment
, LPCSTR lpCurrentDirectory
,
994 LPSTARTUPINFOA lpStartupInfo
,
995 LPPROCESS_INFORMATION lpProcessInfo
)
1000 char name
[MAX_PATH
];
1003 /* Process the AppName and/or CmdLine to get module name and path */
1005 TRACE("app '%s' cmdline '%s'\n", lpApplicationName
, lpCommandLine
);
1007 if (!(tidy_cmdline
= get_file_name( lpApplicationName
, lpCommandLine
, name
, sizeof(name
) )))
1010 /* Warn if unsupported features are used */
1012 if (dwCreationFlags
& DETACHED_PROCESS
)
1013 FIXME("(%s,...): DETACHED_PROCESS ignored\n", name
);
1014 if (dwCreationFlags
& CREATE_NEW_CONSOLE
)
1015 FIXME("(%s,...): CREATE_NEW_CONSOLE ignored\n", name
);
1016 if (dwCreationFlags
& NORMAL_PRIORITY_CLASS
)
1017 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1018 if (dwCreationFlags
& IDLE_PRIORITY_CLASS
)
1019 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1020 if (dwCreationFlags
& HIGH_PRIORITY_CLASS
)
1021 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1022 if (dwCreationFlags
& REALTIME_PRIORITY_CLASS
)
1023 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1024 if (dwCreationFlags
& CREATE_NEW_PROCESS_GROUP
)
1025 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1026 if (dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
)
1027 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1028 if (dwCreationFlags
& CREATE_SEPARATE_WOW_VDM
)
1029 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1030 if (dwCreationFlags
& CREATE_SHARED_WOW_VDM
)
1031 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1032 if (dwCreationFlags
& CREATE_DEFAULT_ERROR_MODE
)
1033 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1034 if (dwCreationFlags
& CREATE_NO_WINDOW
)
1035 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1036 if (dwCreationFlags
& PROFILE_USER
)
1037 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1038 if (dwCreationFlags
& PROFILE_KERNEL
)
1039 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1040 if (dwCreationFlags
& PROFILE_SERVER
)
1041 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1042 if (lpStartupInfo
->lpDesktop
)
1043 FIXME("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
1044 name
, lpStartupInfo
->lpDesktop
);
1045 if (lpStartupInfo
->lpTitle
)
1046 FIXME("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
1047 name
, lpStartupInfo
->lpTitle
);
1048 if (lpStartupInfo
->dwFlags
& STARTF_USECOUNTCHARS
)
1049 FIXME("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
1050 name
, lpStartupInfo
->dwXCountChars
, lpStartupInfo
->dwYCountChars
);
1051 if (lpStartupInfo
->dwFlags
& STARTF_USEFILLATTRIBUTE
)
1052 FIXME("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
1053 name
, lpStartupInfo
->dwFillAttribute
);
1054 if (lpStartupInfo
->dwFlags
& STARTF_RUNFULLSCREEN
)
1055 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1056 if (lpStartupInfo
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1057 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1058 if (lpStartupInfo
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1059 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1060 if (lpStartupInfo
->dwFlags
& STARTF_USEHOTKEY
)
1061 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1063 /* Open file and determine executable type */
1065 hFile
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, -1 );
1066 if (hFile
== INVALID_HANDLE_VALUE
) goto done
;
1068 if ( !MODULE_GetBinaryType( hFile
, name
, &type
) )
1070 CloseHandle( hFile
);
1071 retv
= PROCESS_Create( -1, name
, tidy_cmdline
, lpEnvironment
,
1072 lpProcessAttributes
, lpThreadAttributes
,
1073 bInheritHandles
, dwCreationFlags
,
1074 lpStartupInfo
, lpProcessInfo
, lpCurrentDirectory
);
1078 /* Create process */
1082 case SCS_32BIT_BINARY
:
1083 case SCS_WOW_BINARY
:
1084 case SCS_DOS_BINARY
:
1085 retv
= PROCESS_Create( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1086 lpProcessAttributes
, lpThreadAttributes
,
1087 bInheritHandles
, dwCreationFlags
,
1088 lpStartupInfo
, lpProcessInfo
, lpCurrentDirectory
);
1091 case SCS_PIF_BINARY
:
1092 case SCS_POSIX_BINARY
:
1093 case SCS_OS216_BINARY
:
1094 FIXME("Unsupported executable type: %ld\n", type
);
1098 SetLastError( ERROR_BAD_FORMAT
);
1101 CloseHandle( hFile
);
1104 if (tidy_cmdline
!= lpCommandLine
) HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1108 /**********************************************************************
1109 * CreateProcessW (KERNEL32.172)
1111 * lpReserved is not converted
1113 BOOL WINAPI
CreateProcessW( LPCWSTR lpApplicationName
, LPWSTR lpCommandLine
,
1114 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1115 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1116 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1117 LPVOID lpEnvironment
, LPCWSTR lpCurrentDirectory
,
1118 LPSTARTUPINFOW lpStartupInfo
,
1119 LPPROCESS_INFORMATION lpProcessInfo
)
1121 STARTUPINFOA StartupInfoA
;
1123 LPSTR lpApplicationNameA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName
);
1124 LPSTR lpCommandLineA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine
);
1125 LPSTR lpCurrentDirectoryA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory
);
1127 memcpy (&StartupInfoA
, lpStartupInfo
, sizeof(STARTUPINFOA
));
1128 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpDesktop
);
1129 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpTitle
);
1131 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(lpApplicationName
), debugstr_w(lpCommandLine
));
1133 if (lpStartupInfo
->lpReserved
)
1134 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo
->lpReserved
));
1136 ret
= CreateProcessA( lpApplicationNameA
, lpCommandLineA
,
1137 lpProcessAttributes
, lpThreadAttributes
,
1138 bInheritHandles
, dwCreationFlags
,
1139 lpEnvironment
, lpCurrentDirectoryA
,
1140 &StartupInfoA
, lpProcessInfo
);
1142 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA
);
1143 HeapFree( GetProcessHeap(), 0, lpCommandLineA
);
1144 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1145 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1150 /***********************************************************************
1151 * GetModuleHandleA (KERNEL32.237)
1153 HMODULE WINAPI
GetModuleHandleA(LPCSTR module
)
1157 if ( module
== NULL
)
1158 wm
= PROCESS_Current()->exe_modref
;
1160 wm
= MODULE_FindModule( module
);
1162 return wm
? wm
->module
: 0;
1165 /***********************************************************************
1168 HMODULE WINAPI
GetModuleHandleW(LPCWSTR module
)
1171 LPSTR modulea
= HEAP_strdupWtoA( GetProcessHeap(), 0, module
);
1172 hModule
= GetModuleHandleA( modulea
);
1173 HeapFree( GetProcessHeap(), 0, modulea
);
1178 /***********************************************************************
1179 * GetModuleFileNameA (KERNEL32.235)
1181 * GetModuleFileNameA seems to *always* return the long path;
1182 * it's only GetModuleFileName16 that decides between short/long path
1183 * by checking if exe version >= 4.0.
1184 * (SDK docu doesn't mention this)
1186 DWORD WINAPI
GetModuleFileNameA(
1187 HMODULE hModule
, /* [in] module handle (32bit) */
1188 LPSTR lpFileName
, /* [out] filenamebuffer */
1189 DWORD size
) /* [in] size of filenamebuffer */
1193 EnterCriticalSection( &PROCESS_Current()->crit_section
);
1196 if ((wm
= MODULE32_LookupHMODULE( hModule
)))
1197 lstrcpynA( lpFileName
, wm
->filename
, size
);
1199 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
1200 TRACE("%s\n", lpFileName
);
1201 return strlen(lpFileName
);
1205 /***********************************************************************
1206 * GetModuleFileNameW (KERNEL32.236)
1208 DWORD WINAPI
GetModuleFileNameW( HMODULE hModule
, LPWSTR lpFileName
,
1211 LPSTR fnA
= (char*)HeapAlloc( GetProcessHeap(), 0, size
);
1212 DWORD res
= GetModuleFileNameA( hModule
, fnA
, size
);
1213 lstrcpynAtoW( lpFileName
, fnA
, size
);
1214 HeapFree( GetProcessHeap(), 0, fnA
);
1219 /***********************************************************************
1220 * LoadLibraryExA (KERNEL32)
1222 HMODULE WINAPI
LoadLibraryExA(LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1228 SetLastError(ERROR_INVALID_PARAMETER
);
1232 if (flags
& LOAD_LIBRARY_AS_DATAFILE
)
1238 if (!SearchPathA( NULL
, libname
, ".dll", sizeof(filename
), filename
, NULL
))
1240 /* FIXME: maybe we should use the hfile parameter instead */
1241 hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
1242 NULL
, OPEN_EXISTING
, 0, -1 );
1243 if (hFile
!= INVALID_HANDLE_VALUE
)
1245 hmod
= PE_LoadImage( hFile
, filename
, flags
);
1246 CloseHandle( hFile
);
1251 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1253 wm
= MODULE_LoadLibraryExA( libname
, hfile
, flags
);
1256 if ( !MODULE_DllProcessAttach( wm
, NULL
) )
1258 WARN_(module
)("Attach failed for module '%s', \n", libname
);
1259 MODULE_FreeLibrary(wm
);
1260 SetLastError(ERROR_DLL_INIT_FAILED
);
1265 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1267 return wm
? wm
->module
: 0;
1270 /***********************************************************************
1271 * MODULE_LoadLibraryExA (internal)
1273 * Load a PE style module according to the load order.
1275 * The HFILE parameter is not used and marked reserved in the SDK. I can
1276 * only guess that it should force a file to be mapped, but I rather
1277 * ignore the parameter because it would be extremely difficult to
1278 * integrate this with different types of module represenations.
1281 WINE_MODREF
*MODULE_LoadLibraryExA( LPCSTR libname
, HFILE hfile
, DWORD flags
)
1283 DWORD err
= GetLastError();
1286 module_loadorder_t
*plo
;
1289 if ( !libname
) return NULL
;
1291 filename
= HeapAlloc ( GetProcessHeap(), 0, MAX_PATH
+ 1 );
1292 if ( !filename
) return NULL
;
1294 /* build the modules filename */
1295 if (!SearchPathA( NULL
, libname
, ".dll", MAX_PATH
, filename
, NULL
))
1297 if ( ! GetSystemDirectoryA ( filename
, MAX_PATH
) )
1300 /* if the library name contains a path and can not be found, return an error.
1301 exception: if the path is the system directory, proceed, so that modules,
1302 which are not PE-modules can be loaded
1304 if the library name does not contain a path and can not be found, assume the
1305 system directory is meant */
1307 if ( ! strncasecmp ( filename
, libname
, strlen ( filename
) ))
1308 strcpy ( filename
, libname
);
1311 if ( strchr ( libname
, '\\' ) || strchr ( libname
, ':') || strchr ( libname
, '/' ) )
1315 strcat ( filename
, "\\" );
1316 strcat ( filename
, libname
);
1320 /* if the filename doesn't have an extension append .DLL */
1321 if (!(p
= strrchr( filename
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
1322 strcat( filename
, ".DLL" );
1325 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1327 /* Check for already loaded module */
1328 if (!(pwm
= MODULE_FindModule(filename
)) &&
1329 /* no path in libpath */
1330 !strchr( libname
, '\\' ) && !strchr( libname
, ':') && !strchr( libname
, '/' ))
1332 LPSTR fn
= HeapAlloc ( GetProcessHeap(), 0, MAX_PATH
+ 1 );
1335 /* since the default loading mechanism uses a more detailed algorithm
1336 * than SearchPath (like using PATH, which can even be modified between
1337 * two attempts of loading the same DLL), the look-up above (with
1338 * SearchPath) can have put the file in system directory, whereas it
1339 * has already been loaded but with a different path. So do a specific
1340 * look-up with filename (without any path)
1342 strcpy ( fn
, libname
);
1343 /* if the filename doesn't have an extension append .DLL */
1344 if (!strrchr( fn
, '.')) strcat( fn
, ".dll" );
1345 if ((pwm
= MODULE_FindModule( fn
)) != NULL
)
1346 strcpy( filename
, fn
);
1347 HeapFree( GetProcessHeap(), 0, fn
);
1352 if(!(pwm
->flags
& WINE_MODREF_MARKER
))
1355 if ((pwm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
) &&
1356 !(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1358 extern DWORD
fixup_imports(WINE_MODREF
*wm
); /*FIXME*/
1359 pwm
->flags
&= ~WINE_MODREF_DONT_RESOLVE_REFS
;
1360 fixup_imports( pwm
);
1362 TRACE("Already loaded module '%s' at 0x%08x, count=%d, \n", filename
, pwm
->module
, pwm
->refCount
);
1363 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1364 HeapFree ( GetProcessHeap(), 0, filename
);
1368 plo
= MODULE_GetLoadOrder(filename
, TRUE
);
1370 for(i
= 0; i
< MODULE_LOADORDER_NTYPES
; i
++)
1372 SetLastError( ERROR_FILE_NOT_FOUND
);
1373 switch(plo
->loadorder
[i
])
1375 case MODULE_LOADORDER_DLL
:
1376 TRACE("Trying native dll '%s'\n", filename
);
1377 pwm
= PE_LoadLibraryExA(filename
, flags
);
1380 case MODULE_LOADORDER_ELFDLL
:
1381 TRACE("Trying elfdll '%s'\n", filename
);
1382 if (!(pwm
= BUILTIN32_LoadLibraryExA(filename
, flags
)))
1383 pwm
= ELFDLL_LoadLibraryExA(filename
, flags
);
1386 case MODULE_LOADORDER_SO
:
1387 TRACE("Trying so-library '%s'\n", filename
);
1388 if (!(pwm
= BUILTIN32_LoadLibraryExA(filename
, flags
)))
1389 pwm
= ELF_LoadLibraryExA(filename
, flags
);
1392 case MODULE_LOADORDER_BI
:
1393 TRACE("Trying built-in '%s'\n", filename
);
1394 pwm
= BUILTIN32_LoadLibraryExA(filename
, flags
);
1398 ERR("Got invalid loadorder type %d (%s index %d)\n", plo
->loadorder
[i
], plo
->modulename
, i
);
1401 case MODULE_LOADORDER_INVALID
: /* We ignore this as it is an empty entry */
1408 /* Initialize DLL just loaded */
1409 TRACE("Loaded module '%s' at 0x%08x, \n", filename
, pwm
->module
);
1411 /* Set the refCount here so that an attach failure will */
1412 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1415 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1416 SetLastError( err
); /* restore last error */
1417 HeapFree ( GetProcessHeap(), 0, filename
);
1421 if(GetLastError() != ERROR_FILE_NOT_FOUND
)
1425 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1427 WARN("Failed to load module '%s'; error=0x%08lx, \n", filename
, GetLastError());
1428 HeapFree ( GetProcessHeap(), 0, filename
);
1432 /***********************************************************************
1433 * LoadLibraryA (KERNEL32)
1435 HMODULE WINAPI
LoadLibraryA(LPCSTR libname
) {
1436 return LoadLibraryExA(libname
,0,0);
1439 /***********************************************************************
1440 * LoadLibraryW (KERNEL32)
1442 HMODULE WINAPI
LoadLibraryW(LPCWSTR libnameW
)
1444 return LoadLibraryExW(libnameW
,0,0);
1447 /***********************************************************************
1448 * LoadLibrary32_16 (KERNEL.452)
1450 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
1454 SYSLEVEL_ReleaseWin16Lock();
1455 hModule
= LoadLibraryA( libname
);
1456 SYSLEVEL_RestoreWin16Lock();
1461 /***********************************************************************
1462 * LoadLibraryExW (KERNEL32)
1464 HMODULE WINAPI
LoadLibraryExW(LPCWSTR libnameW
,HANDLE hfile
,DWORD flags
)
1466 LPSTR libnameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW
);
1467 HMODULE ret
= LoadLibraryExA( libnameA
, hfile
, flags
);
1469 HeapFree( GetProcessHeap(), 0, libnameA
);
1473 /***********************************************************************
1474 * MODULE_FlushModrefs
1476 * NOTE: Assumes that the process critical section is held!
1478 * Remove all unused modrefs and call the internal unloading routines
1479 * for the library type.
1481 static void MODULE_FlushModrefs(void)
1483 WINE_MODREF
*wm
, *next
;
1485 for(wm
= PROCESS_Current()->modref_list
; wm
; wm
= next
)
1492 /* Unlink this modref from the chain */
1494 wm
->next
->prev
= wm
->prev
;
1496 wm
->prev
->next
= wm
->next
;
1497 if(wm
== PROCESS_Current()->modref_list
)
1498 PROCESS_Current()->modref_list
= wm
->next
;
1500 TRACE(" unloading %s\n", wm
->filename
);
1501 /* VirtualFree( (LPVOID)wm->module, 0, MEM_RELEASE ); */ /* FIXME */
1502 /* if (wm->dlhandle) dlclose( wm->dlhandle ); */ /* FIXME */
1503 FreeLibrary16(wm
->hDummyMod
);
1504 HeapFree( GetProcessHeap(), 0, wm
->deps
);
1505 HeapFree( GetProcessHeap(), 0, wm
->filename
);
1506 HeapFree( GetProcessHeap(), 0, wm
->short_filename
);
1507 HeapFree( GetProcessHeap(), 0, wm
);
1511 /***********************************************************************
1514 BOOL WINAPI
FreeLibrary(HINSTANCE hLibModule
)
1519 EnterCriticalSection( &PROCESS_Current()->crit_section
);
1520 PROCESS_Current()->free_lib_count
++;
1522 wm
= MODULE32_LookupHMODULE( hLibModule
);
1523 if ( !wm
|| !hLibModule
)
1524 SetLastError( ERROR_INVALID_HANDLE
);
1526 retv
= MODULE_FreeLibrary( wm
);
1528 PROCESS_Current()->free_lib_count
--;
1529 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
1534 /***********************************************************************
1535 * MODULE_DecRefCount
1537 * NOTE: Assumes that the process critical section is held!
1539 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
1543 if ( wm
->flags
& WINE_MODREF_MARKER
)
1546 if ( wm
->refCount
<= 0 )
1550 TRACE("(%s) refCount: %d\n", wm
->modname
, wm
->refCount
);
1552 if ( wm
->refCount
== 0 )
1554 wm
->flags
|= WINE_MODREF_MARKER
;
1556 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1558 MODULE_DecRefCount( wm
->deps
[i
] );
1560 wm
->flags
&= ~WINE_MODREF_MARKER
;
1564 /***********************************************************************
1565 * MODULE_FreeLibrary
1567 * NOTE: Assumes that the process critical section is held!
1569 BOOL
MODULE_FreeLibrary( WINE_MODREF
*wm
)
1571 TRACE("(%s) - START\n", wm
->modname
);
1573 /* Recursively decrement reference counts */
1574 MODULE_DecRefCount( wm
);
1576 /* Call process detach notifications */
1577 if ( PROCESS_Current()->free_lib_count
<= 1 )
1579 MODULE_DllProcessDetach( FALSE
, NULL
);
1582 struct unload_dll_request
*req
= server_alloc_req( sizeof(*req
), 0 );
1583 req
->base
= (void *)wm
->module
;
1584 server_call_noerr( REQ_UNLOAD_DLL
);
1587 MODULE_FlushModrefs();
1596 /***********************************************************************
1597 * FreeLibraryAndExitThread
1599 VOID WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
1601 FreeLibrary(hLibModule
);
1602 ExitThread(dwExitCode
);
1605 /***********************************************************************
1606 * PrivateLoadLibrary (KERNEL32)
1608 * FIXME: rough guesswork, don't know what "Private" means
1610 HINSTANCE WINAPI
PrivateLoadLibrary(LPCSTR libname
)
1612 return (HINSTANCE
)LoadLibrary16(libname
);
1617 /***********************************************************************
1618 * PrivateFreeLibrary (KERNEL32)
1620 * FIXME: rough guesswork, don't know what "Private" means
1622 void WINAPI
PrivateFreeLibrary(HINSTANCE handle
)
1624 FreeLibrary16((HINSTANCE16
)handle
);
1628 /***********************************************************************
1629 * WIN32_GetProcAddress16 (KERNEL32.36)
1630 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1632 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1638 WARN("hModule may not be 0!\n");
1639 return (FARPROC16
)0;
1641 if (HIWORD(hModule
))
1643 WARN("hModule is Win32 handle (%08x)\n", hModule
);
1644 return (FARPROC16
)0;
1646 hModule
= GetExePtr( hModule
);
1648 ordinal
= NE_GetOrdinal( hModule
, name
);
1649 TRACE("%04x '%s'\n", hModule
, name
);
1651 ordinal
= LOWORD(name
);
1652 TRACE("%04x %04x\n", hModule
, ordinal
);
1654 if (!ordinal
) return (FARPROC16
)0;
1655 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1656 TRACE("returning %08x\n",(UINT
)ret
);
1660 /***********************************************************************
1661 * GetProcAddress16 (KERNEL.50)
1663 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, SEGPTR name
)
1668 if (!hModule
) hModule
= GetCurrentTask();
1669 hModule
= GetExePtr( hModule
);
1671 if (HIWORD(name
) != 0)
1673 ordinal
= NE_GetOrdinal( hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1674 TRACE("%04x '%s'\n", hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1678 ordinal
= LOWORD(name
);
1679 TRACE("%04x %04x\n", hModule
, ordinal
);
1681 if (!ordinal
) return (FARPROC16
)0;
1683 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1685 TRACE("returning %08x\n", (UINT
)ret
);
1690 /***********************************************************************
1691 * GetProcAddress (KERNEL32.257)
1693 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
1695 return MODULE_GetProcAddress( hModule
, function
, TRUE
);
1698 /***********************************************************************
1699 * GetProcAddress32 (KERNEL.453)
1701 FARPROC WINAPI
GetProcAddress32_16( HMODULE hModule
, LPCSTR function
)
1703 return MODULE_GetProcAddress( hModule
, function
, FALSE
);
1706 /***********************************************************************
1707 * MODULE_GetProcAddress (internal)
1709 FARPROC
MODULE_GetProcAddress(
1710 HMODULE hModule
, /* [in] current module handle */
1711 LPCSTR function
, /* [in] function to be looked up */
1715 FARPROC retproc
= 0;
1717 if (HIWORD(function
))
1718 TRACE_(win32
)("(%08lx,%s)\n",(DWORD
)hModule
,function
);
1720 TRACE_(win32
)("(%08lx,%p)\n",(DWORD
)hModule
,function
);
1722 EnterCriticalSection( &PROCESS_Current()->crit_section
);
1723 if ((wm
= MODULE32_LookupHMODULE( hModule
)))
1725 retproc
= wm
->find_export( wm
, function
, snoop
);
1726 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1728 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
1733 /***************************************************************************
1734 * HasGPHandler (KERNEL.338)
1737 #include "pshpack1.h"
1738 typedef struct _GPHANDLERDEF
1745 #include "poppack.h"
1747 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1752 GPHANDLERDEF
*gpHandler
;
1754 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1755 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1756 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1757 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1758 && (gpHandler
= PTR_SEG_TO_LIN( gpPtr
)) != NULL
)
1760 while (gpHandler
->selector
)
1762 if ( SELECTOROF(address
) == gpHandler
->selector
1763 && OFFSETOF(address
) >= gpHandler
->rangeStart
1764 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1765 return PTR_SEG_OFF_TO_SEGPTR( gpHandler
->selector
,
1766 gpHandler
->handler
);