4 * Copyright 1995 Alexandre Julliard
12 #include <sys/types.h>
14 #include "wine/winbase16.h"
15 #include "wine/winestring.h"
21 #include "selectors.h"
22 #include "debugtools.h"
24 #include "loadorder.h"
28 DEFAULT_DEBUG_CHANNEL(module);
29 DECLARE_DEBUG_CHANNEL(win32);
32 /*************************************************************************
33 * MODULE32_LookupHMODULE
34 * looks for the referenced HMODULE in the current process
35 * NOTE: Assumes that the process critical section is held!
37 static WINE_MODREF *MODULE32_LookupHMODULE( HMODULE hmod )
42 return PROCESS_Current()->exe_modref;
45 ERR("tried to lookup 0x%04x in win32 module handler!\n",hmod);
46 SetLastError( ERROR_INVALID_HANDLE );
49 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next )
50 if (wm->module == hmod)
52 SetLastError( ERROR_INVALID_HANDLE );
56 /*************************************************************************
59 * Allocate a WINE_MODREF structure and add it to the process list
60 * NOTE: Assumes that the process critical section is held!
62 WINE_MODREF *MODULE_AllocModRef( HMODULE hModule, LPCSTR filename )
67 if ((wm = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wm) )))
72 wm->filename = HEAP_strdupA( GetProcessHeap(), 0, filename );
73 if ((wm->modname = strrchr( wm->filename, '\\' ))) wm->modname++;
74 else wm->modname = wm->filename;
76 len = GetShortPathNameA( wm->filename, NULL, 0 );
77 wm->short_filename = (char *)HeapAlloc( GetProcessHeap(), 0, len+1 );
78 GetShortPathNameA( wm->filename, wm->short_filename, len+1 );
79 if ((wm->short_modname = strrchr( wm->short_filename, '\\' ))) wm->short_modname++;
80 else wm->short_modname = wm->short_filename;
82 wm->next = PROCESS_Current()->modref_list;
83 if (wm->next) wm->next->prev = wm;
84 PROCESS_Current()->modref_list = wm;
89 /*************************************************************************
92 static BOOL MODULE_InitDLL( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
96 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
97 "THREAD_ATTACH", "THREAD_DETACH" };
100 /* Skip calls for modules loaded with special load flags */
102 if (wm->flags & WINE_MODREF_DONT_RESOLVE_REFS) return TRUE;
104 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
106 /* Call the initialization routine */
107 retv = PE_InitDLL( wm->module, type, lpReserved );
109 /* The state of the module list may have changed due to the call
110 to PE_InitDLL. We cannot assume that this module has not been
112 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
117 /*************************************************************************
118 * MODULE_DllProcessAttach
120 * Send the process attach notification to all DLLs the given module
121 * depends on (recursively). This is somewhat complicated due to the fact that
123 * - we have to respect the module dependencies, i.e. modules implicitly
124 * referenced by another module have to be initialized before the module
125 * itself can be initialized
127 * - the initialization routine of a DLL can itself call LoadLibrary,
128 * thereby introducing a whole new set of dependencies (even involving
129 * the 'old' modules) at any time during the whole process
131 * (Note that this routine can be recursively entered not only directly
132 * from itself, but also via LoadLibrary from one of the called initialization
135 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
136 * the process *detach* notifications to be sent in the correct order.
137 * This must not only take into account module dependencies, but also
138 * 'hidden' dependencies created by modules calling LoadLibrary in their
139 * attach notification routine.
141 * The strategy is rather simple: we move a WINE_MODREF to the head of the
142 * list after the attach notification has returned. This implies that the
143 * detach notifications are called in the reverse of the sequence the attach
144 * notifications *returned*.
146 * NOTE: Assumes that the process critical section is held!
149 BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
155 /* prevent infinite recursion in case of cyclical dependencies */
156 if ( ( wm->flags & WINE_MODREF_MARKER )
157 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
160 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
162 /* Tag current MODREF to prevent recursive loop */
163 wm->flags |= WINE_MODREF_MARKER;
165 /* Recursively attach all DLLs this one depends on */
166 for ( i = 0; retv && i < wm->nDeps; i++ )
168 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
170 /* Call DLL entry point */
173 retv = MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved );
175 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
178 /* Re-insert MODREF at head of list */
179 if ( retv && wm->prev )
181 wm->prev->next = wm->next;
182 if ( wm->next ) wm->next->prev = wm->prev;
185 wm->next = PROCESS_Current()->modref_list;
186 PROCESS_Current()->modref_list = wm->next->prev = wm;
189 /* Remove recursion flag */
190 wm->flags &= ~WINE_MODREF_MARKER;
192 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
197 /*************************************************************************
198 * MODULE_DllProcessDetach
200 * Send DLL process detach notifications. See the comment about calling
201 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
202 * is set, only DLLs with zero refcount are notified.
204 void MODULE_DllProcessDetach( BOOL bForceDetach, LPVOID lpReserved )
208 EnterCriticalSection( &PROCESS_Current()->crit_section );
212 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
214 /* Check whether to detach this DLL */
215 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
217 if ( wm->refCount > 0 && !bForceDetach )
220 /* Call detach notification */
221 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
222 MODULE_InitDLL( wm, DLL_PROCESS_DETACH, lpReserved );
224 /* Restart at head of WINE_MODREF list, as entries might have
225 been added and/or removed while performing the call ... */
230 LeaveCriticalSection( &PROCESS_Current()->crit_section );
233 /*************************************************************************
234 * MODULE_DllThreadAttach
236 * Send DLL thread attach notifications. These are sent in the
237 * reverse sequence of process detach notification.
240 void MODULE_DllThreadAttach( LPVOID lpReserved )
244 EnterCriticalSection( &PROCESS_Current()->crit_section );
246 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
250 for ( ; wm; wm = wm->prev )
252 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
254 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
257 MODULE_InitDLL( wm, DLL_THREAD_ATTACH, lpReserved );
260 LeaveCriticalSection( &PROCESS_Current()->crit_section );
263 /*************************************************************************
264 * MODULE_DllThreadDetach
266 * Send DLL thread detach notifications. These are sent in the
267 * same sequence as process detach notification.
270 void MODULE_DllThreadDetach( LPVOID lpReserved )
274 EnterCriticalSection( &PROCESS_Current()->crit_section );
276 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
278 if ( !(wm->flags & WINE_MODREF_PROCESS_ATTACHED) )
280 if ( wm->flags & WINE_MODREF_NO_DLL_CALLS )
283 MODULE_InitDLL( wm, DLL_THREAD_DETACH, lpReserved );
286 LeaveCriticalSection( &PROCESS_Current()->crit_section );
289 /****************************************************************************
290 * DisableThreadLibraryCalls (KERNEL32.74)
292 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
294 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
299 EnterCriticalSection( &PROCESS_Current()->crit_section );
301 wm = MODULE32_LookupHMODULE( hModule );
305 wm->flags |= WINE_MODREF_NO_DLL_CALLS;
307 LeaveCriticalSection( &PROCESS_Current()->crit_section );
313 /***********************************************************************
314 * MODULE_CreateDummyModule
316 * Create a dummy NE module for Win32 or Winelib.
318 HMODULE MODULE_CreateDummyModule( LPCSTR filename, HMODULE module32 )
322 SEGTABLEENTRY *pSegment;
325 const char* basename;
329 /* Extract base filename */
330 basename = strrchr(filename, '\\');
331 if (!basename) basename = filename;
333 len = strlen(basename);
334 if ((s = strchr(basename, '.'))) len = s - basename;
336 /* Allocate module */
337 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
338 + strlen(filename) + 1;
339 size = sizeof(NE_MODULE) +
340 /* loaded file info */
342 /* segment table: DS,CS */
343 2 * sizeof(SEGTABLEENTRY) +
346 /* several empty tables */
349 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
350 if (!hModule) return (HMODULE)11; /* invalid exe */
352 FarSetOwner16( hModule, hModule );
353 pModule = (NE_MODULE *)GlobalLock16( hModule );
355 /* Set all used entries */
356 pModule->magic = IMAGE_OS2_SIGNATURE;
363 pModule->heap_size = 0;
364 pModule->stack_size = 0;
365 pModule->seg_count = 2;
366 pModule->modref_count = 0;
367 pModule->nrname_size = 0;
368 pModule->fileinfo = sizeof(NE_MODULE);
369 pModule->os_flags = NE_OSFLAGS_WINDOWS;
370 pModule->self = hModule;
371 pModule->module32 = module32;
373 /* Set version and flags */
376 pModule->expected_version =
377 ((PE_HEADER(module32)->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
378 (PE_HEADER(module32)->OptionalHeader.MinorSubsystemVersion & 0xff);
379 pModule->flags |= NE_FFLAGS_WIN32;
380 if (PE_HEADER(module32)->FileHeader.Characteristics & IMAGE_FILE_DLL)
381 pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
384 /* Set loaded file information */
385 ofs = (OFSTRUCT *)(pModule + 1);
386 memset( ofs, 0, of_size );
387 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
388 strcpy( ofs->szPathName, filename );
390 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
391 pModule->seg_table = (int)pSegment - (int)pModule;
394 pSegment->flags = NE_SEGFLAGS_DATA;
395 pSegment->minsize = 0x1000;
402 pStr = (char *)pSegment;
403 pModule->name_table = (int)pStr - (int)pModule;
406 lstrcpynA( pStr+1, basename, len+1 );
409 /* All tables zero terminated */
410 pModule->res_table = pModule->import_table = pModule->entry_table =
411 (int)pStr - (int)pModule;
413 NE_RegisterModule( pModule );
418 /**********************************************************************
421 * Find a (loaded) win32 module depending on path
424 * the module handle if found
427 WINE_MODREF *MODULE_FindModule(
428 LPCSTR path /* [in] pathname of module/library to be found */
431 char dllname[260], *p;
433 /* Append .DLL to name if no extension present */
434 strcpy( dllname, path );
435 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
436 strcat( dllname, ".DLL" );
438 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
440 if ( !strcasecmp( dllname, wm->modname ) )
442 if ( !strcasecmp( dllname, wm->filename ) )
444 if ( !strcasecmp( dllname, wm->short_modname ) )
446 if ( !strcasecmp( dllname, wm->short_filename ) )
454 /* Check whether a file is an OS/2 or a very old Windows executable
455 * by testing on import of KERNEL.
457 * FIXME: is reading the module imports the only way of discerning
458 * old Windows binaries from OS/2 ones ? At least it seems so...
460 static DWORD MODULE_Decide_OS2_OldWin(HANDLE hfile, IMAGE_DOS_HEADER *mz, IMAGE_OS2_HEADER *ne)
462 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
463 DWORD type = SCS_OS216_BINARY;
464 LPWORD modtab = NULL;
465 LPSTR nametab = NULL;
469 /* read modref table */
470 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
471 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
472 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
473 || (len != ne->ne_cmod*sizeof(WORD)) )
476 /* read imported names table */
477 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
478 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
479 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
480 || (len != ne->ne_enttab - ne->ne_imptab) )
483 for (i=0; i < ne->ne_cmod; i++)
485 LPSTR module = &nametab[modtab[i]];
486 TRACE("modref: %.*s\n", module[0], &module[1]);
487 if (!(strncmp(&module[1], "KERNEL", module[0])))
488 { /* very old Windows file */
489 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
490 type = SCS_WOW_BINARY;
496 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
499 HeapFree( GetProcessHeap(), 0, modtab);
500 HeapFree( GetProcessHeap(), 0, nametab);
501 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
505 /***********************************************************************
506 * MODULE_GetBinaryType
508 * The GetBinaryType function determines whether a file is executable
509 * or not and if it is it returns what type of executable it is.
510 * The type of executable is a property that determines in which
511 * subsystem an executable file runs under.
513 * Binary types returned:
514 * SCS_32BIT_BINARY: A Win32 based application
515 * SCS_DOS_BINARY: An MS-Dos based application
516 * SCS_WOW_BINARY: A Win16 based application
517 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
518 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
519 * SCS_OS216_BINARY: A 16bit OS/2 based application
521 * Returns TRUE if the file is an executable in which case
522 * the value pointed by lpBinaryType is set.
523 * Returns FALSE if the file is not an executable or if the function fails.
525 * To do so it opens the file and reads in the header information
526 * if the extended header information is not present it will
527 * assume that the file is a DOS executable.
528 * If the extended header information is present it will
529 * determine if the file is a 16 or 32 bit Windows executable
530 * by check the flags in the header.
532 * Note that .COM and .PIF files are only recognized by their
533 * file name extension; but Windows does it the same way ...
535 static BOOL MODULE_GetBinaryType( HANDLE hfile, LPCSTR filename, LPDWORD lpBinaryType )
537 IMAGE_DOS_HEADER mz_header;
541 /* Seek to the start of the file and read the DOS header information.
543 if ( SetFilePointer( hfile, 0, NULL, SEEK_SET ) != -1
544 && ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL )
545 && len == sizeof(mz_header) )
547 /* Now that we have the header check the e_magic field
548 * to see if this is a dos image.
550 if ( mz_header.e_magic == IMAGE_DOS_SIGNATURE )
552 BOOL lfanewValid = FALSE;
553 /* We do have a DOS image so we will now try to seek into
554 * the file by the amount indicated by the field
555 * "Offset to extended header" and read in the
556 * "magic" field information at that location.
557 * This will tell us if there is more header information
560 /* But before we do we will make sure that header
561 * structure encompasses the "Offset to extended header"
564 if ( (mz_header.e_cparhdr<<4) >= sizeof(IMAGE_DOS_HEADER) )
565 if ( ( mz_header.e_crlc == 0 ) ||
566 ( mz_header.e_lfarlc >= sizeof(IMAGE_DOS_HEADER) ) )
567 if ( mz_header.e_lfanew >= sizeof(IMAGE_DOS_HEADER)
568 && SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET ) != -1
569 && ReadFile( hfile, magic, sizeof(magic), &len, NULL )
570 && len == sizeof(magic) )
575 /* If we cannot read this "extended header" we will
576 * assume that we have a simple DOS executable.
578 *lpBinaryType = SCS_DOS_BINARY;
583 /* Reading the magic field succeeded so
584 * we will try to determine what type it is.
586 if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
588 /* This is an NT signature.
590 *lpBinaryType = SCS_32BIT_BINARY;
593 else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
595 /* The IMAGE_OS2_SIGNATURE indicates that the
596 * "extended header is a Windows executable (NE)
597 * header." This can mean either a 16-bit OS/2
598 * or a 16-bit Windows or even a DOS program
599 * (running under a DOS extender). To decide
600 * which, we'll have to read the NE header.
604 if ( SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET ) != -1
605 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
606 && len == sizeof(ne) )
608 switch ( ne.ne_exetyp )
610 case 2: *lpBinaryType = SCS_WOW_BINARY; return TRUE;
611 case 5: *lpBinaryType = SCS_DOS_BINARY; return TRUE;
612 default: *lpBinaryType =
613 MODULE_Decide_OS2_OldWin(hfile, &mz_header, &ne);
617 /* Couldn't read header, so abort. */
622 /* Unknown extended header, but this file is nonetheless
625 *lpBinaryType = SCS_DOS_BINARY;
632 /* If we get here, we don't even have a correct MZ header.
633 * Try to check the file extension for known types ...
635 ptr = strrchr( filename, '.' );
636 if ( ptr && !strchr( ptr, '\\' ) && !strchr( ptr, '/' ) )
638 if ( !strcasecmp( ptr, ".COM" ) )
640 *lpBinaryType = SCS_DOS_BINARY;
644 if ( !strcasecmp( ptr, ".PIF" ) )
646 *lpBinaryType = SCS_PIF_BINARY;
654 /***********************************************************************
655 * GetBinaryTypeA [KERNEL32.280]
657 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
662 TRACE_(win32)("%s\n", lpApplicationName );
666 if ( lpApplicationName == NULL || lpBinaryType == NULL )
669 /* Open the file indicated by lpApplicationName for reading.
671 hfile = CreateFileA( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
672 NULL, OPEN_EXISTING, 0, -1 );
673 if ( hfile == INVALID_HANDLE_VALUE )
678 ret = MODULE_GetBinaryType( hfile, lpApplicationName, lpBinaryType );
682 CloseHandle( hfile );
687 /***********************************************************************
688 * GetBinaryTypeW [KERNEL32.281]
690 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
695 TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) );
699 if ( lpApplicationName == NULL || lpBinaryType == NULL )
702 /* Convert the wide string to a ascii string.
704 strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
706 if ( strNew != NULL )
708 ret = GetBinaryTypeA( strNew, lpBinaryType );
710 /* Free the allocated string.
712 HeapFree( GetProcessHeap(), 0, strNew );
719 /***********************************************************************
720 * WinExec16 (KERNEL.166)
722 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
724 LPCSTR p, args = NULL;
725 LPCSTR name_beg, name_end;
729 char buffer[MAX_PATH];
731 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
733 name_beg = lpCmdLine+1;
734 p = strchr ( lpCmdLine+1, '"' );
738 args = strchr ( p, ' ' );
740 else /* yes, even valid with trailing '"' missing */
741 name_end = lpCmdLine+strlen(lpCmdLine);
745 name_beg = lpCmdLine;
746 args = strchr( lpCmdLine, ' ' );
747 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
750 if ((name_beg == lpCmdLine) && (!args))
751 { /* just use the original cmdline string as file name */
752 name = (LPSTR)lpCmdLine;
756 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
757 return ERROR_NOT_ENOUGH_MEMORY;
758 memcpy( name, name_beg, name_end - name_beg );
759 name[name_end - name_beg] = '\0';
765 arglen = strlen(args);
766 cmdline = SEGPTR_ALLOC( 2 + arglen );
767 cmdline[0] = (BYTE)arglen;
768 strcpy( cmdline + 1, args );
772 cmdline = SEGPTR_ALLOC( 2 );
773 cmdline[0] = cmdline[1] = 0;
776 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
778 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
781 WORD *showCmd = SEGPTR_ALLOC( 2*sizeof(WORD) );
783 showCmd[1] = nCmdShow;
785 params.hEnvironment = 0;
786 params.cmdLine = SEGPTR_GET(cmdline);
787 params.showCmd = SEGPTR_GET(showCmd);
790 ret = LoadModule16( buffer, ¶ms );
792 SEGPTR_FREE( showCmd );
793 SEGPTR_FREE( cmdline );
795 else ret = GetLastError();
797 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
799 if (ret == 21) /* 32-bit module */
801 SYSLEVEL_ReleaseWin16Lock();
802 ret = WinExec( lpCmdLine, nCmdShow );
803 SYSLEVEL_RestoreWin16Lock();
808 /***********************************************************************
809 * WinExec (KERNEL32.566)
811 HINSTANCE WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
813 PROCESS_INFORMATION info;
814 STARTUPINFOA startup;
818 memset( &startup, 0, sizeof(startup) );
819 startup.cb = sizeof(startup);
820 startup.dwFlags = STARTF_USESHOWWINDOW;
821 startup.wShowWindow = nCmdShow;
823 /* cmdline needs to be writeable for CreateProcess */
824 if (!(cmdline = HEAP_strdupA( GetProcessHeap(), 0, lpCmdLine ))) return 0;
826 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
827 0, NULL, NULL, &startup, &info ))
829 /* Give 30 seconds to the app to come up */
830 if (Callout.WaitForInputIdle &&
831 Callout.WaitForInputIdle( info.hProcess, 30000 ) == 0xFFFFFFFF)
832 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
834 /* Close off the handles */
835 CloseHandle( info.hThread );
836 CloseHandle( info.hProcess );
838 else if ((hInstance = GetLastError()) >= 32)
840 FIXME("Strange error set by CreateProcess: %d\n", hInstance );
843 HeapFree( GetProcessHeap(), 0, cmdline );
847 /**********************************************************************
848 * LoadModule (KERNEL32.499)
850 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
852 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
853 PROCESS_INFORMATION info;
854 STARTUPINFOA startup;
857 char filename[MAX_PATH];
860 if (!name) return ERROR_FILE_NOT_FOUND;
862 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
863 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
864 return GetLastError();
866 len = (BYTE)params->lpCmdLine[0];
867 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
868 return ERROR_NOT_ENOUGH_MEMORY;
870 strcpy( cmdline, filename );
871 p = cmdline + strlen(cmdline);
873 memcpy( p, params->lpCmdLine + 1, len );
876 memset( &startup, 0, sizeof(startup) );
877 startup.cb = sizeof(startup);
878 if (params->lpCmdShow)
880 startup.dwFlags = STARTF_USESHOWWINDOW;
881 startup.wShowWindow = params->lpCmdShow[1];
884 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
885 params->lpEnvAddress, NULL, &startup, &info ))
887 /* Give 30 seconds to the app to come up */
888 if (Callout.WaitForInputIdle &&
889 Callout.WaitForInputIdle( info.hProcess, 30000 ) == 0xFFFFFFFF )
890 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
892 /* Close off the handles */
893 CloseHandle( info.hThread );
894 CloseHandle( info.hProcess );
896 else if ((hInstance = GetLastError()) >= 32)
898 FIXME("Strange error set by CreateProcess: %d\n", hInstance );
902 HeapFree( GetProcessHeap(), 0, cmdline );
907 /*************************************************************************
910 * Helper for CreateProcess: retrieve the file name to load from the
911 * app name and command line. Store the file name in buffer, and
912 * return a possibly modified command line.
914 static LPSTR get_file_name( LPCSTR appname, LPSTR cmdline, LPSTR buffer, int buflen )
916 char *name, *pos, *ret = NULL;
919 /* if we have an app name, everything is easy */
923 /* use the unmodified app name as file name */
924 lstrcpynA( buffer, appname, buflen );
925 if (!(ret = cmdline))
927 /* no command-line, create one */
928 if ((ret = HeapAlloc( GetProcessHeap(), 0, strlen(appname) + 3 )))
929 sprintf( ret, "\"%s\"", appname );
936 SetLastError( ERROR_INVALID_PARAMETER );
940 /* first check for a quoted file name */
942 if ((cmdline[0] == '"') && ((p = strchr( cmdline + 1, '"' ))))
944 int len = p - cmdline - 1;
945 /* extract the quoted portion as file name */
946 if (!(name = HeapAlloc( GetProcessHeap(), 0, len + 1 ))) return NULL;
947 memcpy( name, cmdline + 1, len );
950 if (SearchPathA( NULL, name, ".exe", buflen, buffer, NULL ) ||
951 SearchPathA( NULL, name, NULL, buflen, buffer, NULL ))
952 ret = cmdline; /* no change necessary */
956 /* now try the command-line word by word */
958 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(cmdline) + 1 ))) return NULL;
964 do *pos++ = *p++; while (*p && *p != ' ');
966 TRACE("trying '%s'\n", name );
967 if (SearchPathA( NULL, name, ".exe", buflen, buffer, NULL ) ||
968 SearchPathA( NULL, name, NULL, buflen, buffer, NULL ))
975 if (!ret || !strchr( name, ' ' )) goto done; /* no change necessary */
977 /* now build a new command-line with quotes */
979 if (!(ret = HeapAlloc( GetProcessHeap(), 0, strlen(cmdline) + 3 ))) goto done;
980 sprintf( ret, "\"%s\"%s", name, p );
983 HeapFree( GetProcessHeap(), 0, name );
988 /**********************************************************************
989 * CreateProcessA (KERNEL32.171)
991 BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
992 LPSECURITY_ATTRIBUTES lpProcessAttributes,
993 LPSECURITY_ATTRIBUTES lpThreadAttributes,
994 BOOL bInheritHandles, DWORD dwCreationFlags,
995 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
996 LPSTARTUPINFOA lpStartupInfo,
997 LPPROCESS_INFORMATION lpProcessInfo )
1002 char name[MAX_PATH];
1005 /* Process the AppName and/or CmdLine to get module name and path */
1007 TRACE("app '%s' cmdline '%s'\n", lpApplicationName, lpCommandLine );
1009 if (!(tidy_cmdline = get_file_name( lpApplicationName, lpCommandLine, name, sizeof(name) )))
1012 /* Warn if unsupported features are used */
1014 if (dwCreationFlags & DETACHED_PROCESS)
1015 FIXME("(%s,...): DETACHED_PROCESS ignored\n", name);
1016 if (dwCreationFlags & CREATE_NEW_CONSOLE)
1017 FIXME("(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
1018 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
1019 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
1020 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
1021 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
1022 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
1023 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
1024 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
1025 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
1026 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
1027 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
1028 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
1029 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
1030 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
1031 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
1032 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
1033 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
1034 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
1035 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
1036 if (dwCreationFlags & CREATE_NO_WINDOW)
1037 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name);
1038 if (dwCreationFlags & PROFILE_USER)
1039 FIXME("(%s,...): PROFILE_USER ignored\n", name);
1040 if (dwCreationFlags & PROFILE_KERNEL)
1041 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name);
1042 if (dwCreationFlags & PROFILE_SERVER)
1043 FIXME("(%s,...): PROFILE_SERVER ignored\n", name);
1044 if (lpStartupInfo->lpDesktop)
1045 FIXME("(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
1046 name, lpStartupInfo->lpDesktop);
1047 if (lpStartupInfo->lpTitle)
1048 FIXME("(%s,...): lpStartupInfo->lpTitle %s ignored\n",
1049 name, lpStartupInfo->lpTitle);
1050 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
1051 FIXME("(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
1052 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
1053 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
1054 FIXME("(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
1055 name, lpStartupInfo->dwFillAttribute);
1056 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
1057 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
1058 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
1059 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
1060 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
1061 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
1062 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
1063 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name);
1065 /* Open file and determine executable type */
1067 hFile = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, -1 );
1068 if (hFile == INVALID_HANDLE_VALUE) goto done;
1070 if ( !MODULE_GetBinaryType( hFile, name, &type ) )
1072 CloseHandle( hFile );
1073 retv = PROCESS_Create( -1, name, tidy_cmdline, lpEnvironment,
1074 lpProcessAttributes, lpThreadAttributes,
1075 bInheritHandles, dwCreationFlags,
1076 lpStartupInfo, lpProcessInfo, lpCurrentDirectory );
1080 /* Create process */
1084 case SCS_32BIT_BINARY:
1085 case SCS_WOW_BINARY:
1086 case SCS_DOS_BINARY:
1087 retv = PROCESS_Create( hFile, name, tidy_cmdline, lpEnvironment,
1088 lpProcessAttributes, lpThreadAttributes,
1089 bInheritHandles, dwCreationFlags,
1090 lpStartupInfo, lpProcessInfo, lpCurrentDirectory);
1093 case SCS_PIF_BINARY:
1094 case SCS_POSIX_BINARY:
1095 case SCS_OS216_BINARY:
1096 FIXME("Unsupported executable type: %ld\n", type );
1100 SetLastError( ERROR_BAD_FORMAT );
1103 CloseHandle( hFile );
1106 if (tidy_cmdline != lpCommandLine) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1110 /**********************************************************************
1111 * CreateProcessW (KERNEL32.172)
1113 * lpReserved is not converted
1115 BOOL WINAPI CreateProcessW( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
1116 LPSECURITY_ATTRIBUTES lpProcessAttributes,
1117 LPSECURITY_ATTRIBUTES lpThreadAttributes,
1118 BOOL bInheritHandles, DWORD dwCreationFlags,
1119 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
1120 LPSTARTUPINFOW lpStartupInfo,
1121 LPPROCESS_INFORMATION lpProcessInfo )
1123 STARTUPINFOA StartupInfoA;
1125 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
1126 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
1127 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
1129 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFOA));
1130 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
1131 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
1133 TRACE_(win32)("(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
1135 if (lpStartupInfo->lpReserved)
1136 FIXME_(win32)("StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
1138 ret = CreateProcessA( lpApplicationNameA, lpCommandLineA,
1139 lpProcessAttributes, lpThreadAttributes,
1140 bInheritHandles, dwCreationFlags,
1141 lpEnvironment, lpCurrentDirectoryA,
1142 &StartupInfoA, lpProcessInfo );
1144 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
1145 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
1146 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
1147 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
1152 /***********************************************************************
1153 * GetModuleHandleA (KERNEL32.237)
1155 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
1159 if ( module == NULL )
1160 wm = PROCESS_Current()->exe_modref;
1162 wm = MODULE_FindModule( module );
1164 return wm? wm->module : 0;
1167 /***********************************************************************
1170 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
1173 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
1174 hModule = GetModuleHandleA( modulea );
1175 HeapFree( GetProcessHeap(), 0, modulea );
1180 /***********************************************************************
1181 * GetModuleFileNameA (KERNEL32.235)
1183 * GetModuleFileNameA seems to *always* return the long path;
1184 * it's only GetModuleFileName16 that decides between short/long path
1185 * by checking if exe version >= 4.0.
1186 * (SDK docu doesn't mention this)
1188 DWORD WINAPI GetModuleFileNameA(
1189 HMODULE hModule, /* [in] module handle (32bit) */
1190 LPSTR lpFileName, /* [out] filenamebuffer */
1191 DWORD size ) /* [in] size of filenamebuffer */
1195 EnterCriticalSection( &PROCESS_Current()->crit_section );
1198 if ((wm = MODULE32_LookupHMODULE( hModule )))
1199 lstrcpynA( lpFileName, wm->filename, size );
1201 LeaveCriticalSection( &PROCESS_Current()->crit_section );
1202 TRACE("%s\n", lpFileName );
1203 return strlen(lpFileName);
1207 /***********************************************************************
1208 * GetModuleFileNameW (KERNEL32.236)
1210 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName,
1213 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
1214 DWORD res = GetModuleFileNameA( hModule, fnA, size );
1215 lstrcpynAtoW( lpFileName, fnA, size );
1216 HeapFree( GetProcessHeap(), 0, fnA );
1221 /***********************************************************************
1222 * LoadLibraryExA (KERNEL32)
1224 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
1230 SetLastError(ERROR_INVALID_PARAMETER);
1234 if (flags & LOAD_LIBRARY_AS_DATAFILE)
1240 if (!SearchPathA( NULL, libname, ".dll", sizeof(filename), filename, NULL ))
1242 /* FIXME: maybe we should use the hfile parameter instead */
1243 hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
1244 NULL, OPEN_EXISTING, 0, -1 );
1245 if (hFile != INVALID_HANDLE_VALUE)
1247 hmod = PE_LoadImage( hFile, filename, flags );
1248 CloseHandle( hFile );
1253 EnterCriticalSection(&PROCESS_Current()->crit_section);
1255 wm = MODULE_LoadLibraryExA( libname, hfile, flags );
1258 if ( !MODULE_DllProcessAttach( wm, NULL ) )
1260 WARN_(module)("Attach failed for module '%s', \n", libname);
1261 MODULE_FreeLibrary(wm);
1262 SetLastError(ERROR_DLL_INIT_FAILED);
1267 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1269 return wm ? wm->module : 0;
1272 /***********************************************************************
1273 * MODULE_LoadLibraryExA (internal)
1275 * Load a PE style module according to the load order.
1277 * The HFILE parameter is not used and marked reserved in the SDK. I can
1278 * only guess that it should force a file to be mapped, but I rather
1279 * ignore the parameter because it would be extremely difficult to
1280 * integrate this with different types of module represenations.
1283 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
1285 DWORD err = GetLastError();
1288 module_loadorder_t *plo;
1291 if ( !libname ) return NULL;
1293 filename = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1294 if ( !filename ) return NULL;
1296 /* build the modules filename */
1297 if (!SearchPathA( NULL, libname, ".dll", MAX_PATH, filename, NULL ))
1299 if ( ! GetSystemDirectoryA ( filename, MAX_PATH ) )
1302 /* if the library name contains a path and can not be found, return an error.
1303 exception: if the path is the system directory, proceed, so that modules,
1304 which are not PE-modules can be loaded
1306 if the library name does not contain a path and can not be found, assume the
1307 system directory is meant */
1309 if ( ! strncasecmp ( filename, libname, strlen ( filename ) ))
1310 strcpy ( filename, libname );
1313 if ( strchr ( libname, '\\' ) || strchr ( libname, ':') || strchr ( libname, '/' ) )
1317 strcat ( filename, "\\" );
1318 strcat ( filename, libname );
1322 /* if the filename doesn't have an extension append .DLL */
1323 if (!(p = strrchr( filename, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
1324 strcat( filename, ".DLL" );
1327 EnterCriticalSection(&PROCESS_Current()->crit_section);
1329 /* Check for already loaded module */
1330 if (!(pwm = MODULE_FindModule(filename)) &&
1331 /* no path in libpath */
1332 !strchr( libname, '\\' ) && !strchr( libname, ':') && !strchr( libname, '/' ))
1334 LPSTR fn = HeapAlloc ( GetProcessHeap(), 0, MAX_PATH + 1 );
1337 /* since the default loading mechanism uses a more detailed algorithm
1338 * than SearchPath (like using PATH, which can even be modified between
1339 * two attempts of loading the same DLL), the look-up above (with
1340 * SearchPath) can have put the file in system directory, whereas it
1341 * has already been loaded but with a different path. So do a specific
1342 * look-up with filename (without any path)
1344 strcpy ( fn, libname );
1345 /* if the filename doesn't have an extension append .DLL */
1346 if (!strrchr( fn, '.')) strcat( fn, ".dll" );
1347 if ((pwm = MODULE_FindModule( fn )) != NULL)
1348 strcpy( filename, fn );
1349 HeapFree( GetProcessHeap(), 0, fn );
1354 if(!(pwm->flags & WINE_MODREF_MARKER))
1357 if ((pwm->flags & WINE_MODREF_DONT_RESOLVE_REFS) &&
1358 !(flags & DONT_RESOLVE_DLL_REFERENCES))
1360 extern DWORD fixup_imports(WINE_MODREF *wm); /*FIXME*/
1361 pwm->flags &= ~WINE_MODREF_DONT_RESOLVE_REFS;
1362 fixup_imports( pwm );
1364 TRACE("Already loaded module '%s' at 0x%08x, count=%d, \n", filename, pwm->module, pwm->refCount);
1365 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1366 HeapFree ( GetProcessHeap(), 0, filename );
1370 plo = MODULE_GetLoadOrder(filename, TRUE);
1372 for(i = 0; i < MODULE_LOADORDER_NTYPES; i++)
1374 SetLastError( ERROR_FILE_NOT_FOUND );
1375 switch(plo->loadorder[i])
1377 case MODULE_LOADORDER_DLL:
1378 TRACE("Trying native dll '%s'\n", filename);
1379 pwm = PE_LoadLibraryExA(filename, flags);
1382 case MODULE_LOADORDER_ELFDLL:
1383 TRACE("Trying elfdll '%s'\n", filename);
1384 if (!(pwm = BUILTIN32_LoadLibraryExA(filename, flags)))
1385 pwm = ELFDLL_LoadLibraryExA(filename, flags);
1388 case MODULE_LOADORDER_SO:
1389 TRACE("Trying so-library '%s'\n", filename);
1390 if (!(pwm = BUILTIN32_LoadLibraryExA(filename, flags)))
1391 pwm = ELF_LoadLibraryExA(filename, flags);
1394 case MODULE_LOADORDER_BI:
1395 TRACE("Trying built-in '%s'\n", filename);
1396 pwm = BUILTIN32_LoadLibraryExA(filename, flags);
1400 ERR("Got invalid loadorder type %d (%s index %d)\n", plo->loadorder[i], plo->modulename, i);
1403 case MODULE_LOADORDER_INVALID: /* We ignore this as it is an empty entry */
1410 /* Initialize DLL just loaded */
1411 TRACE("Loaded module '%s' at 0x%08x, \n", filename, pwm->module);
1413 /* Set the refCount here so that an attach failure will */
1414 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1417 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1418 SetLastError( err ); /* restore last error */
1419 HeapFree ( GetProcessHeap(), 0, filename );
1423 if(GetLastError() != ERROR_FILE_NOT_FOUND)
1427 LeaveCriticalSection(&PROCESS_Current()->crit_section);
1429 WARN("Failed to load module '%s'; error=0x%08lx, \n", filename, GetLastError());
1430 HeapFree ( GetProcessHeap(), 0, filename );
1434 /***********************************************************************
1435 * LoadLibraryA (KERNEL32)
1437 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
1438 return LoadLibraryExA(libname,0,0);
1441 /***********************************************************************
1442 * LoadLibraryW (KERNEL32)
1444 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
1446 return LoadLibraryExW(libnameW,0,0);
1449 /***********************************************************************
1450 * LoadLibrary32_16 (KERNEL.452)
1452 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
1456 SYSLEVEL_ReleaseWin16Lock();
1457 hModule = LoadLibraryA( libname );
1458 SYSLEVEL_RestoreWin16Lock();
1463 /***********************************************************************
1464 * LoadLibraryExW (KERNEL32)
1466 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HANDLE hfile,DWORD flags)
1468 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
1469 HMODULE ret = LoadLibraryExA( libnameA , hfile, flags );
1471 HeapFree( GetProcessHeap(), 0, libnameA );
1475 /***********************************************************************
1476 * MODULE_FlushModrefs
1478 * NOTE: Assumes that the process critical section is held!
1480 * Remove all unused modrefs and call the internal unloading routines
1481 * for the library type.
1483 static void MODULE_FlushModrefs(void)
1485 WINE_MODREF *wm, *next;
1487 for(wm = PROCESS_Current()->modref_list; wm; wm = next)
1494 /* Unlink this modref from the chain */
1496 wm->next->prev = wm->prev;
1498 wm->prev->next = wm->next;
1499 if(wm == PROCESS_Current()->modref_list)
1500 PROCESS_Current()->modref_list = wm->next;
1502 TRACE(" unloading %s\n", wm->filename);
1503 /* VirtualFree( (LPVOID)wm->module, 0, MEM_RELEASE ); */ /* FIXME */
1504 /* if (wm->dlhandle) dlclose( wm->dlhandle ); */ /* FIXME */
1505 FreeLibrary16(wm->hDummyMod);
1506 HeapFree( GetProcessHeap(), 0, wm->deps );
1507 HeapFree( GetProcessHeap(), 0, wm->filename );
1508 HeapFree( GetProcessHeap(), 0, wm->short_filename );
1509 HeapFree( GetProcessHeap(), 0, wm );
1513 /***********************************************************************
1516 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
1521 EnterCriticalSection( &PROCESS_Current()->crit_section );
1522 PROCESS_Current()->free_lib_count++;
1524 wm = MODULE32_LookupHMODULE( hLibModule );
1525 if ( !wm || !hLibModule )
1526 SetLastError( ERROR_INVALID_HANDLE );
1528 retv = MODULE_FreeLibrary( wm );
1530 PROCESS_Current()->free_lib_count--;
1531 LeaveCriticalSection( &PROCESS_Current()->crit_section );
1536 /***********************************************************************
1537 * MODULE_DecRefCount
1539 * NOTE: Assumes that the process critical section is held!
1541 static void MODULE_DecRefCount( WINE_MODREF *wm )
1545 if ( wm->flags & WINE_MODREF_MARKER )
1548 if ( wm->refCount <= 0 )
1552 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
1554 if ( wm->refCount == 0 )
1556 wm->flags |= WINE_MODREF_MARKER;
1558 for ( i = 0; i < wm->nDeps; i++ )
1560 MODULE_DecRefCount( wm->deps[i] );
1562 wm->flags &= ~WINE_MODREF_MARKER;
1566 /***********************************************************************
1567 * MODULE_FreeLibrary
1569 * NOTE: Assumes that the process critical section is held!
1571 BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
1573 TRACE("(%s) - START\n", wm->modname );
1575 /* Recursively decrement reference counts */
1576 MODULE_DecRefCount( wm );
1578 /* Call process detach notifications */
1579 if ( PROCESS_Current()->free_lib_count <= 1 )
1581 MODULE_DllProcessDetach( FALSE, NULL );
1584 struct unload_dll_request *req = server_alloc_req( sizeof(*req), 0 );
1585 req->base = (void *)wm->module;
1586 server_call_noerr( REQ_UNLOAD_DLL );
1589 MODULE_FlushModrefs();
1598 /***********************************************************************
1599 * FreeLibraryAndExitThread
1601 VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
1603 FreeLibrary(hLibModule);
1604 ExitThread(dwExitCode);
1607 /***********************************************************************
1608 * PrivateLoadLibrary (KERNEL32)
1610 * FIXME: rough guesswork, don't know what "Private" means
1612 HINSTANCE WINAPI PrivateLoadLibrary(LPCSTR libname)
1614 return (HINSTANCE)LoadLibrary16(libname);
1619 /***********************************************************************
1620 * PrivateFreeLibrary (KERNEL32)
1622 * FIXME: rough guesswork, don't know what "Private" means
1624 void WINAPI PrivateFreeLibrary(HINSTANCE handle)
1626 FreeLibrary16((HINSTANCE16)handle);
1630 /***********************************************************************
1631 * WIN32_GetProcAddress16 (KERNEL32.36)
1632 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1634 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1640 WARN("hModule may not be 0!\n");
1641 return (FARPROC16)0;
1643 if (HIWORD(hModule))
1645 WARN("hModule is Win32 handle (%08x)\n", hModule );
1646 return (FARPROC16)0;
1648 hModule = GetExePtr( hModule );
1650 ordinal = NE_GetOrdinal( hModule, name );
1651 TRACE("%04x '%s'\n", hModule, name );
1653 ordinal = LOWORD(name);
1654 TRACE("%04x %04x\n", hModule, ordinal );
1656 if (!ordinal) return (FARPROC16)0;
1657 ret = NE_GetEntryPoint( hModule, ordinal );
1658 TRACE("returning %08x\n",(UINT)ret);
1662 /***********************************************************************
1663 * GetProcAddress16 (KERNEL.50)
1665 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1670 if (!hModule) hModule = GetCurrentTask();
1671 hModule = GetExePtr( hModule );
1673 if (HIWORD(name) != 0)
1675 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1676 TRACE("%04x '%s'\n", hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1680 ordinal = LOWORD(name);
1681 TRACE("%04x %04x\n", hModule, ordinal );
1683 if (!ordinal) return (FARPROC16)0;
1685 ret = NE_GetEntryPoint( hModule, ordinal );
1687 TRACE("returning %08x\n", (UINT)ret );
1692 /***********************************************************************
1693 * GetProcAddress (KERNEL32.257)
1695 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
1697 return MODULE_GetProcAddress( hModule, function, TRUE );
1700 /***********************************************************************
1701 * GetProcAddress32 (KERNEL.453)
1703 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
1705 return MODULE_GetProcAddress( hModule, function, FALSE );
1708 /***********************************************************************
1709 * MODULE_GetProcAddress (internal)
1711 FARPROC MODULE_GetProcAddress(
1712 HMODULE hModule, /* [in] current module handle */
1713 LPCSTR function, /* [in] function to be looked up */
1717 FARPROC retproc = 0;
1719 if (HIWORD(function))
1720 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
1722 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
1724 EnterCriticalSection( &PROCESS_Current()->crit_section );
1725 if ((wm = MODULE32_LookupHMODULE( hModule )))
1727 retproc = wm->find_export( wm, function, snoop );
1728 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
1730 LeaveCriticalSection( &PROCESS_Current()->crit_section );
1735 /***************************************************************************
1736 * HasGPHandler (KERNEL.338)
1739 #include "pshpack1.h"
1740 typedef struct _GPHANDLERDEF
1747 #include "poppack.h"
1749 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1754 GPHANDLERDEF *gpHandler;
1756 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1757 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1758 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1759 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1760 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1762 while (gpHandler->selector)
1764 if ( SELECTOROF(address) == gpHandler->selector
1765 && OFFSETOF(address) >= gpHandler->rangeStart
1766 && OFFSETOF(address) < gpHandler->rangeEnd )
1767 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1768 gpHandler->handler );