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 );
330 /*************************************************************************
331 * MODULE_SendLoadDLLEvents
333 * Sends DEBUG_DLL_LOAD events for all outstanding modules.
335 * NOTE: Assumes that the process critical section is held!
338 void MODULE_SendLoadDLLEvents( void )
342 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
344 if ( wm->type != MODULE32_PE ) continue;
345 if ( wm == PROCESS_Current()->exe_modref ) continue;
346 if ( wm->flags & WINE_MODREF_DEBUG_EVENT_SENT ) continue;
348 DEBUG_SendLoadDLLEvent( -1 /*FIXME*/, wm->module, &wm->modname );
349 wm->flags |= WINE_MODREF_DEBUG_EVENT_SENT;
354 /***********************************************************************
355 * MODULE_CreateDummyModule
357 * Create a dummy NE module for Win32 or Winelib.
359 HMODULE MODULE_CreateDummyModule( LPCSTR filename, WORD version )
363 SEGTABLEENTRY *pSegment;
366 const char* basename;
370 /* Extract base filename */
371 basename = strrchr(filename, '\\');
372 if (!basename) basename = filename;
374 len = strlen(basename);
375 if ((s = strchr(basename, '.'))) len = s - basename;
377 /* Allocate module */
378 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
379 + strlen(filename) + 1;
380 size = sizeof(NE_MODULE) +
381 /* loaded file info */
383 /* segment table: DS,CS */
384 2 * sizeof(SEGTABLEENTRY) +
387 /* several empty tables */
390 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
391 if (!hModule) return (HMODULE)11; /* invalid exe */
393 FarSetOwner16( hModule, hModule );
394 pModule = (NE_MODULE *)GlobalLock16( hModule );
396 /* Set all used entries */
397 pModule->magic = IMAGE_OS2_SIGNATURE;
404 pModule->heap_size = 0;
405 pModule->stack_size = 0;
406 pModule->seg_count = 2;
407 pModule->modref_count = 0;
408 pModule->nrname_size = 0;
409 pModule->fileinfo = sizeof(NE_MODULE);
410 pModule->os_flags = NE_OSFLAGS_WINDOWS;
411 pModule->expected_version = version;
412 pModule->self = hModule;
414 /* Set loaded file information */
415 ofs = (OFSTRUCT *)(pModule + 1);
416 memset( ofs, 0, of_size );
417 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
418 strcpy( ofs->szPathName, filename );
420 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
421 pModule->seg_table = (int)pSegment - (int)pModule;
424 pSegment->flags = NE_SEGFLAGS_DATA;
425 pSegment->minsize = 0x1000;
432 pStr = (char *)pSegment;
433 pModule->name_table = (int)pStr - (int)pModule;
435 strncpy( pStr+1, basename, len );
439 /* All tables zero terminated */
440 pModule->res_table = pModule->import_table = pModule->entry_table =
441 (int)pStr - (int)pModule;
443 NE_RegisterModule( pModule );
448 /**********************************************************************
449 * MODULE_FindModule32
451 * Find a (loaded) win32 module depending on path
454 * the module handle if found
457 WINE_MODREF *MODULE_FindModule(
458 LPCSTR path /* [in] pathname of module/library to be found */
461 char dllname[260], *p;
463 /* Append .DLL to name if no extension present */
464 strcpy( dllname, path );
465 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
466 strcat( dllname, ".DLL" );
468 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
470 if ( !strcasecmp( dllname, wm->modname ) )
472 if ( !strcasecmp( dllname, wm->filename ) )
474 if ( !strcasecmp( dllname, wm->short_modname ) )
476 if ( !strcasecmp( dllname, wm->short_filename ) )
483 /***********************************************************************
484 * MODULE_GetBinaryType
486 * The GetBinaryType function determines whether a file is executable
487 * or not and if it is it returns what type of executable it is.
488 * The type of executable is a property that determines in which
489 * subsystem an executable file runs under.
491 * Binary types returned:
492 * SCS_32BIT_BINARY: A Win32 based application
493 * SCS_DOS_BINARY: An MS-Dos based application
494 * SCS_WOW_BINARY: A Win16 based application
495 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
496 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
497 * SCS_OS216_BINARY: A 16bit OS/2 based application
499 * Returns TRUE if the file is an executable in which case
500 * the value pointed by lpBinaryType is set.
501 * Returns FALSE if the file is not an executable or if the function fails.
503 * To do so it opens the file and reads in the header information
504 * if the extended header information is not presend it will
505 * assume that that the file is a DOS executable.
506 * If the extended header information is present it will
507 * determine if the file is an 16 or 32 bit Windows executable
508 * by check the flags in the header.
510 * Note that .COM and .PIF files are only recognized by their
511 * file name extension; but Windows does it the same way ...
513 static BOOL MODULE_GetBinaryType( HANDLE hfile, LPCSTR filename,
514 LPDWORD lpBinaryType )
516 IMAGE_DOS_HEADER mz_header;
520 /* Seek to the start of the file and read the DOS header information.
522 if ( SetFilePointer( hfile, 0, NULL, SEEK_SET ) != -1
523 && ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL )
524 && len == sizeof(mz_header) )
526 /* Now that we have the header check the e_magic field
527 * to see if this is a dos image.
529 if ( mz_header.e_magic == IMAGE_DOS_SIGNATURE )
531 BOOL lfanewValid = FALSE;
532 /* We do have a DOS image so we will now try to seek into
533 * the file by the amount indicated by the field
534 * "Offset to extended header" and read in the
535 * "magic" field information at that location.
536 * This will tell us if there is more header information
539 /* But before we do we will make sure that header
540 * structure encompasses the "Offset to extended header"
543 if ( (mz_header.e_cparhdr<<4) >= sizeof(IMAGE_DOS_HEADER) )
544 if ( ( mz_header.e_crlc == 0 ) ||
545 ( mz_header.e_lfarlc >= sizeof(IMAGE_DOS_HEADER) ) )
546 if ( mz_header.e_lfanew >= sizeof(IMAGE_DOS_HEADER)
547 && SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET ) != -1
548 && ReadFile( hfile, magic, sizeof(magic), &len, NULL )
549 && len == sizeof(magic) )
554 /* If we cannot read this "extended header" we will
555 * assume that we have a simple DOS executable.
557 *lpBinaryType = SCS_DOS_BINARY;
562 /* Reading the magic field succeeded so
563 * we will try to determine what type it is.
565 if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
567 /* This is an NT signature.
569 *lpBinaryType = SCS_32BIT_BINARY;
572 else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
574 /* The IMAGE_OS2_SIGNATURE indicates that the
575 * "extended header is a Windows executable (NE)
576 * header." This can mean either a 16-bit OS/2
577 * or a 16-bit Windows or even a DOS program
578 * (running under a DOS extender). To decide
579 * which, we'll have to read the NE header.
583 if ( SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET ) != -1
584 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
585 && len == sizeof(ne) )
587 switch ( ne.operating_system )
589 case 2: *lpBinaryType = SCS_WOW_BINARY; return TRUE;
590 case 5: *lpBinaryType = SCS_DOS_BINARY; return TRUE;
591 default: *lpBinaryType = SCS_OS216_BINARY; return TRUE;
594 /* Couldn't read header, so abort. */
599 /* Unknown extended header, but this file is nonetheless
602 *lpBinaryType = SCS_DOS_BINARY;
609 /* If we get here, we don't even have a correct MZ header.
610 * Try to check the file extension for known types ...
612 ptr = strrchr( filename, '.' );
613 if ( ptr && !strchr( ptr, '\\' ) && !strchr( ptr, '/' ) )
615 if ( !lstrcmpiA( ptr, ".COM" ) )
617 *lpBinaryType = SCS_DOS_BINARY;
621 if ( !lstrcmpiA( ptr, ".PIF" ) )
623 *lpBinaryType = SCS_PIF_BINARY;
631 /***********************************************************************
632 * GetBinaryTypeA [KERNEL32.280]
634 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
639 TRACE_(win32)("%s\n", lpApplicationName );
643 if ( lpApplicationName == NULL || lpBinaryType == NULL )
646 /* Open the file indicated by lpApplicationName for reading.
648 hfile = CreateFileA( lpApplicationName, GENERIC_READ, 0,
649 NULL, OPEN_EXISTING, 0, -1 );
650 if ( hfile == INVALID_HANDLE_VALUE )
655 ret = MODULE_GetBinaryType( hfile, lpApplicationName, lpBinaryType );
659 CloseHandle( hfile );
664 /***********************************************************************
665 * GetBinaryTypeW [KERNEL32.281]
667 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
672 TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) );
676 if ( lpApplicationName == NULL || lpBinaryType == NULL )
679 /* Convert the wide string to a ascii string.
681 strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
683 if ( strNew != NULL )
685 ret = GetBinaryTypeA( strNew, lpBinaryType );
687 /* Free the allocated string.
689 HeapFree( GetProcessHeap(), 0, strNew );
695 /**********************************************************************
696 * MODULE_CreateUnixProcess
698 static BOOL MODULE_CreateUnixProcess( LPCSTR filename, LPCSTR lpCmdLine,
699 LPSTARTUPINFOA lpStartupInfo,
700 LPPROCESS_INFORMATION lpProcessInfo,
703 DOS_FULL_NAME full_name;
704 const char *unixfilename = filename;
705 const char *argv[256], **argptr;
706 char *cmdline = NULL;
709 /* Get Unix file name and iconic flag */
711 if ( lpStartupInfo->dwFlags & STARTF_USESHOWWINDOW )
712 if ( lpStartupInfo->wShowWindow == SW_SHOWMINIMIZED
713 || lpStartupInfo->wShowWindow == SW_SHOWMINNOACTIVE )
716 /* Build argument list */
722 p = cmdline = strdup(lpCmdLine);
723 if (strchr(filename, '/') || strchr(filename, ':') || strchr(filename, '\\'))
725 if ( DOSFS_GetFullName( filename, TRUE, &full_name ) )
726 unixfilename = full_name.long_name;
728 *argptr++ = unixfilename;
729 if (iconic) *argptr++ = "-iconic";
732 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
735 while (*p && *p != ' ' && *p != '\t') p++;
741 if (iconic) *argptr++ = "-iconic";
742 *argptr++ = lpCmdLine;
746 /* Fork and execute */
750 /* Note: don't use Wine routines here, as this process
751 has not been correctly initialized! */
753 execvp( argv[0], (char**)argv );
757 fprintf( stderr, "CreateProcess: can't exec 'wine %s'\n",
762 /* Fake success return value */
764 memset( lpProcessInfo, '\0', sizeof( *lpProcessInfo ) );
765 lpProcessInfo->hProcess = INVALID_HANDLE_VALUE;
766 lpProcessInfo->hThread = INVALID_HANDLE_VALUE;
767 if (cmdline) free(cmdline);
769 SetLastError( ERROR_SUCCESS );
773 /***********************************************************************
774 * WinExec16 (KERNEL.166)
776 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
780 SYSLEVEL_ReleaseWin16Lock();
781 hInst = WinExec( lpCmdLine, nCmdShow );
782 SYSLEVEL_RestoreWin16Lock();
787 /***********************************************************************
788 * WinExec (KERNEL32.566)
790 HINSTANCE WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
793 UINT16 paramCmdShow[2];
796 return 2; /* File not found */
798 /* Set up LOADPARAMS buffer for LoadModule */
800 memset( ¶ms, '\0', sizeof(params) );
801 params.lpCmdLine = (LPSTR)lpCmdLine;
802 params.lpCmdShow = paramCmdShow;
803 params.lpCmdShow[0] = 2;
804 params.lpCmdShow[1] = nCmdShow;
806 /* Now load the executable file */
808 return LoadModule( NULL, ¶ms );
811 /**********************************************************************
812 * LoadModule (KERNEL32.499)
814 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
816 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
817 PROCESS_INFORMATION info;
818 STARTUPINFOA startup;
823 memset( &startup, '\0', sizeof(startup) );
824 startup.cb = sizeof(startup);
825 startup.dwFlags = STARTF_USESHOWWINDOW;
826 startup.wShowWindow = params->lpCmdShow? params->lpCmdShow[1] : 0;
828 if ( !CreateProcessA( name, params->lpCmdLine,
829 NULL, NULL, FALSE, 0, params->lpEnvAddress,
830 NULL, &startup, &info ) )
832 hInstance = GetLastError();
833 if ( hInstance < 32 ) return hInstance;
835 FIXME_(module)("Strange error set by CreateProcess: %d\n", hInstance );
839 /* Get 16-bit hInstance/hTask from process */
840 pdb = PROCESS_IdToPDB( info.dwProcessId );
841 tdb = pdb? (TDB *)GlobalLock16( pdb->task ) : NULL;
842 hInstance = tdb && tdb->hInstance? tdb->hInstance : pdb? pdb->task : 0;
843 /* If there is no hInstance (32-bit process) return a dummy value
845 * FIXME: should do this in all cases and fix Win16 callers */
846 if (!hInstance) hInstance = 33;
848 /* Close off the handles */
849 CloseHandle( info.hThread );
850 CloseHandle( info.hProcess );
855 /*************************************************************************
858 * Get next blank delimited token from input string. If quoted then
859 * process till matching quote and then till blank.
861 * Returns number of characters in token (not including \0). On
862 * end of string (EOS), returns a 0.
864 * from (IO) address of start of input string to scan, updated to
865 * next non-processed character.
866 * to (IO) address of start of output string (previous token \0
867 * char), updated to end of new output string (the \0
870 static int get_makename_token(LPCSTR *from, LPSTR *to )
873 LPCSTR to_old = *to; /* only used for tracing */
875 while ( **from == ' ') {
876 /* Copy leading blanks (separators between previous */
877 /* token and this token). */
884 while ( (**from != 0) && (**from != ' ') && (**from != '"') ) {
885 **to = **from; (*from)++; (*to)++; len++;
887 if ( **from == '"' ) {
888 /* Handle quoted string. */
890 if ( !strchr(*from, '"') ) {
891 /* fail - no closing quote. Return entire string */
892 while ( **from != 0 ) {
893 **to = **from; (*from)++; (*to)++; len++;
897 while( **from != '"') {
907 /* either EOS or ' ' */
912 **to = 0; /* terminate output string */
914 TRACE_(module)("returning token len=%d, string=%s\n",
920 /*************************************************************************
921 * make_lpCommandLine_name
923 * Try longer and longer strings from "line" to find an existing
924 * file name. Each attempt is delimited by a blank outside of quotes.
925 * Also will attempt to append ".exe" if requested and not already
926 * present. Returns the address of the remaining portion of the
931 static BOOL make_lpCommandLine_name( LPCSTR line, LPSTR name, int namelen,
943 /* scan over initial blanks if any */
944 while ( *from == ' ') from++;
946 /* get a token and append to previous data the check for existance */
948 if ( !get_makename_token( &from, &to ) ) {
949 /* EOS has occured and not found - exit */
954 TRACE_(module)("checking if file exists '%s'\n", name);
955 retlen = SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, &lastpart);
956 if ( retlen && (retlen < sizeof(buffer)) ) break;
959 /* if we have a non-null full path name in buffer then move to output */
961 if ( strlen(buffer) <= namelen ) {
962 strcpy( name, buffer );
964 /* not enough space to return full path string */
965 FIXME_(module)("internal string not long enough, need %d\n",
970 /* all done, indicate end of module name and then trace and exit */
971 if (after) *after = from;
972 TRACE_(module)("%i, selected file name '%s'\n and cmdline as %s\n",
973 found, name, debugstr_a(from));
977 /*************************************************************************
978 * make_lpApplicationName_name
980 * Scan input string (the lpApplicationName) and remove any quotes
981 * if they are balanced.
985 static BOOL make_lpApplicationName_name( LPCSTR line, LPSTR name, int namelen)
988 LPSTR to, to_end, to_old;
992 to_end = to + sizeof(buffer) - 1;
995 while ( *line == ' ' ) line++; /* point to beginning of string */
998 /* Copy all input till end, or quote */
999 while((*from != 0) && (*from != '"') && (to < to_end))
1001 if (to >= to_end) { *to = 0; break; }
1005 /* Handle quoted string. If there is a closing quote, copy all */
1006 /* that is inside. */
1008 if (!strchr(from, '"'))
1010 /* fail - no closing quote */
1011 to = to_old; /* restore to previous attempt */
1012 *to = 0; /* end string */
1013 break; /* exit with previous attempt */
1015 while((*from != '"') && (to < to_end)) *to++ = *from++;
1016 if (to >= to_end) { *to = 0; break; }
1018 continue; /* past quoted string, so restart from top */
1021 *to = 0; /* terminate output string */
1022 to_old = to; /* save for possible use in unmatched quote case */
1024 /* loop around keeping the blank as part of file name */
1026 break; /* exit if out of input string */
1029 if (!SearchPathA( NULL, buffer, ".exe", namelen, name, NULL )) {
1030 TRACE_(module)("file not found '%s'\n", buffer );
1034 TRACE_(module)("selected as file name '%s'\n", name );
1038 /**********************************************************************
1039 * CreateProcessA (KERNEL32.171)
1041 BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
1042 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1043 LPSECURITY_ATTRIBUTES lpThreadAttributes,
1044 BOOL bInheritHandles, DWORD dwCreationFlags,
1045 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
1046 LPSTARTUPINFOA lpStartupInfo,
1047 LPPROCESS_INFORMATION lpProcessInfo )
1050 BOOL found_file = FALSE;
1053 char name[256], dummy[256];
1054 LPCSTR cmdline = NULL;
1058 /* Get name and command line */
1060 if (!lpApplicationName && !lpCommandLine)
1062 SetLastError( ERROR_FILE_NOT_FOUND );
1066 /* Process the AppName and/or CmdLine to get module name and path */
1070 if (lpApplicationName) {
1071 found_file = make_lpApplicationName_name( lpApplicationName, name, sizeof(name) );
1072 if (lpCommandLine) {
1073 make_lpCommandLine_name( lpCommandLine, dummy, sizeof ( dummy ), &cmdline );
1076 cmdline = lpApplicationName;
1078 len += strlen(lpApplicationName);
1081 found_file = make_lpCommandLine_name( lpCommandLine, name, sizeof ( name ), &cmdline );
1082 if (lpCommandLine) len = strlen(lpCommandLine);
1085 if ( !found_file ) {
1086 /* make an early exit if file not found - save second pass */
1087 SetLastError( ERROR_FILE_NOT_FOUND );
1091 len += strlen(name) + 2;
1092 tidy_cmdline = HeapAlloc( GetProcessHeap(), 0, len );
1093 sprintf( tidy_cmdline, "\"%s\"%s", name, cmdline);
1095 /* Warn if unsupported features are used */
1097 if (dwCreationFlags & DETACHED_PROCESS)
1098 FIXME_(module)("(%s,...): DETACHED_PROCESS ignored\n", name);
1099 if (dwCreationFlags & CREATE_NEW_CONSOLE)
1100 FIXME_(module)("(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
1101 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
1102 FIXME_(module)("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
1103 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
1104 FIXME_(module)("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
1105 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
1106 FIXME_(module)("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
1107 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
1108 FIXME_(module)("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
1109 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
1110 FIXME_(module)("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
1111 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
1112 FIXME_(module)("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
1113 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
1114 FIXME_(module)("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
1115 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
1116 FIXME_(module)("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
1117 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
1118 FIXME_(module)("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
1119 if (dwCreationFlags & CREATE_NO_WINDOW)
1120 FIXME_(module)("(%s,...): CREATE_NO_WINDOW ignored\n", name);
1121 if (dwCreationFlags & PROFILE_USER)
1122 FIXME_(module)("(%s,...): PROFILE_USER ignored\n", name);
1123 if (dwCreationFlags & PROFILE_KERNEL)
1124 FIXME_(module)("(%s,...): PROFILE_KERNEL ignored\n", name);
1125 if (dwCreationFlags & PROFILE_SERVER)
1126 FIXME_(module)("(%s,...): PROFILE_SERVER ignored\n", name);
1127 if (lpCurrentDirectory)
1128 FIXME_(module)("(%s,...): lpCurrentDirectory %s ignored\n",
1129 name, lpCurrentDirectory);
1130 if (lpStartupInfo->lpDesktop)
1131 FIXME_(module)("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
1132 name, lpStartupInfo->lpDesktop);
1133 if (lpStartupInfo->lpTitle)
1134 FIXME_(module)("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
1135 name, lpStartupInfo->lpTitle);
1136 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
1137 FIXME_(module)("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
1138 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
1139 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
1140 FIXME_(module)("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
1141 name, lpStartupInfo->dwFillAttribute);
1142 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
1143 FIXME_(module)("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
1144 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
1145 FIXME_(module)("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
1146 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
1147 FIXME_(module)("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
1148 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
1149 FIXME_(module)("(%s,...): STARTF_USEHOTKEY ignored\n", name);
1152 /* Load file and create process */
1156 /* Open file and determine executable type */
1158 hFile = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
1159 NULL, OPEN_EXISTING, 0, -1 );
1160 if ( hFile == INVALID_HANDLE_VALUE )
1162 SetLastError( ERROR_FILE_NOT_FOUND );
1163 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1167 if ( !MODULE_GetBinaryType( hFile, name, &type ) )
1169 CloseHandle( hFile );
1171 /* FIXME: Try Unix executable only when appropriate! */
1172 if ( MODULE_CreateUnixProcess( name, tidy_cmdline,
1173 lpStartupInfo, lpProcessInfo, FALSE ) )
1175 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1178 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1179 SetLastError( ERROR_BAD_FORMAT );
1184 /* Create process */
1188 case SCS_32BIT_BINARY:
1189 retv = PE_CreateProcess( hFile, name, tidy_cmdline, lpEnvironment,
1190 lpProcessAttributes, lpThreadAttributes,
1191 bInheritHandles, dwCreationFlags,
1192 lpStartupInfo, lpProcessInfo );
1195 case SCS_DOS_BINARY:
1196 retv = MZ_CreateProcess( hFile, name, tidy_cmdline, lpEnvironment,
1197 lpProcessAttributes, lpThreadAttributes,
1198 bInheritHandles, dwCreationFlags,
1199 lpStartupInfo, lpProcessInfo );
1202 case SCS_WOW_BINARY:
1203 retv = NE_CreateProcess( hFile, name, tidy_cmdline, lpEnvironment,
1204 lpProcessAttributes, lpThreadAttributes,
1205 bInheritHandles, dwCreationFlags,
1206 lpStartupInfo, lpProcessInfo );
1209 case SCS_PIF_BINARY:
1210 case SCS_POSIX_BINARY:
1211 case SCS_OS216_BINARY:
1212 FIXME_(module)("Unsupported executable type: %ld\n", type );
1216 SetLastError( ERROR_BAD_FORMAT );
1221 CloseHandle( hFile );
1223 HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1227 /**********************************************************************
1228 * CreateProcessW (KERNEL32.172)
1230 * lpReserved is not converted
1232 BOOL WINAPI CreateProcessW( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
1233 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1234 LPSECURITY_ATTRIBUTES lpThreadAttributes,
1235 BOOL bInheritHandles, DWORD dwCreationFlags,
1236 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
1237 LPSTARTUPINFOW lpStartupInfo,
1238 LPPROCESS_INFORMATION lpProcessInfo )
1240 STARTUPINFOA StartupInfoA;
1242 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
1243 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
1244 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
1246 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFOA));
1247 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
1248 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
1250 TRACE_(win32)("(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
1252 if (lpStartupInfo->lpReserved)
1253 FIXME_(win32)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
1255 ret = CreateProcessA( lpApplicationNameA, lpCommandLineA,
1256 lpProcessAttributes, lpThreadAttributes,
1257 bInheritHandles, dwCreationFlags,
1258 lpEnvironment, lpCurrentDirectoryA,
1259 &StartupInfoA, lpProcessInfo );
1261 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
1262 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
1263 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
1264 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
1269 /***********************************************************************
1270 * GetModuleHandle (KERNEL32.237)
1272 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
1276 if ( module == NULL )
1277 wm = PROCESS_Current()->exe_modref;
1279 wm = MODULE_FindModule( module );
1281 return wm? wm->module : 0;
1284 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
1287 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
1288 hModule = GetModuleHandleA( modulea );
1289 HeapFree( GetProcessHeap(), 0, modulea );
1294 /***********************************************************************
1295 * GetModuleFileName32A (KERNEL32.235)
1297 DWORD WINAPI GetModuleFileNameA(
1298 HMODULE hModule, /* [in] module handle (32bit) */
1299 LPSTR lpFileName, /* [out] filenamebuffer */
1300 DWORD size /* [in] size of filenamebuffer */
1302 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1304 if (!wm) /* can happen on start up or the like */
1307 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
1308 lstrcpynA( lpFileName, wm->filename, size );
1310 lstrcpynA( lpFileName, wm->short_filename, size );
1312 TRACE_(module)("%s\n", lpFileName );
1313 return strlen(lpFileName);
1317 /***********************************************************************
1318 * GetModuleFileName32W (KERNEL32.236)
1320 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName,
1323 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
1324 DWORD res = GetModuleFileNameA( hModule, fnA, size );
1325 lstrcpynAtoW( lpFileName, fnA, size );
1326 HeapFree( GetProcessHeap(), 0, fnA );
1331 /***********************************************************************
1332 * LoadLibraryExA (KERNEL32)
1334 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
1340 SetLastError(ERROR_INVALID_PARAMETER);
1344 EnterCriticalSection(&PROCESS_Current()->crit_section);
1346 wm = MODULE_LoadLibraryExA( libname, hfile, flags );
1349 if ( PROCESS_Current()->flags & PDB32_DEBUGGED )
1350 MODULE_SendLoadDLLEvents();
1352 if ( !MODULE_DllProcessAttach( wm, NULL ) )
1354 WARN_(module)("Attach failed for module '%s', \n", libname);
1355 MODULE_FreeLibrary(wm);
1356 SetLastError(ERROR_DLL_INIT_FAILED);
1361 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1363 return wm ? wm->module : 0;
1366 /***********************************************************************
1367 * MODULE_LoadLibraryExA (internal)
1369 * Load a PE style module according to the load order.
1371 * The HFILE parameter is not used and marked reserved in the SDK. I can
1372 * only guess that it should force a file to be mapped, but I rather
1373 * ignore the parameter because it would be extremely difficult to
1374 * integrate this with different types of module represenations.
1377 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
1382 module_loadorder_t *plo;
1384 EnterCriticalSection(&PROCESS_Current()->crit_section);
1386 /* Check for already loaded module */
1387 if((pwm = MODULE_FindModule(libname)))
1389 if(!(pwm->flags & WINE_MODREF_MARKER))
1391 TRACE_(module)("Already loaded module '%s' at 0x%08x, count=%d, \n", libname, pwm->module, pwm->refCount);
1392 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1396 plo = MODULE_GetLoadOrder(libname);
1398 for(i = 0; i < MODULE_LOADORDER_NTYPES; i++)
1400 switch(plo->loadorder[i])
1402 case MODULE_LOADORDER_DLL:
1403 TRACE_(module)("Trying native dll '%s'\n", libname);
1404 pwm = PE_LoadLibraryExA(libname, flags, &err);
1407 case MODULE_LOADORDER_ELFDLL:
1408 TRACE_(module)("Trying elfdll '%s'\n", libname);
1409 pwm = ELFDLL_LoadLibraryExA(libname, flags, &err);
1412 case MODULE_LOADORDER_SO:
1413 TRACE_(module)("Trying so-library '%s'\n", libname);
1414 pwm = ELF_LoadLibraryExA(libname, flags, &err);
1417 case MODULE_LOADORDER_BI:
1418 TRACE_(module)("Trying built-in '%s'\n", libname);
1419 pwm = BUILTIN32_LoadLibraryExA(libname, flags, &err);
1423 ERR_(module)("Got invalid loadorder type %d (%s index %d)\n", plo->loadorder[i], plo->modulename, i);
1426 case MODULE_LOADORDER_INVALID: /* We ignore this as it is an empty entry */
1433 /* Initialize DLL just loaded */
1434 TRACE_(module)("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
1436 /* Set the refCount here so that an attach failure will */
1437 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1440 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1445 if(err != ERROR_FILE_NOT_FOUND)
1449 WARN_(module)("Failed to load module '%s'; error=0x%08lx, \n", libname, err);
1451 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1455 /***********************************************************************
1456 * LoadLibraryA (KERNEL32)
1458 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
1459 return LoadLibraryExA(libname,0,0);
1462 /***********************************************************************
1463 * LoadLibraryW (KERNEL32)
1465 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
1467 return LoadLibraryExW(libnameW,0,0);
1470 /***********************************************************************
1471 * LoadLibrary32_16 (KERNEL.452)
1473 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
1477 SYSLEVEL_ReleaseWin16Lock();
1478 hModule = LoadLibraryA( libname );
1479 SYSLEVEL_RestoreWin16Lock();
1484 /***********************************************************************
1485 * LoadLibraryExW (KERNEL32)
1487 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HANDLE hfile,DWORD flags)
1489 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
1490 HMODULE ret = LoadLibraryExA( libnameA , hfile, flags );
1492 HeapFree( GetProcessHeap(), 0, libnameA );
1496 /***********************************************************************
1497 * MODULE_FlushModrefs
1499 * NOTE: Assumes that the process critical section is held!
1501 * Remove all unused modrefs and call the internal unloading routines
1502 * for the library type.
1504 static void MODULE_FlushModrefs(void)
1506 WINE_MODREF *wm, *next;
1508 for(wm = PROCESS_Current()->modref_list; wm; wm = next)
1515 /* Unlink this modref from the chain */
1517 wm->next->prev = wm->prev;
1519 wm->prev->next = wm->next;
1520 if(wm == PROCESS_Current()->modref_list)
1521 PROCESS_Current()->modref_list = wm->next;
1524 * The unloaders are also responsible for freeing the modref itself
1525 * because the loaders were responsible for allocating it.
1529 case MODULE32_PE: PE_UnloadLibrary(wm); break;
1530 case MODULE32_ELF: ELF_UnloadLibrary(wm); break;
1531 case MODULE32_ELFDLL: ELFDLL_UnloadLibrary(wm); break;
1532 case MODULE32_BI: BUILTIN32_UnloadLibrary(wm); break;
1535 ERR_(module)("Invalid or unhandled MODREF type %d encountered (wm=%p)\n", wm->type, wm);
1540 /***********************************************************************
1543 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
1548 EnterCriticalSection( &PROCESS_Current()->crit_section );
1549 PROCESS_Current()->free_lib_count++;
1551 wm = MODULE32_LookupHMODULE( hLibModule );
1552 if ( !wm || !hLibModule )
1553 SetLastError( ERROR_INVALID_HANDLE );
1555 retv = MODULE_FreeLibrary( wm );
1557 PROCESS_Current()->free_lib_count--;
1558 LeaveCriticalSection( &PROCESS_Current()->crit_section );
1563 /***********************************************************************
1564 * MODULE_DecRefCount
1566 * NOTE: Assumes that the process critical section is held!
1568 static void MODULE_DecRefCount( WINE_MODREF *wm )
1572 if ( wm->flags & WINE_MODREF_MARKER )
1575 if ( wm->refCount <= 0 )
1579 TRACE_(module)("(%s) refCount: %d\n", wm->modname, wm->refCount );
1581 if ( wm->refCount == 0 )
1583 wm->flags |= WINE_MODREF_MARKER;
1585 for ( i = 0; i < wm->nDeps; i++ )
1587 MODULE_DecRefCount( wm->deps[i] );
1589 wm->flags &= ~WINE_MODREF_MARKER;
1593 /***********************************************************************
1594 * MODULE_FreeLibrary
1596 * NOTE: Assumes that the process critical section is held!
1598 BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
1600 TRACE_(module)("(%s) - START\n", wm->modname );
1602 /* Recursively decrement reference counts */
1603 MODULE_DecRefCount( wm );
1605 /* Call process detach notifications */
1606 if ( PROCESS_Current()->free_lib_count <= 1 )
1608 MODULE_DllProcessDetach( FALSE, NULL );
1609 if (PROCESS_Current()->flags & PDB32_DEBUGGED)
1610 DEBUG_SendUnloadDLLEvent( wm->module );
1613 MODULE_FlushModrefs();
1615 TRACE_(module)("(%s) - END\n", wm->modname );
1621 /***********************************************************************
1622 * FreeLibraryAndExitThread
1624 VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
1626 FreeLibrary(hLibModule);
1627 ExitThread(dwExitCode);
1630 /***********************************************************************
1631 * PrivateLoadLibrary (KERNEL32)
1633 * FIXME: rough guesswork, don't know what "Private" means
1635 HINSTANCE WINAPI PrivateLoadLibrary(LPCSTR libname)
1637 return (HINSTANCE)LoadLibrary16(libname);
1642 /***********************************************************************
1643 * PrivateFreeLibrary (KERNEL32)
1645 * FIXME: rough guesswork, don't know what "Private" means
1647 void WINAPI PrivateFreeLibrary(HINSTANCE handle)
1649 FreeLibrary16((HINSTANCE16)handle);
1653 /***********************************************************************
1654 * WIN32_GetProcAddress16 (KERNEL32.36)
1655 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1657 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1663 WARN_(module)("hModule may not be 0!\n");
1664 return (FARPROC16)0;
1666 if (HIWORD(hModule))
1668 WARN_(module)("hModule is Win32 handle (%08x)\n", hModule );
1669 return (FARPROC16)0;
1671 hModule = GetExePtr( hModule );
1673 ordinal = NE_GetOrdinal( hModule, name );
1674 TRACE_(module)("%04x '%s'\n",
1677 ordinal = LOWORD(name);
1678 TRACE_(module)("%04x %04x\n",
1681 if (!ordinal) return (FARPROC16)0;
1682 ret = NE_GetEntryPoint( hModule, ordinal );
1683 TRACE_(module)("returning %08x\n",(UINT)ret);
1687 /***********************************************************************
1688 * GetProcAddress16 (KERNEL.50)
1690 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1695 if (!hModule) hModule = GetCurrentTask();
1696 hModule = GetExePtr( hModule );
1698 if (HIWORD(name) != 0)
1700 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1701 TRACE_(module)("%04x '%s'\n",
1702 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1706 ordinal = LOWORD(name);
1707 TRACE_(module)("%04x %04x\n",
1710 if (!ordinal) return (FARPROC16)0;
1712 ret = NE_GetEntryPoint( hModule, ordinal );
1714 TRACE_(module)("returning %08x\n", (UINT)ret );
1719 /***********************************************************************
1720 * GetProcAddress32 (KERNEL32.257)
1722 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
1724 return MODULE_GetProcAddress( hModule, function, TRUE );
1727 /***********************************************************************
1728 * WIN16_GetProcAddress32 (KERNEL.453)
1730 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
1732 return MODULE_GetProcAddress( hModule, function, FALSE );
1735 /***********************************************************************
1736 * MODULE_GetProcAddress32 (internal)
1738 FARPROC MODULE_GetProcAddress(
1739 HMODULE hModule, /* [in] current module handle */
1740 LPCSTR function, /* [in] function to be looked up */
1743 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1746 if (HIWORD(function))
1747 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
1749 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
1751 SetLastError(ERROR_INVALID_HANDLE);
1757 retproc = PE_FindExportedFunction( wm, function, snoop );
1758 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1761 retproc = ELF_FindExportedFunction( wm, function);
1762 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1765 ERR_(module)("wine_modref type %d not handled.\n",wm->type);
1766 SetLastError(ERROR_INVALID_HANDLE);
1772 /***********************************************************************
1773 * RtlImageNtHeaders (NTDLL)
1775 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
1778 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1779 * but we could get HMODULE16 or the like (think builtin modules)
1782 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1783 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1784 return PE_HEADER(wm->module);
1788 /***************************************************************************
1789 * HasGPHandler (KERNEL.338)
1792 #include "pshpack1.h"
1793 typedef struct _GPHANDLERDEF
1800 #include "poppack.h"
1802 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1807 GPHANDLERDEF *gpHandler;
1809 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1810 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1811 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1812 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1813 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1815 while (gpHandler->selector)
1817 if ( SELECTOROF(address) == gpHandler->selector
1818 && OFFSETOF(address) >= gpHandler->rangeStart
1819 && OFFSETOF(address) < gpHandler->rangeEnd )
1820 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1821 gpHandler->handler );