4 * Copyright 1995 Alexandre Julliard
12 #include <sys/types.h>
16 #include "wine/winbase16.h"
17 #include "wine/winuser16.h"
30 #include "selectors.h"
31 #include "stackframe.h"
33 #include "debugtools.h"
35 #include "loadorder.h"
39 DEFAULT_DEBUG_CHANNEL(module
);
40 DECLARE_DEBUG_CHANNEL(win32
);
42 /*************************************************************************
44 * Walk MODREFs for input process ID
46 void MODULE_WalkModref( DWORD id
)
49 WINE_MODREF
*zwm
, *prev
= NULL
;
50 PDB
*pdb
= PROCESS_IdToPDB( id
);
53 MESSAGE("Invalid process id (pid)\n");
57 MESSAGE("Modref list for process pdb=%p\n", pdb
);
58 MESSAGE("Modref next prev handle deps flags name\n");
59 for ( zwm
= pdb
->modref_list
; zwm
; zwm
= zwm
->next
) {
60 MESSAGE("%p %p %p %04x %5d %04x %s\n", zwm
, zwm
->next
, zwm
->prev
,
61 zwm
->module
, zwm
->nDeps
, zwm
->flags
, zwm
->modname
);
62 for ( i
= 0; i
< zwm
->nDeps
; i
++ ) {
64 MESSAGE(" %d %p %s\n", i
, zwm
->deps
[i
], zwm
->deps
[i
]->modname
);
66 if (prev
!= zwm
->prev
)
67 MESSAGE(" --> modref corrupt, previous pointer wrong!!\n");
72 /*************************************************************************
73 * MODULE32_LookupHMODULE
74 * looks for the referenced HMODULE in the current process
76 WINE_MODREF
*MODULE32_LookupHMODULE( HMODULE hmod
)
81 return PROCESS_Current()->exe_modref
;
84 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod
);
87 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
=wm
->next
)
88 if (wm
->module
== hmod
)
93 /*************************************************************************
96 static BOOL
MODULE_InitDll( WINE_MODREF
*wm
, DWORD type
, LPVOID lpReserved
)
100 static LPCSTR typeName
[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
101 "THREAD_ATTACH", "THREAD_DETACH" };
105 /* Skip calls for modules loaded with special load flags */
107 if ( ( wm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
)
108 || ( wm
->flags
& WINE_MODREF_LOAD_AS_DATAFILE
) )
112 TRACE("(%s,%s,%p) - CALL\n",
113 wm
->modname
, typeName
[type
], lpReserved
);
115 /* Call the initialization routine */
119 retv
= PE_InitDLL( wm
, type
, lpReserved
);
123 /* no need to do that, dlopen() already does */
127 ERR("wine_modref type %d not handled.\n", wm
->type
);
132 TRACE("(%s,%s,%p) - RETURN %d\n",
133 wm
->modname
, typeName
[type
], lpReserved
, retv
);
138 /*************************************************************************
139 * MODULE_DllProcessAttach
141 * Send the process attach notification to all DLLs the given module
142 * depends on (recursively). This is somewhat complicated due to the fact that
144 * - we have to respect the module dependencies, i.e. modules implicitly
145 * referenced by another module have to be initialized before the module
146 * itself can be initialized
148 * - the initialization routine of a DLL can itself call LoadLibrary,
149 * thereby introducing a whole new set of dependencies (even involving
150 * the 'old' modules) at any time during the whole process
152 * (Note that this routine can be recursively entered not only directly
153 * from itself, but also via LoadLibrary from one of the called initialization
156 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
157 * the process *detach* notifications to be sent in the correct order.
158 * This must not only take into account module dependencies, but also
159 * 'hidden' dependencies created by modules calling LoadLibrary in their
160 * attach notification routine.
162 * The strategy is rather simple: we move a WINE_MODREF to the head of the
163 * list after the attach notification has returned. This implies that the
164 * detach notifications are called in the reverse of the sequence the attach
165 * notifications *returned*.
167 * NOTE: Assumes that the process critical section is held!
170 BOOL
MODULE_DllProcessAttach( WINE_MODREF
*wm
, LPVOID lpReserved
)
176 /* prevent infinite recursion in case of cyclical dependencies */
177 if ( ( wm
->flags
& WINE_MODREF_MARKER
)
178 || ( wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
181 TRACE("(%s,%p) - START\n", wm
->modname
, lpReserved
);
183 /* Tag current MODREF to prevent recursive loop */
184 wm
->flags
|= WINE_MODREF_MARKER
;
186 /* Recursively attach all DLLs this one depends on */
187 for ( i
= 0; retv
&& i
< wm
->nDeps
; i
++ )
189 retv
= MODULE_DllProcessAttach( wm
->deps
[i
], lpReserved
);
191 /* Call DLL entry point */
194 retv
= MODULE_InitDll( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
196 wm
->flags
|= WINE_MODREF_PROCESS_ATTACHED
;
199 /* Re-insert MODREF at head of list */
200 if ( retv
&& wm
->prev
)
202 wm
->prev
->next
= wm
->next
;
203 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
206 wm
->next
= PROCESS_Current()->modref_list
;
207 PROCESS_Current()->modref_list
= wm
->next
->prev
= wm
;
210 /* Remove recursion flag */
211 wm
->flags
&= ~WINE_MODREF_MARKER
;
213 TRACE("(%s,%p) - END\n", wm
->modname
, lpReserved
);
218 /*************************************************************************
219 * MODULE_DllProcessDetach
221 * Send DLL process detach notifications. See the comment about calling
222 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
223 * is set, only DLLs with zero refcount are notified.
225 void MODULE_DllProcessDetach( BOOL bForceDetach
, LPVOID lpReserved
)
229 EnterCriticalSection( &PROCESS_Current()->crit_section
);
233 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
235 /* Check whether to detach this DLL */
236 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
238 if ( wm
->refCount
> 0 && !bForceDetach
)
241 /* Call detach notification */
242 wm
->flags
&= ~WINE_MODREF_PROCESS_ATTACHED
;
243 MODULE_InitDll( wm
, DLL_PROCESS_DETACH
, lpReserved
);
245 /* Restart at head of WINE_MODREF list, as entries might have
246 been added and/or removed while performing the call ... */
251 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
254 /*************************************************************************
255 * MODULE_DllThreadAttach
257 * Send DLL thread attach notifications. These are sent in the
258 * reverse sequence of process detach notification.
261 void MODULE_DllThreadAttach( LPVOID lpReserved
)
265 EnterCriticalSection( &PROCESS_Current()->crit_section
);
267 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
271 for ( ; wm
; wm
= wm
->prev
)
273 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
275 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
278 MODULE_InitDll( wm
, DLL_THREAD_ATTACH
, lpReserved
);
281 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
284 /*************************************************************************
285 * MODULE_DllThreadDetach
287 * Send DLL thread detach notifications. These are sent in the
288 * same sequence as process detach notification.
291 void MODULE_DllThreadDetach( LPVOID lpReserved
)
295 EnterCriticalSection( &PROCESS_Current()->crit_section
);
297 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
299 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
301 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
304 MODULE_InitDll( wm
, DLL_THREAD_DETACH
, lpReserved
);
307 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
310 /****************************************************************************
311 * DisableThreadLibraryCalls (KERNEL32.74)
313 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
315 BOOL WINAPI
DisableThreadLibraryCalls( HMODULE hModule
)
320 EnterCriticalSection( &PROCESS_Current()->crit_section
);
322 wm
= MODULE32_LookupHMODULE( hModule
);
326 wm
->flags
|= WINE_MODREF_NO_DLL_CALLS
;
328 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
334 /***********************************************************************
335 * MODULE_CreateDummyModule
337 * Create a dummy NE module for Win32 or Winelib.
339 HMODULE
MODULE_CreateDummyModule( LPCSTR filename
, HMODULE module32
)
343 SEGTABLEENTRY
*pSegment
;
346 const char* basename
;
350 /* Extract base filename */
351 basename
= strrchr(filename
, '\\');
352 if (!basename
) basename
= filename
;
354 len
= strlen(basename
);
355 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
357 /* Allocate module */
358 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
359 + strlen(filename
) + 1;
360 size
= sizeof(NE_MODULE
) +
361 /* loaded file info */
363 /* segment table: DS,CS */
364 2 * sizeof(SEGTABLEENTRY
) +
367 /* several empty tables */
370 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
371 if (!hModule
) return (HMODULE
)11; /* invalid exe */
373 FarSetOwner16( hModule
, hModule
);
374 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
376 /* Set all used entries */
377 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
384 pModule
->heap_size
= 0;
385 pModule
->stack_size
= 0;
386 pModule
->seg_count
= 2;
387 pModule
->modref_count
= 0;
388 pModule
->nrname_size
= 0;
389 pModule
->fileinfo
= sizeof(NE_MODULE
);
390 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
391 pModule
->self
= hModule
;
392 pModule
->module32
= module32
;
394 /* Set version and flags */
397 pModule
->expected_version
=
398 ((PE_HEADER(module32
)->OptionalHeader
.MajorSubsystemVersion
& 0xff) << 8 ) |
399 (PE_HEADER(module32
)->OptionalHeader
.MinorSubsystemVersion
& 0xff);
400 pModule
->flags
|= NE_FFLAGS_WIN32
;
401 if (PE_HEADER(module32
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
402 pModule
->flags
|= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
;
405 /* Set loaded file information */
406 ofs
= (OFSTRUCT
*)(pModule
+ 1);
407 memset( ofs
, 0, of_size
);
408 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
409 strcpy( ofs
->szPathName
, filename
);
411 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + of_size
);
412 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
415 pSegment
->flags
= NE_SEGFLAGS_DATA
;
416 pSegment
->minsize
= 0x1000;
423 pStr
= (char *)pSegment
;
424 pModule
->name_table
= (int)pStr
- (int)pModule
;
427 lstrcpynA( pStr
+1, basename
, len
+1 );
430 /* All tables zero terminated */
431 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
=
432 (int)pStr
- (int)pModule
;
434 NE_RegisterModule( pModule
);
439 /**********************************************************************
440 * MODULE_FindModule32
442 * Find a (loaded) win32 module depending on path
445 * the module handle if found
448 WINE_MODREF
*MODULE_FindModule(
449 LPCSTR path
/* [in] pathname of module/library to be found */
452 char dllname
[260], *p
;
454 /* Append .DLL to name if no extension present */
455 strcpy( dllname
, path
);
456 if (!(p
= strrchr( dllname
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
457 strcat( dllname
, ".DLL" );
459 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
461 if ( !strcasecmp( dllname
, wm
->modname
) )
463 if ( !strcasecmp( dllname
, wm
->filename
) )
465 if ( !strcasecmp( dllname
, wm
->short_modname
) )
467 if ( !strcasecmp( dllname
, wm
->short_filename
) )
474 /***********************************************************************
475 * MODULE_GetBinaryType
477 * The GetBinaryType function determines whether a file is executable
478 * or not and if it is it returns what type of executable it is.
479 * The type of executable is a property that determines in which
480 * subsystem an executable file runs under.
482 * Binary types returned:
483 * SCS_32BIT_BINARY: A Win32 based application
484 * SCS_DOS_BINARY: An MS-Dos based application
485 * SCS_WOW_BINARY: A Win16 based application
486 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
487 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
488 * SCS_OS216_BINARY: A 16bit OS/2 based application
490 * Returns TRUE if the file is an executable in which case
491 * the value pointed by lpBinaryType is set.
492 * Returns FALSE if the file is not an executable or if the function fails.
494 * To do so it opens the file and reads in the header information
495 * if the extended header information is not present it will
496 * assume that the file is a DOS executable.
497 * If the extended header information is present it will
498 * determine if the file is a 16 or 32 bit Windows executable
499 * by check the flags in the header.
501 * Note that .COM and .PIF files are only recognized by their
502 * file name extension; but Windows does it the same way ...
504 static BOOL
MODULE_GetBinaryType( HANDLE hfile
, LPCSTR filename
,
505 LPDWORD lpBinaryType
)
507 IMAGE_DOS_HEADER mz_header
;
511 /* Seek to the start of the file and read the DOS header information.
513 if ( SetFilePointer( hfile
, 0, NULL
, SEEK_SET
) != -1
514 && ReadFile( hfile
, &mz_header
, sizeof(mz_header
), &len
, NULL
)
515 && len
== sizeof(mz_header
) )
517 /* Now that we have the header check the e_magic field
518 * to see if this is a dos image.
520 if ( mz_header
.e_magic
== IMAGE_DOS_SIGNATURE
)
522 BOOL lfanewValid
= FALSE
;
523 /* We do have a DOS image so we will now try to seek into
524 * the file by the amount indicated by the field
525 * "Offset to extended header" and read in the
526 * "magic" field information at that location.
527 * This will tell us if there is more header information
530 /* But before we do we will make sure that header
531 * structure encompasses the "Offset to extended header"
534 if ( (mz_header
.e_cparhdr
<<4) >= sizeof(IMAGE_DOS_HEADER
) )
535 if ( ( mz_header
.e_crlc
== 0 ) ||
536 ( mz_header
.e_lfarlc
>= sizeof(IMAGE_DOS_HEADER
) ) )
537 if ( mz_header
.e_lfanew
>= sizeof(IMAGE_DOS_HEADER
)
538 && SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
539 && ReadFile( hfile
, magic
, sizeof(magic
), &len
, NULL
)
540 && len
== sizeof(magic
) )
545 /* If we cannot read this "extended header" we will
546 * assume that we have a simple DOS executable.
548 *lpBinaryType
= SCS_DOS_BINARY
;
553 /* Reading the magic field succeeded so
554 * we will try to determine what type it is.
556 if ( *(DWORD
*)magic
== IMAGE_NT_SIGNATURE
)
558 /* This is an NT signature.
560 *lpBinaryType
= SCS_32BIT_BINARY
;
563 else if ( *(WORD
*)magic
== IMAGE_OS2_SIGNATURE
)
565 /* The IMAGE_OS2_SIGNATURE indicates that the
566 * "extended header is a Windows executable (NE)
567 * header." This can mean either a 16-bit OS/2
568 * or a 16-bit Windows or even a DOS program
569 * (running under a DOS extender). To decide
570 * which, we'll have to read the NE header.
574 if ( SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
575 && ReadFile( hfile
, &ne
, sizeof(ne
), &len
, NULL
)
576 && len
== sizeof(ne
) )
578 switch ( ne
.operating_system
)
580 case 2: *lpBinaryType
= SCS_WOW_BINARY
; return TRUE
;
581 case 5: *lpBinaryType
= SCS_DOS_BINARY
; return TRUE
;
582 default: *lpBinaryType
= SCS_OS216_BINARY
; return TRUE
;
585 /* Couldn't read header, so abort. */
590 /* Unknown extended header, but this file is nonetheless
593 *lpBinaryType
= SCS_DOS_BINARY
;
600 /* If we get here, we don't even have a correct MZ header.
601 * Try to check the file extension for known types ...
603 ptr
= strrchr( filename
, '.' );
604 if ( ptr
&& !strchr( ptr
, '\\' ) && !strchr( ptr
, '/' ) )
606 if ( !lstrcmpiA( ptr
, ".COM" ) )
608 *lpBinaryType
= SCS_DOS_BINARY
;
612 if ( !lstrcmpiA( ptr
, ".PIF" ) )
614 *lpBinaryType
= SCS_PIF_BINARY
;
622 /***********************************************************************
623 * GetBinaryTypeA [KERNEL32.280]
625 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
630 TRACE_(win32
)("%s\n", lpApplicationName
);
634 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
637 /* Open the file indicated by lpApplicationName for reading.
639 hfile
= CreateFileA( lpApplicationName
, GENERIC_READ
, 0,
640 NULL
, OPEN_EXISTING
, 0, -1 );
641 if ( hfile
== INVALID_HANDLE_VALUE
)
646 ret
= MODULE_GetBinaryType( hfile
, lpApplicationName
, lpBinaryType
);
650 CloseHandle( hfile
);
655 /***********************************************************************
656 * GetBinaryTypeW [KERNEL32.281]
658 BOOL WINAPI
GetBinaryTypeW( LPCWSTR lpApplicationName
, LPDWORD lpBinaryType
)
663 TRACE_(win32
)("%s\n", debugstr_w(lpApplicationName
) );
667 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
670 /* Convert the wide string to a ascii string.
672 strNew
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName
);
674 if ( strNew
!= NULL
)
676 ret
= GetBinaryTypeA( strNew
, lpBinaryType
);
678 /* Free the allocated string.
680 HeapFree( GetProcessHeap(), 0, strNew
);
686 /**********************************************************************
687 * MODULE_CreateUnixProcess
689 static BOOL
MODULE_CreateUnixProcess( LPCSTR filename
, LPCSTR lpCmdLine
,
690 LPSTARTUPINFOA lpStartupInfo
,
691 LPPROCESS_INFORMATION lpProcessInfo
,
694 const char *argv
[256], **argptr
;
695 char *cmdline
= NULL
;
698 /* Get Unix file name and iconic flag */
700 if ( lpStartupInfo
->dwFlags
& STARTF_USESHOWWINDOW
)
701 if ( lpStartupInfo
->wShowWindow
== SW_SHOWMINIMIZED
702 || lpStartupInfo
->wShowWindow
== SW_SHOWMINNOACTIVE
)
705 /* Build argument list */
710 const char *unixfilename
= filename
;
711 DOS_FULL_NAME full_name
;
713 p
= cmdline
= strdup(lpCmdLine
);
714 if (strchr(filename
, '/') || strchr(filename
, ':') || strchr(filename
, '\\'))
716 if ( DOSFS_GetFullName( filename
, TRUE
, &full_name
) )
717 unixfilename
= full_name
.long_name
;
719 if (iconic
) *argptr
++ = "-iconic";
722 while (*p
&& (*p
== ' ' || *p
== '\t')) *p
++ = '\0';
725 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
727 /* overwrite program name gotten from tidy_cmd */
728 argv
[0] = unixfilename
;
733 if (iconic
) *argptr
++ = "-iconic";
734 *argptr
++ = lpCmdLine
;
738 /* Fork and execute */
742 /* Note: don't use Wine routines here, as this process
743 has not been correctly initialized! */
745 execvp( argv
[0], (char**)argv
);
749 fprintf( stderr
, "CreateProcess: can't exec 'wine %s'\n",
754 /* Fake success return value */
756 memset( lpProcessInfo
, '\0', sizeof( *lpProcessInfo
) );
757 lpProcessInfo
->hProcess
= INVALID_HANDLE_VALUE
;
758 lpProcessInfo
->hThread
= INVALID_HANDLE_VALUE
;
759 if (cmdline
) free(cmdline
);
761 SetLastError( ERROR_SUCCESS
);
765 /***********************************************************************
766 * WinExec16 (KERNEL.166)
768 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
772 SYSLEVEL_ReleaseWin16Lock();
773 hInst
= WinExec( lpCmdLine
, nCmdShow
);
774 SYSLEVEL_RestoreWin16Lock();
779 /***********************************************************************
780 * WinExec (KERNEL32.566)
782 HINSTANCE WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
785 UINT16 paramCmdShow
[2];
788 return 2; /* File not found */
790 /* Set up LOADPARAMS buffer for LoadModule */
792 memset( ¶ms
, '\0', sizeof(params
) );
793 params
.lpCmdLine
= (LPSTR
)lpCmdLine
;
794 params
.lpCmdShow
= paramCmdShow
;
795 params
.lpCmdShow
[0] = 2;
796 params
.lpCmdShow
[1] = nCmdShow
;
798 /* Now load the executable file */
800 return LoadModule( NULL
, ¶ms
);
803 /**********************************************************************
804 * LoadModule (KERNEL32.499)
806 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
808 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
809 PROCESS_INFORMATION info
;
810 STARTUPINFOA startup
;
815 memset( &startup
, '\0', sizeof(startup
) );
816 startup
.cb
= sizeof(startup
);
817 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
818 startup
.wShowWindow
= params
->lpCmdShow
? params
->lpCmdShow
[1] : 0;
820 if ( !CreateProcessA( name
, params
->lpCmdLine
,
821 NULL
, NULL
, FALSE
, 0, params
->lpEnvAddress
,
822 NULL
, &startup
, &info
) )
824 hInstance
= GetLastError();
825 if ( hInstance
< 32 ) return hInstance
;
827 FIXME("Strange error set by CreateProcess: %d\n", hInstance
);
831 /* Give 30 seconds to the app to come up */
832 if ( Callout
.WaitForInputIdle ( info
.hProcess
, 30000 ) == 0xFFFFFFFF )
833 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
835 /* Get 16-bit hInstance/hTask from process */
836 pdb
= PROCESS_IdToPDB( info
.dwProcessId
);
837 tdb
= pdb
? (TDB
*)GlobalLock16( pdb
->task
) : NULL
;
838 hInstance
= tdb
&& tdb
->hInstance
? tdb
->hInstance
: pdb
? pdb
->task
: 0;
839 /* If there is no hInstance (32-bit process) return a dummy value
841 * FIXME: should do this in all cases and fix Win16 callers */
842 if (!hInstance
) hInstance
= 33;
844 /* Close off the handles */
845 CloseHandle( info
.hThread
);
846 CloseHandle( info
.hProcess
);
851 /*************************************************************************
854 * Get next blank delimited token from input string. If quoted then
855 * process till matching quote and then till blank.
857 * Returns number of characters in token (not including \0). On
858 * end of string (EOS), returns a 0.
860 * from (IO) address of start of input string to scan, updated to
861 * next non-processed character.
862 * to (IO) address of start of output string (previous token \0
863 * char), updated to end of new output string (the \0
866 static int get_makename_token(LPCSTR
*from
, LPSTR
*to
)
869 LPCSTR to_old
= *to
; /* only used for tracing */
871 while ( **from
== ' ') {
872 /* Copy leading blanks (separators between previous */
873 /* token and this token). */
880 while ( (**from
!= 0) && (**from
!= ' ') && (**from
!= '"') ) {
881 **to
= **from
; (*from
)++; (*to
)++; len
++;
883 if ( **from
== '"' ) {
884 /* Handle quoted string. */
886 if ( !strchr(*from
, '"') ) {
887 /* fail - no closing quote. Return entire string */
888 while ( **from
!= 0 ) {
889 **to
= **from
; (*from
)++; (*to
)++; len
++;
893 while( **from
!= '"') {
903 /* either EOS or ' ' */
908 **to
= 0; /* terminate output string */
910 TRACE("returning token len=%d, string=%s\n", len
, to_old
);
915 /*************************************************************************
916 * make_lpCommandLine_name
918 * Try longer and longer strings from "line" to find an existing
919 * file name. Each attempt is delimited by a blank outside of quotes.
920 * Also will attempt to append ".exe" if requested and not already
921 * present. Returns the address of the remaining portion of the
926 static BOOL
make_lpCommandLine_name( LPCSTR line
, LPSTR name
, int namelen
,
938 /* scan over initial blanks if any */
939 while ( *from
== ' ') from
++;
941 /* get a token and append to previous data the check for existance */
943 if ( !get_makename_token( &from
, &to
) ) {
944 /* EOS has occured and not found - exit */
949 TRACE("checking if file exists '%s'\n", name
);
950 retlen
= SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, &lastpart
);
952 retlen
= SearchPathA( NULL
, name
, NULL
, sizeof(buffer
), buffer
, &lastpart
);
954 if ( retlen
&& (retlen
< sizeof(buffer
)) ) break;
957 /* if we have a non-null full path name in buffer then move to output */
959 if ( strlen(buffer
) <= namelen
) {
960 strcpy( name
, buffer
);
962 /* not enough space to return full path string */
963 FIXME("internal string not long enough, need %d\n",
968 /* all done, indicate end of module name and then trace and exit */
969 if (after
) *after
= from
;
970 TRACE("%i, selected file name '%s'\n and cmdline as %s\n",
971 found
, name
, debugstr_a(from
));
975 /*************************************************************************
976 * make_lpApplicationName_name
978 * Scan input string (the lpApplicationName) and remove any quotes
979 * if they are balanced.
983 static BOOL
make_lpApplicationName_name( LPCSTR line
, LPSTR name
, int namelen
)
986 LPSTR to
, to_end
, to_old
;
990 to_end
= to
+ sizeof(buffer
) - 1;
993 while ( *line
== ' ' ) line
++; /* point to beginning of string */
996 /* Copy all input till end, or quote */
997 while((*from
!= 0) && (*from
!= '"') && (to
< to_end
))
999 if (to
>= to_end
) { *to
= 0; break; }
1003 /* Handle quoted string. If there is a closing quote, copy all */
1004 /* that is inside. */
1006 if (!strchr(from
, '"'))
1008 /* fail - no closing quote */
1009 to
= to_old
; /* restore to previous attempt */
1010 *to
= 0; /* end string */
1011 break; /* exit with previous attempt */
1013 while((*from
!= '"') && (to
< to_end
)) *to
++ = *from
++;
1014 if (to
>= to_end
) { *to
= 0; break; }
1016 continue; /* past quoted string, so restart from top */
1019 *to
= 0; /* terminate output string */
1020 to_old
= to
; /* save for possible use in unmatched quote case */
1022 /* loop around keeping the blank as part of file name */
1024 break; /* exit if out of input string */
1027 if (!SearchPathA( NULL
, buffer
, ".exe", namelen
, name
, NULL
) &&
1028 !SearchPathA( NULL
, buffer
, NULL
, namelen
, name
, NULL
) ) {
1029 TRACE("file not found '%s'\n", buffer
);
1033 TRACE("selected as file name '%s'\n", name
);
1037 /**********************************************************************
1038 * CreateProcessA (KERNEL32.171)
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
)
1049 BOOL found_file
= FALSE
;
1052 char name
[256], dummy
[256];
1053 LPCSTR cmdline
= NULL
;
1056 /* Get name and command line */
1058 if (!lpApplicationName
&& !lpCommandLine
)
1060 SetLastError( ERROR_FILE_NOT_FOUND
);
1064 /* Process the AppName and/or CmdLine to get module name and path */
1068 if (lpApplicationName
)
1070 found_file
= make_lpApplicationName_name( lpApplicationName
, name
, sizeof(name
) );
1072 make_lpCommandLine_name( lpCommandLine
, dummy
, sizeof ( dummy
), &cmdline
);
1074 cmdline
= lpApplicationName
;
1079 found_file
= make_lpCommandLine_name( lpCommandLine
, name
, sizeof ( name
), &cmdline
);
1082 if ( !found_file
) {
1083 /* make an early exit if file not found - save second pass */
1084 SetLastError( ERROR_FILE_NOT_FOUND
);
1088 if (!cmdline
) cmdline
= "";
1089 tidy_cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(name
) + strlen(cmdline
) + 3 );
1090 TRACE_(module
)("tidy_cmdline: name '%s'[%d], cmdline '%s'[%d]\n",
1091 name
, strlen(name
), cmdline
, strlen(cmdline
));
1092 sprintf( tidy_cmdline
, "\"%s\"%s", name
, cmdline
);
1094 /* Warn if unsupported features are used */
1096 if (dwCreationFlags
& DETACHED_PROCESS
)
1097 FIXME("(%s,...): DETACHED_PROCESS ignored\n", name
);
1098 if (dwCreationFlags
& CREATE_NEW_CONSOLE
)
1099 FIXME("(%s,...): CREATE_NEW_CONSOLE ignored\n", name
);
1100 if (dwCreationFlags
& NORMAL_PRIORITY_CLASS
)
1101 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1102 if (dwCreationFlags
& IDLE_PRIORITY_CLASS
)
1103 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1104 if (dwCreationFlags
& HIGH_PRIORITY_CLASS
)
1105 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1106 if (dwCreationFlags
& REALTIME_PRIORITY_CLASS
)
1107 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1108 if (dwCreationFlags
& CREATE_NEW_PROCESS_GROUP
)
1109 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1110 if (dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
)
1111 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1112 if (dwCreationFlags
& CREATE_SEPARATE_WOW_VDM
)
1113 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1114 if (dwCreationFlags
& CREATE_SHARED_WOW_VDM
)
1115 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1116 if (dwCreationFlags
& CREATE_DEFAULT_ERROR_MODE
)
1117 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1118 if (dwCreationFlags
& CREATE_NO_WINDOW
)
1119 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1120 if (dwCreationFlags
& PROFILE_USER
)
1121 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1122 if (dwCreationFlags
& PROFILE_KERNEL
)
1123 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1124 if (dwCreationFlags
& PROFILE_SERVER
)
1125 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1126 if (lpCurrentDirectory
)
1127 FIXME("(%s,...): lpCurrentDirectory %s ignored\n",
1128 name
, lpCurrentDirectory
);
1129 if (lpStartupInfo
->lpDesktop
)
1130 FIXME("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
1131 name
, lpStartupInfo
->lpDesktop
);
1132 if (lpStartupInfo
->lpTitle
)
1133 FIXME("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
1134 name
, lpStartupInfo
->lpTitle
);
1135 if (lpStartupInfo
->dwFlags
& STARTF_USECOUNTCHARS
)
1136 FIXME("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
1137 name
, lpStartupInfo
->dwXCountChars
, lpStartupInfo
->dwYCountChars
);
1138 if (lpStartupInfo
->dwFlags
& STARTF_USEFILLATTRIBUTE
)
1139 FIXME("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
1140 name
, lpStartupInfo
->dwFillAttribute
);
1141 if (lpStartupInfo
->dwFlags
& STARTF_RUNFULLSCREEN
)
1142 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1143 if (lpStartupInfo
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1144 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1145 if (lpStartupInfo
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1146 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1147 if (lpStartupInfo
->dwFlags
& STARTF_USEHOTKEY
)
1148 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1151 /* Load file and create process */
1155 /* Open file and determine executable type */
1157 hFile
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
1158 NULL
, OPEN_EXISTING
, 0, -1 );
1159 if ( hFile
== INVALID_HANDLE_VALUE
)
1161 SetLastError( ERROR_FILE_NOT_FOUND
);
1162 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1166 if ( !MODULE_GetBinaryType( hFile
, name
, &type
) )
1168 CloseHandle( hFile
);
1170 /* FIXME: Try Unix executable only when appropriate! */
1171 if ( MODULE_CreateUnixProcess( name
, tidy_cmdline
,
1172 lpStartupInfo
, lpProcessInfo
, FALSE
) )
1174 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1177 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1178 SetLastError( ERROR_BAD_FORMAT
);
1183 /* Create process */
1187 case SCS_32BIT_BINARY
:
1188 retv
= PE_CreateProcess( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1189 lpProcessAttributes
, lpThreadAttributes
,
1190 bInheritHandles
, dwCreationFlags
,
1191 lpStartupInfo
, lpProcessInfo
);
1194 case SCS_DOS_BINARY
:
1195 retv
= MZ_CreateProcess( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1196 lpProcessAttributes
, lpThreadAttributes
,
1197 bInheritHandles
, dwCreationFlags
,
1198 lpStartupInfo
, lpProcessInfo
);
1201 case SCS_WOW_BINARY
:
1202 retv
= NE_CreateProcess( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1203 lpProcessAttributes
, lpThreadAttributes
,
1204 bInheritHandles
, dwCreationFlags
,
1205 lpStartupInfo
, lpProcessInfo
);
1208 case SCS_PIF_BINARY
:
1209 case SCS_POSIX_BINARY
:
1210 case SCS_OS216_BINARY
:
1211 FIXME("Unsupported executable type: %ld\n", type
);
1215 SetLastError( ERROR_BAD_FORMAT
);
1220 CloseHandle( hFile
);
1222 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1226 /**********************************************************************
1227 * CreateProcessW (KERNEL32.172)
1229 * lpReserved is not converted
1231 BOOL WINAPI
CreateProcessW( LPCWSTR lpApplicationName
, LPWSTR lpCommandLine
,
1232 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1233 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1234 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1235 LPVOID lpEnvironment
, LPCWSTR lpCurrentDirectory
,
1236 LPSTARTUPINFOW lpStartupInfo
,
1237 LPPROCESS_INFORMATION lpProcessInfo
)
1239 STARTUPINFOA StartupInfoA
;
1241 LPSTR lpApplicationNameA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName
);
1242 LPSTR lpCommandLineA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine
);
1243 LPSTR lpCurrentDirectoryA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory
);
1245 memcpy (&StartupInfoA
, lpStartupInfo
, sizeof(STARTUPINFOA
));
1246 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpDesktop
);
1247 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpTitle
);
1249 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(lpApplicationName
), debugstr_w(lpCommandLine
));
1251 if (lpStartupInfo
->lpReserved
)
1252 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo
->lpReserved
));
1254 ret
= CreateProcessA( lpApplicationNameA
, lpCommandLineA
,
1255 lpProcessAttributes
, lpThreadAttributes
,
1256 bInheritHandles
, dwCreationFlags
,
1257 lpEnvironment
, lpCurrentDirectoryA
,
1258 &StartupInfoA
, lpProcessInfo
);
1260 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA
);
1261 HeapFree( GetProcessHeap(), 0, lpCommandLineA
);
1262 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1263 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1268 /***********************************************************************
1269 * GetModuleHandleA (KERNEL32.237)
1271 HMODULE WINAPI
GetModuleHandleA(LPCSTR module
)
1275 if ( module
== NULL
)
1276 wm
= PROCESS_Current()->exe_modref
;
1278 wm
= MODULE_FindModule( module
);
1280 return wm
? wm
->module
: 0;
1283 /***********************************************************************
1286 HMODULE WINAPI
GetModuleHandleW(LPCWSTR module
)
1289 LPSTR modulea
= HEAP_strdupWtoA( GetProcessHeap(), 0, module
);
1290 hModule
= GetModuleHandleA( modulea
);
1291 HeapFree( GetProcessHeap(), 0, modulea
);
1296 /***********************************************************************
1297 * GetModuleFileNameA (KERNEL32.235)
1299 * GetModuleFileNameA seems to *always* return the long path;
1300 * it's only GetModuleFileName16 that decides between short/long path
1301 * by checking if exe version >= 4.0.
1302 * (SDK docu doesn't mention this)
1304 DWORD WINAPI
GetModuleFileNameA(
1305 HMODULE hModule
, /* [in] module handle (32bit) */
1306 LPSTR lpFileName
, /* [out] filenamebuffer */
1307 DWORD size
/* [in] size of filenamebuffer */
1309 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1311 if (!wm
) /* can happen on start up or the like */
1314 lstrcpynA( lpFileName
, wm
->filename
, size
);
1316 TRACE("%s\n", lpFileName
);
1317 return strlen(lpFileName
);
1321 /***********************************************************************
1322 * GetModuleFileNameW (KERNEL32.236)
1324 DWORD WINAPI
GetModuleFileNameW( HMODULE hModule
, LPWSTR lpFileName
,
1327 LPSTR fnA
= (char*)HeapAlloc( GetProcessHeap(), 0, size
);
1328 DWORD res
= GetModuleFileNameA( hModule
, fnA
, size
);
1329 lstrcpynAtoW( lpFileName
, fnA
, size
);
1330 HeapFree( GetProcessHeap(), 0, fnA
);
1335 /***********************************************************************
1336 * LoadLibraryExA (KERNEL32)
1338 HMODULE WINAPI
LoadLibraryExA(LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1344 SetLastError(ERROR_INVALID_PARAMETER
);
1348 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1350 wm
= MODULE_LoadLibraryExA( libname
, hfile
, flags
);
1353 if ( !MODULE_DllProcessAttach( wm
, NULL
) )
1355 WARN_(module
)("Attach failed for module '%s', \n", libname
);
1356 MODULE_FreeLibrary(wm
);
1357 SetLastError(ERROR_DLL_INIT_FAILED
);
1362 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1364 return wm
? wm
->module
: 0;
1367 /***********************************************************************
1368 * MODULE_LoadLibraryExA (internal)
1370 * Load a PE style module according to the load order.
1372 * The HFILE parameter is not used and marked reserved in the SDK. I can
1373 * only guess that it should force a file to be mapped, but I rather
1374 * ignore the parameter because it would be extremely difficult to
1375 * integrate this with different types of module represenations.
1378 WINE_MODREF
*MODULE_LoadLibraryExA( LPCSTR libname
, HFILE hfile
, DWORD flags
)
1380 DWORD err
= GetLastError();
1383 module_loadorder_t
*plo
;
1385 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1387 /* Check for already loaded module */
1388 if((pwm
= MODULE_FindModule(libname
)))
1390 if(!(pwm
->flags
& WINE_MODREF_MARKER
))
1392 TRACE("Already loaded module '%s' at 0x%08x, count=%d, \n", libname
, pwm
->module
, pwm
->refCount
);
1393 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1397 plo
= MODULE_GetLoadOrder(libname
);
1399 for(i
= 0; i
< MODULE_LOADORDER_NTYPES
; i
++)
1401 SetLastError( ERROR_FILE_NOT_FOUND
);
1402 switch(plo
->loadorder
[i
])
1404 case MODULE_LOADORDER_DLL
:
1405 TRACE("Trying native dll '%s'\n", libname
);
1406 pwm
= PE_LoadLibraryExA(libname
, flags
);
1409 case MODULE_LOADORDER_ELFDLL
:
1410 TRACE("Trying elfdll '%s'\n", libname
);
1411 if (!(pwm
= BUILTIN32_LoadLibraryExA(libname
, flags
)))
1412 pwm
= ELFDLL_LoadLibraryExA(libname
, flags
);
1415 case MODULE_LOADORDER_SO
:
1416 TRACE("Trying so-library '%s'\n", libname
);
1417 if (!(pwm
= BUILTIN32_LoadLibraryExA(libname
, flags
)))
1418 pwm
= ELF_LoadLibraryExA(libname
, flags
);
1421 case MODULE_LOADORDER_BI
:
1422 TRACE("Trying built-in '%s'\n", libname
);
1423 pwm
= BUILTIN32_LoadLibraryExA(libname
, flags
);
1427 ERR("Got invalid loadorder type %d (%s index %d)\n", plo
->loadorder
[i
], plo
->modulename
, i
);
1430 case MODULE_LOADORDER_INVALID
: /* We ignore this as it is an empty entry */
1437 /* Initialize DLL just loaded */
1438 TRACE("Loaded module '%s' at 0x%08x, \n", libname
, pwm
->module
);
1440 /* Set the refCount here so that an attach failure will */
1441 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1444 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1445 SetLastError( err
); /* restore last error */
1449 if(GetLastError() != ERROR_FILE_NOT_FOUND
)
1453 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname
, GetLastError());
1454 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1458 /***********************************************************************
1459 * LoadLibraryA (KERNEL32)
1461 HMODULE WINAPI
LoadLibraryA(LPCSTR libname
) {
1462 return LoadLibraryExA(libname
,0,0);
1465 /***********************************************************************
1466 * LoadLibraryW (KERNEL32)
1468 HMODULE WINAPI
LoadLibraryW(LPCWSTR libnameW
)
1470 return LoadLibraryExW(libnameW
,0,0);
1473 /***********************************************************************
1474 * LoadLibrary32_16 (KERNEL.452)
1476 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
1480 SYSLEVEL_ReleaseWin16Lock();
1481 hModule
= LoadLibraryA( libname
);
1482 SYSLEVEL_RestoreWin16Lock();
1487 /***********************************************************************
1488 * LoadLibraryExW (KERNEL32)
1490 HMODULE WINAPI
LoadLibraryExW(LPCWSTR libnameW
,HANDLE hfile
,DWORD flags
)
1492 LPSTR libnameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW
);
1493 HMODULE ret
= LoadLibraryExA( libnameA
, hfile
, flags
);
1495 HeapFree( GetProcessHeap(), 0, libnameA
);
1499 /***********************************************************************
1500 * MODULE_FlushModrefs
1502 * NOTE: Assumes that the process critical section is held!
1504 * Remove all unused modrefs and call the internal unloading routines
1505 * for the library type.
1507 static void MODULE_FlushModrefs(void)
1509 WINE_MODREF
*wm
, *next
;
1511 for(wm
= PROCESS_Current()->modref_list
; wm
; wm
= next
)
1518 /* Unlink this modref from the chain */
1520 wm
->next
->prev
= wm
->prev
;
1522 wm
->prev
->next
= wm
->next
;
1523 if(wm
== PROCESS_Current()->modref_list
)
1524 PROCESS_Current()->modref_list
= wm
->next
;
1527 * The unloaders are also responsible for freeing the modref itself
1528 * because the loaders were responsible for allocating it.
1532 case MODULE32_PE
: PE_UnloadLibrary(wm
); break;
1533 case MODULE32_ELF
: ELF_UnloadLibrary(wm
); break;
1534 case MODULE32_ELFDLL
: ELFDLL_UnloadLibrary(wm
); break;
1535 case MODULE32_BI
: BUILTIN32_UnloadLibrary(wm
); break;
1538 ERR("Invalid or unhandled MODREF type %d encountered (wm=%p)\n", wm
->type
, wm
);
1543 /***********************************************************************
1546 BOOL WINAPI
FreeLibrary(HINSTANCE hLibModule
)
1551 EnterCriticalSection( &PROCESS_Current()->crit_section
);
1552 PROCESS_Current()->free_lib_count
++;
1554 wm
= MODULE32_LookupHMODULE( hLibModule
);
1555 if ( !wm
|| !hLibModule
)
1556 SetLastError( ERROR_INVALID_HANDLE
);
1558 retv
= MODULE_FreeLibrary( wm
);
1560 PROCESS_Current()->free_lib_count
--;
1561 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
1566 /***********************************************************************
1567 * MODULE_DecRefCount
1569 * NOTE: Assumes that the process critical section is held!
1571 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
1575 if ( wm
->flags
& WINE_MODREF_MARKER
)
1578 if ( wm
->refCount
<= 0 )
1582 TRACE("(%s) refCount: %d\n", wm
->modname
, wm
->refCount
);
1584 if ( wm
->refCount
== 0 )
1586 wm
->flags
|= WINE_MODREF_MARKER
;
1588 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1590 MODULE_DecRefCount( wm
->deps
[i
] );
1592 wm
->flags
&= ~WINE_MODREF_MARKER
;
1596 /***********************************************************************
1597 * MODULE_FreeLibrary
1599 * NOTE: Assumes that the process critical section is held!
1601 BOOL
MODULE_FreeLibrary( WINE_MODREF
*wm
)
1603 TRACE("(%s) - START\n", wm
->modname
);
1605 /* Recursively decrement reference counts */
1606 MODULE_DecRefCount( wm
);
1608 /* Call process detach notifications */
1609 if ( PROCESS_Current()->free_lib_count
<= 1 )
1611 struct unload_dll_request
*req
= get_req_buffer();
1613 MODULE_DllProcessDetach( FALSE
, NULL
);
1614 req
->base
= (void *)wm
->module
;
1615 server_call_noerr( REQ_UNLOAD_DLL
);
1620 MODULE_FlushModrefs();
1626 /***********************************************************************
1627 * FreeLibraryAndExitThread
1629 VOID WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
1631 FreeLibrary(hLibModule
);
1632 ExitThread(dwExitCode
);
1635 /***********************************************************************
1636 * PrivateLoadLibrary (KERNEL32)
1638 * FIXME: rough guesswork, don't know what "Private" means
1640 HINSTANCE WINAPI
PrivateLoadLibrary(LPCSTR libname
)
1642 return (HINSTANCE
)LoadLibrary16(libname
);
1647 /***********************************************************************
1648 * PrivateFreeLibrary (KERNEL32)
1650 * FIXME: rough guesswork, don't know what "Private" means
1652 void WINAPI
PrivateFreeLibrary(HINSTANCE handle
)
1654 FreeLibrary16((HINSTANCE16
)handle
);
1658 /***********************************************************************
1659 * WIN32_GetProcAddress16 (KERNEL32.36)
1660 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1662 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1668 WARN("hModule may not be 0!\n");
1669 return (FARPROC16
)0;
1671 if (HIWORD(hModule
))
1673 WARN("hModule is Win32 handle (%08x)\n", hModule
);
1674 return (FARPROC16
)0;
1676 hModule
= GetExePtr( hModule
);
1678 ordinal
= NE_GetOrdinal( hModule
, name
);
1679 TRACE("%04x '%s'\n", hModule
, name
);
1681 ordinal
= LOWORD(name
);
1682 TRACE("%04x %04x\n", hModule
, ordinal
);
1684 if (!ordinal
) return (FARPROC16
)0;
1685 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1686 TRACE("returning %08x\n",(UINT
)ret
);
1690 /***********************************************************************
1691 * GetProcAddress16 (KERNEL.50)
1693 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, SEGPTR name
)
1698 if (!hModule
) hModule
= GetCurrentTask();
1699 hModule
= GetExePtr( hModule
);
1701 if (HIWORD(name
) != 0)
1703 ordinal
= NE_GetOrdinal( hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1704 TRACE("%04x '%s'\n", hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1708 ordinal
= LOWORD(name
);
1709 TRACE("%04x %04x\n", hModule
, ordinal
);
1711 if (!ordinal
) return (FARPROC16
)0;
1713 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1715 TRACE("returning %08x\n", (UINT
)ret
);
1720 /***********************************************************************
1721 * GetProcAddress (KERNEL32.257)
1723 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
1725 return MODULE_GetProcAddress( hModule
, function
, TRUE
);
1728 /***********************************************************************
1729 * GetProcAddress32 (KERNEL.453)
1731 FARPROC WINAPI
GetProcAddress32_16( HMODULE hModule
, LPCSTR function
)
1733 return MODULE_GetProcAddress( hModule
, function
, FALSE
);
1736 /***********************************************************************
1737 * MODULE_GetProcAddress (internal)
1739 FARPROC
MODULE_GetProcAddress(
1740 HMODULE hModule
, /* [in] current module handle */
1741 LPCSTR function
, /* [in] function to be looked up */
1744 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1747 if (HIWORD(function
))
1748 TRACE_(win32
)("(%08lx,%s)\n",(DWORD
)hModule
,function
);
1750 TRACE_(win32
)("(%08lx,%p)\n",(DWORD
)hModule
,function
);
1752 SetLastError(ERROR_INVALID_HANDLE
);
1758 retproc
= PE_FindExportedFunction( wm
, function
, snoop
);
1759 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1762 retproc
= ELF_FindExportedFunction( wm
, function
);
1763 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1766 ERR("wine_modref type %d not handled.\n",wm
->type
);
1767 SetLastError(ERROR_INVALID_HANDLE
);
1773 /***********************************************************************
1774 * RtlImageNtHeader (NTDLL)
1776 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
1779 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1780 * but we could get HMODULE16 or the like (think builtin modules)
1783 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1784 if (!wm
|| (wm
->type
!= MODULE32_PE
)) return (PIMAGE_NT_HEADERS
)0;
1785 return PE_HEADER(wm
->module
);
1789 /***************************************************************************
1790 * HasGPHandler (KERNEL.338)
1793 #include "pshpack1.h"
1794 typedef struct _GPHANDLERDEF
1801 #include "poppack.h"
1803 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1808 GPHANDLERDEF
*gpHandler
;
1810 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1811 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1812 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1813 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1814 && (gpHandler
= PTR_SEG_TO_LIN( gpPtr
)) != NULL
)
1816 while (gpHandler
->selector
)
1818 if ( SELECTOROF(address
) == gpHandler
->selector
1819 && OFFSETOF(address
) >= gpHandler
->rangeStart
1820 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1821 return PTR_SEG_OFF_TO_SEGPTR( gpHandler
->selector
,
1822 gpHandler
->handler
);