4 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
13 #include "wine/winuser16.h"
14 #include "wine/winbase16.h"
26 #include "selectors.h"
27 #include "stackframe.h"
32 extern BOOL32 THREAD_InitDone;
35 /*************************************************************************
36 * MODULE32_LookupHMODULE
37 * looks for the referenced HMODULE in the current process
39 WINE_MODREF *MODULE32_LookupHMODULE( HMODULE32 hmod )
44 return PROCESS_Current()->exe_modref;
47 ERR(module,"tried to lookup 0x%04x in win32 module handler!\n",hmod);
50 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next )
51 if (wm->module == hmod)
56 /*************************************************************************
57 * MODULE_InitializeDLLs
59 * Call the initialization routines of all DLLs belonging to the
60 * current process. This is somewhat complicated due to the fact that
62 * - we have to respect the module dependencies, i.e. modules implicitly
63 * referenced by another module have to be initialized before the module
64 * itself can be initialized
66 * - the initialization routine of a DLL can itself call LoadLibrary,
67 * thereby introducing a whole new set of dependencies (even involving
68 * the 'old' modules) at any time during the whole process
70 * (Note that this routine can be recursively entered not only directly
71 * from itself, but also via LoadLibrary from one of the called initialization
74 static void MODULE_DoInitializeDLLs( WINE_MODREF *wm,
75 DWORD type, LPVOID lpReserved )
79 assert( wm && !wm->initDone );
80 TRACE( module, "(%08x,%ld,%p) - START\n",
81 wm->module, type, lpReserved );
83 /* Tag current MODREF to prevent recursive loop */
86 /* Recursively initialize all child DLLs */
87 for ( i = 0; i < wm->nDeps; i++ )
88 if ( wm->deps[i] && !wm->deps[i]->initDone )
89 MODULE_DoInitializeDLLs( wm->deps[i], type, lpReserved );
91 /* Now we can call the initialization routine */
95 PE_InitDLL( wm, type, lpReserved );
99 /* no need to do that, dlopen() already does */
102 ERR(module, "wine_modref type %d not handled.\n", wm->type);
106 TRACE( module, "(%08x,%ld,%p) - END\n",
107 wm->module, type, lpReserved );
110 void MODULE_InitializeDLLs( HMODULE32 root, DWORD type, LPVOID lpReserved )
112 BOOL32 inProgress = FALSE;
115 /* Grab the process critical section to protect the recursion flags */
116 /* FIXME: This is probably overkill! */
117 EnterCriticalSection( &PROCESS_Current()->crit_section );
119 TRACE( module, "(%08x,%ld,%p) - START\n", root, type, lpReserved );
121 /* First, check whether initialization is currently in progress */
122 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
132 * If this a LoadLibrary call from within an initialization routine,
133 * treat it analogously to an implicitly referenced DLL.
134 * Anything else may not happen at this point!
138 wm = MODULE32_LookupHMODULE( root );
139 if ( wm && !wm->initDone )
140 MODULE_DoInitializeDLLs( wm, type, lpReserved );
143 FIXME(module, "Invalid recursion!\n");
147 /* If we arrive here, this is the start of an initialization run */
150 /* If called for main EXE, initialize all DLLs */
151 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
153 MODULE_DoInitializeDLLs( wm, type, lpReserved );
157 /* If called for a specific DLL, initialize only it and its children */
158 wm = MODULE32_LookupHMODULE( root );
159 if (wm) MODULE_DoInitializeDLLs( wm, type, lpReserved );
162 /* We're finished, so we reset all recursion flags */
163 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
164 wm->initDone = FALSE;
167 TRACE( module, "(%08x,%ld,%p) - END\n", root, type, lpReserved );
169 /* Release critical section */
170 LeaveCriticalSection( &PROCESS_Current()->crit_section );
174 /***********************************************************************
175 * MODULE_CreateDummyModule
177 * Create a dummy NE module for Win32 or Winelib.
179 HMODULE32 MODULE_CreateDummyModule( const OFSTRUCT *ofs, LPCSTR modName )
183 SEGTABLEENTRY *pSegment;
186 const char* basename;
188 INT32 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
189 + strlen(ofs->szPathName) + 1;
190 INT32 size = sizeof(NE_MODULE) +
191 /* loaded file info */
193 /* segment table: DS,CS */
194 2 * sizeof(SEGTABLEENTRY) +
197 /* several empty tables */
200 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
201 if (!hModule) return (HMODULE32)11; /* invalid exe */
203 FarSetOwner( hModule, hModule );
204 pModule = (NE_MODULE *)GlobalLock16( hModule );
206 /* Set all used entries */
207 pModule->magic = IMAGE_OS2_SIGNATURE;
214 pModule->heap_size = 0xe000;
215 pModule->stack_size = 0x1000;
216 pModule->seg_count = 2;
217 pModule->modref_count = 0;
218 pModule->nrname_size = 0;
219 pModule->fileinfo = sizeof(NE_MODULE);
220 pModule->os_flags = NE_OSFLAGS_WINDOWS;
221 pModule->expected_version = 0x030a;
222 pModule->self = hModule;
224 /* Set loaded file information */
225 memcpy( pModule + 1, ofs, of_size );
226 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
228 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
229 pModule->seg_table = pModule->dgroup_entry = (int)pSegment - (int)pModule;
232 pSegment->flags = NE_SEGFLAGS_DATA;
233 pSegment->minsize = 0x1000;
240 pStr = (char *)pSegment;
241 pModule->name_table = (int)pStr - (int)pModule;
246 basename = strrchr(ofs->szPathName,'\\');
247 if (!basename) basename = ofs->szPathName;
250 len = strlen(basename);
251 if ((s = strchr(basename,'.'))) len = s - basename;
252 if (len > 8) len = 8;
254 strncpy( pStr+1, basename, len );
255 if (len < 8) pStr[len+1] = 0;
258 /* All tables zero terminated */
259 pModule->res_table = pModule->import_table = pModule->entry_table =
260 (int)pStr - (int)pModule;
262 NE_RegisterModule( pModule );
267 /***********************************************************************
268 * MODULE_GetWndProcEntry16 (not a Windows API function)
270 * Return an entry point from the WPROCS dll.
272 FARPROC16 MODULE_GetWndProcEntry16( LPCSTR name )
274 FARPROC16 ret = NULL;
278 /* FIXME: hack for Winelib */
279 extern LRESULT ColorDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
280 extern LRESULT FileOpenDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
281 extern LRESULT FileSaveDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
282 extern LRESULT FindTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
283 extern LRESULT PrintDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
284 extern LRESULT PrintSetupDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
285 extern LRESULT ReplaceTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
287 if (!strcmp(name,"ColorDlgProc"))
288 return (FARPROC16)ColorDlgProc;
289 if (!strcmp(name,"FileOpenDlgProc"))
290 return (FARPROC16)FileOpenDlgProc;
291 if (!strcmp(name,"FileSaveDlgProc"))
292 return (FARPROC16)FileSaveDlgProc;
293 if (!strcmp(name,"FindTextDlgProc"))
294 return (FARPROC16)FindTextDlgProc16;
295 if (!strcmp(name,"PrintDlgProc"))
296 return (FARPROC16)PrintDlgProc;
297 if (!strcmp(name,"PrintSetupDlgProc"))
298 return (FARPROC16)PrintSetupDlgProc;
299 if (!strcmp(name,"ReplaceTextDlgProc"))
300 return (FARPROC16)ReplaceTextDlgProc16;
301 if (!strcmp(name,"DefResourceHandler"))
302 return (FARPROC16)NE_DefResourceHandler;
303 if (!strcmp(name,"LoadDIBIconHandler"))
304 return (FARPROC16)LoadDIBIconHandler;
305 if (!strcmp(name,"LoadDIBCursorHandler"))
306 return (FARPROC16)LoadDIBCursorHandler;
307 FIXME(module,"No mapping for %s(), add one in library/miscstubs.c\n",name);
314 static HMODULE32 hModule = 0;
316 if (!hModule) hModule = GetModuleHandle16( "WPROCS" );
317 ordinal = NE_GetOrdinal( hModule, name );
318 if (!(ret = NE_GetEntryPoint( hModule, ordinal )))
320 WARN( module, "%s not found\n", name );
328 /**********************************************************************
329 * MODULE_FindModule32
331 * Find a (loaded) win32 module depending on path
332 * The handling of '.' is a bit weird, but we need it that way,
333 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
334 * this is the only way to differentiate. (mainly hypertrm.exe)
337 * the module handle if found
340 HMODULE32 MODULE_FindModule32(
341 LPCSTR path /* [in] pathname of module/library to be found */
347 if (!(filename = strrchr( path, '\\' )))
348 filename = HEAP_strdupA( GetProcessHeap(), 0, path );
350 filename = HEAP_strdupA( GetProcessHeap(), 0, filename+1 );
351 dotptr=strrchr(filename,'.');
353 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
354 LPSTR xmodname,xdotptr;
356 assert (wm->modname);
357 xmodname = HEAP_strdupA( GetProcessHeap(), 0, wm->modname );
358 xdotptr=strrchr(xmodname,'.');
359 if ( (xdotptr && !dotptr) ||
362 if (dotptr) *dotptr = '\0';
363 if (xdotptr) *xdotptr = '\0';
365 if (!strcasecmp( filename, xmodname)) {
366 HeapFree( GetProcessHeap(), 0, filename );
367 HeapFree( GetProcessHeap(), 0, xmodname );
370 if (dotptr) *dotptr='.';
371 /* FIXME: add paths, shortname */
372 HeapFree( GetProcessHeap(), 0, xmodname );
374 /* if that fails, try looking for the filename... */
375 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
376 LPSTR xlname,xdotptr;
378 assert (wm->longname);
379 xlname = strrchr(wm->longname,'\\');
381 xlname = wm->longname;
384 xlname = HEAP_strdupA( GetProcessHeap(), 0, xlname );
385 xdotptr=strrchr(xlname,'.');
386 if ( (xdotptr && !dotptr) ||
389 if (dotptr) *dotptr = '\0';
390 if (xdotptr) *xdotptr = '\0';
392 if (!strcasecmp( filename, xlname)) {
393 HeapFree( GetProcessHeap(), 0, filename );
394 HeapFree( GetProcessHeap(), 0, xlname );
397 if (dotptr) *dotptr='.';
398 /* FIXME: add paths, shortname */
399 HeapFree( GetProcessHeap(), 0, xlname );
401 HeapFree( GetProcessHeap(), 0, filename );
407 /**********************************************************************
410 static HINSTANCE16 NE_CreateProcess( LPCSTR name, LPCSTR cmd_line, LPCSTR env,
411 BOOL32 inherit, LPSTARTUPINFO32A startup,
412 LPPROCESS_INFORMATION info )
414 HINSTANCE16 hInstance, hPrevInstance;
419 hInstance = NE_LoadModule( name, &hPrevInstance, TRUE, FALSE );
420 if (hInstance < 32) return hInstance;
422 if ( !(pModule = NE_GetPtr(hInstance))
423 || (pModule->flags & NE_FFLAGS_LIBMODULE))
429 /* Create a task for this instance */
431 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
433 PROCESS_Create( pModule, cmd_line, env, hInstance,
434 hPrevInstance, inherit, startup, info );
440 /**********************************************************************
441 * LoadModule16 (KERNEL.45)
443 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
446 LPSTR cmd_line, new_cmd_line;
448 STARTUPINFO32A startup;
449 PROCESS_INFORMATION info;
450 HINSTANCE16 hInstance, hPrevInstance;
456 if (!paramBlock || (paramBlock == (LPVOID)-1))
457 return LoadLibrary16( name );
459 hInstance = NE_LoadModule( name, &hPrevInstance, FALSE, FALSE );
460 if ( hInstance < 32 || !(pModule = NE_GetPtr(hInstance))
461 || (pModule->flags & NE_FFLAGS_LIBMODULE))
464 /* Create a task for this instance */
466 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
468 params = (LOADPARAMS *)paramBlock;
469 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
470 if (!cmd_line) cmd_line = "";
471 else if (*cmd_line) cmd_line++; /* skip the length byte */
473 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
474 strlen(cmd_line)+strlen(name)+2 )))
476 strcpy( new_cmd_line, name );
477 strcat( new_cmd_line, " " );
478 strcat( new_cmd_line, cmd_line );
480 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
482 memset( &info, '\0', sizeof(info) );
483 memset( &startup, '\0', sizeof(startup) );
484 startup.cb = sizeof(startup);
487 startup.dwFlags = STARTF_USESHOWWINDOW;
488 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
491 pdb = PROCESS_Create( pModule, new_cmd_line, env,
492 hInstance, hPrevInstance, TRUE, &startup, &info );
494 CloseHandle( info.hThread );
495 CloseHandle( info.hProcess );
497 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
498 HeapFree( GetProcessHeap(), 0, new_cmd_line );
502 if (pdb) TASK_StartTask( pdb->task );
507 /**********************************************************************
508 * LoadModule32 (KERNEL32.499)
510 HINSTANCE32 WINAPI LoadModule32( LPCSTR name, LPVOID paramBlock )
512 LOADPARAMS32 *params = (LOADPARAMS32 *)paramBlock;
513 PROCESS_INFORMATION info;
514 STARTUPINFO32A startup;
515 HINSTANCE32 hInstance;
519 memset( &startup, '\0', sizeof(startup) );
520 startup.cb = sizeof(startup);
521 startup.dwFlags = STARTF_USESHOWWINDOW;
522 startup.wShowWindow = params->lpCmdShow? params->lpCmdShow[1] : 0;
524 if (!CreateProcess32A( name, params->lpCmdLine,
525 NULL, NULL, FALSE, 0, params->lpEnvAddress,
526 NULL, &startup, &info ))
527 return GetLastError(); /* guaranteed to be < 32 */
529 /* Get 16-bit hInstance/hTask from process */
530 pdb = PROCESS_IdToPDB( info.dwProcessId );
531 tdb = pdb? (TDB *)GlobalLock16( pdb->task ) : NULL;
532 hInstance = tdb && tdb->hInstance? tdb->hInstance : pdb? pdb->task : 0;
534 /* Close off the handles */
535 CloseHandle( info.hThread );
536 CloseHandle( info.hProcess );
541 /**********************************************************************
542 * CreateProcess32A (KERNEL32.171)
544 BOOL32 WINAPI CreateProcess32A( LPCSTR lpApplicationName, LPSTR lpCommandLine,
545 LPSECURITY_ATTRIBUTES lpProcessAttributes,
546 LPSECURITY_ATTRIBUTES lpThreadAttributes,
547 BOOL32 bInheritHandles, DWORD dwCreationFlags,
548 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
549 LPSTARTUPINFO32A lpStartupInfo,
550 LPPROCESS_INFORMATION lpProcessInfo )
552 HINSTANCE16 hInstance;
557 /* Get name and command line */
559 if (!lpApplicationName && !lpCommandLine)
561 SetLastError( ERROR_FILE_NOT_FOUND );
565 cmdline = lpCommandLine? lpCommandLine : lpApplicationName;
567 if (lpApplicationName)
568 lstrcpyn32A(name, lpApplicationName, sizeof(name) - 4);
573 /* Take care off .exes with spaces in their names */
574 ptr = strchr(lpCommandLine, ' ');
576 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
577 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
578 lstrcpyn32A(name, lpCommandLine, len);
579 if (!strchr(name, '\\') && !strchr(name, '.'))
580 strcat(name, ".exe");
581 if (GetFileAttributes32A(name)!=-1)
583 /* if there is a space and no file found yet, include the word
584 * up to the next space too. If there is no next space, just
585 * use the first word.
588 ptr = strchr(ptr+1, ' ');
590 ptr = strchr(lpCommandLine, ' ');
591 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
592 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
593 lstrcpyn32A(name, lpCommandLine, len);
599 if (!strchr(name, '\\') && !strchr(name, '.'))
600 strcat(name, ".exe");
603 /* Warn if unsupported features are used */
605 if (lpProcessAttributes)
606 FIXME(module, "(%s,...): lpProcessAttributes ignored\n", name);
607 if (lpThreadAttributes)
608 FIXME(module, "(%s,...): lpThreadAttributes ignored\n", name);
609 if (dwCreationFlags & DEBUG_PROCESS)
610 FIXME(module, "(%s,...): DEBUG_PROCESS ignored\n", name);
611 if (dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
612 FIXME(module, "(%s,...): DEBUG_ONLY_THIS_PROCESS ignored\n", name);
613 if (dwCreationFlags & CREATE_SUSPENDED)
614 FIXME(module, "(%s,...): CREATE_SUSPENDED ignored\n", name);
615 if (dwCreationFlags & DETACHED_PROCESS)
616 FIXME(module, "(%s,...): DETACHED_PROCESS ignored\n", name);
617 if (dwCreationFlags & CREATE_NEW_CONSOLE)
618 FIXME(module, "(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
619 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
620 FIXME(module, "(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
621 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
622 FIXME(module, "(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
623 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
624 FIXME(module, "(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
625 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
626 FIXME(module, "(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
627 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
628 FIXME(module, "(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
629 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
630 FIXME(module, "(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
631 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
632 FIXME(module, "(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
633 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
634 FIXME(module, "(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
635 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
636 FIXME(module, "(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
637 if (dwCreationFlags & CREATE_NO_WINDOW)
638 FIXME(module, "(%s,...): CREATE_NO_WINDOW ignored\n", name);
639 if (dwCreationFlags & PROFILE_USER)
640 FIXME(module, "(%s,...): PROFILE_USER ignored\n", name);
641 if (dwCreationFlags & PROFILE_KERNEL)
642 FIXME(module, "(%s,...): PROFILE_KERNEL ignored\n", name);
643 if (dwCreationFlags & PROFILE_SERVER)
644 FIXME(module, "(%s,...): PROFILE_SERVER ignored\n", name);
645 if (lpCurrentDirectory)
646 FIXME(module, "(%s,...): lpCurrentDirectory %s ignored\n",
647 name, lpCurrentDirectory);
648 if (lpStartupInfo->lpDesktop)
649 FIXME(module, "(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
650 name, lpStartupInfo->lpDesktop);
651 if (lpStartupInfo->lpTitle)
652 FIXME(module, "(%s,...): lpStartupInfo->lpTitle %s ignored\n",
653 name, lpStartupInfo->lpTitle);
654 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
655 FIXME(module, "(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
656 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
657 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
658 FIXME(module, "(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
659 name, lpStartupInfo->dwFillAttribute);
660 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
661 FIXME(module, "(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
662 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
663 FIXME(module, "(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
664 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
665 FIXME(module, "(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
666 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
667 FIXME(module, "(%s,...): STARTF_USEHOTKEY ignored\n", name);
671 hInstance = NE_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
672 lpStartupInfo, lpProcessInfo );
676 hInstance = PE_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
677 lpStartupInfo, lpProcessInfo );
681 hInstance = MZ_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
682 lpStartupInfo, lpProcessInfo );
686 SetLastError( hInstance );
690 /* Get hTask from process and start the task */
691 pdb = PROCESS_IdToPDB( lpProcessInfo->dwProcessId );
692 if (pdb) TASK_StartTask( pdb->task );
697 /**********************************************************************
698 * CreateProcess32W (KERNEL32.172)
700 * lpReserved is not converted
702 BOOL32 WINAPI CreateProcess32W( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
703 LPSECURITY_ATTRIBUTES lpProcessAttributes,
704 LPSECURITY_ATTRIBUTES lpThreadAttributes,
705 BOOL32 bInheritHandles, DWORD dwCreationFlags,
706 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
707 LPSTARTUPINFO32W lpStartupInfo,
708 LPPROCESS_INFORMATION lpProcessInfo )
710 STARTUPINFO32A StartupInfoA;
712 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
713 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
714 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
716 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFO32A));
717 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
718 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
720 TRACE(win32, "(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
722 if (lpStartupInfo->lpReserved)
723 FIXME(win32,"StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
725 ret = CreateProcess32A( lpApplicationNameA, lpCommandLineA,
726 lpProcessAttributes, lpThreadAttributes,
727 bInheritHandles, dwCreationFlags,
728 lpEnvironment, lpCurrentDirectoryA,
729 &StartupInfoA, lpProcessInfo );
731 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
732 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
733 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
734 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
739 /***********************************************************************
740 * GetModuleHandle (KERNEL32.237)
742 HMODULE32 WINAPI GetModuleHandle32A(LPCSTR module)
745 return PROCESS_Current()->exe_modref->module;
747 return MODULE_FindModule32( module );
750 HMODULE32 WINAPI GetModuleHandle32W(LPCWSTR module)
753 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
754 hModule = GetModuleHandle32A( modulea );
755 HeapFree( GetProcessHeap(), 0, modulea );
760 /***********************************************************************
761 * GetModuleFileName32A (KERNEL32.235)
763 DWORD WINAPI GetModuleFileName32A(
764 HMODULE32 hModule, /* [in] module handle (32bit) */
765 LPSTR lpFileName, /* [out] filenamebuffer */
766 DWORD size /* [in] size of filenamebuffer */
768 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
770 if (!wm) /* can happen on start up or the like */
773 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
774 lstrcpyn32A( lpFileName, wm->longname, size );
776 lstrcpyn32A( lpFileName, wm->shortname, size );
778 TRACE(module, "%s\n", lpFileName );
779 return strlen(lpFileName);
783 /***********************************************************************
784 * GetModuleFileName32W (KERNEL32.236)
786 DWORD WINAPI GetModuleFileName32W( HMODULE32 hModule, LPWSTR lpFileName,
789 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
790 DWORD res = GetModuleFileName32A( hModule, fnA, size );
791 lstrcpynAtoW( lpFileName, fnA, size );
792 HeapFree( GetProcessHeap(), 0, fnA );
797 /***********************************************************************
798 * LoadLibraryEx32W (KERNEL.513)
801 HMODULE32 WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
804 TRACE(module,"(%s,%d,%08lx)\n",libname,hf,flags);
805 return LoadLibraryEx32A(libname, hf,flags);
808 /***********************************************************************
809 * LoadLibraryEx32A (KERNEL32)
811 HMODULE32 WINAPI LoadLibraryEx32A(LPCSTR libname,HFILE32 hfile,DWORD flags)
814 hmod = MODULE_LoadLibraryEx32A( libname, hfile, flags );
816 /* at least call not the dllmain...*/
817 if ( DONT_RESOLVE_DLL_REFERENCES==flags || LOAD_LIBRARY_AS_DATAFILE==flags )
818 { FIXME(module,"flag not properly supported %lx\n", flags);
822 /* initialize DLL just loaded */
824 MODULE_InitializeDLLs( hmod, DLL_PROCESS_ATTACH, (LPVOID)-1 );
829 HMODULE32 MODULE_LoadLibraryEx32A( LPCSTR libname, HFILE32 hfile, DWORD flags )
833 hmod = ELF_LoadLibraryEx32A( libname, hfile, flags );
834 if (hmod) return hmod;
836 hmod = PE_LoadLibraryEx32A( libname, hfile, flags );
840 /***********************************************************************
841 * LoadLibraryA (KERNEL32)
843 HMODULE32 WINAPI LoadLibrary32A(LPCSTR libname) {
844 return LoadLibraryEx32A(libname,0,0);
847 /***********************************************************************
848 * LoadLibraryW (KERNEL32)
850 HMODULE32 WINAPI LoadLibrary32W(LPCWSTR libnameW)
852 return LoadLibraryEx32W(libnameW,0,0);
855 /***********************************************************************
856 * LoadLibraryExW (KERNEL32)
858 HMODULE32 WINAPI LoadLibraryEx32W(LPCWSTR libnameW,HFILE32 hfile,DWORD flags)
860 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
861 HMODULE32 ret = LoadLibraryEx32A( libnameA , hfile, flags );
863 HeapFree( GetProcessHeap(), 0, libnameA );
867 /***********************************************************************
870 BOOL32 WINAPI FreeLibrary32(HINSTANCE32 hLibModule)
872 FIXME(module,"(0x%08x): stub\n", hLibModule);
873 return TRUE; /* FIXME */
877 /***********************************************************************
878 * PrivateLoadLibrary (KERNEL32)
880 * FIXME: rough guesswork, don't know what "Private" means
882 HINSTANCE32 WINAPI PrivateLoadLibrary(LPCSTR libname)
884 return (HINSTANCE32)LoadLibrary16(libname);
889 /***********************************************************************
890 * PrivateFreeLibrary (KERNEL32)
892 * FIXME: rough guesswork, don't know what "Private" means
894 void WINAPI PrivateFreeLibrary(HINSTANCE32 handle)
896 FreeLibrary16((HINSTANCE16)handle);
900 /***********************************************************************
901 * WinExec16 (KERNEL.166)
903 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
905 return WinExec32( lpCmdLine, nCmdShow );
909 /***********************************************************************
910 * WinExec32 (KERNEL32.566)
912 HINSTANCE32 WINAPI WinExec32( LPCSTR lpCmdLine, UINT32 nCmdShow )
914 HINSTANCE32 handle = 2;
915 char *p, filename[256];
916 int spacelimit = 0, exhausted = 0;
918 UINT16 paramCmdShow[2];
921 return 2; /* File not found */
923 /* Set up LOADPARAMS32 buffer for LoadModule32 */
925 memset( ¶ms, '\0', sizeof(params) );
926 params.lpCmdLine = (LPSTR)lpCmdLine;
927 params.lpCmdShow = paramCmdShow;
928 params.lpCmdShow[0] = 2;
929 params.lpCmdShow[1] = nCmdShow;
932 /* Keep trying to load a file by trying different filenames; e.g.,
933 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
934 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
937 while(!exhausted && handle == 2) {
940 /* Build the filename and command-line */
942 lstrcpyn32A(filename, lpCmdLine,
943 sizeof(filename) - 4 /* for extension */);
945 /* Keep grabbing characters until end-of-string, tab, or until the
946 number of spaces is greater than the spacelimit */
948 for (p = filename; ; p++) {
951 if(spacecount > spacelimit) {
957 if(*p == '\0' || *p == '\t') {
965 /* Now load the executable file */
969 handle = LoadModule32( filename, ¶ms );
970 if (handle == 2) /* file not found */
972 /* Check that the original file name did not have a suffix */
973 p = strrchr(filename, '.');
974 /* if there is a '.', check if either \ OR / follow */
975 if (!p || strchr(p, '/') || strchr(p, '\\'))
977 p = filename + strlen(filename);
979 handle = LoadModule32( filename, ¶ms );
980 *p = '\0'; /* Remove extension */
985 handle = 2; /* file not found */
989 /* Try to start it as a unix program */
993 DOS_FULL_NAME full_name;
994 const char *unixfilename = NULL;
995 const char *argv[256], **argptr;
996 int iconic = (nCmdShow == SW_SHOWMINIMIZED ||
997 nCmdShow == SW_SHOWMINNOACTIVE);
999 THREAD_InitDone = FALSE; /* we didn't init this process */
1000 /* get unixfilename */
1001 if (strchr(filename, '/') ||
1002 strchr(filename, ':') ||
1003 strchr(filename, '\\'))
1005 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
1006 unixfilename = full_name.long_name;
1008 else unixfilename = filename;
1014 if (iconic) *argptr++ = "-iconic";
1015 *argptr++ = unixfilename;
1016 p = strdup(lpCmdLine);
1019 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
1022 while (*p && *p != ' ' && *p != '\t') p++;
1027 execvp(argv[0], (char**)argv);
1037 if (iconic) *argptr++ = "-iconic";
1038 *argptr++ = lpCmdLine;
1042 execvp(argv[0] , (char**)argv);
1045 MSG("WinExec: can't exec 'wine %s'\n",
1051 } /* while (!exhausted && handle < 32) */
1057 /***********************************************************************
1058 * WIN32_GetProcAddress16 (KERNEL32.36)
1059 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1061 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hModule, LPCSTR name )
1067 WARN(module,"hModule may not be 0!\n");
1068 return (FARPROC16)0;
1070 if (HIWORD(hModule))
1072 WARN( module, "hModule is Win32 handle (%08x)\n", hModule );
1073 return (FARPROC16)0;
1075 hModule = GetExePtr( hModule );
1077 ordinal = NE_GetOrdinal( hModule, name );
1078 TRACE(module, "%04x '%s'\n",
1081 ordinal = LOWORD(name);
1082 TRACE(module, "%04x %04x\n",
1085 if (!ordinal) return (FARPROC16)0;
1086 ret = NE_GetEntryPoint( hModule, ordinal );
1087 TRACE(module,"returning %08x\n",(UINT32)ret);
1091 /***********************************************************************
1092 * GetProcAddress16 (KERNEL.50)
1094 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1099 if (!hModule) hModule = GetCurrentTask();
1100 hModule = GetExePtr( hModule );
1102 if (HIWORD(name) != 0)
1104 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1105 TRACE(module, "%04x '%s'\n",
1106 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1110 ordinal = LOWORD(name);
1111 TRACE(module, "%04x %04x\n",
1114 if (!ordinal) return (FARPROC16)0;
1116 ret = NE_GetEntryPoint( hModule, ordinal );
1118 TRACE(module, "returning %08x\n", (UINT32)ret );
1123 /***********************************************************************
1124 * GetProcAddress32 (KERNEL32.257)
1126 FARPROC32 WINAPI GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1128 return MODULE_GetProcAddress32( hModule, function, TRUE );
1131 /***********************************************************************
1132 * WIN16_GetProcAddress32 (KERNEL.453)
1134 FARPROC32 WINAPI WIN16_GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1136 return MODULE_GetProcAddress32( hModule, function, FALSE );
1139 /***********************************************************************
1140 * MODULE_GetProcAddress32 (internal)
1142 FARPROC32 MODULE_GetProcAddress32(
1143 HMODULE32 hModule, /* [in] current module handle */
1144 LPCSTR function, /* [in] function to be looked up */
1147 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1149 if (HIWORD(function))
1150 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
1152 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
1154 return (FARPROC32)0;
1158 return PE_FindExportedFunction( wm, function, snoop );
1160 return ELF_FindExportedFunction( wm, function);
1162 ERR(module,"wine_modref type %d not handled.\n",wm->type);
1163 return (FARPROC32)0;
1168 /***********************************************************************
1169 * RtlImageNtHeaders (NTDLL)
1171 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE32 hModule)
1174 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1175 * but we could get HMODULE16 or the like (think builtin modules)
1178 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1179 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1180 return PE_HEADER(wm->module);
1184 /***************************************************************************
1185 * HasGPHandler (KERNEL.338)
1189 typedef struct _GPHANDLERDEF
1198 SEGPTR WINAPI HasGPHandler( SEGPTR address )
1203 GPHANDLERDEF *gpHandler;
1205 if ( (hModule = FarGetOwner( SELECTOROF(address) )) != 0
1206 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1207 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1208 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1209 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1211 while (gpHandler->selector)
1213 if ( SELECTOROF(address) == gpHandler->selector
1214 && OFFSETOF(address) >= gpHandler->rangeStart
1215 && OFFSETOF(address) < gpHandler->rangeEnd )
1216 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1217 gpHandler->handler );