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 DECLARE_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_(module
)("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_(module
)("(%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_(module
)("wine_modref type %d not handled.\n", wm
->type
);
128 TRACE_(module
)("(%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_(module
)("(%s,%p) - START\n",
178 wm
->modname
, lpReserved
);
180 /* Tag current MODREF to prevent recursive loop */
181 wm
->flags
|= WINE_MODREF_MARKER
;
183 /* Recursively attach all DLLs this one depends on */
184 for ( i
= 0; retv
&& i
< wm
->nDeps
; i
++ )
186 retv
= MODULE_DllProcessAttach( wm
->deps
[i
], lpReserved
);
188 /* Call DLL entry point */
191 retv
= MODULE_InitDll( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
193 wm
->flags
|= WINE_MODREF_PROCESS_ATTACHED
;
196 /* Re-insert MODREF at head of list */
197 if ( retv
&& wm
->prev
)
199 wm
->prev
->next
= wm
->next
;
200 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
203 wm
->next
= PROCESS_Current()->modref_list
;
204 PROCESS_Current()->modref_list
= wm
->next
->prev
= wm
;
207 /* Remove recursion flag */
208 wm
->flags
&= ~WINE_MODREF_MARKER
;
210 TRACE_(module
)("(%s,%p) - END\n",
211 wm
->modname
, lpReserved
);
216 /*************************************************************************
217 * MODULE_DllProcessDetach
219 * Send DLL process detach notifications. See the comment about calling
220 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
221 * is set, only DLLs with zero refcount are notified.
223 * NOTE: Assumes that the process critical section is held!
226 void MODULE_DllProcessDetach( BOOL bForceDetach
, LPVOID lpReserved
)
232 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
234 /* Check whether to detach this DLL */
235 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
237 if ( wm
->refCount
> 0 && !bForceDetach
)
240 /* Call detach notification */
241 wm
->flags
&= ~WINE_MODREF_PROCESS_ATTACHED
;
242 MODULE_InitDll( wm
, DLL_PROCESS_DETACH
, lpReserved
);
244 /* Restart at head of WINE_MODREF list, as entries might have
245 been added and/or removed while performing the call ... */
251 /*************************************************************************
252 * MODULE_DllThreadAttach
254 * Send DLL thread attach notifications. These are sent in the
255 * reverse sequence of process detach notification.
258 void MODULE_DllThreadAttach( LPVOID lpReserved
)
262 EnterCriticalSection( &PROCESS_Current()->crit_section
);
264 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
268 for ( ; wm
; wm
= wm
->prev
)
270 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
272 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
275 MODULE_InitDll( wm
, DLL_THREAD_ATTACH
, lpReserved
);
278 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
281 /*************************************************************************
282 * MODULE_DllThreadDetach
284 * Send DLL thread detach notifications. These are sent in the
285 * same sequence as process detach notification.
288 void MODULE_DllThreadDetach( LPVOID lpReserved
)
292 EnterCriticalSection( &PROCESS_Current()->crit_section
);
294 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
= wm
->next
)
296 if ( !(wm
->flags
& WINE_MODREF_PROCESS_ATTACHED
) )
298 if ( wm
->flags
& WINE_MODREF_NO_DLL_CALLS
)
301 MODULE_InitDll( wm
, DLL_THREAD_DETACH
, lpReserved
);
304 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
307 /****************************************************************************
308 * DisableThreadLibraryCalls (KERNEL32.74)
310 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
312 BOOL WINAPI
DisableThreadLibraryCalls( HMODULE hModule
)
317 EnterCriticalSection( &PROCESS_Current()->crit_section
);
319 wm
= MODULE32_LookupHMODULE( hModule
);
323 wm
->flags
|= WINE_MODREF_NO_DLL_CALLS
;
325 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
331 /***********************************************************************
332 * MODULE_CreateDummyModule
334 * Create a dummy NE module for Win32 or Winelib.
336 HMODULE
MODULE_CreateDummyModule( const OFSTRUCT
*ofs
, LPCSTR modName
, WORD version
)
340 SEGTABLEENTRY
*pSegment
;
343 const char* basename
;
345 INT of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
346 + strlen(ofs
->szPathName
) + 1;
347 INT size
= sizeof(NE_MODULE
) +
348 /* loaded file info */
350 /* segment table: DS,CS */
351 2 * sizeof(SEGTABLEENTRY
) +
354 /* several empty tables */
357 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
358 if (!hModule
) return (HMODULE
)11; /* invalid exe */
360 FarSetOwner16( hModule
, hModule
);
361 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
363 /* Set all used entries */
364 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
371 pModule
->heap_size
= 0;
372 pModule
->stack_size
= 0;
373 pModule
->seg_count
= 2;
374 pModule
->modref_count
= 0;
375 pModule
->nrname_size
= 0;
376 pModule
->fileinfo
= sizeof(NE_MODULE
);
377 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
378 pModule
->expected_version
= version
;
379 pModule
->self
= hModule
;
381 /* Set loaded file information */
382 memcpy( pModule
+ 1, ofs
, of_size
);
383 ((OFSTRUCT
*)(pModule
+1))->cBytes
= of_size
- 1;
385 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + of_size
);
386 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
389 pSegment
->flags
= NE_SEGFLAGS_DATA
;
390 pSegment
->minsize
= 0x1000;
397 pStr
= (char *)pSegment
;
398 pModule
->name_table
= (int)pStr
- (int)pModule
;
403 basename
= strrchr(ofs
->szPathName
,'\\');
404 if (!basename
) basename
= ofs
->szPathName
;
407 len
= strlen(basename
);
408 if ((s
= strchr(basename
,'.'))) len
= s
- basename
;
409 if (len
> 8) len
= 8;
411 strncpy( pStr
+1, basename
, len
);
412 if (len
< 8) pStr
[len
+1] = 0;
415 /* All tables zero terminated */
416 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
=
417 (int)pStr
- (int)pModule
;
419 NE_RegisterModule( pModule
);
424 /**********************************************************************
425 * MODULE_FindModule32
427 * Find a (loaded) win32 module depending on path
428 * The handling of '.' is a bit weird, but we need it that way,
429 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
430 * this is the only way to differentiate. (mainly hypertrm.exe)
433 * the module handle if found
436 WINE_MODREF
*MODULE_FindModule(
437 LPCSTR path
/* [in] pathname of module/library to be found */
443 if (!(filename
= strrchr( path
, '\\' )))
444 filename
= HEAP_strdupA( GetProcessHeap(), 0, path
);
446 filename
= HEAP_strdupA( GetProcessHeap(), 0, filename
+1 );
447 dotptr
=strrchr(filename
,'.');
449 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
=wm
->next
) {
450 LPSTR xmodname
,xdotptr
;
452 assert (wm
->modname
);
453 xmodname
= HEAP_strdupA( GetProcessHeap(), 0, wm
->modname
);
454 xdotptr
=strrchr(xmodname
,'.');
455 if ( (xdotptr
&& !dotptr
) ||
458 if (dotptr
) *dotptr
= '\0';
459 if (xdotptr
) *xdotptr
= '\0';
461 if (!strcasecmp( filename
, xmodname
)) {
462 HeapFree( GetProcessHeap(), 0, filename
);
463 HeapFree( GetProcessHeap(), 0, xmodname
);
466 if (dotptr
) *dotptr
='.';
467 /* FIXME: add paths, shortname */
468 HeapFree( GetProcessHeap(), 0, xmodname
);
470 /* if that fails, try looking for the filename... */
471 for ( wm
= PROCESS_Current()->modref_list
; wm
; wm
=wm
->next
) {
472 LPSTR xlname
,xdotptr
;
474 assert (wm
->longname
);
475 xlname
= strrchr(wm
->longname
,'\\');
477 xlname
= wm
->longname
;
480 xlname
= HEAP_strdupA( GetProcessHeap(), 0, xlname
);
481 xdotptr
=strrchr(xlname
,'.');
482 if ( (xdotptr
&& !dotptr
) ||
485 if (dotptr
) *dotptr
= '\0';
486 if (xdotptr
) *xdotptr
= '\0';
488 if (!strcasecmp( filename
, xlname
)) {
489 HeapFree( GetProcessHeap(), 0, filename
);
490 HeapFree( GetProcessHeap(), 0, xlname
);
493 if (dotptr
) *dotptr
='.';
494 /* FIXME: add paths, shortname */
495 HeapFree( GetProcessHeap(), 0, xlname
);
497 HeapFree( GetProcessHeap(), 0, filename
);
501 /***********************************************************************
502 * MODULE_GetBinaryType
504 * The GetBinaryType function determines whether a file is executable
505 * or not and if it is it returns what type of executable it is.
506 * The type of executable is a property that determines in which
507 * subsystem an executable file runs under.
509 * Binary types returned:
510 * SCS_32BIT_BINARY: A Win32 based application
511 * SCS_DOS_BINARY: An MS-Dos based application
512 * SCS_WOW_BINARY: A Win16 based application
513 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
514 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
515 * SCS_OS216_BINARY: A 16bit OS/2 based application
517 * Returns TRUE if the file is an executable in which case
518 * the value pointed by lpBinaryType is set.
519 * Returns FALSE if the file is not an executable or if the function fails.
521 * To do so it opens the file and reads in the header information
522 * if the extended header information is not presend it will
523 * assume that that the file is a DOS executable.
524 * If the extended header information is present it will
525 * determine if the file is an 16 or 32 bit Windows executable
526 * by check the flags in the header.
528 * Note that .COM and .PIF files are only recognized by their
529 * file name extension; but Windows does it the same way ...
531 static BOOL
MODULE_GetBinaryType( HFILE hfile
, OFSTRUCT
*ofs
,
532 LPDWORD lpBinaryType
)
534 IMAGE_DOS_HEADER mz_header
;
537 /* Seek to the start of the file and read the DOS header information.
539 if ( _llseek( hfile
, 0, SEEK_SET
) >= 0 &&
540 _lread( hfile
, &mz_header
, sizeof(mz_header
) ) == sizeof(mz_header
) )
542 /* Now that we have the header check the e_magic field
543 * to see if this is a dos image.
545 if ( mz_header
.e_magic
== IMAGE_DOS_SIGNATURE
)
547 BOOL lfanewValid
= FALSE
;
548 /* We do have a DOS image so we will now try to seek into
549 * the file by the amount indicated by the field
550 * "Offset to extended header" and read in the
551 * "magic" field information at that location.
552 * This will tell us if there is more header information
555 /* But before we do we will make sure that header
556 * structure encompasses the "Offset to extended header"
559 if ( (mz_header
.e_cparhdr
<<4) >= sizeof(IMAGE_DOS_HEADER
) )
560 if ( ( mz_header
.e_crlc
== 0 ) ||
561 ( mz_header
.e_lfarlc
>= sizeof(IMAGE_DOS_HEADER
) ) )
562 if ( mz_header
.e_lfanew
>= sizeof(IMAGE_DOS_HEADER
) &&
563 _llseek( hfile
, mz_header
.e_lfanew
, SEEK_SET
) >= 0 &&
564 _lread( hfile
, magic
, sizeof(magic
) ) == sizeof(magic
) )
569 /* If we cannot read this "extended header" we will
570 * assume that we have a simple DOS executable.
572 *lpBinaryType
= SCS_DOS_BINARY
;
577 /* Reading the magic field succeeded so
578 * we will try to determine what type it is.
580 if ( *(DWORD
*)magic
== IMAGE_NT_SIGNATURE
)
582 /* This is an NT signature.
584 *lpBinaryType
= SCS_32BIT_BINARY
;
587 else if ( *(WORD
*)magic
== IMAGE_OS2_SIGNATURE
)
589 /* The IMAGE_OS2_SIGNATURE indicates that the
590 * "extended header is a Windows executable (NE)
591 * header." This can mean either a 16-bit OS/2
592 * or a 16-bit Windows or even a DOS program
593 * (running under a DOS extender). To decide
594 * which, we'll have to read the NE header.
598 if ( _llseek( hfile
, mz_header
.e_lfanew
, SEEK_SET
) >= 0 &&
599 _lread( hfile
, &ne
, sizeof(ne
) ) == sizeof(ne
) )
601 switch ( ne
.operating_system
)
603 case 2: *lpBinaryType
= SCS_WOW_BINARY
; return TRUE
;
604 case 5: *lpBinaryType
= SCS_DOS_BINARY
; return TRUE
;
605 default: *lpBinaryType
= SCS_OS216_BINARY
; return TRUE
;
608 /* Couldn't read header, so abort. */
613 /* Unknown extended header, so abort.
621 /* If we get here, we don't even have a correct MZ header.
622 * Try to check the file extension for known types ...
624 ptr
= strrchr( ofs
->szPathName
, '.' );
625 if ( ptr
&& !strchr( ptr
, '\\' ) && !strchr( ptr
, '/' ) )
627 if ( !lstrcmpiA( ptr
, ".COM" ) )
629 *lpBinaryType
= SCS_DOS_BINARY
;
633 if ( !lstrcmpiA( ptr
, ".PIF" ) )
635 *lpBinaryType
= SCS_PIF_BINARY
;
643 /***********************************************************************
644 * GetBinaryTypeA [KERNEL32.280]
646 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
652 TRACE_(win32
)("%s\n", lpApplicationName
);
656 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
659 /* Open the file indicated by lpApplicationName for reading.
661 if ( (hfile
= OpenFile( lpApplicationName
, &ofs
, OF_READ
)) == HFILE_ERROR
)
666 ret
= MODULE_GetBinaryType( hfile
, &ofs
, lpBinaryType
);
670 CloseHandle( hfile
);
675 /***********************************************************************
676 * GetBinaryTypeW [KERNEL32.281]
678 BOOL WINAPI
GetBinaryTypeW( LPCWSTR lpApplicationName
, LPDWORD lpBinaryType
)
683 TRACE_(win32
)("%s\n", debugstr_w(lpApplicationName
) );
687 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
690 /* Convert the wide string to a ascii string.
692 strNew
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName
);
694 if ( strNew
!= NULL
)
696 ret
= GetBinaryTypeA( strNew
, lpBinaryType
);
698 /* Free the allocated string.
700 HeapFree( GetProcessHeap(), 0, strNew
);
706 /**********************************************************************
707 * MODULE_CreateUnixProcess
709 static BOOL
MODULE_CreateUnixProcess( LPCSTR filename
, LPCSTR lpCmdLine
,
710 LPSTARTUPINFOA lpStartupInfo
,
711 LPPROCESS_INFORMATION lpProcessInfo
,
714 DOS_FULL_NAME full_name
;
715 const char *unixfilename
= filename
;
716 const char *argv
[256], **argptr
;
717 char *cmdline
= NULL
;
720 /* Get Unix file name and iconic flag */
722 if ( lpStartupInfo
->dwFlags
& STARTF_USESHOWWINDOW
)
723 if ( lpStartupInfo
->wShowWindow
== SW_SHOWMINIMIZED
724 || lpStartupInfo
->wShowWindow
== SW_SHOWMINNOACTIVE
)
727 /* Build argument list */
733 p
= cmdline
= strdup(lpCmdLine
);
734 if (strchr(filename
, '/') || strchr(filename
, ':') || strchr(filename
, '\\'))
736 if ( DOSFS_GetFullName( filename
, TRUE
, &full_name
) )
737 unixfilename
= full_name
.long_name
;
739 *argptr
++ = unixfilename
;
740 if (iconic
) *argptr
++ = "-iconic";
743 while (*p
&& (*p
== ' ' || *p
== '\t')) *p
++ = '\0';
746 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
752 if (iconic
) *argptr
++ = "-iconic";
753 *argptr
++ = lpCmdLine
;
757 /* Fork and execute */
761 /* Note: don't use Wine routines here, as this process
762 has not been correctly initialized! */
764 execvp( argv
[0], (char**)argv
);
768 fprintf( stderr
, "CreateProcess: can't exec 'wine %s'\n",
773 /* Fake success return value */
775 memset( lpProcessInfo
, '\0', sizeof( *lpProcessInfo
) );
776 lpProcessInfo
->hProcess
= INVALID_HANDLE_VALUE
;
777 lpProcessInfo
->hThread
= INVALID_HANDLE_VALUE
;
778 if (cmdline
) free(cmdline
);
780 SetLastError( ERROR_SUCCESS
);
784 /***********************************************************************
785 * WinExec16 (KERNEL.166)
787 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
791 SYSLEVEL_ReleaseWin16Lock();
792 hInst
= WinExec( lpCmdLine
, nCmdShow
);
793 SYSLEVEL_RestoreWin16Lock();
798 /***********************************************************************
799 * WinExec (KERNEL32.566)
801 HINSTANCE WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
804 UINT16 paramCmdShow
[2];
807 return 2; /* File not found */
809 /* Set up LOADPARAMS buffer for LoadModule */
811 memset( ¶ms
, '\0', sizeof(params
) );
812 params
.lpCmdLine
= (LPSTR
)lpCmdLine
;
813 params
.lpCmdShow
= paramCmdShow
;
814 params
.lpCmdShow
[0] = 2;
815 params
.lpCmdShow
[1] = nCmdShow
;
817 /* Now load the executable file */
819 return LoadModule( NULL
, ¶ms
);
822 /**********************************************************************
823 * LoadModule (KERNEL32.499)
825 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
827 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
828 PROCESS_INFORMATION info
;
829 STARTUPINFOA startup
;
834 memset( &startup
, '\0', sizeof(startup
) );
835 startup
.cb
= sizeof(startup
);
836 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
837 startup
.wShowWindow
= params
->lpCmdShow
? params
->lpCmdShow
[1] : 0;
839 if ( !CreateProcessA( name
, params
->lpCmdLine
,
840 NULL
, NULL
, FALSE
, 0, params
->lpEnvAddress
,
841 NULL
, &startup
, &info
) )
843 hInstance
= GetLastError();
844 if ( hInstance
< 32 ) return hInstance
;
846 FIXME_(module
)("Strange error set by CreateProcess: %d\n", hInstance
);
850 /* Get 16-bit hInstance/hTask from process */
851 pdb
= PROCESS_IdToPDB( info
.dwProcessId
);
852 tdb
= pdb
? (TDB
*)GlobalLock16( pdb
->task
) : NULL
;
853 hInstance
= tdb
&& tdb
->hInstance
? tdb
->hInstance
: pdb
? pdb
->task
: 0;
854 /* If there is no hInstance (32-bit process) return a dummy value
856 * FIXME: should do this in all cases and fix Win16 callers */
857 if (!hInstance
) hInstance
= 33;
859 /* Close off the handles */
860 CloseHandle( info
.hThread
);
861 CloseHandle( info
.hProcess
);
866 /*************************************************************************
869 * Get next blank delimited token from input string. If quoted then
870 * process till matching quote and then till blank.
872 * Returns number of characters in token (not including \0). On
873 * end of string (EOS), returns a 0.
875 * from (IO) address of start of input string to scan, updated to
876 * next non-processed character.
877 * to (IO) address of start of output string (previous token \0
878 * char), updated to end of new output string (the \0
881 static int get_makename_token(LPCSTR
*from
, LPSTR
*to
)
884 LPCSTR to_old
= *to
; /* only used for tracing */
886 while ( **from
== ' ') {
887 /* Copy leading blanks (separators between previous */
888 /* token and this token). */
895 while ( (**from
!= 0) && (**from
!= ' ') && (**from
!= '"') ) {
896 **to
= **from
; (*from
)++; (*to
)++; len
++;
898 if ( **from
== '"' ) {
899 /* Handle quoted string. */
901 if ( !strchr(*from
, '"') ) {
902 /* fail - no closing quote. Return entire string */
903 while ( **from
!= 0 ) {
904 **to
= **from
; (*from
)++; (*to
)++; len
++;
908 while( **from
!= '"') {
918 /* either EOS or ' ' */
923 **to
= 0; /* terminate output string */
925 TRACE_(module
)("returning token len=%d, string=%s\n",
931 /*************************************************************************
932 * make_lpCommandLine_name
934 * Try longer and longer strings from "line" to find an existing
935 * file name. Each attempt is delimited by a blank outside of quotes.
936 * Also will attempt to append ".exe" if requested and not already
937 * present. Returns the address of the remaining portion of the
942 static BOOL
make_lpCommandLine_name( LPCSTR line
, LPSTR name
, int namelen
,
954 /* scan over initial blanks if any */
955 while ( *from
== ' ') from
++;
957 /* get a token and append to previous data the check for existance */
959 if ( !get_makename_token( &from
, &to
) ) {
960 /* EOS has occured and not found - exit */
965 TRACE_(module
)("checking if file exists '%s'\n", name
);
966 retlen
= SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, &lastpart
);
967 if ( retlen
&& (retlen
< sizeof(buffer
)) ) break;
970 /* if we have a non-null full path name in buffer then move to output */
972 if ( strlen(buffer
) <= namelen
) {
973 strcpy( name
, buffer
);
975 /* not enough space to return full path string */
976 FIXME_(module
)("internal string not long enough, need %d\n",
981 /* all done, indicate end of module name and then trace and exit */
982 if (after
) *after
= from
;
983 TRACE_(module
)("%i, selected file name '%s'\n and cmdline as %s\n",
984 found
, name
, debugstr_a(from
));
988 /*************************************************************************
989 * make_lpApplicationName_name
991 * Scan input string (the lpApplicationName) and remove any quotes
992 * if they are balanced.
996 static BOOL
make_lpApplicationName_name( LPCSTR line
, LPSTR name
, int namelen
)
999 LPSTR to
, to_end
, to_old
;
1000 DOS_FULL_NAME full_name
;
1003 to_end
= to
+ namelen
- 1;
1006 while ( *line
== ' ' ) line
++; /* point to beginning of string */
1009 /* Copy all input till end, or quote */
1010 while((*from
!= 0) && (*from
!= '"') && (to
< to_end
))
1012 if (to
>= to_end
) { *to
= 0; break; }
1016 /* Handle quoted string. If there is a closing quote, copy all */
1017 /* that is inside. */
1019 if (!strchr(from
, '"'))
1021 /* fail - no closing quote */
1022 to
= to_old
; /* restore to previous attempt */
1023 *to
= 0; /* end string */
1024 break; /* exit with previous attempt */
1026 while((*from
!= '"') && (to
< to_end
)) *to
++ = *from
++;
1027 if (to
>= to_end
) { *to
= 0; break; }
1029 continue; /* past quoted string, so restart from top */
1032 *to
= 0; /* terminate output string */
1033 to_old
= to
; /* save for possible use in unmatched quote case */
1035 /* loop around keeping the blank as part of file name */
1037 break; /* exit if out of input string */
1040 if (!DOSFS_GetFullName(name
, TRUE
, &full_name
)) {
1041 TRACE_(module
)("file not found '%s'\n", name
);
1045 if (strlen(full_name
.long_name
) >= namelen
) {
1046 FIXME_(module
)("name longer than buffer (len=%d), file=%s\n",
1047 namelen
, full_name
.long_name
);
1050 strcpy(name
, full_name
.long_name
);
1052 TRACE_(module
)("selected as file name '%s'\n", name
);
1056 /**********************************************************************
1057 * CreateProcessA (KERNEL32.171)
1059 BOOL WINAPI
CreateProcessA( LPCSTR lpApplicationName
, LPSTR lpCommandLine
,
1060 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1061 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1062 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1063 LPVOID lpEnvironment
, LPCSTR lpCurrentDirectory
,
1064 LPSTARTUPINFOA lpStartupInfo
,
1065 LPPROCESS_INFORMATION lpProcessInfo
)
1068 BOOL found_file
= FALSE
;
1072 char name
[256], dummy
[256];
1073 LPCSTR cmdline
= NULL
;
1077 /* Get name and command line */
1079 if (!lpApplicationName
&& !lpCommandLine
)
1081 SetLastError( ERROR_FILE_NOT_FOUND
);
1085 /* Process the AppName and/or CmdLine to get module name and path */
1089 if (lpApplicationName
) {
1090 found_file
= make_lpApplicationName_name( lpApplicationName
, name
, sizeof(name
) );
1091 if (lpCommandLine
) {
1092 make_lpCommandLine_name( lpCommandLine
, dummy
, sizeof ( dummy
), &cmdline
);
1095 cmdline
= lpApplicationName
;
1097 len
+= strlen(lpApplicationName
);
1100 found_file
= make_lpCommandLine_name( lpCommandLine
, name
, sizeof ( name
), &cmdline
);
1101 if (lpCommandLine
) len
= strlen(lpCommandLine
);
1104 if ( !found_file
) {
1105 /* make an early exit if file not found - save second pass */
1106 SetLastError( ERROR_FILE_NOT_FOUND
);
1110 len
+= strlen(name
) + 2;
1111 tidy_cmdline
= HeapAlloc( GetProcessHeap(), 0, len
);
1112 sprintf( tidy_cmdline
, "\"%s\"%s", name
, cmdline
);
1114 /* Warn if unsupported features are used */
1116 if (dwCreationFlags
& DETACHED_PROCESS
)
1117 FIXME_(module
)("(%s,...): DETACHED_PROCESS ignored\n", name
);
1118 if (dwCreationFlags
& CREATE_NEW_CONSOLE
)
1119 FIXME_(module
)("(%s,...): CREATE_NEW_CONSOLE ignored\n", name
);
1120 if (dwCreationFlags
& NORMAL_PRIORITY_CLASS
)
1121 FIXME_(module
)("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1122 if (dwCreationFlags
& IDLE_PRIORITY_CLASS
)
1123 FIXME_(module
)("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1124 if (dwCreationFlags
& HIGH_PRIORITY_CLASS
)
1125 FIXME_(module
)("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1126 if (dwCreationFlags
& REALTIME_PRIORITY_CLASS
)
1127 FIXME_(module
)("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1128 if (dwCreationFlags
& CREATE_NEW_PROCESS_GROUP
)
1129 FIXME_(module
)("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1130 if (dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
)
1131 FIXME_(module
)("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1132 if (dwCreationFlags
& CREATE_SEPARATE_WOW_VDM
)
1133 FIXME_(module
)("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1134 if (dwCreationFlags
& CREATE_SHARED_WOW_VDM
)
1135 FIXME_(module
)("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1136 if (dwCreationFlags
& CREATE_DEFAULT_ERROR_MODE
)
1137 FIXME_(module
)("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1138 if (dwCreationFlags
& CREATE_NO_WINDOW
)
1139 FIXME_(module
)("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1140 if (dwCreationFlags
& PROFILE_USER
)
1141 FIXME_(module
)("(%s,...): PROFILE_USER ignored\n", name
);
1142 if (dwCreationFlags
& PROFILE_KERNEL
)
1143 FIXME_(module
)("(%s,...): PROFILE_KERNEL ignored\n", name
);
1144 if (dwCreationFlags
& PROFILE_SERVER
)
1145 FIXME_(module
)("(%s,...): PROFILE_SERVER ignored\n", name
);
1146 if (lpCurrentDirectory
)
1147 FIXME_(module
)("(%s,...): lpCurrentDirectory %s ignored\n",
1148 name
, lpCurrentDirectory
);
1149 if (lpStartupInfo
->lpDesktop
)
1150 FIXME_(module
)("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
1151 name
, lpStartupInfo
->lpDesktop
);
1152 if (lpStartupInfo
->lpTitle
)
1153 FIXME_(module
)("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
1154 name
, lpStartupInfo
->lpTitle
);
1155 if (lpStartupInfo
->dwFlags
& STARTF_USECOUNTCHARS
)
1156 FIXME_(module
)("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
1157 name
, lpStartupInfo
->dwXCountChars
, lpStartupInfo
->dwYCountChars
);
1158 if (lpStartupInfo
->dwFlags
& STARTF_USEFILLATTRIBUTE
)
1159 FIXME_(module
)("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
1160 name
, lpStartupInfo
->dwFillAttribute
);
1161 if (lpStartupInfo
->dwFlags
& STARTF_RUNFULLSCREEN
)
1162 FIXME_(module
)("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1163 if (lpStartupInfo
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1164 FIXME_(module
)("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1165 if (lpStartupInfo
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1166 FIXME_(module
)("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1167 if (lpStartupInfo
->dwFlags
& STARTF_USEHOTKEY
)
1168 FIXME_(module
)("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1170 /* Check for special case: second instance of NE module */
1172 lstrcpynA( ofs
.szPathName
, name
, sizeof( ofs
.szPathName
) );
1173 retv
= NE_CreateProcess( HFILE_ERROR
, &ofs
, tidy_cmdline
, lpEnvironment
,
1174 lpProcessAttributes
, lpThreadAttributes
,
1175 bInheritHandles
, dwCreationFlags
,
1176 lpStartupInfo
, lpProcessInfo
);
1178 /* Load file and create process */
1182 /* Open file and determine executable type */
1184 if ( (hFile
= OpenFile( name
, &ofs
, OF_READ
)) == HFILE_ERROR
)
1186 SetLastError( ERROR_FILE_NOT_FOUND
);
1187 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1191 if ( !MODULE_GetBinaryType( hFile
, &ofs
, &type
) )
1193 CloseHandle( hFile
);
1195 /* FIXME: Try Unix executable only when appropriate! */
1196 if ( MODULE_CreateUnixProcess( name
, tidy_cmdline
,
1197 lpStartupInfo
, lpProcessInfo
, FALSE
) )
1199 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1202 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1203 SetLastError( ERROR_BAD_FORMAT
);
1208 /* Create process */
1212 case SCS_32BIT_BINARY
:
1213 retv
= PE_CreateProcess( hFile
, &ofs
, tidy_cmdline
, lpEnvironment
,
1214 lpProcessAttributes
, lpThreadAttributes
,
1215 bInheritHandles
, dwCreationFlags
,
1216 lpStartupInfo
, lpProcessInfo
);
1219 case SCS_DOS_BINARY
:
1220 retv
= MZ_CreateProcess( hFile
, &ofs
, tidy_cmdline
, lpEnvironment
,
1221 lpProcessAttributes
, lpThreadAttributes
,
1222 bInheritHandles
, dwCreationFlags
,
1223 lpStartupInfo
, lpProcessInfo
);
1226 case SCS_WOW_BINARY
:
1227 retv
= NE_CreateProcess( hFile
, &ofs
, tidy_cmdline
, lpEnvironment
,
1228 lpProcessAttributes
, lpThreadAttributes
,
1229 bInheritHandles
, dwCreationFlags
,
1230 lpStartupInfo
, lpProcessInfo
);
1233 case SCS_PIF_BINARY
:
1234 case SCS_POSIX_BINARY
:
1235 case SCS_OS216_BINARY
:
1236 FIXME_(module
)("Unsupported executable type: %ld\n", type
);
1240 SetLastError( ERROR_BAD_FORMAT
);
1245 CloseHandle( hFile
);
1247 HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1251 /**********************************************************************
1252 * CreateProcessW (KERNEL32.172)
1254 * lpReserved is not converted
1256 BOOL WINAPI
CreateProcessW( LPCWSTR lpApplicationName
, LPWSTR lpCommandLine
,
1257 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
1258 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
1259 BOOL bInheritHandles
, DWORD dwCreationFlags
,
1260 LPVOID lpEnvironment
, LPCWSTR lpCurrentDirectory
,
1261 LPSTARTUPINFOW lpStartupInfo
,
1262 LPPROCESS_INFORMATION lpProcessInfo
)
1264 STARTUPINFOA StartupInfoA
;
1266 LPSTR lpApplicationNameA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName
);
1267 LPSTR lpCommandLineA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine
);
1268 LPSTR lpCurrentDirectoryA
= HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory
);
1270 memcpy (&StartupInfoA
, lpStartupInfo
, sizeof(STARTUPINFOA
));
1271 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpDesktop
);
1272 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo
->lpTitle
);
1274 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(lpApplicationName
), debugstr_w(lpCommandLine
));
1276 if (lpStartupInfo
->lpReserved
)
1277 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo
->lpReserved
));
1279 ret
= CreateProcessA( lpApplicationNameA
, lpCommandLineA
,
1280 lpProcessAttributes
, lpThreadAttributes
,
1281 bInheritHandles
, dwCreationFlags
,
1282 lpEnvironment
, lpCurrentDirectoryA
,
1283 &StartupInfoA
, lpProcessInfo
);
1285 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA
);
1286 HeapFree( GetProcessHeap(), 0, lpCommandLineA
);
1287 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1288 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1293 /***********************************************************************
1294 * GetModuleHandle (KERNEL32.237)
1296 HMODULE WINAPI
GetModuleHandleA(LPCSTR module
)
1300 if ( module
== NULL
)
1301 wm
= PROCESS_Current()->exe_modref
;
1303 wm
= MODULE_FindModule( module
);
1305 return wm
? wm
->module
: 0;
1308 HMODULE WINAPI
GetModuleHandleW(LPCWSTR module
)
1311 LPSTR modulea
= HEAP_strdupWtoA( GetProcessHeap(), 0, module
);
1312 hModule
= GetModuleHandleA( modulea
);
1313 HeapFree( GetProcessHeap(), 0, modulea
);
1318 /***********************************************************************
1319 * GetModuleFileName32A (KERNEL32.235)
1321 DWORD WINAPI
GetModuleFileNameA(
1322 HMODULE hModule
, /* [in] module handle (32bit) */
1323 LPSTR lpFileName
, /* [out] filenamebuffer */
1324 DWORD size
/* [in] size of filenamebuffer */
1326 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1328 if (!wm
) /* can happen on start up or the like */
1331 if (PE_HEADER(wm
->module
)->OptionalHeader
.MajorOperatingSystemVersion
>= 4.0)
1332 lstrcpynA( lpFileName
, wm
->longname
, size
);
1334 lstrcpynA( lpFileName
, wm
->shortname
, size
);
1336 TRACE_(module
)("%s\n", lpFileName
);
1337 return strlen(lpFileName
);
1341 /***********************************************************************
1342 * GetModuleFileName32W (KERNEL32.236)
1344 DWORD WINAPI
GetModuleFileNameW( HMODULE hModule
, LPWSTR lpFileName
,
1347 LPSTR fnA
= (char*)HeapAlloc( GetProcessHeap(), 0, size
);
1348 DWORD res
= GetModuleFileNameA( hModule
, fnA
, size
);
1349 lstrcpynAtoW( lpFileName
, fnA
, size
);
1350 HeapFree( GetProcessHeap(), 0, fnA
);
1355 /***********************************************************************
1356 * LoadLibraryExA (KERNEL32)
1358 HMODULE WINAPI
LoadLibraryExA(LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1364 SetLastError(ERROR_INVALID_PARAMETER
);
1368 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1370 wm
= MODULE_LoadLibraryExA( libname
, hfile
, flags
);
1372 if(wm
&& !MODULE_DllProcessAttach(wm
, NULL
))
1374 WARN_(module
)("Attach failed for module '%s', \n", libname
);
1375 MODULE_FreeLibrary(wm
);
1376 SetLastError(ERROR_DLL_INIT_FAILED
);
1380 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1382 return wm
? wm
->module
: 0;
1385 /***********************************************************************
1386 * MODULE_LoadLibraryExA (internal)
1388 * Load a PE style module according to the load order.
1390 * The HFILE parameter is not used and marked reserved in the SDK. I can
1391 * only guess that it should force a file to be mapped, but I rather
1392 * ignore the parameter because it would be extremely difficult to
1393 * integrate this with different types of module represenations.
1396 WINE_MODREF
*MODULE_LoadLibraryExA( LPCSTR libname
, HFILE hfile
, DWORD flags
)
1401 module_loadorder_t
*plo
;
1403 EnterCriticalSection(&PROCESS_Current()->crit_section
);
1405 /* Check for already loaded module */
1406 if((pwm
= MODULE_FindModule(libname
)))
1408 if(!(pwm
->flags
& WINE_MODREF_MARKER
))
1410 TRACE_(module
)("Already loaded module '%s' at 0x%08x, count=%d, \n", libname
, pwm
->module
, pwm
->refCount
);
1411 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1415 plo
= MODULE_GetLoadOrder(libname
);
1417 for(i
= 0; i
< MODULE_LOADORDER_NTYPES
; i
++)
1419 switch(plo
->loadorder
[i
])
1421 case MODULE_LOADORDER_DLL
:
1422 TRACE_(module
)("Trying native dll '%s'\n", libname
);
1423 pwm
= PE_LoadLibraryExA(libname
, flags
, &err
);
1426 case MODULE_LOADORDER_ELFDLL
:
1427 TRACE_(module
)("Trying elfdll '%s'\n", libname
);
1428 pwm
= ELFDLL_LoadLibraryExA(libname
, flags
, &err
);
1431 case MODULE_LOADORDER_SO
:
1432 TRACE_(module
)("Trying so-library '%s'\n", libname
);
1433 pwm
= ELF_LoadLibraryExA(libname
, flags
, &err
);
1436 case MODULE_LOADORDER_BI
:
1437 TRACE_(module
)("Trying built-in '%s'\n", libname
);
1438 pwm
= BUILTIN32_LoadLibraryExA(libname
, flags
, &err
);
1442 ERR_(module
)("Got invalid loadorder type %d (%s index %d)\n", plo
->loadorder
[i
], plo
->modulename
, i
);
1445 case MODULE_LOADORDER_INVALID
: /* We ignore this as it is an empty entry */
1452 /* Initialize DLL just loaded */
1453 TRACE_(module
)("Loaded module '%s' at 0x%08x, \n", libname
, pwm
->module
);
1455 /* Set the refCount here so that an attach failure will */
1456 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1459 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1461 if (PROCESS_Current()->flags
& PDB32_DEBUGGED
)
1462 DEBUG_SendLoadDLLEvent( -1 /*FIXME*/, pwm
->module
, &pwm
->modname
);
1467 if(err
!= ERROR_FILE_NOT_FOUND
)
1471 WARN_(module
)("Failed to load module '%s'; error=0x%08lx, \n", libname
, err
);
1473 LeaveCriticalSection(&PROCESS_Current()->crit_section
);
1477 /***********************************************************************
1478 * LoadLibraryA (KERNEL32)
1480 HMODULE WINAPI
LoadLibraryA(LPCSTR libname
) {
1481 return LoadLibraryExA(libname
,0,0);
1484 /***********************************************************************
1485 * LoadLibraryW (KERNEL32)
1487 HMODULE WINAPI
LoadLibraryW(LPCWSTR libnameW
)
1489 return LoadLibraryExW(libnameW
,0,0);
1492 /***********************************************************************
1493 * LoadLibrary32_16 (KERNEL.452)
1495 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
1499 SYSLEVEL_ReleaseWin16Lock();
1500 hModule
= LoadLibraryA( libname
);
1501 SYSLEVEL_RestoreWin16Lock();
1506 /***********************************************************************
1507 * LoadLibraryExW (KERNEL32)
1509 HMODULE WINAPI
LoadLibraryExW(LPCWSTR libnameW
,HANDLE hfile
,DWORD flags
)
1511 LPSTR libnameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW
);
1512 HMODULE ret
= LoadLibraryExA( libnameA
, hfile
, flags
);
1514 HeapFree( GetProcessHeap(), 0, libnameA
);
1518 /***********************************************************************
1519 * MODULE_FlushModrefs
1521 * NOTE: Assumes that the process critical section is held!
1523 * Remove all unused modrefs and call the internal unloading routines
1524 * for the library type.
1526 static void MODULE_FlushModrefs(void)
1528 WINE_MODREF
*wm
, *next
;
1530 for(wm
= PROCESS_Current()->modref_list
; wm
; wm
= next
)
1537 /* Unlink this modref from the chain */
1539 wm
->next
->prev
= wm
->prev
;
1541 wm
->prev
->next
= wm
->next
;
1542 if(wm
== PROCESS_Current()->modref_list
)
1543 PROCESS_Current()->modref_list
= wm
->next
;
1546 * The unloaders are also responsible for freeing the modref itself
1547 * because the loaders were responsible for allocating it.
1551 case MODULE32_PE
: PE_UnloadLibrary(wm
); break;
1552 case MODULE32_ELF
: ELF_UnloadLibrary(wm
); break;
1553 case MODULE32_ELFDLL
: ELFDLL_UnloadLibrary(wm
); break;
1554 case MODULE32_BI
: BUILTIN32_UnloadLibrary(wm
); break;
1557 ERR_(module
)("Invalid or unhandled MODREF type %d encountered (wm=%p)\n", wm
->type
, wm
);
1562 /***********************************************************************
1565 BOOL WINAPI
FreeLibrary(HINSTANCE hLibModule
)
1570 EnterCriticalSection( &PROCESS_Current()->crit_section
);
1571 PROCESS_Current()->free_lib_count
++;
1573 wm
= MODULE32_LookupHMODULE( hLibModule
);
1574 if ( !wm
|| !hLibModule
)
1575 SetLastError( ERROR_INVALID_HANDLE
);
1577 retv
= MODULE_FreeLibrary( wm
);
1579 PROCESS_Current()->free_lib_count
--;
1580 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
1585 /***********************************************************************
1586 * MODULE_DecRefCount
1588 * NOTE: Assumes that the process critical section is held!
1590 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
1594 if ( wm
->flags
& WINE_MODREF_MARKER
)
1597 if ( wm
->refCount
<= 0 )
1601 TRACE_(module
)("(%s) refCount: %d\n", wm
->modname
, wm
->refCount
);
1603 if ( wm
->refCount
== 0 )
1605 wm
->flags
|= WINE_MODREF_MARKER
;
1607 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1609 MODULE_DecRefCount( wm
->deps
[i
] );
1611 wm
->flags
&= ~WINE_MODREF_MARKER
;
1615 /***********************************************************************
1616 * MODULE_FreeLibrary
1618 * NOTE: Assumes that the process critical section is held!
1620 BOOL
MODULE_FreeLibrary( WINE_MODREF
*wm
)
1622 TRACE_(module
)("(%s) - START\n", wm
->modname
);
1624 /* Recursively decrement reference counts */
1625 MODULE_DecRefCount( wm
);
1627 /* Call process detach notifications */
1628 if ( PROCESS_Current()->free_lib_count
<= 1 )
1630 MODULE_DllProcessDetach( FALSE
, NULL
);
1631 if (PROCESS_Current()->flags
& PDB32_DEBUGGED
)
1632 DEBUG_SendUnloadDLLEvent( wm
->module
);
1635 MODULE_FlushModrefs();
1637 TRACE_(module
)("(%s) - END\n", wm
->modname
);
1643 /***********************************************************************
1644 * FreeLibraryAndExitThread
1646 VOID WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
1648 FreeLibrary(hLibModule
);
1649 ExitThread(dwExitCode
);
1652 /***********************************************************************
1653 * PrivateLoadLibrary (KERNEL32)
1655 * FIXME: rough guesswork, don't know what "Private" means
1657 HINSTANCE WINAPI
PrivateLoadLibrary(LPCSTR libname
)
1659 return (HINSTANCE
)LoadLibrary16(libname
);
1664 /***********************************************************************
1665 * PrivateFreeLibrary (KERNEL32)
1667 * FIXME: rough guesswork, don't know what "Private" means
1669 void WINAPI
PrivateFreeLibrary(HINSTANCE handle
)
1671 FreeLibrary16((HINSTANCE16
)handle
);
1675 /***********************************************************************
1676 * WIN32_GetProcAddress16 (KERNEL32.36)
1677 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1679 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1685 WARN_(module
)("hModule may not be 0!\n");
1686 return (FARPROC16
)0;
1688 if (HIWORD(hModule
))
1690 WARN_(module
)("hModule is Win32 handle (%08x)\n", hModule
);
1691 return (FARPROC16
)0;
1693 hModule
= GetExePtr( hModule
);
1695 ordinal
= NE_GetOrdinal( hModule
, name
);
1696 TRACE_(module
)("%04x '%s'\n",
1699 ordinal
= LOWORD(name
);
1700 TRACE_(module
)("%04x %04x\n",
1703 if (!ordinal
) return (FARPROC16
)0;
1704 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1705 TRACE_(module
)("returning %08x\n",(UINT
)ret
);
1709 /***********************************************************************
1710 * GetProcAddress16 (KERNEL.50)
1712 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, SEGPTR name
)
1717 if (!hModule
) hModule
= GetCurrentTask();
1718 hModule
= GetExePtr( hModule
);
1720 if (HIWORD(name
) != 0)
1722 ordinal
= NE_GetOrdinal( hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1723 TRACE_(module
)("%04x '%s'\n",
1724 hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
1728 ordinal
= LOWORD(name
);
1729 TRACE_(module
)("%04x %04x\n",
1732 if (!ordinal
) return (FARPROC16
)0;
1734 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1736 TRACE_(module
)("returning %08x\n", (UINT
)ret
);
1741 /***********************************************************************
1742 * GetProcAddress32 (KERNEL32.257)
1744 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
1746 return MODULE_GetProcAddress( hModule
, function
, TRUE
);
1749 /***********************************************************************
1750 * WIN16_GetProcAddress32 (KERNEL.453)
1752 FARPROC WINAPI
GetProcAddress32_16( HMODULE hModule
, LPCSTR function
)
1754 return MODULE_GetProcAddress( hModule
, function
, FALSE
);
1757 /***********************************************************************
1758 * MODULE_GetProcAddress32 (internal)
1760 FARPROC
MODULE_GetProcAddress(
1761 HMODULE hModule
, /* [in] current module handle */
1762 LPCSTR function
, /* [in] function to be looked up */
1765 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1768 if (HIWORD(function
))
1769 TRACE_(win32
)("(%08lx,%s)\n",(DWORD
)hModule
,function
);
1771 TRACE_(win32
)("(%08lx,%p)\n",(DWORD
)hModule
,function
);
1773 SetLastError(ERROR_INVALID_HANDLE
);
1779 retproc
= PE_FindExportedFunction( wm
, function
, snoop
);
1780 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1783 retproc
= ELF_FindExportedFunction( wm
, function
);
1784 if (!retproc
) SetLastError(ERROR_PROC_NOT_FOUND
);
1787 ERR_(module
)("wine_modref type %d not handled.\n",wm
->type
);
1788 SetLastError(ERROR_INVALID_HANDLE
);
1794 /***********************************************************************
1795 * RtlImageNtHeaders (NTDLL)
1797 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
1800 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1801 * but we could get HMODULE16 or the like (think builtin modules)
1804 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( hModule
);
1805 if (!wm
|| (wm
->type
!= MODULE32_PE
)) return (PIMAGE_NT_HEADERS
)0;
1806 return PE_HEADER(wm
->module
);
1810 /***************************************************************************
1811 * HasGPHandler (KERNEL.338)
1814 #include "pshpack1.h"
1815 typedef struct _GPHANDLERDEF
1822 #include "poppack.h"
1824 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1829 GPHANDLERDEF
*gpHandler
;
1831 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1832 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1833 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1834 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1835 && (gpHandler
= PTR_SEG_TO_LIN( gpPtr
)) != NULL
)
1837 while (gpHandler
->selector
)
1839 if ( SELECTOROF(address
) == gpHandler
->selector
1840 && OFFSETOF(address
) >= gpHandler
->rangeStart
1841 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1842 return PTR_SEG_OFF_TO_SEGPTR( gpHandler
->selector
,
1843 gpHandler
->handler
);