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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
36 #include "wine/winbase16.h"
40 #include "kernel_private.h"
41 #include "kernel16_private.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(module
);
46 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
47 WINE_DECLARE_DEBUG_CHANNEL(relay
);
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 /* patch all the flat cs references of the code segment if necessary */
93 static inline void patch_code_segment( NE_MODULE
*pModule
)
98 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
);
100 for (i
= 0; i
< pModule
->ne_cseg
; i
++, pSeg
++)
101 if (!(pSeg
->flags
& NE_SEGFLAGS_DATA
)) break; /* found the code segment */
103 call
= GlobalLock16( pSeg
->hSeg
);
105 /* patch glue code address and code selector */
106 for (i
= 0; call
[i
].pushl
== 0x68; i
++)
108 if (call
[i
].ret
[0] == 0xca66 || call
[i
].ret
[0] == 0xcb66) /* register entry point? */
109 call
[i
].glue
= __wine_call_from_16_regs
;
111 call
[i
].glue
= __wine_call_from_16
;
112 call
[i
].flatcs
= wine_get_cs();
115 if (TRACE_ON(relay
)) /* patch relay functions to all point to relay_call_from_16 */
116 for (i
= 0; call
[i
].pushl
== 0x68; i
++) call
[i
].relay
= relay_call_from_16
;
121 /***********************************************************************
124 * locale-independent case conversion for module lookups
126 static int NE_strcasecmp( const char *str1
, const char *str2
)
129 for ( ; ; str1
++, str2
++)
130 if ((ret
= RtlUpperChar(*str1
) - RtlUpperChar(*str2
)) || !*str1
) break;
135 /***********************************************************************
138 * locale-independent case conversion for module lookups
140 static int NE_strncasecmp( const char *str1
, const char *str2
, int len
)
143 for ( ; len
> 0; len
--, str1
++, str2
++)
144 if ((ret
= RtlUpperChar(*str1
) - RtlUpperChar(*str2
)) || !*str1
) break;
149 /***********************************************************************
152 * Find a descriptor in the list
154 static const IMAGE_DOS_HEADER
*find_dll_descr( const char *dllname
, const char **file_name
)
157 const IMAGE_DOS_HEADER
*mz_header
;
158 const IMAGE_OS2_HEADER
*ne_header
;
159 const BYTE
*name_table
;
161 for (i
= 0; i
< MAX_DLLS
; i
++)
163 mz_header
= builtin_dlls
[i
].header
;
166 ne_header
= (const IMAGE_OS2_HEADER
*)((const char *)mz_header
+ mz_header
->e_lfanew
);
167 name_table
= (const BYTE
*)ne_header
+ ne_header
->ne_restab
;
169 /* check the dll file name */
170 if (!NE_strcasecmp( builtin_dlls
[i
].file_name
, dllname
) ||
171 /* check the dll module name (without extension) */
172 (!NE_strncasecmp( dllname
, (const char*)name_table
+1, *name_table
) &&
173 !strcmp( dllname
+ *name_table
, ".dll" )))
175 *file_name
= builtin_dlls
[i
].file_name
;
176 return builtin_dlls
[i
].header
;
184 /***********************************************************************
185 * __wine_dll_register_16 (KERNEL32.@)
187 * Register a built-in DLL descriptor.
189 void __wine_dll_register_16( const IMAGE_DOS_HEADER
*header
, const char *file_name
)
193 for (i
= 0; i
< MAX_DLLS
; i
++)
195 if (builtin_dlls
[i
].header
) continue;
196 builtin_dlls
[i
].header
= header
;
197 builtin_dlls
[i
].file_name
= file_name
;
200 assert( i
< MAX_DLLS
);
204 /***********************************************************************
205 * __wine_dll_unregister_16 (KERNEL32.@)
207 * Unregister a built-in DLL descriptor.
209 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER
*header
)
213 for (i
= 0; i
< MAX_DLLS
; i
++)
215 if (builtin_dlls
[i
].header
!= header
) continue;
216 builtin_dlls
[i
].header
= NULL
;
222 /***********************************************************************
225 NE_MODULE
*NE_GetPtr( HMODULE16 hModule
)
227 return (NE_MODULE
*)GlobalLock16( GetExePtr(hModule
) );
231 /**********************************************************************
234 static void NE_RegisterModule( NE_MODULE
*pModule
)
236 pModule
->next
= hFirstModule
;
237 hFirstModule
= pModule
->self
;
241 /***********************************************************************
244 void NE_DumpModule( HMODULE16 hModule
)
254 if (!(pModule
= NE_GetPtr( hModule
)))
256 ERR( "**** %04x is not a module handle\n", hModule
);
260 /* Dump the module info */
262 TRACE( "Module %04x:\n", hModule
);
263 TRACE( "count=%d flags=%04x heap=%d stack=%d\n",
264 pModule
->count
, pModule
->ne_flags
,
265 pModule
->ne_heap
, pModule
->ne_stack
);
266 TRACE( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
267 SELECTOROF(pModule
->ne_csip
), OFFSETOF(pModule
->ne_csip
),
268 SELECTOROF(pModule
->ne_sssp
), OFFSETOF(pModule
->ne_sssp
),
269 pModule
->ne_autodata
, pModule
->ne_cseg
, pModule
->ne_cmod
);
270 TRACE( "os_flags=%d swap_area=%d version=%04x\n",
271 pModule
->ne_exetyp
, pModule
->ne_swaparea
, pModule
->ne_expver
);
272 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
)
273 TRACE( "PE module=%p\n", pModule
->module32
);
275 /* Dump the file info */
277 TRACE( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
279 /* Dump the segment table */
281 TRACE( "Segment table:\n" );
282 pSeg
= NE_SEG_TABLE( pModule
);
283 for (i
= 0; i
< pModule
->ne_cseg
; i
++, pSeg
++)
284 TRACE( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
285 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
286 pSeg
->minsize
, pSeg
->hSeg
);
288 /* Dump the resource table */
290 TRACE( "Resource table:\n" );
291 if (pModule
->ne_rsrctab
)
293 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->ne_rsrctab
);
294 TRACE( "Alignment: %d\n", *pword
++ );
297 NE_TYPEINFO
*ptr
= (NE_TYPEINFO
*)pword
;
298 NE_NAMEINFO
*pname
= (NE_NAMEINFO
*)(ptr
+ 1);
299 TRACE( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
300 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
301 TRACE( "offset=%d len=%d id=%04x\n",
302 pname
->offset
, pname
->length
, pname
->id
);
303 pword
= (WORD
*)pname
;
306 else TRACE( "None\n" );
308 /* Dump the resident name table */
310 TRACE( "Resident-name table:\n" );
311 pstr
= (BYTE
*) pModule
+ pModule
->ne_restab
;
314 TRACE( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
315 *(WORD
*)(pstr
+ *pstr
+ 1) );
316 pstr
+= *pstr
+ 1 + sizeof(WORD
);
319 /* Dump the module reference table */
321 TRACE( "Module ref table:\n" );
322 if (pModule
->ne_modtab
)
324 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->ne_modtab
);
325 for (i
= 0; i
< pModule
->ne_cmod
; i
++, pword
++)
328 GetModuleName16( *pword
, name
, sizeof(name
) );
329 TRACE( "%d: %04x -> '%s'\n", i
, *pword
, name
);
332 else TRACE( "None\n" );
334 /* Dump the entry table */
336 TRACE( "Entry table:\n" );
337 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+pModule
->ne_enttab
);
339 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
340 TRACE( "Bundle %d-%d: %02x\n", bundle
->first
, bundle
->last
, entry
->type
);
341 ordinal
= bundle
->first
;
342 while (ordinal
< bundle
->last
)
344 if (entry
->type
== 0xff)
345 TRACE("%d: %02x:%04x (moveable)\n", ordinal
++, entry
->segnum
, entry
->offs
);
347 TRACE("%d: %02x:%04x (fixed)\n", ordinal
++, entry
->segnum
, entry
->offs
);
350 } while ( (bundle
->next
) && (bundle
= ((ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
))) );
352 /* Dump the non-resident names table */
354 TRACE( "Non-resident names table:\n" );
355 if (pModule
->nrname_handle
)
357 pstr
= GlobalLock16( pModule
->nrname_handle
);
360 TRACE( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
361 *(WORD
*)(pstr
+ *pstr
+ 1) );
362 pstr
+= *pstr
+ 1 + sizeof(WORD
);
369 /***********************************************************************
372 * Walk the module list and print the modules.
374 void NE_WalkModules(void)
376 HMODULE16 hModule
= hFirstModule
;
377 MESSAGE( "Module Flags Name\n" );
380 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
383 MESSAGE( "Bad module %04x in list\n", hModule
);
386 MESSAGE( " %04x %04x %.*s\n", hModule
, pModule
->ne_flags
,
387 *((char *)pModule
+ pModule
->ne_restab
),
388 (char *)pModule
+ pModule
->ne_restab
+ 1 );
389 hModule
= pModule
->next
;
394 /***********************************************************************
395 * NE_InitResourceHandler
397 * Fill in 'resloader' fields in the resource table.
399 static void NE_InitResourceHandler( HMODULE16 hModule
)
401 static FARPROC16 proc
;
403 NE_TYPEINFO
*pTypeInfo
;
406 if (!(pModule
= NE_GetPtr( hModule
)) || !pModule
->ne_rsrctab
) return;
408 TRACE("InitResourceHandler[%04x]\n", hModule
);
410 if (!proc
) proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
412 pTypeInfo
= (NE_TYPEINFO
*)((char *)pModule
+ pModule
->ne_rsrctab
+ 2);
413 while(pTypeInfo
->type_id
)
415 memcpy_unaligned( &pTypeInfo
->resloader
, &proc
, sizeof(FARPROC16
) );
416 pTypeInfo
= (NE_TYPEINFO
*)((char*)(pTypeInfo
+ 1) + pTypeInfo
->count
* sizeof(NE_NAMEINFO
));
421 /***********************************************************************
424 * Lookup the ordinal for a given name.
426 WORD
NE_GetOrdinal( HMODULE16 hModule
, const char *name
)
428 char buffer
[256], *p
;
433 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
434 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) return 0;
436 TRACE("(%04x,'%s')\n", hModule
, name
);
438 /* First handle names of the form '#xxxx' */
440 if (name
[0] == '#') return atoi( name
+ 1 );
442 /* Now copy and uppercase the string */
444 strcpy( buffer
, name
);
445 for (p
= buffer
; *p
; p
++) *p
= RtlUpperChar(*p
);
448 /* First search the resident names */
450 cpnt
= (BYTE
*)pModule
+ pModule
->ne_restab
;
452 /* Skip the first entry (module name) */
453 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
456 if ((*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
459 memcpy( &ordinal
, cpnt
+ *cpnt
+ 1, sizeof(ordinal
) );
460 TRACE(" Found: ordinal=%d\n", ordinal
);
463 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
466 /* Now search the non-resident names table */
468 if (!pModule
->nrname_handle
) return 0; /* No non-resident table */
469 cpnt
= GlobalLock16( pModule
->nrname_handle
);
471 /* Skip the first entry (module description string) */
472 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
475 if ((*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
478 memcpy( &ordinal
, cpnt
+ *cpnt
+ 1, sizeof(ordinal
) );
479 TRACE(" Found: ordinal=%d\n", ordinal
);
482 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
488 /***********************************************************************
491 FARPROC16 WINAPI
NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
493 return NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
496 /***********************************************************************
499 FARPROC16
NE_GetEntryPointEx( HMODULE16 hModule
, WORD ordinal
, BOOL16 snoop
)
507 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
508 assert( !(pModule
->ne_flags
& NE_FFLAGS_WIN32
) );
510 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->ne_enttab
);
511 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
515 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
518 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
519 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
523 memcpy( &offset
, &entry
->offs
, sizeof(WORD
) );
525 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
526 else sel
= GlobalHandleToSel16(NE_SEG_TABLE(pModule
)[sel
-1].hSeg
);
528 return (FARPROC16
)MAKESEGPTR( sel
, offset
);
530 return (FARPROC16
)MAKESEGPTR( sel
, offset
);
532 return SNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)MAKESEGPTR( sel
, offset
));
536 /***********************************************************************
537 * EntryAddrProc (KERNEL.667) Wine-specific export
539 * Return the entry point for a given ordinal.
541 FARPROC16 WINAPI
EntryAddrProc16( HMODULE16 hModule
, WORD ordinal
)
543 FARPROC16 ret
= NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
544 CURRENT_STACK16
->ecx
= hModule
; /* FIXME: might be incorrect value */
548 /***********************************************************************
551 * Change the value of an entry point. Use with caution!
552 * It can only change the offset value, not the selector.
554 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
561 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
562 assert( !(pModule
->ne_flags
& NE_FFLAGS_WIN32
) );
564 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->ne_enttab
);
565 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
567 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
568 if (!(bundle
->next
)) return 0;
571 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
572 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
575 memcpy( &entry
->offs
, &offset
, sizeof(WORD
) );
580 /***********************************************************************
583 * Build the entry table bundle data from the on-disk format. Helper for build_module.
585 static void *build_bundle_data( NE_MODULE
*pModule
, void *dest
, const BYTE
*table
)
587 ET_BUNDLE
*oldbundle
, *bundle
= dest
;
589 BYTE nr_entries
, type
;
591 memset(bundle
, 0, sizeof(ET_BUNDLE
)); /* in case no entry table exists */
592 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
594 while ((nr_entries
= *table
++))
596 if ((type
= *table
++))
598 bundle
->last
+= nr_entries
;
604 entry
->flags
= *table
++;
605 table
+= sizeof(WORD
);
606 entry
->segnum
= *table
++;
607 entry
->offs
= *(const WORD
*)table
;
608 table
+= sizeof(WORD
);
617 entry
->flags
= *table
++;
618 entry
->segnum
= type
;
619 entry
->offs
= *(const WORD
*)table
;
620 table
+= sizeof(WORD
);
627 if (bundle
->first
== bundle
->last
)
629 bundle
->first
+= nr_entries
;
630 bundle
->last
+= nr_entries
;
635 oldbundle
->next
= (char *)entry
- (char *)pModule
;
636 bundle
= (ET_BUNDLE
*)entry
;
637 bundle
->first
= bundle
->last
= oldbundle
->last
+ nr_entries
;
639 entry
= (ET_ENTRY
*)(((BYTE
*)entry
)+sizeof(ET_BUNDLE
));
647 /***********************************************************************
650 * Build the in-memory module from the on-disk data.
652 static HMODULE16
build_module( const void *mapping
, SIZE_T mapping_size
, LPCSTR path
)
654 const IMAGE_DOS_HEADER
*mz_header
= mapping
;
655 const IMAGE_OS2_HEADER
*ne_header
;
656 const struct ne_segment_table_entry_s
*pSeg
;
662 BYTE
*buffer
, *pData
, *end
;
665 if (mapping_size
< sizeof(*mz_header
)) return ERROR_BAD_FORMAT
;
666 if (mz_header
->e_magic
!= IMAGE_DOS_SIGNATURE
) return ERROR_BAD_FORMAT
;
667 ne_header
= (const IMAGE_OS2_HEADER
*)((const char *)mapping
+ mz_header
->e_lfanew
);
668 if (mz_header
->e_lfanew
+ sizeof(*ne_header
) > mapping_size
) return ERROR_BAD_FORMAT
;
669 if (ne_header
->ne_magic
== IMAGE_NT_SIGNATURE
) return 21; /* win32 exe */
670 if (ne_header
->ne_magic
== IMAGE_OS2_SIGNATURE_LX
)
672 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path
);
675 if (ne_header
->ne_magic
!= IMAGE_OS2_SIGNATURE
) return ERROR_BAD_FORMAT
;
677 /* We now have a valid NE header */
679 /* check to be able to fall back to loading OS/2 programs as DOS
680 * FIXME: should this check be reversed in order to be less strict?
681 * (only fail for OS/2 ne_exetyp 0x01 here?) */
682 if ((ne_header
->ne_exetyp
!= 0x02 /* Windows */)
683 && (ne_header
->ne_exetyp
!= 0x04) /* Windows 386 */)
684 return ERROR_BAD_FORMAT
;
686 size
= sizeof(NE_MODULE
) +
688 ne_header
->ne_cseg
* sizeof(SEGTABLEENTRY
) +
690 ne_header
->ne_restab
- ne_header
->ne_rsrctab
+
691 /* resident names table */
692 ne_header
->ne_modtab
- ne_header
->ne_restab
+
693 /* module ref table */
694 ne_header
->ne_cmod
* sizeof(WORD
) +
695 /* imported names table */
696 ne_header
->ne_enttab
- ne_header
->ne_imptab
+
697 /* entry table length */
698 ne_header
->ne_cbenttab
+
699 /* entry table extra conversion space */
701 2 * (ne_header
->ne_cbenttab
- ne_header
->ne_cmovent
*6) +
702 /* loaded file info */
703 sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
) + 1;
705 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
706 if (!hModule
) return ERROR_BAD_FORMAT
;
708 FarSetOwner16( hModule
, hModule
);
709 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
710 memcpy( pModule
, ne_header
, sizeof(*ne_header
) );
712 /* check programs for default minimal stack size */
713 if (!(pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
) && (pModule
->ne_stack
< 0x1400))
714 pModule
->ne_stack
= 0x1400;
716 pModule
->self
= hModule
;
717 pModule
->mapping
= mapping
;
718 pModule
->mapping_size
= mapping_size
;
720 pData
= (BYTE
*)(pModule
+ 1);
722 /* Clear internal Wine flags in case they are set in the EXE file */
724 pModule
->ne_flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
726 /* Get the segment table */
728 pModule
->ne_segtab
= pData
- (BYTE
*)pModule
;
729 if (!(pSeg
= NE_GET_DATA( pModule
, mz_header
->e_lfanew
+ ne_header
->ne_segtab
,
730 ne_header
->ne_cseg
* sizeof(struct ne_segment_table_entry_s
) )))
732 for (i
= ne_header
->ne_cseg
; i
> 0; i
--, pSeg
++)
734 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
735 pData
+= sizeof(SEGTABLEENTRY
);
738 /* Get the resource table */
740 if (ne_header
->ne_rsrctab
< ne_header
->ne_restab
)
742 pModule
->ne_rsrctab
= pData
- (BYTE
*)pModule
;
743 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_rsrctab
,
744 ne_header
->ne_restab
- ne_header
->ne_rsrctab
)) goto failed
;
745 pData
+= ne_header
->ne_restab
- ne_header
->ne_rsrctab
;
747 else pModule
->ne_rsrctab
= 0; /* No resource table */
749 /* Get the resident names table */
751 pModule
->ne_restab
= pData
- (BYTE
*)pModule
;
752 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_restab
,
753 ne_header
->ne_modtab
- ne_header
->ne_restab
)) goto failed
;
754 pData
+= ne_header
->ne_modtab
- ne_header
->ne_restab
;
756 /* Get the module references table */
758 if (ne_header
->ne_cmod
> 0)
760 pModule
->ne_modtab
= pData
- (BYTE
*)pModule
;
761 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_modtab
,
762 ne_header
->ne_cmod
* sizeof(WORD
) )) goto failed
;
763 pData
+= ne_header
->ne_cmod
* sizeof(WORD
);
765 else pModule
->ne_modtab
= 0; /* No module references */
767 /* Get the imported names table */
769 pModule
->ne_imptab
= pData
- (BYTE
*)pModule
;
770 if (!NE_READ_DATA( pModule
, pData
, mz_header
->e_lfanew
+ ne_header
->ne_imptab
,
771 ne_header
->ne_enttab
- ne_header
->ne_imptab
)) goto failed
;
772 pData
+= ne_header
->ne_enttab
- ne_header
->ne_imptab
;
774 /* Load entry table, convert it to the optimized version used by Windows */
776 pModule
->ne_enttab
= pData
- (BYTE
*)pModule
;
777 if (!(ptr
= NE_GET_DATA( pModule
, mz_header
->e_lfanew
+ ne_header
->ne_enttab
,
778 ne_header
->ne_cbenttab
))) goto failed
;
779 end
= build_bundle_data( pModule
, pData
, ptr
);
781 pData
+= ne_header
->ne_cbenttab
+ sizeof(ET_BUNDLE
) +
782 2 * (ne_header
->ne_cbenttab
- ne_header
->ne_cmovent
*6);
786 FIXME( "not enough space for entry table for %s\n", debugstr_a(path
) );
790 /* Store the filename information */
792 pModule
->fileinfo
= pData
- (BYTE
*)pModule
;
793 ofs
= (OFSTRUCT
*)pData
;
794 ofs
->cBytes
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
);
796 strcpy( ofs
->szPathName
, path
);
797 pData
+= ofs
->cBytes
+ 1;
798 assert( (BYTE
*)pModule
+ size
<= pData
);
800 /* Get the non-resident names table */
802 if (ne_header
->ne_cbnrestab
)
804 pModule
->nrname_handle
= GlobalAlloc16( 0, ne_header
->ne_cbnrestab
);
805 if (!pModule
->nrname_handle
) goto failed
;
806 FarSetOwner16( pModule
->nrname_handle
, hModule
);
807 buffer
= GlobalLock16( pModule
->nrname_handle
);
808 if (!NE_READ_DATA( pModule
, buffer
, ne_header
->ne_nrestab
, ne_header
->ne_cbnrestab
))
810 GlobalFree16( pModule
->nrname_handle
);
814 else pModule
->nrname_handle
= 0;
816 /* Allocate a segment for the implicitly-loaded DLLs */
818 if (pModule
->ne_cmod
)
820 pModule
->dlls_to_init
= GlobalAlloc16( GMEM_ZEROINIT
,
821 (pModule
->ne_cmod
+1)*sizeof(HMODULE16
) );
822 if (!pModule
->dlls_to_init
)
824 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
827 FarSetOwner16( pModule
->dlls_to_init
, hModule
);
829 else pModule
->dlls_to_init
= 0;
831 NE_RegisterModule( pModule
);
835 GlobalFree16( hModule
);
836 return ERROR_BAD_FORMAT
;
840 /***********************************************************************
843 * Load all DLLs implicitly linked to a module.
845 static BOOL
NE_LoadDLLs( NE_MODULE
*pModule
)
848 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->ne_modtab
);
849 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
851 for (i
= 0; i
< pModule
->ne_cmod
; i
++, pModRef
++)
853 char buffer
[260], *p
;
854 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->ne_imptab
+ *pModRef
;
855 memcpy( buffer
, pstr
+ 1, *pstr
);
856 *(buffer
+ *pstr
) = 0; /* terminate it */
858 TRACE("Loading '%s'\n", buffer
);
859 if (!(*pModRef
= GetModuleHandle16( buffer
)))
861 /* If the DLL is not loaded yet, load it and store */
862 /* its handle in the list of DLLs to initialize. */
865 /* Append .DLL to name if no extension present */
866 if (!(p
= strrchr( buffer
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
867 strcat( buffer
, ".DLL" );
869 if ((hDLL
= MODULE_LoadModule16( buffer
, TRUE
, TRUE
)) < 32)
871 /* FIXME: cleanup what was done */
873 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
874 buffer
, *((BYTE
*)pModule
+ pModule
->ne_restab
),
875 (char *)pModule
+ pModule
->ne_restab
+ 1, hDLL
);
878 *pModRef
= GetExePtr( hDLL
);
881 else /* Increment the reference count of the DLL */
883 NE_MODULE
*pOldDLL
= NE_GetPtr( *pModRef
);
884 if (pOldDLL
) pOldDLL
->count
++;
891 /**********************************************************************
894 * Load first instance of NE module from file.
896 * pModule must point to a module structure prepared by build_module.
897 * This routine must never be called twice on a module.
899 static HINSTANCE16
NE_DoLoadModule( NE_MODULE
*pModule
)
901 /* Allocate the segments for this module */
903 if (!NE_CreateAllSegments( pModule
))
904 return ERROR_NOT_ENOUGH_MEMORY
; /* 8 */
906 /* Load the referenced DLLs */
908 if (!NE_LoadDLLs( pModule
))
909 return ERROR_FILE_NOT_FOUND
; /* 2 */
911 /* Load the segments */
913 NE_LoadAllSegments( pModule
);
915 /* Make sure the usage count is 1 on the first loading of */
916 /* the module, even if it contains circular DLL references */
920 return NE_GetInstance( pModule
);
923 /**********************************************************************
926 * Load first instance of NE module. (Note: caller is responsible for
927 * ensuring the module isn't already loaded!)
929 * If the module turns out to be an executable module, only a
930 * handle to a module stub is returned; this needs to be initialized
931 * by calling NE_DoLoadModule later, in the context of the newly
934 * If lib_only is TRUE, however, the module is perforce treated
935 * like a DLL module, even if it is an executable module.
938 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
)
942 HINSTANCE16 hInstance
;
947 MEMORY_BASIC_INFORMATION info
;
950 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
|OF_SHARE_DENY_WRITE
)) == HFILE_ERROR16
)
951 return ERROR_FILE_NOT_FOUND
;
953 mapping
= CreateFileMappingW( DosFileHandleToWin32Handle(hFile
), NULL
, PAGE_WRITECOPY
, 0, 0, NULL
);
955 if (!mapping
) return ERROR_BAD_FORMAT
;
957 ptr
= MapViewOfFile( mapping
, FILE_MAP_COPY
, 0, 0, 0 );
958 CloseHandle( mapping
);
959 if (!ptr
) return ERROR_BAD_FORMAT
;
961 VirtualQuery( ptr
, &info
, sizeof(info
) );
962 hModule
= build_module( ptr
, info
.RegionSize
, ofs
.szPathName
);
966 UnmapViewOfFile( ptr
);
970 SNOOP16_RegisterDLL( hModule
, ofs
.szPathName
);
971 NE_InitResourceHandler( hModule
);
973 pModule
= NE_GetPtr( hModule
);
975 if ( !lib_only
&& !( pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
) )
978 hInstance
= NE_DoLoadModule( pModule
);
979 if ( hInstance
< 32 )
982 NE_FreeModule( hModule
, 0 );
989 /***********************************************************************
990 * NE_DoLoadBuiltinModule
992 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
994 static HMODULE16
NE_DoLoadBuiltinModule( const IMAGE_DOS_HEADER
*mz_header
, const char *file_name
,
999 HINSTANCE16 hInstance
;
1000 OSVERSIONINFOW versionInfo
;
1001 const IMAGE_OS2_HEADER
*ne_header
;
1002 SIZE_T mapping_size
= ~0UL; /* assume builtins don't contain invalid offsets... */
1004 ne_header
= (const IMAGE_OS2_HEADER
*)((const BYTE
*)mz_header
+ mz_header
->e_lfanew
);
1005 hModule
= build_module( mz_header
, mapping_size
, file_name
);
1006 if (hModule
< 32) return hModule
;
1007 pModule
= GlobalLock16( hModule
);
1008 pModule
->ne_flags
|= NE_FFLAGS_BUILTIN
;
1009 pModule
->owner32
= owner32
;
1011 /* fake the expected version the module should have according to the current Windows version */
1012 versionInfo
.dwOSVersionInfoSize
= sizeof(versionInfo
);
1013 if (GetVersionExW( &versionInfo
))
1014 pModule
->ne_expver
= MAKEWORD( versionInfo
.dwMinorVersion
, versionInfo
.dwMajorVersion
);
1016 hInstance
= NE_DoLoadModule( pModule
);
1017 if (hInstance
< 32) NE_FreeModule( hModule
, 0 );
1019 NE_InitResourceHandler( hModule
);
1021 if (pModule
->ne_heap
)
1023 SEGTABLEENTRY
*pSeg
= NE_SEG_TABLE( pModule
) + pModule
->ne_autodata
- 1;
1024 unsigned int size
= pSeg
->minsize
+ pModule
->ne_heap
;
1025 if (size
> 0xfffe) size
= 0xfffe;
1026 LocalInit16( GlobalHandleToSel16(pSeg
->hSeg
), pSeg
->minsize
, size
);
1029 patch_code_segment( pModule
);
1035 /**********************************************************************
1036 * MODULE_LoadModule16
1038 * Load a NE module in the order of the loadorder specification.
1039 * The caller is responsible that the module is not loaded already.
1042 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
)
1044 HINSTANCE16 hinst
= 2;
1048 const IMAGE_DOS_HEADER
*descr
= NULL
;
1049 const char *file_name
= NULL
;
1050 char dllname
[20], owner
[20], *p
;
1051 const char *basename
;
1052 int owner_exists
= FALSE
;
1054 /* strip path information */
1057 if (basename
[0] && basename
[1] == ':') basename
+= 2; /* strip drive specification */
1058 if ((p
= strrchr( basename
, '\\' ))) basename
= p
+ 1;
1059 if ((p
= strrchr( basename
, '/' ))) basename
= p
+ 1;
1061 if (strlen(basename
) < sizeof(dllname
)-4)
1063 strcpy( dllname
, basename
);
1064 p
= strrchr( dllname
, '.' );
1065 if (!p
) strcat( dllname
, ".dll" );
1066 for (p
= dllname
; *p
; p
++) if (*p
>= 'A' && *p
<= 'Z') *p
+= 32;
1068 if (wine_dll_get_owner( dllname
, owner
, sizeof(owner
), &owner_exists
) != -1)
1070 mod32
= LoadLibraryA( owner
);
1073 if (!(descr
= find_dll_descr( dllname
, &file_name
)))
1075 FreeLibrary( mod32
);
1078 /* loading the 32-bit library can have the side effect of loading the module */
1079 /* if so, simply incr the ref count and return the module */
1080 if ((hModule
= GetModuleHandle16( libname
)))
1082 TRACE( "module %s already loaded by owner\n", libname
);
1083 pModule
= NE_GetPtr( hModule
);
1084 if (pModule
) pModule
->count
++;
1085 FreeLibrary( mod32
);
1091 /* it's probably disabled by the load order config */
1092 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner
, dllname
);
1093 return ERROR_FILE_NOT_FOUND
;
1100 TRACE("Trying built-in '%s'\n", libname
);
1101 hinst
= NE_DoLoadBuiltinModule( descr
, file_name
, mod32
);
1102 if (hinst
> 32) TRACE_(loaddll
)("Loaded module %s : builtin\n", debugstr_a(file_name
));
1106 TRACE("Trying native dll '%s'\n", libname
);
1107 hinst
= NE_LoadModule(libname
, lib_only
);
1108 if (hinst
> 32) TRACE_(loaddll
)("Loaded module %s : native\n", debugstr_a(libname
));
1109 if (hinst
== ERROR_FILE_NOT_FOUND
&& owner_exists
) hinst
= 21; /* win32 module */
1112 if (hinst
> 32 && !implicit
)
1114 hModule
= GetModuleHandle16(libname
);
1117 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1119 return ERROR_INVALID_HANDLE
;
1122 pModule
= NE_GetPtr(hModule
);
1125 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1127 return ERROR_INVALID_HANDLE
;
1130 TRACE("Loaded module '%s' at 0x%04x.\n", libname
, hinst
);
1133 * Call initialization routines for all loaded DLLs. Note that
1134 * when we load implicitly linked DLLs this will be done by InitTask().
1136 if(pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
)
1138 NE_InitializeDLLs(hModule
);
1139 NE_DllProcessAttach(hModule
);
1142 return hinst
; /* The last error that occurred */
1146 /**********************************************************************
1149 * Create the thread for a 16-bit module.
1151 static HINSTANCE16
NE_CreateThread( NE_MODULE
*pModule
, WORD cmdShow
, LPCSTR cmdline
)
1156 HINSTANCE16 instance
= 0;
1158 if (!(hTask
= TASK_SpawnTask( pModule
, cmdShow
, cmdline
+ 1, *cmdline
, &hThread
)))
1161 /* Post event to start the task */
1162 PostEvent16( hTask
);
1164 /* Wait until we get the instance handle */
1167 DirectedYield16( hTask
);
1168 if (!IsTask16( hTask
)) /* thread has died */
1171 WaitForSingleObject( hThread
, INFINITE
);
1172 GetExitCodeThread( hThread
, &exit_code
);
1173 CloseHandle( hThread
);
1176 if (!(pTask
= GlobalLock16( hTask
))) break;
1177 instance
= pTask
->hInstance
;
1178 GlobalUnlock16( hTask
);
1179 } while (!instance
);
1181 CloseHandle( hThread
);
1186 /**********************************************************************
1187 * LoadModule (KERNEL.45)
1189 HINSTANCE16 WINAPI
LoadModule16( LPCSTR name
, LPVOID paramBlock
)
1191 BOOL lib_only
= !paramBlock
|| (paramBlock
== (LPVOID
)-1);
1192 LOADPARAMS16
*params
;
1196 WORD cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
1198 if (name
== NULL
) return 0;
1200 TRACE("name %s, paramBlock %p\n", name
, paramBlock
);
1204 if ( (hModule
= NE_GetModuleByFilename(name
) ) != 0 )
1206 /* Special case: second instance of an already loaded NE module */
1208 if ( !( pModule
= NE_GetPtr( hModule
) ) ) return ERROR_BAD_FORMAT
;
1209 if ( pModule
->module32
) return (HINSTANCE16
)21;
1211 /* Increment refcount */
1217 /* Main case: load first instance of NE module */
1219 if ( (hModule
= MODULE_LoadModule16( name
, FALSE
, lib_only
)) < 32 )
1222 if ( !(pModule
= NE_GetPtr( hModule
)) )
1223 return ERROR_BAD_FORMAT
;
1226 /* If library module, we just retrieve the instance handle */
1228 if ( ( pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
) || lib_only
)
1229 return NE_GetInstance( pModule
);
1232 * At this point, we need to create a new process.
1234 * pModule points either to an already loaded module, whose refcount
1235 * has already been incremented (to avoid having the module vanish
1236 * in the meantime), or else to a stub module which contains only header
1239 params
= (LOADPARAMS16
*)paramBlock
;
1240 if (params
->showCmd
)
1241 cmdShow
= ((WORD
*)MapSL( params
->showCmd
))[1];
1242 cmdline
= MapSL( params
->cmdLine
);
1243 return NE_CreateThread( pModule
, cmdShow
, cmdline
);
1247 /**********************************************************************
1250 * Startup code for a new 16-bit task.
1252 DWORD
NE_StartTask(void)
1254 TDB
*pTask
= TASK_GetCurrent();
1255 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
1256 HINSTANCE16 hInstance
, hPrevInstance
;
1257 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
1260 if ( pModule
->count
> 0 )
1262 /* Second instance of an already loaded NE module */
1263 /* Note that the refcount was already incremented by the parent */
1265 hPrevInstance
= NE_GetInstance( pModule
);
1267 if ( pModule
->ne_autodata
)
1268 if ( NE_CreateSegment( pModule
, pModule
->ne_autodata
) )
1269 NE_LoadSegment( pModule
, pModule
->ne_autodata
);
1271 hInstance
= NE_GetInstance( pModule
);
1272 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance
, pModule
->ne_autodata
, hPrevInstance
);
1277 /* Load first instance of NE module */
1279 pModule
->ne_flags
|= NE_FFLAGS_GUI
; /* FIXME: is this necessary? */
1281 hInstance
= NE_DoLoadModule( pModule
);
1285 if ( hInstance
>= 32 )
1289 /* Enter instance handles into task struct */
1291 pTask
->hInstance
= hInstance
;
1292 pTask
->hPrevInstance
= hPrevInstance
;
1294 /* Use DGROUP for 16-bit stack */
1296 if (!(sp
= OFFSETOF(pModule
->ne_sssp
)))
1297 sp
= pSegTable
[SELECTOROF(pModule
->ne_sssp
)-1].minsize
+ pModule
->ne_stack
;
1299 sp
-= sizeof(STACK16FRAME
);
1300 NtCurrentTeb()->WOW32Reserved
= (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance
), sp
);
1302 /* Registers at initialization must be:
1304 * bx stack size in bytes
1305 * cx heap size in bytes
1306 * si previous app instance
1307 * di current app instance
1309 * es selector to the PSP
1310 * ds dgroup of the application
1312 * sp top of the stack
1314 memset( &context
, 0, sizeof(context
) );
1315 context
.SegCs
= GlobalHandleToSel16(pSegTable
[SELECTOROF(pModule
->ne_csip
) - 1].hSeg
);
1316 context
.SegDs
= GlobalHandleToSel16(pTask
->hInstance
);
1317 context
.SegEs
= pTask
->hPDB
;
1318 context
.SegFs
= wine_get_fs();
1319 context
.SegGs
= wine_get_gs();
1320 context
.Eip
= OFFSETOF(pModule
->ne_csip
);
1321 context
.Ebx
= pModule
->ne_stack
;
1322 context
.Ecx
= pModule
->ne_heap
;
1323 context
.Edi
= pTask
->hInstance
;
1324 context
.Esi
= pTask
->hPrevInstance
;
1326 /* Now call 16-bit entry point */
1328 TRACE("Starting main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
1329 context
.SegCs
, context
.Eip
, context
.SegDs
,
1330 SELECTOROF(NtCurrentTeb()->WOW32Reserved
),
1331 OFFSETOF(NtCurrentTeb()->WOW32Reserved
) );
1333 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
1334 ExitThread( LOWORD(context
.Eax
) );
1336 return hInstance
; /* error code */
1339 /***********************************************************************
1340 * LoadLibrary (KERNEL.95)
1341 * LoadLibrary16 (KERNEL32.35)
1343 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
1345 return LoadModule16(libname
, (LPVOID
)-1 );
1349 /**********************************************************************
1352 * Call a DLL's WEP, allowing it to shut down.
1353 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1355 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
1358 FARPROC16 WEP
= GetProcAddress16( hModule
, "WEP" );
1359 if (!WEP
) return FALSE
;
1366 args
[0] = WEP_FREE_DLL
;
1367 WOWCallback16Ex( (DWORD
)WEP
, WCB16_PASCAL
, sizeof(args
), args
, &dwRet
);
1368 ret
= LOWORD(dwRet
);
1372 WARN("Page fault\n");
1381 /**********************************************************************
1384 * Implementation of FreeModule16().
1386 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
)
1388 HMODULE16
*hPrevModule
;
1393 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
1394 hModule
= pModule
->self
;
1396 TRACE("%04x count %d\n", hModule
, pModule
->count
);
1398 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
1399 else pModule
->count
= 0;
1401 if (call_wep
&& !(pModule
->ne_flags
& NE_FFLAGS_WIN32
))
1403 /* Free the objects owned by the DLL module */
1404 NE_CallUserSignalProc( hModule
, USIG16_DLL_UNLOAD
);
1406 if (pModule
->ne_flags
& NE_FFLAGS_LIBMODULE
)
1407 MODULE_CallWEP( hModule
);
1409 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
1412 TRACE_(loaddll
)("Unloaded module %s : %s\n", debugstr_a(NE_MODULE_NAME(pModule
)),
1413 (pModule
->ne_flags
& NE_FFLAGS_BUILTIN
) ? "builtin" : "native");
1415 /* Clear magic number just in case */
1417 pModule
->ne_magic
= pModule
->self
= 0;
1418 if (pModule
->owner32
) FreeLibrary( pModule
->owner32
);
1419 else if (pModule
->mapping
) UnmapViewOfFile( pModule
->mapping
);
1421 /* Remove it from the linked list */
1423 hPrevModule
= &hFirstModule
;
1424 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
1426 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
1428 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
1430 /* Free the referenced modules */
1432 pModRef
= (HMODULE16
*)((char *)pModule
+ pModule
->ne_modtab
);
1433 for (i
= 0; i
< pModule
->ne_cmod
; i
++, pModRef
++)
1435 NE_FreeModule( *pModRef
, call_wep
);
1438 /* Free the module storage */
1440 GlobalFreeAll16( hModule
);
1445 /**********************************************************************
1446 * FreeModule (KERNEL.46)
1448 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
1450 return NE_FreeModule( hModule
, TRUE
);
1454 /***********************************************************************
1455 * FreeLibrary (KERNEL.96)
1456 * FreeLibrary16 (KERNEL32.36)
1458 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
1460 TRACE("%04x\n", handle
);
1461 FreeModule16( handle
);
1465 /***********************************************************************
1466 * GetModuleHandle16 (KERNEL32.@)
1468 HMODULE16 WINAPI
GetModuleHandle16( LPCSTR name
)
1470 HMODULE16 hModule
= hFirstModule
;
1472 BYTE len
, *name_table
;
1473 char tmpstr
[MAX_PATH
];
1476 TRACE("(%s)\n", name
);
1478 if (!HIWORD(name
)) return GetExePtr(LOWORD(name
));
1483 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1485 /* If 'name' matches exactly the module name of a module:
1486 * Return its handle.
1488 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1490 pModule
= NE_GetPtr( hModule
);
1491 if (!pModule
) break;
1492 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1494 name_table
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1495 if ((*name_table
== len
) && !strncmp(name
, (char*) name_table
+1, len
))
1499 /* If uppercased 'name' matches exactly the module name of a module:
1502 for (s
= tmpstr
; *s
; s
++) *s
= RtlUpperChar(*s
);
1504 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1506 pModule
= NE_GetPtr( hModule
);
1507 if (!pModule
) break;
1508 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1510 name_table
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1511 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1512 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1513 * lowercased module names, but try to load uppercase DLLs, so this
1514 * 'i' compare is just a quickfix until the loader handles that
1515 * correctly. -MM 990705
1517 if ((*name_table
== len
) && !NE_strncasecmp(tmpstr
, (const char*)name_table
+1, len
))
1521 /* If the base filename of 'name' matches the base filename of the module
1522 * filename of some module (case-insensitive compare):
1523 * Return its handle.
1526 /* basename: search backwards in passed name to \ / or : */
1527 s
= tmpstr
+ strlen(tmpstr
);
1530 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1535 /* search this in loaded filename list */
1536 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1541 pModule
= NE_GetPtr( hModule
);
1542 if (!pModule
) break;
1543 if (!pModule
->fileinfo
) continue;
1544 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1546 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1547 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1548 /* basename: search backwards in pathname to \ / or : */
1549 while (loadedfn
> (char*)ofs
->szPathName
)
1551 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1555 /* case insensitive compare ... */
1556 if (!NE_strcasecmp(loadedfn
, s
))
1563 /**********************************************************************
1564 * GetModuleName (KERNEL.27)
1566 BOOL16 WINAPI
GetModuleName16( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
1571 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
1572 p
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1573 if (count
> *p
) count
= *p
+ 1;
1576 memcpy( buf
, p
+ 1, count
- 1 );
1577 buf
[count
-1] = '\0';
1583 /**********************************************************************
1584 * GetModuleFileName (KERNEL.49)
1586 * Comment: see GetModuleFileNameA
1588 * Even if invoked by second instance of a program,
1589 * it still returns path of first one.
1591 INT16 WINAPI
GetModuleFileName16( HINSTANCE16 hModule
, LPSTR lpFileName
,
1596 /* Win95 does not query hModule if set to 0 !
1597 * Is this wrong or maybe Win3.1 only ? */
1598 if (!hModule
) hModule
= GetCurrentTask();
1600 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
1601 lstrcpynA( lpFileName
, NE_MODULE_NAME(pModule
), nSize
);
1602 if (pModule
->ne_expver
< 0x400)
1603 GetShortPathNameA(NE_MODULE_NAME(pModule
), lpFileName
, nSize
);
1604 TRACE("%04x -> '%s'\n", hModule
, lpFileName
);
1605 return strlen(lpFileName
);
1609 /**********************************************************************
1610 * GetModuleUsage (KERNEL.48)
1612 INT16 WINAPI
GetModuleUsage16( HINSTANCE16 hModule
)
1614 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1615 return pModule
? pModule
->count
: 0;
1619 /**********************************************************************
1620 * GetExpWinVer (KERNEL.167)
1622 WORD WINAPI
GetExpWinVer16( HMODULE16 hModule
)
1624 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1625 if ( !pModule
) return 0;
1626 return pModule
->ne_expver
;
1630 /***********************************************************************
1631 * WinExec (KERNEL.166)
1633 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
1635 LPCSTR p
, args
= NULL
;
1636 LPCSTR name_beg
, name_end
;
1637 LPSTR name
, cmdline
;
1640 char buffer
[MAX_PATH
];
1642 if (*lpCmdLine
== '"') /* has to be only one and only at beginning ! */
1644 name_beg
= lpCmdLine
+1;
1645 p
= strchr ( lpCmdLine
+1, '"' );
1649 args
= strchr ( p
, ' ' );
1651 else /* yes, even valid with trailing '"' missing */
1652 name_end
= lpCmdLine
+strlen(lpCmdLine
);
1656 name_beg
= lpCmdLine
;
1657 args
= strchr( lpCmdLine
, ' ' );
1658 name_end
= args
? args
: lpCmdLine
+strlen(lpCmdLine
);
1661 if ((name_beg
== lpCmdLine
) && (!args
))
1662 { /* just use the original cmdline string as file name */
1663 name
= (LPSTR
)lpCmdLine
;
1667 if (!(name
= HeapAlloc( GetProcessHeap(), 0, name_end
- name_beg
+ 1 )))
1668 return ERROR_NOT_ENOUGH_MEMORY
;
1669 memcpy( name
, name_beg
, name_end
- name_beg
);
1670 name
[name_end
- name_beg
] = '\0';
1676 arglen
= strlen(args
);
1677 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 + arglen
);
1678 cmdline
[0] = (BYTE
)arglen
;
1679 strcpy( cmdline
+ 1, args
);
1683 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 );
1684 cmdline
[0] = cmdline
[1] = 0;
1687 TRACE("name: '%s', cmdline: '%.*s'\n", name
, cmdline
[0], &cmdline
[1]);
1689 if (SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, NULL
))
1691 LOADPARAMS16 params
;
1694 showCmd
[1] = nCmdShow
;
1696 params
.hEnvironment
= 0;
1697 params
.cmdLine
= MapLS( cmdline
);
1698 params
.showCmd
= MapLS( showCmd
);
1699 params
.reserved
= 0;
1701 ret
= LoadModule16( buffer
, ¶ms
);
1702 UnMapLS( params
.cmdLine
);
1703 UnMapLS( params
.showCmd
);
1705 else ret
= GetLastError();
1707 HeapFree( GetProcessHeap(), 0, cmdline
);
1708 if (name
!= lpCmdLine
) HeapFree( GetProcessHeap(), 0, name
);
1710 if (ret
== 21 || ret
== ERROR_BAD_FORMAT
) /* 32-bit module or unknown executable*/
1712 LOADPARAMS16 params
;
1715 showCmd
[1] = nCmdShow
;
1717 arglen
= strlen( lpCmdLine
);
1718 cmdline
= HeapAlloc( GetProcessHeap(), 0, arglen
+ 1 );
1719 cmdline
[0] = (BYTE
)arglen
;
1720 memcpy( cmdline
+ 1, lpCmdLine
, arglen
);
1722 params
.hEnvironment
= 0;
1723 params
.cmdLine
= MapLS( cmdline
);
1724 params
.showCmd
= MapLS( showCmd
);
1725 params
.reserved
= 0;
1727 ret
= LoadModule16( "winoldap.mod", ¶ms
);
1728 UnMapLS( params
.cmdLine
);
1729 UnMapLS( params
.showCmd
);
1734 /***********************************************************************
1735 * GetProcAddress (KERNEL.50)
1737 FARPROC16 WINAPI
GetProcAddress16( HMODULE16 hModule
, LPCSTR name
)
1742 if (!hModule
) hModule
= GetCurrentTask();
1743 hModule
= GetExePtr( hModule
);
1745 if (HIWORD(name
) != 0)
1747 ordinal
= NE_GetOrdinal( hModule
, name
);
1748 TRACE("%04x '%s'\n", hModule
, name
);
1752 ordinal
= LOWORD(name
);
1753 TRACE("%04x %04x\n", hModule
, ordinal
);
1755 if (!ordinal
) return (FARPROC16
)0;
1757 ret
= NE_GetEntryPoint( hModule
, ordinal
);
1759 TRACE("returning %p\n", ret
);
1764 /***************************************************************************
1765 * HasGPHandler (KERNEL.338)
1767 SEGPTR WINAPI
HasGPHandler16( SEGPTR address
)
1772 GPHANDLERDEF
*gpHandler
;
1774 if ( (hModule
= FarGetOwner16( SELECTOROF(address
) )) != 0
1775 && (gpOrdinal
= NE_GetOrdinal( hModule
, "__GP" )) != 0
1776 && (gpPtr
= (SEGPTR
)NE_GetEntryPointEx( hModule
, gpOrdinal
, FALSE
)) != 0
1777 && !IsBadReadPtr16( gpPtr
, sizeof(GPHANDLERDEF
) )
1778 && (gpHandler
= MapSL( gpPtr
)) != NULL
)
1780 while (gpHandler
->selector
)
1782 if ( SELECTOROF(address
) == gpHandler
->selector
1783 && OFFSETOF(address
) >= gpHandler
->rangeStart
1784 && OFFSETOF(address
) < gpHandler
->rangeEnd
)
1785 return MAKESEGPTR( gpHandler
->selector
, gpHandler
->handler
);
1794 /**********************************************************************
1795 * GetModuleHandle (KERNEL.47)
1797 * Find a module from a module name.
1799 * NOTE: The current implementation works the same way the Windows 95 one
1800 * does. Do not try to 'fix' it, fix the callers.
1801 * + It does not do ANY extension handling (except that strange .EXE bit)!
1802 * + It does not care about paths, just about basenames. (same as Windows)
1806 * the win16 module handle if found
1808 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1809 * Always hFirstModule
1811 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1813 if (HIWORD(name
) == 0)
1814 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1815 return MAKELONG(GetModuleHandle16( MapSL(name
)), hFirstModule
);
1818 /**********************************************************************
1819 * NE_GetModuleByFilename
1821 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
)
1825 BYTE len
, *name_table
;
1826 char tmpstr
[MAX_PATH
];
1829 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1831 /* If the base filename of 'name' matches the base filename of the module
1832 * filename of some module (case-insensitive compare):
1833 * Return its handle.
1836 /* basename: search backwards in passed name to \ / or : */
1837 s
= tmpstr
+ strlen(tmpstr
);
1840 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1845 /* search this in loaded filename list */
1846 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1851 pModule
= NE_GetPtr( hModule
);
1852 if (!pModule
) break;
1853 if (!pModule
->fileinfo
) continue;
1854 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1856 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1857 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1858 /* basename: search backwards in pathname to \ / or : */
1859 while (loadedfn
> (char*)ofs
->szPathName
)
1861 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1865 /* case insensitive compare ... */
1866 if (!NE_strcasecmp(loadedfn
, s
))
1869 /* If basename (without ext) matches the module name of a module:
1870 * Return its handle.
1873 if ( (p
= strrchr( s
, '.' )) != NULL
) *p
= '\0';
1876 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1878 pModule
= NE_GetPtr( hModule
);
1879 if (!pModule
) break;
1880 if (pModule
->ne_flags
& NE_FFLAGS_WIN32
) continue;
1882 name_table
= (BYTE
*)pModule
+ pModule
->ne_restab
;
1883 if ((*name_table
== len
) && !NE_strncasecmp(s
, (const char*)name_table
+1, len
))
1890 /***********************************************************************
1891 * GetProcAddress16 (KERNEL32.37)
1892 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1894 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1896 if (!hModule
) return 0;
1897 if (HIWORD(hModule
))
1899 WARN("hModule is Win32 handle (%p)\n", hModule
);
1902 return GetProcAddress16( LOWORD(hModule
), name
);
1905 /**********************************************************************
1906 * ModuleFirst (TOOLHELP.59)
1908 BOOL16 WINAPI
ModuleFirst16( MODULEENTRY
*lpme
)
1910 lpme
->wNext
= hFirstModule
;
1911 return ModuleNext16( lpme
);
1915 /**********************************************************************
1916 * ModuleNext (TOOLHELP.60)
1918 BOOL16 WINAPI
ModuleNext16( MODULEENTRY
*lpme
)
1923 if (!lpme
->wNext
) return FALSE
;
1924 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1925 name
= (char *)pModule
+ pModule
->ne_restab
;
1926 memcpy( lpme
->szModule
, name
+ 1, min(*name
, MAX_MODULE_NAME
) );
1927 lpme
->szModule
[min(*name
, MAX_MODULE_NAME
)] = '\0';
1928 lpme
->hModule
= lpme
->wNext
;
1929 lpme
->wcUsage
= pModule
->count
;
1930 lstrcpynA( lpme
->szExePath
, NE_MODULE_NAME(pModule
), sizeof(lpme
->szExePath
) );
1931 lpme
->wNext
= pModule
->next
;
1936 /**********************************************************************
1937 * ModuleFindName (TOOLHELP.61)
1939 BOOL16 WINAPI
ModuleFindName16( MODULEENTRY
*lpme
, LPCSTR name
)
1941 lpme
->wNext
= GetModuleHandle16( name
);
1942 return ModuleNext16( lpme
);
1946 /**********************************************************************
1947 * ModuleFindHandle (TOOLHELP.62)
1949 BOOL16 WINAPI
ModuleFindHandle16( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1951 hModule
= GetExePtr( hModule
);
1952 lpme
->wNext
= hModule
;
1953 return ModuleNext16( lpme
);
1957 /***************************************************************************
1958 * IsRomModule (KERNEL.323)
1960 BOOL16 WINAPI
IsRomModule16( HMODULE16 unused
)
1965 /***************************************************************************
1966 * IsRomFile (KERNEL.326)
1968 BOOL16 WINAPI
IsRomFile16( HFILE16 unused
)
1973 /***********************************************************************
1974 * create_dummy_module
1976 * Create a dummy NE module for Win32 or Winelib.
1978 static HMODULE16
create_dummy_module( HMODULE module32
)
1982 SEGTABLEENTRY
*pSegment
;
1985 const char* basename
;
1988 char filename
[MAX_PATH
];
1989 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module32
);
1991 if (!nt
) return ERROR_BAD_FORMAT
;
1993 /* Extract base filename */
1994 len
= GetModuleFileNameA( module32
, filename
, sizeof(filename
) );
1995 if (!len
|| len
>= sizeof(filename
)) return ERROR_BAD_FORMAT
;
1996 basename
= strrchr(filename
, '\\');
1997 if (!basename
) basename
= filename
;
1999 len
= strlen(basename
);
2000 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
2002 /* Allocate module */
2003 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
2004 + strlen(filename
) + 1;
2005 size
= sizeof(NE_MODULE
) +
2006 /* loaded file info */
2007 ((of_size
+ 3) & ~3) +
2008 /* segment table: DS,CS */
2009 2 * sizeof(SEGTABLEENTRY
) +
2012 /* several empty tables */
2015 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
2016 if (!hModule
) return ERROR_BAD_FORMAT
;
2018 FarSetOwner16( hModule
, hModule
);
2019 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
2021 /* Set all used entries */
2022 pModule
->ne_magic
= IMAGE_OS2_SIGNATURE
;
2025 pModule
->ne_flags
= NE_FFLAGS_WIN32
;
2026 pModule
->ne_autodata
= 0;
2027 pModule
->ne_sssp
= MAKESEGPTR( 0, 1 );
2028 pModule
->ne_csip
= MAKESEGPTR( 0, 2 );
2029 pModule
->ne_heap
= 0;
2030 pModule
->ne_stack
= 0;
2031 pModule
->ne_cseg
= 2;
2032 pModule
->ne_cmod
= 0;
2033 pModule
->ne_cbnrestab
= 0;
2034 pModule
->fileinfo
= sizeof(NE_MODULE
);
2035 pModule
->ne_exetyp
= NE_OSFLAGS_WINDOWS
;
2036 pModule
->self
= hModule
;
2037 pModule
->module32
= module32
;
2039 /* Set version and flags */
2040 pModule
->ne_expver
= ((nt
->OptionalHeader
.MajorSubsystemVersion
& 0xff) << 8 ) |
2041 (nt
->OptionalHeader
.MinorSubsystemVersion
& 0xff);
2042 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
2043 pModule
->ne_flags
|= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
;
2045 /* Set loaded file information */
2046 ofs
= (OFSTRUCT
*)(pModule
+ 1);
2047 memset( ofs
, 0, of_size
);
2048 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
2049 strcpy( ofs
->szPathName
, filename
);
2051 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + ((of_size
+ 3) & ~3));
2052 pModule
->ne_segtab
= (char *)pSegment
- (char *)pModule
;
2055 pSegment
->flags
= NE_SEGFLAGS_DATA
;
2056 pSegment
->minsize
= 0x1000;
2059 pSegment
->flags
= 0;
2063 pStr
= (char *)pSegment
;
2064 pModule
->ne_restab
= pStr
- (char *)pModule
;
2067 lstrcpynA( pStr
+1, basename
, len
+1 );
2070 /* All tables zero terminated */
2071 pModule
->ne_rsrctab
= pModule
->ne_imptab
= pModule
->ne_enttab
= pStr
- (char *)pModule
;
2073 NE_RegisterModule( pModule
);
2074 pModule
->owner32
= LoadLibraryA( filename
); /* increment the ref count of the 32-bit module */
2078 /***********************************************************************
2079 * PrivateLoadLibrary (KERNEL32.@)
2081 * FIXME: rough guesswork, don't know what "Private" means
2083 HINSTANCE16 WINAPI
PrivateLoadLibrary(LPCSTR libname
)
2085 return LoadLibrary16(libname
);
2088 /***********************************************************************
2089 * PrivateFreeLibrary (KERNEL32.@)
2091 * FIXME: rough guesswork, don't know what "Private" means
2093 void WINAPI
PrivateFreeLibrary(HINSTANCE16 handle
)
2095 FreeLibrary16(handle
);
2098 /***********************************************************************
2099 * LoadLibrary32 (KERNEL.452)
2100 * LoadSystemLibrary32 (KERNEL.482)
2102 HMODULE WINAPI
LoadLibrary32_16( LPCSTR libname
)
2107 ReleaseThunkLock( &count
);
2108 hModule
= LoadLibraryA( libname
);
2109 RestoreThunkLock( count
);
2113 /***************************************************************************
2114 * MapHModuleLS (KERNEL32.@)
2116 HMODULE16 WINAPI
MapHModuleLS(HMODULE hmod
)
2122 return TASK_GetCurrent()->hInstance
;
2124 return LOWORD(hmod
); /* we already have a 16 bit module handle */
2125 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
2127 if (pModule
->module32
== hmod
)
2128 return pModule
->self
;
2129 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
2131 if ((ret
= create_dummy_module( hmod
)) < 32)
2139 /***************************************************************************
2140 * MapHModuleSL (KERNEL32.@)
2142 HMODULE WINAPI
MapHModuleSL(HMODULE16 hmod
)
2147 TDB
*pTask
= TASK_GetCurrent();
2148 hmod
= pTask
->hModule
;
2150 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
2151 if ((pModule
->ne_magic
!= IMAGE_OS2_SIGNATURE
) || !(pModule
->ne_flags
& NE_FFLAGS_WIN32
))
2153 return pModule
->module32
;
2156 /***************************************************************************
2157 * MapHInstLS (KERNEL.472)
2159 void WINAPI
MapHInstLS16( CONTEXT86
*context
)
2161 context
->Eax
= MapHModuleLS( (HMODULE
)context
->Eax
);
2164 /***************************************************************************
2165 * MapHInstSL (KERNEL.473)
2167 void WINAPI
MapHInstSL16( CONTEXT86
*context
)
2169 context
->Eax
= (DWORD
)MapHModuleSL( context
->Eax
);
2174 /***************************************************************************
2175 * MapHInstLS (KERNEL32.@)
2177 __ASM_GLOBAL_FUNC( MapHInstLS
,
2179 "call " __ASM_NAME("MapHModuleLS") "\n\t"
2182 /***************************************************************************
2183 * MapHInstSL (KERNEL32.@)
2185 __ASM_GLOBAL_FUNC( MapHInstSL
,
2187 "call " __ASM_NAME("MapHModuleSL") "\n\t"
2190 /***************************************************************************
2191 * MapHInstLS_PN (KERNEL32.@)
2193 __ASM_GLOBAL_FUNC( MapHInstLS_PN
,
2194 "testl %eax,%eax\n\t"
2197 "call " __ASM_NAME("MapHModuleLS") "\n"
2200 /***************************************************************************
2201 * MapHInstSL_PN (KERNEL32.@)
2203 __ASM_GLOBAL_FUNC( MapHInstSL_PN
,
2204 "andl $0xffff,%eax\n\t"
2207 "call " __ASM_NAME("MapHModuleSL") "\n"
2210 #endif /* __i386__ */