Large-scale renaming of all Win32 functions and types to use the
[wine/multimedia.git] / loader / module.c
bloba771e6f269a04df6f49bfaa4e85b1f3e6914670d
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 BOOL THREAD_InitDone;
35 /*************************************************************************
36 * MODULE32_LookupHMODULE
37 * looks for the referenced HMODULE in the current process
39 WINE_MODREF *MODULE32_LookupHMODULE( HMODULE hmod )
41 WINE_MODREF *wm;
43 if (!hmod)
44 return PROCESS_Current()->exe_modref;
46 if (!HIWORD(hmod)) {
47 ERR(module,"tried to lookup 0x%04x in win32 module handler!\n",hmod);
48 return NULL;
50 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next )
51 if (wm->module == hmod)
52 return wm;
53 return NULL;
56 /*************************************************************************
57 * MODULE_InitializeDLLs
59 * Call the initialization routines of all DLLs belonging to the
60 * current process. This is somewhat complicated due to the fact that
62 * - we have to respect the module dependencies, i.e. modules implicitly
63 * referenced by another module have to be initialized before the module
64 * itself can be initialized
66 * - the initialization routine of a DLL can itself call LoadLibrary,
67 * thereby introducing a whole new set of dependencies (even involving
68 * the 'old' modules) at any time during the whole process
70 * (Note that this routine can be recursively entered not only directly
71 * from itself, but also via LoadLibrary from one of the called initialization
72 * routines.)
74 static void MODULE_DoInitializeDLLs( WINE_MODREF *wm,
75 DWORD type, LPVOID lpReserved )
77 int i;
79 assert( wm && !wm->initDone );
80 TRACE( module, "(%08x,%ld,%p) - START\n",
81 wm->module, type, lpReserved );
83 /* Tag current MODREF to prevent recursive loop */
84 wm->initDone = TRUE;
86 /* Recursively initialize all child DLLs */
87 for ( i = 0; i < wm->nDeps; i++ )
88 if ( wm->deps[i] && !wm->deps[i]->initDone )
89 MODULE_DoInitializeDLLs( 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, "(%08x,%ld,%p) - END\n",
107 wm->module, type, lpReserved );
110 void MODULE_InitializeDLLs( HMODULE root, DWORD type, LPVOID lpReserved )
112 BOOL inProgress = FALSE;
113 WINE_MODREF *wm;
115 /* Grab the process critical section to protect the recursion flags */
116 /* FIXME: This is probably overkill! */
117 EnterCriticalSection( &PROCESS_Current()->crit_section );
119 TRACE( module, "(%08x,%ld,%p) - START\n", root, type, lpReserved );
121 /* First, check whether initialization is currently in progress */
122 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
123 if ( wm->initDone )
125 inProgress = TRUE;
126 break;
129 if ( inProgress )
132 * If this a LoadLibrary call from within an initialization routine,
133 * treat it analogously to an implicitly referenced DLL.
134 * Anything else may not happen at this point!
136 if ( root )
138 wm = MODULE32_LookupHMODULE( root );
139 if ( wm && !wm->initDone )
140 MODULE_DoInitializeDLLs( wm, type, lpReserved );
142 else
143 FIXME(module, "Invalid recursion!\n");
145 else
147 /* If we arrive here, this is the start of an initialization run */
148 if ( !root )
150 /* If called for main EXE, initialize all DLLs */
151 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
152 if ( !wm->initDone )
153 MODULE_DoInitializeDLLs( wm, type, lpReserved );
155 else
157 /* If called for a specific DLL, initialize only it and its children */
158 wm = MODULE32_LookupHMODULE( root );
159 if (wm) MODULE_DoInitializeDLLs( wm, type, lpReserved );
162 /* We're finished, so we reset all recursion flags */
163 for ( wm = PROCESS_Current()->modref_list; wm; wm = wm->next )
164 wm->initDone = FALSE;
167 TRACE( module, "(%08x,%ld,%p) - END\n", root, type, lpReserved );
169 /* Release critical section */
170 LeaveCriticalSection( &PROCESS_Current()->crit_section );
174 /***********************************************************************
175 * MODULE_CreateDummyModule
177 * Create a dummy NE module for Win32 or Winelib.
179 HMODULE MODULE_CreateDummyModule( const OFSTRUCT *ofs, LPCSTR modName )
181 HMODULE hModule;
182 NE_MODULE *pModule;
183 SEGTABLEENTRY *pSegment;
184 char *pStr,*s;
185 int len;
186 const char* basename;
188 INT of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
189 + strlen(ofs->szPathName) + 1;
190 INT size = sizeof(NE_MODULE) +
191 /* loaded file info */
192 of_size +
193 /* segment table: DS,CS */
194 2 * sizeof(SEGTABLEENTRY) +
195 /* name table */
197 /* several empty tables */
200 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
201 if (!hModule) return (HMODULE)11; /* invalid exe */
203 FarSetOwner16( hModule, hModule );
204 pModule = (NE_MODULE *)GlobalLock16( hModule );
206 /* Set all used entries */
207 pModule->magic = IMAGE_OS2_SIGNATURE;
208 pModule->count = 1;
209 pModule->next = 0;
210 pModule->flags = 0;
211 pModule->dgroup = 1;
212 pModule->ss = 1;
213 pModule->cs = 2;
214 pModule->heap_size = 0xe000;
215 pModule->stack_size = 0x1000;
216 pModule->seg_count = 2;
217 pModule->modref_count = 0;
218 pModule->nrname_size = 0;
219 pModule->fileinfo = sizeof(NE_MODULE);
220 pModule->os_flags = NE_OSFLAGS_WINDOWS;
221 pModule->expected_version = 0x030a;
222 pModule->self = hModule;
224 /* Set loaded file information */
225 memcpy( pModule + 1, ofs, of_size );
226 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
228 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
229 pModule->seg_table = pModule->dgroup_entry = (int)pSegment - (int)pModule;
230 /* Data segment */
231 pSegment->size = 0;
232 pSegment->flags = NE_SEGFLAGS_DATA;
233 pSegment->minsize = 0x1000;
234 pSegment++;
235 /* Code segment */
236 pSegment->flags = 0;
237 pSegment++;
239 /* Module name */
240 pStr = (char *)pSegment;
241 pModule->name_table = (int)pStr - (int)pModule;
242 if ( modName )
243 basename = modName;
244 else
246 basename = strrchr(ofs->szPathName,'\\');
247 if (!basename) basename = ofs->szPathName;
248 else basename++;
250 len = strlen(basename);
251 if ((s = strchr(basename,'.'))) len = s - basename;
252 if (len > 8) len = 8;
253 *pStr = len;
254 strncpy( pStr+1, basename, len );
255 if (len < 8) pStr[len+1] = 0;
256 pStr += 9;
258 /* All tables zero terminated */
259 pModule->res_table = pModule->import_table = pModule->entry_table =
260 (int)pStr - (int)pModule;
262 NE_RegisterModule( pModule );
263 return hModule;
267 /***********************************************************************
268 * MODULE_GetWndProcEntry16 (not a Windows API function)
270 * Return an entry point from the WPROCS dll.
272 FARPROC16 MODULE_GetWndProcEntry16( LPCSTR name )
274 FARPROC16 ret = NULL;
276 if (__winelib)
278 /* FIXME: hack for Winelib */
279 extern LRESULT ColorDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
280 extern LRESULT FileOpenDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
281 extern LRESULT FileSaveDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
282 extern LRESULT FindTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
283 extern LRESULT PrintDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
284 extern LRESULT PrintSetupDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
285 extern LRESULT ReplaceTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
287 if (!strcmp(name,"ColorDlgProc"))
288 return (FARPROC16)ColorDlgProc16;
289 if (!strcmp(name,"FileOpenDlgProc"))
290 return (FARPROC16)FileOpenDlgProc16;
291 if (!strcmp(name,"FileSaveDlgProc"))
292 return (FARPROC16)FileSaveDlgProc16;
293 if (!strcmp(name,"FindTextDlgProc"))
294 return (FARPROC16)FindTextDlgProc16;
295 if (!strcmp(name,"PrintDlgProc"))
296 return (FARPROC16)PrintDlgProc16;
297 if (!strcmp(name,"PrintSetupDlgProc"))
298 return (FARPROC16)PrintSetupDlgProc16;
299 if (!strcmp(name,"ReplaceTextDlgProc"))
300 return (FARPROC16)ReplaceTextDlgProc16;
301 if (!strcmp(name,"DefResourceHandler"))
302 return (FARPROC16)NE_DefResourceHandler;
303 if (!strcmp(name,"LoadDIBIconHandler"))
304 return (FARPROC16)LoadDIBIconHandler16;
305 if (!strcmp(name,"LoadDIBCursorHandler"))
306 return (FARPROC16)LoadDIBCursorHandler16;
307 FIXME(module,"No mapping for %s(), add one in library/miscstubs.c\n",name);
308 assert( FALSE );
309 return NULL;
311 else
313 WORD ordinal;
314 static HMODULE hModule = 0;
316 if (!hModule) hModule = GetModuleHandle16( "WPROCS" );
317 ordinal = NE_GetOrdinal( hModule, name );
318 if (!(ret = NE_GetEntryPoint( hModule, ordinal )))
320 WARN( module, "%s not found\n", name );
321 assert( FALSE );
324 return ret;
328 /**********************************************************************
329 * MODULE_FindModule32
331 * Find a (loaded) win32 module depending on path
332 * The handling of '.' is a bit weird, but we need it that way,
333 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
334 * this is the only way to differentiate. (mainly hypertrm.exe)
336 * RETURNS
337 * the module handle if found
338 * 0 if not
340 HMODULE MODULE_FindModule(
341 LPCSTR path /* [in] pathname of module/library to be found */
343 LPSTR filename;
344 LPSTR dotptr;
345 WINE_MODREF *wm;
347 if (!(filename = strrchr( path, '\\' )))
348 filename = HEAP_strdupA( GetProcessHeap(), 0, path );
349 else
350 filename = HEAP_strdupA( GetProcessHeap(), 0, filename+1 );
351 dotptr=strrchr(filename,'.');
353 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
354 LPSTR xmodname,xdotptr;
356 assert (wm->modname);
357 xmodname = HEAP_strdupA( GetProcessHeap(), 0, wm->modname );
358 xdotptr=strrchr(xmodname,'.');
359 if ( (xdotptr && !dotptr) ||
360 (!xdotptr && dotptr)
362 if (dotptr) *dotptr = '\0';
363 if (xdotptr) *xdotptr = '\0';
365 if (!strcasecmp( filename, xmodname)) {
366 HeapFree( GetProcessHeap(), 0, filename );
367 HeapFree( GetProcessHeap(), 0, xmodname );
368 return wm->module;
370 if (dotptr) *dotptr='.';
371 /* FIXME: add paths, shortname */
372 HeapFree( GetProcessHeap(), 0, xmodname );
374 /* if that fails, try looking for the filename... */
375 for ( wm = PROCESS_Current()->modref_list; wm; wm=wm->next ) {
376 LPSTR xlname,xdotptr;
378 assert (wm->longname);
379 xlname = strrchr(wm->longname,'\\');
380 if (!xlname)
381 xlname = wm->longname;
382 else
383 xlname++;
384 xlname = HEAP_strdupA( GetProcessHeap(), 0, xlname );
385 xdotptr=strrchr(xlname,'.');
386 if ( (xdotptr && !dotptr) ||
387 (!xdotptr && dotptr)
389 if (dotptr) *dotptr = '\0';
390 if (xdotptr) *xdotptr = '\0';
392 if (!strcasecmp( filename, xlname)) {
393 HeapFree( GetProcessHeap(), 0, filename );
394 HeapFree( GetProcessHeap(), 0, xlname );
395 return wm->module;
397 if (dotptr) *dotptr='.';
398 /* FIXME: add paths, shortname */
399 HeapFree( GetProcessHeap(), 0, xlname );
401 HeapFree( GetProcessHeap(), 0, filename );
402 return 0;
407 /**********************************************************************
408 * NE_CreateProcess
410 static HINSTANCE16 NE_CreateProcess( LPCSTR name, LPCSTR cmd_line, LPCSTR env,
411 BOOL inherit, LPSTARTUPINFOA startup,
412 LPPROCESS_INFORMATION info )
414 HINSTANCE16 hInstance, hPrevInstance;
415 NE_MODULE *pModule;
417 /* Load module */
419 hInstance = NE_LoadModule( name, &hPrevInstance, TRUE, FALSE );
420 if (hInstance < 32) return hInstance;
422 if ( !(pModule = NE_GetPtr(hInstance))
423 || (pModule->flags & NE_FFLAGS_LIBMODULE))
425 /* FIXME: cleanup */
426 return 11;
429 /* Create a task for this instance */
431 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
433 PROCESS_Create( pModule, cmd_line, env, hInstance,
434 hPrevInstance, inherit, startup, info );
436 return hInstance;
440 /**********************************************************************
441 * LoadModule16 (KERNEL.45)
443 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
445 LOADPARAMS16 *params;
446 LPSTR cmd_line, new_cmd_line;
447 LPCVOID env = NULL;
448 STARTUPINFOA startup;
449 PROCESS_INFORMATION info;
450 HINSTANCE16 hInstance, hPrevInstance;
451 NE_MODULE *pModule;
452 PDB *pdb;
454 /* Load module */
456 if (!paramBlock || (paramBlock == (LPVOID)-1))
457 return LoadLibrary16( name );
459 hInstance = NE_LoadModule( name, &hPrevInstance, FALSE, FALSE );
460 if ( hInstance < 32 || !(pModule = NE_GetPtr(hInstance))
461 || (pModule->flags & NE_FFLAGS_LIBMODULE))
462 return hInstance;
464 /* Create a task for this instance */
466 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
468 params = (LOADPARAMS16 *)paramBlock;
469 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
470 if (!cmd_line) cmd_line = "";
471 else if (*cmd_line) cmd_line++; /* skip the length byte */
473 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
474 strlen(cmd_line)+strlen(name)+2 )))
475 return 0;
476 strcpy( new_cmd_line, name );
477 strcat( new_cmd_line, " " );
478 strcat( new_cmd_line, cmd_line );
480 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
482 memset( &info, '\0', sizeof(info) );
483 memset( &startup, '\0', sizeof(startup) );
484 startup.cb = sizeof(startup);
485 if (params->showCmd)
487 startup.dwFlags = STARTF_USESHOWWINDOW;
488 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
491 pdb = PROCESS_Create( pModule, new_cmd_line, env,
492 hInstance, hPrevInstance, TRUE, &startup, &info );
494 CloseHandle( info.hThread );
495 CloseHandle( info.hProcess );
497 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
498 HeapFree( GetProcessHeap(), 0, new_cmd_line );
500 /* Start task */
502 if (pdb) TASK_StartTask( pdb->task );
504 return hInstance;
507 /**********************************************************************
508 * LoadModule32 (KERNEL32.499)
510 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
512 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
513 PROCESS_INFORMATION info;
514 STARTUPINFOA startup;
515 HINSTANCE hInstance;
516 PDB *pdb;
517 TDB *tdb;
519 memset( &startup, '\0', sizeof(startup) );
520 startup.cb = sizeof(startup);
521 startup.dwFlags = STARTF_USESHOWWINDOW;
522 startup.wShowWindow = params->lpCmdShow? params->lpCmdShow[1] : 0;
524 if (!CreateProcessA( name, params->lpCmdLine,
525 NULL, NULL, FALSE, 0, params->lpEnvAddress,
526 NULL, &startup, &info ))
527 return GetLastError(); /* guaranteed to be < 32 */
529 /* Get 16-bit hInstance/hTask from process */
530 pdb = PROCESS_IdToPDB( info.dwProcessId );
531 tdb = pdb? (TDB *)GlobalLock16( pdb->task ) : NULL;
532 hInstance = tdb && tdb->hInstance? tdb->hInstance : pdb? pdb->task : 0;
534 /* Close off the handles */
535 CloseHandle( info.hThread );
536 CloseHandle( info.hProcess );
538 return hInstance;
541 /**********************************************************************
542 * CreateProcess32A (KERNEL32.171)
544 BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
545 LPSECURITY_ATTRIBUTES lpProcessAttributes,
546 LPSECURITY_ATTRIBUTES lpThreadAttributes,
547 BOOL bInheritHandles, DWORD dwCreationFlags,
548 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
549 LPSTARTUPINFOA lpStartupInfo,
550 LPPROCESS_INFORMATION lpProcessInfo )
552 HINSTANCE16 hInstance;
553 LPCSTR cmdline;
554 PDB *pdb;
555 char name[256];
557 /* Get name and command line */
559 if (!lpApplicationName && !lpCommandLine)
561 SetLastError( ERROR_FILE_NOT_FOUND );
562 return FALSE;
565 cmdline = lpCommandLine? lpCommandLine : lpApplicationName;
567 if (lpApplicationName)
568 lstrcpynA(name, lpApplicationName, sizeof(name) - 4);
569 else {
570 char *ptr;
571 int len;
573 /* Take care off .exes with spaces in their names */
574 ptr = strchr(lpCommandLine, ' ');
575 do {
576 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
577 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
578 lstrcpynA(name, lpCommandLine, len);
579 if (!strchr(name, '\\') && !strchr(name, '.'))
580 strcat(name, ".exe");
581 if (GetFileAttributesA(name)!=-1)
582 break;
583 /* if there is a space and no file found yet, include the word
584 * up to the next space too. If there is no next space, just
585 * use the first word.
587 if (ptr) {
588 ptr = strchr(ptr+1, ' ');
589 } else {
590 ptr = strchr(lpCommandLine, ' ');
591 len = (ptr? ptr-lpCommandLine : strlen(lpCommandLine)) + 1;
592 if (len > sizeof(name) - 4) len = sizeof(name) - 4;
593 lstrcpynA(name, lpCommandLine, len);
594 break;
596 } while (1);
599 if (!strchr(name, '\\') && !strchr(name, '.'))
600 strcat(name, ".exe");
603 /* Warn if unsupported features are used */
605 if (lpProcessAttributes)
606 FIXME(module, "(%s,...): lpProcessAttributes ignored\n", name);
607 if (lpThreadAttributes)
608 FIXME(module, "(%s,...): lpThreadAttributes ignored\n", name);
609 if (dwCreationFlags & DEBUG_PROCESS)
610 FIXME(module, "(%s,...): DEBUG_PROCESS ignored\n", name);
611 if (dwCreationFlags & DEBUG_ONLY_THIS_PROCESS)
612 FIXME(module, "(%s,...): DEBUG_ONLY_THIS_PROCESS ignored\n", name);
613 if (dwCreationFlags & CREATE_SUSPENDED)
614 FIXME(module, "(%s,...): CREATE_SUSPENDED ignored\n", name);
615 if (dwCreationFlags & DETACHED_PROCESS)
616 FIXME(module, "(%s,...): DETACHED_PROCESS ignored\n", name);
617 if (dwCreationFlags & CREATE_NEW_CONSOLE)
618 FIXME(module, "(%s,...): CREATE_NEW_CONSOLE ignored\n", name);
619 if (dwCreationFlags & NORMAL_PRIORITY_CLASS)
620 FIXME(module, "(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name);
621 if (dwCreationFlags & IDLE_PRIORITY_CLASS)
622 FIXME(module, "(%s,...): IDLE_PRIORITY_CLASS ignored\n", name);
623 if (dwCreationFlags & HIGH_PRIORITY_CLASS)
624 FIXME(module, "(%s,...): HIGH_PRIORITY_CLASS ignored\n", name);
625 if (dwCreationFlags & REALTIME_PRIORITY_CLASS)
626 FIXME(module, "(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name);
627 if (dwCreationFlags & CREATE_NEW_PROCESS_GROUP)
628 FIXME(module, "(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name);
629 if (dwCreationFlags & CREATE_UNICODE_ENVIRONMENT)
630 FIXME(module, "(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name);
631 if (dwCreationFlags & CREATE_SEPARATE_WOW_VDM)
632 FIXME(module, "(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name);
633 if (dwCreationFlags & CREATE_SHARED_WOW_VDM)
634 FIXME(module, "(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name);
635 if (dwCreationFlags & CREATE_DEFAULT_ERROR_MODE)
636 FIXME(module, "(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name);
637 if (dwCreationFlags & CREATE_NO_WINDOW)
638 FIXME(module, "(%s,...): CREATE_NO_WINDOW ignored\n", name);
639 if (dwCreationFlags & PROFILE_USER)
640 FIXME(module, "(%s,...): PROFILE_USER ignored\n", name);
641 if (dwCreationFlags & PROFILE_KERNEL)
642 FIXME(module, "(%s,...): PROFILE_KERNEL ignored\n", name);
643 if (dwCreationFlags & PROFILE_SERVER)
644 FIXME(module, "(%s,...): PROFILE_SERVER ignored\n", name);
645 if (lpCurrentDirectory)
646 FIXME(module, "(%s,...): lpCurrentDirectory %s ignored\n",
647 name, lpCurrentDirectory);
648 if (lpStartupInfo->lpDesktop)
649 FIXME(module, "(%s,...): lpStartupInfo->lpDesktop %s ignored\n",
650 name, lpStartupInfo->lpDesktop);
651 if (lpStartupInfo->lpTitle)
652 FIXME(module, "(%s,...): lpStartupInfo->lpTitle %s ignored\n",
653 name, lpStartupInfo->lpTitle);
654 if (lpStartupInfo->dwFlags & STARTF_USECOUNTCHARS)
655 FIXME(module, "(%s,...): STARTF_USECOUNTCHARS (%ld,%ld) ignored\n",
656 name, lpStartupInfo->dwXCountChars, lpStartupInfo->dwYCountChars);
657 if (lpStartupInfo->dwFlags & STARTF_USEFILLATTRIBUTE)
658 FIXME(module, "(%s,...): STARTF_USEFILLATTRIBUTE %lx ignored\n",
659 name, lpStartupInfo->dwFillAttribute);
660 if (lpStartupInfo->dwFlags & STARTF_RUNFULLSCREEN)
661 FIXME(module, "(%s,...): STARTF_RUNFULLSCREEN ignored\n", name);
662 if (lpStartupInfo->dwFlags & STARTF_FORCEONFEEDBACK)
663 FIXME(module, "(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name);
664 if (lpStartupInfo->dwFlags & STARTF_FORCEOFFFEEDBACK)
665 FIXME(module, "(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name);
666 if (lpStartupInfo->dwFlags & STARTF_USEHOTKEY)
667 FIXME(module, "(%s,...): STARTF_USEHOTKEY ignored\n", name);
670 /* Try NE module */
671 hInstance = NE_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
672 lpStartupInfo, lpProcessInfo );
674 /* Try PE module */
675 if (hInstance == 21)
676 hInstance = PE_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
677 lpStartupInfo, lpProcessInfo );
679 /* Try DOS module */
680 if (hInstance == 11)
681 hInstance = MZ_CreateProcess( name, cmdline, lpEnvironment, bInheritHandles,
682 lpStartupInfo, lpProcessInfo );
684 if (hInstance < 32)
686 SetLastError( hInstance );
687 return FALSE;
690 /* Get hTask from process and start the task */
691 pdb = PROCESS_IdToPDB( lpProcessInfo->dwProcessId );
692 if (pdb) TASK_StartTask( pdb->task );
694 return TRUE;
697 /**********************************************************************
698 * CreateProcess32W (KERNEL32.172)
699 * NOTES
700 * lpReserved is not converted
702 BOOL WINAPI CreateProcessW( LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
703 LPSECURITY_ATTRIBUTES lpProcessAttributes,
704 LPSECURITY_ATTRIBUTES lpThreadAttributes,
705 BOOL bInheritHandles, DWORD dwCreationFlags,
706 LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
707 LPSTARTUPINFOW lpStartupInfo,
708 LPPROCESS_INFORMATION lpProcessInfo )
709 { BOOL ret;
710 STARTUPINFOA StartupInfoA;
712 LPSTR lpApplicationNameA = HEAP_strdupWtoA (GetProcessHeap(),0,lpApplicationName);
713 LPSTR lpCommandLineA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCommandLine);
714 LPSTR lpCurrentDirectoryA = HEAP_strdupWtoA (GetProcessHeap(),0,lpCurrentDirectory);
716 memcpy (&StartupInfoA, lpStartupInfo, sizeof(STARTUPINFOA));
717 StartupInfoA.lpDesktop = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpDesktop);
718 StartupInfoA.lpTitle = HEAP_strdupWtoA (GetProcessHeap(),0,lpStartupInfo->lpTitle);
720 TRACE(win32, "(%s,%s,...)\n", debugstr_w(lpApplicationName), debugstr_w(lpCommandLine));
722 if (lpStartupInfo->lpReserved)
723 FIXME(win32,"StartupInfo.lpReserved is used, please report (%s)\n", debugstr_w(lpStartupInfo->lpReserved));
725 ret = CreateProcessA( lpApplicationNameA, lpCommandLineA,
726 lpProcessAttributes, lpThreadAttributes,
727 bInheritHandles, dwCreationFlags,
728 lpEnvironment, lpCurrentDirectoryA,
729 &StartupInfoA, lpProcessInfo );
731 HeapFree( GetProcessHeap(), 0, lpCurrentDirectoryA );
732 HeapFree( GetProcessHeap(), 0, lpCommandLineA );
733 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpDesktop );
734 HeapFree( GetProcessHeap(), 0, StartupInfoA.lpTitle );
736 return ret;
739 /***********************************************************************
740 * GetModuleHandle (KERNEL32.237)
742 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
744 if (module == NULL)
745 return PROCESS_Current()->exe_modref->module;
746 else
747 return MODULE_FindModule( module );
750 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
752 HMODULE hModule;
753 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
754 hModule = GetModuleHandleA( modulea );
755 HeapFree( GetProcessHeap(), 0, modulea );
756 return hModule;
760 /***********************************************************************
761 * GetModuleFileName32A (KERNEL32.235)
763 DWORD WINAPI GetModuleFileNameA(
764 HMODULE hModule, /* [in] module handle (32bit) */
765 LPSTR lpFileName, /* [out] filenamebuffer */
766 DWORD size /* [in] size of filenamebuffer */
767 ) {
768 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
770 if (!wm) /* can happen on start up or the like */
771 return 0;
773 if (PE_HEADER(wm->module)->OptionalHeader.MajorOperatingSystemVersion >= 4.0)
774 lstrcpynA( lpFileName, wm->longname, size );
775 else
776 lstrcpynA( lpFileName, wm->shortname, size );
778 TRACE(module, "%s\n", lpFileName );
779 return strlen(lpFileName);
783 /***********************************************************************
784 * GetModuleFileName32W (KERNEL32.236)
786 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName,
787 DWORD size )
789 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
790 DWORD res = GetModuleFileNameA( hModule, fnA, size );
791 lstrcpynAtoW( lpFileName, fnA, size );
792 HeapFree( GetProcessHeap(), 0, fnA );
793 return res;
797 /***********************************************************************
798 * LoadLibraryEx32W (KERNEL.513)
799 * FIXME
801 HMODULE WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
802 DWORD flags )
804 TRACE(module,"(%s,%d,%08lx)\n",libname,hf,flags);
805 return LoadLibraryExA(libname, hf,flags);
808 /***********************************************************************
809 * LoadLibraryEx32A (KERNEL32)
811 HMODULE WINAPI LoadLibraryExA(LPCSTR libname,HFILE hfile,DWORD flags)
813 HMODULE hmod;
814 hmod = MODULE_LoadLibraryExA( libname, hfile, flags );
816 /* at least call not the dllmain...*/
817 if ( DONT_RESOLVE_DLL_REFERENCES==flags || LOAD_LIBRARY_AS_DATAFILE==flags )
818 { FIXME(module,"flag not properly supported %lx\n", flags);
819 return hmod;
822 /* initialize DLL just loaded */
823 if ( hmod >= 32 )
824 MODULE_InitializeDLLs( hmod, DLL_PROCESS_ATTACH, (LPVOID)-1 );
826 return hmod;
829 HMODULE MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
831 HMODULE hmod;
833 hmod = ELF_LoadLibraryExA( libname, hfile, flags );
834 if (hmod) return hmod;
836 hmod = PE_LoadLibraryExA( libname, hfile, flags );
837 return hmod;
840 /***********************************************************************
841 * LoadLibraryA (KERNEL32)
843 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
844 return LoadLibraryExA(libname,0,0);
847 /***********************************************************************
848 * LoadLibraryW (KERNEL32)
850 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
852 return LoadLibraryExW(libnameW,0,0);
855 /***********************************************************************
856 * LoadLibraryExW (KERNEL32)
858 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW,HFILE hfile,DWORD flags)
860 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
861 HMODULE ret = LoadLibraryExA( libnameA , hfile, flags );
863 HeapFree( GetProcessHeap(), 0, libnameA );
864 return ret;
867 /***********************************************************************
868 * FreeLibrary
870 BOOL WINAPI FreeLibrary(HINSTANCE 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 HINSTANCE WINAPI PrivateLoadLibrary(LPCSTR libname)
884 return (HINSTANCE)LoadLibrary16(libname);
889 /***********************************************************************
890 * PrivateFreeLibrary (KERNEL32)
892 * FIXME: rough guesswork, don't know what "Private" means
894 void WINAPI PrivateFreeLibrary(HINSTANCE handle)
896 FreeLibrary16((HINSTANCE16)handle);
900 /***********************************************************************
901 * WinExec16 (KERNEL.166)
903 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
905 return WinExec( lpCmdLine, nCmdShow );
909 /***********************************************************************
910 * WinExec32 (KERNEL32.566)
912 HINSTANCE WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
914 HINSTANCE handle = 2;
915 char *p, filename[256];
916 int spacelimit = 0, exhausted = 0;
917 LOADPARAMS 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 lstrcpynA(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 = LoadModule( 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 = LoadModule( 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( HMODULE 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",(UINT)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", (UINT)ret );
1119 return ret;
1123 /***********************************************************************
1124 * GetProcAddress32 (KERNEL32.257)
1126 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
1128 return MODULE_GetProcAddress( hModule, function, TRUE );
1131 /***********************************************************************
1132 * WIN16_GetProcAddress32 (KERNEL.453)
1134 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
1136 return MODULE_GetProcAddress( hModule, function, FALSE );
1139 /***********************************************************************
1140 * MODULE_GetProcAddress32 (internal)
1142 FARPROC MODULE_GetProcAddress(
1143 HMODULE hModule, /* [in] current module handle */
1144 LPCSTR function, /* [in] function to be looked up */
1145 BOOL snoop )
1147 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1149 if (HIWORD(function))
1150 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
1151 else
1152 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
1153 if (!wm)
1154 return (FARPROC)0;
1155 switch (wm->type)
1157 case MODULE32_PE:
1158 return PE_FindExportedFunction( wm, function, snoop );
1159 case MODULE32_ELF:
1160 return ELF_FindExportedFunction( wm, function);
1161 default:
1162 ERR(module,"wine_modref type %d not handled.\n",wm->type);
1163 return (FARPROC)0;
1168 /***********************************************************************
1169 * RtlImageNtHeaders (NTDLL)
1171 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
1173 /* basically:
1174 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
1175 * but we could get HMODULE16 or the like (think builtin modules)
1178 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
1179 if (!wm || (wm->type != MODULE32_PE)) return (PIMAGE_NT_HEADERS)0;
1180 return PE_HEADER(wm->module);
1184 /***************************************************************************
1185 * HasGPHandler (KERNEL.338)
1188 #pragma pack(1)
1189 typedef struct _GPHANDLERDEF
1191 WORD selector;
1192 WORD rangeStart;
1193 WORD rangeEnd;
1194 WORD handler;
1195 } GPHANDLERDEF;
1196 #pragma pack(4)
1198 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1200 HMODULE16 hModule;
1201 int gpOrdinal;
1202 SEGPTR gpPtr;
1203 GPHANDLERDEF *gpHandler;
1205 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1206 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1207 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1208 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1209 && (gpHandler = PTR_SEG_TO_LIN( gpPtr )) != NULL )
1211 while (gpHandler->selector)
1213 if ( SELECTOROF(address) == gpHandler->selector
1214 && OFFSETOF(address) >= gpHandler->rangeStart
1215 && OFFSETOF(address) < gpHandler->rangeEnd )
1216 return PTR_SEG_OFF_TO_SEGPTR( gpHandler->selector,
1217 gpHandler->handler );
1218 gpHandler++;
1222 return 0;