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"
36 #include "wine/winbase16.h"
41 #include "kernel_private.h"
42 #include "kernel16_private.h"
43 #include "wine/exception.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(module
);
47 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
50 typedef struct _GPHANDLERDEF
62 struct ne_segment_table_entry_s
64 WORD seg_data_offset
; /* Sector offset of segment data */
65 WORD seg_data_length
; /* Length of segment data */
66 WORD seg_flags
; /* Flags associated with this segment */
67 WORD min_alloc
; /* Minimum allocation size for this */
70 #define hFirstModule (pThhook->hExeHead)
74 const IMAGE_DOS_HEADER
*header
; /* module headers */
75 const char *file_name
; /* module file name */
78 /* Table of all built-in DLLs */
82 static struct builtin_dll builtin_dlls
[MAX_DLLS
];
84 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
);
85 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
);
87 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
);
89 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
);
92 static WINE_EXCEPTION_FILTER(page_fault
)
94 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
95 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
96 return EXCEPTION_EXECUTE_HANDLER
;
97 return EXCEPTION_CONTINUE_SEARCH
;
101 /* patch all the flat cs references of the code segment if necessary */
102 inline static void patch_code_segment( NE_MODULE
*pModule
)
106 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
);
108 for (i
= 0; i
< pModule
->ne_cseg
; i
++, pSeg
++)
110 if (!(pSeg
->flags
& NE_SEGFLAGS_DATA
)) /* found the code segment */
112 CALLFROM16
*call
= GlobalLock16( pSeg
->hSeg
);
113 if (call
->flatcs
== wine_get_cs()) return; /* nothing to patch */
114 while (call
->pushl
== 0x68)
116 call
->flatcs
= wine_get_cs();
125 /***********************************************************************
128 * locale-independent case conversion for module lookups
130 static int NE_strcasecmp( const char *str1
, const char *str2
)
133 for ( ; ; str1
++, str2
++)
134 if ((ret
= RtlUpperChar(*str1
) - RtlUpperChar(*str2
)) || !*str1
) break;
139 /***********************************************************************
142 * locale-independent case conversion for module lookups
144 static int NE_strncasecmp( const char *str1
, const char *str2
, int len
)
147 for ( ; len
> 0; len
--, str1
++, str2
++)
148 if ((ret
= RtlUpperChar(*str1
) - RtlUpperChar(*str2
)) || !*str1
) break;
153 /***********************************************************************
156 * Find a descriptor in the list
158 static const IMAGE_DOS_HEADER
*find_dll_descr( const char *dllname
, const char **file_name
)
161 const IMAGE_DOS_HEADER
*mz_header
;
162 const IMAGE_OS2_HEADER
*ne_header
;
165 for (i
= 0; i
< MAX_DLLS
; i
++)
167 mz_header
= builtin_dlls
[i
].header
;
170 ne_header
= (const IMAGE_OS2_HEADER
*)((const char *)mz_header
+ mz_header
->e_lfanew
);
171 name_table
= (BYTE
*)ne_header
+ ne_header
->ne_restab
;
173 /* check the dll file name */
174 if (!NE_strcasecmp( builtin_dlls
[i
].file_name
, dllname
) ||
175 /* check the dll module name (without extension) */
176 (!NE_strncasecmp( dllname
, name_table
+1, *name_table
) &&
177 !strcmp( dllname
+ *name_table
, ".dll" )))
179 *file_name
= builtin_dlls
[i
].file_name
;
180 return builtin_dlls
[i
].header
;
188 /***********************************************************************
189 * __wine_dll_register_16 (KERNEL32.@)
191 * Register a built-in DLL descriptor.
193 void __wine_dll_register_16( const IMAGE_DOS_HEADER
*header
, const char *file_name
)
197 for (i
= 0; i
< MAX_DLLS
; i
++)
199 if (builtin_dlls
[i
].header
) continue;
200 builtin_dlls
[i
].header
= header
;
201 builtin_dlls
[i
].file_name
= file_name
;
204 assert( i
< MAX_DLLS
);
208 /***********************************************************************
209 * __wine_dll_unregister_16 (KERNEL32.@)
211 * Unregister a built-in DLL descriptor.
213 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER
*header
)
217 for (i
= 0; i
< MAX_DLLS
; i
++)
219 if (builtin_dlls
[i
].header
!= header
) continue;
220 builtin_dlls
[i
].header
= NULL
;
226 /***********************************************************************
229 NE_MODULE
*NE_GetPtr( HMODULE16 hModule
)
231 return (NE_MODULE
*)GlobalLock16( GetExePtr(hModule
) );
235 /**********************************************************************
238 static void NE_RegisterModule( NE_MODULE
*pModule
)
240 pModule
->next
= hFirstModule
;
241 hFirstModule
= pModule
->self
;
245 /***********************************************************************
248 void NE_DumpModule( HMODULE16 hModule
)
258 if (!(pModule
= NE_GetPtr( hModule
)))
260 MESSAGE( "**** %04x is not a module handle\n", hModule
);
264 /* Dump the module info */
266 DPRINTF( "Module %04x:\n", hModule
);
267 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
268 pModule
->count
, pModule
->ne_flags
,
269 pModule
->ne_heap
, pModule
->ne_stack
);
270 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
271 SELECTOROF(pModule
->ne_csip
), OFFSETOF(pModule
->ne_csip
),
272 SELECTOROF(pModule
->ne_sssp
), OFFSETOF(pModule
->ne_sssp
),
273 pModule
->ne_autodata
, pModule
->ne_cseg
, pModule
->ne_cmod
);
274 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
275 pModule
->ne_exetyp
, pModule
->ne_swaparea
, pModule
->ne_expver
);
276 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
)
277 DPRINTF( "PE module=%p\n", pModule
->module32
);
279 /* Dump the file info */
281 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
283 /* Dump the segment table */
285 DPRINTF( "Segment table:\n" );
286 pSeg
= NE_SEG_TABLE( pModule
);
287 for (i
= 0; i
< pModule
->ne_cseg
; i
++, pSeg
++)
288 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
289 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
290 pSeg
->minsize
, pSeg
->hSeg
);
292 /* Dump the resource table */
294 DPRINTF( "Resource table:\n" );
295 if (pModule
->ne_rsrctab
)
297 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->ne_rsrctab
);
298 DPRINTF( "Alignment: %d\n", *pword
++ );
301 NE_TYPEINFO
*ptr
= (NE_TYPEINFO
*)pword
;
302 NE_NAMEINFO
*pname
= (NE_NAMEINFO
*)(ptr
+ 1);
303 DPRINTF( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
304 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
305 DPRINTF( "offset=%d len=%d id=%04x\n",
306 pname
->offset
, pname
->length
, pname
->id
);
307 pword
= (WORD
*)pname
;
310 else DPRINTF( "None\n" );
312 /* Dump the resident name table */
314 DPRINTF( "Resident-name table:\n" );
315 pstr
= (char *)pModule
+ pModule
->ne_restab
;
318 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
319 *(WORD
*)(pstr
+ *pstr
+ 1) );
320 pstr
+= *pstr
+ 1 + sizeof(WORD
);
323 /* Dump the module reference table */
325 DPRINTF( "Module ref table:\n" );
326 if (pModule
->ne_modtab
)
328 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->ne_modtab
);
329 for (i
= 0; i
< pModule
->ne_cmod
; i
++, pword
++)
332 GetModuleName16( *pword
, name
, sizeof(name
) );
333 DPRINTF( "%d: %04x -> '%s'\n", i
, *pword
, name
);
336 else DPRINTF( "None\n" );
338 /* Dump the entry table */
340 DPRINTF( "Entry table:\n" );
341 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+pModule
->ne_enttab
);
343 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
344 DPRINTF( "Bundle %d-%d: %02x\n", bundle
->first
, bundle
->last
, entry
->type
);
345 ordinal
= bundle
->first
;
346 while (ordinal
< bundle
->last
)
348 if (entry
->type
== 0xff)
349 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal
++, entry
->segnum
, entry
->offs
);
351 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal
++, entry
->segnum
, entry
->offs
);
354 } while ( (bundle
->next
) && (bundle
= ((ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
))) );
356 /* Dump the non-resident names table */
358 DPRINTF( "Non-resident names table:\n" );
359 if (pModule
->nrname_handle
)
361 pstr
= (char *)GlobalLock16( pModule
->nrname_handle
);
364 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
365 *(WORD
*)(pstr
+ *pstr
+ 1) );
366 pstr
+= *pstr
+ 1 + sizeof(WORD
);
373 /***********************************************************************
376 * Walk the module list and print the modules.
378 void NE_WalkModules(void)
380 HMODULE16 hModule
= hFirstModule
;
381 MESSAGE( "Module Flags Name\n" );
384 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
387 MESSAGE( "Bad module %04x in list\n", hModule
);
390 MESSAGE( " %04x %04x %.*s\n", hModule
, pModule
->ne_flags
,
391 *((char *)pModule
+ pModule
->ne_restab
),
392 (char *)pModule
+ pModule
->ne_restab
+ 1 );
393 hModule
= pModule
->next
;
398 /***********************************************************************
399 * NE_InitResourceHandler
401 * Fill in 'resloader' fields in the resource table.
403 static void NE_InitResourceHandler( HMODULE16 hModule
)
405 static FARPROC16 proc
;
407 NE_TYPEINFO
*pTypeInfo
;
410 if (!(pModule
= NE_GetPtr( hModule
)) || !pModule
->ne_rsrctab
) return;
412 TRACE("InitResourceHandler[%04x]\n", hModule
);
414 if (!proc
) proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
416 pTypeInfo
= (NE_TYPEINFO
*)((char *)pModule
+ pModule
->ne_rsrctab
+ 2);
417 while(pTypeInfo
->type_id
)
419 memcpy_unaligned( &pTypeInfo
->resloader
, &proc
, sizeof(FARPROC16
) );
420 pTypeInfo
= (NE_TYPEINFO
*)((char*)(pTypeInfo
+ 1) + pTypeInfo
->count
* sizeof(NE_NAMEINFO
));
425 /***********************************************************************
428 * Lookup the ordinal for a given name.
430 WORD
NE_GetOrdinal( HMODULE16 hModule
, const char *name
)
432 unsigned char buffer
[256], *cpnt
;
436 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
437 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) return 0;
439 TRACE("(%04x,'%s')\n", hModule
, name
);
441 /* First handle names of the form '#xxxx' */
443 if (name
[0] == '#') return atoi( name
+ 1 );
445 /* Now copy and uppercase the string */
447 strcpy( buffer
, name
);
448 for (cpnt
= buffer
; *cpnt
; cpnt
++) *cpnt
= RtlUpperChar(*cpnt
);
451 /* First search the resident names */
453 cpnt
= (char *)pModule
+ pModule
->ne_restab
;
455 /* Skip the first entry (module name) */
456 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
459 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
462 memcpy( &ordinal
, cpnt
+ *cpnt
+ 1, sizeof(ordinal
) );
463 TRACE(" Found: ordinal=%d\n", ordinal
);
466 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
469 /* Now search the non-resident names table */
471 if (!pModule
->nrname_handle
) return 0; /* No non-resident table */
472 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
474 /* Skip the first entry (module description string) */
475 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
478 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
481 memcpy( &ordinal
, cpnt
+ *cpnt
+ 1, sizeof(ordinal
) );
482 TRACE(" Found: ordinal=%d\n", ordinal
);
485 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
491 /***********************************************************************
494 FARPROC16 WINAPI
NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
496 return NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
499 /***********************************************************************
502 FARPROC16
NE_GetEntryPointEx( HMODULE16 hModule
, WORD ordinal
, BOOL16 snoop
)
510 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
511 assert( !(pModule
->ne_flags
& NE_FFLAGS_WIN32
) );
513 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->ne_enttab
);
514 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
518 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
521 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
522 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
526 memcpy( &offset
, &entry
->offs
, sizeof(WORD
) );
528 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
529 else sel
= GlobalHandleToSel16(NE_SEG_TABLE(pModule
)[sel
-1].hSeg
);
531 return (FARPROC16
)MAKESEGPTR( sel
, offset
);
533 return (FARPROC16
)MAKESEGPTR( sel
, offset
);
535 return (FARPROC16
)SNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)MAKESEGPTR( sel
, offset
));
539 /***********************************************************************
540 * EntryAddrProc (KERNEL.667) Wine-specific export
542 * Return the entry point for a given ordinal.
544 FARPROC16 WINAPI
EntryAddrProc16( HMODULE16 hModule
, WORD ordinal
)
546 FARPROC16 ret
= NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
547 CURRENT_STACK16
->ecx
= hModule
; /* FIXME: might be incorrect value */
551 /***********************************************************************
554 * Change the value of an entry point. Use with caution!
555 * It can only change the offset value, not the selector.
557 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
564 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
565 assert( !(pModule
->ne_flags
& NE_FFLAGS_WIN32
) );
567 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->ne_enttab
);
568 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
570 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
571 if (!(bundle
->next
)) return 0;
574 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
575 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
578 memcpy( &entry
->offs
, &offset
, sizeof(WORD
) );
583 /***********************************************************************
586 * Build the entry table bundle data from the on-disk format. Helper for build_module.
588 static void *build_bundle_data( NE_MODULE
*pModule
, void *dest
, const BYTE
*table
)
590 ET_BUNDLE
*oldbundle
, *bundle
= dest
;
592 BYTE nr_entries
, type
;
594 memset(bundle
, 0, sizeof(ET_BUNDLE
)); /* in case no entry table exists */
595 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
597 while ((nr_entries
= *table
++))
599 if ((type
= *table
++))
601 bundle
->last
+= nr_entries
;
607 entry
->flags
= *table
++;
608 table
+= sizeof(WORD
);
609 entry
->segnum
= *table
++;
610 entry
->offs
= *(WORD
*)table
;
611 table
+= sizeof(WORD
);
620 entry
->flags
= *table
++;
621 entry
->segnum
= type
;
622 entry
->offs
= *(WORD
*)table
;
623 table
+= sizeof(WORD
);
630 if (bundle
->first
== bundle
->last
)
632 bundle
->first
+= nr_entries
;
633 bundle
->last
+= nr_entries
;
638 oldbundle
->next
= (char *)entry
- (char *)pModule
;
639 bundle
= (ET_BUNDLE
*)entry
;
640 bundle
->first
= bundle
->last
= oldbundle
->last
+ nr_entries
;
642 entry
= (ET_ENTRY
*)(((BYTE
*)entry
)+sizeof(ET_BUNDLE
));
650 /***********************************************************************
653 * Build the in-memory module from the on-disk data.
655 static HMODULE16
build_module( const void *mapping
, SIZE_T mapping_size
, LPCSTR path
)
657 const IMAGE_DOS_HEADER
*mz_header
= mapping
;
658 const IMAGE_OS2_HEADER
*ne_header
;
659 const struct ne_segment_table_entry_s
*pSeg
;
665 BYTE
*buffer
, *pData
, *end
;
668 if (mapping_size
< sizeof(*mz_header
)) return ERROR_BAD_FORMAT
;
669 if (mz_header
->e_magic
!= IMAGE_DOS_SIGNATURE
) return ERROR_BAD_FORMAT
;
670 ne_header
= (const IMAGE_OS2_HEADER
*)((const char *)mapping
+ mz_header
->e_lfanew
);
671 if (mz_header
->e_lfanew
+ sizeof(*ne_header
) > mapping_size
) return ERROR_BAD_FORMAT
;
672 if (ne_header
->ne_magic
== IMAGE_NT_SIGNATURE
) return 21; /* win32 exe */
673 if (ne_header
->ne_magic
== IMAGE_OS2_SIGNATURE_LX
)
675 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path
);
678 if (ne_header
->ne_magic
!= IMAGE_OS2_SIGNATURE
) return ERROR_BAD_FORMAT
;
680 /* We now have a valid NE header */
682 /* check to be able to fall back to loading OS/2 programs as DOS
683 * FIXME: should this check be reversed in order to be less strict?
684 * (only fail for OS/2 ne_exetyp 0x01 here?) */
685 if ((ne_header
->ne_exetyp
!= 0x02 /* Windows */)
686 && (ne_header
->ne_exetyp
!= 0x04) /* Windows 386 */)
687 return ERROR_BAD_FORMAT
;
689 size
= sizeof(NE_MODULE
) +
691 ne_header
->ne_cseg
* sizeof(SEGTABLEENTRY
) +
693 ne_header
->ne_restab
- ne_header
->ne_rsrctab
+
694 /* resident names table */
695 ne_header
->ne_modtab
- ne_header
->ne_restab
+
696 /* module ref table */
697 ne_header
->ne_cmod
* sizeof(WORD
) +
698 /* imported names table */
699 ne_header
->ne_enttab
- ne_header
->ne_imptab
+
700 /* entry table length */
701 ne_header
->ne_cbenttab
+
702 /* entry table extra conversion space */
704 2 * (ne_header
->ne_cbenttab
- ne_header
->ne_cmovent
*6) +
705 /* loaded file info */
706 sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
) + 1;
708 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
709 if (!hModule
) return ERROR_BAD_FORMAT
;
711 FarSetOwner16( hModule
, hModule
);
712 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
713 memcpy( pModule
, ne_header
, sizeof(*ne_header
) );
715 /* check programs for default minimal stack size */
716 if (!(pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
) && (pModule
->ne_stack
< 0x1400))
717 pModule
->ne_stack
= 0x1400;
719 pModule
->self
= hModule
;
720 pModule
->mapping
= mapping
;
721 pModule
->mapping_size
= mapping_size
;
723 pData
= (BYTE
*)(pModule
+ 1);
725 /* Clear internal Wine flags in case they are set in the EXE file */
727 pModule
->ne_flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
729 /* Get the segment table */
731 pModule
->ne_segtab
= pData
- (BYTE
*)pModule
;
732 if (!(pSeg
= NE_GET_DATA( pModule
, mz_header
->e_lfanew
+ ne_header
->ne_segtab
,
733 ne_header
->ne_cseg
* sizeof(struct ne_segment_table_entry_s
) )))
735 for (i
= ne_header
->ne_cseg
; i
> 0; i
--, pSeg
++)
737 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
738 pData
+= sizeof(SEGTABLEENTRY
);
741 /* Get the resource table */
743 if (ne_header
->ne_rsrctab
< ne_header
->ne_restab
)
745 pModule
->ne_rsrctab
= pData
- (BYTE
*)pModule
;
746 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_rsrctab
,
747 ne_header
->ne_restab
- ne_header
->ne_rsrctab
)) goto failed
;
748 pData
+= ne_header
->ne_restab
- ne_header
->ne_rsrctab
;
750 else pModule
->ne_rsrctab
= 0; /* No resource table */
752 /* Get the resident names table */
754 pModule
->ne_restab
= pData
- (BYTE
*)pModule
;
755 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_restab
,
756 ne_header
->ne_modtab
- ne_header
->ne_restab
)) goto failed
;
757 pData
+= ne_header
->ne_modtab
- ne_header
->ne_restab
;
759 /* Get the module references table */
761 if (ne_header
->ne_cmod
> 0)
763 pModule
->ne_modtab
= pData
- (BYTE
*)pModule
;
764 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_modtab
,
765 ne_header
->ne_cmod
* sizeof(WORD
) )) goto failed
;
766 pData
+= ne_header
->ne_cmod
* sizeof(WORD
);
768 else pModule
->ne_modtab
= 0; /* No module references */
770 /* Get the imported names table */
772 pModule
->ne_imptab
= pData
- (BYTE
*)pModule
;
773 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_imptab
,
774 ne_header
->ne_enttab
- ne_header
->ne_imptab
)) goto failed
;
775 pData
+= ne_header
->ne_enttab
- ne_header
->ne_imptab
;
777 /* Load entry table, convert it to the optimized version used by Windows */
779 pModule
->ne_enttab
= pData
- (BYTE
*)pModule
;
780 if (!(ptr
= NE_GET_DATA( pModule
, mz_header
->e_lfanew
+ ne_header
->ne_enttab
,
781 ne_header
->ne_cbenttab
))) goto failed
;
782 end
= build_bundle_data( pModule
, pData
, ptr
);
784 pData
+= ne_header
->ne_cbenttab
+ sizeof(ET_BUNDLE
) +
785 2 * (ne_header
->ne_cbenttab
- ne_header
->ne_cmovent
*6);
789 FIXME( "not enough space for entry table for %s\n", debugstr_a(path
) );
793 /* Store the filename information */
795 pModule
->fileinfo
= pData
- (BYTE
*)pModule
;
796 ofs
= (OFSTRUCT
*)pData
;
797 ofs
->cBytes
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
);
799 strcpy( ofs
->szPathName
, path
);
800 pData
+= ofs
->cBytes
+ 1;
801 assert( (BYTE
*)pModule
+ size
<= pData
);
803 /* Get the non-resident names table */
805 if (ne_header
->ne_cbnrestab
)
807 pModule
->nrname_handle
= GlobalAlloc16( 0, ne_header
->ne_cbnrestab
);
808 if (!pModule
->nrname_handle
) goto failed
;
809 FarSetOwner16( pModule
->nrname_handle
, hModule
);
810 buffer
= GlobalLock16( pModule
->nrname_handle
);
811 if (!NE_READ_DATA( pModule
, buffer
, ne_header
->ne_nrestab
, ne_header
->ne_cbnrestab
))
813 GlobalFree16( pModule
->nrname_handle
);
817 else pModule
->nrname_handle
= 0;
819 /* Allocate a segment for the implicitly-loaded DLLs */
821 if (pModule
->ne_cmod
)
823 pModule
->dlls_to_init
= GlobalAlloc16( GMEM_ZEROINIT
,
824 (pModule
->ne_cmod
+1)*sizeof(HMODULE16
) );
825 if (!pModule
->dlls_to_init
)
827 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
830 FarSetOwner16( pModule
->dlls_to_init
, hModule
);
832 else pModule
->dlls_to_init
= 0;
834 NE_RegisterModule( pModule
);
838 GlobalFree16( hModule
);
839 return ERROR_BAD_FORMAT
;
843 /***********************************************************************
846 * Load all DLLs implicitly linked to a module.
848 static BOOL
NE_LoadDLLs( NE_MODULE
*pModule
)
851 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->ne_modtab
);
852 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
854 for (i
= 0; i
< pModule
->ne_cmod
; i
++, pModRef
++)
856 char buffer
[260], *p
;
857 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->ne_imptab
+ *pModRef
;
858 memcpy( buffer
, pstr
+ 1, *pstr
);
859 *(buffer
+ *pstr
) = 0; /* terminate it */
861 TRACE("Loading '%s'\n", buffer
);
862 if (!(*pModRef
= GetModuleHandle16( buffer
)))
864 /* If the DLL is not loaded yet, load it and store */
865 /* its handle in the list of DLLs to initialize. */
868 /* Append .DLL to name if no extension present */
869 if (!(p
= strrchr( buffer
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
870 strcat( buffer
, ".DLL" );
872 if ((hDLL
= MODULE_LoadModule16( buffer
, TRUE
, TRUE
)) < 32)
874 /* FIXME: cleanup what was done */
876 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
877 buffer
, *((BYTE
*)pModule
+ pModule
->ne_restab
),
878 (char *)pModule
+ pModule
->ne_restab
+ 1, hDLL
);
881 *pModRef
= GetExePtr( hDLL
);
884 else /* Increment the reference count of the DLL */
886 NE_MODULE
*pOldDLL
= NE_GetPtr( *pModRef
);
887 if (pOldDLL
) pOldDLL
->count
++;
894 /**********************************************************************
897 * Load first instance of NE module from file.
899 * pModule must point to a module structure prepared by build_module.
900 * This routine must never be called twice on a module.
902 static HINSTANCE16
NE_DoLoadModule( NE_MODULE
*pModule
)
904 /* Allocate the segments for this module */
906 if (!NE_CreateAllSegments( pModule
))
907 return ERROR_NOT_ENOUGH_MEMORY
; /* 8 */
909 /* Load the referenced DLLs */
911 if (!NE_LoadDLLs( pModule
))
912 return ERROR_FILE_NOT_FOUND
; /* 2 */
914 /* Load the segments */
916 NE_LoadAllSegments( pModule
);
918 /* Make sure the usage count is 1 on the first loading of */
919 /* the module, even if it contains circular DLL references */
923 return NE_GetInstance( pModule
);
926 /**********************************************************************
929 * Load first instance of NE module. (Note: caller is responsible for
930 * ensuring the module isn't already loaded!)
932 * If the module turns out to be an executable module, only a
933 * handle to a module stub is returned; this needs to be initialized
934 * by calling NE_DoLoadModule later, in the context of the newly
937 * If lib_only is TRUE, however, the module is perforce treated
938 * like a DLL module, even if it is an executable module.
941 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
)
945 HINSTANCE16 hInstance
;
950 MEMORY_BASIC_INFORMATION info
;
953 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
|OF_SHARE_DENY_WRITE
)) == HFILE_ERROR16
)
954 return ERROR_FILE_NOT_FOUND
;
956 mapping
= CreateFileMappingW( DosFileHandleToWin32Handle(hFile
), NULL
, PAGE_WRITECOPY
, 0, 0, NULL
);
958 if (!mapping
) return ERROR_BAD_FORMAT
;
960 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 0 );
961 CloseHandle( mapping
);
962 if (!ptr
) return ERROR_BAD_FORMAT
;
964 VirtualQuery( ptr
, &info
, sizeof(info
) );
965 hModule
= build_module( ptr
, info
.RegionSize
, ofs
.szPathName
);
969 UnmapViewOfFile( ptr
);
973 SNOOP16_RegisterDLL( hModule
, ofs
.szPathName
);
974 NE_InitResourceHandler( hModule
);
976 pModule
= NE_GetPtr( hModule
);
978 if ( !lib_only
&& !( pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
) )
981 hInstance
= NE_DoLoadModule( pModule
);
982 if ( hInstance
< 32 )
985 NE_FreeModule( hModule
, 0 );
992 /***********************************************************************
993 * NE_DoLoadBuiltinModule
995 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
997 static HMODULE16
NE_DoLoadBuiltinModule( const IMAGE_DOS_HEADER
*mz_header
, const char *file_name
,
1002 HINSTANCE16 hInstance
;
1003 OSVERSIONINFOW versionInfo
;
1004 const IMAGE_OS2_HEADER
*ne_header
;
1005 SIZE_T mapping_size
= ~0UL; /* assume builtins don't contain invalid offsets... */
1007 ne_header
= (const IMAGE_OS2_HEADER
*)((const BYTE
*)mz_header
+ mz_header
->e_lfanew
);
1008 hModule
= build_module( mz_header
, mapping_size
, file_name
);
1009 if (hModule
< 32) return hModule
;
1010 pModule
= GlobalLock16( hModule
);
1011 pModule
->ne_flags
|= NE_FFLAGS_BUILTIN
;
1012 pModule
->owner32
= owner32
;
1014 /* fake the expected version the module should have according to the current Windows version */
1015 versionInfo
.dwOSVersionInfoSize
= sizeof(versionInfo
);
1016 if (GetVersionExW( &versionInfo
))
1017 pModule
->ne_expver
= MAKEWORD( versionInfo
.dwMinorVersion
, versionInfo
.dwMajorVersion
);
1019 hInstance
= NE_DoLoadModule( pModule
);
1020 if (hInstance
< 32) NE_FreeModule( hModule
, 0 );
1022 NE_InitResourceHandler( hModule
);
1024 if (pModule
->ne_heap
)
1026 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
) + pModule
->ne_autodata
- 1;
1027 unsigned int size
= pSeg
->minsize
+ pModule
->ne_heap
;
1028 if (size
> 0xfffe) size
= 0xfffe;
1029 LocalInit16( GlobalHandleToSel16(pSeg
->hSeg
), pSeg
->minsize
, size
);
1032 patch_code_segment( pModule
);
1038 /**********************************************************************
1039 * MODULE_LoadModule16
1041 * Load a NE module in the order of the loadorder specification.
1042 * The caller is responsible that the module is not loaded already.
1045 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
)
1047 HINSTANCE16 hinst
= 2;
1051 const IMAGE_DOS_HEADER
*descr
= NULL
;
1052 const char *file_name
= NULL
;
1053 char dllname
[20], owner
[20], *p
;
1054 const char *basename
;
1057 /* strip path information */
1060 if (basename
[0] && basename
[1] == ':') basename
+= 2; /* strip drive specification */
1061 if ((p
= strrchr( basename
, '\\' ))) basename
= p
+ 1;
1062 if ((p
= strrchr( basename
, '/' ))) basename
= p
+ 1;
1064 if (strlen(basename
) < sizeof(dllname
)-4)
1066 strcpy( dllname
, basename
);
1067 p
= strrchr( dllname
, '.' );
1068 if (!p
) strcat( dllname
, ".dll" );
1069 for (p
= dllname
; *p
; p
++) if (*p
>= 'A' && *p
<= 'Z') *p
+= 32;
1071 if (wine_dll_get_owner( dllname
, owner
, sizeof(owner
), &owner_exists
) != -1)
1073 mod32
= LoadLibraryA( owner
);
1076 if (!(descr
= find_dll_descr( dllname
, &file_name
)))
1078 FreeLibrary( mod32
);
1081 /* loading the 32-bit library can have the side effect of loading the module */
1082 /* if so, simply incr the ref count and return the module */
1083 if ((hModule
= GetModuleHandle16( libname
)))
1085 TRACE( "module %s already loaded by owner\n", libname
);
1086 pModule
= NE_GetPtr( hModule
);
1087 if (pModule
) pModule
->count
++;
1088 FreeLibrary( mod32
);
1094 /* it's probably disabled by the load order config */
1095 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner
, dllname
);
1096 return ERROR_FILE_NOT_FOUND
;
1103 TRACE("Trying built-in '%s'\n", libname
);
1104 hinst
= NE_DoLoadBuiltinModule( descr
, file_name
, mod32
);
1105 if (hinst
> 32) TRACE_(loaddll
)("Loaded module %s : builtin\n", debugstr_a(file_name
));
1109 TRACE("Trying native dll '%s'\n", libname
);
1110 hinst
= NE_LoadModule(libname
, lib_only
);
1111 if (hinst
> 32) TRACE_(loaddll
)("Loaded module %s : native\n", debugstr_a(libname
));
1112 if (hinst
== ERROR_FILE_NOT_FOUND
&& owner_exists
) hinst
= 21; /* win32 module */
1115 if (hinst
> 32 && !implicit
)
1117 hModule
= GetModuleHandle16(libname
);
1120 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1122 return ERROR_INVALID_HANDLE
;
1125 pModule
= NE_GetPtr(hModule
);
1128 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1130 return ERROR_INVALID_HANDLE
;
1133 TRACE("Loaded module '%s' at 0x%04x.\n", libname
, hinst
);
1136 * Call initialization routines for all loaded DLLs. Note that
1137 * when we load implicitly linked DLLs this will be done by InitTask().
1139 if(pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
)
1141 NE_InitializeDLLs(hModule
);
1142 NE_DllProcessAttach(hModule
);
1145 return hinst
; /* The last error that occurred */
1149 /**********************************************************************
1152 * Create the thread for a 16-bit module.
1154 static HINSTANCE16
NE_CreateThread( NE_MODULE
*pModule
, WORD cmdShow
, LPCSTR cmdline
)
1159 HINSTANCE16 instance
= 0;
1161 if (!(hTask
= TASK_SpawnTask( pModule
, cmdShow
, cmdline
+ 1, *cmdline
, &hThread
)))
1164 /* Post event to start the task */
1165 PostEvent16( hTask
);
1167 /* Wait until we get the instance handle */
1170 DirectedYield16( hTask
);
1171 if (!IsTask16( hTask
)) /* thread has died */
1174 WaitForSingleObject( hThread
, INFINITE
);
1175 GetExitCodeThread( hThread
, &exit_code
);
1176 CloseHandle( hThread
);
1179 if (!(pTask
= GlobalLock16( hTask
))) break;
1180 instance
= pTask
->hInstance
;
1181 GlobalUnlock16( hTask
);
1182 } while (!instance
);
1184 CloseHandle( hThread
);
1189 /**********************************************************************
1190 * LoadModule (KERNEL.45)
1192 HINSTANCE16 WINAPI
LoadModule16( LPCSTR name
, LPVOID paramBlock
)
1194 BOOL lib_only
= !paramBlock
|| (paramBlock
== (LPVOID
)-1);
1195 LOADPARAMS16
*params
;
1203 if ( (hModule
= NE_GetModuleByFilename(name
) ) != 0 )
1205 /* Special case: second instance of an already loaded NE module */
1207 if ( !( pModule
= NE_GetPtr( hModule
) ) ) return ERROR_BAD_FORMAT
;
1208 if ( pModule
->module32
) return (HINSTANCE16
)21;
1210 /* Increment refcount */
1216 /* Main case: load first instance of NE module */
1218 if ( (hModule
= MODULE_LoadModule16( name
, FALSE
, lib_only
)) < 32 )
1221 if ( !(pModule
= NE_GetPtr( hModule
)) )
1222 return ERROR_BAD_FORMAT
;
1225 /* If library module, we just retrieve the instance handle */
1227 if ( ( pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
) || lib_only
)
1228 return NE_GetInstance( pModule
);
1231 * At this point, we need to create a new process.
1233 * pModule points either to an already loaded module, whose refcount
1234 * has already been incremented (to avoid having the module vanish
1235 * in the meantime), or else to a stub module which contains only header
1238 params
= (LOADPARAMS16
*)paramBlock
;
1239 cmdShow
= ((WORD
*)MapSL(params
->showCmd
))[1];
1240 cmdline
= MapSL( params
->cmdLine
);
1241 return NE_CreateThread( pModule
, cmdShow
, cmdline
);
1245 /**********************************************************************
1248 * Startup code for a new 16-bit task.
1250 DWORD
NE_StartTask(void)
1252 TDB
*pTask
= TASK_GetCurrent();
1253 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
1254 HINSTANCE16 hInstance
, hPrevInstance
;
1255 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
1258 if ( pModule
->count
> 0 )
1260 /* Second instance of an already loaded NE module */
1261 /* Note that the refcount was already incremented by the parent */
1263 hPrevInstance
= NE_GetInstance( pModule
);
1265 if ( pModule
->ne_autodata
)
1266 if ( NE_CreateSegment( pModule
, pModule
->ne_autodata
) )
1267 NE_LoadSegment( pModule
, pModule
->ne_autodata
);
1269 hInstance
= NE_GetInstance( pModule
);
1270 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance
, pModule
->ne_autodata
, hPrevInstance
);
1275 /* Load first instance of NE module */
1277 pModule
->ne_flags
|= NE_FFLAGS_GUI
; /* FIXME: is this necessary? */
1279 hInstance
= NE_DoLoadModule( pModule
);
1283 if ( hInstance
>= 32 )
1287 /* Enter instance handles into task struct */
1289 pTask
->hInstance
= hInstance
;
1290 pTask
->hPrevInstance
= hPrevInstance
;
1292 /* Use DGROUP for 16-bit stack */
1294 if (!(sp
= OFFSETOF(pModule
->ne_sssp
)))
1295 sp
= pSegTable
[SELECTOROF(pModule
->ne_sssp
)-1].minsize
+ pModule
->ne_stack
;
1297 sp
-= sizeof(STACK16FRAME
);
1298 NtCurrentTeb()->WOW32Reserved
= (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance
), sp
);
1300 /* Registers at initialization must be:
1302 * bx stack size in bytes
1303 * cx heap size in bytes
1304 * si previous app instance
1305 * di current app instance
1307 * es selector to the PSP
1308 * ds dgroup of the application
1310 * sp top of the stack
1312 memset( &context
, 0, sizeof(context
) );
1313 context
.SegCs
= GlobalHandleToSel16(pSegTable
[SELECTOROF(pModule
->ne_csip
) - 1].hSeg
);
1314 context
.SegDs
= GlobalHandleToSel16(pTask
->hInstance
);
1315 context
.SegEs
= pTask
->hPDB
;
1316 context
.SegFs
= wine_get_fs();
1317 context
.SegGs
= wine_get_gs();
1318 context
.Eip
= OFFSETOF(pModule
->ne_csip
);
1319 context
.Ebx
= pModule
->ne_stack
;
1320 context
.Ecx
= pModule
->ne_heap
;
1321 context
.Edi
= pTask
->hInstance
;
1322 context
.Esi
= pTask
->hPrevInstance
;
1324 /* Now call 16-bit entry point */
1326 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1327 context
.SegCs
, context
.Eip
, context
.SegDs
,
1328 SELECTOROF(NtCurrentTeb()->WOW32Reserved
),
1329 OFFSETOF(NtCurrentTeb()->WOW32Reserved
) );
1331 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
1332 ExitThread( LOWORD(context
.Eax
) );
1334 return hInstance
; /* error code */
1337 /***********************************************************************
1338 * LoadLibrary (KERNEL.95)
1339 * LoadLibrary16 (KERNEL32.35)
1341 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
1343 return LoadModule16(libname
, (LPVOID
)-1 );
1347 /**********************************************************************
1350 * Call a DLL's WEP, allowing it to shut down.
1351 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1353 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
1356 FARPROC16 WEP
= GetProcAddress16( hModule
, "WEP" );
1357 if (!WEP
) return FALSE
;
1364 args
[0] = WEP_FREE_DLL
;
1365 WOWCallback16Ex( (DWORD
)WEP
, WCB16_PASCAL
, sizeof(args
), args
, &dwRet
);
1366 ret
= LOWORD(dwRet
);
1368 __EXCEPT(page_fault
)
1370 WARN("Page fault\n");
1379 /**********************************************************************
1382 * Implementation of FreeModule16().
1384 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
)
1386 HMODULE16
*hPrevModule
;
1391 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
1392 hModule
= pModule
->self
;
1394 TRACE("%04x count %d\n", hModule
, pModule
->count
);
1396 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
1397 else pModule
->count
= 0;
1399 if (call_wep
&& !(pModule
->ne_flags
& NE_FFLAGS_WIN32
))
1401 /* Free the objects owned by the DLL module */
1402 NE_CallUserSignalProc( hModule
, USIG16_DLL_UNLOAD
);
1404 if (pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
)
1405 MODULE_CallWEP( hModule
);
1407 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
1410 TRACE_(loaddll
)("Unloaded module %s : %s\n", debugstr_a(NE_MODULE_NAME(pModule
)),
1411 (pModule
->ne_flags
& NE_FFLAGS_BUILTIN
) ? "builtin" : "native");
1413 /* Clear magic number just in case */
1415 pModule
->ne_magic
= pModule
->self
= 0;
1416 if (pModule
->owner32
) FreeLibrary( pModule
->owner32
);
1417 else if (pModule
->mapping
) UnmapViewOfFile( (void *)pModule
->mapping
);
1419 /* Remove it from the linked list */
1421 hPrevModule
= &hFirstModule
;
1422 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
1424 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
1426 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
1428 /* Free the referenced modules */
1430 pModRef
= (HMODULE16
*)((char *)pModule
+ pModule
->ne_modtab
);
1431 for (i
= 0; i
< pModule
->ne_cmod
; i
++, pModRef
++)
1433 NE_FreeModule( *pModRef
, call_wep
);
1436 /* Free the module storage */
1438 GlobalFreeAll16( hModule
);
1443 /**********************************************************************
1444 * FreeModule (KERNEL.46)
1446 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
1448 return NE_FreeModule( hModule
, TRUE
);
1452 /***********************************************************************
1453 * FreeLibrary (KERNEL.96)
1454 * FreeLibrary16 (KERNEL32.36)
1456 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
1458 TRACE("%04x\n", handle
);
1459 FreeModule16( handle
);
1463 /***********************************************************************
1464 * GetModuleHandle16 (KERNEL32.@)
1466 HMODULE16 WINAPI
GetModuleHandle16( LPCSTR name
)
1468 HMODULE16 hModule
= hFirstModule
;
1470 BYTE len
, *name_table
;
1471 char tmpstr
[MAX_PATH
];
1474 TRACE("(%s)\n", name
);
1476 if (!HIWORD(name
)) return GetExePtr(LOWORD(name
));
1481 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1483 /* If 'name' matches exactly the module name of a module:
1484 * Return its handle.
1486 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1488 pModule
= NE_GetPtr( hModule
);
1489 if (!pModule
) break;
1490 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1492 name_table
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1493 if ((*name_table
== len
) && !strncmp(name
, name_table
+1, len
))
1497 /* If uppercased 'name' matches exactly the module name of a module:
1500 for (s
= tmpstr
; *s
; s
++) *s
= RtlUpperChar(*s
);
1502 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1504 pModule
= NE_GetPtr( hModule
);
1505 if (!pModule
) break;
1506 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1508 name_table
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1509 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1510 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1511 * lowercased module names, but try to load uppercase DLLs, so this
1512 * 'i' compare is just a quickfix until the loader handles that
1513 * correctly. -MM 990705
1515 if ((*name_table
== len
) && !NE_strncasecmp(tmpstr
, name_table
+1, len
))
1519 /* If the base filename of 'name' matches the base filename of the module
1520 * filename of some module (case-insensitive compare):
1521 * Return its handle.
1524 /* basename: search backwards in passed name to \ / or : */
1525 s
= tmpstr
+ strlen(tmpstr
);
1528 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1533 /* search this in loaded filename list */
1534 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1539 pModule
= NE_GetPtr( hModule
);
1540 if (!pModule
) break;
1541 if (!pModule
->fileinfo
) continue;
1542 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1544 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1545 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1546 /* basename: search backwards in pathname to \ / or : */
1547 while (loadedfn
> (char*)ofs
->szPathName
)
1549 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1553 /* case insensitive compare ... */
1554 if (!NE_strcasecmp(loadedfn
, s
))
1561 /**********************************************************************
1562 * GetModuleName (KERNEL.27)
1564 BOOL16 WINAPI
GetModuleName16( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
1569 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
1570 p
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1571 if (count
> *p
) count
= *p
+ 1;
1574 memcpy( buf
, p
+ 1, count
- 1 );
1575 buf
[count
-1] = '\0';
1581 /**********************************************************************
1582 * GetModuleFileName (KERNEL.49)
1584 * Comment: see GetModuleFileNameA
1586 * Even if invoked by second instance of a program,
1587 * it still returns path of first one.
1589 INT16 WINAPI
GetModuleFileName16( HINSTANCE16 hModule
, LPSTR lpFileName
,
1594 /* Win95 does not query hModule if set to 0 !
1595 * Is this wrong or maybe Win3.1 only ? */
1596 if (!hModule
) hModule
= GetCurrentTask();
1598 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
1599 lstrcpynA( lpFileName
, NE_MODULE_NAME(pModule
), nSize
);
1600 if (pModule
->ne_expver
>= 0x400)
1601 GetLongPathNameA(NE_MODULE_NAME(pModule
), lpFileName
, nSize
);
1602 TRACE("%04x -> '%s'\n", hModule
, lpFileName
);
1603 return strlen(lpFileName
);
1607 /**********************************************************************
1608 * GetModuleUsage (KERNEL.48)
1610 INT16 WINAPI
GetModuleUsage16( HINSTANCE16 hModule
)
1612 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1613 return pModule
? pModule
->count
: 0;
1617 /**********************************************************************
1618 * GetExpWinVer (KERNEL.167)
1620 WORD WINAPI
GetExpWinVer16( HMODULE16 hModule
)
1622 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1623 if ( !pModule
) return 0;
1624 return pModule
->ne_expver
;
1628 /***********************************************************************
1629 * WinExec (KERNEL.166)
1631 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
1633 LPCSTR p
, args
= NULL
;
1634 LPCSTR name_beg
, name_end
;
1635 LPSTR name
, cmdline
;
1638 char buffer
[MAX_PATH
];
1640 if (*lpCmdLine
== '"') /* has to be only one and only at beginning ! */
1642 name_beg
= lpCmdLine
+1;
1643 p
= strchr ( lpCmdLine
+1, '"' );
1647 args
= strchr ( p
, ' ' );
1649 else /* yes, even valid with trailing '"' missing */
1650 name_end
= lpCmdLine
+strlen(lpCmdLine
);
1654 name_beg
= lpCmdLine
;
1655 args
= strchr( lpCmdLine
, ' ' );
1656 name_end
= args
? args
: lpCmdLine
+strlen(lpCmdLine
);
1659 if ((name_beg
== lpCmdLine
) && (!args
))
1660 { /* just use the original cmdline string as file name */
1661 name
= (LPSTR
)lpCmdLine
;
1665 if (!(name
= HeapAlloc( GetProcessHeap(), 0, name_end
- name_beg
+ 1 )))
1666 return ERROR_NOT_ENOUGH_MEMORY
;
1667 memcpy( name
, name_beg
, name_end
- name_beg
);
1668 name
[name_end
- name_beg
] = '\0';
1674 arglen
= strlen(args
);
1675 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 + arglen
);
1676 cmdline
[0] = (BYTE
)arglen
;
1677 strcpy( cmdline
+ 1, args
);
1681 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 );
1682 cmdline
[0] = cmdline
[1] = 0;
1685 TRACE("name: '%s', cmdline: '%.*s'\n", name
, cmdline
[0], &cmdline
[1]);
1687 if (SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, NULL
))
1689 LOADPARAMS16 params
;
1692 showCmd
[1] = nCmdShow
;
1694 params
.hEnvironment
= 0;
1695 params
.cmdLine
= MapLS( cmdline
);
1696 params
.showCmd
= MapLS( showCmd
);
1697 params
.reserved
= 0;
1699 ret
= LoadModule16( buffer
, ¶ms
);
1700 UnMapLS( params
.cmdLine
);
1701 UnMapLS( params
.showCmd
);
1703 else ret
= GetLastError();
1705 HeapFree( GetProcessHeap(), 0, cmdline
);
1706 if (name
!= lpCmdLine
) HeapFree( GetProcessHeap(), 0, name
);
1708 if (ret
== 21 || ret
== ERROR_BAD_FORMAT
) /* 32-bit module or unknown executable*/
1711 ReleaseThunkLock( &count
);
1712 ret
= LOWORD( WinExec( lpCmdLine
, nCmdShow
) );
1713 RestoreThunkLock( count
);
1718 /***********************************************************************
1719 * GetProcAddress (KERNEL.50)
1721 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, LPCSTR name
)
1726 if (!hModule
) hModule
= GetCurrentTask();
1727 hModule
= GetExePtr( hModule
);
1729 if (HIWORD(name
) != 0)
1731 ordinal
= NE_GetOrdinal( hModule
, name
);
1732 TRACE("%04x '%s'\n", hModule
, name
);
1736 ordinal
= LOWORD(name
);
1737 TRACE("%04x %04x\n", hModule
, ordinal
);
1739 if (!ordinal
) return (FARPROC16
)0;
1741 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1743 TRACE("returning %08x\n", (UINT
)ret
);
1748 /***************************************************************************
1749 * HasGPHandler (KERNEL.338)
1751 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1756 GPHANDLERDEF
*gpHandler
;
1758 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1759 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1760 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1761 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1762 && (gpHandler
= MapSL( gpPtr
)) != NULL
)
1764 while (gpHandler
->selector
)
1766 if ( SELECTOROF(address
) == gpHandler
->selector
1767 && OFFSETOF(address
) >= gpHandler
->rangeStart
1768 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1769 return MAKESEGPTR( gpHandler
->selector
, gpHandler
->handler
);
1778 /**********************************************************************
1779 * GetModuleHandle (KERNEL.47)
1781 * Find a module from a module name.
1783 * NOTE: The current implementation works the same way the Windows 95 one
1784 * does. Do not try to 'fix' it, fix the callers.
1785 * + It does not do ANY extension handling (except that strange .EXE bit)!
1786 * + It does not care about paths, just about basenames. (same as Windows)
1790 * the win16 module handle if found
1792 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1793 * Always hFirstModule
1795 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1797 if (HIWORD(name
) == 0)
1798 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1799 return MAKELONG(GetModuleHandle16( MapSL(name
)), hFirstModule
);
1802 /**********************************************************************
1803 * NE_GetModuleByFilename
1805 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
)
1809 BYTE len
, *name_table
;
1810 char tmpstr
[MAX_PATH
];
1813 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1815 /* If the base filename of 'name' matches the base filename of the module
1816 * filename of some module (case-insensitive compare):
1817 * Return its handle.
1820 /* basename: search backwards in passed name to \ / or : */
1821 s
= tmpstr
+ strlen(tmpstr
);
1824 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1829 /* search this in loaded filename list */
1830 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1835 pModule
= NE_GetPtr( hModule
);
1836 if (!pModule
) break;
1837 if (!pModule
->fileinfo
) continue;
1838 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1840 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1841 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1842 /* basename: search backwards in pathname to \ / or : */
1843 while (loadedfn
> (char*)ofs
->szPathName
)
1845 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1849 /* case insensitive compare ... */
1850 if (!NE_strcasecmp(loadedfn
, s
))
1853 /* If basename (without ext) matches the module name of a module:
1854 * Return its handle.
1857 if ( (p
= strrchr( s
, '.' )) != NULL
) *p
= '\0';
1860 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1862 pModule
= NE_GetPtr( hModule
);
1863 if (!pModule
) break;
1864 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1866 name_table
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1867 if ((*name_table
== len
) && !NE_strncasecmp(s
, name_table
+1, len
))
1874 /***********************************************************************
1875 * GetProcAddress16 (KERNEL32.37)
1876 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1878 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1880 if (!hModule
) return 0;
1881 if (HIWORD(hModule
))
1883 WARN("hModule is Win32 handle (%p)\n", hModule
);
1886 return GetProcAddress16( LOWORD(hModule
), name
);
1889 /**********************************************************************
1890 * ModuleFirst (TOOLHELP.59)
1892 BOOL16 WINAPI
ModuleFirst16( MODULEENTRY
*lpme
)
1894 lpme
->wNext
= hFirstModule
;
1895 return ModuleNext16( lpme
);
1899 /**********************************************************************
1900 * ModuleNext (TOOLHELP.60)
1902 BOOL16 WINAPI
ModuleNext16( MODULEENTRY
*lpme
)
1907 if (!lpme
->wNext
) return FALSE
;
1908 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1909 name
= (char *)pModule
+ pModule
->ne_restab
;
1910 memcpy( lpme
->szModule
, name
+ 1, min(*name
, MAX_MODULE_NAME
) );
1911 lpme
->szModule
[min(*name
, MAX_MODULE_NAME
)] = '\0';
1912 lpme
->hModule
= lpme
->wNext
;
1913 lpme
->wcUsage
= pModule
->count
;
1914 lstrcpynA( lpme
->szExePath
, NE_MODULE_NAME(pModule
), sizeof(lpme
->szExePath
) );
1915 lpme
->wNext
= pModule
->next
;
1920 /**********************************************************************
1921 * ModuleFindName (TOOLHELP.61)
1923 BOOL16 WINAPI
ModuleFindName16( MODULEENTRY
*lpme
, LPCSTR name
)
1925 lpme
->wNext
= GetModuleHandle16( name
);
1926 return ModuleNext16( lpme
);
1930 /**********************************************************************
1931 * ModuleFindHandle (TOOLHELP.62)
1933 BOOL16 WINAPI
ModuleFindHandle16( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1935 hModule
= GetExePtr( hModule
);
1936 lpme
->wNext
= hModule
;
1937 return ModuleNext16( lpme
);
1941 /***************************************************************************
1942 * IsRomModule (KERNEL.323)
1944 BOOL16 WINAPI
IsRomModule16( HMODULE16 unused
)
1949 /***************************************************************************
1950 * IsRomFile (KERNEL.326)
1952 BOOL16 WINAPI
IsRomFile16( HFILE16 unused
)
1957 /***********************************************************************
1958 * create_dummy_module
1960 * Create a dummy NE module for Win32 or Winelib.
1962 static HMODULE16
create_dummy_module( HMODULE module32
)
1966 SEGTABLEENTRY
*pSegment
;
1969 const char* basename
;
1972 char filename
[MAX_PATH
];
1973 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module32
);
1975 if (!nt
) return ERROR_BAD_FORMAT
;
1977 /* Extract base filename */
1978 len
= GetModuleFileNameA( module32
, filename
, sizeof(filename
) );
1979 if (!len
|| len
>= sizeof(filename
)) return ERROR_BAD_FORMAT
;
1980 basename
= strrchr(filename
, '\\');
1981 if (!basename
) basename
= filename
;
1983 len
= strlen(basename
);
1984 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
1986 /* Allocate module */
1987 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
1988 + strlen(filename
) + 1;
1989 size
= sizeof(NE_MODULE
) +
1990 /* loaded file info */
1991 ((of_size
+ 3) & ~3) +
1992 /* segment table: DS,CS */
1993 2 * sizeof(SEGTABLEENTRY
) +
1996 /* several empty tables */
1999 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
2000 if (!hModule
) return ERROR_BAD_FORMAT
;
2002 FarSetOwner16( hModule
, hModule
);
2003 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
2005 /* Set all used entries */
2006 pModule
->ne_magic
= IMAGE_OS2_SIGNATURE
;
2009 pModule
->ne_flags
= NE_FFLAGS_WIN32
;
2010 pModule
->ne_autodata
= 0;
2011 pModule
->ne_sssp
= MAKESEGPTR( 0, 1 );
2012 pModule
->ne_csip
= MAKESEGPTR( 0, 2 );
2013 pModule
->ne_heap
= 0;
2014 pModule
->ne_stack
= 0;
2015 pModule
->ne_cseg
= 2;
2016 pModule
->ne_cmod
= 0;
2017 pModule
->ne_cbnrestab
= 0;
2018 pModule
->fileinfo
= sizeof(NE_MODULE
);
2019 pModule
->ne_exetyp
= NE_OSFLAGS_WINDOWS
;
2020 pModule
->self
= hModule
;
2021 pModule
->module32
= module32
;
2023 /* Set version and flags */
2024 pModule
->ne_expver
= ((nt
->OptionalHeader
.MajorSubsystemVersion
& 0xff) << 8 ) |
2025 (nt
->OptionalHeader
.MinorSubsystemVersion
& 0xff);
2026 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
2027 pModule
->ne_flags
|= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
;
2029 /* Set loaded file information */
2030 ofs
= (OFSTRUCT
*)(pModule
+ 1);
2031 memset( ofs
, 0, of_size
);
2032 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
2033 strcpy( ofs
->szPathName
, filename
);
2035 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + ((of_size
+ 3) & ~3));
2036 pModule
->ne_segtab
= (char *)pSegment
- (char *)pModule
;
2039 pSegment
->flags
= NE_SEGFLAGS_DATA
;
2040 pSegment
->minsize
= 0x1000;
2043 pSegment
->flags
= 0;
2047 pStr
= (char *)pSegment
;
2048 pModule
->ne_restab
= pStr
- (char *)pModule
;
2051 lstrcpynA( pStr
+1, basename
, len
+1 );
2054 /* All tables zero terminated */
2055 pModule
->ne_rsrctab
= pModule
->ne_imptab
= pModule
->ne_enttab
= (char *)pStr
- (char *)pModule
;
2057 NE_RegisterModule( pModule
);
2058 pModule
->owner32
= LoadLibraryA( filename
); /* increment the ref count of the 32-bit module */
2062 /***********************************************************************
2063 * PrivateLoadLibrary (KERNEL32.@)
2065 * FIXME: rough guesswork, don't know what "Private" means
2067 HINSTANCE16 WINAPI
PrivateLoadLibrary(LPCSTR libname
)
2069 return LoadLibrary16(libname
);
2072 /***********************************************************************
2073 * PrivateFreeLibrary (KERNEL32.@)
2075 * FIXME: rough guesswork, don't know what "Private" means
2077 void WINAPI
PrivateFreeLibrary(HINSTANCE16 handle
)
2079 FreeLibrary16(handle
);
2082 /***********************************************************************
2083 * LoadLibrary32 (KERNEL.452)
2084 * LoadSystemLibrary32 (KERNEL.482)
2086 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
2091 ReleaseThunkLock( &count
);
2092 hModule
= LoadLibraryA( libname
);
2093 RestoreThunkLock( count
);
2097 /***************************************************************************
2098 * MapHModuleLS (KERNEL32.@)
2100 HMODULE16 WINAPI
MapHModuleLS(HMODULE hmod
)
2106 return TASK_GetCurrent()->hInstance
;
2108 return LOWORD(hmod
); /* we already have a 16 bit module handle */
2109 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
2111 if (pModule
->module32
== hmod
)
2112 return pModule
->self
;
2113 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
2115 if ((ret
= create_dummy_module( hmod
)) < 32)
2123 /***************************************************************************
2124 * MapHModuleSL (KERNEL32.@)
2126 HMODULE WINAPI
MapHModuleSL(HMODULE16 hmod
)
2131 TDB
*pTask
= TASK_GetCurrent();
2132 hmod
= pTask
->hModule
;
2134 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
2135 if ((pModule
->ne_magic
!= IMAGE_OS2_SIGNATURE
) || !(pModule
->ne_flags
& NE_FFLAGS_WIN32
))
2137 return pModule
->module32
;
2140 /***************************************************************************
2141 * MapHInstLS (KERNEL32.@)
2142 * MapHInstLS (KERNEL.472)
2144 void WINAPI
__regs_MapHInstLS( CONTEXT86
*context
)
2146 context
->Eax
= MapHModuleLS( (HMODULE
)context
->Eax
);
2148 #ifdef DEFINE_REGS_ENTRYPOINT
2149 DEFINE_REGS_ENTRYPOINT( MapHInstLS
, 0, 0 );
2152 /***************************************************************************
2153 * MapHInstSL (KERNEL32.@)
2154 * MapHInstSL (KERNEL.473)
2156 void WINAPI
__regs_MapHInstSL( CONTEXT86
*context
)
2158 context
->Eax
= (DWORD
)MapHModuleSL( context
->Eax
);
2160 #ifdef DEFINE_REGS_ENTRYPOINT
2161 DEFINE_REGS_ENTRYPOINT( MapHInstSL
, 0, 0 );
2164 /***************************************************************************
2165 * MapHInstLS_PN (KERNEL32.@)
2167 void WINAPI
__regs_MapHInstLS_PN( CONTEXT86
*context
)
2169 if (context
->Eax
) context
->Eax
= MapHModuleLS( (HMODULE
)context
->Eax
);
2171 #ifdef DEFINE_REGS_ENTRYPOINT
2172 DEFINE_REGS_ENTRYPOINT( MapHInstLS_PN
, 0, 0 );
2175 /***************************************************************************
2176 * MapHInstSL_PN (KERNEL32.@)
2178 void WINAPI
__regs_MapHInstSL_PN( CONTEXT86
*context
)
2180 if (context
->Eax
) context
->Eax
= (DWORD
)MapHModuleSL( context
->Eax
);
2182 #ifdef DEFINE_REGS_ENTRYPOINT
2183 DEFINE_REGS_ENTRYPOINT( MapHInstSL_PN
, 0, 0 );