Implementation of CreateProcess32W.
[wine/multimedia.git] / loader / module.c
blob92ecdd76a5ebf71ed5e0b6953415db92f8a4c0f2
1 /*
2 * Modules
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include "windows.h"
14 #include "winerror.h"
15 #include "class.h"
16 #include "file.h"
17 #include "global.h"
18 #include "heap.h"
19 #include "module.h"
20 #include "neexe.h"
21 #include "pe_image.h"
22 #include "dosexe.h"
23 #include "process.h"
24 #include "thread.h"
25 #include "resource.h"
26 #include "selectors.h"
27 #include "stackframe.h"
28 #include "task.h"
29 #include "debug.h"
30 #include "callback.h"
32 extern BOOL32 THREAD_InitDone;
35 /*************************************************************************
36 * MODULE32_LookupHMODULE
37 * looks for the referenced HMODULE in the current process
39 WINE_MODREF*
40 MODULE32_LookupHMODULE(PDB32 *process,HMODULE32 hmod) {
41 WINE_MODREF *wm;
43 if (!hmod)
44 return process->exe_modref;
45 if (!HIWORD(hmod)) {
46 ERR(module,"tried to lookup 0x%04x in win32 module handler!\n",hmod);
47 return NULL;
49 for (wm = process->modref_list;wm;wm=wm->next)
50 if (wm->module == hmod)
51 return wm;
52 return NULL;
55 /*************************************************************************
56 * MODULE_InitializeDLLs
58 * Call the initialization routines of all DLLs belonging to the
59 * current process. This is somewhat complicated due to the fact that
61 * - we have to respect the module dependencies, i.e. modules implicitly
62 * referenced by another module have to be initialized before the module
63 * itself can be initialized
65 * - the initialization routine of a DLL can itself call LoadLibrary,
66 * thereby introducing a whole new set of dependencies (even involving
67 * the 'old' modules) at any time during the whole process
69 * (Note that this routine can be recursively entered not only directly
70 * from itself, but also via LoadLibrary from one of the called initialization
71 * routines.)
73 static void MODULE_DoInitializeDLLs( PDB32 *process, WINE_MODREF *wm,
74 DWORD type, LPVOID lpReserved )
76 int i;
78 assert( wm && !wm->initDone );
79 TRACE( module, "(%p,%08x,%ld,%p) - START\n",
80 process, wm->module, type, lpReserved );
82 /* Tag current MODREF to prevent recursive loop */
83 wm->initDone = TRUE;
85 /* Recursively initialize all child DLLs */
86 for ( i = 0; i < wm->nDeps; i++ )
87 if ( wm->deps[i] && !wm->deps[i]->initDone )
88 MODULE_DoInitializeDLLs( process,
89 wm->deps[i], type, lpReserved );
91 /* Now we can call the initialization routine */
92 switch ( wm->type )
94 case MODULE32_PE:
95 PE_InitDLL( wm, type, lpReserved );
96 break;
98 default:
99 ERR(module, "wine_modref type %d not handled.\n", wm->type);
100 break;
103 TRACE( module, "(%p,%08x,%ld,%p) - END\n",
104 process, wm->module, type, lpReserved );
107 void MODULE_InitializeDLLs( PDB32 *process, HMODULE32 root,
108 DWORD type, LPVOID lpReserved )
110 BOOL32 inProgress = FALSE;
111 WINE_MODREF *wm;
113 /* Grab the process critical section to protect the recursion flags */
114 /* FIXME: This is probably overkill! */
115 EnterCriticalSection( &process->crit_section );
117 TRACE( module, "(%p,%08x,%ld,%p) - START\n", process, root, type, lpReserved );
119 /* First, check whether initialization is currently in progress */
120 for ( wm = process->modref_list; wm; wm = wm->next )
121 if ( wm->initDone )
123 inProgress = TRUE;
124 break;
127 if ( inProgress )
130 * If this a LoadLibrary call from within an initialization routine,
131 * treat it analogously to an implicitly referenced DLL.
132 * Anything else may not happen at this point!
134 if ( root )
136 wm = MODULE32_LookupHMODULE( process, root );
137 if ( wm && !wm->initDone )
138 MODULE_DoInitializeDLLs( process, wm, type, lpReserved );
140 else
141 FIXME(module, "Invalid recursion!\n");
143 else
145 /* If we arrive here, this is the start of an initialization run */
146 if ( !root )
148 /* If called for main EXE, initialize all DLLs */
149 for ( wm = process->modref_list; wm; wm = wm->next )
150 if ( !wm->initDone )
151 MODULE_DoInitializeDLLs( process, wm, type, lpReserved );
153 else
155 /* If called for a specific DLL, initialize only it and its children */
156 wm = MODULE32_LookupHMODULE( process, root );
157 if (wm) MODULE_DoInitializeDLLs( process, wm, type, lpReserved );
160 /* We're finished, so we reset all recursion flags */
161 for ( wm = process->modref_list; wm; wm = wm->next )
162 wm->initDone = FALSE;
165 TRACE( module, "(%p,%08x,%ld,%p) - END\n", process, root, type, lpReserved );
167 /* Release critical section */
168 LeaveCriticalSection( &process->crit_section );
172 /***********************************************************************
173 * MODULE_CreateDummyModule
175 * Create a dummy NE module for Win32 or Winelib.
177 HMODULE32 MODULE_CreateDummyModule( const OFSTRUCT *ofs )
179 HMODULE32 hModule;
180 NE_MODULE *pModule;
181 SEGTABLEENTRY *pSegment;
182 char *pStr,*s;
183 int len;
184 const char* basename;
186 INT32 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
187 + strlen(ofs->szPathName) + 1;
188 INT32 size = sizeof(NE_MODULE) +
189 /* loaded file info */
190 of_size +
191 /* segment table: DS,CS */
192 2 * sizeof(SEGTABLEENTRY) +
193 /* name table */
195 /* several empty tables */
198 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
199 if (!hModule) return (HMODULE32)11; /* invalid exe */
201 FarSetOwner( hModule, hModule );
202 pModule = (NE_MODULE *)GlobalLock16( hModule );
204 /* Set all used entries */
205 pModule->magic = IMAGE_OS2_SIGNATURE;
206 pModule->count = 1;
207 pModule->next = 0;
208 pModule->flags = 0;
209 pModule->dgroup = 1;
210 pModule->ss = 1;
211 pModule->cs = 2;
212 pModule->heap_size = 0xe000;
213 pModule->stack_size = 0x1000;
214 pModule->seg_count = 2;
215 pModule->modref_count = 0;
216 pModule->nrname_size = 0;
217 pModule->fileinfo = sizeof(NE_MODULE);
218 pModule->os_flags = NE_OSFLAGS_WINDOWS;
219 pModule->expected_version = 0x030a;
220 pModule->self = hModule;
222 /* Set loaded file information */
223 memcpy( pModule + 1, ofs, of_size );
224 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
226 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
227 pModule->seg_table = pModule->dgroup_entry = (int)pSegment - (int)pModule;
228 /* Data segment */
229 pSegment->size = 0;
230 pSegment->flags = NE_SEGFLAGS_DATA;
231 pSegment->minsize = 0x1000;
232 pSegment++;
233 /* Code segment */
234 pSegment->flags = 0;
235 pSegment++;
237 /* Module name */
238 pStr = (char *)pSegment;
239 pModule->name_table = (int)pStr - (int)pModule;
240 basename = strrchr(ofs->szPathName,'\\');
241 if (!basename) basename = ofs->szPathName;
242 else basename++;
243 len = strlen(basename);
244 if ((s = strchr(basename,'.'))) len = s - basename;
245 if (len > 8) len = 8;
246 *pStr = len;
247 strncpy( pStr+1, basename, len );
248 if (len < 8) pStr[len+1] = 0;
249 pStr += 9;
251 /* All tables zero terminated */
252 pModule->res_table = pModule->import_table = pModule->entry_table =
253 (int)pStr - (int)pModule;
255 NE_RegisterModule( pModule );
256 return hModule;
260 /***********************************************************************
261 * MODULE_GetWndProcEntry16 (not a Windows API function)
263 * Return an entry point from the WPROCS dll.
265 FARPROC16 MODULE_GetWndProcEntry16( LPCSTR name )
267 FARPROC16 ret = NULL;
269 if (__winelib)
271 /* FIXME: hack for Winelib */
272 extern LRESULT ColorDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
273 extern LRESULT FileOpenDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
274 extern LRESULT FileSaveDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
275 extern LRESULT FindTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
276 extern LRESULT PrintDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
277 extern LRESULT PrintSetupDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
278 extern LRESULT ReplaceTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
280 if (!strcmp(name,"ColorDlgProc"))
281 return (FARPROC16)ColorDlgProc;
282 if (!strcmp(name,"FileOpenDlgProc"))
283 return (FARPROC16)FileOpenDlgProc;
284 if (!strcmp(name,"FileSaveDlgProc"))
285 return (FARPROC16)FileSaveDlgProc;
286 if (!strcmp(name,"FindTextDlgProc"))
287 return (FARPROC16)FindTextDlgProc16;
288 if (!strcmp(name,"PrintDlgProc"))
289 return (FARPROC16)PrintDlgProc;
290 if (!strcmp(name,"PrintSetupDlgProc"))
291 return (FARPROC16)PrintSetupDlgProc;
292 if (!strcmp(name,"ReplaceTextDlgProc"))
293 return (FARPROC16)ReplaceTextDlgProc16;
294 if (!strcmp(name,"DefResourceHandler"))
295 return (FARPROC16)NE_DefResourceHandler;
296 if (!strcmp(name,"LoadDIBIconHandler"))
297 return (FARPROC16)LoadDIBIconHandler;
298 if (!strcmp(name,"LoadDIBCursorHandler"))
299 return (FARPROC16)LoadDIBCursorHandler;
300 FIXME(module,"No mapping for %s(), add one in library/miscstubs.c\n",name);
301 assert( FALSE );
302 return NULL;
304 else
306 WORD ordinal;
307 static HMODULE32 hModule = 0;
309 if (!hModule) hModule = GetModuleHandle16( "WPROCS" );
310 ordinal = NE_GetOrdinal( hModule, name );
311 if (!(ret = NE_GetEntryPoint( hModule, ordinal )))
313 WARN( module, "%s not found\n", name );
314 assert( FALSE );
317 return ret;
321 /**********************************************************************
322 * MODULE_FindModule32
324 * Find a (loaded) win32 module depending on path
325 * The handling of '.' is a bit weird, but we need it that way,
326 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
327 * this is the only way to differentiate. (mainly hypertrm.exe)
329 * RETURNS
330 * the module handle if found
331 * 0 if not
333 HMODULE32 MODULE_FindModule32(
334 PDB32* process, /* [in] process in which to find the library */
335 LPCSTR path /* [in] pathname of module/library to be found */
337 LPSTR filename;
338 LPSTR dotptr;
339 WINE_MODREF *wm;
341 if (!process)
342 return 0;
343 if (!(filename = strrchr( path, '\\' )))
344 filename = HEAP_strdupA(process->heap,0,path);
345 else
346 filename = HEAP_strdupA(process->heap,0,filename+1);
347 dotptr=strrchr(filename,'.');
349 for (wm=process->modref_list;wm;wm=wm->next) {
350 LPSTR xmodname,xdotptr;
352 assert (wm->modname);
353 xmodname = HEAP_strdupA(process->heap,0,wm->modname);
354 xdotptr=strrchr(xmodname,'.');
355 if ( (xdotptr && !dotptr) ||
356 (!xdotptr && dotptr)
358 if (dotptr) *dotptr = '\0';
359 if (xdotptr) *xdotptr = '\0';
361 if (!strcasecmp( filename, xmodname)) {
362 HeapFree(process->heap,0,filename);
363 HeapFree(process->heap,0,xmodname);
364 return wm->module;
366 if (dotptr) *dotptr='.';
367 /* FIXME: add paths, shortname */
368 HeapFree(process->heap,0,xmodname);
370 /* if that fails, try looking for the filename... */
371 for (wm=process->modref_list;wm;wm=wm->next) {
372 LPSTR xlname,xdotptr;
374 assert (wm->longname);
375 xlname = strrchr(wm->longname,'\\');
376 if (!xlname)
377 xlname = wm->longname;
378 else
379 xlname++;
380 xlname = HEAP_strdupA(process->heap,0,xlname);
381 xdotptr=strrchr(xlname,'.');
382 if ( (xdotptr && !dotptr) ||
383 (!xdotptr && dotptr)
385 if (dotptr) *dotptr = '\0';
386 if (xdotptr) *xdotptr = '\0';
388 if (!strcasecmp( filename, xlname)) {
389 HeapFree(process->heap,0,filename);
390 HeapFree(process->heap,0,xlname);
391 return wm->module;
393 if (dotptr) *dotptr='.';
394 /* FIXME: add paths, shortname */
395 HeapFree(process->heap,0,xlname);
397 HeapFree(process->heap,0,filename);
398 return 0;
403 /**********************************************************************
404 * NE_CreateProcess
406 static HINSTANCE16 NE_CreateProcess( LPCSTR name, LPCSTR cmd_line, LPCSTR env,
407 LPSTARTUPINFO32A startup,
408 LPPROCESS_INFORMATION info )
410 HINSTANCE16 hInstance, hPrevInstance;
411 NE_MODULE *pModule;
413 /* Load module */
415 hInstance = NE_LoadModule( name, &hPrevInstance, TRUE, FALSE );
416 if (hInstance < 32) return hInstance;
418 if ( !(pModule = NE_GetPtr(hInstance))
419 || (pModule->flags & NE_FFLAGS_LIBMODULE))
421 /* FIXME: cleanup */
422 return 11;
425 /* Create a task for this instance */
427 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
429 PROCESS_Create( pModule, cmd_line, env, hInstance,
430 hPrevInstance, startup, info );
432 return hInstance;
436 /**********************************************************************
437 * LoadModule16 (KERNEL.45)
439 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
441 LOADPARAMS *params;
442 LPSTR cmd_line, new_cmd_line;
443 LPCVOID env = NULL;
444 STARTUPINFO32A startup;
445 PROCESS_INFORMATION info;
446 HINSTANCE16 hInstance, hPrevInstance;
447 NE_MODULE *pModule;
448 PDB32 *pdb;
450 /* Load module */
452 if (!paramBlock || (paramBlock == (LPVOID)-1))
453 return LoadLibrary16( name );
455 hInstance = NE_LoadModule( name, &hPrevInstance, FALSE, FALSE );
456 if ( hInstance < 32 || !(pModule = NE_GetPtr(hInstance))
457 || (pModule->flags & NE_FFLAGS_LIBMODULE))
458 return hInstance;
460 /* Create a task for this instance */
462 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
464 params = (LOADPARAMS *)paramBlock;
465 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
466 if (!cmd_line) cmd_line = "";
467 else if (*cmd_line) cmd_line++; /* skip the length byte */
469 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
470 strlen(cmd_line)+strlen(name)+2 )))
471 return 0;
472 strcpy( new_cmd_line, name );
473 strcat( new_cmd_line, " " );
474 strcat( new_cmd_line, cmd_line );
476 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
478 memset( &info, '\0', sizeof(info) );
479 memset( &startup, '\0', sizeof(startup) );
480 startup.cb = sizeof(startup);
481 if (params->showCmd)
483 startup.dwFlags = STARTF_USESHOWWINDOW;
484 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
487 pdb = PROCESS_Create( pModule, new_cmd_line, env,
488 hInstance, hPrevInstance, &startup, &info );
490 CloseHandle( info.hThread );
491 CloseHandle( info.hProcess );
493 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
494 HeapFree( GetProcessHeap(), 0, new_cmd_line );
496 /* Start task */
498 if (pdb) TASK_StartTask( pdb->task );
500 return hInstance;
503 /**********************************************************************
504 * LoadModule32 (KERNEL32.499)
506 HINSTANCE32 WINAPI LoadModule32( LPCSTR name, LPVOID paramBlock )
508 LOADPARAMS32 *params = (LOADPARAMS32 *)paramBlock;
509 PROCESS_INFORMATION info;
510 STARTUPINFO32A startup;
511 HINSTANCE32 hInstance;
512 PDB32 *pdb;
513 TDB *tdb;
515 memset( &startup, '\0', sizeof(startup) );
516 startup.cb = sizeof(startup);
517 startup.dwFlags = STARTF_USESHOWWINDOW;
518 startup.wShowWindow = params->lpCmdShow? params->lpCmdShow[1] : 0;
520 if (!CreateProcess32A( name, params->lpCmdLine,
521 NULL, NULL, FALSE, 0, params->lpEnvAddress,
522 NULL, &startup, &info ))
523 return GetLastError(); /* guaranteed to be < 32 */
525 /* Get hInstance from process */
526 pdb = PROCESS_IdToPDB( info.dwProcessId );
527 if ( pdb->exe_modref )
528 hInstance = pdb->exe_modref->module;
529 else
531 tdb = pdb? (TDB *)GlobalLock16( pdb->task ) : NULL;
532 hInstance = tdb? tdb->hInstance : 0;
535 /* Close off the handles */
536 CloseHandle( info.hThread );
537 CloseHandle( info.hProcess );
539 return hInstance;
542 /**********************************************************************
543 * CreateProcess32A (KERNEL32.171)
545 BOOL32 WINAPI CreateProcess32A( LPCSTR lpApplicationName, LPSTR lpCommandLine,
546 LPSECURITY_ATTRIBUTES lpProcessAttributes,
547 LPSECURITY_ATTRIBUTES lpThreadAttributes,
548 BOOL32 bInheritHandles, DWORD dwCreationFlags,
549 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
550 LPSTARTUPINFO32A lpStartupInfo,
551 LPPROCESS_INFORMATION lpProcessInfo )
553 HINSTANCE16 hInstance;
554 LPCSTR cmdline;
555 PDB32 *pdb;
556 char name[256];
558 /* Get name and command line */
560 if (!lpApplicationName && !lpCommandLine)
562 SetLastError( ERROR_FILE_NOT_FOUND );
563 return FALSE;
566 cmdline = lpCommandLine? lpCommandLine : lpApplicationName;
568 if (lpApplicationName)
569 lstrcpyn32A(name, lpApplicationName, sizeof(name) - 4);
570 else {
571 char *ptr;
572 int len;
574 /* Take care off .exes with spaces in their names */
575 ptr = strchr(lpCommandLine, ' ');
576 do {
577 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
578 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
579 lstrcpyn32A(name, lpCommandLine, len);
580 if (!strchr(name, '\\') && !strchr(name, '.'))
581 strcat(name, ".exe");
582 fprintf(stderr,"looking for: %s\n",name);
583 if (GetFileAttributes32A(name)!=-1)
584 break;
585 /* if there is a space and no file found yet, include the word
586 * up to the next space too. If there is no next space, just
587 * use the first word.
589 if (ptr) {
590 ptr = strchr(ptr+1, ' ');
591 } else {
592 ptr = strchr(lpCommandLine, ' ');
593 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
594 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
595 lstrcpyn32A(name, lpCommandLine, len);
596 break;
598 } while (1);
601 if (!strchr(name, '\\') && !strchr(name, '.'))
602 strcat(name, ".exe");
605 /* Warn if unsupported features are used */
607 if (lpProcessAttributes)
608 FIXME(module, "(%s,...): lpProcessAttributes ignored\n", name);
609 if (lpThreadAttributes)
610 FIXME(module, "(%s,...): lpThreadAttributes ignored\n", name);
611 if (bInheritHandles)
612 FIXME(module, "(%s,...): bInheritHandles ignored\n", name);
613 if (dwCreationFlags & DEBUG_PROCESS)
614 FIXME(module, "(%s,...): DEBUG_PROCESS ignored\n", name);
615 if (dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
616 FIXME(module, "(%s,...): DEBUG_ONLY_THIS_PROCESS ignored\n", name);
617 if (dwCreationFlags & CREATE_SUSPENDED)
618 FIXME(module, "(%s,...): CREATE_SUSPENDED ignored\n", name);
619 if (dwCreationFlags & DETACHED_PROCESS)
620 FIXME(module, "(%s,...): DETACHED_PROCESS ignored\n", name);
621 if (dwCreationFlags & CREATE_NEW_CONSOLE)
622 FIXME(module, "(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
623 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
624 FIXME(module, "(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
625 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
626 FIXME(module, "(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
627 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
628 FIXME(module, "(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
629 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
630 FIXME(module, "(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
631 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
632 FIXME(module, "(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
633 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
634 FIXME(module, "(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
635 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
636 FIXME(module, "(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
637 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
638 FIXME(module, "(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
639 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
640 FIXME(module, "(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
641 if (dwCreationFlags & CREATE_NO_WINDOW)
642 FIXME(module, "(%s,...): CREATE_NO_WINDOW ignored\n", name);
643 if (dwCreationFlags & PROFILE_USER)
644 FIXME(module, "(%s,...): PROFILE_USER ignored\n", name);
645 if (dwCreationFlags & PROFILE_KERNEL)
646 FIXME(module, "(%s,...): PROFILE_KERNEL ignored\n", name);
647 if (dwCreationFlags & PROFILE_SERVER)
648 FIXME(module, "(%s,...): PROFILE_SERVER ignored\n", name);
649 if (lpCurrentDirectory)
650 FIXME(module, "(%s,...): lpCurrentDirectory %s ignored\n",
651 name, lpCurrentDirectory);
652 if (lpStartupInfo->lpDesktop)
653 FIXME(module, "(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
654 name, lpStartupInfo->lpDesktop);
655 if (lpStartupInfo->lpTitle)
656 FIXME(module, "(%s,...): lpStartupInfo->lpTitle %s ignored\n",
657 name, lpStartupInfo->lpTitle);
658 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
659 FIXME(module, "(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
660 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
661 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
662 FIXME(module, "(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
663 name, lpStartupInfo->dwFillAttribute);
664 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
665 FIXME(module, "(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
666 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
667 FIXME(module, "(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
668 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
669 FIXME(module, "(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
670 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
671 FIXME(module, "(%s,...): STARTF_USEHOTKEY ignored\n", name);
674 /* Try NE module */
675 hInstance = NE_CreateProcess( name, cmdline, lpEnvironment,
676 lpStartupInfo, lpProcessInfo );
678 /* Try PE module */
679 if (hInstance == 21)
680 hInstance = PE_CreateProcess( name, cmdline, lpEnvironment,
681 lpStartupInfo, lpProcessInfo );
683 /* Try DOS module */
684 if (hInstance == 11)
685 hInstance = MZ_CreateProcess( name, cmdline, lpEnvironment,
686 lpStartupInfo, lpProcessInfo );
688 if (hInstance < 32)
690 SetLastError( hInstance );
691 return FALSE;
694 /* Get hTask from process and start the task */
695 pdb = PROCESS_IdToPDB( lpProcessInfo->dwProcessId );
696 if (pdb) TASK_StartTask( pdb->task );
698 return TRUE;
701 /**********************************************************************
702 * CreateProcess32W (KERNEL32.172)
703 * NOTES
704 * lpReserved is not converted
706 BOOL32 WINAPI CreateProcess32W( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
707 LPSECURITY_ATTRIBUTES lpProcessAttributes,
708 LPSECURITY_ATTRIBUTES lpThreadAttributes,
709 BOOL32 bInheritHandles, DWORD dwCreationFlags,
710 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
711 LPSTARTUPINFO32W lpStartupInfo,
712 LPPROCESS_INFORMATION lpProcessInfo )
713 { BOOL32 ret;
714 STARTUPINFO32A StartupInfoA;
716 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
717 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
718 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
720 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFO32A));
721 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
722 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
724 TRACE(win32, "(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
726 if (lpStartupInfo->lpReserved)
727 FIXME(win32,"StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
729 ret = CreateProcess32A( lpApplicationNameA, lpCommandLineA,
730 lpProcessAttributes, lpThreadAttributes,
731 bInheritHandles, dwCreationFlags,
732 lpEnvironment, lpCurrentDirectoryA,
733 &StartupInfoA, lpProcessInfo );
735 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
736 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
737 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
738 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
740 return ret;
743 /***********************************************************************
744 * GetModuleHandle (KERNEL32.237)
746 HMODULE32 WINAPI GetModuleHandle32A(LPCSTR module)
748 if (module == NULL)
749 return PROCESS_Current()->exe_modref->module;
750 else
751 return MODULE_FindModule32(PROCESS_Current(),module);
754 HMODULE32 WINAPI GetModuleHandle32W(LPCWSTR module)
756 HMODULE32 hModule;
757 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
758 hModule = GetModuleHandle32A( modulea );
759 HeapFree( GetProcessHeap(), 0, modulea );
760 return hModule;
764 /***********************************************************************
765 * GetModuleFileName32A (KERNEL32.235)
767 DWORD WINAPI GetModuleFileName32A(
768 HMODULE32 hModule, /* [in] module handle (32bit) */
769 LPSTR lpFileName, /* [out] filenamebuffer */
770 DWORD size /* [in] size of filenamebuffer */
771 ) {
772 WINE_MODREF *wm = MODULE32_LookupHMODULE(PROCESS_Current(),hModule);
774 if (!wm) /* can happen on start up or the like */
775 return 0;
777 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
778 lstrcpyn32A( lpFileName, wm->longname, size );
779 else
780 lstrcpyn32A( lpFileName, wm->shortname, size );
782 TRACE(module, "%s\n", lpFileName );
783 return strlen(lpFileName);
787 /***********************************************************************
788 * GetModuleFileName32W (KERNEL32.236)
790 DWORD WINAPI GetModuleFileName32W( HMODULE32 hModule, LPWSTR lpFileName,
791 DWORD size )
793 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
794 DWORD res = GetModuleFileName32A( hModule, fnA, size );
795 lstrcpynAtoW( lpFileName, fnA, size );
796 HeapFree( GetProcessHeap(), 0, fnA );
797 return res;
801 /***********************************************************************
802 * LoadLibraryEx32W (KERNEL.513)
803 * FIXME
805 HMODULE32 WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
806 DWORD flags )
808 TRACE(module,"(%s,%d,%08lx)\n",libname,hf,flags);
809 return LoadLibraryEx32A(libname, hf,flags);
812 /***********************************************************************
813 * LoadLibraryEx32A (KERNEL32)
815 HMODULE32 WINAPI LoadLibraryEx32A(LPCSTR libname,HFILE32 hfile,DWORD flags)
817 HMODULE32 hmod;
818 hmod = MODULE_LoadLibraryEx32A(libname,PROCESS_Current(),hfile,flags);
820 /* at least call not the dllmain...*/
821 if ( DONT_RESOLVE_DLL_REFERENCES==flags || LOAD_LIBRARY_AS_DATAFILE==flags )
822 { FIXME(module,"flag not properly supported %lx\n", flags);
823 return hmod;
826 /* initialize DLL just loaded */
827 if ( hmod >= 32 )
828 MODULE_InitializeDLLs( PROCESS_Current(), hmod,
829 DLL_PROCESS_ATTACH, (LPVOID)-1 );
831 return hmod;
834 HMODULE32 MODULE_LoadLibraryEx32A(LPCSTR libname,PDB32*process,HFILE32 hfile,DWORD flags)
836 HMODULE32 hmod;
838 hmod = ELF_LoadLibraryEx32A(libname,process,hfile,flags);
839 if (hmod)
840 return hmod; /* already initialized for ELF */
842 hmod = PE_LoadLibraryEx32A(libname,process,hfile,flags);
843 if (hmod < 32) {
844 char buffer[256];
846 strcpy( buffer, libname );
847 strcat( buffer, ".dll" );
848 hmod = PE_LoadLibraryEx32A(buffer,process,hfile,flags);
850 return hmod;
853 /***********************************************************************
854 * LoadLibraryA (KERNEL32)
856 HMODULE32 WINAPI LoadLibrary32A(LPCSTR libname) {
857 return LoadLibraryEx32A(libname,0,0);
860 /***********************************************************************
861 * LoadLibraryW (KERNEL32)
863 HMODULE32 WINAPI LoadLibrary32W(LPCWSTR libnameW)
865 return LoadLibraryEx32W(libnameW,0,0);
868 /***********************************************************************
869 * LoadLibraryExW (KERNEL32)
871 HMODULE32 WINAPI LoadLibraryEx32W(LPCWSTR libnameW,HFILE32 hfile,DWORD flags)
873 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
874 HMODULE32 ret = LoadLibraryEx32A( libnameA , hfile, flags );
876 HeapFree( GetProcessHeap(), 0, libnameA );
877 return ret;
880 /***********************************************************************
881 * FreeLibrary
883 BOOL32 WINAPI FreeLibrary32(HINSTANCE32 hLibModule)
885 FIXME(module,"(0x%08x): stub\n", hLibModule);
886 return TRUE; /* FIXME */
890 /***********************************************************************
891 * PrivateLoadLibrary (KERNEL32)
893 * FIXME: rough guesswork, don't know what "Private" means
895 HINSTANCE32 WINAPI PrivateLoadLibrary(LPCSTR libname)
897 return (HINSTANCE32)LoadLibrary16(libname);
902 /***********************************************************************
903 * PrivateFreeLibrary (KERNEL32)
905 * FIXME: rough guesswork, don't know what "Private" means
907 void WINAPI PrivateFreeLibrary(HINSTANCE32 handle)
909 FreeLibrary16((HINSTANCE16)handle);
913 /***********************************************************************
914 * WinExec16 (KERNEL.166)
916 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
918 return WinExec32( lpCmdLine, nCmdShow );
922 /***********************************************************************
923 * WinExec32 (KERNEL32.566)
925 HINSTANCE32 WINAPI WinExec32( LPCSTR lpCmdLine, UINT32 nCmdShow )
927 HINSTANCE32 handle = 2;
928 char *p, filename[256];
929 int spacelimit = 0, exhausted = 0;
930 LOADPARAMS32 params;
931 UINT16 paramCmdShow[2];
933 if (!lpCmdLine)
934 return 2; /* File not found */
936 /* Set up LOADPARAMS32 buffer for LoadModule32 */
938 memset( &params, '\0', sizeof(params) );
939 params.lpCmdLine = (LPSTR)lpCmdLine;
940 params.lpCmdShow = paramCmdShow;
941 params.lpCmdShow[0] = 2;
942 params.lpCmdShow[1] = nCmdShow;
945 /* Keep trying to load a file by trying different filenames; e.g.,
946 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
947 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
948 no args */
950 while(!exhausted && handle == 2) {
951 int spacecount = 0;
953 /* Build the filename and command-line */
955 lstrcpyn32A(filename, lpCmdLine,
956 sizeof(filename) - 4 /* for extension */);
958 /* Keep grabbing characters until end-of-string, tab, or until the
959 number of spaces is greater than the spacelimit */
961 for (p = filename; ; p++) {
962 if(*p == ' ') {
963 ++spacecount;
964 if(spacecount > spacelimit) {
965 ++spacelimit;
966 break;
970 if(*p == '\0' || *p == '\t') {
971 exhausted = 1;
972 break;
976 *p = '\0';
978 /* Now load the executable file */
980 if (!__winelib)
982 handle = LoadModule32( filename, &params );
983 if (handle == 2) /* file not found */
985 /* Check that the original file name did not have a suffix */
986 p = strrchr(filename, '.');
987 /* if there is a '.', check if either \ OR / follow */
988 if (!p || strchr(p, '/') || strchr(p, '\\'))
990 p = filename + strlen(filename);
991 strcpy( p, ".exe" );
992 handle = LoadModule32( filename, &params );
993 *p = '\0'; /* Remove extension */
997 else
998 handle = 2; /* file not found */
1000 if (handle < 32)
1002 /* Try to start it as a unix program */
1003 if (!fork())
1005 /* Child process */
1006 DOS_FULL_NAME full_name;
1007 const char *unixfilename = NULL;
1008 const char *argv[256], **argptr;
1009 int iconic = (nCmdShow == SW_SHOWMINIMIZED ||
1010 nCmdShow == SW_SHOWMINNOACTIVE);
1012 THREAD_InitDone = FALSE; /* we didn't init this process */
1013 /* get unixfilename */
1014 if (strchr(filename, '/') ||
1015 strchr(filename, ':') ||
1016 strchr(filename, '\\'))
1018 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
1019 unixfilename = full_name.long_name;
1021 else unixfilename = filename;
1023 if (unixfilename)
1025 /* build argv */
1026 argptr = argv;
1027 if (iconic) *argptr++ = "-iconic";
1028 *argptr++ = unixfilename;
1029 p = strdup(lpCmdLine);
1030 while (1)
1032 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
1033 if (!*p) break;
1034 *argptr++ = p;
1035 while (*p && *p != ' ' && *p != '\t') p++;
1037 *argptr++ = 0;
1039 /* Execute */
1040 execvp(argv[0], (char**)argv);
1043 /* Failed ! */
1045 if (__winelib)
1047 /* build argv */
1048 argptr = argv;
1049 *argptr++ = "wine";
1050 if (iconic) *argptr++ = "-iconic";
1051 *argptr++ = lpCmdLine;
1052 *argptr++ = 0;
1054 /* Execute */
1055 execvp(argv[0] , (char**)argv);
1057 /* Failed ! */
1058 MSG("WinExec: can't exec 'wine %s'\n",
1059 lpCmdLine);
1061 exit(1);
1064 } /* while (!exhausted && handle < 32) */
1066 return handle;
1070 /***********************************************************************
1071 * WIN32_GetProcAddress16 (KERNEL32.36)
1072 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1074 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hModule, LPCSTR name )
1076 WORD ordinal;
1077 FARPROC16 ret;
1079 if (!hModule) {
1080 WARN(module,"hModule may not be 0!\n");
1081 return (FARPROC16)0;
1083 if (HIWORD(hModule))
1085 WARN( module, "hModule is Win32 handle (%08x)\n", hModule );
1086 return (FARPROC16)0;
1088 hModule = GetExePtr( hModule );
1089 if (HIWORD(name)) {
1090 ordinal = NE_GetOrdinal( hModule, name );
1091 TRACE(module, "%04x '%s'\n",
1092 hModule, name );
1093 } else {
1094 ordinal = LOWORD(name);
1095 TRACE(module, "%04x %04x\n",
1096 hModule, ordinal );
1098 if (!ordinal) return (FARPROC16)0;
1099 ret = NE_GetEntryPoint( hModule, ordinal );
1100 TRACE(module,"returning %08x\n",(UINT32)ret);
1101 return ret;
1104 /***********************************************************************
1105 * GetProcAddress16 (KERNEL.50)
1107 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1109 WORD ordinal;
1110 FARPROC16 ret;
1112 if (!hModule) hModule = GetCurrentTask();
1113 hModule = GetExePtr( hModule );
1115 if (HIWORD(name) != 0)
1117 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1118 TRACE(module, "%04x '%s'\n",
1119 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1121 else
1123 ordinal = LOWORD(name);
1124 TRACE(module, "%04x %04x\n",
1125 hModule, ordinal );
1127 if (!ordinal) return (FARPROC16)0;
1129 ret = NE_GetEntryPoint( hModule, ordinal );
1131 TRACE(module, "returning %08x\n", (UINT32)ret );
1132 return ret;
1136 /***********************************************************************
1137 * GetProcAddress32 (KERNEL32.257)
1139 FARPROC32 WINAPI GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1141 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, TRUE );
1144 /***********************************************************************
1145 * WIN16_GetProcAddress32 (KERNEL.453)
1147 FARPROC32 WINAPI WIN16_GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1149 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, FALSE );
1152 /***********************************************************************
1153 * MODULE_GetProcAddress32 (internal)
1155 FARPROC32 MODULE_GetProcAddress32(
1156 PDB32 *process, /* [in] process context */
1157 HMODULE32 hModule, /* [in] current module handle */
1158 LPCSTR function, /* [in] function to be looked up */
1159 BOOL32 snoop )
1161 WINE_MODREF *wm = MODULE32_LookupHMODULE(process,hModule);
1163 if (HIWORD(function))
1164 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
1165 else
1166 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
1167 if (!wm)
1168 return (FARPROC32)0;
1169 switch (wm->type)
1171 case MODULE32_PE:
1172 return PE_FindExportedFunction( process, wm, function, snoop );
1173 case MODULE32_ELF:
1174 return ELF_FindExportedFunction( process, wm, function);
1175 default:
1176 ERR(module,"wine_modref type %d not handled.\n",wm->type);
1177 return (FARPROC32)0;
1182 /***********************************************************************
1183 * RtlImageNtHeaders (NTDLL)
1185 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE32 hModule)
1187 /* basically:
1188 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1189 * but we could get HMODULE16 or the like (think builtin modules)
1192 WINE_MODREF *wm = MODULE32_LookupHMODULE( PROCESS_Current(), hModule );
1193 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1194 return PE_HEADER(wm->module);
1198 /***************************************************************************
1199 * HasGPHandler (KERNEL.338)
1202 #pragma pack(1)
1203 typedef struct _GPHANDLERDEF
1205 WORD selector;
1206 WORD rangeStart;
1207 WORD rangeEnd;
1208 WORD handler;
1209 } GPHANDLERDEF;
1210 #pragma pack(4)
1212 SEGPTR WINAPI HasGPHandler( SEGPTR address )
1214 HMODULE16 hModule;
1215 int gpOrdinal;
1216 SEGPTR gpPtr;
1217 GPHANDLERDEF *gpHandler;
1219 if ( (hModule = FarGetOwner( SELECTOROF(address) )) != 0
1220 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1221 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1222 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1223 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1225 while (gpHandler->selector)
1227 if ( SELECTOROF(address) == gpHandler->selector
1228 && OFFSETOF(address) >= gpHandler->rangeStart
1229 && OFFSETOF(address) < gpHandler->rangeEnd )
1230 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1231 gpHandler->handler );
1232 gpHandler++;
1236 return 0;