4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
35 #include "wine/winbase16.h"
38 #include "wine/library.h"
45 #include "builtin16.h"
46 #include "stackframe.h"
48 #include "wine/exception.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(module
);
52 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
55 typedef struct _GPHANDLERDEF
67 struct ne_segment_table_entry_s
69 WORD seg_data_offset
; /* Sector offset of segment data */
70 WORD seg_data_length
; /* Length of segment data */
71 WORD seg_flags
; /* Flags associated with this segment */
72 WORD min_alloc
; /* Minimum allocation size for this */
75 #define hFirstModule (pThhook->hExeHead)
77 static NE_MODULE
*pCachedModule
= 0; /* Module cached by NE_OpenFile */
81 void *module_start
; /* 32-bit address of the module data */
82 int module_size
; /* Size of the module data */
83 void *code_start
; /* 32-bit address of DLL code */
84 void *data_start
; /* 32-bit address of DLL data */
85 const char *owner
; /* 32-bit dll that contains this dll */
86 const void *rsrc
; /* resources data */
87 } BUILTIN16_DESCRIPTOR
;
89 /* Table of all built-in DLLs */
93 static const BUILTIN16_DESCRIPTOR
*builtin_dlls
[MAX_DLLS
];
96 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
);
97 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
);
99 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
);
101 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
);
104 static WINE_EXCEPTION_FILTER(page_fault
)
106 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
107 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
108 return EXCEPTION_EXECUTE_HANDLER
;
109 return EXCEPTION_CONTINUE_SEARCH
;
113 /* patch all the flat cs references of the code segment if necessary */
114 inline static void patch_code_segment( void *code_segment
)
117 CALLFROM16
*call
= code_segment
;
118 if (call
->flatcs
== wine_get_cs()) return; /* nothing to patch */
119 while (call
->pushl
== 0x68)
121 call
->flatcs
= wine_get_cs();
128 /***********************************************************************
131 * Find a descriptor in the list
133 static const BUILTIN16_DESCRIPTOR
*find_dll_descr( const char *dllname
)
136 for (i
= 0; i
< MAX_DLLS
; i
++)
138 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
141 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
142 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
143 BYTE
*name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
145 /* check the dll file name */
146 if (!FILE_strcasecmp( pOfs
->szPathName
, dllname
)) return descr
;
147 /* check the dll module name (without extension) */
148 if (!FILE_strncasecmp( dllname
, name_table
+1, *name_table
) &&
149 !strcmp( dllname
+ *name_table
, ".dll" ))
157 /***********************************************************************
160 * Check if a builtin dll descriptor is present (because we loaded its 32-bit counterpart).
162 static BOOL
is_builtin_present( LPCSTR name
)
164 char dllname
[20], *p
;
166 if (strlen(name
) >= sizeof(dllname
)-4) return FALSE
;
167 strcpy( dllname
, name
);
168 p
= strrchr( dllname
, '.' );
169 if (!p
) strcat( dllname
, ".dll" );
170 for (p
= dllname
; *p
; p
++) *p
= FILE_tolower(*p
);
172 return (find_dll_descr( dllname
) != NULL
);
176 /***********************************************************************
177 * __wine_register_dll_16 (KERNEL32.@)
179 * Register a built-in DLL descriptor.
181 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR
*descr
)
185 for (i
= 0; i
< MAX_DLLS
; i
++)
187 if (builtin_dlls
[i
]) continue;
188 builtin_dlls
[i
] = descr
;
191 assert( i
< MAX_DLLS
);
195 /***********************************************************************
196 * __wine_unregister_dll_16 (KERNEL32.@)
198 * Unregister a built-in DLL descriptor.
200 void __wine_unregister_dll_16( const BUILTIN16_DESCRIPTOR
*descr
)
204 for (i
= 0; i
< MAX_DLLS
; i
++)
206 if (builtin_dlls
[i
] != descr
) continue;
207 builtin_dlls
[i
] = NULL
;
213 /**********************************************************************
216 void NE_RegisterModule( NE_MODULE
*pModule
)
218 pModule
->next
= hFirstModule
;
219 hFirstModule
= pModule
->self
;
223 /***********************************************************************
226 void NE_DumpModule( HMODULE16 hModule
)
236 if (!(pModule
= NE_GetPtr( hModule
)))
238 MESSAGE( "**** %04x is not a module handle\n", hModule
);
242 /* Dump the module info */
244 DPRINTF( "Module %04x:\n", hModule
);
245 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
246 pModule
->count
, pModule
->flags
,
247 pModule
->heap_size
, pModule
->stack_size
);
248 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
249 pModule
->cs
, pModule
->ip
, pModule
->ss
, pModule
->sp
, pModule
->dgroup
,
250 pModule
->seg_count
, pModule
->modref_count
);
251 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
252 pModule
->os_flags
, pModule
->min_swap_area
,
253 pModule
->expected_version
);
254 if (pModule
->flags
& NE_FFLAGS_WIN32
)
255 DPRINTF( "PE module=%p\n", pModule
->module32
);
257 /* Dump the file info */
259 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
261 /* Dump the segment table */
263 DPRINTF( "Segment table:\n" );
264 pSeg
= NE_SEG_TABLE( pModule
);
265 for (i
= 0; i
< pModule
->seg_count
; i
++, pSeg
++)
266 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
267 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
268 pSeg
->minsize
, pSeg
->hSeg
);
270 /* Dump the resource table */
272 DPRINTF( "Resource table:\n" );
273 if (pModule
->res_table
)
275 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->res_table
);
276 DPRINTF( "Alignment: %d\n", *pword
++ );
279 NE_TYPEINFO
*ptr
= (NE_TYPEINFO
*)pword
;
280 NE_NAMEINFO
*pname
= (NE_NAMEINFO
*)(ptr
+ 1);
281 DPRINTF( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
282 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
283 DPRINTF( "offset=%d len=%d id=%04x\n",
284 pname
->offset
, pname
->length
, pname
->id
);
285 pword
= (WORD
*)pname
;
288 else DPRINTF( "None\n" );
290 /* Dump the resident name table */
292 DPRINTF( "Resident-name table:\n" );
293 pstr
= (char *)pModule
+ pModule
->name_table
;
296 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
297 *(WORD
*)(pstr
+ *pstr
+ 1) );
298 pstr
+= *pstr
+ 1 + sizeof(WORD
);
301 /* Dump the module reference table */
303 DPRINTF( "Module ref table:\n" );
304 if (pModule
->modref_table
)
306 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->modref_table
);
307 for (i
= 0; i
< pModule
->modref_count
; i
++, pword
++)
310 GetModuleName16( *pword
, name
, sizeof(name
) );
311 DPRINTF( "%d: %04x -> '%s'\n", i
, *pword
, name
);
314 else DPRINTF( "None\n" );
316 /* Dump the entry table */
318 DPRINTF( "Entry table:\n" );
319 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+pModule
->entry_table
);
321 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
322 DPRINTF( "Bundle %d-%d: %02x\n", bundle
->first
, bundle
->last
, entry
->type
);
323 ordinal
= bundle
->first
;
324 while (ordinal
< bundle
->last
)
326 if (entry
->type
== 0xff)
327 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal
++, entry
->segnum
, entry
->offs
);
329 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal
++, entry
->segnum
, entry
->offs
);
332 } while ( (bundle
->next
) && (bundle
= ((ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
))) );
334 /* Dump the non-resident names table */
336 DPRINTF( "Non-resident names table:\n" );
337 if (pModule
->nrname_handle
)
339 pstr
= (char *)GlobalLock16( pModule
->nrname_handle
);
342 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
343 *(WORD
*)(pstr
+ *pstr
+ 1) );
344 pstr
+= *pstr
+ 1 + sizeof(WORD
);
351 /***********************************************************************
354 * Walk the module list and print the modules.
356 void NE_WalkModules(void)
358 HMODULE16 hModule
= hFirstModule
;
359 MESSAGE( "Module Flags Name\n" );
362 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
365 MESSAGE( "Bad module %04x in list\n", hModule
);
368 MESSAGE( " %04x %04x %.*s\n", hModule
, pModule
->flags
,
369 *((char *)pModule
+ pModule
->name_table
),
370 (char *)pModule
+ pModule
->name_table
+ 1 );
371 hModule
= pModule
->next
;
376 /***********************************************************************
377 * NE_InitResourceHandler
379 * Fill in 'resloader' fields in the resource table.
381 void NE_InitResourceHandler( NE_MODULE
*pModule
)
383 static FARPROC16 proc
;
385 NE_TYPEINFO
*pTypeInfo
= (NE_TYPEINFO
*)((char *)pModule
+ pModule
->res_table
+ 2);
387 TRACE("InitResourceHandler[%04x]\n", pModule
->self
);
389 if (!proc
) proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
391 while(pTypeInfo
->type_id
)
393 memcpy_unaligned( &pTypeInfo
->resloader
, &proc
, sizeof(FARPROC16
) );
394 pTypeInfo
= (NE_TYPEINFO
*)((char*)(pTypeInfo
+ 1) + pTypeInfo
->count
* sizeof(NE_NAMEINFO
));
399 /***********************************************************************
402 * Lookup the ordinal for a given name.
404 WORD
NE_GetOrdinal( HMODULE16 hModule
, const char *name
)
406 unsigned char buffer
[256], *cpnt
;
410 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
411 if (pModule
->flags
& NE_FFLAGS_WIN32
) return 0;
413 TRACE("(%04x,'%s')\n", hModule
, name
);
415 /* First handle names of the form '#xxxx' */
417 if (name
[0] == '#') return atoi( name
+ 1 );
419 /* Now copy and uppercase the string */
421 strcpy( buffer
, name
);
422 for (cpnt
= buffer
; *cpnt
; cpnt
++) *cpnt
= FILE_toupper(*cpnt
);
425 /* First search the resident names */
427 cpnt
= (char *)pModule
+ pModule
->name_table
;
429 /* Skip the first entry (module name) */
430 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
433 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
436 memcpy( &ordinal
, cpnt
+ *cpnt
+ 1, sizeof(ordinal
) );
437 TRACE(" Found: ordinal=%d\n", ordinal
);
440 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
443 /* Now search the non-resident names table */
445 if (!pModule
->nrname_handle
) return 0; /* No non-resident table */
446 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
448 /* Skip the first entry (module description string) */
449 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
452 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
455 memcpy( &ordinal
, cpnt
+ *cpnt
+ 1, sizeof(ordinal
) );
456 TRACE(" Found: ordinal=%d\n", ordinal
);
459 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
465 /***********************************************************************
468 FARPROC16 WINAPI
NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
470 return NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
473 /***********************************************************************
476 FARPROC16
NE_GetEntryPointEx( HMODULE16 hModule
, WORD ordinal
, BOOL16 snoop
)
484 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
485 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
487 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
488 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
492 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
495 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
496 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
500 memcpy( &offset
, &entry
->offs
, sizeof(WORD
) );
502 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
503 else sel
= GlobalHandleToSel16(NE_SEG_TABLE(pModule
)[sel
-1].hSeg
);
505 return (FARPROC16
)MAKESEGPTR( sel
, offset
);
507 return (FARPROC16
)MAKESEGPTR( sel
, offset
);
509 return (FARPROC16
)SNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)MAKESEGPTR( sel
, offset
));
513 /***********************************************************************
514 * EntryAddrProc (KERNEL.667) Wine-specific export
516 * Return the entry point for a given ordinal.
518 FARPROC16 WINAPI
EntryAddrProc16( HMODULE16 hModule
, WORD ordinal
)
520 FARPROC16 ret
= NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
521 CURRENT_STACK16
->ecx
= hModule
; /* FIXME: might be incorrect value */
525 /***********************************************************************
528 * Change the value of an entry point. Use with caution!
529 * It can only change the offset value, not the selector.
531 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
538 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
539 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
541 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
542 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
544 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
545 if (!(bundle
->next
)) return 0;
548 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
549 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
552 memcpy( &entry
->offs
, &offset
, sizeof(WORD
) );
557 /***********************************************************************
560 HANDLE
NE_OpenFile( NE_MODULE
*pModule
)
565 TRACE("(%p)\n", pModule
);
566 /* mjm - removed module caching because it keeps open file handles
567 thus preventing CDROMs from being ejected */
568 name
= NE_MODULE_NAME( pModule
);
569 if ((handle
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
570 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
571 MESSAGE( "Can't open file '%s' for module %04x\n", name
, pModule
->self
);
572 TRACE("opened '%s' -> %p\n", name
, handle
);
577 /* wrapper for SetFilePointer and ReadFile */
578 static BOOL
read_data( HANDLE handle
, LONG offset
, void *buffer
, DWORD size
)
582 if (SetFilePointer( handle
, offset
, NULL
, SEEK_SET
) == INVALID_SET_FILE_POINTER
) return FALSE
;
583 if (!ReadFile( handle
, buffer
, size
, &result
, NULL
)) return FALSE
;
584 return (result
== size
);
587 /***********************************************************************
590 static HMODULE16
NE_LoadExeHeader( HANDLE handle
, LPCSTR path
)
592 IMAGE_DOS_HEADER mz_header
;
593 IMAGE_OS2_HEADER ne_header
;
597 BYTE
*pData
, *pTempEntryTable
;
598 char *buffer
, *fastload
= NULL
;
599 int fastload_offset
= 0, fastload_length
= 0;
601 ET_BUNDLE
*bundle
, *oldbundle
;
604 /* Read a block from either the file or the fast-load area. */
605 #define READ(offset,size,buffer) \
606 ((fastload && ((offset) >= fastload_offset) && \
607 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
608 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
609 read_data( handle, (offset), (buffer), (size)))
611 if (!read_data( handle
, 0, &mz_header
, sizeof(mz_header
)) ||
612 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
613 return (HMODULE16
)11; /* invalid exe */
615 if (!read_data( handle
, mz_header
.e_lfanew
, &ne_header
, sizeof(ne_header
) ))
616 return (HMODULE16
)11; /* invalid exe */
618 if (ne_header
.ne_magic
== IMAGE_NT_SIGNATURE
) return (HMODULE16
)21; /* win32 exe */
619 if (ne_header
.ne_magic
== IMAGE_OS2_SIGNATURE_LX
) {
620 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
621 return (HMODULE16
)12;
623 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return (HMODULE16
)11; /* invalid exe */
625 /* We now have a valid NE header */
627 size
= sizeof(NE_MODULE
) +
629 ne_header
.ne_cseg
* sizeof(SEGTABLEENTRY
) +
631 ne_header
.ne_restab
- ne_header
.ne_rsrctab
+
632 /* resident names table */
633 ne_header
.ne_modtab
- ne_header
.ne_restab
+
634 /* module ref table */
635 ne_header
.ne_cmod
* sizeof(WORD
) +
636 /* imported names table */
637 ne_header
.ne_enttab
- ne_header
.ne_imptab
+
638 /* entry table length */
639 ne_header
.ne_cbenttab
+
640 /* entry table extra conversion space */
642 2 * (ne_header
.ne_cbenttab
- ne_header
.ne_cmovent
*6) +
643 /* loaded file info */
644 sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
) + 1;
646 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
647 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
649 FarSetOwner16( hModule
, hModule
);
650 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
651 memcpy( pModule
, &ne_header
, sizeof(ne_header
) );
653 /* check *programs* for default minimal stack size */
654 if ( (!(pModule
->flags
& NE_FFLAGS_LIBMODULE
))
655 && (pModule
->stack_size
< 0x1400) )
656 pModule
->stack_size
= 0x1400;
657 pModule
->module32
= 0;
658 pModule
->self
= hModule
;
659 pModule
->self_loading_sel
= 0;
660 pData
= (BYTE
*)(pModule
+ 1);
662 /* Clear internal Wine flags in case they are set in the EXE file */
664 pModule
->flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
666 /* Read the fast-load area */
668 if (ne_header
.ne_flagsothers
& NE_AFLAGS_FASTLOAD
)
670 fastload_offset
=ne_header
.ne_pretthunks
<< ne_header
.ne_align
;
671 fastload_length
=ne_header
.ne_psegrefbytes
<< ne_header
.ne_align
;
672 TRACE("Using fast-load area offset=%x len=%d\n",
673 fastload_offset
, fastload_length
);
674 if ((fastload
= HeapAlloc( GetProcessHeap(), 0, fastload_length
)) != NULL
)
676 if (!read_data( handle
, fastload_offset
, fastload
, fastload_length
))
678 HeapFree( GetProcessHeap(), 0, fastload
);
679 WARN("Error reading fast-load area!\n");
685 /* Get the segment table */
687 pModule
->seg_table
= pData
- (BYTE
*)pModule
;
688 buffer
= HeapAlloc( GetProcessHeap(), 0, ne_header
.ne_cseg
*
689 sizeof(struct ne_segment_table_entry_s
));
693 struct ne_segment_table_entry_s
*pSeg
;
695 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_segtab
,
696 ne_header
.ne_cseg
* sizeof(struct ne_segment_table_entry_s
),
699 HeapFree( GetProcessHeap(), 0, buffer
);
700 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
701 GlobalFree16( hModule
);
702 return (HMODULE16
)11; /* invalid exe */
704 pSeg
= (struct ne_segment_table_entry_s
*)buffer
;
705 for (i
= ne_header
.ne_cseg
; i
> 0; i
--, pSeg
++)
707 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
708 pData
+= sizeof(SEGTABLEENTRY
);
710 HeapFree( GetProcessHeap(), 0, buffer
);
714 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
715 GlobalFree16( hModule
);
716 return (HMODULE16
)11; /* invalid exe */
719 /* Get the resource table */
721 if (ne_header
.ne_rsrctab
< ne_header
.ne_restab
)
723 pModule
->res_table
= pData
- (BYTE
*)pModule
;
724 if (!READ(mz_header
.e_lfanew
+ ne_header
.ne_rsrctab
,
725 ne_header
.ne_restab
- ne_header
.ne_rsrctab
,
727 return (HMODULE16
)11; /* invalid exe */
728 pData
+= ne_header
.ne_restab
- ne_header
.ne_rsrctab
;
729 NE_InitResourceHandler( pModule
);
731 else pModule
->res_table
= 0; /* No resource table */
733 /* Get the resident names table */
735 pModule
->name_table
= pData
- (BYTE
*)pModule
;
736 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_restab
,
737 ne_header
.ne_modtab
- ne_header
.ne_restab
,
740 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
741 GlobalFree16( hModule
);
742 return (HMODULE16
)11; /* invalid exe */
744 pData
+= ne_header
.ne_modtab
- ne_header
.ne_restab
;
746 /* Get the module references table */
748 if (ne_header
.ne_cmod
> 0)
750 pModule
->modref_table
= pData
- (BYTE
*)pModule
;
751 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_modtab
,
752 ne_header
.ne_cmod
* sizeof(WORD
),
755 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
756 GlobalFree16( hModule
);
757 return (HMODULE16
)11; /* invalid exe */
759 pData
+= ne_header
.ne_cmod
* sizeof(WORD
);
761 else pModule
->modref_table
= 0; /* No module references */
763 /* Get the imported names table */
765 pModule
->import_table
= pData
- (BYTE
*)pModule
;
766 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_imptab
,
767 ne_header
.ne_enttab
- ne_header
.ne_imptab
,
770 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
771 GlobalFree16( hModule
);
772 return (HMODULE16
)11; /* invalid exe */
774 pData
+= ne_header
.ne_enttab
- ne_header
.ne_imptab
;
776 /* Load entry table, convert it to the optimized version used by Windows */
778 if ((pTempEntryTable
= HeapAlloc( GetProcessHeap(), 0, ne_header
.ne_cbenttab
)) != NULL
)
780 BYTE nr_entries
, type
, *s
;
782 TRACE("Converting entry table.\n");
783 pModule
->entry_table
= pData
- (BYTE
*)pModule
;
784 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_enttab
,
785 ne_header
.ne_cbenttab
, pTempEntryTable
))
787 HeapFree( GetProcessHeap(), 0, pTempEntryTable
);
788 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
789 GlobalFree16( hModule
);
790 return (HMODULE16
)11; /* invalid exe */
794 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header
.ne_enttab
, ne_header
.ne_cbenttab
, *s
);
796 bundle
= (ET_BUNDLE
*)pData
;
797 TRACE("first bundle: %p\n", bundle
);
798 memset(bundle
, 0, sizeof(ET_BUNDLE
)); /* in case no entry table exists */
799 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
801 while ((nr_entries
= *s
++))
805 bundle
->last
+= nr_entries
;
812 entry
->segnum
= *s
++;
813 entry
->offs
= *(WORD
*)s
; s
+= 2;
814 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
822 entry
->segnum
= type
;
823 entry
->offs
= *(WORD
*)s
; s
+= 2;
824 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
830 if (bundle
->first
== bundle
->last
)
832 bundle
->first
+= nr_entries
;
833 bundle
->last
+= nr_entries
;
838 oldbundle
->next
= ((int)entry
- (int)pModule
);
839 bundle
= (ET_BUNDLE
*)entry
;
840 TRACE("new bundle: %p\n", bundle
);
841 bundle
->first
= bundle
->last
=
842 oldbundle
->last
+ nr_entries
;
844 (BYTE
*)entry
+= sizeof(ET_BUNDLE
);
848 HeapFree( GetProcessHeap(), 0, pTempEntryTable
);
852 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
853 GlobalFree16( hModule
);
854 return (HMODULE16
)11; /* invalid exe */
857 pData
+= ne_header
.ne_cbenttab
+ sizeof(ET_BUNDLE
) +
858 2 * (ne_header
.ne_cbenttab
- ne_header
.ne_cmovent
*6);
860 if ((DWORD
)entry
> (DWORD
)pData
)
861 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry
, pData
);
863 /* Store the filename information */
865 pModule
->fileinfo
= pData
- (BYTE
*)pModule
;
866 size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
) + 1;
867 ofs
= (OFSTRUCT
*)pData
;
868 ofs
->cBytes
= size
- 1;
870 strcpy( ofs
->szPathName
, path
);
873 /* Free the fast-load area */
876 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
878 /* Get the non-resident names table */
880 if (ne_header
.ne_cbnrestab
)
882 pModule
->nrname_handle
= GlobalAlloc16( 0, ne_header
.ne_cbnrestab
);
883 if (!pModule
->nrname_handle
)
885 GlobalFree16( hModule
);
886 return (HMODULE16
)11; /* invalid exe */
888 FarSetOwner16( pModule
->nrname_handle
, hModule
);
889 buffer
= GlobalLock16( pModule
->nrname_handle
);
890 if (!read_data( handle
, ne_header
.ne_nrestab
, buffer
, ne_header
.ne_cbnrestab
))
892 GlobalFree16( pModule
->nrname_handle
);
893 GlobalFree16( hModule
);
894 return (HMODULE16
)11; /* invalid exe */
897 else pModule
->nrname_handle
= 0;
899 /* Allocate a segment for the implicitly-loaded DLLs */
901 if (pModule
->modref_count
)
903 pModule
->dlls_to_init
= GlobalAlloc16( GMEM_ZEROINIT
,
904 (pModule
->modref_count
+1)*sizeof(HMODULE16
) );
905 if (!pModule
->dlls_to_init
)
907 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
908 GlobalFree16( hModule
);
909 return (HMODULE16
)11; /* invalid exe */
911 FarSetOwner16( pModule
->dlls_to_init
, hModule
);
913 else pModule
->dlls_to_init
= 0;
915 NE_RegisterModule( pModule
);
916 SNOOP16_RegisterDLL(pModule
,path
);
921 /***********************************************************************
924 * Load all DLLs implicitly linked to a module.
926 static BOOL
NE_LoadDLLs( NE_MODULE
*pModule
)
929 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->modref_table
);
930 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
932 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
934 char buffer
[260], *p
;
935 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->import_table
+ *pModRef
;
936 memcpy( buffer
, pstr
+ 1, *pstr
);
937 *(buffer
+ *pstr
) = 0; /* terminate it */
939 TRACE("Loading '%s'\n", buffer
);
940 if (!(*pModRef
= GetModuleHandle16( buffer
)))
942 /* If the DLL is not loaded yet, load it and store */
943 /* its handle in the list of DLLs to initialize. */
946 /* Append .DLL to name if no extension present */
947 if (!(p
= strrchr( buffer
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
948 strcat( buffer
, ".DLL" );
950 if ((hDLL
= MODULE_LoadModule16( buffer
, TRUE
, TRUE
)) < 32)
952 /* FIXME: cleanup what was done */
954 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
955 buffer
, *((BYTE
*)pModule
+ pModule
->name_table
),
956 (char *)pModule
+ pModule
->name_table
+ 1, hDLL
);
959 *pModRef
= GetExePtr( hDLL
);
962 else /* Increment the reference count of the DLL */
964 NE_MODULE
*pOldDLL
= NE_GetPtr( *pModRef
);
965 if (pOldDLL
) pOldDLL
->count
++;
972 /**********************************************************************
975 * Load first instance of NE module from file.
977 * pModule must point to a module structure prepared by NE_LoadExeHeader.
978 * This routine must never be called twice on a module.
981 static HINSTANCE16
NE_DoLoadModule( NE_MODULE
*pModule
)
983 /* Allocate the segments for this module */
985 if (!NE_CreateAllSegments( pModule
))
986 return ERROR_NOT_ENOUGH_MEMORY
; /* 8 */
988 /* Load the referenced DLLs */
990 if (!NE_LoadDLLs( pModule
))
991 return ERROR_FILE_NOT_FOUND
; /* 2 */
993 /* Load the segments */
995 NE_LoadAllSegments( pModule
);
997 /* Make sure the usage count is 1 on the first loading of */
998 /* the module, even if it contains circular DLL references */
1002 return NE_GetInstance( pModule
);
1005 /**********************************************************************
1008 * Load first instance of NE module. (Note: caller is responsible for
1009 * ensuring the module isn't already loaded!)
1011 * If the module turns out to be an executable module, only a
1012 * handle to a module stub is returned; this needs to be initialized
1013 * by calling NE_DoLoadModule later, in the context of the newly
1016 * If lib_only is TRUE, however, the module is perforce treated
1017 * like a DLL module, even if it is an executable module.
1020 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
)
1024 HINSTANCE16 hInstance
;
1029 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
1030 return (HMODULE16
)2; /* File not found */
1032 hModule
= NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile
), ofs
.szPathName
);
1034 if (hModule
< 32) return hModule
;
1036 pModule
= NE_GetPtr( hModule
);
1037 if ( !pModule
) return hModule
;
1039 if ( !lib_only
&& !( pModule
->flags
& NE_FFLAGS_LIBMODULE
) )
1042 hInstance
= NE_DoLoadModule( pModule
);
1043 if ( hInstance
< 32 )
1046 NE_FreeModule( hModule
, 0 );
1053 /***********************************************************************
1054 * NE_DoLoadBuiltinModule
1056 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1058 static HMODULE16
NE_DoLoadBuiltinModule( const BUILTIN16_DESCRIPTOR
*descr
)
1062 SEGTABLEENTRY
*pSegTable
;
1065 hModule
= GLOBAL_CreateBlock( GMEM_MOVEABLE
, descr
->module_start
,
1066 descr
->module_size
, 0, WINE_LDT_FLAGS_DATA
);
1067 if (!hModule
) return 0;
1068 FarSetOwner16( hModule
, hModule
);
1070 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
1071 pModule
->self
= hModule
;
1072 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
1073 pModule
->hRsrcMap
= (void *)descr
->rsrc
;
1075 /* Allocate the code segment */
1077 pSegTable
= NE_SEG_TABLE( pModule
);
1078 pSegTable
->hSeg
= GLOBAL_CreateBlock( GMEM_FIXED
, descr
->code_start
,
1079 pSegTable
->minsize
, hModule
,
1080 WINE_LDT_FLAGS_CODE
|WINE_LDT_FLAGS_32BIT
);
1081 if (!pSegTable
->hSeg
) return 0;
1082 patch_code_segment( descr
->code_start
);
1085 /* Allocate the data segment */
1087 minsize
= pSegTable
->minsize
? pSegTable
->minsize
: 0x10000;
1088 minsize
+= pModule
->heap_size
;
1089 if (minsize
> 0x10000) minsize
= 0x10000;
1090 pSegTable
->hSeg
= GlobalAlloc16( GMEM_FIXED
, minsize
);
1091 if (!pSegTable
->hSeg
) return 0;
1092 FarSetOwner16( pSegTable
->hSeg
, hModule
);
1093 if (pSegTable
->minsize
) memcpy( GlobalLock16( pSegTable
->hSeg
),
1094 descr
->data_start
, pSegTable
->minsize
);
1095 if (pModule
->heap_size
)
1096 LocalInit16( GlobalHandleToSel16(pSegTable
->hSeg
), pSegTable
->minsize
, minsize
);
1098 if (descr
->rsrc
) NE_InitResourceHandler(pModule
);
1100 NE_RegisterModule( pModule
);
1102 /* make sure the 32-bit library containing this one is loaded too */
1103 LoadLibraryA( descr
->owner
);
1109 /***********************************************************************
1110 * NE_LoadBuiltinModule
1112 * Load a built-in module.
1114 static HMODULE16
NE_LoadBuiltinModule( LPCSTR name
)
1116 const BUILTIN16_DESCRIPTOR
*descr
;
1117 char error
[256], dllname
[20], *p
;
1121 /* Fix the name in case we have a full path and extension */
1123 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
1124 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
1126 if (strlen(name
) >= sizeof(dllname
)-4) return (HMODULE16
)2;
1128 strcpy( dllname
, name
);
1129 p
= strrchr( dllname
, '.' );
1130 if (!p
) strcat( dllname
, ".dll" );
1131 for (p
= dllname
; *p
; p
++) *p
= FILE_tolower(*p
);
1133 if ((descr
= find_dll_descr( dllname
)))
1134 return NE_DoLoadBuiltinModule( descr
);
1136 if ((handle
= wine_dll_load( dllname
, error
, sizeof(error
), &file_exists
)))
1138 if ((descr
= find_dll_descr( dllname
)))
1139 return NE_DoLoadBuiltinModule( descr
);
1141 ERR( "loaded .so but dll %s still not found\n", dllname
);
1145 if (!file_exists
) WARN("cannot open .so lib for 16-bit builtin %s: %s\n", name
, error
);
1146 else ERR("failed to load .so lib for 16-bit builtin %s: %s\n", name
, error
);
1148 return (HMODULE16
)2;
1152 /**********************************************************************
1153 * MODULE_LoadModule16
1155 * Load a NE module in the order of the loadorder specification.
1156 * The caller is responsible that the module is not loaded already.
1159 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
)
1161 HINSTANCE16 hinst
= 2;
1162 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
1164 const char *filetype
= "";
1165 const char *ptr
, *basename
;
1167 /* strip path information */
1170 if (basename
[0] && basename
[1] == ':') basename
+= 2; /* strip drive specification */
1171 if ((ptr
= strrchr( basename
, '\\' ))) basename
= ptr
+ 1;
1172 if ((ptr
= strrchr( basename
, '/' ))) basename
= ptr
+ 1;
1174 if (is_builtin_present(basename
))
1176 TRACE( "forcing loadorder to builtin for %s\n", debugstr_a(basename
) );
1177 /* force builtin loadorder since the dll is already in memory */
1178 loadorder
[0] = LOADORDER_BI
;
1179 loadorder
[1] = LOADORDER_INVALID
;
1181 else MODULE_GetLoadOrder(loadorder
, basename
, FALSE
);
1183 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
1185 if (loadorder
[i
] == LOADORDER_INVALID
) break;
1187 switch(loadorder
[i
])
1190 TRACE("Trying native dll '%s'\n", libname
);
1191 hinst
= NE_LoadModule(libname
, lib_only
);
1192 filetype
= "native";
1196 TRACE("Trying built-in '%s'\n", libname
);
1197 hinst
= NE_LoadBuiltinModule(libname
);
1198 filetype
= "builtin";
1208 TRACE_(loaddll
)("Loaded module '%s' : %s\n", libname
, filetype
);
1214 hModule
= GetModuleHandle16(libname
);
1217 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1219 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1222 pModule
= NE_GetPtr(hModule
);
1225 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1227 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1230 TRACE("Loaded module '%s' at 0x%04x.\n", libname
, hinst
);
1233 * Call initialization routines for all loaded DLLs. Note that
1234 * when we load implicitly linked DLLs this will be done by InitTask().
1236 if(pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1238 NE_InitializeDLLs(hModule
);
1239 NE_DllProcessAttach(hModule
);
1247 /* We quit searching when we get another error than 'File not found' */
1251 return hinst
; /* The last error that occurred */
1255 /**********************************************************************
1258 * Create the thread for a 16-bit module.
1260 static HINSTANCE16
NE_CreateThread( NE_MODULE
*pModule
, WORD cmdShow
, LPCSTR cmdline
)
1265 HINSTANCE16 instance
= 0;
1267 if (!(hTask
= TASK_SpawnTask( pModule
, cmdShow
, cmdline
+ 1, *cmdline
, &hThread
)))
1270 /* Post event to start the task */
1271 PostEvent16( hTask
);
1273 /* Wait until we get the instance handle */
1276 DirectedYield16( hTask
);
1277 if (!IsTask16( hTask
)) /* thread has died */
1280 WaitForSingleObject( hThread
, INFINITE
);
1281 GetExitCodeThread( hThread
, &exit_code
);
1282 CloseHandle( hThread
);
1285 if (!(pTask
= GlobalLock16( hTask
))) break;
1286 instance
= pTask
->hInstance
;
1287 GlobalUnlock16( hTask
);
1288 } while (!instance
);
1290 CloseHandle( hThread
);
1295 /**********************************************************************
1296 * LoadModule (KERNEL.45)
1298 HINSTANCE16 WINAPI
LoadModule16( LPCSTR name
, LPVOID paramBlock
)
1300 BOOL lib_only
= !paramBlock
|| (paramBlock
== (LPVOID
)-1);
1301 LOADPARAMS16
*params
;
1309 if ( (hModule
= NE_GetModuleByFilename(name
) ) != 0 )
1311 /* Special case: second instance of an already loaded NE module */
1313 if ( !( pModule
= NE_GetPtr( hModule
) ) ) return (HINSTANCE16
)11;
1314 if ( pModule
->module32
) return (HINSTANCE16
)21;
1316 /* Increment refcount */
1322 /* Main case: load first instance of NE module */
1324 if ( (hModule
= MODULE_LoadModule16( name
, FALSE
, lib_only
)) < 32 )
1327 if ( !(pModule
= NE_GetPtr( hModule
)) )
1328 return (HINSTANCE16
)11;
1331 /* If library module, we just retrieve the instance handle */
1333 if ( ( pModule
->flags
& NE_FFLAGS_LIBMODULE
) || lib_only
)
1334 return NE_GetInstance( pModule
);
1337 * At this point, we need to create a new process.
1339 * pModule points either to an already loaded module, whose refcount
1340 * has already been incremented (to avoid having the module vanish
1341 * in the meantime), or else to a stub module which contains only header
1344 params
= (LOADPARAMS16
*)paramBlock
;
1345 cmdShow
= ((WORD
*)MapSL(params
->showCmd
))[1];
1346 cmdline
= MapSL( params
->cmdLine
);
1347 return NE_CreateThread( pModule
, cmdShow
, cmdline
);
1351 /**********************************************************************
1354 * Startup code for a new 16-bit task.
1356 DWORD
NE_StartTask(void)
1358 TDB
*pTask
= TASK_GetCurrent();
1359 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
1360 HINSTANCE16 hInstance
, hPrevInstance
;
1361 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
1364 if ( pModule
->count
> 0 )
1366 /* Second instance of an already loaded NE module */
1367 /* Note that the refcount was already incremented by the parent */
1369 hPrevInstance
= NE_GetInstance( pModule
);
1371 if ( pModule
->dgroup
)
1372 if ( NE_CreateSegment( pModule
, pModule
->dgroup
) )
1373 NE_LoadSegment( pModule
, pModule
->dgroup
);
1375 hInstance
= NE_GetInstance( pModule
);
1376 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance
, pModule
->dgroup
, hPrevInstance
);
1381 /* Load first instance of NE module */
1383 pModule
->flags
|= NE_FFLAGS_GUI
; /* FIXME: is this necessary? */
1385 hInstance
= NE_DoLoadModule( pModule
);
1389 if ( hInstance
>= 32 )
1393 /* Enter instance handles into task struct */
1395 pTask
->hInstance
= hInstance
;
1396 pTask
->hPrevInstance
= hPrevInstance
;
1398 /* Use DGROUP for 16-bit stack */
1400 if (!(sp
= pModule
->sp
))
1401 sp
= pSegTable
[pModule
->ss
-1].minsize
+ pModule
->stack_size
;
1403 sp
-= sizeof(STACK16FRAME
);
1404 NtCurrentTeb()->cur_stack
= MAKESEGPTR( GlobalHandleToSel16(hInstance
), sp
);
1406 /* Registers at initialization must be:
1408 * bx stack size in bytes
1409 * cx heap size in bytes
1410 * si previous app instance
1411 * di current app instance
1413 * es selector to the PSP
1414 * ds dgroup of the application
1416 * sp top of the stack
1418 memset( &context
, 0, sizeof(context
) );
1419 context
.SegCs
= GlobalHandleToSel16(pSegTable
[pModule
->cs
- 1].hSeg
);
1420 context
.SegDs
= GlobalHandleToSel16(pTask
->hInstance
);
1421 context
.SegEs
= pTask
->hPDB
;
1422 context
.Eip
= pModule
->ip
;
1423 context
.Ebx
= pModule
->stack_size
;
1424 context
.Ecx
= pModule
->heap_size
;
1425 context
.Edi
= pTask
->hInstance
;
1426 context
.Esi
= pTask
->hPrevInstance
;
1428 /* Now call 16-bit entry point */
1430 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1431 context
.SegCs
, context
.Eip
, context
.SegDs
,
1432 SELECTOROF(NtCurrentTeb()->cur_stack
),
1433 OFFSETOF(NtCurrentTeb()->cur_stack
) );
1435 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
1436 ExitThread( LOWORD(context
.Eax
) );
1438 return hInstance
; /* error code */
1441 /***********************************************************************
1442 * LoadLibrary (KERNEL.95)
1443 * LoadLibrary16 (KERNEL32.35)
1445 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
1447 return LoadModule16(libname
, (LPVOID
)-1 );
1451 /**********************************************************************
1454 * Call a DLL's WEP, allowing it to shut down.
1455 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1457 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
1460 FARPROC16 WEP
= GetProcAddress16( hModule
, "WEP" );
1461 if (!WEP
) return FALSE
;
1468 args
[0] = WEP_FREE_DLL
;
1469 WOWCallback16Ex( (DWORD
)WEP
, WCB16_PASCAL
, sizeof(args
), args
, &dwRet
);
1470 ret
= LOWORD(dwRet
);
1472 __EXCEPT(page_fault
)
1474 WARN("Page fault\n");
1483 /**********************************************************************
1486 * Implementation of FreeModule16().
1488 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
)
1490 HMODULE16
*hPrevModule
;
1495 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
1496 hModule
= pModule
->self
;
1498 TRACE("%04x count %d\n", hModule
, pModule
->count
);
1500 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
1501 else pModule
->count
= 0;
1503 if (pModule
->flags
& NE_FFLAGS_BUILTIN
)
1504 return FALSE
; /* Can't free built-in module */
1506 if (call_wep
&& !(pModule
->flags
& NE_FFLAGS_WIN32
))
1508 /* Free the objects owned by the DLL module */
1509 NE_CallUserSignalProc( hModule
, USIG16_DLL_UNLOAD
);
1511 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1512 MODULE_CallWEP( hModule
);
1514 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
1518 /* Clear magic number just in case */
1520 pModule
->magic
= pModule
->self
= 0;
1522 /* Remove it from the linked list */
1524 hPrevModule
= &hFirstModule
;
1525 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
1527 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
1529 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
1531 /* Free the referenced modules */
1533 pModRef
= (HMODULE16
*)NE_MODULE_TABLE( pModule
);
1534 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
1536 NE_FreeModule( *pModRef
, call_wep
);
1539 /* Free the module storage */
1541 GlobalFreeAll16( hModule
);
1543 /* Remove module from cache */
1545 if (pCachedModule
== pModule
) pCachedModule
= NULL
;
1550 /**********************************************************************
1551 * FreeModule (KERNEL.46)
1553 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
1555 return NE_FreeModule( hModule
, TRUE
);
1559 /***********************************************************************
1560 * FreeLibrary (KERNEL.96)
1561 * FreeLibrary16 (KERNEL32.36)
1563 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
1565 TRACE("%04x\n", handle
);
1566 FreeModule16( handle
);
1570 /**********************************************************************
1571 * GetModuleName (KERNEL.27)
1573 BOOL16 WINAPI
GetModuleName16( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
1578 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
1579 p
= (BYTE
*)pModule
+ pModule
->name_table
;
1580 if (count
> *p
) count
= *p
+ 1;
1583 memcpy( buf
, p
+ 1, count
- 1 );
1584 buf
[count
-1] = '\0';
1590 /**********************************************************************
1591 * GetModuleUsage (KERNEL.48)
1593 INT16 WINAPI
GetModuleUsage16( HINSTANCE16 hModule
)
1595 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1596 return pModule
? pModule
->count
: 0;
1600 /**********************************************************************
1601 * GetExpWinVer (KERNEL.167)
1603 WORD WINAPI
GetExpWinVer16( HMODULE16 hModule
)
1605 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1606 if ( !pModule
) return 0;
1609 * For built-in modules, fake the expected version the module should
1610 * have according to the Windows version emulated by Wine
1612 if ( !pModule
->expected_version
)
1614 OSVERSIONINFOA versionInfo
;
1615 versionInfo
.dwOSVersionInfoSize
= sizeof(versionInfo
);
1617 if ( GetVersionExA( &versionInfo
) )
1618 pModule
->expected_version
=
1619 (versionInfo
.dwMajorVersion
& 0xff) << 8
1620 | (versionInfo
.dwMinorVersion
& 0xff);
1623 return pModule
->expected_version
;
1627 /***********************************************************************
1628 * WinExec (KERNEL.166)
1630 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
1632 LPCSTR p
, args
= NULL
;
1633 LPCSTR name_beg
, name_end
;
1634 LPSTR name
, cmdline
;
1637 char buffer
[MAX_PATH
];
1639 if (*lpCmdLine
== '"') /* has to be only one and only at beginning ! */
1641 name_beg
= lpCmdLine
+1;
1642 p
= strchr ( lpCmdLine
+1, '"' );
1646 args
= strchr ( p
, ' ' );
1648 else /* yes, even valid with trailing '"' missing */
1649 name_end
= lpCmdLine
+strlen(lpCmdLine
);
1653 name_beg
= lpCmdLine
;
1654 args
= strchr( lpCmdLine
, ' ' );
1655 name_end
= args
? args
: lpCmdLine
+strlen(lpCmdLine
);
1658 if ((name_beg
== lpCmdLine
) && (!args
))
1659 { /* just use the original cmdline string as file name */
1660 name
= (LPSTR
)lpCmdLine
;
1664 if (!(name
= HeapAlloc( GetProcessHeap(), 0, name_end
- name_beg
+ 1 )))
1665 return ERROR_NOT_ENOUGH_MEMORY
;
1666 memcpy( name
, name_beg
, name_end
- name_beg
);
1667 name
[name_end
- name_beg
] = '\0';
1673 arglen
= strlen(args
);
1674 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 + arglen
);
1675 cmdline
[0] = (BYTE
)arglen
;
1676 strcpy( cmdline
+ 1, args
);
1680 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 );
1681 cmdline
[0] = cmdline
[1] = 0;
1684 TRACE("name: '%s', cmdline: '%.*s'\n", name
, cmdline
[0], &cmdline
[1]);
1686 if (SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, NULL
))
1688 LOADPARAMS16 params
;
1691 showCmd
[1] = nCmdShow
;
1693 params
.hEnvironment
= 0;
1694 params
.cmdLine
= MapLS( cmdline
);
1695 params
.showCmd
= MapLS( showCmd
);
1696 params
.reserved
= 0;
1698 ret
= LoadModule16( buffer
, ¶ms
);
1699 UnMapLS( params
.cmdLine
);
1700 UnMapLS( params
.showCmd
);
1702 else ret
= GetLastError();
1704 HeapFree( GetProcessHeap(), 0, cmdline
);
1705 if (name
!= lpCmdLine
) HeapFree( GetProcessHeap(), 0, name
);
1707 if (ret
== 21) /* 32-bit module */
1710 ReleaseThunkLock( &count
);
1711 ret
= LOWORD( WinExec( lpCmdLine
, nCmdShow
) );
1712 RestoreThunkLock( count
);
1717 /***********************************************************************
1718 * GetProcAddress (KERNEL.50)
1720 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, LPCSTR name
)
1725 if (!hModule
) hModule
= GetCurrentTask();
1726 hModule
= GetExePtr( hModule
);
1728 if (HIWORD(name
) != 0)
1730 ordinal
= NE_GetOrdinal( hModule
, name
);
1731 TRACE("%04x '%s'\n", hModule
, name
);
1735 ordinal
= LOWORD(name
);
1736 TRACE("%04x %04x\n", hModule
, ordinal
);
1738 if (!ordinal
) return (FARPROC16
)0;
1740 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1742 TRACE("returning %08x\n", (UINT
)ret
);
1747 /***************************************************************************
1748 * HasGPHandler (KERNEL.338)
1750 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1755 GPHANDLERDEF
*gpHandler
;
1757 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1758 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1759 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1760 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1761 && (gpHandler
= MapSL( gpPtr
)) != NULL
)
1763 while (gpHandler
->selector
)
1765 if ( SELECTOROF(address
) == gpHandler
->selector
1766 && OFFSETOF(address
) >= gpHandler
->rangeStart
1767 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1768 return MAKESEGPTR( gpHandler
->selector
, gpHandler
->handler
);
1777 /**********************************************************************
1778 * GetModuleHandle (KERNEL.47)
1780 * Find a module from a module name.
1782 * NOTE: The current implementation works the same way the Windows 95 one
1783 * does. Do not try to 'fix' it, fix the callers.
1784 * + It does not do ANY extension handling (except that strange .EXE bit)!
1785 * + It does not care about paths, just about basenames. (same as Windows)
1789 * the win16 module handle if found
1791 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1792 * Always hFirstModule
1794 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1796 if (HIWORD(name
) == 0)
1797 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1798 return MAKELONG(GetModuleHandle16( MapSL(name
)), hFirstModule
);
1801 /**********************************************************************
1802 * NE_GetModuleByFilename
1804 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
)
1808 BYTE len
, *name_table
;
1809 char tmpstr
[MAX_PATH
];
1812 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1814 /* If the base filename of 'name' matches the base filename of the module
1815 * filename of some module (case-insensitive compare):
1816 * Return its handle.
1819 /* basename: search backwards in passed name to \ / or : */
1820 s
= tmpstr
+ strlen(tmpstr
);
1823 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1828 /* search this in loaded filename list */
1829 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1834 pModule
= NE_GetPtr( hModule
);
1835 if (!pModule
) break;
1836 if (!pModule
->fileinfo
) continue;
1837 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1839 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1840 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1841 /* basename: search backwards in pathname to \ / or : */
1842 while (loadedfn
> (char*)ofs
->szPathName
)
1844 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1848 /* case insensitive compare ... */
1849 if (!FILE_strcasecmp(loadedfn
, s
))
1852 /* If basename (without ext) matches the module name of a module:
1853 * Return its handle.
1856 if ( (p
= strrchr( s
, '.' )) != NULL
) *p
= '\0';
1859 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1861 pModule
= NE_GetPtr( hModule
);
1862 if (!pModule
) break;
1863 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1865 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1866 if ((*name_table
== len
) && !FILE_strncasecmp(s
, name_table
+1, len
))
1873 /***********************************************************************
1874 * GetProcAddress16 (KERNEL32.37)
1875 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1877 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1879 if (!hModule
) return 0;
1880 if (HIWORD(hModule
))
1882 WARN("hModule is Win32 handle (%p)\n", hModule
);
1885 return GetProcAddress16( LOWORD(hModule
), name
);
1888 /**********************************************************************
1889 * ModuleFirst (TOOLHELP.59)
1891 BOOL16 WINAPI
ModuleFirst16( MODULEENTRY
*lpme
)
1893 lpme
->wNext
= hFirstModule
;
1894 return ModuleNext16( lpme
);
1898 /**********************************************************************
1899 * ModuleNext (TOOLHELP.60)
1901 BOOL16 WINAPI
ModuleNext16( MODULEENTRY
*lpme
)
1906 if (!lpme
->wNext
) return FALSE
;
1907 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1908 name
= (char *)pModule
+ pModule
->name_table
;
1909 memcpy( lpme
->szModule
, name
+ 1, min(*name
, MAX_MODULE_NAME
) );
1910 lpme
->szModule
[min(*name
, MAX_MODULE_NAME
)] = '\0';
1911 lpme
->hModule
= lpme
->wNext
;
1912 lpme
->wcUsage
= pModule
->count
;
1913 lstrcpynA( lpme
->szExePath
, NE_MODULE_NAME(pModule
), sizeof(lpme
->szExePath
) );
1914 lpme
->wNext
= pModule
->next
;
1919 /**********************************************************************
1920 * ModuleFindName (TOOLHELP.61)
1922 BOOL16 WINAPI
ModuleFindName16( MODULEENTRY
*lpme
, LPCSTR name
)
1924 lpme
->wNext
= GetModuleHandle16( name
);
1925 return ModuleNext16( lpme
);
1929 /**********************************************************************
1930 * ModuleFindHandle (TOOLHELP.62)
1932 BOOL16 WINAPI
ModuleFindHandle16( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1934 hModule
= GetExePtr( hModule
);
1935 lpme
->wNext
= hModule
;
1936 return ModuleNext16( lpme
);
1940 /***************************************************************************
1941 * IsRomModule (KERNEL.323)
1943 BOOL16 WINAPI
IsRomModule16( HMODULE16 unused
)
1948 /***************************************************************************
1949 * IsRomFile (KERNEL.326)
1951 BOOL16 WINAPI
IsRomFile16( HFILE16 unused
)
1956 /***********************************************************************
1957 * create_dummy_module
1959 * Create a dummy NE module for Win32 or Winelib.
1961 static HMODULE16
create_dummy_module( HMODULE module32
)
1965 SEGTABLEENTRY
*pSegment
;
1968 const char* basename
;
1971 char filename
[MAX_PATH
];
1972 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module32
);
1974 if (!nt
) return (HMODULE16
)11; /* invalid exe */
1976 /* Extract base filename */
1977 GetModuleFileNameA( module32
, filename
, sizeof(filename
) );
1978 basename
= strrchr(filename
, '\\');
1979 if (!basename
) basename
= filename
;
1981 len
= strlen(basename
);
1982 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
1984 /* Allocate module */
1985 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
1986 + strlen(filename
) + 1;
1987 size
= sizeof(NE_MODULE
) +
1988 /* loaded file info */
1989 ((of_size
+ 3) & ~3) +
1990 /* segment table: DS,CS */
1991 2 * sizeof(SEGTABLEENTRY
) +
1994 /* several empty tables */
1997 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
1998 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
2000 FarSetOwner16( hModule
, hModule
);
2001 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
2003 /* Set all used entries */
2004 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
2007 pModule
->flags
= NE_FFLAGS_WIN32
;
2008 pModule
->dgroup
= 0;
2011 pModule
->heap_size
= 0;
2012 pModule
->stack_size
= 0;
2013 pModule
->seg_count
= 2;
2014 pModule
->modref_count
= 0;
2015 pModule
->nrname_size
= 0;
2016 pModule
->fileinfo
= sizeof(NE_MODULE
);
2017 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
2018 pModule
->self
= hModule
;
2019 pModule
->module32
= module32
;
2021 /* Set version and flags */
2022 pModule
->expected_version
= ((nt
->OptionalHeader
.MajorSubsystemVersion
& 0xff) << 8 ) |
2023 (nt
->OptionalHeader
.MinorSubsystemVersion
& 0xff);
2024 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
2025 pModule
->flags
|= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
;
2027 /* Set loaded file information */
2028 ofs
= (OFSTRUCT
*)(pModule
+ 1);
2029 memset( ofs
, 0, of_size
);
2030 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
2031 strcpy( ofs
->szPathName
, filename
);
2033 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + ((of_size
+ 3) & ~3));
2034 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
2037 pSegment
->flags
= NE_SEGFLAGS_DATA
;
2038 pSegment
->minsize
= 0x1000;
2041 pSegment
->flags
= 0;
2045 pStr
= (char *)pSegment
;
2046 pModule
->name_table
= (int)pStr
- (int)pModule
;
2049 lstrcpynA( pStr
+1, basename
, len
+1 );
2052 /* All tables zero terminated */
2053 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
= (int)pStr
- (int)pModule
;
2055 NE_RegisterModule( pModule
);
2056 LoadLibraryA( filename
); /* increment the ref count of the 32-bit module */
2060 /***********************************************************************
2061 * PrivateLoadLibrary (KERNEL32.@)
2063 * FIXME: rough guesswork, don't know what "Private" means
2065 HINSTANCE16 WINAPI
PrivateLoadLibrary(LPCSTR libname
)
2067 return LoadLibrary16(libname
);
2070 /***********************************************************************
2071 * PrivateFreeLibrary (KERNEL32.@)
2073 * FIXME: rough guesswork, don't know what "Private" means
2075 void WINAPI
PrivateFreeLibrary(HINSTANCE16 handle
)
2077 FreeLibrary16(handle
);
2080 /***************************************************************************
2081 * MapHModuleLS (KERNEL32.@)
2083 HMODULE16 WINAPI
MapHModuleLS(HMODULE hmod
)
2089 return TASK_GetCurrent()->hInstance
;
2091 return LOWORD(hmod
); /* we already have a 16 bit module handle */
2092 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
2094 if (pModule
->module32
== hmod
)
2095 return pModule
->self
;
2096 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
2098 if ((ret
= create_dummy_module( hmod
)) < 32)
2106 /***************************************************************************
2107 * MapHModuleSL (KERNEL32.@)
2109 HMODULE WINAPI
MapHModuleSL(HMODULE16 hmod
)
2114 TDB
*pTask
= TASK_GetCurrent();
2115 hmod
= pTask
->hModule
;
2117 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
2118 if ((pModule
->magic
!=IMAGE_OS2_SIGNATURE
) || !(pModule
->flags
& NE_FFLAGS_WIN32
))
2120 return pModule
->module32
;
2123 /***************************************************************************
2124 * MapHInstLS (KERNEL32.@)
2125 * MapHInstLS (KERNEL.472)
2127 void WINAPI
MapHInstLS( CONTEXT86
*context
)
2129 context
->Eax
= MapHModuleLS( (HMODULE
)context
->Eax
);
2132 /***************************************************************************
2133 * MapHInstSL (KERNEL32.@)
2134 * MapHInstSL (KERNEL.473)
2136 void WINAPI
MapHInstSL( CONTEXT86
*context
)
2138 context
->Eax
= (DWORD
)MapHModuleSL( context
->Eax
);
2141 /***************************************************************************
2142 * MapHInstLS_PN (KERNEL32.@)
2144 void WINAPI
MapHInstLS_PN( CONTEXT86
*context
)
2146 if (context
->Eax
) context
->Eax
= MapHModuleLS( (HMODULE
)context
->Eax
);
2149 /***************************************************************************
2150 * MapHInstSL_PN (KERNEL32.@)
2152 void WINAPI
MapHInstSL_PN( CONTEXT86
*context
)
2154 if (context
->Eax
) context
->Eax
= (DWORD
)MapHModuleSL( context
->Eax
);