Fix a couple of 64bit platform problems and speed up compilation. See
[wine/multimedia.git] / loader / module.c
blob802a6ac59440bdaf53d5aff929d21b48b36f76b3
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 "wine/winuser16.h"
14 #include "wine/winbase16.h"
15 #include "winerror.h"
16 #include "class.h"
17 #include "file.h"
18 #include "global.h"
19 #include "heap.h"
20 #include "module.h"
21 #include "neexe.h"
22 #include "pe_image.h"
23 #include "dosexe.h"
24 #include "process.h"
25 #include "thread.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 case MODULE32_ELF:
99 /* no need to do that, dlopen() already does */
100 break;
101 default:
102 ERR(module, "wine_modref type %d not handled.\n", wm->type);
103 break;
106 TRACE( module, "(%p,%08x,%ld,%p) - END\n",
107 process, wm->module, type, lpReserved );
110 void MODULE_InitializeDLLs( PDB32 *process, HMODULE32 root,
111 DWORD type, LPVOID lpReserved )
113 BOOL32 inProgress = FALSE;
114 WINE_MODREF *wm;
116 /* Grab the process critical section to protect the recursion flags */
117 /* FIXME: This is probably overkill! */
118 EnterCriticalSection( &process->crit_section );
120 TRACE( module, "(%p,%08x,%ld,%p) - START\n", process, root, type, lpReserved );
122 /* First, check whether initialization is currently in progress */
123 for ( wm = process->modref_list; wm; wm = wm->next )
124 if ( wm->initDone )
126 inProgress = TRUE;
127 break;
130 if ( inProgress )
133 * If this a LoadLibrary call from within an initialization routine,
134 * treat it analogously to an implicitly referenced DLL.
135 * Anything else may not happen at this point!
137 if ( root )
139 wm = MODULE32_LookupHMODULE( process, root );
140 if ( wm && !wm->initDone )
141 MODULE_DoInitializeDLLs( process, wm, type, lpReserved );
143 else
144 FIXME(module, "Invalid recursion!\n");
146 else
148 /* If we arrive here, this is the start of an initialization run */
149 if ( !root )
151 /* If called for main EXE, initialize all DLLs */
152 for ( wm = process->modref_list; wm; wm = wm->next )
153 if ( !wm->initDone )
154 MODULE_DoInitializeDLLs( process, wm, type, lpReserved );
156 else
158 /* If called for a specific DLL, initialize only it and its children */
159 wm = MODULE32_LookupHMODULE( process, root );
160 if (wm) MODULE_DoInitializeDLLs( process, wm, type, lpReserved );
163 /* We're finished, so we reset all recursion flags */
164 for ( wm = process->modref_list; wm; wm = wm->next )
165 wm->initDone = FALSE;
168 TRACE( module, "(%p,%08x,%ld,%p) - END\n", process, root, type, lpReserved );
170 /* Release critical section */
171 LeaveCriticalSection( &process->crit_section );
175 /***********************************************************************
176 * MODULE_CreateDummyModule
178 * Create a dummy NE module for Win32 or Winelib.
180 HMODULE32 MODULE_CreateDummyModule( const OFSTRUCT *ofs, LPCSTR modName )
182 HMODULE32 hModule;
183 NE_MODULE *pModule;
184 SEGTABLEENTRY *pSegment;
185 char *pStr,*s;
186 int len;
187 const char* basename;
189 INT32 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
190 + strlen(ofs->szPathName) + 1;
191 INT32 size = sizeof(NE_MODULE) +
192 /* loaded file info */
193 of_size +
194 /* segment table: DS,CS */
195 2 * sizeof(SEGTABLEENTRY) +
196 /* name table */
198 /* several empty tables */
201 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
202 if (!hModule) return (HMODULE32)11; /* invalid exe */
204 FarSetOwner( hModule, hModule );
205 pModule = (NE_MODULE *)GlobalLock16( hModule );
207 /* Set all used entries */
208 pModule->magic = IMAGE_OS2_SIGNATURE;
209 pModule->count = 1;
210 pModule->next = 0;
211 pModule->flags = 0;
212 pModule->dgroup = 1;
213 pModule->ss = 1;
214 pModule->cs = 2;
215 pModule->heap_size = 0xe000;
216 pModule->stack_size = 0x1000;
217 pModule->seg_count = 2;
218 pModule->modref_count = 0;
219 pModule->nrname_size = 0;
220 pModule->fileinfo = sizeof(NE_MODULE);
221 pModule->os_flags = NE_OSFLAGS_WINDOWS;
222 pModule->expected_version = 0x030a;
223 pModule->self = hModule;
225 /* Set loaded file information */
226 memcpy( pModule + 1, ofs, of_size );
227 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
229 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
230 pModule->seg_table = pModule->dgroup_entry = (int)pSegment - (int)pModule;
231 /* Data segment */
232 pSegment->size = 0;
233 pSegment->flags = NE_SEGFLAGS_DATA;
234 pSegment->minsize = 0x1000;
235 pSegment++;
236 /* Code segment */
237 pSegment->flags = 0;
238 pSegment++;
240 /* Module name */
241 pStr = (char *)pSegment;
242 pModule->name_table = (int)pStr - (int)pModule;
243 if ( modName )
244 basename = modName;
245 else
247 basename = strrchr(ofs->szPathName,'\\');
248 if (!basename) basename = ofs->szPathName;
249 else basename++;
251 len = strlen(basename);
252 if ((s = strchr(basename,'.'))) len = s - basename;
253 if (len > 8) len = 8;
254 *pStr = len;
255 strncpy( pStr+1, basename, len );
256 if (len < 8) pStr[len+1] = 0;
257 pStr += 9;
259 /* All tables zero terminated */
260 pModule->res_table = pModule->import_table = pModule->entry_table =
261 (int)pStr - (int)pModule;
263 NE_RegisterModule( pModule );
264 return hModule;
268 /***********************************************************************
269 * MODULE_GetWndProcEntry16 (not a Windows API function)
271 * Return an entry point from the WPROCS dll.
273 FARPROC16 MODULE_GetWndProcEntry16( LPCSTR name )
275 FARPROC16 ret = NULL;
277 if (__winelib)
279 /* FIXME: hack for Winelib */
280 extern LRESULT ColorDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
281 extern LRESULT FileOpenDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
282 extern LRESULT FileSaveDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
283 extern LRESULT FindTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
284 extern LRESULT PrintDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
285 extern LRESULT PrintSetupDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
286 extern LRESULT ReplaceTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
288 if (!strcmp(name,"ColorDlgProc"))
289 return (FARPROC16)ColorDlgProc;
290 if (!strcmp(name,"FileOpenDlgProc"))
291 return (FARPROC16)FileOpenDlgProc;
292 if (!strcmp(name,"FileSaveDlgProc"))
293 return (FARPROC16)FileSaveDlgProc;
294 if (!strcmp(name,"FindTextDlgProc"))
295 return (FARPROC16)FindTextDlgProc16;
296 if (!strcmp(name,"PrintDlgProc"))
297 return (FARPROC16)PrintDlgProc;
298 if (!strcmp(name,"PrintSetupDlgProc"))
299 return (FARPROC16)PrintSetupDlgProc;
300 if (!strcmp(name,"ReplaceTextDlgProc"))
301 return (FARPROC16)ReplaceTextDlgProc16;
302 if (!strcmp(name,"DefResourceHandler"))
303 return (FARPROC16)NE_DefResourceHandler;
304 if (!strcmp(name,"LoadDIBIconHandler"))
305 return (FARPROC16)LoadDIBIconHandler;
306 if (!strcmp(name,"LoadDIBCursorHandler"))
307 return (FARPROC16)LoadDIBCursorHandler;
308 FIXME(module,"No mapping for %s(), add one in library/miscstubs.c\n",name);
309 assert( FALSE );
310 return NULL;
312 else
314 WORD ordinal;
315 static HMODULE32 hModule = 0;
317 if (!hModule) hModule = GetModuleHandle16( "WPROCS" );
318 ordinal = NE_GetOrdinal( hModule, name );
319 if (!(ret = NE_GetEntryPoint( hModule, ordinal )))
321 WARN( module, "%s not found\n", name );
322 assert( FALSE );
325 return ret;
329 /**********************************************************************
330 * MODULE_FindModule32
332 * Find a (loaded) win32 module depending on path
333 * The handling of '.' is a bit weird, but we need it that way,
334 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
335 * this is the only way to differentiate. (mainly hypertrm.exe)
337 * RETURNS
338 * the module handle if found
339 * 0 if not
341 HMODULE32 MODULE_FindModule32(
342 PDB32* process, /* [in] process in which to find the library */
343 LPCSTR path /* [in] pathname of module/library to be found */
345 LPSTR filename;
346 LPSTR dotptr;
347 WINE_MODREF *wm;
349 if (!process)
350 return 0;
351 if (!(filename = strrchr( path, '\\' )))
352 filename = HEAP_strdupA(process->heap,0,path);
353 else
354 filename = HEAP_strdupA(process->heap,0,filename+1);
355 dotptr=strrchr(filename,'.');
357 for (wm=process->modref_list;wm;wm=wm->next) {
358 LPSTR xmodname,xdotptr;
360 assert (wm->modname);
361 xmodname = HEAP_strdupA(process->heap,0,wm->modname);
362 xdotptr=strrchr(xmodname,'.');
363 if ( (xdotptr && !dotptr) ||
364 (!xdotptr && dotptr)
366 if (dotptr) *dotptr = '\0';
367 if (xdotptr) *xdotptr = '\0';
369 if (!strcasecmp( filename, xmodname)) {
370 HeapFree(process->heap,0,filename);
371 HeapFree(process->heap,0,xmodname);
372 return wm->module;
374 if (dotptr) *dotptr='.';
375 /* FIXME: add paths, shortname */
376 HeapFree(process->heap,0,xmodname);
378 /* if that fails, try looking for the filename... */
379 for (wm=process->modref_list;wm;wm=wm->next) {
380 LPSTR xlname,xdotptr;
382 assert (wm->longname);
383 xlname = strrchr(wm->longname,'\\');
384 if (!xlname)
385 xlname = wm->longname;
386 else
387 xlname++;
388 xlname = HEAP_strdupA(process->heap,0,xlname);
389 xdotptr=strrchr(xlname,'.');
390 if ( (xdotptr && !dotptr) ||
391 (!xdotptr && dotptr)
393 if (dotptr) *dotptr = '\0';
394 if (xdotptr) *xdotptr = '\0';
396 if (!strcasecmp( filename, xlname)) {
397 HeapFree(process->heap,0,filename);
398 HeapFree(process->heap,0,xlname);
399 return wm->module;
401 if (dotptr) *dotptr='.';
402 /* FIXME: add paths, shortname */
403 HeapFree(process->heap,0,xlname);
405 HeapFree(process->heap,0,filename);
406 return 0;
411 /**********************************************************************
412 * NE_CreateProcess
414 static HINSTANCE16 NE_CreateProcess( LPCSTR name, LPCSTR cmd_line, LPCSTR env,
415 BOOL32 inherit, LPSTARTUPINFO32A startup,
416 LPPROCESS_INFORMATION info )
418 HINSTANCE16 hInstance, hPrevInstance;
419 NE_MODULE *pModule;
421 /* Load module */
423 hInstance = NE_LoadModule( name, &hPrevInstance, TRUE, FALSE );
424 if (hInstance < 32) return hInstance;
426 if ( !(pModule = NE_GetPtr(hInstance))
427 || (pModule->flags & NE_FFLAGS_LIBMODULE))
429 /* FIXME: cleanup */
430 return 11;
433 /* Create a task for this instance */
435 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
437 PROCESS_Create( pModule, cmd_line, env, hInstance,
438 hPrevInstance, inherit, startup, info );
440 return hInstance;
444 /**********************************************************************
445 * LoadModule16 (KERNEL.45)
447 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
449 LOADPARAMS *params;
450 LPSTR cmd_line, new_cmd_line;
451 LPCVOID env = NULL;
452 STARTUPINFO32A startup;
453 PROCESS_INFORMATION info;
454 HINSTANCE16 hInstance, hPrevInstance;
455 NE_MODULE *pModule;
456 PDB32 *pdb;
458 /* Load module */
460 if (!paramBlock || (paramBlock == (LPVOID)-1))
461 return LoadLibrary16( name );
463 hInstance = NE_LoadModule( name, &hPrevInstance, FALSE, FALSE );
464 if ( hInstance < 32 || !(pModule = NE_GetPtr(hInstance))
465 || (pModule->flags & NE_FFLAGS_LIBMODULE))
466 return hInstance;
468 /* Create a task for this instance */
470 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
472 params = (LOADPARAMS *)paramBlock;
473 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
474 if (!cmd_line) cmd_line = "";
475 else if (*cmd_line) cmd_line++; /* skip the length byte */
477 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
478 strlen(cmd_line)+strlen(name)+2 )))
479 return 0;
480 strcpy( new_cmd_line, name );
481 strcat( new_cmd_line, " " );
482 strcat( new_cmd_line, cmd_line );
484 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
486 memset( &info, '\0', sizeof(info) );
487 memset( &startup, '\0', sizeof(startup) );
488 startup.cb = sizeof(startup);
489 if (params->showCmd)
491 startup.dwFlags = STARTF_USESHOWWINDOW;
492 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
495 pdb = PROCESS_Create( pModule, new_cmd_line, env,
496 hInstance, hPrevInstance, TRUE, &startup, &info );
498 CloseHandle( info.hThread );
499 CloseHandle( info.hProcess );
501 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
502 HeapFree( GetProcessHeap(), 0, new_cmd_line );
504 /* Start task */
506 if (pdb) TASK_StartTask( pdb->task );
508 return hInstance;
511 /**********************************************************************
512 * LoadModule32 (KERNEL32.499)
514 HINSTANCE32 WINAPI LoadModule32( LPCSTR name, LPVOID paramBlock )
516 LOADPARAMS32 *params = (LOADPARAMS32 *)paramBlock;
517 PROCESS_INFORMATION info;
518 STARTUPINFO32A startup;
519 HINSTANCE32 hInstance;
520 PDB32 *pdb;
521 TDB *tdb;
523 memset( &startup, '\0', sizeof(startup) );
524 startup.cb = sizeof(startup);
525 startup.dwFlags = STARTF_USESHOWWINDOW;
526 startup.wShowWindow = params->lpCmdShow? params->lpCmdShow[1] : 0;
528 if (!CreateProcess32A( name, params->lpCmdLine,
529 NULL, NULL, FALSE, 0, params->lpEnvAddress,
530 NULL, &startup, &info ))
531 return GetLastError(); /* guaranteed to be < 32 */
533 /* Get 16-bit hInstance/hTask from process */
534 pdb = PROCESS_IdToPDB( info.dwProcessId );
535 tdb = pdb? (TDB *)GlobalLock16( pdb->task ) : NULL;
536 hInstance = tdb && tdb->hInstance? tdb->hInstance : pdb? pdb->task : 0;
538 /* Close off the handles */
539 CloseHandle( info.hThread );
540 CloseHandle( info.hProcess );
542 return hInstance;
545 /**********************************************************************
546 * CreateProcess32A (KERNEL32.171)
548 BOOL32 WINAPI CreateProcess32A( LPCSTR lpApplicationName, LPSTR lpCommandLine,
549 LPSECURITY_ATTRIBUTES lpProcessAttributes,
550 LPSECURITY_ATTRIBUTES lpThreadAttributes,
551 BOOL32 bInheritHandles, DWORD dwCreationFlags,
552 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
553 LPSTARTUPINFO32A lpStartupInfo,
554 LPPROCESS_INFORMATION lpProcessInfo )
556 HINSTANCE16 hInstance;
557 LPCSTR cmdline;
558 PDB32 *pdb;
559 char name[256];
561 /* Get name and command line */
563 if (!lpApplicationName && !lpCommandLine)
565 SetLastError( ERROR_FILE_NOT_FOUND );
566 return FALSE;
569 cmdline = lpCommandLine? lpCommandLine : lpApplicationName;
571 if (lpApplicationName)
572 lstrcpyn32A(name, lpApplicationName, sizeof(name) - 4);
573 else {
574 char *ptr;
575 int len;
577 /* Take care off .exes with spaces in their names */
578 ptr = strchr(lpCommandLine, ' ');
579 do {
580 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
581 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
582 lstrcpyn32A(name, lpCommandLine, len);
583 if (!strchr(name, '\\') && !strchr(name, '.'))
584 strcat(name, ".exe");
585 if (GetFileAttributes32A(name)!=-1)
586 break;
587 /* if there is a space and no file found yet, include the word
588 * up to the next space too. If there is no next space, just
589 * use the first word.
591 if (ptr) {
592 ptr = strchr(ptr+1, ' ');
593 } else {
594 ptr = strchr(lpCommandLine, ' ');
595 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
596 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
597 lstrcpyn32A(name, lpCommandLine, len);
598 break;
600 } while (1);
603 if (!strchr(name, '\\') && !strchr(name, '.'))
604 strcat(name, ".exe");
607 /* Warn if unsupported features are used */
609 if (lpProcessAttributes)
610 FIXME(module, "(%s,...): lpProcessAttributes ignored\n", name);
611 if (lpThreadAttributes)
612 FIXME(module, "(%s,...): lpThreadAttributes 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, bInheritHandles,
676 lpStartupInfo, lpProcessInfo );
678 /* Try PE module */
679 if (hInstance == 21)
680 hInstance = PE_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
681 lpStartupInfo, lpProcessInfo );
683 /* Try DOS module */
684 if (hInstance == 11)
685 hInstance = MZ_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
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) return hmod;
841 hmod = PE_LoadLibraryEx32A(libname,process,hfile,flags);
842 return hmod;
845 /***********************************************************************
846 * LoadLibraryA (KERNEL32)
848 HMODULE32 WINAPI LoadLibrary32A(LPCSTR libname) {
849 return LoadLibraryEx32A(libname,0,0);
852 /***********************************************************************
853 * LoadLibraryW (KERNEL32)
855 HMODULE32 WINAPI LoadLibrary32W(LPCWSTR libnameW)
857 return LoadLibraryEx32W(libnameW,0,0);
860 /***********************************************************************
861 * LoadLibraryExW (KERNEL32)
863 HMODULE32 WINAPI LoadLibraryEx32W(LPCWSTR libnameW,HFILE32 hfile,DWORD flags)
865 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
866 HMODULE32 ret = LoadLibraryEx32A( libnameA , hfile, flags );
868 HeapFree( GetProcessHeap(), 0, libnameA );
869 return ret;
872 /***********************************************************************
873 * FreeLibrary
875 BOOL32 WINAPI FreeLibrary32(HINSTANCE32 hLibModule)
877 FIXME(module,"(0x%08x): stub\n", hLibModule);
878 return TRUE; /* FIXME */
882 /***********************************************************************
883 * PrivateLoadLibrary (KERNEL32)
885 * FIXME: rough guesswork, don't know what "Private" means
887 HINSTANCE32 WINAPI PrivateLoadLibrary(LPCSTR libname)
889 return (HINSTANCE32)LoadLibrary16(libname);
894 /***********************************************************************
895 * PrivateFreeLibrary (KERNEL32)
897 * FIXME: rough guesswork, don't know what "Private" means
899 void WINAPI PrivateFreeLibrary(HINSTANCE32 handle)
901 FreeLibrary16((HINSTANCE16)handle);
905 /***********************************************************************
906 * WinExec16 (KERNEL.166)
908 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
910 return WinExec32( lpCmdLine, nCmdShow );
914 /***********************************************************************
915 * WinExec32 (KERNEL32.566)
917 HINSTANCE32 WINAPI WinExec32( LPCSTR lpCmdLine, UINT32 nCmdShow )
919 HINSTANCE32 handle = 2;
920 char *p, filename[256];
921 int spacelimit = 0, exhausted = 0;
922 LOADPARAMS32 params;
923 UINT16 paramCmdShow[2];
925 if (!lpCmdLine)
926 return 2; /* File not found */
928 /* Set up LOADPARAMS32 buffer for LoadModule32 */
930 memset( &params, '\0', sizeof(params) );
931 params.lpCmdLine = (LPSTR)lpCmdLine;
932 params.lpCmdShow = paramCmdShow;
933 params.lpCmdShow[0] = 2;
934 params.lpCmdShow[1] = nCmdShow;
937 /* Keep trying to load a file by trying different filenames; e.g.,
938 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
939 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
940 no args */
942 while(!exhausted && handle == 2) {
943 int spacecount = 0;
945 /* Build the filename and command-line */
947 lstrcpyn32A(filename, lpCmdLine,
948 sizeof(filename) - 4 /* for extension */);
950 /* Keep grabbing characters until end-of-string, tab, or until the
951 number of spaces is greater than the spacelimit */
953 for (p = filename; ; p++) {
954 if(*p == ' ') {
955 ++spacecount;
956 if(spacecount > spacelimit) {
957 ++spacelimit;
958 break;
962 if(*p == '\0' || *p == '\t') {
963 exhausted = 1;
964 break;
968 *p = '\0';
970 /* Now load the executable file */
972 if (!__winelib)
974 handle = LoadModule32( filename, &params );
975 if (handle == 2) /* file not found */
977 /* Check that the original file name did not have a suffix */
978 p = strrchr(filename, '.');
979 /* if there is a '.', check if either \ OR / follow */
980 if (!p || strchr(p, '/') || strchr(p, '\\'))
982 p = filename + strlen(filename);
983 strcpy( p, ".exe" );
984 handle = LoadModule32( filename, &params );
985 *p = '\0'; /* Remove extension */
989 else
990 handle = 2; /* file not found */
992 if (handle < 32)
994 /* Try to start it as a unix program */
995 if (!fork())
997 /* Child process */
998 DOS_FULL_NAME full_name;
999 const char *unixfilename = NULL;
1000 const char *argv[256], **argptr;
1001 int iconic = (nCmdShow == SW_SHOWMINIMIZED ||
1002 nCmdShow == SW_SHOWMINNOACTIVE);
1004 THREAD_InitDone = FALSE; /* we didn't init this process */
1005 /* get unixfilename */
1006 if (strchr(filename, '/') ||
1007 strchr(filename, ':') ||
1008 strchr(filename, '\\'))
1010 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
1011 unixfilename = full_name.long_name;
1013 else unixfilename = filename;
1015 if (unixfilename)
1017 /* build argv */
1018 argptr = argv;
1019 if (iconic) *argptr++ = "-iconic";
1020 *argptr++ = unixfilename;
1021 p = strdup(lpCmdLine);
1022 while (1)
1024 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
1025 if (!*p) break;
1026 *argptr++ = p;
1027 while (*p && *p != ' ' && *p != '\t') p++;
1029 *argptr++ = 0;
1031 /* Execute */
1032 execvp(argv[0], (char**)argv);
1035 /* Failed ! */
1037 if (__winelib)
1039 /* build argv */
1040 argptr = argv;
1041 *argptr++ = "wine";
1042 if (iconic) *argptr++ = "-iconic";
1043 *argptr++ = lpCmdLine;
1044 *argptr++ = 0;
1046 /* Execute */
1047 execvp(argv[0] , (char**)argv);
1049 /* Failed ! */
1050 MSG("WinExec: can't exec 'wine %s'\n",
1051 lpCmdLine);
1053 exit(1);
1056 } /* while (!exhausted && handle < 32) */
1058 return handle;
1062 /***********************************************************************
1063 * WIN32_GetProcAddress16 (KERNEL32.36)
1064 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1066 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hModule, LPCSTR name )
1068 WORD ordinal;
1069 FARPROC16 ret;
1071 if (!hModule) {
1072 WARN(module,"hModule may not be 0!\n");
1073 return (FARPROC16)0;
1075 if (HIWORD(hModule))
1077 WARN( module, "hModule is Win32 handle (%08x)\n", hModule );
1078 return (FARPROC16)0;
1080 hModule = GetExePtr( hModule );
1081 if (HIWORD(name)) {
1082 ordinal = NE_GetOrdinal( hModule, name );
1083 TRACE(module, "%04x '%s'\n",
1084 hModule, name );
1085 } else {
1086 ordinal = LOWORD(name);
1087 TRACE(module, "%04x %04x\n",
1088 hModule, ordinal );
1090 if (!ordinal) return (FARPROC16)0;
1091 ret = NE_GetEntryPoint( hModule, ordinal );
1092 TRACE(module,"returning %08x\n",(UINT32)ret);
1093 return ret;
1096 /***********************************************************************
1097 * GetProcAddress16 (KERNEL.50)
1099 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1101 WORD ordinal;
1102 FARPROC16 ret;
1104 if (!hModule) hModule = GetCurrentTask();
1105 hModule = GetExePtr( hModule );
1107 if (HIWORD(name) != 0)
1109 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1110 TRACE(module, "%04x '%s'\n",
1111 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1113 else
1115 ordinal = LOWORD(name);
1116 TRACE(module, "%04x %04x\n",
1117 hModule, ordinal );
1119 if (!ordinal) return (FARPROC16)0;
1121 ret = NE_GetEntryPoint( hModule, ordinal );
1123 TRACE(module, "returning %08x\n", (UINT32)ret );
1124 return ret;
1128 /***********************************************************************
1129 * GetProcAddress32 (KERNEL32.257)
1131 FARPROC32 WINAPI GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1133 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, TRUE );
1136 /***********************************************************************
1137 * WIN16_GetProcAddress32 (KERNEL.453)
1139 FARPROC32 WINAPI WIN16_GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1141 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, FALSE );
1144 /***********************************************************************
1145 * MODULE_GetProcAddress32 (internal)
1147 FARPROC32 MODULE_GetProcAddress32(
1148 PDB32 *process, /* [in] process context */
1149 HMODULE32 hModule, /* [in] current module handle */
1150 LPCSTR function, /* [in] function to be looked up */
1151 BOOL32 snoop )
1153 WINE_MODREF *wm = MODULE32_LookupHMODULE(process,hModule);
1155 if (HIWORD(function))
1156 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
1157 else
1158 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
1159 if (!wm)
1160 return (FARPROC32)0;
1161 switch (wm->type)
1163 case MODULE32_PE:
1164 return PE_FindExportedFunction( process, wm, function, snoop );
1165 case MODULE32_ELF:
1166 return ELF_FindExportedFunction( process, wm, function);
1167 default:
1168 ERR(module,"wine_modref type %d not handled.\n",wm->type);
1169 return (FARPROC32)0;
1174 /***********************************************************************
1175 * RtlImageNtHeaders (NTDLL)
1177 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE32 hModule)
1179 /* basically:
1180 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1181 * but we could get HMODULE16 or the like (think builtin modules)
1184 WINE_MODREF *wm = MODULE32_LookupHMODULE( PROCESS_Current(), hModule );
1185 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1186 return PE_HEADER(wm->module);
1190 /***************************************************************************
1191 * HasGPHandler (KERNEL.338)
1194 #pragma pack(1)
1195 typedef struct _GPHANDLERDEF
1197 WORD selector;
1198 WORD rangeStart;
1199 WORD rangeEnd;
1200 WORD handler;
1201 } GPHANDLERDEF;
1202 #pragma pack(4)
1204 SEGPTR WINAPI HasGPHandler( SEGPTR address )
1206 HMODULE16 hModule;
1207 int gpOrdinal;
1208 SEGPTR gpPtr;
1209 GPHANDLERDEF *gpHandler;
1211 if ( (hModule = FarGetOwner( SELECTOROF(address) )) != 0
1212 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1213 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1214 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1215 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1217 while (gpHandler->selector)
1219 if ( SELECTOROF(address) == gpHandler->selector
1220 && OFFSETOF(address) >= gpHandler->rangeStart
1221 && OFFSETOF(address) < gpHandler->rangeEnd )
1222 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1223 gpHandler->handler );
1224 gpHandler++;
1228 return 0;