Added implementations for InterlockedExchangeAdd() and
[wine/multimedia.git] / loader / module.c
blob200b22c86d1e33d36f9401e260095b3561f0123e
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)
704 BOOL32 WINAPI CreateProcess32W( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
705 LPSECURITY_ATTRIBUTES lpProcessAttributes,
706 LPSECURITY_ATTRIBUTES lpThreadAttributes,
707 BOOL32 bInheritHandles, DWORD dwCreationFlags,
708 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
709 LPSTARTUPINFO32W lpStartupInfo,
710 LPPROCESS_INFORMATION lpProcessInfo )
712 FIXME(win32, "(%s,%s,...): stub\n", debugstr_w(lpApplicationName),
713 debugstr_w(lpCommandLine));
715 /* make from lcc uses system as fallback if CreateProcess returns
716 FALSE, so return false */
717 return FALSE;
720 /***********************************************************************
721 * GetModuleHandle (KERNEL32.237)
723 HMODULE32 WINAPI GetModuleHandle32A(LPCSTR module)
725 if (module == NULL)
726 return PROCESS_Current()->exe_modref->module;
727 else
728 return MODULE_FindModule32(PROCESS_Current(),module);
731 HMODULE32 WINAPI GetModuleHandle32W(LPCWSTR module)
733 HMODULE32 hModule;
734 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
735 hModule = GetModuleHandle32A( modulea );
736 HeapFree( GetProcessHeap(), 0, modulea );
737 return hModule;
741 /***********************************************************************
742 * GetModuleFileName32A (KERNEL32.235)
744 DWORD WINAPI GetModuleFileName32A(
745 HMODULE32 hModule, /* [in] module handle (32bit) */
746 LPSTR lpFileName, /* [out] filenamebuffer */
747 DWORD size /* [in] size of filenamebuffer */
748 ) {
749 WINE_MODREF *wm = MODULE32_LookupHMODULE(PROCESS_Current(),hModule);
751 if (!wm) /* can happen on start up or the like */
752 return 0;
754 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
755 lstrcpyn32A( lpFileName, wm->longname, size );
756 else
757 lstrcpyn32A( lpFileName, wm->shortname, size );
759 TRACE(module, "%s\n", lpFileName );
760 return strlen(lpFileName);
764 /***********************************************************************
765 * GetModuleFileName32W (KERNEL32.236)
767 DWORD WINAPI GetModuleFileName32W( HMODULE32 hModule, LPWSTR lpFileName,
768 DWORD size )
770 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
771 DWORD res = GetModuleFileName32A( hModule, fnA, size );
772 lstrcpynAtoW( lpFileName, fnA, size );
773 HeapFree( GetProcessHeap(), 0, fnA );
774 return res;
778 /***********************************************************************
779 * LoadLibraryEx32W (KERNEL.513)
780 * FIXME
782 HMODULE32 WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
783 DWORD flags )
785 TRACE(module,"(%s,%d,%08lx)\n",libname,hf,flags);
786 return LoadLibraryEx32A(libname, hf,flags);
789 /***********************************************************************
790 * LoadLibraryEx32A (KERNEL32)
792 HMODULE32 WINAPI LoadLibraryEx32A(LPCSTR libname,HFILE32 hfile,DWORD flags)
794 HMODULE32 hmod;
795 hmod = MODULE_LoadLibraryEx32A(libname,PROCESS_Current(),hfile,flags);
797 /* initialize DLL just loaded */
798 if ( hmod >= 32 )
799 MODULE_InitializeDLLs( PROCESS_Current(), hmod,
800 DLL_PROCESS_ATTACH, (LPVOID)-1 );
802 return hmod;
805 HMODULE32 MODULE_LoadLibraryEx32A(LPCSTR libname,PDB32*process,HFILE32 hfile,DWORD flags)
807 HMODULE32 hmod;
809 hmod = ELF_LoadLibraryEx32A(libname,process,hfile,flags);
810 if (hmod)
811 return hmod; /* already initialized for ELF */
813 hmod = PE_LoadLibraryEx32A(libname,process,hfile,flags);
814 if (hmod < 32) {
815 char buffer[256];
817 strcpy( buffer, libname );
818 strcat( buffer, ".dll" );
819 hmod = PE_LoadLibraryEx32A(buffer,process,hfile,flags);
821 return hmod;
824 /***********************************************************************
825 * LoadLibraryA (KERNEL32)
827 HMODULE32 WINAPI LoadLibrary32A(LPCSTR libname) {
828 return LoadLibraryEx32A(libname,0,0);
831 /***********************************************************************
832 * LoadLibraryW (KERNEL32)
834 HMODULE32 WINAPI LoadLibrary32W(LPCWSTR libnameW)
836 return LoadLibraryEx32W(libnameW,0,0);
839 /***********************************************************************
840 * LoadLibraryExW (KERNEL32)
842 HMODULE32 WINAPI LoadLibraryEx32W(LPCWSTR libnameW,HFILE32 hfile,DWORD flags)
844 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
845 HMODULE32 ret = LoadLibraryEx32A( libnameA , hfile, flags );
847 HeapFree( GetProcessHeap(), 0, libnameA );
848 return ret;
851 /***********************************************************************
852 * FreeLibrary
854 BOOL32 WINAPI FreeLibrary32(HINSTANCE32 hLibModule)
856 FIXME(module,"(0x%08x): stub\n", hLibModule);
857 return TRUE; /* FIXME */
861 /***********************************************************************
862 * PrivateLoadLibrary (KERNEL32)
864 * FIXME: rough guesswork, don't know what "Private" means
866 HINSTANCE32 WINAPI PrivateLoadLibrary(LPCSTR libname)
868 return (HINSTANCE32)LoadLibrary16(libname);
873 /***********************************************************************
874 * PrivateFreeLibrary (KERNEL32)
876 * FIXME: rough guesswork, don't know what "Private" means
878 void WINAPI PrivateFreeLibrary(HINSTANCE32 handle)
880 FreeLibrary16((HINSTANCE16)handle);
884 /***********************************************************************
885 * WinExec16 (KERNEL.166)
887 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
889 return WinExec32( lpCmdLine, nCmdShow );
893 /***********************************************************************
894 * WinExec32 (KERNEL32.566)
896 HINSTANCE32 WINAPI WinExec32( LPCSTR lpCmdLine, UINT32 nCmdShow )
898 HINSTANCE32 handle = 2;
899 char *p, filename[256];
900 int spacelimit = 0, exhausted = 0;
901 LOADPARAMS32 params;
902 UINT16 paramCmdShow[2];
904 if (!lpCmdLine)
905 return 2; /* File not found */
907 /* Set up LOADPARAMS32 buffer for LoadModule32 */
909 memset( &params, '\0', sizeof(params) );
910 params.lpCmdLine = (LPSTR)lpCmdLine;
911 params.lpCmdShow = paramCmdShow;
912 params.lpCmdShow[0] = 2;
913 params.lpCmdShow[1] = nCmdShow;
916 /* Keep trying to load a file by trying different filenames; e.g.,
917 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
918 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
919 no args */
921 while(!exhausted && handle == 2) {
922 int spacecount = 0;
924 /* Build the filename and command-line */
926 lstrcpyn32A(filename, lpCmdLine,
927 sizeof(filename) - 4 /* for extension */);
929 /* Keep grabbing characters until end-of-string, tab, or until the
930 number of spaces is greater than the spacelimit */
932 for (p = filename; ; p++) {
933 if(*p == ' ') {
934 ++spacecount;
935 if(spacecount > spacelimit) {
936 ++spacelimit;
937 break;
941 if(*p == '\0' || *p == '\t') {
942 exhausted = 1;
943 break;
947 *p = '\0';
949 /* Now load the executable file */
951 if (!__winelib)
953 handle = LoadModule32( filename, &params );
954 if (handle == 2) /* file not found */
956 /* Check that the original file name did not have a suffix */
957 p = strrchr(filename, '.');
958 /* if there is a '.', check if either \ OR / follow */
959 if (!p || strchr(p, '/') || strchr(p, '\\'))
961 p = filename + strlen(filename);
962 strcpy( p, ".exe" );
963 handle = LoadModule32( filename, &params );
964 *p = '\0'; /* Remove extension */
968 else
969 handle = 2; /* file not found */
971 if (handle < 32)
973 /* Try to start it as a unix program */
974 if (!fork())
976 /* Child process */
977 DOS_FULL_NAME full_name;
978 const char *unixfilename = NULL;
979 const char *argv[256], **argptr;
980 int iconic = (nCmdShow == SW_SHOWMINIMIZED ||
981 nCmdShow == SW_SHOWMINNOACTIVE);
983 THREAD_InitDone = FALSE; /* we didn't init this process */
984 /* get unixfilename */
985 if (strchr(filename, '/') ||
986 strchr(filename, ':') ||
987 strchr(filename, '\\'))
989 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
990 unixfilename = full_name.long_name;
992 else unixfilename = filename;
994 if (unixfilename)
996 /* build argv */
997 argptr = argv;
998 if (iconic) *argptr++ = "-iconic";
999 *argptr++ = unixfilename;
1000 p = strdup(lpCmdLine);
1001 while (1)
1003 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
1004 if (!*p) break;
1005 *argptr++ = p;
1006 while (*p && *p != ' ' && *p != '\t') p++;
1008 *argptr++ = 0;
1010 /* Execute */
1011 execvp(argv[0], (char**)argv);
1014 /* Failed ! */
1016 if (__winelib)
1018 /* build argv */
1019 argptr = argv;
1020 *argptr++ = "wine";
1021 if (iconic) *argptr++ = "-iconic";
1022 *argptr++ = lpCmdLine;
1023 *argptr++ = 0;
1025 /* Execute */
1026 execvp(argv[0] , (char**)argv);
1028 /* Failed ! */
1029 MSG("WinExec: can't exec 'wine %s'\n",
1030 lpCmdLine);
1032 exit(1);
1035 } /* while (!exhausted && handle < 32) */
1037 return handle;
1041 /***********************************************************************
1042 * WIN32_GetProcAddress16 (KERNEL32.36)
1043 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1045 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hModule, LPCSTR name )
1047 WORD ordinal;
1048 FARPROC16 ret;
1050 if (!hModule) {
1051 WARN(module,"hModule may not be 0!\n");
1052 return (FARPROC16)0;
1054 if (HIWORD(hModule))
1056 WARN( module, "hModule is Win32 handle (%08x)\n", hModule );
1057 return (FARPROC16)0;
1059 hModule = GetExePtr( hModule );
1060 if (HIWORD(name)) {
1061 ordinal = NE_GetOrdinal( hModule, name );
1062 TRACE(module, "%04x '%s'\n",
1063 hModule, name );
1064 } else {
1065 ordinal = LOWORD(name);
1066 TRACE(module, "%04x %04x\n",
1067 hModule, ordinal );
1069 if (!ordinal) return (FARPROC16)0;
1070 ret = NE_GetEntryPoint( hModule, ordinal );
1071 TRACE(module,"returning %08x\n",(UINT32)ret);
1072 return ret;
1075 /***********************************************************************
1076 * GetProcAddress16 (KERNEL.50)
1078 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1080 WORD ordinal;
1081 FARPROC16 ret;
1083 if (!hModule) hModule = GetCurrentTask();
1084 hModule = GetExePtr( hModule );
1086 if (HIWORD(name) != 0)
1088 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1089 TRACE(module, "%04x '%s'\n",
1090 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1092 else
1094 ordinal = LOWORD(name);
1095 TRACE(module, "%04x %04x\n",
1096 hModule, ordinal );
1098 if (!ordinal) return (FARPROC16)0;
1100 ret = NE_GetEntryPoint( hModule, ordinal );
1102 TRACE(module, "returning %08x\n", (UINT32)ret );
1103 return ret;
1107 /***********************************************************************
1108 * GetProcAddress32 (KERNEL32.257)
1110 FARPROC32 WINAPI GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1112 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, TRUE );
1115 /***********************************************************************
1116 * WIN16_GetProcAddress32 (KERNEL.453)
1118 FARPROC32 WINAPI WIN16_GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1120 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, FALSE );
1123 /***********************************************************************
1124 * MODULE_GetProcAddress32 (internal)
1126 FARPROC32 MODULE_GetProcAddress32(
1127 PDB32 *process, /* [in] process context */
1128 HMODULE32 hModule, /* [in] current module handle */
1129 LPCSTR function, /* [in] function to be looked up */
1130 BOOL32 snoop )
1132 WINE_MODREF *wm = MODULE32_LookupHMODULE(process,hModule);
1134 if (HIWORD(function))
1135 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
1136 else
1137 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
1138 if (!wm)
1139 return (FARPROC32)0;
1140 switch (wm->type)
1142 case MODULE32_PE:
1143 return PE_FindExportedFunction( process, wm, function, snoop );
1144 case MODULE32_ELF:
1145 return ELF_FindExportedFunction( process, wm, function);
1146 default:
1147 ERR(module,"wine_modref type %d not handled.\n",wm->type);
1148 return (FARPROC32)0;
1153 /***********************************************************************
1154 * RtlImageNtHeaders (NTDLL)
1156 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE32 hModule)
1158 /* basically:
1159 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1160 * but we could get HMODULE16 or the like (think builtin modules)
1163 WINE_MODREF *wm = MODULE32_LookupHMODULE( PROCESS_Current(), hModule );
1164 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1165 return PE_HEADER(wm->module);
1169 /***************************************************************************
1170 * HasGPHandler (KERNEL.338)
1173 #pragma pack(1)
1174 typedef struct _GPHANDLERDEF
1176 WORD selector;
1177 WORD rangeStart;
1178 WORD rangeEnd;
1179 WORD handler;
1180 } GPHANDLERDEF;
1181 #pragma pack(4)
1183 SEGPTR WINAPI HasGPHandler( SEGPTR address )
1185 HMODULE16 hModule;
1186 int gpOrdinal;
1187 SEGPTR gpPtr;
1188 GPHANDLERDEF *gpHandler;
1190 if ( (hModule = FarGetOwner( SELECTOROF(address) )) != 0
1191 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1192 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1193 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1194 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1196 while (gpHandler->selector)
1198 if ( SELECTOROF(address) == gpHandler->selector
1199 && OFFSETOF(address) >= gpHandler->rangeStart
1200 && OFFSETOF(address) < gpHandler->rangeEnd )
1201 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1202 gpHandler->handler );
1203 gpHandler++;
1207 return 0;