4 * Copyright 1995 Alexandre Julliard
22 #include "stackframe.h"
25 FARPROC16 (*fnSNOOP16_GetProcAddress16
)(HMODULE16
,DWORD
,FARPROC16
) = NULL
;
26 void (*fnSNOOP16_RegisterDLL
)(NE_MODULE
*,LPCSTR
) = NULL
;
28 #define hFirstModule (pThhook->hExeHead)
30 static NE_MODULE
*pCachedModule
= 0; /* Module cached by NE_OpenFile */
32 static HMODULE16
NE_LoadBuiltin(LPCSTR name
,BOOL32 force
) { return 0; }
33 HMODULE16 (*fnBUILTIN_LoadModule
)(LPCSTR name
,BOOL32 force
) = NE_LoadBuiltin
;
36 /***********************************************************************
39 NE_MODULE
*NE_GetPtr( HMODULE16 hModule
)
41 return (NE_MODULE
*)GlobalLock16( GetExePtr(hModule
) );
45 /***********************************************************************
48 void NE_DumpModule( HMODULE16 hModule
)
56 if (!(pModule
= NE_GetPtr( hModule
)))
58 MSG( "**** %04x is not a module handle\n", hModule
);
62 /* Dump the module info */
64 DUMP( "Module %04x:\n", hModule
);
65 DUMP( "count=%d flags=%04x heap=%d stack=%d\n",
66 pModule
->count
, pModule
->flags
,
67 pModule
->heap_size
, pModule
->stack_size
);
68 DUMP( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
69 pModule
->cs
, pModule
->ip
, pModule
->ss
, pModule
->sp
, pModule
->dgroup
,
70 pModule
->seg_count
, pModule
->modref_count
);
71 DUMP( "os_flags=%d swap_area=%d version=%04x\n",
72 pModule
->os_flags
, pModule
->min_swap_area
,
73 pModule
->expected_version
);
74 if (pModule
->flags
& NE_FFLAGS_WIN32
)
75 DUMP( "PE module=%08x\n", pModule
->module32
);
77 /* Dump the file info */
79 DUMP( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
81 /* Dump the segment table */
83 DUMP( "Segment table:\n" );
84 pSeg
= NE_SEG_TABLE( pModule
);
85 for (i
= 0; i
< pModule
->seg_count
; i
++, pSeg
++)
86 DUMP( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
87 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
88 pSeg
->minsize
, pSeg
->hSeg
);
90 /* Dump the resource table */
92 DUMP( "Resource table:\n" );
93 if (pModule
->res_table
)
95 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->res_table
);
96 DUMP( "Alignment: %d\n", *pword
++ );
99 struct resource_typeinfo_s
*ptr
= (struct resource_typeinfo_s
*)pword
;
100 struct resource_nameinfo_s
*pname
= (struct resource_nameinfo_s
*)(ptr
+ 1);
101 DUMP( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
102 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
103 DUMP( "offset=%d len=%d id=%04x\n",
104 pname
->offset
, pname
->length
, pname
->id
);
105 pword
= (WORD
*)pname
;
108 else DUMP( "None\n" );
110 /* Dump the resident name table */
112 DUMP( "Resident-name table:\n" );
113 pstr
= (char *)pModule
+ pModule
->name_table
;
116 DUMP( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
117 *(WORD
*)(pstr
+ *pstr
+ 1) );
118 pstr
+= *pstr
+ 1 + sizeof(WORD
);
121 /* Dump the module reference table */
123 DUMP( "Module ref table:\n" );
124 if (pModule
->modref_table
)
126 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->modref_table
);
127 for (i
= 0; i
< pModule
->modref_count
; i
++, pword
++)
130 GetModuleName( *pword
, name
, sizeof(name
) );
131 DUMP( "%d: %04x -> '%s'\n", i
, *pword
, name
);
134 else DUMP( "None\n" );
136 /* Dump the entry table */
138 DUMP( "Entry table:\n" );
139 pstr
= (char *)pModule
+ pModule
->entry_table
;
143 DUMP( "Bundle %d-%d: %02x\n", ordinal
, ordinal
+ *pstr
- 1, pstr
[1]);
149 else if ((BYTE
)pstr
[1] == 0xff) /* moveable */
155 DUMP( "%d: %02x:%04x (moveable)\n",
156 ordinal
++, pstr
[3], *(WORD
*)(pstr
+ 4) );
166 DUMP( "%d: %04x (fixed)\n",
167 ordinal
++, *(WORD
*)(pstr
+ 1) );
173 /* Dump the non-resident names table */
175 DUMP( "Non-resident names table:\n" );
176 if (pModule
->nrname_handle
)
178 pstr
= (char *)GlobalLock16( pModule
->nrname_handle
);
181 DUMP( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
182 *(WORD
*)(pstr
+ *pstr
+ 1) );
183 pstr
+= *pstr
+ 1 + sizeof(WORD
);
190 /***********************************************************************
193 * Walk the module list and print the modules.
195 void NE_WalkModules(void)
197 HMODULE16 hModule
= hFirstModule
;
198 MSG( "Module Flags Name\n" );
201 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
204 MSG( "Bad module %04x in list\n", hModule
);
207 MSG( " %04x %04x %.*s\n", hModule
, pModule
->flags
,
208 *((char *)pModule
+ pModule
->name_table
),
209 (char *)pModule
+ pModule
->name_table
+ 1 );
210 hModule
= pModule
->next
;
215 /**********************************************************************
218 void NE_RegisterModule( NE_MODULE
*pModule
)
220 pModule
->next
= hFirstModule
;
221 hFirstModule
= pModule
->self
;
225 /***********************************************************************
228 * Lookup the ordinal for a given name.
230 WORD
NE_GetOrdinal( HMODULE16 hModule
, const char *name
)
232 unsigned char buffer
[256], *cpnt
;
236 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
237 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
239 TRACE( module
, "(%04x,'%s')\n", hModule
, name
);
241 /* First handle names of the form '#xxxx' */
243 if (name
[0] == '#') return atoi( name
+ 1 );
245 /* Now copy and uppercase the string */
247 strcpy( buffer
, name
);
248 CharUpper32A( buffer
);
249 len
= strlen( buffer
);
251 /* First search the resident names */
253 cpnt
= (char *)pModule
+ pModule
->name_table
;
255 /* Skip the first entry (module name) */
256 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
259 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
261 TRACE(module
, " Found: ordinal=%d\n",
262 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
263 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
265 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
268 /* Now search the non-resident names table */
270 if (!pModule
->nrname_handle
) return 0; /* No non-resident table */
271 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
273 /* Skip the first entry (module description string) */
274 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
277 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
279 TRACE(module
, " Found: ordinal=%d\n",
280 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
281 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
283 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
289 /***********************************************************************
290 * NE_GetEntryPoint (WPROCS.27)
292 * Return the entry point for a given ordinal.
294 FARPROC16
NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
296 return NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
298 FARPROC16
NE_GetEntryPointEx( HMODULE16 hModule
, WORD ordinal
, BOOL16 snoop
)
305 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
306 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
308 p
= (BYTE
*)pModule
+ pModule
->entry_table
;
309 while (*p
&& (curOrdinal
+ *p
<= ordinal
))
311 /* Skipping this bundle */
315 case 0: p
+= 2; break; /* unused */
316 case 0xff: p
+= 2 + *p
* 6; break; /* moveable */
317 default: p
+= 2 + *p
* 3; break; /* fixed */
326 case 0xff: /* moveable */
327 p
+= 2 + 6 * (ordinal
- curOrdinal
);
329 offset
= *(WORD
*)(p
+ 4);
333 p
+= 2 + 3 * (ordinal
- curOrdinal
);
334 offset
= *(WORD
*)(p
+ 1);
338 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
339 else sel
= GlobalHandleToSel(NE_SEG_TABLE(pModule
)[sel
-1].hSeg
);
341 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
342 if (!snoop
|| !fnSNOOP16_GetProcAddress16
)
343 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
345 return (FARPROC16
)fnSNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
));
349 /***********************************************************************
352 * Change the value of an entry point. Use with caution!
353 * It can only change the offset value, not the selector.
355 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
361 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
362 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
364 p
= (BYTE
*)pModule
+ pModule
->entry_table
;
365 while (*p
&& (curOrdinal
+ *p
<= ordinal
))
367 /* Skipping this bundle */
371 case 0: p
+= 2; break; /* unused */
372 case 0xff: p
+= 2 + *p
* 6; break; /* moveable */
373 default: p
+= 2 + *p
* 3; break; /* fixed */
376 if (!*p
) return FALSE
;
382 case 0xff: /* moveable */
383 p
+= 2 + 6 * (ordinal
- curOrdinal
);
384 *(WORD
*)(p
+ 4) = offset
;
387 p
+= 2 + 3 * (ordinal
- curOrdinal
);
388 *(WORD
*)(p
+ 1) = offset
;
395 /***********************************************************************
398 int NE_OpenFile( NE_MODULE
*pModule
)
400 DOS_FULL_NAME full_name
;
403 static int cachedfd
= -1;
405 TRACE( module
, "(%p) cache: mod=%p fd=%d\n",
406 pModule
, pCachedModule
, cachedfd
);
407 if (pCachedModule
== pModule
) return cachedfd
;
409 pCachedModule
= pModule
;
410 name
= NE_MODULE_NAME( pModule
);
411 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
) ||
412 (cachedfd
= open( full_name
.long_name
, O_RDONLY
)) == -1)
413 MSG( "Can't open file '%s' for module %04x\n", name
, pModule
->self
);
414 TRACE(module
, "opened '%s' -> %d\n",
420 /***********************************************************************
423 static HMODULE16
NE_LoadExeHeader( HFILE16 hFile
, OFSTRUCT
*ofs
)
425 IMAGE_DOS_HEADER mz_header
;
426 IMAGE_OS2_HEADER ne_header
;
431 char *buffer
, *fastload
= NULL
;
432 int fastload_offset
= 0, fastload_length
= 0;
434 /* Read a block from either the file or the fast-load area. */
435 #define READ(offset,size,buffer) \
436 ((fastload && ((offset) >= fastload_offset) && \
437 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
438 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
439 (_llseek16( hFile, (offset), SEEK_SET), \
440 _hread16( hFile, (buffer), (size) ) == (size)))
442 _llseek16( hFile
, 0, SEEK_SET
);
443 if ((_hread16(hFile
,&mz_header
,sizeof(mz_header
)) != sizeof(mz_header
)) ||
444 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
445 return (HMODULE16
)11; /* invalid exe */
447 _llseek16( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
448 if (_hread16( hFile
, &ne_header
, sizeof(ne_header
) ) != sizeof(ne_header
))
449 return (HMODULE16
)11; /* invalid exe */
451 if (ne_header
.ne_magic
== IMAGE_NT_SIGNATURE
) return (HMODULE16
)21; /* win32 exe */
452 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return (HMODULE16
)11; /* invalid exe */
454 if (ne_header
.ne_magic
== IMAGE_OS2_SIGNATURE_LX
) {
455 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
456 return (HMODULE16
)12;
459 /* We now have a valid NE header */
461 size
= sizeof(NE_MODULE
) +
463 ne_header
.n_segment_tab
* sizeof(SEGTABLEENTRY
) +
465 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
+
466 /* resident names table */
467 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
+
468 /* module ref table */
469 ne_header
.n_mod_ref_tab
* sizeof(WORD
) +
470 /* imported names table */
471 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
+
472 /* entry table length */
473 ne_header
.entry_tab_length
+
474 /* loaded file info */
475 sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
477 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
478 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
479 FarSetOwner( hModule
, hModule
);
480 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
481 memcpy( pModule
, &ne_header
, sizeof(ne_header
) );
483 pModule
->module32
= 0;
484 pModule
->self
= hModule
;
485 pModule
->self_loading_sel
= 0;
486 pData
= (BYTE
*)(pModule
+ 1);
488 /* Clear internal Wine flags in case they are set in the EXE file */
490 pModule
->flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
492 /* Read the fast-load area */
494 if (ne_header
.additional_flags
& NE_AFLAGS_FASTLOAD
)
496 fastload_offset
=ne_header
.fastload_offset
<<ne_header
.align_shift_count
;
497 fastload_length
=ne_header
.fastload_length
<<ne_header
.align_shift_count
;
498 TRACE(module
, "Using fast-load area offset=%x len=%d\n",
499 fastload_offset
, fastload_length
);
500 if ((fastload
= HeapAlloc( SystemHeap
, 0, fastload_length
)) != NULL
)
502 _llseek16( hFile
, fastload_offset
, SEEK_SET
);
503 if (_hread16(hFile
, fastload
, fastload_length
) != fastload_length
)
505 HeapFree( SystemHeap
, 0, fastload
);
506 WARN( module
, "Error reading fast-load area!\n");
512 /* Get the segment table */
514 pModule
->seg_table
= (int)pData
- (int)pModule
;
515 buffer
= HeapAlloc( SystemHeap
, 0, ne_header
.n_segment_tab
*
516 sizeof(struct ne_segment_table_entry_s
));
520 struct ne_segment_table_entry_s
*pSeg
;
522 if (!READ( mz_header
.e_lfanew
+ ne_header
.segment_tab_offset
,
523 ne_header
.n_segment_tab
* sizeof(struct ne_segment_table_entry_s
),
526 HeapFree( SystemHeap
, 0, buffer
);
527 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
528 GlobalFree16( hModule
);
529 return (HMODULE16
)11; /* invalid exe */
531 pSeg
= (struct ne_segment_table_entry_s
*)buffer
;
532 for (i
= ne_header
.n_segment_tab
; i
> 0; i
--, pSeg
++)
534 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
535 pData
+= sizeof(SEGTABLEENTRY
);
537 HeapFree( SystemHeap
, 0, buffer
);
541 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
542 GlobalFree16( hModule
);
543 return (HMODULE16
)11; /* invalid exe */
546 /* Get the resource table */
548 if (ne_header
.resource_tab_offset
< ne_header
.rname_tab_offset
)
550 pModule
->res_table
= (int)pData
- (int)pModule
;
551 if (!READ(mz_header
.e_lfanew
+ ne_header
.resource_tab_offset
,
552 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
,
553 pData
)) return (HMODULE16
)11; /* invalid exe */
554 pData
+= ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
;
555 NE_InitResourceHandler( hModule
);
557 else pModule
->res_table
= 0; /* No resource table */
559 /* Get the resident names table */
561 pModule
->name_table
= (int)pData
- (int)pModule
;
562 if (!READ( mz_header
.e_lfanew
+ ne_header
.rname_tab_offset
,
563 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
,
566 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
567 GlobalFree16( hModule
);
568 return (HMODULE16
)11; /* invalid exe */
570 pData
+= ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
;
572 /* Get the module references table */
574 if (ne_header
.n_mod_ref_tab
> 0)
576 pModule
->modref_table
= (int)pData
- (int)pModule
;
577 if (!READ( mz_header
.e_lfanew
+ ne_header
.moduleref_tab_offset
,
578 ne_header
.n_mod_ref_tab
* sizeof(WORD
),
581 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
582 GlobalFree16( hModule
);
583 return (HMODULE16
)11; /* invalid exe */
585 pData
+= ne_header
.n_mod_ref_tab
* sizeof(WORD
);
587 else pModule
->modref_table
= 0; /* No module references */
589 /* Get the imported names table */
591 pModule
->import_table
= (int)pData
- (int)pModule
;
592 if (!READ( mz_header
.e_lfanew
+ ne_header
.iname_tab_offset
,
593 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
,
596 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
597 GlobalFree16( hModule
);
598 return (HMODULE16
)11; /* invalid exe */
600 pData
+= ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
;
602 /* Get the entry table */
604 pModule
->entry_table
= (int)pData
- (int)pModule
;
605 if (!READ( mz_header
.e_lfanew
+ ne_header
.entry_tab_offset
,
606 ne_header
.entry_tab_length
,
609 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
610 GlobalFree16( hModule
);
611 return (HMODULE16
)11; /* invalid exe */
613 pData
+= ne_header
.entry_tab_length
;
615 /* Store the filename information */
617 pModule
->fileinfo
= (int)pData
- (int)pModule
;
618 size
= sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
619 memcpy( pData
, ofs
, size
);
620 ((OFSTRUCT
*)pData
)->cBytes
= size
- 1;
623 /* Free the fast-load area */
626 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
628 /* Get the non-resident names table */
630 if (ne_header
.nrname_tab_length
)
632 pModule
->nrname_handle
= GLOBAL_Alloc( 0, ne_header
.nrname_tab_length
,
633 hModule
, FALSE
, FALSE
, FALSE
);
634 if (!pModule
->nrname_handle
)
636 GlobalFree16( hModule
);
637 return (HMODULE16
)11; /* invalid exe */
639 buffer
= GlobalLock16( pModule
->nrname_handle
);
640 _llseek16( hFile
, ne_header
.nrname_tab_offset
, SEEK_SET
);
641 if (_hread16( hFile
, buffer
, ne_header
.nrname_tab_length
)
642 != ne_header
.nrname_tab_length
)
644 GlobalFree16( pModule
->nrname_handle
);
645 GlobalFree16( hModule
);
646 return (HMODULE16
)11; /* invalid exe */
649 else pModule
->nrname_handle
= 0;
651 /* Allocate a segment for the implicitly-loaded DLLs */
653 if (pModule
->modref_count
)
655 pModule
->dlls_to_init
= GLOBAL_Alloc(GMEM_ZEROINIT
,
656 (pModule
->modref_count
+1)*sizeof(HMODULE16
),
657 hModule
, FALSE
, FALSE
, FALSE
);
658 if (!pModule
->dlls_to_init
)
660 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
661 GlobalFree16( hModule
);
662 return (HMODULE16
)11; /* invalid exe */
665 else pModule
->dlls_to_init
= 0;
667 NE_RegisterModule( pModule
);
668 if (fnSNOOP16_RegisterDLL
)
669 fnSNOOP16_RegisterDLL(pModule
,ofs
->szPathName
);
674 /***********************************************************************
677 * Load all DLLs implicitly linked to a module.
679 static BOOL32
NE_LoadDLLs( NE_MODULE
*pModule
)
682 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->modref_table
);
683 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
685 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
688 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->import_table
+ *pModRef
;
689 memcpy( buffer
, pstr
+ 1, *pstr
);
690 strcpy( buffer
+ *pstr
, ".dll" );
691 TRACE(module
, "Loading '%s'\n", buffer
);
692 if (!(*pModRef
= GetModuleHandle16( buffer
)))
694 /* If the DLL is not loaded yet, load it and store */
695 /* its handle in the list of DLLs to initialize. */
698 if ((hDLL
= NE_LoadModule( buffer
, NULL
, TRUE
, TRUE
)) == 2)
703 /* Try with prepending the path of the current module */
704 GetModuleFileName16( pModule
->self
, buffer
, sizeof(buffer
) );
705 if (!(p
= strrchr( buffer
, '\\' ))) p
= buffer
;
706 memcpy( p
+ 1, pstr
+ 1, *pstr
);
707 strcpy( p
+ 1 + *pstr
, ".dll" );
708 hDLL
= NE_LoadModule( buffer
, NULL
, TRUE
, TRUE
);
712 /* FIXME: cleanup what was done */
714 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
715 buffer
, *((BYTE
*)pModule
+ pModule
->name_table
),
716 (char *)pModule
+ pModule
->name_table
+ 1, hDLL
);
719 *pModRef
= GetExePtr( hDLL
);
722 else /* Increment the reference count of the DLL */
724 NE_MODULE
*pOldDLL
= NE_GetPtr( *pModRef
);
725 if (pOldDLL
) pOldDLL
->count
++;
732 /**********************************************************************
735 * Implementation of LoadModule16().
737 HINSTANCE16
NE_LoadModule( LPCSTR name
, HINSTANCE16
*hPrevInstance
,
738 BOOL32 implicit
, BOOL32 lib_only
)
741 HINSTANCE16 hInstance
;
746 /* Check if the module is already loaded */
748 if ((hModule
= GetModuleHandle16( name
)) != 0)
751 pModule
= NE_GetPtr( hModule
);
752 if ( pModule
->module32
) return (HINSTANCE16
)21;
754 hInstance
= NE_CreateInstance( pModule
, &prev
, lib_only
);
755 if (hInstance
!= prev
) /* not a library */
756 NE_LoadSegment( pModule
, pModule
->dgroup
);
758 if (hPrevInstance
) *hPrevInstance
= prev
;
761 if (hPrevInstance
) *hPrevInstance
= 0;
763 /* Try to load the built-in first if not disabled */
765 if ((hModule
= fnBUILTIN_LoadModule( name
, FALSE
))) return hModule
;
767 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
769 /* Now try the built-in even if disabled */
770 if ((hModule
= fnBUILTIN_LoadModule( name
, TRUE
)))
772 MSG( "Could not load Windows DLL '%s', using built-in module.\n",
776 return 2; /* File not found */
779 /* Create the module structure */
781 hModule
= NE_LoadExeHeader( hFile
, &ofs
);
783 if (hModule
< 32) return hModule
;
784 pModule
= NE_GetPtr( hModule
);
786 /* Allocate the segments for this module */
788 if (!NE_CreateSegments( pModule
) ||
789 !(hInstance
= NE_CreateInstance( pModule
, NULL
, lib_only
)))
791 GlobalFreeAll( hModule
);
792 return 8; /* Insufficient memory */
795 /* Load the referenced DLLs */
797 if (!NE_LoadDLLs( pModule
))
798 return 2; /* File not found (FIXME: free everything) */
800 /* Load the segments */
802 NE_LoadAllSegments( pModule
);
804 /* Fixup the functions prologs */
806 NE_FixupPrologs( pModule
);
808 /* Make sure the usage count is 1 on the first loading of */
809 /* the module, even if it contains circular DLL references */
813 /* Call initialization rountines for all loaded DLLs. Note that
814 * when we load implicitly linked DLLs this will be done by InitTask().
817 if (!implicit
&& (pModule
->flags
& NE_FFLAGS_LIBMODULE
))
818 NE_InitializeDLLs( hModule
);
824 /***********************************************************************
825 * LoadLibrary (KERNEL.95)
827 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
833 TRACE(module
, "(%08x) %s\n", (int)libname
, libname
);
835 /* Check for an extension */
837 if ((p
= strrchr( libname
, '.')) && !strchr( p
, '/' ) && !strchr( p
, '\\'))
839 /* An extension is present -> use the name as is */
840 return NE_LoadModule( libname
, NULL
, FALSE
, TRUE
);
843 /* Now append .dll before loading */
845 if (!(new_name
= HeapAlloc( GetProcessHeap(), 0, strlen(libname
) + 4 )))
847 strcpy( new_name
, libname
);
848 strcat( new_name
, ".dll" );
849 handle
= NE_LoadModule( new_name
, NULL
, FALSE
, TRUE
);
850 HeapFree( GetProcessHeap(), 0, new_name
);
855 /**********************************************************************
858 * Call a DLL's WEP, allowing it to shut down.
859 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
861 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
863 FARPROC16 WEP
= (FARPROC16
)0;
864 WORD ordinal
= NE_GetOrdinal( hModule
, "WEP" );
866 if (ordinal
) WEP
= NE_GetEntryPoint( hModule
, ordinal
);
869 WARN(module
, "module %04x doesn't have a WEP\n", hModule
);
872 return Callbacks
->CallWindowsExitProc( WEP
, WEP_FREE_DLL
);
876 /**********************************************************************
879 * Implementation of FreeModule16().
881 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL32 call_wep
)
883 HMODULE16
*hPrevModule
;
888 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
889 hModule
= pModule
->self
;
891 TRACE( module
, "%04x count %d\n", hModule
, pModule
->count
);
893 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
894 else pModule
->count
= 0;
896 if (pModule
->flags
& NE_FFLAGS_BUILTIN
)
897 return FALSE
; /* Can't free built-in module */
901 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
)
903 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
904 MODULE_CallWEP( hModule
);
906 /* Free the objects owned by the DLL module */
908 if (pTask
&& pTask
->userhandler
)
909 pTask
->userhandler( hModule
, USIG_DLL_UNLOAD
, 0,
910 pTask
->hInstance
, pTask
->hQueue
);
913 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
917 /* Clear magic number just in case */
919 pModule
->magic
= pModule
->self
= 0;
921 /* Remove it from the linked list */
923 hPrevModule
= &hFirstModule
;
924 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
926 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
928 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
930 /* Free the referenced modules */
932 pModRef
= (HMODULE16
*)NE_MODULE_TABLE( pModule
);
933 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
935 NE_FreeModule( *pModRef
, call_wep
);
938 /* Free the module storage */
940 GlobalFreeAll( hModule
);
942 /* Remove module from cache */
944 if (pCachedModule
== pModule
) pCachedModule
= NULL
;
949 /**********************************************************************
950 * FreeModule16 (KERNEL.46)
952 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
954 return NE_FreeModule( hModule
, TRUE
);
958 /***********************************************************************
959 * FreeLibrary16 (KERNEL.96)
961 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
963 TRACE(module
,"%04x\n", handle
);
964 FreeModule16( handle
);
968 /**********************************************************************
969 * GetModuleName (KERNEL.27)
971 BOOL16 WINAPI
GetModuleName( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
976 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
977 p
= (BYTE
*)pModule
+ pModule
->name_table
;
978 if (count
> *p
) count
= *p
+ 1;
981 memcpy( buf
, p
+ 1, count
- 1 );
988 /**********************************************************************
989 * GetModuleUsage (KERNEL.48)
991 INT16 WINAPI
GetModuleUsage( HINSTANCE16 hModule
)
993 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
994 return pModule
? pModule
->count
: 0;
998 /**********************************************************************
999 * GetExpWinVer (KERNEL.167)
1001 WORD WINAPI
GetExpWinVer( HMODULE16 hModule
)
1003 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1004 return pModule
? pModule
->expected_version
: 0;
1008 /**********************************************************************
1009 * GetModuleFileName16 (KERNEL.49)
1011 INT16 WINAPI
GetModuleFileName16( HINSTANCE16 hModule
, LPSTR lpFileName
,
1016 if (!hModule
) hModule
= GetCurrentTask();
1017 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
1018 lstrcpyn32A( lpFileName
, NE_MODULE_NAME(pModule
), nSize
);
1019 TRACE(module
, "%s\n", lpFileName
);
1020 return strlen(lpFileName
);
1024 /**********************************************************************
1025 * GetModuleHandle16 (KERNEL.47)
1027 * Find a module from a path name.
1031 * the win16 module handle if found
1033 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1034 * Always hFirstModule
1036 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1038 if (HIWORD(name
) == 0)
1039 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1040 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name
)), hFirstModule
);
1043 HMODULE16 WINAPI
GetModuleHandle16( LPCSTR name
)
1045 HMODULE16 hModule
= hFirstModule
;
1046 LPCSTR filename
, dotptr
, modulepath
, modulename
;
1047 BYTE len
, *name_table
;
1049 if (!(filename
= strrchr( name
, '\\' ))) filename
= name
;
1051 if ((dotptr
= strrchr( filename
, '.' )) != NULL
)
1052 len
= (BYTE
)(dotptr
- filename
);
1053 else len
= strlen( filename
);
1057 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1058 if (!pModule
) break;
1059 modulepath
= NE_MODULE_NAME(pModule
);
1060 if (!(modulename
= strrchr( modulepath
, '\\' )))
1061 modulename
= modulepath
;
1063 if (!lstrcmpi32A( modulename
, filename
)) return hModule
;
1065 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1066 if ((*name_table
== len
) && !lstrncmpi32A(filename
, name_table
+1, len
))
1068 hModule
= pModule
->next
;
1074 /**********************************************************************
1075 * ModuleFirst (TOOLHELP.59)
1077 BOOL16 WINAPI
ModuleFirst( MODULEENTRY
*lpme
)
1079 lpme
->wNext
= hFirstModule
;
1080 return ModuleNext( lpme
);
1084 /**********************************************************************
1085 * ModuleNext (TOOLHELP.60)
1087 BOOL16 WINAPI
ModuleNext( MODULEENTRY
*lpme
)
1092 if (!lpme
->wNext
) return FALSE
;
1093 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1094 name
= (char *)pModule
+ pModule
->name_table
;
1095 memcpy( lpme
->szModule
, name
+ 1, *name
);
1096 lpme
->szModule
[(BYTE
)*name
] = '\0';
1097 lpme
->hModule
= lpme
->wNext
;
1098 lpme
->wcUsage
= pModule
->count
;
1099 strncpy( lpme
->szExePath
, NE_MODULE_NAME(pModule
), MAX_PATH
);
1100 lpme
->szExePath
[MAX_PATH
] = '\0';
1101 lpme
->wNext
= pModule
->next
;
1106 /**********************************************************************
1107 * ModuleFindName (TOOLHELP.61)
1109 BOOL16 WINAPI
ModuleFindName( MODULEENTRY
*lpme
, LPCSTR name
)
1111 lpme
->wNext
= GetModuleHandle16( name
);
1112 return ModuleNext( lpme
);
1116 /**********************************************************************
1117 * ModuleFindHandle (TOOLHELP.62)
1119 BOOL16 WINAPI
ModuleFindHandle( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1121 hModule
= GetExePtr( hModule
);
1122 lpme
->wNext
= hModule
;
1123 return ModuleNext( lpme
);
1126 /***************************************************************************
1127 * MapHModuleLS (KERNEL32.520)
1129 HMODULE16 WINAPI
MapHModuleLS(HMODULE32 hmod
) {
1133 return ((TDB
*)GlobalLock16(GetCurrentTask()))->hInstance
;
1135 return hmod
; /* we already have a 16 bit module handle */
1136 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
1138 if (pModule
->module32
== hmod
)
1139 return pModule
->self
;
1140 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
1145 /***************************************************************************
1146 * MapHModuleSL (KERNEL32.521)
1148 HMODULE32 WINAPI
MapHModuleSL(HMODULE16 hmod
) {
1152 TDB
*pTask
= (TDB
*)GlobalLock16(GetCurrentTask());
1154 hmod
= pTask
->hInstance
;
1156 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
1157 if ( (pModule
->magic
!=IMAGE_OS2_SIGNATURE
) ||
1158 !(pModule
->flags
& NE_FFLAGS_WIN32
)
1161 return pModule
->module32
;
1164 /***************************************************************************
1165 * MapHInstLS (KERNEL32.516)
1167 REGS_ENTRYPOINT(MapHInstLS
) {
1168 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1171 /***************************************************************************
1172 * MapHInstSL (KERNEL32.518)
1174 REGS_ENTRYPOINT(MapHInstSL
) {
1175 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));
1178 /***************************************************************************
1179 * MapHInstLS_PN (KERNEL32.517)
1181 REGS_ENTRYPOINT(MapHInstLS_PN
) {
1182 if (EAX_reg(context
))
1183 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1186 /***************************************************************************
1187 * MapHInstSL_PN (KERNEL32.519)
1189 REGS_ENTRYPOINT(MapHInstSL_PN
) {
1190 if (EAX_reg(context
))
1191 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));
1194 /***************************************************************************
1195 * WIN16_MapHInstLS (KERNEL.472)
1197 VOID WINAPI
WIN16_MapHInstLS( CONTEXT
*context
) {
1198 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1201 /***************************************************************************
1202 * WIN16_MapHInstSL (KERNEL.473)
1204 VOID WINAPI
WIN16_MapHInstSL( CONTEXT
*context
) {
1205 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));