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 if (!strcmp(name
,"DefResourceHandler"))
177 return (FARPROC16
)NE_DefResourceHandler
;
178 if (!strcmp(name
,"LoadDIBIconHandler"))
179 return (FARPROC16
)LoadDIBIconHandler
;
180 if (!strcmp(name
,"LoadDIBCursorHandler"))
181 return (FARPROC16
)LoadDIBCursorHandler
;
182 FIXME(module
,"No mapping for %s(), add one in library/miscstubs.c\n",name
);
189 static HMODULE32 hModule
= 0;
191 if (!hModule
) hModule
= GetModuleHandle16( "WPROCS" );
192 ordinal
= NE_GetOrdinal( hModule
, name
);
193 if (!(ret
= NE_GetEntryPoint( hModule
, ordinal
)))
195 WARN( module
, "%s not found\n", name
);
203 /**********************************************************************
204 * MODULE_FindModule32
206 * Find a (loaded) win32 module depending on path
207 * The handling of '.' is a bit weird, but we need it that way,
208 * for sometimes the programs use '<name>.exe' and '<name>.dll' and
209 * this is the only way to differentiate. (mainly hypertrm.exe)
212 * the module handle if found
215 HMODULE32
MODULE_FindModule32(
216 PDB32
* process
, /* [in] process in which to find the library */
217 LPCSTR path
/* [in] pathname of module/library to be found */
223 if (!(filename
= strrchr( path
, '\\' )))
224 filename
= HEAP_strdupA(process
->heap
,0,path
);
226 filename
= HEAP_strdupA(process
->heap
,0,filename
+1);
227 dotptr
=strrchr(filename
,'.');
230 HeapFree(process
->heap
,0,filename
);
233 for (wm
=process
->modref_list
;wm
;wm
=wm
->next
) {
234 LPSTR xmodname
,xdotptr
;
236 assert (wm
->modname
);
237 xmodname
= HEAP_strdupA(process
->heap
,0,wm
->modname
);
238 xdotptr
=strrchr(xmodname
,'.');
239 if ( (xdotptr
&& !dotptr
) ||
242 if (dotptr
) *dotptr
= '\0';
243 if (xdotptr
) *xdotptr
= '\0';
245 if (!strcasecmp( filename
, xmodname
)) {
246 HeapFree(process
->heap
,0,filename
);
247 HeapFree(process
->heap
,0,xmodname
);
250 if (dotptr
) *dotptr
='.';
251 /* FIXME: add paths, shortname */
252 HeapFree(process
->heap
,0,xmodname
);
254 HeapFree(process
->heap
,0,filename
);
260 /**********************************************************************
263 * Implementation of LoadModule().
265 * cmd_line must contain the whole command-line, including argv[0] (and
266 * without a preceding length byte).
267 * If cmd_line is NULL, the module is loaded as a library even if it is a .exe
269 HINSTANCE16
MODULE_Load( LPCSTR name
, BOOL32 implicit
,
270 LPCSTR cmd_line
, LPCSTR env
, UINT32 show_cmd
)
273 HINSTANCE16 hInstance
, hPrevInstance
;
279 lstrcpyn32A( ofs
.szPathName
, name
, sizeof(ofs
.szPathName
) );
280 if ((hModule
= MODULE_CreateDummyModule( &ofs
)) < 32) return hModule
;
281 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
282 hInstance
= NE_CreateInstance( pModule
, &hPrevInstance
,
283 (cmd_line
== NULL
) );
287 hInstance
= NE_LoadModule( name
, &hPrevInstance
, implicit
,
288 (cmd_line
== NULL
) );
289 if ((hInstance
== 21) && cmd_line
)
290 return PE_LoadModule( name
, cmd_line
, env
, show_cmd
);
293 /* Create a task for this instance */
295 if (hInstance
< 32) return hInstance
;
296 pModule
= NE_GetPtr( hInstance
);
297 if (cmd_line
&& !(pModule
->flags
& NE_FFLAGS_LIBMODULE
))
301 pModule
->flags
|= NE_FFLAGS_GUI
;
303 pdb
= PROCESS_Create( pModule
, cmd_line
, env
, hInstance
,
304 hPrevInstance
, show_cmd
);
305 if (pdb
&& (GetNumTasks() > 1)) Yield16();
312 /**********************************************************************
313 * LoadModule16 (KERNEL.45)
315 HINSTANCE16
LoadModule16( LPCSTR name
, LPVOID paramBlock
)
318 LPSTR cmd_line
, new_cmd_line
;
321 HINSTANCE16 hInstance
;
323 if (!paramBlock
|| (paramBlock
== (LPVOID
)-1))
324 return LoadLibrary16( name
);
326 params
= (LOADPARAMS
*)paramBlock
;
327 cmd_line
= (LPSTR
)PTR_SEG_TO_LIN( params
->cmdLine
);
328 /* PowerPoint passes NULL as showCmd */
330 show_cmd
= *((UINT16
*)PTR_SEG_TO_LIN(params
->showCmd
)+1);
332 if (!cmd_line
) cmd_line
= "";
333 else if (*cmd_line
) cmd_line
++; /* skip the length byte */
335 if (!(new_cmd_line
= HeapAlloc( GetProcessHeap(), 0,
336 strlen(cmd_line
) + strlen(name
) + 2 )))
338 strcpy( new_cmd_line
, name
);
339 strcat( new_cmd_line
, " " );
340 strcat( new_cmd_line
, cmd_line
);
342 if (params
->hEnvironment
) env
= GlobalLock16( params
->hEnvironment
);
343 hInstance
= MODULE_Load( name
, FALSE
, new_cmd_line
, env
, show_cmd
);
344 if (params
->hEnvironment
) GlobalUnlock16( params
->hEnvironment
);
345 HeapFree( GetProcessHeap(), 0, new_cmd_line
);
349 /**********************************************************************
350 * LoadModule32 (KERNEL32.499)
354 * This should get implemented via CreateProcess -- MODULE_Load
355 * is resolutely 16-bit.
357 DWORD
LoadModule32( LPCSTR name
, LPVOID paramBlock
)
359 LOADPARAMS32
*params
= (LOADPARAMS32
*)paramBlock
;
362 PROCESSINFORMATION pi
;
363 st
.cb
= sizeof(STARTUPINFO
);
364 st
.wShowWindow
= p
->lpCmdShow
[2] ; WRONG
366 BOOL32 ret
= CreateProcess32A( name
, p
->lpCmdLine
,
367 NULL
, NULL
, FALSE
, 0, p
->lpEnvAddress
,
370 /* handle errors appropriately */
372 CloseHandle32(pi
.hProcess
);
373 CloseHandle32(pi
.hThread
);
376 return MODULE_Load( name
, FALSE
, params
->lpCmdLine
, params
->lpEnvAddress
,
377 *((UINT16
*)params
->lpCmdShow
+ 1) );
382 /***********************************************************************
383 * GetModuleHandle (KERNEL32.237)
385 HMODULE32 WINAPI
GetModuleHandle32A(LPCSTR module
)
388 TRACE(win32
, "%s\n", module
? module
: "NULL");
390 return PROCESS_Current()->exe_modref
->module
;
392 return MODULE_FindModule32(PROCESS_Current(),module
);
395 HMODULE32 WINAPI
GetModuleHandle32W(LPCWSTR module
)
398 LPSTR modulea
= HEAP_strdupWtoA( GetProcessHeap(), 0, module
);
399 hModule
= GetModuleHandle32A( modulea
);
400 HeapFree( GetProcessHeap(), 0, modulea
);
405 /***********************************************************************
406 * GetModuleFileName32A (KERNEL32.235)
408 DWORD WINAPI
GetModuleFileName32A(
409 HMODULE32 hModule
, /* [in] module handle (32bit) */
410 LPSTR lpFileName
, /* [out] filenamebuffer */
411 DWORD size
/* [in] size of filenamebuffer */
413 WINE_MODREF
*wm
= MODULE32_LookupHMODULE(PROCESS_Current(),hModule
);
415 if (!wm
) /* can happen on start up or the like */
418 /* FIXME: we should probably get a real long name, but wm->longname
419 * is currently a UNIX filename!
421 lstrcpyn32A( lpFileName
, wm
->shortname
, size
);
422 TRACE(module
, "%s\n", lpFileName
);
423 return strlen(lpFileName
);
427 /***********************************************************************
428 * GetModuleFileName32W (KERNEL32.236)
430 DWORD WINAPI
GetModuleFileName32W( HMODULE32 hModule
, LPWSTR lpFileName
,
433 LPSTR fnA
= (char*)HeapAlloc( GetProcessHeap(), 0, size
);
434 DWORD res
= GetModuleFileName32A( hModule
, fnA
, size
);
435 lstrcpynAtoW( lpFileName
, fnA
, size
);
436 HeapFree( GetProcessHeap(), 0, fnA
);
441 /***********************************************************************
442 * LoadLibraryEx32W (KERNEL.513)
445 HMODULE32 WINAPI
LoadLibraryEx32W16( LPCSTR libname
, HANDLE16 hf
,
448 TRACE(module
,"(%s,%d,%08lx)\n",libname
,hf
,flags
);
449 return LoadLibraryEx32A(libname
, hf
,flags
);
452 /***********************************************************************
453 * LoadLibraryEx32A (KERNEL32)
455 HMODULE32 WINAPI
LoadLibraryEx32A(LPCSTR libname
,HFILE32 hfile
,DWORD flags
)
459 hmod
= PE_LoadLibraryEx32A(libname
,PROCESS_Current(),hfile
,flags
);
463 strcpy( buffer
, libname
);
464 strcat( buffer
, ".dll" );
465 hmod
= PE_LoadLibraryEx32A(buffer
,PROCESS_Current(),hfile
,flags
);
467 /* initialize all DLLs, which haven't been initialized yet. */
469 PE_InitializeDLLs( PROCESS_Current(), DLL_PROCESS_ATTACH
, NULL
);
473 /***********************************************************************
474 * LoadLibraryA (KERNEL32)
476 HMODULE32 WINAPI
LoadLibrary32A(LPCSTR libname
) {
477 return LoadLibraryEx32A(libname
,0,0);
480 /***********************************************************************
481 * LoadLibraryW (KERNEL32)
483 HMODULE32 WINAPI
LoadLibrary32W(LPCWSTR libnameW
)
485 return LoadLibraryEx32W(libnameW
,0,0);
488 /***********************************************************************
489 * LoadLibraryExW (KERNEL32)
491 HMODULE32 WINAPI
LoadLibraryEx32W(LPCWSTR libnameW
,HFILE32 hfile
,DWORD flags
)
493 LPSTR libnameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, libnameW
);
494 HMODULE32 ret
= LoadLibraryEx32A( libnameA
, hfile
, flags
);
496 HeapFree( GetProcessHeap(), 0, libnameA
);
500 /***********************************************************************
503 BOOL32 WINAPI
FreeLibrary32(HINSTANCE32 hLibModule
)
505 FIXME(module
,"(%08x): stub\n", hLibModule
);
506 return TRUE
; /* FIXME */
510 /***********************************************************************
511 * PrivateLoadLibrary (KERNEL32)
513 * FIXME: rough guesswork, don't know what "Private" means
515 HINSTANCE32 WINAPI
PrivateLoadLibrary(LPCSTR libname
)
517 return (HINSTANCE32
)LoadLibrary16(libname
);
522 /***********************************************************************
523 * PrivateFreeLibrary (KERNEL32)
525 * FIXME: rough guesswork, don't know what "Private" means
527 void WINAPI
PrivateFreeLibrary(HINSTANCE32 handle
)
529 FreeLibrary16((HINSTANCE16
)handle
);
533 /***********************************************************************
534 * WinExec16 (KERNEL.166)
536 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
538 return WinExec32( lpCmdLine
, nCmdShow
);
542 /***********************************************************************
543 * WinExec32 (KERNEL32.566)
545 HINSTANCE32 WINAPI
WinExec32( LPCSTR lpCmdLine
, UINT32 nCmdShow
)
547 HINSTANCE32 handle
= 2;
548 char *p
, filename
[256];
549 static int use_load_module
= 1;
550 int spacelimit
= 0, exhausted
= 0;
553 return 2; /* File not found */
555 /* Keep trying to load a file by trying different filenames; e.g.,
556 for the cmdline "abcd efg hij", try "abcd" with args "efg hij",
557 then "abcd efg" with arg "hij", and finally "abcd efg hij" with
560 while(!exhausted
&& handle
== 2) {
563 /* Build the filename and command-line */
565 lstrcpyn32A(filename
, lpCmdLine
,
566 sizeof(filename
) - 4 /* for extension */);
568 /* Keep grabbing characters until end-of-string, tab, or until the
569 number of spaces is greater than the spacelimit */
571 for (p
= filename
; ; p
++) {
574 if(spacecount
> spacelimit
) {
580 if(*p
== '\0' || *p
== '\t') {
588 /* Now load the executable file */
592 /* Winelib: Use LoadModule() only for the program itself */
593 if (__winelib
) use_load_module
= 0;
594 handle
= MODULE_Load( filename
, FALSE
, lpCmdLine
, NULL
, nCmdShow
);
595 if (handle
== 2) /* file not found */
597 /* Check that the original file name did not have a suffix */
598 p
= strrchr(filename
, '.');
599 /* if there is a '.', check if either \ OR / follow */
600 if (!p
|| strchr(p
, '/') || strchr(p
, '\\'))
602 p
= filename
+ strlen(filename
);
604 handle
= MODULE_Load( filename
, FALSE
, lpCmdLine
,
606 *p
= '\0'; /* Remove extension */
611 handle
= 2; /* file not found */
615 /* Try to start it as a unix program */
619 DOS_FULL_NAME full_name
;
620 const char *unixfilename
= NULL
;
621 const char *argv
[256], **argptr
;
622 int iconic
= (nCmdShow
== SW_SHOWMINIMIZED
||
623 nCmdShow
== SW_SHOWMINNOACTIVE
);
625 THREAD_InitDone
= FALSE
; /* we didn't init this process */
626 /* get unixfilename */
627 if (strchr(filename
, '/') ||
628 strchr(filename
, ':') ||
629 strchr(filename
, '\\'))
631 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
))
632 unixfilename
= full_name
.long_name
;
634 else unixfilename
= filename
;
640 if (iconic
) *argptr
++ = "-iconic";
641 *argptr
++ = unixfilename
;
642 p
= strdup(lpCmdLine
);
645 while (*p
&& (*p
== ' ' || *p
== '\t')) *p
++ = '\0';
648 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
653 execvp(argv
[0], (char**)argv
);
663 if (iconic
) *argptr
++ = "-iconic";
664 *argptr
++ = lpCmdLine
;
668 execvp(argv
[0] , (char**)argv
);
671 MSG("WinExec: can't exec 'wine %s'\n",
677 } /* while (!exhausted && handle < 32) */
683 /***********************************************************************
684 * WIN32_GetProcAddress16 (KERNEL32.36)
685 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
687 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE32 hModule
, LPCSTR name
)
693 WARN(module
,"hModule may not be 0!\n");
698 WARN( module
, "hModule is Win32 handle (%08x)\n", hModule
);
701 hModule
= GetExePtr( hModule
);
703 ordinal
= NE_GetOrdinal( hModule
, name
);
704 TRACE(module
, "%04x '%s'\n",
707 ordinal
= LOWORD(name
);
708 TRACE(module
, "%04x %04x\n",
711 if (!ordinal
) return (FARPROC16
)0;
712 ret
= NE_GetEntryPoint( hModule
, ordinal
);
713 TRACE(module
,"returning %08x\n",(UINT32
)ret
);
717 /***********************************************************************
718 * GetProcAddress16 (KERNEL.50)
720 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, SEGPTR name
)
725 if (!hModule
) hModule
= GetCurrentTask();
726 hModule
= GetExePtr( hModule
);
728 if (HIWORD(name
) != 0)
730 ordinal
= NE_GetOrdinal( hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
731 TRACE(module
, "%04x '%s'\n",
732 hModule
, (LPSTR
)PTR_SEG_TO_LIN(name
) );
736 ordinal
= LOWORD(name
);
737 TRACE(module
, "%04x %04x\n",
740 if (!ordinal
) return (FARPROC16
)0;
742 ret
= NE_GetEntryPoint( hModule
, ordinal
);
744 TRACE(module
, "returning %08x\n", (UINT32
)ret
);
749 /***********************************************************************
750 * GetProcAddress32 (KERNEL32.257)
752 FARPROC32 WINAPI
GetProcAddress32( HMODULE32 hModule
, LPCSTR function
)
754 return MODULE_GetProcAddress32( PROCESS_Current(), hModule
, function
);
758 /***********************************************************************
759 * MODULE_GetProcAddress32 (internal)
761 FARPROC32
MODULE_GetProcAddress32(
762 PDB32
*process
, /* [in] process context */
763 HMODULE32 hModule
, /* [in] current module handle */
764 LPCSTR function
) /* [in] function to be looked up */
766 WINE_MODREF
*wm
= MODULE32_LookupHMODULE(process
,hModule
);
768 if (HIWORD(function
))
769 TRACE(win32
,"(%08lx,%s)\n",(DWORD
)hModule
,function
);
771 TRACE(win32
,"(%08lx,%p)\n",(DWORD
)hModule
,function
);
777 return PE_FindExportedFunction( process
, wm
, function
);
779 ERR(module
,"wine_modref type %d not handled.\n",wm
->type
);
785 /***********************************************************************
786 * RtlImageNtHeaders (NTDLL)
788 LPIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE32 hModule
)
791 * return hModule+(((IMAGE_DOS_HEADER*)hModule)->e_lfanew);
792 * but we could get HMODULE16 or the like (think builtin modules)
795 WINE_MODREF
*wm
= MODULE32_LookupHMODULE( PROCESS_Current(), hModule
);
796 if (!wm
|| (wm
->type
!= MODULE32_PE
)) return (LPIMAGE_NT_HEADERS
)0;
797 return PE_HEADER(wm
->module
);