4 * Copyright 1995 Alexandre Julliard
13 #include "wine/winbase16.h"
28 #include "builtin16.h"
29 #include "stackframe.h"
30 #include "debugtools.h"
32 #include "loadorder.h"
36 DEFAULT_DEBUG_CHANNEL(module
)
38 #define hFirstModule (pThhook->hExeHead)
40 static NE_MODULE
*pCachedModule
= 0; /* Module cached by NE_OpenFile */
42 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
);
44 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
);
46 /***********************************************************************
49 NE_MODULE
*NE_GetPtr( HMODULE16 hModule
)
51 return (NE_MODULE
*)GlobalLock16( GetExePtr(hModule
) );
55 /***********************************************************************
58 void NE_DumpModule( HMODULE16 hModule
)
68 if (!(pModule
= NE_GetPtr( hModule
)))
70 MESSAGE( "**** %04x is not a module handle\n", hModule
);
74 /* Dump the module info */
76 DPRINTF( "Module %04x:\n", hModule
);
77 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
78 pModule
->count
, pModule
->flags
,
79 pModule
->heap_size
, pModule
->stack_size
);
80 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
81 pModule
->cs
, pModule
->ip
, pModule
->ss
, pModule
->sp
, pModule
->dgroup
,
82 pModule
->seg_count
, pModule
->modref_count
);
83 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
84 pModule
->os_flags
, pModule
->min_swap_area
,
85 pModule
->expected_version
);
86 if (pModule
->flags
& NE_FFLAGS_WIN32
)
87 DPRINTF( "PE module=%08x\n", pModule
->module32
);
89 /* Dump the file info */
91 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
93 /* Dump the segment table */
95 DPRINTF( "Segment table:\n" );
96 pSeg
= NE_SEG_TABLE( pModule
);
97 for (i
= 0; i
< pModule
->seg_count
; i
++, pSeg
++)
98 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
99 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
100 pSeg
->minsize
, pSeg
->hSeg
);
102 /* Dump the resource table */
104 DPRINTF( "Resource table:\n" );
105 if (pModule
->res_table
)
107 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->res_table
);
108 DPRINTF( "Alignment: %d\n", *pword
++ );
111 struct resource_typeinfo_s
*ptr
= (struct resource_typeinfo_s
*)pword
;
112 struct resource_nameinfo_s
*pname
= (struct resource_nameinfo_s
*)(ptr
+ 1);
113 DPRINTF( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
114 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
115 DPRINTF( "offset=%d len=%d id=%04x\n",
116 pname
->offset
, pname
->length
, pname
->id
);
117 pword
= (WORD
*)pname
;
120 else DPRINTF( "None\n" );
122 /* Dump the resident name table */
124 DPRINTF( "Resident-name table:\n" );
125 pstr
= (char *)pModule
+ pModule
->name_table
;
128 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
129 *(WORD
*)(pstr
+ *pstr
+ 1) );
130 pstr
+= *pstr
+ 1 + sizeof(WORD
);
133 /* Dump the module reference table */
135 DPRINTF( "Module ref table:\n" );
136 if (pModule
->modref_table
)
138 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->modref_table
);
139 for (i
= 0; i
< pModule
->modref_count
; i
++, pword
++)
142 GetModuleName16( *pword
, name
, sizeof(name
) );
143 DPRINTF( "%d: %04x -> '%s'\n", i
, *pword
, name
);
146 else DPRINTF( "None\n" );
148 /* Dump the entry table */
150 DPRINTF( "Entry table:\n" );
151 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+pModule
->entry_table
);
153 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
154 DPRINTF( "Bundle %d-%d: %02x\n", bundle
->first
, bundle
->last
, entry
->type
);
155 ordinal
= bundle
->first
;
156 while (ordinal
< bundle
->last
)
158 if (entry
->type
== 0xff)
159 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal
++, entry
->segnum
, entry
->offs
);
161 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal
++, entry
->segnum
, entry
->offs
);
164 } while ( (bundle
->next
)
165 && (bundle
= ((ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
))) );
167 /* Dump the non-resident names table */
169 DPRINTF( "Non-resident names table:\n" );
170 if (pModule
->nrname_handle
)
172 pstr
= (char *)GlobalLock16( pModule
->nrname_handle
);
175 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
176 *(WORD
*)(pstr
+ *pstr
+ 1) );
177 pstr
+= *pstr
+ 1 + sizeof(WORD
);
184 /***********************************************************************
187 * Walk the module list and print the modules.
189 void NE_WalkModules(void)
191 HMODULE16 hModule
= hFirstModule
;
192 MESSAGE( "Module Flags Name\n" );
195 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
198 MESSAGE( "Bad module %04x in list\n", hModule
);
201 MESSAGE( " %04x %04x %.*s\n", hModule
, pModule
->flags
,
202 *((char *)pModule
+ pModule
->name_table
),
203 (char *)pModule
+ pModule
->name_table
+ 1 );
204 hModule
= pModule
->next
;
209 /**********************************************************************
212 void NE_RegisterModule( NE_MODULE
*pModule
)
214 pModule
->next
= hFirstModule
;
215 hFirstModule
= pModule
->self
;
219 /***********************************************************************
222 * Lookup the ordinal for a given name.
224 WORD
NE_GetOrdinal( HMODULE16 hModule
, const char *name
)
226 unsigned char buffer
[256], *cpnt
;
230 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
231 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
233 TRACE("(%04x,'%s')\n", hModule
, name
);
235 /* First handle names of the form '#xxxx' */
237 if (name
[0] == '#') return atoi( name
+ 1 );
239 /* Now copy and uppercase the string */
241 strcpy( buffer
, name
);
242 CharUpperA( buffer
);
243 len
= strlen( buffer
);
245 /* First search the resident names */
247 cpnt
= (char *)pModule
+ pModule
->name_table
;
249 /* Skip the first entry (module name) */
250 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
253 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
255 TRACE(" Found: ordinal=%d\n",
256 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
257 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
259 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
262 /* Now search the non-resident names table */
264 if (!pModule
->nrname_handle
) return 0; /* No non-resident table */
265 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
267 /* Skip the first entry (module description string) */
268 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
271 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
273 TRACE(" Found: ordinal=%d\n",
274 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
275 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
277 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
283 /***********************************************************************
284 * NE_GetEntryPoint (WPROCS.27)
286 * Return the entry point for a given ordinal.
288 FARPROC16 WINAPI
WIN16_NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
290 FARPROC16 ret
= NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
291 CURRENT_STACK16
->ecx
= hModule
; /* FIXME: might be incorrect value */
294 FARPROC16 WINAPI
NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
296 return NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
298 FARPROC16
NE_GetEntryPointEx( HMODULE16 hModule
, WORD ordinal
, BOOL16 snoop
)
306 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
307 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
309 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
310 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
314 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
317 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
318 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
322 offset
= entry
->offs
;
324 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
325 else sel
= GlobalHandleToSel16(NE_SEG_TABLE(pModule
)[sel
-1].hSeg
);
327 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
329 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
331 return (FARPROC16
)SNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
));
335 /***********************************************************************
338 * Change the value of an entry point. Use with caution!
339 * It can only change the offset value, not the selector.
341 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
348 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
349 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
351 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
352 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
354 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
359 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
360 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
363 entry
->offs
= offset
;
368 /***********************************************************************
371 HANDLE
NE_OpenFile( NE_MODULE
*pModule
)
375 static HANDLE cachedfd
= -1;
377 TRACE("(%p) cache: mod=%p fd=%d\n",
378 pModule
, pCachedModule
, cachedfd
);
379 if (pCachedModule
== pModule
) return cachedfd
;
380 CloseHandle( cachedfd
);
381 pCachedModule
= pModule
;
382 name
= NE_MODULE_NAME( pModule
);
383 if ((cachedfd
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
384 NULL
, OPEN_EXISTING
, 0, -1 )) == -1)
385 MESSAGE( "Can't open file '%s' for module %04x\n", name
, pModule
->self
);
387 /* FIXME: should not be necessary */
388 cachedfd
= ConvertToGlobalHandle(cachedfd
);
389 TRACE("opened '%s' -> %d\n",
395 /***********************************************************************
398 static HMODULE16
NE_LoadExeHeader( HFILE16 hFile
, OFSTRUCT
*ofs
)
400 IMAGE_DOS_HEADER mz_header
;
401 IMAGE_OS2_HEADER ne_header
;
405 BYTE
*pData
, *pTempEntryTable
;
406 char *buffer
, *fastload
= NULL
;
407 int fastload_offset
= 0, fastload_length
= 0;
409 ET_BUNDLE
*bundle
, *oldbundle
;
411 /* Read a block from either the file or the fast-load area. */
412 #define READ(offset,size,buffer) \
413 ((fastload && ((offset) >= fastload_offset) && \
414 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
415 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
416 (_llseek16( hFile, (offset), SEEK_SET), \
417 _hread16( hFile, (buffer), (size) ) == (size)))
419 _llseek16( hFile
, 0, SEEK_SET
);
420 if ((_hread16(hFile
,&mz_header
,sizeof(mz_header
)) != sizeof(mz_header
)) ||
421 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
422 return (HMODULE16
)11; /* invalid exe */
424 _llseek16( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
425 if (_hread16( hFile
, &ne_header
, sizeof(ne_header
) ) != sizeof(ne_header
))
426 return (HMODULE16
)11; /* invalid exe */
428 if (ne_header
.ne_magic
== IMAGE_NT_SIGNATURE
) return (HMODULE16
)21; /* win32 exe */
429 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return (HMODULE16
)11; /* invalid exe */
431 if (ne_header
.ne_magic
== IMAGE_OS2_SIGNATURE_LX
) {
432 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
433 return (HMODULE16
)12;
436 /* We now have a valid NE header */
438 size
= sizeof(NE_MODULE
) +
440 ne_header
.n_segment_tab
* sizeof(SEGTABLEENTRY
) +
442 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
+
443 /* resident names table */
444 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
+
445 /* module ref table */
446 ne_header
.n_mod_ref_tab
* sizeof(WORD
) +
447 /* imported names table */
448 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
+
449 /* entry table length */
450 ne_header
.entry_tab_length
+
451 /* entry table extra conversion space */
453 2 * (ne_header
.entry_tab_length
- ne_header
.n_mov_entry_points
*6) +
454 /* loaded file info */
455 sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
457 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
458 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
459 FarSetOwner16( hModule
, hModule
);
460 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
461 memcpy( pModule
, &ne_header
, sizeof(ne_header
) );
463 /* check *programs* for default minimal stack size */
464 if ( (!(pModule
->flags
& NE_FFLAGS_LIBMODULE
))
465 && (pModule
->stack_size
< 0x1400) )
466 pModule
->stack_size
= 0x1400;
467 pModule
->module32
= 0;
468 pModule
->self
= hModule
;
469 pModule
->self_loading_sel
= 0;
470 pData
= (BYTE
*)(pModule
+ 1);
472 /* Clear internal Wine flags in case they are set in the EXE file */
474 pModule
->flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
476 /* Read the fast-load area */
478 if (ne_header
.additional_flags
& NE_AFLAGS_FASTLOAD
)
480 fastload_offset
=ne_header
.fastload_offset
<<ne_header
.align_shift_count
;
481 fastload_length
=ne_header
.fastload_length
<<ne_header
.align_shift_count
;
482 TRACE("Using fast-load area offset=%x len=%d\n",
483 fastload_offset
, fastload_length
);
484 if ((fastload
= HeapAlloc( SystemHeap
, 0, fastload_length
)) != NULL
)
486 _llseek16( hFile
, fastload_offset
, SEEK_SET
);
487 if (_hread16(hFile
, fastload
, fastload_length
) != fastload_length
)
489 HeapFree( SystemHeap
, 0, fastload
);
490 WARN("Error reading fast-load area!\n");
496 /* Get the segment table */
498 pModule
->seg_table
= (int)pData
- (int)pModule
;
499 buffer
= HeapAlloc( SystemHeap
, 0, ne_header
.n_segment_tab
*
500 sizeof(struct ne_segment_table_entry_s
));
504 struct ne_segment_table_entry_s
*pSeg
;
506 if (!READ( mz_header
.e_lfanew
+ ne_header
.segment_tab_offset
,
507 ne_header
.n_segment_tab
* sizeof(struct ne_segment_table_entry_s
),
510 HeapFree( SystemHeap
, 0, buffer
);
512 HeapFree( SystemHeap
, 0, fastload
);
513 GlobalFree16( hModule
);
514 return (HMODULE16
)11; /* invalid exe */
516 pSeg
= (struct ne_segment_table_entry_s
*)buffer
;
517 for (i
= ne_header
.n_segment_tab
; i
> 0; i
--, pSeg
++)
519 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
520 pData
+= sizeof(SEGTABLEENTRY
);
522 HeapFree( SystemHeap
, 0, buffer
);
527 HeapFree( SystemHeap
, 0, fastload
);
528 GlobalFree16( hModule
);
529 return (HMODULE16
)11; /* invalid exe */
532 /* Get the resource table */
534 if (ne_header
.resource_tab_offset
< ne_header
.rname_tab_offset
)
536 pModule
->res_table
= (int)pData
- (int)pModule
;
537 if (!READ(mz_header
.e_lfanew
+ ne_header
.resource_tab_offset
,
538 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
,
539 pData
)) return (HMODULE16
)11; /* invalid exe */
540 pData
+= ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
;
541 NE_InitResourceHandler( hModule
);
543 else pModule
->res_table
= 0; /* No resource table */
545 /* Get the resident names table */
547 pModule
->name_table
= (int)pData
- (int)pModule
;
548 if (!READ( mz_header
.e_lfanew
+ ne_header
.rname_tab_offset
,
549 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
,
553 HeapFree( SystemHeap
, 0, fastload
);
554 GlobalFree16( hModule
);
555 return (HMODULE16
)11; /* invalid exe */
557 pData
+= ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
;
559 /* Get the module references table */
561 if (ne_header
.n_mod_ref_tab
> 0)
563 pModule
->modref_table
= (int)pData
- (int)pModule
;
564 if (!READ( mz_header
.e_lfanew
+ ne_header
.moduleref_tab_offset
,
565 ne_header
.n_mod_ref_tab
* sizeof(WORD
),
569 HeapFree( SystemHeap
, 0, fastload
);
570 GlobalFree16( hModule
);
571 return (HMODULE16
)11; /* invalid exe */
573 pData
+= ne_header
.n_mod_ref_tab
* sizeof(WORD
);
575 else pModule
->modref_table
= 0; /* No module references */
577 /* Get the imported names table */
579 pModule
->import_table
= (int)pData
- (int)pModule
;
580 if (!READ( mz_header
.e_lfanew
+ ne_header
.iname_tab_offset
,
581 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
,
585 HeapFree( SystemHeap
, 0, fastload
);
586 GlobalFree16( hModule
);
587 return (HMODULE16
)11; /* invalid exe */
589 pData
+= ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
;
591 /* Load entry table, convert it to the optimized version used by Windows */
593 if ((pTempEntryTable
= HeapAlloc( SystemHeap
, 0, ne_header
.entry_tab_length
)) != NULL
)
595 BYTE nr_entries
, type
, *s
;
597 TRACE("Converting entry table.\n");
598 pModule
->entry_table
= (int)pData
- (int)pModule
;
599 if (!READ( mz_header
.e_lfanew
+ ne_header
.entry_tab_offset
,
600 ne_header
.entry_tab_length
, pTempEntryTable
))
602 HeapFree( SystemHeap
, 0, pTempEntryTable
);
604 HeapFree( SystemHeap
, 0, fastload
);
605 GlobalFree16( hModule
);
606 return (HMODULE16
)11; /* invalid exe */
610 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header
.entry_tab_offset
, ne_header
.entry_tab_length
, *s
);
612 bundle
= (ET_BUNDLE
*)pData
;
613 TRACE("first bundle: %p\n", bundle
);
614 memset(bundle
, 0, sizeof(ET_BUNDLE
)); /* in case no entry table exists */
615 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
617 while ((nr_entries
= *s
++))
621 bundle
->last
+= nr_entries
;
628 entry
->segnum
= *s
++;
629 entry
->offs
= *(WORD
*)s
; s
+= 2;
630 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
638 entry
->segnum
= type
;
639 entry
->offs
= *(WORD
*)s
; s
+= 2;
640 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
646 if (bundle
->first
== bundle
->last
)
648 bundle
->first
+= nr_entries
;
649 bundle
->last
+= nr_entries
;
654 oldbundle
->next
= ((int)entry
- (int)pModule
);
655 bundle
= (ET_BUNDLE
*)entry
;
656 TRACE("new bundle: %p\n", bundle
);
657 bundle
->first
= bundle
->last
=
658 oldbundle
->last
+ nr_entries
;
660 (BYTE
*)entry
+= sizeof(ET_BUNDLE
);
664 HeapFree( SystemHeap
, 0, pTempEntryTable
);
669 HeapFree( SystemHeap
, 0, fastload
);
670 GlobalFree16( hModule
);
671 return (HMODULE16
)11; /* invalid exe */
674 pData
+= ne_header
.entry_tab_length
+ sizeof(ET_BUNDLE
) +
675 2 * (ne_header
.entry_tab_length
- ne_header
.n_mov_entry_points
*6);
677 if ((DWORD
)entry
> (DWORD
)pData
)
678 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry
, pData
);
680 /* Store the filename information */
682 pModule
->fileinfo
= (int)pData
- (int)pModule
;
683 size
= sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
684 memcpy( pData
, ofs
, size
);
685 ((OFSTRUCT
*)pData
)->cBytes
= size
- 1;
688 /* Free the fast-load area */
692 HeapFree( SystemHeap
, 0, fastload
);
694 /* Get the non-resident names table */
696 if (ne_header
.nrname_tab_length
)
698 pModule
->nrname_handle
= GLOBAL_Alloc( 0, ne_header
.nrname_tab_length
,
699 hModule
, FALSE
, FALSE
, FALSE
);
700 if (!pModule
->nrname_handle
)
702 GlobalFree16( hModule
);
703 return (HMODULE16
)11; /* invalid exe */
705 buffer
= GlobalLock16( pModule
->nrname_handle
);
706 _llseek16( hFile
, ne_header
.nrname_tab_offset
, SEEK_SET
);
707 if (_hread16( hFile
, buffer
, ne_header
.nrname_tab_length
)
708 != ne_header
.nrname_tab_length
)
710 GlobalFree16( pModule
->nrname_handle
);
711 GlobalFree16( hModule
);
712 return (HMODULE16
)11; /* invalid exe */
715 else pModule
->nrname_handle
= 0;
717 /* Allocate a segment for the implicitly-loaded DLLs */
719 if (pModule
->modref_count
)
721 pModule
->dlls_to_init
= GLOBAL_Alloc(GMEM_ZEROINIT
,
722 (pModule
->modref_count
+1)*sizeof(HMODULE16
),
723 hModule
, FALSE
, FALSE
, FALSE
);
724 if (!pModule
->dlls_to_init
)
726 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
727 GlobalFree16( hModule
);
728 return (HMODULE16
)11; /* invalid exe */
731 else pModule
->dlls_to_init
= 0;
733 NE_RegisterModule( pModule
);
734 SNOOP16_RegisterDLL(pModule
,ofs
->szPathName
);
740 /***********************************************************************
743 * Load all DLLs implicitly linked to a module.
745 static BOOL
NE_LoadDLLs( NE_MODULE
*pModule
)
748 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->modref_table
);
749 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
751 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
754 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->import_table
+ *pModRef
;
755 memcpy( buffer
, pstr
+ 1, *pstr
);
756 *(buffer
+ *pstr
) = 0; /* terminate it */
758 TRACE("Loading '%s'\n", buffer
);
759 if (!(*pModRef
= GetModuleHandle16( buffer
)))
761 /* If the DLL is not loaded yet, load it and store */
762 /* its handle in the list of DLLs to initialize. */
765 if ((hDLL
= MODULE_LoadModule16( buffer
, TRUE
, TRUE
)) < 32)
767 /* FIXME: cleanup what was done */
769 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
770 buffer
, *((BYTE
*)pModule
+ pModule
->name_table
),
771 (char *)pModule
+ pModule
->name_table
+ 1, hDLL
);
774 *pModRef
= GetExePtr( hDLL
);
777 else /* Increment the reference count of the DLL */
781 pOldDLL
= NE_GetPtr( *pModRef
);
782 if (pOldDLL
) pOldDLL
->count
++;
789 /**********************************************************************
792 * Load first instance of NE module from file.
794 * pModule must point to a module structure prepared by NE_LoadExeHeader.
795 * This routine must never be called twice on a module.
798 static HINSTANCE16
NE_DoLoadModule( NE_MODULE
*pModule
)
800 HINSTANCE16 hInstance
;
802 /* Allocate the segments for this module */
804 if (!NE_CreateSegments( pModule
) ||
805 !(hInstance
= NE_CreateInstance( pModule
, NULL
, FALSE
)))
806 return 8; /* Insufficient memory */
808 /* Load the referenced DLLs */
810 if (!NE_LoadDLLs( pModule
))
813 /* Load the segments */
815 NE_LoadAllSegments( pModule
);
817 /* Fixup the functions prologs */
819 NE_FixupPrologs( pModule
);
821 /* Make sure the usage count is 1 on the first loading of */
822 /* the module, even if it contains circular DLL references */
829 /**********************************************************************
832 * Load first instance of NE module. (Note: caller is responsible for
833 * ensuring the module isn't already loaded!)
835 * If the module turns out to be an executable module, only a
836 * handle to a module stub is returned; this needs to be initialized
837 * by calling NE_DoLoadModule later, in the context of the newly
840 * If lib_only is TRUE, however, the module is perforce treated
841 * like a DLL module, even if it is an executable module.
844 HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL implicit
, BOOL lib_only
)
848 HINSTANCE16 hInstance
;
852 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
858 /* 4 == strlen(".dll") */
859 strncpy(buffer
, name
, sizeof(buffer
) - 1 - 4);
860 strcat(buffer
, ".dll");
861 if ((hFile
= OpenFile16( buffer
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
862 return 2; /* File not found */
866 hModule
= NE_LoadExeHeader( hFile
, &ofs
);
869 if (hModule
< 32) return hModule
;
870 pModule
= NE_GetPtr( hModule
);
871 if ( !pModule
) return hModule
;
873 if ( !lib_only
&& !( pModule
->flags
& NE_FFLAGS_LIBMODULE
) )
876 hInstance
= NE_DoLoadModule( pModule
);
877 if ( hInstance
< 32 )
880 NE_FreeModule( hModule
, 0 );
887 /**********************************************************************
888 * MODULE_LoadModule16
890 * Load a NE module in the order of the loadorder specification.
891 * The caller is responsible that the module is not loaded already.
894 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
)
898 module_loadorder_t
*plo
;
900 plo
= MODULE_GetLoadOrder(libname
);
902 for(i
= 0; i
< MODULE_LOADORDER_NTYPES
; i
++)
904 switch(plo
->loadorder
[i
])
906 case MODULE_LOADORDER_DLL
:
907 TRACE("Trying native dll '%s'\n", libname
);
908 hinst
= NE_LoadModule(libname
, implicit
, lib_only
);
911 case MODULE_LOADORDER_ELFDLL
:
912 TRACE("Trying elfdll '%s'\n", libname
);
913 hinst
= ELFDLL_LoadModule16(libname
, implicit
);
916 case MODULE_LOADORDER_BI
:
917 TRACE("Trying built-in '%s'\n", libname
);
918 hinst
= BUILTIN_LoadModule(libname
, TRUE
);
922 ERR("Got invalid loadorder type %d (%s index %d)\n", plo
->loadorder
[i
], plo
->modulename
, i
);
925 case MODULE_LOADORDER_SO
: /* This is not supported for NE modules */
926 case MODULE_LOADORDER_INVALID
: /* We ignore this as it is an empty entry */
938 hModule
= GetModuleHandle16(libname
);
941 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle\n",
943 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
946 pModule
= NE_GetPtr(hModule
);
949 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
951 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
954 TRACE("Loaded module '%s' at 0x%04x, \n", libname
, hinst
);
957 * Call initialization routines for all loaded DLLs. Note that
958 * when we load implicitly linked DLLs this will be done by InitTask().
960 if(pModule
->flags
& NE_FFLAGS_LIBMODULE
)
961 NE_InitializeDLLs(hModule
);
968 /* We quit searching when we get another error than 'File not found' */
972 return hinst
; /* The last error that occured */
976 /**********************************************************************
977 * LoadModule16 (KERNEL.45)
979 HINSTANCE16 WINAPI
LoadModule16( LPCSTR name
, LPVOID paramBlock
)
981 BOOL lib_only
= !paramBlock
|| (paramBlock
== (LPVOID
)-1);
982 LOADPARAMS16
*params
;
983 LPSTR cmd_line
, new_cmd_line
;
985 STARTUPINFOA startup
;
986 PROCESS_INFORMATION info
;
993 if ( (hModule
= GetModuleHandle16(name
) ) != 0 )
995 /* Special case: second instance of an already loaded NE module */
997 if ( !( pModule
= NE_GetPtr( hModule
) ) ) return (HINSTANCE16
)11;
998 if ( pModule
->module32
) return (HINSTANCE16
)21;
1000 /* Increment refcount */
1004 /* If library module, we just retrieve the instance handle */
1006 if ( ( pModule
->flags
& NE_FFLAGS_LIBMODULE
) || lib_only
)
1007 return NE_CreateInstance( pModule
, NULL
, TRUE
);
1011 /* Main case: load first instance of NE module */
1013 if ( (hModule
= MODULE_LoadModule16( name
, FALSE
, lib_only
)) < 32 )
1016 if ( !(pModule
= NE_GetPtr( hModule
)) )
1017 return (HINSTANCE16
)11;
1019 /* If library module, we're finished */
1021 if ( ( pModule
->flags
& NE_FFLAGS_LIBMODULE
) || lib_only
)
1027 * At this point, we need to create a new process.
1029 * pModule points either to an already loaded module, whose refcount
1030 * has already been incremented (to avoid having the module vanish
1031 * in the meantime), or else to a stub module which contains only header
1034 * All remaining initialization (really loading the module in the second
1035 * case, and creating the new instance in both cases) are to be done in
1036 * the context of the new process. This happens in the NE_InitProcess
1037 * routine, which will be called from the 32-bit process initialization.
1041 params
= (LOADPARAMS16
*)paramBlock
;
1042 cmd_line
= (LPSTR
)PTR_SEG_TO_LIN( params
->cmdLine
);
1043 if (!cmd_line
) cmd_line
= "";
1044 else if (*cmd_line
) cmd_line
++; /* skip the length byte */
1046 if (!(new_cmd_line
= HeapAlloc( GetProcessHeap(), 0,
1047 strlen(cmd_line
)+strlen(name
)+2 )))
1049 strcpy( new_cmd_line
, name
);
1050 strcat( new_cmd_line
, " " );
1051 strcat( new_cmd_line
, cmd_line
);
1053 if (params
->hEnvironment
) env
= GlobalLock16( params
->hEnvironment
);
1055 memset( &info
, '\0', sizeof(info
) );
1056 memset( &startup
, '\0', sizeof(startup
) );
1057 startup
.cb
= sizeof(startup
);
1058 if (params
->showCmd
)
1060 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
1061 startup
.wShowWindow
= ((UINT16
*)PTR_SEG_TO_LIN(params
->showCmd
))[1];
1064 SYSLEVEL_ReleaseWin16Lock();
1065 pdb
= PROCESS_Create( pModule
, new_cmd_line
, env
,
1066 NULL
, NULL
, TRUE
, 0, &startup
, &info
);
1067 SYSLEVEL_RestoreWin16Lock();
1069 CloseHandle( info
.hThread
);
1070 CloseHandle( info
.hProcess
);
1072 if (params
->hEnvironment
) GlobalUnlock16( params
->hEnvironment
);
1073 HeapFree( GetProcessHeap(), 0, new_cmd_line
);
1075 return GetProcessDword( info
.dwProcessId
, GPD_HINSTANCE16
);
1078 /**********************************************************************
1081 BOOL
NE_CreateProcess( HFILE hFile
, OFSTRUCT
*ofs
, LPCSTR cmd_line
, LPCSTR env
,
1082 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
1083 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
1084 LPPROCESS_INFORMATION info
)
1090 SYSLEVEL_EnterWin16Lock();
1092 /* Special case: second instance of an already loaded NE module */
1094 if ( ( hModule
= GetModuleHandle16( ofs
->szPathName
) ) != 0 )
1096 if ( !( pModule
= NE_GetPtr( hModule
) )
1097 || ( pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1098 || pModule
->module32
)
1100 SetLastError( ERROR_BAD_FORMAT
);
1107 /* Main case: load first instance of NE module */
1110 /* If we didn't get a file handle, return */
1112 if ( hFile
== HFILE_ERROR
)
1115 /* Allocate temporary HFILE16 for NE_LoadFileModule */
1117 if (!DuplicateHandle( GetCurrentProcess(), hFile
,
1118 GetCurrentProcess(), &hFile
,
1119 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1121 SetLastError( ERROR_INVALID_HANDLE
);
1124 hFile16
= FILE_AllocDosHandle( hFile
);
1128 hModule
= NE_LoadExeHeader( hFile16
, ofs
);
1129 _lclose16( hFile16
);
1133 SetLastError( hModule
);
1137 if ( !( pModule
= NE_GetPtr( hModule
) )
1138 || ( pModule
->flags
& NE_FFLAGS_LIBMODULE
) )
1140 GlobalFreeAll16( hModule
);
1141 SetLastError( ERROR_BAD_FORMAT
);
1146 SYSLEVEL_LeaveWin16Lock();
1148 if ( !PROCESS_Create( pModule
, cmd_line
, env
,
1149 psa
, tsa
, inherit
, flags
, startup
, info
) )
1155 SYSLEVEL_LeaveWin16Lock();
1159 /**********************************************************************
1162 BOOL
NE_InitProcess( NE_MODULE
*pModule
)
1164 HINSTANCE16 hInstance
, hPrevInstance
;
1167 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
1171 SYSLEVEL_EnterWin16Lock();
1173 if ( pModule
->count
> 0 )
1175 /* Second instance of an already loaded NE module */
1176 /* Note that the refcount was already incremented by the parent */
1178 hInstance
= NE_CreateInstance( pModule
, &hPrevInstance
, FALSE
);
1179 if ( hInstance
!= hPrevInstance
) /* not a library */
1180 NE_LoadSegment( pModule
, pModule
->dgroup
);
1184 /* Load first instance of NE module */
1186 pModule
->flags
|= NE_FFLAGS_GUI
; /* FIXME: is this necessary? */
1188 hInstance
= NE_DoLoadModule( pModule
);
1192 if ( hInstance
< 32 )
1194 SYSLEVEL_LeaveWin16Lock();
1196 SetLastError( hInstance
);
1200 /* Enter instance handles into task struct */
1202 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1203 pTask
->hInstance
= hInstance
;
1204 pTask
->hPrevInstance
= hPrevInstance
;
1206 /* Use DGROUP for 16-bit stack */
1208 if (!(sp
= pModule
->sp
))
1209 sp
= pSegTable
[pModule
->ss
-1].minsize
+ pModule
->stack_size
;
1210 sp
&= ~1; sp
-= sizeof(STACK16FRAME
);
1211 pTask
->teb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( hInstance
, sp
);
1213 SYSLEVEL_LeaveWin16Lock();
1218 /***********************************************************************
1219 * LoadLibrary16 (KERNEL.95)
1221 * In Win95 LoadLibrary16("c:/junkname/user.foo") returns the HINSTANCE
1222 * to user.exe. As GetModuleHandle as of 990425 explicitly asks _not_
1223 * to change its handling of extensions, we have to try a stripped down
1224 * libname here too (bon 990425)
1226 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
1228 char strippedname
[256];
1229 char *dirsep1
,*dirsep2
;
1232 dirsep1
=strrchr(libname
,'\\');
1233 dirsep2
=strrchr(libname
,'/');
1234 dirsep1
=MAX(dirsep1
,dirsep2
);
1236 dirsep1
=(LPSTR
)libname
;
1239 lstrcpynA(strippedname
,dirsep1
,256);
1240 dirsep1
=strchr(strippedname
,'.');
1244 TRACE("looking for (%p) %s and %s \n",
1245 libname
, libname
,strippedname
);
1247 /* Load library module */
1248 ret
= LoadModule16( strippedname
, (LPVOID
)-1 );
1249 if (ret
> HINSTANCE_ERROR
)
1251 return LoadModule16(libname
, (LPVOID
)-1 );
1255 /**********************************************************************
1258 * Call a DLL's WEP, allowing it to shut down.
1259 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1261 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
1263 FARPROC16 WEP
= (FARPROC16
)0;
1264 WORD ordinal
= NE_GetOrdinal( hModule
, "WEP" );
1266 if (ordinal
) WEP
= NE_GetEntryPoint( hModule
, ordinal
);
1269 WARN("module %04x doesn't have a WEP\n", hModule
);
1272 return Callbacks
->CallWindowsExitProc( WEP
, WEP_FREE_DLL
);
1276 /**********************************************************************
1279 * Implementation of FreeModule16().
1281 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
)
1283 HMODULE16
*hPrevModule
;
1288 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
1289 hModule
= pModule
->self
;
1291 TRACE("%04x count %d\n", hModule
, pModule
->count
);
1293 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
1294 else pModule
->count
= 0;
1296 if (pModule
->flags
& NE_FFLAGS_BUILTIN
)
1297 return FALSE
; /* Can't free built-in module */
1299 if (call_wep
&& !(pModule
->flags
& NE_FFLAGS_WIN32
))
1301 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1303 MODULE_CallWEP( hModule
);
1305 /* Free the objects owned by the DLL module */
1306 TASK_CallTaskSignalProc( USIG16_DLL_UNLOAD
, hModule
);
1307 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16
, hModule
);
1310 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
1314 /* Clear magic number just in case */
1316 pModule
->magic
= pModule
->self
= 0;
1318 /* Remove it from the linked list */
1320 hPrevModule
= &hFirstModule
;
1321 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
1323 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
1325 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
1327 /* Free the referenced modules */
1329 pModRef
= (HMODULE16
*)NE_MODULE_TABLE( pModule
);
1330 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
1332 NE_FreeModule( *pModRef
, call_wep
);
1335 /* Free the module storage */
1337 GlobalFreeAll16( hModule
);
1339 /* Remove module from cache */
1341 if (pCachedModule
== pModule
) pCachedModule
= NULL
;
1346 /**********************************************************************
1347 * FreeModule16 (KERNEL.46)
1349 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
1351 return NE_FreeModule( hModule
, TRUE
);
1355 /***********************************************************************
1356 * FreeLibrary16 (KERNEL.96)
1358 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
1360 TRACE("%04x\n", handle
);
1361 FreeModule16( handle
);
1365 /**********************************************************************
1366 * GetModuleName (KERNEL.27)
1368 BOOL16 WINAPI
GetModuleName16( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
1373 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
1374 p
= (BYTE
*)pModule
+ pModule
->name_table
;
1375 if (count
> *p
) count
= *p
+ 1;
1378 memcpy( buf
, p
+ 1, count
- 1 );
1379 buf
[count
-1] = '\0';
1385 /**********************************************************************
1386 * GetModuleUsage (KERNEL.48)
1388 INT16 WINAPI
GetModuleUsage16( HINSTANCE16 hModule
)
1390 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1391 return pModule
? pModule
->count
: 0;
1395 /**********************************************************************
1396 * GetExpWinVer (KERNEL.167)
1398 WORD WINAPI
GetExpWinVer16( HMODULE16 hModule
)
1400 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1401 if ( !pModule
) return 0;
1404 * For built-in modules, fake the expected version the module should
1405 * have according to the Windows version emulated by Wine
1407 * FIXME: Should we really do this for Win32 dummy modules as well?
1409 if ( (pModule
->flags
& NE_FFLAGS_BUILTIN
)
1410 || (pModule
->flags
& NE_FFLAGS_WIN32
) )
1412 OSVERSIONINFOA versionInfo
;
1413 versionInfo
.dwOSVersionInfoSize
= sizeof(versionInfo
);
1415 if ( GetVersionExA( &versionInfo
) )
1416 return (versionInfo
.dwMajorVersion
& 0xff) << 8
1417 | (versionInfo
.dwMinorVersion
& 0xff);
1420 return pModule
->expected_version
;
1424 /**********************************************************************
1425 * GetModuleFileName16 (KERNEL.49)
1427 INT16 WINAPI
GetModuleFileName16( HINSTANCE16 hModule
, LPSTR lpFileName
,
1432 if (!hModule
) hModule
= GetCurrentTask();
1433 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
1434 lstrcpynA( lpFileName
, NE_MODULE_NAME(pModule
), nSize
);
1435 TRACE("%s\n", lpFileName
);
1436 return strlen(lpFileName
);
1440 /**********************************************************************
1441 * GetModuleHandle16 (KERNEL.47)
1443 * Find a module from a module name.
1445 * NOTE: The current implementation works the same way the Windows 95 one
1446 * does. Do not try to 'fix' it, fix the callers.
1447 * + It does not do ANY extension handling (except that strange .EXE bit)!
1448 * + It does not care about paths, just about basenames. (same as Windows)
1452 * the win16 module handle if found
1454 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1455 * Always hFirstModule
1457 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1459 if (HIWORD(name
) == 0)
1460 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1461 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name
)), hFirstModule
);
1464 HMODULE16 WINAPI
GetModuleHandle16( LPCSTR name
)
1466 HMODULE16 hModule
= hFirstModule
;
1468 BYTE len
, *name_table
;
1472 TRACE("(%s)\n", name
);
1475 return GetExePtr(LOWORD(name
));
1481 strncpy(tmpstr
, name
, sizeof(tmpstr
));
1482 tmpstr
[sizeof(tmpstr
)-1] = '\0';
1484 /* If 'name' matches exactly the module name of a module:
1485 * Return its handle.
1487 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1489 pModule
= NE_GetPtr( hModule
);
1490 if (!pModule
) break;
1491 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1493 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1494 if ((*name_table
== len
) && !strncmp(name
, name_table
+1, len
))
1498 /* If uppercased 'name' matches exactly the module name of a module:
1501 for (s
= tmpstr
; *s
; s
++)
1504 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1506 pModule
= NE_GetPtr( hModule
);
1507 if (!pModule
) break;
1508 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1510 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1511 /* FIXME: the lstrncmpiA 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
) && !lstrncmpiA(tmpstr
, 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
->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 (!lstrcmpiA(loadedfn
, s
))
1560 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1561 * matches the base filename of the module filename of some 32-bit module:
1562 * Return the corresponding 16-bit dummy module handle.
1564 if (len
>= 4 && !strcasecmp(name
+len
-4, ".EXE"))
1566 HMODULE hModule
= GetModuleHandleA( name
);
1568 return MapHModuleLS( hModule
);
1571 if (!strcmp(tmpstr
,"MSDOS"))
1574 if (!strcmp(tmpstr
,"TIMER"))
1576 FIXME("Eh... Should return caller's code segment, expect crash\n");
1584 /**********************************************************************
1585 * ModuleFirst (TOOLHELP.59)
1587 BOOL16 WINAPI
ModuleFirst16( MODULEENTRY
*lpme
)
1589 lpme
->wNext
= hFirstModule
;
1590 return ModuleNext16( lpme
);
1594 /**********************************************************************
1595 * ModuleNext (TOOLHELP.60)
1597 BOOL16 WINAPI
ModuleNext16( MODULEENTRY
*lpme
)
1602 if (!lpme
->wNext
) return FALSE
;
1603 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1604 name
= (char *)pModule
+ pModule
->name_table
;
1605 memcpy( lpme
->szModule
, name
+ 1, min(*name
, MAX_MODULE_NAME
) );
1606 lpme
->szModule
[min(*name
, MAX_MODULE_NAME
)] = '\0';
1607 lpme
->hModule
= lpme
->wNext
;
1608 lpme
->wcUsage
= pModule
->count
;
1609 lstrcpynA( lpme
->szExePath
, NE_MODULE_NAME(pModule
), sizeof(lpme
->szExePath
) );
1610 lpme
->wNext
= pModule
->next
;
1615 /**********************************************************************
1616 * ModuleFindName (TOOLHELP.61)
1618 BOOL16 WINAPI
ModuleFindName16( MODULEENTRY
*lpme
, LPCSTR name
)
1620 lpme
->wNext
= GetModuleHandle16( name
);
1621 return ModuleNext16( lpme
);
1625 /**********************************************************************
1626 * ModuleFindHandle (TOOLHELP.62)
1628 BOOL16 WINAPI
ModuleFindHandle16( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1630 hModule
= GetExePtr( hModule
);
1631 lpme
->wNext
= hModule
;
1632 return ModuleNext16( lpme
);
1636 /***************************************************************************
1637 * IsRomModule16 (KERNEL.323)
1639 BOOL16 WINAPI
IsRomModule16( HMODULE16 unused
)
1644 /***************************************************************************
1645 * IsRomFile16 (KERNEL.326)
1647 BOOL16 WINAPI
IsRomFile16( HFILE16 unused
)
1652 /***************************************************************************
1653 * MapHModuleLS (KERNEL32.520)
1655 HMODULE16 WINAPI
MapHModuleLS(HMODULE hmod
) {
1659 return ((TDB
*)GlobalLock16(GetCurrentTask()))->hInstance
;
1661 return hmod
; /* we already have a 16 bit module handle */
1662 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
1664 if (pModule
->module32
== hmod
)
1665 return pModule
->self
;
1666 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
1671 /***************************************************************************
1672 * MapHModuleSL (KERNEL32.521)
1674 HMODULE WINAPI
MapHModuleSL(HMODULE16 hmod
) {
1678 TDB
*pTask
= (TDB
*)GlobalLock16(GetCurrentTask());
1680 hmod
= pTask
->hModule
;
1682 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
1683 if ( (pModule
->magic
!=IMAGE_OS2_SIGNATURE
) ||
1684 !(pModule
->flags
& NE_FFLAGS_WIN32
)
1687 return pModule
->module32
;
1690 /***************************************************************************
1691 * MapHInstLS (KERNEL32.516)
1693 void WINAPI
REGS_FUNC(MapHInstLS
)( CONTEXT
*context
)
1696 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1700 /***************************************************************************
1701 * MapHInstSL (KERNEL32.518)
1703 void WINAPI
REGS_FUNC(MapHInstSL
)( CONTEXT
*context
)
1706 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));
1710 /***************************************************************************
1711 * MapHInstLS_PN (KERNEL32.517)
1713 void WINAPI
REGS_FUNC(MapHInstLS_PN
)( CONTEXT
*context
)
1716 if (EAX_reg(context
))
1717 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1721 /***************************************************************************
1722 * MapHInstSL_PN (KERNEL32.519)
1724 void WINAPI
REGS_FUNC(MapHInstSL_PN
)( CONTEXT
*context
)
1727 if (EAX_reg(context
))
1728 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));
1732 /***************************************************************************
1733 * WIN16_MapHInstLS (KERNEL.472)
1735 VOID WINAPI
WIN16_MapHInstLS( CONTEXT86
*context
)
1737 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1740 /***************************************************************************
1741 * WIN16_MapHInstSL (KERNEL.473)
1743 VOID WINAPI
WIN16_MapHInstSL( CONTEXT86
*context
)
1745 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));