Moves more stuff from windows.h.
[wine/testsucceed.git] / loader / module.c
blob735b7a79d7760447c1f2669cf3da9306ae401186
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 "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 BOOL32 inherit, 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, inherit, 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, TRUE, &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 (dwCreationFlags & DEBUG_PROCESS)
609 FIXME(module, "(%s,...): DEBUG_PROCESS ignored\n", name);
610 if (dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
611 FIXME(module, "(%s,...): DEBUG_ONLY_THIS_PROCESS ignored\n", name);
612 if (dwCreationFlags & CREATE_SUSPENDED)
613 FIXME(module, "(%s,...): CREATE_SUSPENDED ignored\n", name);
614 if (dwCreationFlags & DETACHED_PROCESS)
615 FIXME(module, "(%s,...): DETACHED_PROCESS ignored\n", name);
616 if (dwCreationFlags & CREATE_NEW_CONSOLE)
617 FIXME(module, "(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
618 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
619 FIXME(module, "(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
620 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
621 FIXME(module, "(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
622 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
623 FIXME(module, "(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
624 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
625 FIXME(module, "(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
626 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
627 FIXME(module, "(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
628 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
629 FIXME(module, "(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
630 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
631 FIXME(module, "(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
632 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
633 FIXME(module, "(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
634 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
635 FIXME(module, "(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
636 if (dwCreationFlags & CREATE_NO_WINDOW)
637 FIXME(module, "(%s,...): CREATE_NO_WINDOW ignored\n", name);
638 if (dwCreationFlags & PROFILE_USER)
639 FIXME(module, "(%s,...): PROFILE_USER ignored\n", name);
640 if (dwCreationFlags & PROFILE_KERNEL)
641 FIXME(module, "(%s,...): PROFILE_KERNEL ignored\n", name);
642 if (dwCreationFlags & PROFILE_SERVER)
643 FIXME(module, "(%s,...): PROFILE_SERVER ignored\n", name);
644 if (lpCurrentDirectory)
645 FIXME(module, "(%s,...): lpCurrentDirectory %s ignored\n",
646 name, lpCurrentDirectory);
647 if (lpStartupInfo->lpDesktop)
648 FIXME(module, "(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
649 name, lpStartupInfo->lpDesktop);
650 if (lpStartupInfo->lpTitle)
651 FIXME(module, "(%s,...): lpStartupInfo->lpTitle %s ignored\n",
652 name, lpStartupInfo->lpTitle);
653 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
654 FIXME(module, "(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
655 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
656 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
657 FIXME(module, "(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
658 name, lpStartupInfo->dwFillAttribute);
659 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
660 FIXME(module, "(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
661 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
662 FIXME(module, "(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
663 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
664 FIXME(module, "(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
665 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
666 FIXME(module, "(%s,...): STARTF_USEHOTKEY ignored\n", name);
669 /* Try NE module */
670 hInstance = NE_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
671 lpStartupInfo, lpProcessInfo );
673 /* Try PE module */
674 if (hInstance == 21)
675 hInstance = PE_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
676 lpStartupInfo, lpProcessInfo );
678 /* Try DOS module */
679 if (hInstance == 11)
680 hInstance = MZ_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
681 lpStartupInfo, lpProcessInfo );
683 if (hInstance < 32)
685 SetLastError( hInstance );
686 return FALSE;
689 /* Get hTask from process and start the task */
690 pdb = PROCESS_IdToPDB( lpProcessInfo->dwProcessId );
691 if (pdb) TASK_StartTask( pdb->task );
693 return TRUE;
696 /**********************************************************************
697 * CreateProcess32W (KERNEL32.172)
698 * NOTES
699 * lpReserved is not converted
701 BOOL32 WINAPI CreateProcess32W( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
702 LPSECURITY_ATTRIBUTES lpProcessAttributes,
703 LPSECURITY_ATTRIBUTES lpThreadAttributes,
704 BOOL32 bInheritHandles, DWORD dwCreationFlags,
705 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
706 LPSTARTUPINFO32W lpStartupInfo,
707 LPPROCESS_INFORMATION lpProcessInfo )
708 { BOOL32 ret;
709 STARTUPINFO32A StartupInfoA;
711 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
712 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
713 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
715 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFO32A));
716 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
717 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
719 TRACE(win32, "(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
721 if (lpStartupInfo->lpReserved)
722 FIXME(win32,"StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
724 ret = CreateProcess32A( lpApplicationNameA, lpCommandLineA,
725 lpProcessAttributes, lpThreadAttributes,
726 bInheritHandles, dwCreationFlags,
727 lpEnvironment, lpCurrentDirectoryA,
728 &StartupInfoA, lpProcessInfo );
730 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
731 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
732 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
733 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
735 return ret;
738 /***********************************************************************
739 * GetModuleHandle (KERNEL32.237)
741 HMODULE32 WINAPI GetModuleHandle32A(LPCSTR module)
743 if (module == NULL)
744 return PROCESS_Current()->exe_modref->module;
745 else
746 return MODULE_FindModule32(PROCESS_Current(),module);
749 HMODULE32 WINAPI GetModuleHandle32W(LPCWSTR module)
751 HMODULE32 hModule;
752 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
753 hModule = GetModuleHandle32A( modulea );
754 HeapFree( GetProcessHeap(), 0, modulea );
755 return hModule;
759 /***********************************************************************
760 * GetModuleFileName32A (KERNEL32.235)
762 DWORD WINAPI GetModuleFileName32A(
763 HMODULE32 hModule, /* [in] module handle (32bit) */
764 LPSTR lpFileName, /* [out] filenamebuffer */
765 DWORD size /* [in] size of filenamebuffer */
766 ) {
767 WINE_MODREF *wm = MODULE32_LookupHMODULE(PROCESS_Current(),hModule);
769 if (!wm) /* can happen on start up or the like */
770 return 0;
772 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
773 lstrcpyn32A( lpFileName, wm->longname, size );
774 else
775 lstrcpyn32A( lpFileName, wm->shortname, size );
777 TRACE(module, "%s\n", lpFileName );
778 return strlen(lpFileName);
782 /***********************************************************************
783 * GetModuleFileName32W (KERNEL32.236)
785 DWORD WINAPI GetModuleFileName32W( HMODULE32 hModule, LPWSTR lpFileName,
786 DWORD size )
788 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
789 DWORD res = GetModuleFileName32A( hModule, fnA, size );
790 lstrcpynAtoW( lpFileName, fnA, size );
791 HeapFree( GetProcessHeap(), 0, fnA );
792 return res;
796 /***********************************************************************
797 * LoadLibraryEx32W (KERNEL.513)
798 * FIXME
800 HMODULE32 WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
801 DWORD flags )
803 TRACE(module,"(%s,%d,%08lx)\n",libname,hf,flags);
804 return LoadLibraryEx32A(libname, hf,flags);
807 /***********************************************************************
808 * LoadLibraryEx32A (KERNEL32)
810 HMODULE32 WINAPI LoadLibraryEx32A(LPCSTR libname,HFILE32 hfile,DWORD flags)
812 HMODULE32 hmod;
813 hmod = MODULE_LoadLibraryEx32A(libname,PROCESS_Current(),hfile,flags);
815 /* at least call not the dllmain...*/
816 if ( DONT_RESOLVE_DLL_REFERENCES==flags || LOAD_LIBRARY_AS_DATAFILE==flags )
817 { FIXME(module,"flag not properly supported %lx\n", flags);
818 return hmod;
821 /* initialize DLL just loaded */
822 if ( hmod >= 32 )
823 MODULE_InitializeDLLs( PROCESS_Current(), hmod,
824 DLL_PROCESS_ATTACH, (LPVOID)-1 );
826 return hmod;
829 HMODULE32 MODULE_LoadLibraryEx32A(LPCSTR libname,PDB32*process,HFILE32 hfile,DWORD flags)
831 HMODULE32 hmod;
833 hmod = ELF_LoadLibraryEx32A(libname,process,hfile,flags);
834 if (hmod) return hmod;
836 hmod = PE_LoadLibraryEx32A(libname,process,hfile,flags);
837 return hmod;
840 /***********************************************************************
841 * LoadLibraryA (KERNEL32)
843 HMODULE32 WINAPI LoadLibrary32A(LPCSTR libname) {
844 return LoadLibraryEx32A(libname,0,0);
847 /***********************************************************************
848 * LoadLibraryW (KERNEL32)
850 HMODULE32 WINAPI LoadLibrary32W(LPCWSTR libnameW)
852 return LoadLibraryEx32W(libnameW,0,0);
855 /***********************************************************************
856 * LoadLibraryExW (KERNEL32)
858 HMODULE32 WINAPI LoadLibraryEx32W(LPCWSTR libnameW,HFILE32 hfile,DWORD flags)
860 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
861 HMODULE32 ret = LoadLibraryEx32A( libnameA , hfile, flags );
863 HeapFree( GetProcessHeap(), 0, libnameA );
864 return ret;
867 /***********************************************************************
868 * FreeLibrary
870 BOOL32 WINAPI FreeLibrary32(HINSTANCE32 hLibModule)
872 FIXME(module,"(0x%08x): stub\n", hLibModule);
873 return TRUE; /* FIXME */
877 /***********************************************************************
878 * PrivateLoadLibrary (KERNEL32)
880 * FIXME: rough guesswork, don't know what "Private" means
882 HINSTANCE32 WINAPI PrivateLoadLibrary(LPCSTR libname)
884 return (HINSTANCE32)LoadLibrary16(libname);
889 /***********************************************************************
890 * PrivateFreeLibrary (KERNEL32)
892 * FIXME: rough guesswork, don't know what "Private" means
894 void WINAPI PrivateFreeLibrary(HINSTANCE32 handle)
896 FreeLibrary16((HINSTANCE16)handle);
900 /***********************************************************************
901 * WinExec16 (KERNEL.166)
903 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
905 return WinExec32( lpCmdLine, nCmdShow );
909 /***********************************************************************
910 * WinExec32 (KERNEL32.566)
912 HINSTANCE32 WINAPI WinExec32( LPCSTR lpCmdLine, UINT32 nCmdShow )
914 HINSTANCE32 handle = 2;
915 char *p, filename[256];
916 int spacelimit = 0, exhausted = 0;
917 LOADPARAMS32 params;
918 UINT16 paramCmdShow[2];
920 if (!lpCmdLine)
921 return 2; /* File not found */
923 /* Set up LOADPARAMS32 buffer for LoadModule32 */
925 memset( &params, '\0', sizeof(params) );
926 params.lpCmdLine = (LPSTR)lpCmdLine;
927 params.lpCmdShow = paramCmdShow;
928 params.lpCmdShow[0] = 2;
929 params.lpCmdShow[1] = nCmdShow;
932 /* Keep trying to load a file by trying different filenames; e.g.,
933 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
934 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
935 no args */
937 while(!exhausted && handle == 2) {
938 int spacecount = 0;
940 /* Build the filename and command-line */
942 lstrcpyn32A(filename, lpCmdLine,
943 sizeof(filename) - 4 /* for extension */);
945 /* Keep grabbing characters until end-of-string, tab, or until the
946 number of spaces is greater than the spacelimit */
948 for (p = filename; ; p++) {
949 if(*p == ' ') {
950 ++spacecount;
951 if(spacecount > spacelimit) {
952 ++spacelimit;
953 break;
957 if(*p == '\0' || *p == '\t') {
958 exhausted = 1;
959 break;
963 *p = '\0';
965 /* Now load the executable file */
967 if (!__winelib)
969 handle = LoadModule32( filename, &params );
970 if (handle == 2) /* file not found */
972 /* Check that the original file name did not have a suffix */
973 p = strrchr(filename, '.');
974 /* if there is a '.', check if either \ OR / follow */
975 if (!p || strchr(p, '/') || strchr(p, '\\'))
977 p = filename + strlen(filename);
978 strcpy( p, ".exe" );
979 handle = LoadModule32( filename, &params );
980 *p = '\0'; /* Remove extension */
984 else
985 handle = 2; /* file not found */
987 if (handle < 32)
989 /* Try to start it as a unix program */
990 if (!fork())
992 /* Child process */
993 DOS_FULL_NAME full_name;
994 const char *unixfilename = NULL;
995 const char *argv[256], **argptr;
996 int iconic = (nCmdShow == SW_SHOWMINIMIZED ||
997 nCmdShow == SW_SHOWMINNOACTIVE);
999 THREAD_InitDone = FALSE; /* we didn't init this process */
1000 /* get unixfilename */
1001 if (strchr(filename, '/') ||
1002 strchr(filename, ':') ||
1003 strchr(filename, '\\'))
1005 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
1006 unixfilename = full_name.long_name;
1008 else unixfilename = filename;
1010 if (unixfilename)
1012 /* build argv */
1013 argptr = argv;
1014 if (iconic) *argptr++ = "-iconic";
1015 *argptr++ = unixfilename;
1016 p = strdup(lpCmdLine);
1017 while (1)
1019 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
1020 if (!*p) break;
1021 *argptr++ = p;
1022 while (*p && *p != ' ' && *p != '\t') p++;
1024 *argptr++ = 0;
1026 /* Execute */
1027 execvp(argv[0], (char**)argv);
1030 /* Failed ! */
1032 if (__winelib)
1034 /* build argv */
1035 argptr = argv;
1036 *argptr++ = "wine";
1037 if (iconic) *argptr++ = "-iconic";
1038 *argptr++ = lpCmdLine;
1039 *argptr++ = 0;
1041 /* Execute */
1042 execvp(argv[0] , (char**)argv);
1044 /* Failed ! */
1045 MSG("WinExec: can't exec 'wine %s'\n",
1046 lpCmdLine);
1048 exit(1);
1051 } /* while (!exhausted && handle < 32) */
1053 return handle;
1057 /***********************************************************************
1058 * WIN32_GetProcAddress16 (KERNEL32.36)
1059 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1061 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hModule, LPCSTR name )
1063 WORD ordinal;
1064 FARPROC16 ret;
1066 if (!hModule) {
1067 WARN(module,"hModule may not be 0!\n");
1068 return (FARPROC16)0;
1070 if (HIWORD(hModule))
1072 WARN( module, "hModule is Win32 handle (%08x)\n", hModule );
1073 return (FARPROC16)0;
1075 hModule = GetExePtr( hModule );
1076 if (HIWORD(name)) {
1077 ordinal = NE_GetOrdinal( hModule, name );
1078 TRACE(module, "%04x '%s'\n",
1079 hModule, name );
1080 } else {
1081 ordinal = LOWORD(name);
1082 TRACE(module, "%04x %04x\n",
1083 hModule, ordinal );
1085 if (!ordinal) return (FARPROC16)0;
1086 ret = NE_GetEntryPoint( hModule, ordinal );
1087 TRACE(module,"returning %08x\n",(UINT32)ret);
1088 return ret;
1091 /***********************************************************************
1092 * GetProcAddress16 (KERNEL.50)
1094 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
1096 WORD ordinal;
1097 FARPROC16 ret;
1099 if (!hModule) hModule = GetCurrentTask();
1100 hModule = GetExePtr( hModule );
1102 if (HIWORD(name) != 0)
1104 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1105 TRACE(module, "%04x '%s'\n",
1106 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
1108 else
1110 ordinal = LOWORD(name);
1111 TRACE(module, "%04x %04x\n",
1112 hModule, ordinal );
1114 if (!ordinal) return (FARPROC16)0;
1116 ret = NE_GetEntryPoint( hModule, ordinal );
1118 TRACE(module, "returning %08x\n", (UINT32)ret );
1119 return ret;
1123 /***********************************************************************
1124 * GetProcAddress32 (KERNEL32.257)
1126 FARPROC32 WINAPI GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1128 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, TRUE );
1131 /***********************************************************************
1132 * WIN16_GetProcAddress32 (KERNEL.453)
1134 FARPROC32 WINAPI WIN16_GetProcAddress32( HMODULE32 hModule, LPCSTR function )
1136 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function, FALSE );
1139 /***********************************************************************
1140 * MODULE_GetProcAddress32 (internal)
1142 FARPROC32 MODULE_GetProcAddress32(
1143 PDB32 *process, /* [in] process context */
1144 HMODULE32 hModule, /* [in] current module handle */
1145 LPCSTR function, /* [in] function to be looked up */
1146 BOOL32 snoop )
1148 WINE_MODREF *wm = MODULE32_LookupHMODULE(process,hModule);
1150 if (HIWORD(function))
1151 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
1152 else
1153 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
1154 if (!wm)
1155 return (FARPROC32)0;
1156 switch (wm->type)
1158 case MODULE32_PE:
1159 return PE_FindExportedFunction( process, wm, function, snoop );
1160 case MODULE32_ELF:
1161 return ELF_FindExportedFunction( process, wm, function);
1162 default:
1163 ERR(module,"wine_modref type %d not handled.\n",wm->type);
1164 return (FARPROC32)0;
1169 /***********************************************************************
1170 * RtlImageNtHeaders (NTDLL)
1172 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE32 hModule)
1174 /* basically:
1175 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1176 * but we could get HMODULE16 or the like (think builtin modules)
1179 WINE_MODREF *wm = MODULE32_LookupHMODULE( PROCESS_Current(), hModule );
1180 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1181 return PE_HEADER(wm->module);
1185 /***************************************************************************
1186 * HasGPHandler (KERNEL.338)
1189 #pragma pack(1)
1190 typedef struct _GPHANDLERDEF
1192 WORD selector;
1193 WORD rangeStart;
1194 WORD rangeEnd;
1195 WORD handler;
1196 } GPHANDLERDEF;
1197 #pragma pack(4)
1199 SEGPTR WINAPI HasGPHandler( SEGPTR address )
1201 HMODULE16 hModule;
1202 int gpOrdinal;
1203 SEGPTR gpPtr;
1204 GPHANDLERDEF *gpHandler;
1206 if ( (hModule = FarGetOwner( SELECTOROF(address) )) != 0
1207 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1208 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1209 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1210 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1212 while (gpHandler->selector)
1214 if ( SELECTOROF(address) == gpHandler->selector
1215 && OFFSETOF(address) >= gpHandler->rangeStart
1216 && OFFSETOF(address) < gpHandler->rangeEnd )
1217 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1218 gpHandler->handler );
1219 gpHandler++;
1223 return 0;