Small fixes.
[wine/multimedia.git] / loader / module.c
blobf1aca823f383cd99bb8ebc1fc8b50eb2c6a0ffe5
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 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 )
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 basename = strrchr(ofs->szPathName,'\\');
244 if (!basename) basename = ofs->szPathName;
245 else basename++;
246 len = strlen(basename);
247 if ((s = strchr(basename,'.'))) len = s - basename;
248 if (len > 8) len = 8;
249 *pStr = len;
250 strncpy( pStr+1, basename, len );
251 if (len < 8) pStr[len+1] = 0;
252 pStr += 9;
254 /* All tables zero terminated */
255 pModule->res_table = pModule->import_table = pModule->entry_table =
256 (int)pStr - (int)pModule;
258 NE_RegisterModule( pModule );
259 return hModule;
263 /***********************************************************************
264 * MODULE_GetWndProcEntry16 (not a Windows API function)
266 * Return an entry point from the WPROCS dll.
268 FARPROC16 MODULE_GetWndProcEntry16( LPCSTR name )
270 FARPROC16 ret = NULL;
272 if (__winelib)
274 /* FIXME: hack for Winelib */
275 extern LRESULT ColorDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
276 extern LRESULT FileOpenDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
277 extern LRESULT FileSaveDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
278 extern LRESULT FindTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
279 extern LRESULT PrintDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
280 extern LRESULT PrintSetupDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
281 extern LRESULT ReplaceTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
283 if (!strcmp(name,"ColorDlgProc"))
284 return (FARPROC16)ColorDlgProc;
285 if (!strcmp(name,"FileOpenDlgProc"))
286 return (FARPROC16)FileOpenDlgProc;
287 if (!strcmp(name,"FileSaveDlgProc"))
288 return (FARPROC16)FileSaveDlgProc;
289 if (!strcmp(name,"FindTextDlgProc"))
290 return (FARPROC16)FindTextDlgProc16;
291 if (!strcmp(name,"PrintDlgProc"))
292 return (FARPROC16)PrintDlgProc;
293 if (!strcmp(name,"PrintSetupDlgProc"))
294 return (FARPROC16)PrintSetupDlgProc;
295 if (!strcmp(name,"ReplaceTextDlgProc"))
296 return (FARPROC16)ReplaceTextDlgProc16;
297 if (!strcmp(name,"DefResourceHandler"))
298 return (FARPROC16)NE_DefResourceHandler;
299 if (!strcmp(name,"LoadDIBIconHandler"))
300 return (FARPROC16)LoadDIBIconHandler;
301 if (!strcmp(name,"LoadDIBCursorHandler"))
302 return (FARPROC16)LoadDIBCursorHandler;
303 FIXME(module,"No mapping for %s(), add one in library/miscstubs.c\n",name);
304 assert( FALSE );
305 return NULL;
307 else
309 WORD ordinal;
310 static HMODULE32 hModule = 0;
312 if (!hModule) hModule = GetModuleHandle16( "WPROCS" );
313 ordinal = NE_GetOrdinal( hModule, name );
314 if (!(ret = NE_GetEntryPoint( hModule, ordinal )))
316 WARN( module, "%s not found\n", name );
317 assert( FALSE );
320 return ret;
324 /**********************************************************************
325 * MODULE_FindModule32
327 * Find a (loaded) win32 module depending on path
328 * The handling of '.' is a bit weird, but we need it that way,
329 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
330 * this is the only way to differentiate. (mainly hypertrm.exe)
332 * RETURNS
333 * the module handle if found
334 * 0 if not
336 HMODULE32 MODULE_FindModule32(
337 PDB32* process, /* [in] process in which to find the library */
338 LPCSTR path /* [in] pathname of module/library to be found */
340 LPSTR filename;
341 LPSTR dotptr;
342 WINE_MODREF *wm;
344 if (!process)
345 return 0;
346 if (!(filename = strrchr( path, '\\' )))
347 filename = HEAP_strdupA(process->heap,0,path);
348 else
349 filename = HEAP_strdupA(process->heap,0,filename+1);
350 dotptr=strrchr(filename,'.');
352 for (wm=process->modref_list;wm;wm=wm->next) {
353 LPSTR xmodname,xdotptr;
355 assert (wm->modname);
356 xmodname = HEAP_strdupA(process->heap,0,wm->modname);
357 xdotptr=strrchr(xmodname,'.');
358 if ( (xdotptr && !dotptr) ||
359 (!xdotptr && dotptr)
361 if (dotptr) *dotptr = '\0';
362 if (xdotptr) *xdotptr = '\0';
364 if (!strcasecmp( filename, xmodname)) {
365 HeapFree(process->heap,0,filename);
366 HeapFree(process->heap,0,xmodname);
367 return wm->module;
369 if (dotptr) *dotptr='.';
370 /* FIXME: add paths, shortname */
371 HeapFree(process->heap,0,xmodname);
373 /* if that fails, try looking for the filename... */
374 for (wm=process->modref_list;wm;wm=wm->next) {
375 LPSTR xlname,xdotptr;
377 assert (wm->longname);
378 xlname = strrchr(wm->longname,'\\');
379 if (!xlname)
380 xlname = wm->longname;
381 else
382 xlname++;
383 xlname = HEAP_strdupA(process->heap,0,xlname);
384 xdotptr=strrchr(xlname,'.');
385 if ( (xdotptr && !dotptr) ||
386 (!xdotptr && dotptr)
388 if (dotptr) *dotptr = '\0';
389 if (xdotptr) *xdotptr = '\0';
391 if (!strcasecmp( filename, xlname)) {
392 HeapFree(process->heap,0,filename);
393 HeapFree(process->heap,0,xlname);
394 return wm->module;
396 if (dotptr) *dotptr='.';
397 /* FIXME: add paths, shortname */
398 HeapFree(process->heap,0,xlname);
400 HeapFree(process->heap,0,filename);
401 return 0;
406 /**********************************************************************
407 * NE_CreateProcess
409 static HINSTANCE16 NE_CreateProcess( LPCSTR name, LPCSTR cmd_line, LPCSTR env,
410 LPSTARTUPINFO32A startup,
411 LPPROCESS_INFORMATION info )
413 HINSTANCE16 hInstance, hPrevInstance;
414 NE_MODULE *pModule;
416 /* Load module */
418 hInstance = NE_LoadModule( name, &hPrevInstance, TRUE, FALSE );
419 if (hInstance < 32) return hInstance;
421 if ( !(pModule = NE_GetPtr(hInstance))
422 || (pModule->flags & NE_FFLAGS_LIBMODULE))
424 /* FIXME: cleanup */
425 return 11;
428 /* Create a task for this instance */
430 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
432 PROCESS_Create( pModule, cmd_line, env, hInstance,
433 hPrevInstance, startup, info );
435 return hInstance;
439 /**********************************************************************
440 * LoadModule16 (KERNEL.45)
442 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
444 LOADPARAMS *params;
445 LPSTR cmd_line, new_cmd_line;
446 LPCVOID env = NULL;
447 STARTUPINFO32A startup;
448 PROCESS_INFORMATION info;
449 HINSTANCE16 hInstance, hPrevInstance;
450 NE_MODULE *pModule;
451 PDB32 *pdb;
453 /* Load module */
455 if (!paramBlock || (paramBlock == (LPVOID)-1))
456 return LoadLibrary16( name );
458 hInstance = NE_LoadModule( name, &hPrevInstance, FALSE, FALSE );
459 if ( hInstance < 32 || !(pModule = NE_GetPtr(hInstance))
460 || (pModule->flags & NE_FFLAGS_LIBMODULE))
461 return hInstance;
463 /* Create a task for this instance */
465 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
467 params = (LOADPARAMS *)paramBlock;
468 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
469 if (!cmd_line) cmd_line = "";
470 else if (*cmd_line) cmd_line++; /* skip the length byte */
472 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
473 strlen(cmd_line)+strlen(name)+2 )))
474 return 0;
475 strcpy( new_cmd_line, name );
476 strcat( new_cmd_line, " " );
477 strcat( new_cmd_line, cmd_line );
479 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
481 memset( &info, '\0', sizeof(info) );
482 memset( &startup, '\0', sizeof(startup) );
483 startup.cb = sizeof(startup);
484 if (params->showCmd)
486 startup.dwFlags = STARTF_USESHOWWINDOW;
487 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
490 pdb = PROCESS_Create( pModule, new_cmd_line, env,
491 hInstance, hPrevInstance, &startup, &info );
493 CloseHandle( info.hThread );
494 CloseHandle( info.hProcess );
496 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
497 HeapFree( GetProcessHeap(), 0, new_cmd_line );
499 /* Start task */
501 if (pdb) TASK_StartTask( pdb->task );
503 return hInstance;
506 /**********************************************************************
507 * LoadModule32 (KERNEL32.499)
509 HINSTANCE32 WINAPI LoadModule32( LPCSTR name, LPVOID paramBlock )
511 LOADPARAMS32 *params = (LOADPARAMS32 *)paramBlock;
512 PROCESS_INFORMATION info;
513 STARTUPINFO32A startup;
514 HINSTANCE32 hInstance;
515 PDB32 *pdb;
516 TDB *tdb;
518 memset( &startup, '\0', sizeof(startup) );
519 startup.cb = sizeof(startup);
520 startup.dwFlags = STARTF_USESHOWWINDOW;
521 startup.wShowWindow = params->lpCmdShow? params->lpCmdShow[1] : 0;
523 if (!CreateProcess32A( name, params->lpCmdLine,
524 NULL, NULL, FALSE, 0, params->lpEnvAddress,
525 NULL, &startup, &info ))
526 return GetLastError(); /* guaranteed to be < 32 */
528 /* Get 16-bit hInstance/hTask from process */
529 pdb = PROCESS_IdToPDB( info.dwProcessId );
530 tdb = pdb? (TDB *)GlobalLock16( pdb->task ) : NULL;
531 hInstance = tdb && tdb->hInstance? tdb->hInstance : pdb? pdb->task : 0;
533 /* Close off the handles */
534 CloseHandle( info.hThread );
535 CloseHandle( info.hProcess );
537 return hInstance;
540 /**********************************************************************
541 * CreateProcess32A (KERNEL32.171)
543 BOOL32 WINAPI CreateProcess32A( LPCSTR lpApplicationName, LPSTR lpCommandLine,
544 LPSECURITY_ATTRIBUTES lpProcessAttributes,
545 LPSECURITY_ATTRIBUTES lpThreadAttributes,
546 BOOL32 bInheritHandles, DWORD dwCreationFlags,
547 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
548 LPSTARTUPINFO32A lpStartupInfo,
549 LPPROCESS_INFORMATION lpProcessInfo )
551 HINSTANCE16 hInstance;
552 LPCSTR cmdline;
553 PDB32 *pdb;
554 char name[256];
556 /* Get name and command line */
558 if (!lpApplicationName && !lpCommandLine)
560 SetLastError( ERROR_FILE_NOT_FOUND );
561 return FALSE;
564 cmdline = lpCommandLine? lpCommandLine : lpApplicationName;
566 if (lpApplicationName)
567 lstrcpyn32A(name, lpApplicationName, sizeof(name) - 4);
568 else {
569 char *ptr;
570 int len;
572 /* Take care off .exes with spaces in their names */
573 ptr = strchr(lpCommandLine, ' ');
574 do {
575 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
576 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
577 lstrcpyn32A(name, lpCommandLine, len);
578 if (!strchr(name, '\\') && !strchr(name, '.'))
579 strcat(name, ".exe");
580 if (GetFileAttributes32A(name)!=-1)
581 break;
582 /* if there is a space and no file found yet, include the word
583 * up to the next space too. If there is no next space, just
584 * use the first word.
586 if (ptr) {
587 ptr = strchr(ptr+1, ' ');
588 } else {
589 ptr = strchr(lpCommandLine, ' ');
590 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
591 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
592 lstrcpyn32A(name, lpCommandLine, len);
593 break;
595 } while (1);
598 if (!strchr(name, '\\') && !strchr(name, '.'))
599 strcat(name, ".exe");
602 /* Warn if unsupported features are used */
604 if (lpProcessAttributes)
605 FIXME(module, "(%s,...): lpProcessAttributes ignored\n", name);
606 if (lpThreadAttributes)
607 FIXME(module, "(%s,...): lpThreadAttributes ignored\n", name);
608 if (bInheritHandles)
609 FIXME(module, "(%s,...): bInheritHandles ignored\n", name);
610 if (dwCreationFlags & DEBUG_PROCESS)
611 FIXME(module, "(%s,...): DEBUG_PROCESS ignored\n", name);
612 if (dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
613 FIXME(module, "(%s,...): DEBUG_ONLY_THIS_PROCESS ignored\n", name);
614 if (dwCreationFlags & CREATE_SUSPENDED)
615 FIXME(module, "(%s,...): CREATE_SUSPENDED ignored\n", name);
616 if (dwCreationFlags & DETACHED_PROCESS)
617 FIXME(module, "(%s,...): DETACHED_PROCESS ignored\n", name);
618 if (dwCreationFlags & CREATE_NEW_CONSOLE)
619 FIXME(module, "(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
620 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
621 FIXME(module, "(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
622 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
623 FIXME(module, "(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
624 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
625 FIXME(module, "(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
626 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
627 FIXME(module, "(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
628 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
629 FIXME(module, "(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
630 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
631 FIXME(module, "(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
632 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
633 FIXME(module, "(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
634 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
635 FIXME(module, "(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
636 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
637 FIXME(module, "(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
638 if (dwCreationFlags & CREATE_NO_WINDOW)
639 FIXME(module, "(%s,...): CREATE_NO_WINDOW ignored\n", name);
640 if (dwCreationFlags & PROFILE_USER)
641 FIXME(module, "(%s,...): PROFILE_USER ignored\n", name);
642 if (dwCreationFlags & PROFILE_KERNEL)
643 FIXME(module, "(%s,...): PROFILE_KERNEL ignored\n", name);
644 if (dwCreationFlags & PROFILE_SERVER)
645 FIXME(module, "(%s,...): PROFILE_SERVER ignored\n", name);
646 if (lpCurrentDirectory)
647 FIXME(module, "(%s,...): lpCurrentDirectory %s ignored\n",
648 name, lpCurrentDirectory);
649 if (lpStartupInfo->lpDesktop)
650 FIXME(module, "(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
651 name, lpStartupInfo->lpDesktop);
652 if (lpStartupInfo->lpTitle)
653 FIXME(module, "(%s,...): lpStartupInfo->lpTitle %s ignored\n",
654 name, lpStartupInfo->lpTitle);
655 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
656 FIXME(module, "(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
657 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
658 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
659 FIXME(module, "(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
660 name, lpStartupInfo->dwFillAttribute);
661 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
662 FIXME(module, "(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
663 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
664 FIXME(module, "(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
665 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
666 FIXME(module, "(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
667 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
668 FIXME(module, "(%s,...): STARTF_USEHOTKEY ignored\n", name);
671 /* Try NE module */
672 hInstance = NE_CreateProcess( name, cmdline, lpEnvironment,
673 lpStartupInfo, lpProcessInfo );
675 /* Try PE module */
676 if (hInstance == 21)
677 hInstance = PE_CreateProcess( name, cmdline, lpEnvironment,
678 lpStartupInfo, lpProcessInfo );
680 /* Try DOS module */
681 if (hInstance == 11)
682 hInstance = MZ_CreateProcess( name, cmdline, lpEnvironment,
683 lpStartupInfo, lpProcessInfo );
685 if (hInstance < 32)
687 SetLastError( hInstance );
688 return FALSE;
691 /* Get hTask from process and start the task */
692 pdb = PROCESS_IdToPDB( lpProcessInfo->dwProcessId );
693 if (pdb) TASK_StartTask( pdb->task );
695 return TRUE;
698 /**********************************************************************
699 * CreateProcess32W (KERNEL32.172)
700 * NOTES
701 * lpReserved is not converted
703 BOOL32 WINAPI CreateProcess32W( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
704 LPSECURITY_ATTRIBUTES lpProcessAttributes,
705 LPSECURITY_ATTRIBUTES lpThreadAttributes,
706 BOOL32 bInheritHandles, DWORD dwCreationFlags,
707 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
708 LPSTARTUPINFO32W lpStartupInfo,
709 LPPROCESS_INFORMATION lpProcessInfo )
710 { BOOL32 ret;
711 STARTUPINFO32A StartupInfoA;
713 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
714 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
715 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
717 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFO32A));
718 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
719 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
721 TRACE(win32, "(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
723 if (lpStartupInfo->lpReserved)
724 FIXME(win32,"StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
726 ret = CreateProcess32A( lpApplicationNameA, lpCommandLineA,
727 lpProcessAttributes, lpThreadAttributes,
728 bInheritHandles, dwCreationFlags,
729 lpEnvironment, lpCurrentDirectoryA,
730 &StartupInfoA, lpProcessInfo );
732 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
733 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
734 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
735 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
737 return ret;
740 /***********************************************************************
741 * GetModuleHandle (KERNEL32.237)
743 HMODULE32 WINAPI GetModuleHandle32A(LPCSTR module)
745 if (module == NULL)
746 return PROCESS_Current()->exe_modref->module;
747 else
748 return MODULE_FindModule32(PROCESS_Current(),module);
751 HMODULE32 WINAPI GetModuleHandle32W(LPCWSTR module)
753 HMODULE32 hModule;
754 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
755 hModule = GetModuleHandle32A( modulea );
756 HeapFree( GetProcessHeap(), 0, modulea );
757 return hModule;
761 /***********************************************************************
762 * GetModuleFileName32A (KERNEL32.235)
764 DWORD WINAPI GetModuleFileName32A(
765 HMODULE32 hModule, /* [in] module handle (32bit) */
766 LPSTR lpFileName, /* [out] filenamebuffer */
767 DWORD size /* [in] size of filenamebuffer */
768 ) {
769 WINE_MODREF *wm = MODULE32_LookupHMODULE(PROCESS_Current(),hModule);
771 if (!wm) /* can happen on start up or the like */
772 return 0;
774 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
775 lstrcpyn32A( lpFileName, wm->longname, size );
776 else
777 lstrcpyn32A( lpFileName, wm->shortname, size );
779 TRACE(module, "%s\n", lpFileName );
780 return strlen(lpFileName);
784 /***********************************************************************
785 * GetModuleFileName32W (KERNEL32.236)
787 DWORD WINAPI GetModuleFileName32W( HMODULE32 hModule, LPWSTR lpFileName,
788 DWORD size )
790 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
791 DWORD res = GetModuleFileName32A( hModule, fnA, size );
792 lstrcpynAtoW( lpFileName, fnA, size );
793 HeapFree( GetProcessHeap(), 0, fnA );
794 return res;
798 /***********************************************************************
799 * LoadLibraryEx32W (KERNEL.513)
800 * FIXME
802 HMODULE32 WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
803 DWORD flags )
805 TRACE(module,"(%s,%d,%08lx)\n",libname,hf,flags);
806 return LoadLibraryEx32A(libname, hf,flags);
809 /***********************************************************************
810 * LoadLibraryEx32A (KERNEL32)
812 HMODULE32 WINAPI LoadLibraryEx32A(LPCSTR libname,HFILE32 hfile,DWORD flags)
814 HMODULE32 hmod;
815 hmod = MODULE_LoadLibraryEx32A(libname,PROCESS_Current(),hfile,flags);
817 /* at least call not the dllmain...*/
818 if ( DONT_RESOLVE_DLL_REFERENCES==flags || LOAD_LIBRARY_AS_DATAFILE==flags )
819 { FIXME(module,"flag not properly supported %lx\n", flags);
820 return hmod;
823 /* initialize DLL just loaded */
824 if ( hmod >= 32 )
825 MODULE_InitializeDLLs( PROCESS_Current(), hmod,
826 DLL_PROCESS_ATTACH, (LPVOID)-1 );
828 return hmod;
831 HMODULE32 MODULE_LoadLibraryEx32A(LPCSTR libname,PDB32*process,HFILE32 hfile,DWORD flags)
833 HMODULE32 hmod;
835 hmod = ELF_LoadLibraryEx32A(libname,process,hfile,flags);
836 if (hmod)
837 return hmod; /* already initialized for ELF */
839 hmod = PE_LoadLibraryEx32A(libname,process,hfile,flags);
840 if (hmod < 32) {
841 char buffer[256];
843 strcpy( buffer, libname );
844 strcat( buffer, ".dll" );
845 hmod = PE_LoadLibraryEx32A(buffer,process,hfile,flags);
847 return hmod;
850 /***********************************************************************
851 * LoadLibraryA (KERNEL32)
853 HMODULE32 WINAPI LoadLibrary32A(LPCSTR libname) {
854 return LoadLibraryEx32A(libname,0,0);
857 /***********************************************************************
858 * LoadLibraryW (KERNEL32)
860 HMODULE32 WINAPI LoadLibrary32W(LPCWSTR libnameW)
862 return LoadLibraryEx32W(libnameW,0,0);
865 /***********************************************************************
866 * LoadLibraryExW (KERNEL32)
868 HMODULE32 WINAPI LoadLibraryEx32W(LPCWSTR libnameW,HFILE32 hfile,DWORD flags)
870 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
871 HMODULE32 ret = LoadLibraryEx32A( libnameA , hfile, flags );
873 HeapFree( GetProcessHeap(), 0, libnameA );
874 return ret;
877 /***********************************************************************
878 * FreeLibrary
880 BOOL32 WINAPI FreeLibrary32(HINSTANCE32 hLibModule)
882 FIXME(module,"(0x%08x): stub\n", hLibModule);
883 return TRUE; /* FIXME */
887 /***********************************************************************
888 * PrivateLoadLibrary (KERNEL32)
890 * FIXME: rough guesswork, don't know what "Private" means
892 HINSTANCE32 WINAPI PrivateLoadLibrary(LPCSTR libname)
894 return (HINSTANCE32)LoadLibrary16(libname);
899 /***********************************************************************
900 * PrivateFreeLibrary (KERNEL32)
902 * FIXME: rough guesswork, don't know what "Private" means
904 void WINAPI PrivateFreeLibrary(HINSTANCE32 handle)
906 FreeLibrary16((HINSTANCE16)handle);
910 /***********************************************************************
911 * WinExec16 (KERNEL.166)
913 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
915 return WinExec32( lpCmdLine, nCmdShow );
919 /***********************************************************************
920 * WinExec32 (KERNEL32.566)
922 HINSTANCE32 WINAPI WinExec32( LPCSTR lpCmdLine, UINT32 nCmdShow )
924 HINSTANCE32 handle = 2;
925 char *p, filename[256];
926 int spacelimit = 0, exhausted = 0;
927 LOADPARAMS32 params;
928 UINT16 paramCmdShow[2];
930 if (!lpCmdLine)
931 return 2; /* File not found */
933 /* Set up LOADPARAMS32 buffer for LoadModule32 */
935 memset( &params, '\0', sizeof(params) );
936 params.lpCmdLine = (LPSTR)lpCmdLine;
937 params.lpCmdShow = paramCmdShow;
938 params.lpCmdShow[0] = 2;
939 params.lpCmdShow[1] = nCmdShow;
942 /* Keep trying to load a file by trying different filenames; e.g.,
943 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
944 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
945 no args */
947 while(!exhausted && handle == 2) {
948 int spacecount = 0;
950 /* Build the filename and command-line */
952 lstrcpyn32A(filename, lpCmdLine,
953 sizeof(filename) - 4 /* for extension */);
955 /* Keep grabbing characters until end-of-string, tab, or until the
956 number of spaces is greater than the spacelimit */
958 for (p = filename; ; p++) {
959 if(*p == ' ') {
960 ++spacecount;
961 if(spacecount > spacelimit) {
962 ++spacelimit;
963 break;
967 if(*p == '\0' || *p == '\t') {
968 exhausted = 1;
969 break;
973 *p = '\0';
975 /* Now load the executable file */
977 if (!__winelib)
979 handle = LoadModule32( filename, &params );
980 if (handle == 2) /* file not found */
982 /* Check that the original file name did not have a suffix */
983 p = strrchr(filename, '.');
984 /* if there is a '.', check if either \ OR / follow */
985 if (!p || strchr(p, '/') || strchr(p, '\\'))
987 p = filename + strlen(filename);
988 strcpy( p, ".exe" );
989 handle = LoadModule32( filename, &params );
990 *p = '\0'; /* Remove extension */
994 else
995 handle = 2; /* file not found */
997 if (handle < 32)
999 /* Try to start it as a unix program */
1000 if (!fork())
1002 /* Child process */
1003 DOS_FULL_NAME full_name;
1004 const char *unixfilename = NULL;
1005 const char *argv[256], **argptr;
1006 int iconic = (nCmdShow == SW_SHOWMINIMIZED ||
1007 nCmdShow == SW_SHOWMINNOACTIVE);
1009 THREAD_InitDone = FALSE; /* we didn't init this process */
1010 /* get unixfilename */
1011 if (strchr(filename, '/') ||
1012 strchr(filename, ':') ||
1013 strchr(filename, '\\'))
1015 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
1016 unixfilename = full_name.long_name;
1018 else unixfilename = filename;
1020 if (unixfilename)
1022 /* build argv */
1023 argptr = argv;
1024 if (iconic) *argptr++ = "-iconic";
1025 *argptr++ = unixfilename;
1026 p = strdup(lpCmdLine);
1027 while (1)
1029 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
1030 if (!*p) break;
1031 *argptr++ = p;
1032 while (*p && *p != ' ' && *p != '\t') p++;
1034 *argptr++ = 0;
1036 /* Execute */
1037 execvp(argv[0], (char**)argv);
1040 /* Failed ! */
1042 if (__winelib)
1044 /* build argv */
1045 argptr = argv;
1046 *argptr++ = "wine";
1047 if (iconic) *argptr++ = "-iconic";
1048 *argptr++ = lpCmdLine;
1049 *argptr++ = 0;
1051 /* Execute */
1052 execvp(argv[0] , (char**)argv);
1054 /* Failed ! */
1055 MSG("WinExec: can't exec 'wine %s'\n",
1056 lpCmdLine);
1058 exit(1);
1061 } /* while (!exhausted && handle < 32) */
1063 return handle;
1067 /***********************************************************************
1068 * WIN32_GetProcAddress16 (KERNEL32.36)
1069 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1071 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hModule, LPCSTR name )
1073 WORD ordinal;
1074 FARPROC16 ret;
1076 if (!hModule) {
1077 WARN(module,"hModule may not be 0!\n");
1078 return (FARPROC16)0;
1080 if (HIWORD(hModule))
1082 WARN( module, "hModule is Win32 handle (%08x)\n", hModule );
1083 return (FARPROC16)0;
1085 hModule = GetExePtr( hModule );
1086 if (HIWORD(name)) {
1087 ordinal = NE_GetOrdinal( hModule, name );
1088 TRACE(module, "%04x '%s'\n",
1089 hModule, name );
1090 } else {
1091 ordinal = LOWORD(name);
1092 TRACE(module, "%04x %04x\n",
1093 hModule, ordinal );
1095 if (!ordinal) return (FARPROC16)0;
1096 ret = NE_GetEntryPoint( hModule, ordinal );
1097 TRACE(module,"returning %08x\n",(UINT32)ret);
1098 return ret;
1101 /***********************************************************************
1102 * GetProcAddress16 (KERNEL.50)
1104 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1106 WORD ordinal;
1107 FARPROC16 ret;
1109 if (!hModule) hModule = GetCurrentTask();
1110 hModule = GetExePtr( hModule );
1112 if (HIWORD(name) != 0)
1114 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1115 TRACE(module, "%04x '%s'\n",
1116 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1118 else
1120 ordinal = LOWORD(name);
1121 TRACE(module, "%04x %04x\n",
1122 hModule, ordinal );
1124 if (!ordinal) return (FARPROC16)0;
1126 ret = NE_GetEntryPoint( hModule, ordinal );
1128 TRACE(module, "returning %08x\n", (UINT32)ret );
1129 return ret;
1133 /***********************************************************************
1134 * GetProcAddress32 (KERNEL32.257)
1136 FARPROC32 WINAPI GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1138 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, TRUE );
1141 /***********************************************************************
1142 * WIN16_GetProcAddress32 (KERNEL.453)
1144 FARPROC32 WINAPI WIN16_GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1146 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, FALSE );
1149 /***********************************************************************
1150 * MODULE_GetProcAddress32 (internal)
1152 FARPROC32 MODULE_GetProcAddress32(
1153 PDB32 *process, /* [in] process context */
1154 HMODULE32 hModule, /* [in] current module handle */
1155 LPCSTR function, /* [in] function to be looked up */
1156 BOOL32 snoop )
1158 WINE_MODREF *wm = MODULE32_LookupHMODULE(process,hModule);
1160 if (HIWORD(function))
1161 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
1162 else
1163 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
1164 if (!wm)
1165 return (FARPROC32)0;
1166 switch (wm->type)
1168 case MODULE32_PE:
1169 return PE_FindExportedFunction( process, wm, function, snoop );
1170 case MODULE32_ELF:
1171 return ELF_FindExportedFunction( process, wm, function);
1172 default:
1173 ERR(module,"wine_modref type %d not handled.\n",wm->type);
1174 return (FARPROC32)0;
1179 /***********************************************************************
1180 * RtlImageNtHeaders (NTDLL)
1182 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE32 hModule)
1184 /* basically:
1185 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1186 * but we could get HMODULE16 or the like (think builtin modules)
1189 WINE_MODREF *wm = MODULE32_LookupHMODULE( PROCESS_Current(), hModule );
1190 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1191 return PE_HEADER(wm->module);
1195 /***************************************************************************
1196 * HasGPHandler (KERNEL.338)
1199 #pragma pack(1)
1200 typedef struct _GPHANDLERDEF
1202 WORD selector;
1203 WORD rangeStart;
1204 WORD rangeEnd;
1205 WORD handler;
1206 } GPHANDLERDEF;
1207 #pragma pack(4)
1209 SEGPTR WINAPI HasGPHandler( SEGPTR address )
1211 HMODULE16 hModule;
1212 int gpOrdinal;
1213 SEGPTR gpPtr;
1214 GPHANDLERDEF *gpHandler;
1216 if ( (hModule = FarGetOwner( SELECTOROF(address) )) != 0
1217 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1218 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1219 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1220 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1222 while (gpHandler->selector)
1224 if ( SELECTOROF(address) == gpHandler->selector
1225 && OFFSETOF(address) >= gpHandler->rangeStart
1226 && OFFSETOF(address) < gpHandler->rangeEnd )
1227 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1228 gpHandler->handler );
1229 gpHandler++;
1233 return 0;