4 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
13 #include "wine/winuser16.h"
14 #include "wine/winbase16.h"
27 #include "selectors.h"
28 #include "stackframe.h"
30 #include "debugtools.h"
32 #include "loadorder.h"
35 DEFAULT_DEBUG_CHANNEL(module
)
36 DECLARE_DEBUG_CHANNEL(win32
)
38 /*************************************************************************
40 * Walk MODREFs for input process ID
42 void MODULE_WalkModref( DWORD id
)
45 WINE_MODREF
*zwm
, *prev
= NULL
;
46 PDB
*pdb
= PROCESS_IdToPDB( id
);
49 MESSAGE("Invalid process id (pid)\n");
53 MESSAGE("Modref list for process pdb=%p\n", pdb
);
54 MESSAGE("Modref next prev handle deps flags name\n");
55 for ( zwm
= pdb
->modref_list
; zwm
; zwm
= zwm
->next
) {
56 MESSAGE("%p %p %p %04x %5d %04x %s\n", zwm
, zwm
->next
, zwm
->prev
,
57 zwm
->module
, zwm
->nDeps
, zwm
->flags
, zwm
->modname
);
58 for ( i
= 0; i
< zwm
->nDeps
; i
++ ) {
60 MESSAGE(" %d %p %s\n", i
, zwm
->deps
[i
], zwm
->deps
[i
]->modname
);
62 if (prev
!= zwm
->prev
)
63 MESSAGE(" --> modref corrupt, previous pointer wrong!!\n");
68 /*************************************************************************
69 * MODULE32_LookupHMODULE
70 * looks for the referenced HMODULE in the current process
72 WINE_MODREF
*MODULE32_LookupHMODULE( HMODULE hmod
)
77 return PROCESS_Current()->exe_modref
;
80 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod
);
83 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
=wm
->next
)
84 if (wm
->module
== hmod
)
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" };
101 /* Skip calls for modules loaded with special load flags */
103 if ( ( wm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
)
104 || ( wm
->flags
& WINE_MODREF_LOAD_AS_DATAFILE
) )
108 TRACE("(%s,%s,%p) - CALL\n",
109 wm
->modname
, typeName
[type
], lpReserved
);
111 /* Call the initialization routine */
115 retv
= PE_InitDLL( wm
, type
, lpReserved
);
119 /* no need to do that, dlopen() already does */
123 ERR("wine_modref type %d not handled.\n", wm
->type
);
128 TRACE("(%s,%s,%p) - RETURN %d\n",
129 wm
->modname
, typeName
[type
], lpReserved
, retv
);
134 /*************************************************************************
135 * MODULE_DllProcessAttach
137 * Send the process attach notification to all DLLs the given module
138 * depends on (recursively). This is somewhat complicated due to the fact that
140 * - we have to respect the module dependencies, i.e. modules implicitly
141 * referenced by another module have to be initialized before the module
142 * itself can be initialized
144 * - the initialization routine of a DLL can itself call LoadLibrary,
145 * thereby introducing a whole new set of dependencies (even involving
146 * the 'old' modules) at any time during the whole process
148 * (Note that this routine can be recursively entered not only directly
149 * from itself, but also via LoadLibrary from one of the called initialization
152 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
153 * the process *detach* notifications to be sent in the correct order.
154 * This must not only take into account module dependencies, but also
155 * 'hidden' dependencies created by modules calling LoadLibrary in their
156 * attach notification routine.
158 * The strategy is rather simple: we move a WINE_MODREF to the head of the
159 * list after the attach notification has returned. This implies that the
160 * detach notifications are called in the reverse of the sequence the attach
161 * notifications *returned*.
163 * NOTE: Assumes that the process critical section is held!
166 BOOL
MODULE_DllProcessAttach( WINE_MODREF
*wm
, LPVOID lpReserved
)
172 /* prevent infinite recursion in case of cyclical dependencies */
173 if ( ( wm
->flags
& WINE_MODREF_MARKER
)
174 || ( wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
177 TRACE("(%s,%p) - START\n", wm
->modname
, lpReserved
);
179 /* Tag current MODREF to prevent recursive loop */
180 wm
->flags
|= WINE_MODREF_MARKER
;
182 /* Recursively attach all DLLs this one depends on */
183 for ( i
= 0; retv
&& i
< wm
->nDeps
; i
++ )
185 retv
= MODULE_DllProcessAttach( wm
->deps
[i
], lpReserved
);
187 /* Call DLL entry point */
190 retv
= MODULE_InitDll( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
192 wm
->flags
|= WINE_MODREF_PROCESS_ATTACHED
;
195 /* Re-insert MODREF at head of list */
196 if ( retv
&& wm
->prev
)
198 wm
->prev
->next
= wm
->next
;
199 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
202 wm
->next
= PROCESS_Current()->modref_list
;
203 PROCESS_Current()->modref_list
= wm
->next
->prev
= wm
;
206 /* Remove recursion flag */
207 wm
->flags
&= ~WINE_MODREF_MARKER
;
209 TRACE("(%s,%p) - END\n", wm
->modname
, lpReserved
);
214 /*************************************************************************
215 * MODULE_DllProcessDetach
217 * Send DLL process detach notifications. See the comment about calling
218 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
219 * is set, only DLLs with zero refcount are notified.
221 * NOTE: Assumes that the process critical section is held!
224 void MODULE_DllProcessDetach( BOOL bForceDetach
, LPVOID lpReserved
)
230 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
232 /* Check whether to detach this DLL */
233 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
235 if ( wm
->refCount
> 0 && !bForceDetach
)
238 /* Call detach notification */
239 wm
->flags
&= ~WINE_MODREF_PROCESS_ATTACHED
;
240 MODULE_InitDll( wm
, DLL_PROCESS_DETACH
, lpReserved
);
242 /* Restart at head of WINE_MODREF list, as entries might have
243 been added and/or removed while performing the call ... */
249 /*************************************************************************
250 * MODULE_DllThreadAttach
252 * Send DLL thread attach notifications. These are sent in the
253 * reverse sequence of process detach notification.
256 void MODULE_DllThreadAttach( LPVOID lpReserved
)
260 EnterCriticalSection( &PROCESS_Current()->crit_section
);
262 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
266 for ( ; wm
; wm
= wm
->prev
)
268 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
270 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
273 MODULE_InitDll( wm
, DLL_THREAD_ATTACH
, lpReserved
);
276 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
279 /*************************************************************************
280 * MODULE_DllThreadDetach
282 * Send DLL thread detach notifications. These are sent in the
283 * same sequence as process detach notification.
286 void MODULE_DllThreadDetach( LPVOID lpReserved
)
290 EnterCriticalSection( &PROCESS_Current()->crit_section
);
292 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
294 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
296 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
299 MODULE_InitDll( wm
, DLL_THREAD_DETACH
, lpReserved
);
302 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
305 /****************************************************************************
306 * DisableThreadLibraryCalls (KERNEL32.74)
308 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
310 BOOL WINAPI
DisableThreadLibraryCalls( HMODULE hModule
)
315 EnterCriticalSection( &PROCESS_Current()->crit_section
);
317 wm
= MODULE32_LookupHMODULE( hModule
);
321 wm
->flags
|= WINE_MODREF_NO_DLL_CALLS
;
323 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
328 /*************************************************************************
329 * MODULE_SendLoadDLLEvents
331 * Sends DEBUG_DLL_LOAD events for all outstanding modules.
333 * NOTE: Assumes that the process critical section is held!
336 void MODULE_SendLoadDLLEvents( void )
340 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
342 if ( wm
->type
!= MODULE32_PE
) continue;
343 if ( wm
== PROCESS_Current()->exe_modref
) continue;
344 if ( wm
->flags
& WINE_MODREF_DEBUG_EVENT_SENT
) continue;
346 DEBUG_SendLoadDLLEvent( -1 /*FIXME*/, wm
->module
, &wm
->modname
);
347 wm
->flags
|= WINE_MODREF_DEBUG_EVENT_SENT
;
352 /***********************************************************************
353 * MODULE_CreateDummyModule
355 * Create a dummy NE module for Win32 or Winelib.
357 HMODULE
MODULE_CreateDummyModule( LPCSTR filename
, WORD version
)
361 SEGTABLEENTRY
*pSegment
;
364 const char* basename
;
368 /* Extract base filename */
369 basename
= strrchr(filename
, '\\');
370 if (!basename
) basename
= filename
;
372 len
= strlen(basename
);
373 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
375 /* Allocate module */
376 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
377 + strlen(filename
) + 1;
378 size
= sizeof(NE_MODULE
) +
379 /* loaded file info */
381 /* segment table: DS,CS */
382 2 * sizeof(SEGTABLEENTRY
) +
385 /* several empty tables */
388 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
389 if (!hModule
) return (HMODULE
)11; /* invalid exe */
391 FarSetOwner16( hModule
, hModule
);
392 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
394 /* Set all used entries */
395 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
402 pModule
->heap_size
= 0;
403 pModule
->stack_size
= 0;
404 pModule
->seg_count
= 2;
405 pModule
->modref_count
= 0;
406 pModule
->nrname_size
= 0;
407 pModule
->fileinfo
= sizeof(NE_MODULE
);
408 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
409 pModule
->expected_version
= version
;
410 pModule
->self
= hModule
;
412 /* Set loaded file information */
413 ofs
= (OFSTRUCT
*)(pModule
+ 1);
414 memset( ofs
, 0, of_size
);
415 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
416 strcpy( ofs
->szPathName
, filename
);
418 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + of_size
);
419 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
422 pSegment
->flags
= NE_SEGFLAGS_DATA
;
423 pSegment
->minsize
= 0x1000;
430 pStr
= (char *)pSegment
;
431 pModule
->name_table
= (int)pStr
- (int)pModule
;
433 strncpy( pStr
+1, basename
, len
);
437 /* All tables zero terminated */
438 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
=
439 (int)pStr
- (int)pModule
;
441 NE_RegisterModule( pModule
);
446 /**********************************************************************
447 * MODULE_FindModule32
449 * Find a (loaded) win32 module depending on path
452 * the module handle if found
455 WINE_MODREF
*MODULE_FindModule(
456 LPCSTR path
/* [in] pathname of module/library to be found */
459 char dllname
[260], *p
;
461 /* Append .DLL to name if no extension present */
462 strcpy( dllname
, path
);
463 if (!(p
= strrchr( dllname
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
464 strcat( dllname
, ".DLL" );
466 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
468 if ( !strcasecmp( dllname
, wm
->modname
) )
470 if ( !strcasecmp( dllname
, wm
->filename
) )
472 if ( !strcasecmp( dllname
, wm
->short_modname
) )
474 if ( !strcasecmp( dllname
, wm
->short_filename
) )
481 /***********************************************************************
482 * MODULE_GetBinaryType
484 * The GetBinaryType function determines whether a file is executable
485 * or not and if it is it returns what type of executable it is.
486 * The type of executable is a property that determines in which
487 * subsystem an executable file runs under.
489 * Binary types returned:
490 * SCS_32BIT_BINARY: A Win32 based application
491 * SCS_DOS_BINARY: An MS-Dos based application
492 * SCS_WOW_BINARY: A Win16 based application
493 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
494 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
495 * SCS_OS216_BINARY: A 16bit OS/2 based application
497 * Returns TRUE if the file is an executable in which case
498 * the value pointed by lpBinaryType is set.
499 * Returns FALSE if the file is not an executable or if the function fails.
501 * To do so it opens the file and reads in the header information
502 * if the extended header information is not present it will
503 * assume that the file is a DOS executable.
504 * If the extended header information is present it will
505 * determine if the file is a 16 or 32 bit Windows executable
506 * by check the flags in the header.
508 * Note that .COM and .PIF files are only recognized by their
509 * file name extension; but Windows does it the same way ...
511 static BOOL
MODULE_GetBinaryType( HANDLE hfile
, LPCSTR filename
,
512 LPDWORD lpBinaryType
)
514 IMAGE_DOS_HEADER mz_header
;
518 /* Seek to the start of the file and read the DOS header information.
520 if ( SetFilePointer( hfile
, 0, NULL
, SEEK_SET
) != -1
521 && ReadFile( hfile
, &mz_header
, sizeof(mz_header
), &len
, NULL
)
522 && len
== sizeof(mz_header
) )
524 /* Now that we have the header check the e_magic field
525 * to see if this is a dos image.
527 if ( mz_header
.e_magic
== IMAGE_DOS_SIGNATURE
)
529 BOOL lfanewValid
= FALSE
;
530 /* We do have a DOS image so we will now try to seek into
531 * the file by the amount indicated by the field
532 * "Offset to extended header" and read in the
533 * "magic" field information at that location.
534 * This will tell us if there is more header information
537 /* But before we do we will make sure that header
538 * structure encompasses the "Offset to extended header"
541 if ( (mz_header
.e_cparhdr
<<4) >= sizeof(IMAGE_DOS_HEADER
) )
542 if ( ( mz_header
.e_crlc
== 0 ) ||
543 ( mz_header
.e_lfarlc
>= sizeof(IMAGE_DOS_HEADER
) ) )
544 if ( mz_header
.e_lfanew
>= sizeof(IMAGE_DOS_HEADER
)
545 && SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
546 && ReadFile( hfile
, magic
, sizeof(magic
), &len
, NULL
)
547 && len
== sizeof(magic
) )
552 /* If we cannot read this "extended header" we will
553 * assume that we have a simple DOS executable.
555 *lpBinaryType
= SCS_DOS_BINARY
;
560 /* Reading the magic field succeeded so
561 * we will try to determine what type it is.
563 if ( *(DWORD
*)magic
== IMAGE_NT_SIGNATURE
)
565 /* This is an NT signature.
567 *lpBinaryType
= SCS_32BIT_BINARY
;
570 else if ( *(WORD
*)magic
== IMAGE_OS2_SIGNATURE
)
572 /* The IMAGE_OS2_SIGNATURE indicates that the
573 * "extended header is a Windows executable (NE)
574 * header." This can mean either a 16-bit OS/2
575 * or a 16-bit Windows or even a DOS program
576 * (running under a DOS extender). To decide
577 * which, we'll have to read the NE header.
581 if ( SetFilePointer( hfile
, mz_header
.e_lfanew
, NULL
, SEEK_SET
) != -1
582 && ReadFile( hfile
, &ne
, sizeof(ne
), &len
, NULL
)
583 && len
== sizeof(ne
) )
585 switch ( ne
.operating_system
)
587 case 2: *lpBinaryType
= SCS_WOW_BINARY
; return TRUE
;
588 case 5: *lpBinaryType
= SCS_DOS_BINARY
; return TRUE
;
589 default: *lpBinaryType
= SCS_OS216_BINARY
; return TRUE
;
592 /* Couldn't read header, so abort. */
597 /* Unknown extended header, but this file is nonetheless
600 *lpBinaryType
= SCS_DOS_BINARY
;
607 /* If we get here, we don't even have a correct MZ header.
608 * Try to check the file extension for known types ...
610 ptr
= strrchr( filename
, '.' );
611 if ( ptr
&& !strchr( ptr
, '\\' ) && !strchr( ptr
, '/' ) )
613 if ( !lstrcmpiA( ptr
, ".COM" ) )
615 *lpBinaryType
= SCS_DOS_BINARY
;
619 if ( !lstrcmpiA( ptr
, ".PIF" ) )
621 *lpBinaryType
= SCS_PIF_BINARY
;
629 /***********************************************************************
630 * GetBinaryTypeA [KERNEL32.280]
632 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
637 TRACE_(win32
)("%s\n", lpApplicationName
);
641 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
644 /* Open the file indicated by lpApplicationName for reading.
646 hfile
= CreateFileA( lpApplicationName
, GENERIC_READ
, 0,
647 NULL
, OPEN_EXISTING
, 0, -1 );
648 if ( hfile
== INVALID_HANDLE_VALUE
)
653 ret
= MODULE_GetBinaryType( hfile
, lpApplicationName
, lpBinaryType
);
657 CloseHandle( hfile
);
662 /***********************************************************************
663 * GetBinaryTypeW [KERNEL32.281]
665 BOOL WINAPI
GetBinaryTypeW( LPCWSTR lpApplicationName
, LPDWORD lpBinaryType
)
670 TRACE_(win32
)("%s\n", debugstr_w(lpApplicationName
) );
674 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
677 /* Convert the wide string to a ascii string.
679 strNew
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName
);
681 if ( strNew
!= NULL
)
683 ret
= GetBinaryTypeA( strNew
, lpBinaryType
);
685 /* Free the allocated string.
687 HeapFree( GetProcessHeap(), 0, strNew
);
693 /**********************************************************************
694 * MODULE_CreateUnixProcess
696 static BOOL
MODULE_CreateUnixProcess( LPCSTR filename
, LPCSTR lpCmdLine
,
697 LPSTARTUPINFOA lpStartupInfo
,
698 LPPROCESS_INFORMATION lpProcessInfo
,
701 DOS_FULL_NAME full_name
;
702 const char *unixfilename
= filename
;
703 const char *argv
[256], **argptr
;
704 char *cmdline
= NULL
;
707 /* Get Unix file name and iconic flag */
709 if ( lpStartupInfo
->dwFlags
& STARTF_USESHOWWINDOW
)
710 if ( lpStartupInfo
->wShowWindow
== SW_SHOWMINIMIZED
711 || lpStartupInfo
->wShowWindow
== SW_SHOWMINNOACTIVE
)
714 /* Build argument list */
720 p
= cmdline
= strdup(lpCmdLine
);
721 if (strchr(filename
, '/') || strchr(filename
, ':') || strchr(filename
, '\\'))
723 if ( DOSFS_GetFullName( filename
, TRUE
, &full_name
) )
724 unixfilename
= full_name
.long_name
;
726 *argptr
++ = unixfilename
;
727 if (iconic
) *argptr
++ = "-iconic";
730 while (*p
&& (*p
== ' ' || *p
== '\t')) *p
++ = '\0';
733 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
739 if (iconic
) *argptr
++ = "-iconic";
740 *argptr
++ = lpCmdLine
;
744 /* Fork and execute */
748 /* Note: don't use Wine routines here, as this process
749 has not been correctly initialized! */
751 execvp( argv
[0], (char**)argv
);
755 fprintf( stderr
, "CreateProcess: can't exec 'wine %s'\n",
760 /* Fake success return value */
762 memset( lpProcessInfo
, '\0', sizeof( *lpProcessInfo
) );
763 lpProcessInfo
->hProcess
= INVALID_HANDLE_VALUE
;
764 lpProcessInfo
->hThread
= INVALID_HANDLE_VALUE
;
765 if (cmdline
) free(cmdline
);
767 SetLastError( ERROR_SUCCESS
);
771 /***********************************************************************
772 * WinExec16 (KERNEL.166)
774 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
778 SYSLEVEL_ReleaseWin16Lock();
779 hInst
= WinExec( lpCmdLine
, nCmdShow
);
780 SYSLEVEL_RestoreWin16Lock();
785 /***********************************************************************
786 * WinExec (KERNEL32.566)
788 HINSTANCE WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
791 UINT16 paramCmdShow
[2];
794 return 2; /* File not found */
796 /* Set up LOADPARAMS buffer for LoadModule */
798 memset( ¶ms
, '\0', sizeof(params
) );
799 params
.lpCmdLine
= (LPSTR
)lpCmdLine
;
800 params
.lpCmdShow
= paramCmdShow
;
801 params
.lpCmdShow
[0] = 2;
802 params
.lpCmdShow
[1] = nCmdShow
;
804 /* Now load the executable file */
806 return LoadModule( NULL
, ¶ms
);
809 /**********************************************************************
810 * LoadModule (KERNEL32.499)
812 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
814 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
815 PROCESS_INFORMATION info
;
816 STARTUPINFOA startup
;
821 memset( &startup
, '\0', sizeof(startup
) );
822 startup
.cb
= sizeof(startup
);
823 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
824 startup
.wShowWindow
= params
->lpCmdShow
? params
->lpCmdShow
[1] : 0;
826 if ( !CreateProcessA( name
, params
->lpCmdLine
,
827 NULL
, NULL
, FALSE
, 0, params
->lpEnvAddress
,
828 NULL
, &startup
, &info
) )
830 hInstance
= GetLastError();
831 if ( hInstance
< 32 ) return hInstance
;
833 FIXME("Strange error set by CreateProcess: %d\n", hInstance
);
837 /* Get 16-bit hInstance/hTask from process */
838 pdb
= PROCESS_IdToPDB( info
.dwProcessId
);
839 tdb
= pdb
? (TDB
*)GlobalLock16( pdb
->task
) : NULL
;
840 hInstance
= tdb
&& tdb
->hInstance
? tdb
->hInstance
: pdb
? pdb
->task
: 0;
841 /* If there is no hInstance (32-bit process) return a dummy value
843 * FIXME: should do this in all cases and fix Win16 callers */
844 if (!hInstance
) hInstance
= 33;
846 /* Close off the handles */
847 CloseHandle( info
.hThread
);
848 CloseHandle( info
.hProcess
);
853 /*************************************************************************
856 * Get next blank delimited token from input string. If quoted then
857 * process till matching quote and then till blank.
859 * Returns number of characters in token (not including \0). On
860 * end of string (EOS), returns a 0.
862 * from (IO) address of start of input string to scan, updated to
863 * next non-processed character.
864 * to (IO) address of start of output string (previous token \0
865 * char), updated to end of new output string (the \0
868 static int get_makename_token(LPCSTR
*from
, LPSTR
*to
)
871 LPCSTR to_old
= *to
; /* only used for tracing */
873 while ( **from
== ' ') {
874 /* Copy leading blanks (separators between previous */
875 /* token and this token). */
882 while ( (**from
!= 0) && (**from
!= ' ') && (**from
!= '"') ) {
883 **to
= **from
; (*from
)++; (*to
)++; len
++;
885 if ( **from
== '"' ) {
886 /* Handle quoted string. */
888 if ( !strchr(*from
, '"') ) {
889 /* fail - no closing quote. Return entire string */
890 while ( **from
!= 0 ) {
891 **to
= **from
; (*from
)++; (*to
)++; len
++;
895 while( **from
!= '"') {
905 /* either EOS or ' ' */
910 **to
= 0; /* terminate output string */
912 TRACE("returning token len=%d, string=%s\n", len
, to_old
);
917 /*************************************************************************
918 * make_lpCommandLine_name
920 * Try longer and longer strings from "line" to find an existing
921 * file name. Each attempt is delimited by a blank outside of quotes.
922 * Also will attempt to append ".exe" if requested and not already
923 * present. Returns the address of the remaining portion of the
928 static BOOL
make_lpCommandLine_name( LPCSTR line
, LPSTR name
, int namelen
,
940 /* scan over initial blanks if any */
941 while ( *from
== ' ') from
++;
943 /* get a token and append to previous data the check for existance */
945 if ( !get_makename_token( &from
, &to
) ) {
946 /* EOS has occured and not found - exit */
951 TRACE("checking if file exists '%s'\n", name
);
952 retlen
= SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, &lastpart
);
953 if ( retlen
&& (retlen
< sizeof(buffer
)) ) break;
956 /* if we have a non-null full path name in buffer then move to output */
958 if ( strlen(buffer
) <= namelen
) {
959 strcpy( name
, buffer
);
961 /* not enough space to return full path string */
962 FIXME("internal string not long enough, need %d\n",
967 /* all done, indicate end of module name and then trace and exit */
968 if (after
) *after
= from
;
969 TRACE("%i, selected file name '%s'\n and cmdline as %s\n",
970 found
, name
, debugstr_a(from
));
974 /*************************************************************************
975 * make_lpApplicationName_name
977 * Scan input string (the lpApplicationName) and remove any quotes
978 * if they are balanced.
982 static BOOL
make_lpApplicationName_name( LPCSTR line
, LPSTR name
, int namelen
)
985 LPSTR to
, to_end
, to_old
;
989 to_end
= to
+ sizeof(buffer
) - 1;
992 while ( *line
== ' ' ) line
++; /* point to beginning of string */
995 /* Copy all input till end, or quote */
996 while((*from
!= 0) && (*from
!= '"') && (to
< to_end
))
998 if (to
>= to_end
) { *to
= 0; break; }
1002 /* Handle quoted string. If there is a closing quote, copy all */
1003 /* that is inside. */
1005 if (!strchr(from
, '"'))
1007 /* fail - no closing quote */
1008 to
= to_old
; /* restore to previous attempt */
1009 *to
= 0; /* end string */
1010 break; /* exit with previous attempt */
1012 while((*from
!= '"') && (to
< to_end
)) *to
++ = *from
++;
1013 if (to
>= to_end
) { *to
= 0; break; }
1015 continue; /* past quoted string, so restart from top */
1018 *to
= 0; /* terminate output string */
1019 to_old
= to
; /* save for possible use in unmatched quote case */
1021 /* loop around keeping the blank as part of file name */
1023 break; /* exit if out of input string */
1026 if (!SearchPathA( NULL
, buffer
, ".exe", namelen
, name
, NULL
)) {
1027 TRACE("file not found '%s'\n", buffer
);
1031 TRACE("selected as file name '%s'\n", name
);
1035 /**********************************************************************
1036 * CreateProcessA (KERNEL32.171)
1038 BOOL WINAPI
CreateProcessA( LPCSTR lpApplicationName
, LPSTR lpCommandLine
,
1039 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1040 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1041 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1042 LPVOID lpEnvironment
, LPCSTR lpCurrentDirectory
,
1043 LPSTARTUPINFOA lpStartupInfo
,
1044 LPPROCESS_INFORMATION lpProcessInfo
)
1047 BOOL found_file
= FALSE
;
1050 char name
[256], dummy
[256];
1051 LPCSTR cmdline
= NULL
;
1054 /* Get name and command line */
1056 if (!lpApplicationName
&& !lpCommandLine
)
1058 SetLastError( ERROR_FILE_NOT_FOUND
);
1062 /* Process the AppName and/or CmdLine to get module name and path */
1066 if (lpApplicationName
)
1068 found_file
= make_lpApplicationName_name( lpApplicationName
, name
, sizeof(name
) );
1070 make_lpCommandLine_name( lpCommandLine
, dummy
, sizeof ( dummy
), &cmdline
);
1072 cmdline
= lpApplicationName
;
1077 found_file
= make_lpCommandLine_name( lpCommandLine
, name
, sizeof ( name
), &cmdline
);
1080 if ( !found_file
) {
1081 /* make an early exit if file not found - save second pass */
1082 SetLastError( ERROR_FILE_NOT_FOUND
);
1086 if (!cmdline
) cmdline
= "";
1087 tidy_cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(name
) + strlen(cmdline
) + 3 );
1088 TRACE_(module
)("tidy_cmdline: name '%s'[%d], cmdline '%s'[%d]\n",
1089 name
, strlen(name
), cmdline
, strlen(cmdline
));
1090 sprintf( tidy_cmdline
, "\"%s\"%s", name
, cmdline
);
1092 /* Warn if unsupported features are used */
1094 if (dwCreationFlags
& DETACHED_PROCESS
)
1095 FIXME("(%s,...): DETACHED_PROCESS ignored\n", name
);
1096 if (dwCreationFlags
& CREATE_NEW_CONSOLE
)
1097 FIXME("(%s,...): CREATE_NEW_CONSOLE ignored\n", name
);
1098 if (dwCreationFlags
& NORMAL_PRIORITY_CLASS
)
1099 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1100 if (dwCreationFlags
& IDLE_PRIORITY_CLASS
)
1101 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1102 if (dwCreationFlags
& HIGH_PRIORITY_CLASS
)
1103 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1104 if (dwCreationFlags
& REALTIME_PRIORITY_CLASS
)
1105 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1106 if (dwCreationFlags
& CREATE_NEW_PROCESS_GROUP
)
1107 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1108 if (dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
)
1109 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1110 if (dwCreationFlags
& CREATE_SEPARATE_WOW_VDM
)
1111 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1112 if (dwCreationFlags
& CREATE_SHARED_WOW_VDM
)
1113 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1114 if (dwCreationFlags
& CREATE_DEFAULT_ERROR_MODE
)
1115 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1116 if (dwCreationFlags
& CREATE_NO_WINDOW
)
1117 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1118 if (dwCreationFlags
& PROFILE_USER
)
1119 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1120 if (dwCreationFlags
& PROFILE_KERNEL
)
1121 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1122 if (dwCreationFlags
& PROFILE_SERVER
)
1123 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1124 if (lpCurrentDirectory
)
1125 FIXME("(%s,...): lpCurrentDirectory %s ignored\n",
1126 name
, lpCurrentDirectory
);
1127 if (lpStartupInfo
->lpDesktop
)
1128 FIXME("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
1129 name
, lpStartupInfo
->lpDesktop
);
1130 if (lpStartupInfo
->lpTitle
)
1131 FIXME("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
1132 name
, lpStartupInfo
->lpTitle
);
1133 if (lpStartupInfo
->dwFlags
& STARTF_USECOUNTCHARS
)
1134 FIXME("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
1135 name
, lpStartupInfo
->dwXCountChars
, lpStartupInfo
->dwYCountChars
);
1136 if (lpStartupInfo
->dwFlags
& STARTF_USEFILLATTRIBUTE
)
1137 FIXME("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
1138 name
, lpStartupInfo
->dwFillAttribute
);
1139 if (lpStartupInfo
->dwFlags
& STARTF_RUNFULLSCREEN
)
1140 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1141 if (lpStartupInfo
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1142 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1143 if (lpStartupInfo
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1144 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1145 if (lpStartupInfo
->dwFlags
& STARTF_USEHOTKEY
)
1146 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1149 /* Load file and create process */
1153 /* Open file and determine executable type */
1155 hFile
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
1156 NULL
, OPEN_EXISTING
, 0, -1 );
1157 if ( hFile
== INVALID_HANDLE_VALUE
)
1159 SetLastError( ERROR_FILE_NOT_FOUND
);
1160 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1164 if ( !MODULE_GetBinaryType( hFile
, name
, &type
) )
1166 CloseHandle( hFile
);
1168 /* FIXME: Try Unix executable only when appropriate! */
1169 if ( MODULE_CreateUnixProcess( name
, tidy_cmdline
,
1170 lpStartupInfo
, lpProcessInfo
, FALSE
) )
1172 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1175 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1176 SetLastError( ERROR_BAD_FORMAT
);
1181 /* Create process */
1185 case SCS_32BIT_BINARY
:
1186 retv
= PE_CreateProcess( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1187 lpProcessAttributes
, lpThreadAttributes
,
1188 bInheritHandles
, dwCreationFlags
,
1189 lpStartupInfo
, lpProcessInfo
);
1192 case SCS_DOS_BINARY
:
1193 retv
= MZ_CreateProcess( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1194 lpProcessAttributes
, lpThreadAttributes
,
1195 bInheritHandles
, dwCreationFlags
,
1196 lpStartupInfo
, lpProcessInfo
);
1199 case SCS_WOW_BINARY
:
1200 retv
= NE_CreateProcess( hFile
, name
, tidy_cmdline
, lpEnvironment
,
1201 lpProcessAttributes
, lpThreadAttributes
,
1202 bInheritHandles
, dwCreationFlags
,
1203 lpStartupInfo
, lpProcessInfo
);
1206 case SCS_PIF_BINARY
:
1207 case SCS_POSIX_BINARY
:
1208 case SCS_OS216_BINARY
:
1209 FIXME("Unsupported executable type: %ld\n", type
);
1213 SetLastError( ERROR_BAD_FORMAT
);
1218 CloseHandle( hFile
);
1220 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1224 /**********************************************************************
1225 * CreateProcessW (KERNEL32.172)
1227 * lpReserved is not converted
1229 BOOL WINAPI
CreateProcessW( LPCWSTR lpApplicationName
, LPWSTR lpCommandLine
,
1230 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1231 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1232 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1233 LPVOID lpEnvironment
, LPCWSTR lpCurrentDirectory
,
1234 LPSTARTUPINFOW lpStartupInfo
,
1235 LPPROCESS_INFORMATION lpProcessInfo
)
1237 STARTUPINFOA StartupInfoA
;
1239 LPSTR lpApplicationNameA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName
);
1240 LPSTR lpCommandLineA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine
);
1241 LPSTR lpCurrentDirectoryA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory
);
1243 memcpy (&StartupInfoA
, lpStartupInfo
, sizeof(STARTUPINFOA
));
1244 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpDesktop
);
1245 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpTitle
);
1247 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(lpApplicationName
), debugstr_w(lpCommandLine
));
1249 if (lpStartupInfo
->lpReserved
)
1250 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo
->lpReserved
));
1252 ret
= CreateProcessA( lpApplicationNameA
, lpCommandLineA
,
1253 lpProcessAttributes
, lpThreadAttributes
,
1254 bInheritHandles
, dwCreationFlags
,
1255 lpEnvironment
, lpCurrentDirectoryA
,
1256 &StartupInfoA
, lpProcessInfo
);
1258 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA
);
1259 HeapFree( GetProcessHeap(), 0, lpCommandLineA
);
1260 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1261 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1266 /***********************************************************************
1267 * GetModuleHandle (KERNEL32.237)
1269 HMODULE WINAPI
GetModuleHandleA(LPCSTR module
)
1273 if ( module
== NULL
)
1274 wm
= PROCESS_Current()->exe_modref
;
1276 wm
= MODULE_FindModule( module
);
1278 return wm
? wm
->module
: 0;
1281 HMODULE WINAPI
GetModuleHandleW(LPCWSTR module
)
1284 LPSTR modulea
= HEAP_strdupWtoA( GetProcessHeap(), 0, module
);
1285 hModule
= GetModuleHandleA( modulea
);
1286 HeapFree( GetProcessHeap(), 0, modulea
);
1291 /***********************************************************************
1292 * GetModuleFileName32A (KERNEL32.235)
1294 DWORD WINAPI
GetModuleFileNameA(
1295 HMODULE hModule
, /* [in] module handle (32bit) */
1296 LPSTR lpFileName
, /* [out] filenamebuffer */
1297 DWORD size
/* [in] size of filenamebuffer */
1299 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1301 if (!wm
) /* can happen on start up or the like */
1304 if (PE_HEADER(wm
->module
)->OptionalHeader
.MajorOperatingSystemVersion
>= 4.0)
1305 lstrcpynA( lpFileName
, wm
->filename
, size
);
1307 lstrcpynA( lpFileName
, wm
->short_filename
, size
);
1309 TRACE("%s\n", lpFileName
);
1310 return strlen(lpFileName
);
1314 /***********************************************************************
1315 * GetModuleFileName32W (KERNEL32.236)
1317 DWORD WINAPI
GetModuleFileNameW( HMODULE hModule
, LPWSTR lpFileName
,
1320 LPSTR fnA
= (char*)HeapAlloc( GetProcessHeap(), 0, size
);
1321 DWORD res
= GetModuleFileNameA( hModule
, fnA
, size
);
1322 lstrcpynAtoW( lpFileName
, fnA
, size
);
1323 HeapFree( GetProcessHeap(), 0, fnA
);
1328 /***********************************************************************
1329 * LoadLibraryExA (KERNEL32)
1331 HMODULE WINAPI
LoadLibraryExA(LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1337 SetLastError(ERROR_INVALID_PARAMETER
);
1341 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1343 wm
= MODULE_LoadLibraryExA( libname
, hfile
, flags
);
1346 if ( PROCESS_Current()->flags
& PDB32_DEBUGGED
)
1347 MODULE_SendLoadDLLEvents();
1349 if ( !MODULE_DllProcessAttach( wm
, NULL
) )
1351 WARN_(module
)("Attach failed for module '%s', \n", libname
);
1352 MODULE_FreeLibrary(wm
);
1353 SetLastError(ERROR_DLL_INIT_FAILED
);
1358 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1360 return wm
? wm
->module
: 0;
1363 /***********************************************************************
1364 * MODULE_LoadLibraryExA (internal)
1366 * Load a PE style module according to the load order.
1368 * The HFILE parameter is not used and marked reserved in the SDK. I can
1369 * only guess that it should force a file to be mapped, but I rather
1370 * ignore the parameter because it would be extremely difficult to
1371 * integrate this with different types of module represenations.
1374 WINE_MODREF
*MODULE_LoadLibraryExA( LPCSTR libname
, HFILE hfile
, DWORD flags
)
1379 module_loadorder_t
*plo
;
1381 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1383 /* Check for already loaded module */
1384 if((pwm
= MODULE_FindModule(libname
)))
1386 if(!(pwm
->flags
& WINE_MODREF_MARKER
))
1388 TRACE("Already loaded module '%s' at 0x%08x, count=%d, \n", libname
, pwm
->module
, pwm
->refCount
);
1389 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1393 plo
= MODULE_GetLoadOrder(libname
);
1395 for(i
= 0; i
< MODULE_LOADORDER_NTYPES
; i
++)
1397 switch(plo
->loadorder
[i
])
1399 case MODULE_LOADORDER_DLL
:
1400 TRACE("Trying native dll '%s'\n", libname
);
1401 pwm
= PE_LoadLibraryExA(libname
, flags
, &err
);
1404 case MODULE_LOADORDER_ELFDLL
:
1405 TRACE("Trying elfdll '%s'\n", libname
);
1406 pwm
= ELFDLL_LoadLibraryExA(libname
, flags
, &err
);
1409 case MODULE_LOADORDER_SO
:
1410 TRACE("Trying so-library '%s'\n", libname
);
1411 pwm
= ELF_LoadLibraryExA(libname
, flags
, &err
);
1414 case MODULE_LOADORDER_BI
:
1415 TRACE("Trying built-in '%s'\n", libname
);
1416 pwm
= BUILTIN32_LoadLibraryExA(libname
, flags
, &err
);
1420 ERR("Got invalid loadorder type %d (%s index %d)\n", plo
->loadorder
[i
], plo
->modulename
, i
);
1423 case MODULE_LOADORDER_INVALID
: /* We ignore this as it is an empty entry */
1430 /* Initialize DLL just loaded */
1431 TRACE("Loaded module '%s' at 0x%08x, \n", libname
, pwm
->module
);
1433 /* Set the refCount here so that an attach failure will */
1434 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1437 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1442 if(err
!= ERROR_FILE_NOT_FOUND
)
1446 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname
, err
);
1448 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1452 /***********************************************************************
1453 * LoadLibraryA (KERNEL32)
1455 HMODULE WINAPI
LoadLibraryA(LPCSTR libname
) {
1456 return LoadLibraryExA(libname
,0,0);
1459 /***********************************************************************
1460 * LoadLibraryW (KERNEL32)
1462 HMODULE WINAPI
LoadLibraryW(LPCWSTR libnameW
)
1464 return LoadLibraryExW(libnameW
,0,0);
1467 /***********************************************************************
1468 * LoadLibrary32_16 (KERNEL.452)
1470 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
1474 SYSLEVEL_ReleaseWin16Lock();
1475 hModule
= LoadLibraryA( libname
);
1476 SYSLEVEL_RestoreWin16Lock();
1481 /***********************************************************************
1482 * LoadLibraryExW (KERNEL32)
1484 HMODULE WINAPI
LoadLibraryExW(LPCWSTR libnameW
,HANDLE hfile
,DWORD flags
)
1486 LPSTR libnameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW
);
1487 HMODULE ret
= LoadLibraryExA( libnameA
, hfile
, flags
);
1489 HeapFree( GetProcessHeap(), 0, libnameA
);
1493 /***********************************************************************
1494 * MODULE_FlushModrefs
1496 * NOTE: Assumes that the process critical section is held!
1498 * Remove all unused modrefs and call the internal unloading routines
1499 * for the library type.
1501 static void MODULE_FlushModrefs(void)
1503 WINE_MODREF
*wm
, *next
;
1505 for(wm
= PROCESS_Current()->modref_list
; wm
; wm
= next
)
1512 /* Unlink this modref from the chain */
1514 wm
->next
->prev
= wm
->prev
;
1516 wm
->prev
->next
= wm
->next
;
1517 if(wm
== PROCESS_Current()->modref_list
)
1518 PROCESS_Current()->modref_list
= wm
->next
;
1521 * The unloaders are also responsible for freeing the modref itself
1522 * because the loaders were responsible for allocating it.
1526 case MODULE32_PE
: PE_UnloadLibrary(wm
); break;
1527 case MODULE32_ELF
: ELF_UnloadLibrary(wm
); break;
1528 case MODULE32_ELFDLL
: ELFDLL_UnloadLibrary(wm
); break;
1529 case MODULE32_BI
: BUILTIN32_UnloadLibrary(wm
); break;
1532 ERR("Invalid or unhandled MODREF type %d encountered (wm=%p)\n", wm
->type
, wm
);
1537 /***********************************************************************
1540 BOOL WINAPI
FreeLibrary(HINSTANCE hLibModule
)
1545 EnterCriticalSection( &PROCESS_Current()->crit_section
);
1546 PROCESS_Current()->free_lib_count
++;
1548 wm
= MODULE32_LookupHMODULE( hLibModule
);
1549 if ( !wm
|| !hLibModule
)
1550 SetLastError( ERROR_INVALID_HANDLE
);
1552 retv
= MODULE_FreeLibrary( wm
);
1554 PROCESS_Current()->free_lib_count
--;
1555 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
1560 /***********************************************************************
1561 * MODULE_DecRefCount
1563 * NOTE: Assumes that the process critical section is held!
1565 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
1569 if ( wm
->flags
& WINE_MODREF_MARKER
)
1572 if ( wm
->refCount
<= 0 )
1576 TRACE("(%s) refCount: %d\n", wm
->modname
, wm
->refCount
);
1578 if ( wm
->refCount
== 0 )
1580 wm
->flags
|= WINE_MODREF_MARKER
;
1582 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1584 MODULE_DecRefCount( wm
->deps
[i
] );
1586 wm
->flags
&= ~WINE_MODREF_MARKER
;
1590 /***********************************************************************
1591 * MODULE_FreeLibrary
1593 * NOTE: Assumes that the process critical section is held!
1595 BOOL
MODULE_FreeLibrary( WINE_MODREF
*wm
)
1597 TRACE("(%s) - START\n", wm
->modname
);
1599 /* Recursively decrement reference counts */
1600 MODULE_DecRefCount( wm
);
1602 /* Call process detach notifications */
1603 if ( PROCESS_Current()->free_lib_count
<= 1 )
1605 MODULE_DllProcessDetach( FALSE
, NULL
);
1606 if (PROCESS_Current()->flags
& PDB32_DEBUGGED
)
1607 DEBUG_SendUnloadDLLEvent( wm
->module
);
1610 MODULE_FlushModrefs();
1612 TRACE("(%s) - END\n", wm
->modname
);
1618 /***********************************************************************
1619 * FreeLibraryAndExitThread
1621 VOID WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
1623 FreeLibrary(hLibModule
);
1624 ExitThread(dwExitCode
);
1627 /***********************************************************************
1628 * PrivateLoadLibrary (KERNEL32)
1630 * FIXME: rough guesswork, don't know what "Private" means
1632 HINSTANCE WINAPI
PrivateLoadLibrary(LPCSTR libname
)
1634 return (HINSTANCE
)LoadLibrary16(libname
);
1639 /***********************************************************************
1640 * PrivateFreeLibrary (KERNEL32)
1642 * FIXME: rough guesswork, don't know what "Private" means
1644 void WINAPI
PrivateFreeLibrary(HINSTANCE handle
)
1646 FreeLibrary16((HINSTANCE16
)handle
);
1650 /***********************************************************************
1651 * WIN32_GetProcAddress16 (KERNEL32.36)
1652 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1654 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1660 WARN("hModule may not be 0!\n");
1661 return (FARPROC16
)0;
1663 if (HIWORD(hModule
))
1665 WARN("hModule is Win32 handle (%08x)\n", hModule
);
1666 return (FARPROC16
)0;
1668 hModule
= GetExePtr( hModule
);
1670 ordinal
= NE_GetOrdinal( hModule
, name
);
1671 TRACE("%04x '%s'\n", hModule
, name
);
1673 ordinal
= LOWORD(name
);
1674 TRACE("%04x %04x\n", hModule
, ordinal
);
1676 if (!ordinal
) return (FARPROC16
)0;
1677 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1678 TRACE("returning %08x\n",(UINT
)ret
);
1682 /***********************************************************************
1683 * GetProcAddress16 (KERNEL.50)
1685 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, SEGPTR name
)
1690 if (!hModule
) hModule
= GetCurrentTask();
1691 hModule
= GetExePtr( hModule
);
1693 if (HIWORD(name
) != 0)
1695 ordinal
= NE_GetOrdinal( hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1696 TRACE("%04x '%s'\n", hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1700 ordinal
= LOWORD(name
);
1701 TRACE("%04x %04x\n", hModule
, ordinal
);
1703 if (!ordinal
) return (FARPROC16
)0;
1705 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1707 TRACE("returning %08x\n", (UINT
)ret
);
1712 /***********************************************************************
1713 * GetProcAddress32 (KERNEL32.257)
1715 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
1717 return MODULE_GetProcAddress( hModule
, function
, TRUE
);
1720 /***********************************************************************
1721 * WIN16_GetProcAddress32 (KERNEL.453)
1723 FARPROC WINAPI
GetProcAddress32_16( HMODULE hModule
, LPCSTR function
)
1725 return MODULE_GetProcAddress( hModule
, function
, FALSE
);
1728 /***********************************************************************
1729 * MODULE_GetProcAddress32 (internal)
1731 FARPROC
MODULE_GetProcAddress(
1732 HMODULE hModule
, /* [in] current module handle */
1733 LPCSTR function
, /* [in] function to be looked up */
1736 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1739 if (HIWORD(function
))
1740 TRACE_(win32
)("(%08lx,%s)\n",(DWORD
)hModule
,function
);
1742 TRACE_(win32
)("(%08lx,%p)\n",(DWORD
)hModule
,function
);
1744 SetLastError(ERROR_INVALID_HANDLE
);
1750 retproc
= PE_FindExportedFunction( wm
, function
, snoop
);
1751 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1754 retproc
= ELF_FindExportedFunction( wm
, function
);
1755 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1758 ERR("wine_modref type %d not handled.\n",wm
->type
);
1759 SetLastError(ERROR_INVALID_HANDLE
);
1765 /***********************************************************************
1766 * RtlImageNtHeaders (NTDLL)
1768 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
1771 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1772 * but we could get HMODULE16 or the like (think builtin modules)
1775 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1776 if (!wm
|| (wm
->type
!= MODULE32_PE
)) return (PIMAGE_NT_HEADERS
)0;
1777 return PE_HEADER(wm
->module
);
1781 /***************************************************************************
1782 * HasGPHandler (KERNEL.338)
1785 #include "pshpack1.h"
1786 typedef struct _GPHANDLERDEF
1793 #include "poppack.h"
1795 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1800 GPHANDLERDEF
*gpHandler
;
1802 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1803 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1804 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1805 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1806 && (gpHandler
= PTR_SEG_TO_LIN( gpPtr
)) != NULL
)
1808 while (gpHandler
->selector
)
1810 if ( SELECTOROF(address
) == gpHandler
->selector
1811 && OFFSETOF(address
) >= gpHandler
->rangeStart
1812 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1813 return PTR_SEG_OFF_TO_SEGPTR( gpHandler
->selector
,
1814 gpHandler
->handler
);