4 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
24 #include "selectors.h"
25 #include "stackframe.h"
30 extern BOOL32 THREAD_InitDone;
33 /*************************************************************************
34 * MODULE32_LookupHMODULE
35 * looks for the referenced HMODULE in the current process
38 MODULE32_LookupHMODULE(PDB32 *process,HMODULE32 hmod) {
42 return process->exe_modref;
44 ERR(module,"tried to lookup 0x%04x in win32 module handler!\n",hmod);
47 for (wm = process->modref_list;wm;wm=wm->next)
48 if (wm->module == hmod)
54 /***********************************************************************
55 * MODULE_CreateDummyModule
57 * Create a dummy NE module for Win32 or Winelib.
59 HMODULE32 MODULE_CreateDummyModule( const OFSTRUCT *ofs )
63 SEGTABLEENTRY *pSegment;
68 INT32 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
69 + strlen(ofs->szPathName) + 1;
70 INT32 size = sizeof(NE_MODULE) +
71 /* loaded file info */
73 /* segment table: DS,CS */
74 2 * sizeof(SEGTABLEENTRY) +
77 /* several empty tables */
80 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
81 if (!hModule) return (HMODULE32)11; /* invalid exe */
83 FarSetOwner( hModule, hModule );
84 pModule = (NE_MODULE *)GlobalLock16( hModule );
86 /* Set all used entries */
87 pModule->magic = IMAGE_OS2_SIGNATURE;
94 pModule->heap_size = 0xe000;
95 pModule->stack_size = 0x1000;
96 pModule->seg_count = 2;
97 pModule->modref_count = 0;
98 pModule->nrname_size = 0;
99 pModule->fileinfo = sizeof(NE_MODULE);
100 pModule->os_flags = NE_OSFLAGS_WINDOWS;
101 pModule->expected_version = 0x030a;
102 pModule->self = hModule;
104 /* Set loaded file information */
105 memcpy( pModule + 1, ofs, of_size );
106 ((OFSTRUCT *)(pModule+1))->cBytes = of_size - 1;
108 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + of_size);
109 pModule->seg_table = pModule->dgroup_entry = (int)pSegment - (int)pModule;
112 pSegment->flags = NE_SEGFLAGS_DATA;
113 pSegment->minsize = 0x1000;
120 pStr = (char *)pSegment;
121 pModule->name_table = (int)pStr - (int)pModule;
122 basename = strrchr(ofs->szPathName,'\\');
123 if (!basename) basename = ofs->szPathName;
125 len = strlen(basename);
126 if ((s = strchr(basename,'.'))) len = s - basename;
127 if (len > 8) len = 8;
129 strncpy( pStr+1, basename, len );
130 if (len < 8) pStr[len+1] = 0;
133 /* All tables zero terminated */
134 pModule->res_table = pModule->import_table = pModule->entry_table =
135 (int)pStr - (int)pModule;
137 NE_RegisterModule( pModule );
142 /***********************************************************************
143 * MODULE_GetWndProcEntry16 (not a Windows API function)
145 * Return an entry point from the WPROCS dll.
147 FARPROC16 MODULE_GetWndProcEntry16( LPCSTR name )
149 FARPROC16 ret = NULL;
153 /* FIXME: hack for Winelib */
154 extern LRESULT ColorDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
155 extern LRESULT FileOpenDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
156 extern LRESULT FileSaveDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
157 extern LRESULT FindTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
158 extern LRESULT PrintDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
159 extern LRESULT PrintSetupDlgProc(HWND16,UINT16,WPARAM16,LPARAM);
160 extern LRESULT ReplaceTextDlgProc16(HWND16,UINT16,WPARAM16,LPARAM);
162 if (!strcmp(name,"ColorDlgProc"))
163 return (FARPROC16)ColorDlgProc;
164 if (!strcmp(name,"FileOpenDlgProc"))
165 return (FARPROC16)FileOpenDlgProc;
166 if (!strcmp(name,"FileSaveDlgProc"))
167 return (FARPROC16)FileSaveDlgProc;
168 if (!strcmp(name,"FindTextDlgProc"))
169 return (FARPROC16)FindTextDlgProc16;
170 if (!strcmp(name,"PrintDlgProc"))
171 return (FARPROC16)PrintDlgProc;
172 if (!strcmp(name,"PrintSetupDlgProc"))
173 return (FARPROC16)PrintSetupDlgProc;
174 if (!strcmp(name,"ReplaceTextDlgProc"))
175 return (FARPROC16)ReplaceTextDlgProc16;
176 WARN(module,"No mapping for %s(), add one in library/miscstubs.c\n",name);
183 static HMODULE32 hModule = 0;
185 if (!hModule) hModule = GetModuleHandle16( "WPROCS" );
186 ordinal = NE_GetOrdinal( hModule, name );
187 if (!(ret = NE_GetEntryPoint( hModule, ordinal )))
189 WARN( module, "%s not found\n", name );
197 /**********************************************************************
198 * MODULE_FindModule32
200 * Find a (loaded) win32 module depending on path
201 * The handling of '.' is a bit weird, but we need it that way,
202 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
203 * this is the only way to differentiate. (mainly hypertrm.exe)
206 * the module handle if found
209 HMODULE32 MODULE_FindModule32(
210 PDB32* process, /* [in] process in which to find the library */
211 LPCSTR path /* [in] pathname of module/library to be found */
217 if (!(filename = strrchr( path, '\\' )))
218 filename = HEAP_strdupA(process->heap,0,path);
220 filename = HEAP_strdupA(process->heap,0,filename+1);
221 dotptr=strrchr(filename,'.');
224 HeapFree(process->heap,0,filename);
227 for (wm=process->modref_list;wm;wm=wm->next) {
228 LPSTR xmodname,xdotptr;
230 assert (wm->modname);
231 xmodname = HEAP_strdupA(process->heap,0,wm->modname);
232 xdotptr=strrchr(xmodname,'.');
233 if ( (xdotptr && !dotptr) ||
236 if (dotptr) *dotptr = '\0';
237 if (xdotptr) *xdotptr = '\0';
239 if (!lstrcmpi32A( filename, xmodname)) {
240 HeapFree(process->heap,0,filename);
241 HeapFree(process->heap,0,xmodname);
244 if (dotptr) *dotptr='.';
245 /* FIXME: add paths, shortname */
246 HeapFree(process->heap,0,xmodname);
248 HeapFree(process->heap,0,filename);
254 /**********************************************************************
257 * Implementation of LoadModule().
259 * cmd_line must contain the whole command-line, including argv[0] (and
260 * without a preceding length byte).
261 * If cmd_line is NULL, the module is loaded as a library even if it is a .exe
263 HINSTANCE16 MODULE_Load( LPCSTR name, BOOL32 implicit,
264 LPCSTR cmd_line, LPCSTR env, UINT32 show_cmd )
267 HINSTANCE16 hInstance, hPrevInstance;
273 lstrcpyn32A( ofs.szPathName, name, sizeof(ofs.szPathName) );
274 if ((hModule = MODULE_CreateDummyModule( &ofs )) < 32) return hModule;
275 pModule = (NE_MODULE *)GlobalLock16( hModule );
276 hInstance = NE_CreateInstance( pModule, &hPrevInstance,
277 (cmd_line == NULL) );
281 hInstance = NE_LoadModule( name, &hPrevInstance, implicit,
282 (cmd_line == NULL) );
283 if ((hInstance == 21) && cmd_line)
284 return PE_LoadModule( name, cmd_line, env, show_cmd );
287 /* Create a task for this instance */
289 if (hInstance < 32) return hInstance;
290 pModule = NE_GetPtr( hInstance );
291 if (cmd_line && !(pModule->flags & NE_FFLAGS_LIBMODULE))
295 pModule->flags |= NE_FFLAGS_GUI;
297 pdb = PROCESS_Create( pModule, cmd_line, env, hInstance,
298 hPrevInstance, show_cmd );
299 if (pdb && (GetNumTasks() > 1)) Yield16();
306 /**********************************************************************
307 * LoadModule16 (KERNEL.45)
309 HINSTANCE16 LoadModule16( LPCSTR name, LPVOID paramBlock )
312 LPSTR cmd_line, new_cmd_line;
315 HINSTANCE16 hInstance;
317 if (!paramBlock || (paramBlock == (LPVOID)-1))
318 return LoadLibrary16( name );
320 params = (LOADPARAMS *)paramBlock;
321 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
322 /* PowerPoint passes NULL as showCmd */
324 show_cmd = *((UINT16 *)PTR_SEG_TO_LIN(params->showCmd)+1);
326 if (!cmd_line) cmd_line = "";
327 else if (*cmd_line) cmd_line++; /* skip the length byte */
329 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
330 strlen(cmd_line) + strlen(name) + 2 )))
332 strcpy( new_cmd_line, name );
333 strcat( new_cmd_line, " " );
334 strcat( new_cmd_line, cmd_line );
336 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
337 hInstance = MODULE_Load( name, FALSE, new_cmd_line, env, show_cmd );
338 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
339 HeapFree( GetProcessHeap(), 0, new_cmd_line );
343 /**********************************************************************
344 * LoadModule32 (KERNEL32.499)
348 * This should get implemented via CreateProcess -- MODULE_Load
349 * is resolutely 16-bit.
351 DWORD LoadModule32( LPCSTR name, LPVOID paramBlock )
353 LOADPARAMS32 *params = (LOADPARAMS32 *)paramBlock;
356 PROCESSINFORMATION pi;
357 st.cb = sizeof(STARTUPINFO);
358 st.wShowWindow = p->lpCmdShow[2] ; WRONG
360 BOOL32 ret = CreateProcess32A( name, p->lpCmdLine,
361 NULL, NULL, FALSE, 0, p->lpEnvAddress,
364 /* handle errors appropriately */
366 CloseHandle32(pi.hProcess);
367 CloseHandle32(pi.hThread);
370 return MODULE_Load( name, FALSE, params->lpCmdLine, params->lpEnvAddress,
371 *((UINT16 *)params->lpCmdShow + 1) );
376 /***********************************************************************
377 * GetModuleHandle (KERNEL32.237)
379 HMODULE32 WINAPI GetModuleHandle32A(LPCSTR module)
382 TRACE(win32, "%s\n", module ? module : "NULL");
384 return PROCESS_Current()->exe_modref->module;
386 return MODULE_FindModule32(PROCESS_Current(),module);
389 HMODULE32 WINAPI GetModuleHandle32W(LPCWSTR module)
392 LPSTR modulea = HEAP_strdupWtoA( GetProcessHeap(), 0, module );
393 hModule = GetModuleHandle32A( modulea );
394 HeapFree( GetProcessHeap(), 0, modulea );
399 /***********************************************************************
400 * GetModuleFileName32A (KERNEL32.235)
402 DWORD WINAPI GetModuleFileName32A(
403 HMODULE32 hModule, /* [in] module handle (32bit) */
404 LPSTR lpFileName, /* [out] filenamebuffer */
405 DWORD size /* [in] size of filenamebuffer */
407 WINE_MODREF *wm = MODULE32_LookupHMODULE(PROCESS_Current(),hModule);
409 if (!wm) /* can happen on start up or the like */
412 /* FIXME: we should probably get a real long name, but wm->longname
413 * is currently a UNIX filename!
415 lstrcpyn32A( lpFileName, wm->shortname, size );
416 TRACE(module, "%s\n", lpFileName );
417 return strlen(lpFileName);
421 /***********************************************************************
422 * GetModuleFileName32W (KERNEL32.236)
424 DWORD WINAPI GetModuleFileName32W( HMODULE32 hModule, LPWSTR lpFileName,
427 LPSTR fnA = (char*)HeapAlloc( GetProcessHeap(), 0, size );
428 DWORD res = GetModuleFileName32A( hModule, fnA, size );
429 lstrcpynAtoW( lpFileName, fnA, size );
430 HeapFree( GetProcessHeap(), 0, fnA );
435 /***********************************************************************
436 * LoadLibraryEx32W (KERNEL.513)
439 HMODULE32 WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
442 TRACE(module,"(%s,%d,%08lx)\n",libname,hf,flags);
443 return LoadLibraryEx32A(libname, hf,flags);
446 /***********************************************************************
447 * LoadLibraryEx32A (KERNEL32)
449 HMODULE32 WINAPI LoadLibraryEx32A(LPCSTR libname,HFILE32 hfile,DWORD flags)
453 hmod = PE_LoadLibraryEx32A(libname,PROCESS_Current(),hfile,flags);
457 strcpy( buffer, libname );
458 strcat( buffer, ".dll" );
459 hmod = PE_LoadLibraryEx32A(buffer,PROCESS_Current(),hfile,flags);
461 /* initialize all DLLs, which haven't been initialized yet. */
463 PE_InitializeDLLs( PROCESS_Current(), DLL_PROCESS_ATTACH, NULL);
467 /***********************************************************************
468 * LoadLibraryA (KERNEL32)
470 HMODULE32 WINAPI LoadLibrary32A(LPCSTR libname) {
471 return LoadLibraryEx32A(libname,0,0);
474 /***********************************************************************
475 * LoadLibraryW (KERNEL32)
477 HMODULE32 WINAPI LoadLibrary32W(LPCWSTR libnameW)
479 return LoadLibraryEx32W(libnameW,0,0);
482 /***********************************************************************
483 * LoadLibraryExW (KERNEL32)
485 HMODULE32 WINAPI LoadLibraryEx32W(LPCWSTR libnameW,HFILE32 hfile,DWORD flags)
487 LPSTR libnameA = HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW );
488 HMODULE32 ret = LoadLibraryEx32A( libnameA , hfile, flags );
490 HeapFree( GetProcessHeap(), 0, libnameA );
494 /***********************************************************************
497 BOOL32 WINAPI FreeLibrary32(HINSTANCE32 hLibModule)
499 WARN(module,"(%08x): stub\n", hLibModule);
500 return TRUE; /* FIXME */
504 /***********************************************************************
505 * PrivateLoadLibrary (KERNEL32)
507 * FIXME: rough guesswork, don't know what "Private" means
509 HINSTANCE32 WINAPI PrivateLoadLibrary(LPCSTR libname)
511 return (HINSTANCE32)LoadLibrary16(libname);
516 /***********************************************************************
517 * PrivateFreeLibrary (KERNEL32)
519 * FIXME: rough guesswork, don't know what "Private" means
521 void WINAPI PrivateFreeLibrary(HINSTANCE32 handle)
523 FreeLibrary16((HINSTANCE16)handle);
527 /***********************************************************************
528 * WinExec16 (KERNEL.166)
530 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
532 return WinExec32( lpCmdLine, nCmdShow );
536 /***********************************************************************
537 * WinExec32 (KERNEL32.566)
539 HINSTANCE32 WINAPI WinExec32( LPCSTR lpCmdLine, UINT32 nCmdShow )
541 HINSTANCE32 handle = 2;
542 char *p, filename[256];
543 static int use_load_module = 1;
544 int spacelimit = 0, exhausted = 0;
547 return 2; /* File not found */
549 /* Keep trying to load a file by trying different filenames; e.g.,
550 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
551 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
554 while(!exhausted && handle == 2) {
557 /* Build the filename and command-line */
559 lstrcpyn32A(filename, lpCmdLine,
560 sizeof(filename) - 4 /* for extension */);
562 /* Keep grabbing characters until end-of-string, tab, or until the
563 number of spaces is greater than the spacelimit */
565 for (p = filename; ; p++) {
568 if(spacecount > spacelimit) {
574 if(*p == '\0' || *p == '\t') {
582 /* Now load the executable file */
586 /* Winelib: Use LoadModule() only for the program itself */
587 if (__winelib) use_load_module = 0;
588 handle = MODULE_Load( filename, FALSE, lpCmdLine, NULL, nCmdShow );
589 if (handle == 2) /* file not found */
591 /* Check that the original file name did not have a suffix */
592 p = strrchr(filename, '.');
593 /* if there is a '.', check if either \ OR / follow */
594 if (!p || strchr(p, '/') || strchr(p, '\\'))
596 p = filename + strlen(filename);
598 handle = MODULE_Load( filename, FALSE, lpCmdLine,
600 *p = '\0'; /* Remove extension */
605 handle = 2; /* file not found */
609 /* Try to start it as a unix program */
613 DOS_FULL_NAME full_name;
614 const char *unixfilename = NULL;
615 const char *argv[256], **argptr;
616 int iconic = (nCmdShow == SW_SHOWMINIMIZED ||
617 nCmdShow == SW_SHOWMINNOACTIVE);
619 THREAD_InitDone = FALSE; /* we didn't init this process */
620 /* get unixfilename */
621 if (strchr(filename, '/') ||
622 strchr(filename, ':') ||
623 strchr(filename, '\\'))
625 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
626 unixfilename = full_name.long_name;
628 else unixfilename = filename;
634 if (iconic) *argptr++ = "-iconic";
635 *argptr++ = unixfilename;
636 p = strdup(lpCmdLine);
639 while (*p && (*p == ' ' || *p == '\t')) *p++ = '\0';
642 while (*p && *p != ' ' && *p != '\t') p++;
647 execvp(argv[0], (char**)argv);
657 if (iconic) *argptr++ = "-iconic";
658 *argptr++ = lpCmdLine;
662 execvp(argv[0] , (char**)argv);
665 MSG("WinExec: can't exec 'wine %s'\n",
671 } /* while (!exhausted && handle < 32) */
677 /***********************************************************************
678 * WIN32_GetProcAddress16 (KERNEL32.36)
679 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
681 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hModule, LPCSTR name )
687 WARN(module,"hModule may not be 0!\n");
692 WARN( module, "hModule is Win32 handle (%08x)\n", hModule );
695 hModule = GetExePtr( hModule );
697 ordinal = NE_GetOrdinal( hModule, name );
698 TRACE(module, "%04x '%s'\n",
701 ordinal = LOWORD(name);
702 TRACE(module, "%04x %04x\n",
705 if (!ordinal) return (FARPROC16)0;
706 ret = NE_GetEntryPoint( hModule, ordinal );
707 TRACE(module,"returning %08x\n",(UINT32)ret);
711 /***********************************************************************
712 * GetProcAddress16 (KERNEL.50)
714 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, SEGPTR name )
719 if (!hModule) hModule = GetCurrentTask();
720 hModule = GetExePtr( hModule );
722 if (HIWORD(name) != 0)
724 ordinal = NE_GetOrdinal( hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
725 TRACE(module, "%04x '%s'\n",
726 hModule, (LPSTR)PTR_SEG_TO_LIN(name) );
730 ordinal = LOWORD(name);
731 TRACE(module, "%04x %04x\n",
734 if (!ordinal) return (FARPROC16)0;
736 ret = NE_GetEntryPoint( hModule, ordinal );
738 TRACE(module, "returning %08x\n", (UINT32)ret );
743 /***********************************************************************
744 * GetProcAddress32 (KERNEL32.257)
746 FARPROC32 WINAPI GetProcAddress32( HMODULE32 hModule, LPCSTR function )
748 return MODULE_GetProcAddress32( PROCESS_Current(), hModule, function );
752 /***********************************************************************
753 * MODULE_GetProcAddress32 (internal)
755 FARPROC32 MODULE_GetProcAddress32(
756 PDB32 *process, /* [in] process context */
757 HMODULE32 hModule, /* [in] current module handle */
758 LPCSTR function ) /* [in] function to be looked up */
760 WINE_MODREF *wm = MODULE32_LookupHMODULE(process,hModule);
762 if (HIWORD(function))
763 TRACE(win32,"(%08lx,%s)\n",(DWORD)hModule,function);
765 TRACE(win32,"(%08lx,%p)\n",(DWORD)hModule,function);
771 return PE_FindExportedFunction( process, wm, function);
773 ERR(module,"wine_modref type %d not handled.\n",wm->type);
779 /***********************************************************************
780 * RtlImageNtHeaders (NTDLL)
782 LPIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE32 hModule)
785 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
786 * but we could get HMODULE16 or the like (think builtin modules)
789 WINE_MODREF *wm = MODULE32_LookupHMODULE( PROCESS_Current(), hModule );
790 if (!wm || (wm->type != MODULE32_PE)) return (LPIMAGE_NT_HEADERS)0;
791 return PE_HEADER(wm->module);