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
)
301 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
302 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
304 p
= (BYTE
*)pModule
+ pModule
->entry_table
;
305 while (*p
&& (curOrdinal
+ *p
<= ordinal
))
307 /* Skipping this bundle */
311 case 0: p
+= 2; break; /* unused */
312 case 0xff: p
+= 2 + *p
* 6; break; /* moveable */
313 default: p
+= 2 + *p
* 3; break; /* fixed */
322 case 0xff: /* moveable */
323 p
+= 2 + 6 * (ordinal
- curOrdinal
);
325 offset
= *(WORD
*)(p
+ 4);
329 p
+= 2 + 3 * (ordinal
- curOrdinal
);
330 offset
= *(WORD
*)(p
+ 1);
334 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
335 else sel
= GlobalHandleToSel(NE_SEG_TABLE(pModule
)[sel
-1].hSeg
);
337 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
338 if (!fnSNOOP16_GetProcAddress16
)
339 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
341 return (FARPROC16
)fnSNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
));
345 /***********************************************************************
348 * Change the value of an entry point. Use with caution!
349 * It can only change the offset value, not the selector.
351 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
357 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
358 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
360 p
= (BYTE
*)pModule
+ pModule
->entry_table
;
361 while (*p
&& (curOrdinal
+ *p
<= ordinal
))
363 /* Skipping this bundle */
367 case 0: p
+= 2; break; /* unused */
368 case 0xff: p
+= 2 + *p
* 6; break; /* moveable */
369 default: p
+= 2 + *p
* 3; break; /* fixed */
372 if (!*p
) return FALSE
;
378 case 0xff: /* moveable */
379 p
+= 2 + 6 * (ordinal
- curOrdinal
);
380 *(WORD
*)(p
+ 4) = offset
;
383 p
+= 2 + 3 * (ordinal
- curOrdinal
);
384 *(WORD
*)(p
+ 1) = offset
;
391 /***********************************************************************
394 int NE_OpenFile( NE_MODULE
*pModule
)
396 DOS_FULL_NAME full_name
;
399 static int cachedfd
= -1;
401 TRACE( module
, "(%p) cache: mod=%p fd=%d\n",
402 pModule
, pCachedModule
, cachedfd
);
403 if (pCachedModule
== pModule
) return cachedfd
;
405 pCachedModule
= pModule
;
406 name
= NE_MODULE_NAME( pModule
);
407 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
) ||
408 (cachedfd
= open( full_name
.long_name
, O_RDONLY
)) == -1)
409 MSG( "Can't open file '%s' for module %04x\n", name
, pModule
->self
);
410 TRACE(module
, "opened '%s' -> %d\n",
416 /***********************************************************************
419 static HMODULE16
NE_LoadExeHeader( HFILE16 hFile
, OFSTRUCT
*ofs
)
421 IMAGE_DOS_HEADER mz_header
;
422 IMAGE_OS2_HEADER ne_header
;
427 char *buffer
, *fastload
= NULL
;
428 int fastload_offset
= 0, fastload_length
= 0;
430 /* Read a block from either the file or the fast-load area. */
431 #define READ(offset,size,buffer) \
432 ((fastload && ((offset) >= fastload_offset) && \
433 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
434 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
435 (_llseek16( hFile, (offset), SEEK_SET), \
436 _hread16( hFile, (buffer), (size) ) == (size)))
438 _llseek16( hFile
, 0, SEEK_SET
);
439 if ((_hread16(hFile
,&mz_header
,sizeof(mz_header
)) != sizeof(mz_header
)) ||
440 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
441 return (HMODULE16
)11; /* invalid exe */
443 _llseek16( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
444 if (_hread16( hFile
, &ne_header
, sizeof(ne_header
) ) != sizeof(ne_header
))
445 return (HMODULE16
)11; /* invalid exe */
447 if (ne_header
.ne_magic
== IMAGE_NT_SIGNATURE
) return (HMODULE16
)21; /* win32 exe */
448 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return (HMODULE16
)11; /* invalid exe */
450 if (ne_header
.ne_magic
== IMAGE_OS2_SIGNATURE_LX
) {
451 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
452 return (HMODULE16
)12;
455 /* We now have a valid NE header */
457 size
= sizeof(NE_MODULE
) +
459 ne_header
.n_segment_tab
* sizeof(SEGTABLEENTRY
) +
461 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
+
462 /* resident names table */
463 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
+
464 /* module ref table */
465 ne_header
.n_mod_ref_tab
* sizeof(WORD
) +
466 /* imported names table */
467 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
+
468 /* entry table length */
469 ne_header
.entry_tab_length
+
470 /* loaded file info */
471 sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
473 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
474 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
475 FarSetOwner( hModule
, hModule
);
476 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
477 memcpy( pModule
, &ne_header
, sizeof(ne_header
) );
479 pModule
->module32
= 0;
480 pModule
->self
= hModule
;
481 pModule
->self_loading_sel
= 0;
482 pData
= (BYTE
*)(pModule
+ 1);
484 /* Clear internal Wine flags in case they are set in the EXE file */
486 pModule
->flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
488 /* Read the fast-load area */
490 if (ne_header
.additional_flags
& NE_AFLAGS_FASTLOAD
)
492 fastload_offset
=ne_header
.fastload_offset
<<ne_header
.align_shift_count
;
493 fastload_length
=ne_header
.fastload_length
<<ne_header
.align_shift_count
;
494 TRACE(module
, "Using fast-load area offset=%x len=%d\n",
495 fastload_offset
, fastload_length
);
496 if ((fastload
= HeapAlloc( SystemHeap
, 0, fastload_length
)) != NULL
)
498 _llseek16( hFile
, fastload_offset
, SEEK_SET
);
499 if (_hread16(hFile
, fastload
, fastload_length
) != fastload_length
)
501 HeapFree( SystemHeap
, 0, fastload
);
502 WARN( module
, "Error reading fast-load area!\n");
508 /* Get the segment table */
510 pModule
->seg_table
= (int)pData
- (int)pModule
;
511 buffer
= HeapAlloc( SystemHeap
, 0, ne_header
.n_segment_tab
*
512 sizeof(struct ne_segment_table_entry_s
));
516 struct ne_segment_table_entry_s
*pSeg
;
518 if (!READ( mz_header
.e_lfanew
+ ne_header
.segment_tab_offset
,
519 ne_header
.n_segment_tab
* sizeof(struct ne_segment_table_entry_s
),
522 HeapFree( SystemHeap
, 0, buffer
);
523 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
524 GlobalFree16( hModule
);
525 return (HMODULE16
)11; /* invalid exe */
527 pSeg
= (struct ne_segment_table_entry_s
*)buffer
;
528 for (i
= ne_header
.n_segment_tab
; i
> 0; i
--, pSeg
++)
530 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
531 pData
+= sizeof(SEGTABLEENTRY
);
533 HeapFree( SystemHeap
, 0, buffer
);
537 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
538 GlobalFree16( hModule
);
539 return (HMODULE16
)11; /* invalid exe */
542 /* Get the resource table */
544 if (ne_header
.resource_tab_offset
< ne_header
.rname_tab_offset
)
546 pModule
->res_table
= (int)pData
- (int)pModule
;
547 if (!READ(mz_header
.e_lfanew
+ ne_header
.resource_tab_offset
,
548 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
,
549 pData
)) return (HMODULE16
)11; /* invalid exe */
550 pData
+= ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
;
551 NE_InitResourceHandler( hModule
);
553 else pModule
->res_table
= 0; /* No resource table */
555 /* Get the resident names table */
557 pModule
->name_table
= (int)pData
- (int)pModule
;
558 if (!READ( mz_header
.e_lfanew
+ ne_header
.rname_tab_offset
,
559 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
,
562 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
563 GlobalFree16( hModule
);
564 return (HMODULE16
)11; /* invalid exe */
566 pData
+= ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
;
568 /* Get the module references table */
570 if (ne_header
.n_mod_ref_tab
> 0)
572 pModule
->modref_table
= (int)pData
- (int)pModule
;
573 if (!READ( mz_header
.e_lfanew
+ ne_header
.moduleref_tab_offset
,
574 ne_header
.n_mod_ref_tab
* sizeof(WORD
),
577 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
578 GlobalFree16( hModule
);
579 return (HMODULE16
)11; /* invalid exe */
581 pData
+= ne_header
.n_mod_ref_tab
* sizeof(WORD
);
583 else pModule
->modref_table
= 0; /* No module references */
585 /* Get the imported names table */
587 pModule
->import_table
= (int)pData
- (int)pModule
;
588 if (!READ( mz_header
.e_lfanew
+ ne_header
.iname_tab_offset
,
589 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
,
592 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
593 GlobalFree16( hModule
);
594 return (HMODULE16
)11; /* invalid exe */
596 pData
+= ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
;
598 /* Get the entry table */
600 pModule
->entry_table
= (int)pData
- (int)pModule
;
601 if (!READ( mz_header
.e_lfanew
+ ne_header
.entry_tab_offset
,
602 ne_header
.entry_tab_length
,
605 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
606 GlobalFree16( hModule
);
607 return (HMODULE16
)11; /* invalid exe */
609 pData
+= ne_header
.entry_tab_length
;
611 /* Store the filename information */
613 pModule
->fileinfo
= (int)pData
- (int)pModule
;
614 size
= sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
615 memcpy( pData
, ofs
, size
);
616 ((OFSTRUCT
*)pData
)->cBytes
= size
- 1;
619 /* Free the fast-load area */
622 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
624 /* Get the non-resident names table */
626 if (ne_header
.nrname_tab_length
)
628 pModule
->nrname_handle
= GLOBAL_Alloc( 0, ne_header
.nrname_tab_length
,
629 hModule
, FALSE
, FALSE
, FALSE
);
630 if (!pModule
->nrname_handle
)
632 GlobalFree16( hModule
);
633 return (HMODULE16
)11; /* invalid exe */
635 buffer
= GlobalLock16( pModule
->nrname_handle
);
636 _llseek16( hFile
, ne_header
.nrname_tab_offset
, SEEK_SET
);
637 if (_hread16( hFile
, buffer
, ne_header
.nrname_tab_length
)
638 != ne_header
.nrname_tab_length
)
640 GlobalFree16( pModule
->nrname_handle
);
641 GlobalFree16( hModule
);
642 return (HMODULE16
)11; /* invalid exe */
645 else pModule
->nrname_handle
= 0;
647 /* Allocate a segment for the implicitly-loaded DLLs */
649 if (pModule
->modref_count
)
651 pModule
->dlls_to_init
= GLOBAL_Alloc(GMEM_ZEROINIT
,
652 (pModule
->modref_count
+1)*sizeof(HMODULE16
),
653 hModule
, FALSE
, FALSE
, FALSE
);
654 if (!pModule
->dlls_to_init
)
656 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
657 GlobalFree16( hModule
);
658 return (HMODULE16
)11; /* invalid exe */
661 else pModule
->dlls_to_init
= 0;
663 NE_RegisterModule( pModule
);
664 if (fnSNOOP16_RegisterDLL
)
665 fnSNOOP16_RegisterDLL(pModule
,ofs
->szPathName
);
670 /***********************************************************************
673 * Load all DLLs implicitly linked to a module.
675 static BOOL32
NE_LoadDLLs( NE_MODULE
*pModule
)
678 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->modref_table
);
679 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
681 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
684 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->import_table
+ *pModRef
;
685 memcpy( buffer
, pstr
+ 1, *pstr
);
686 strcpy( buffer
+ *pstr
, ".dll" );
687 TRACE(module
, "Loading '%s'\n", buffer
);
688 if (!(*pModRef
= GetModuleHandle16( buffer
)))
690 /* If the DLL is not loaded yet, load it and store */
691 /* its handle in the list of DLLs to initialize. */
694 if ((hDLL
= NE_LoadModule( buffer
, NULL
, TRUE
, TRUE
)) == 2)
699 /* Try with prepending the path of the current module */
700 GetModuleFileName16( pModule
->self
, buffer
, sizeof(buffer
) );
701 if (!(p
= strrchr( buffer
, '\\' ))) p
= buffer
;
702 memcpy( p
+ 1, pstr
+ 1, *pstr
);
703 strcpy( p
+ 1 + *pstr
, ".dll" );
704 hDLL
= NE_LoadModule( buffer
, NULL
, TRUE
, TRUE
);
708 /* FIXME: cleanup what was done */
710 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
711 buffer
, *((BYTE
*)pModule
+ pModule
->name_table
),
712 (char *)pModule
+ pModule
->name_table
+ 1, hDLL
);
715 *pModRef
= GetExePtr( hDLL
);
718 else /* Increment the reference count of the DLL */
720 NE_MODULE
*pOldDLL
= NE_GetPtr( *pModRef
);
721 if (pOldDLL
) pOldDLL
->count
++;
728 /**********************************************************************
731 * Implementation of LoadModule16().
733 HINSTANCE16
NE_LoadModule( LPCSTR name
, HINSTANCE16
*hPrevInstance
,
734 BOOL32 implicit
, BOOL32 lib_only
)
737 HINSTANCE16 hInstance
;
742 /* Check if the module is already loaded */
744 if ((hModule
= GetModuleHandle16( name
)) != 0)
747 pModule
= NE_GetPtr( hModule
);
748 if ( pModule
->module32
) return (HINSTANCE16
)21;
750 hInstance
= NE_CreateInstance( pModule
, &prev
, lib_only
);
751 if (hInstance
!= prev
) /* not a library */
752 NE_LoadSegment( pModule
, pModule
->dgroup
);
754 if (hPrevInstance
) *hPrevInstance
= prev
;
757 if (hPrevInstance
) *hPrevInstance
= 0;
759 /* Try to load the built-in first if not disabled */
761 if ((hModule
= fnBUILTIN_LoadModule( name
, FALSE
))) return hModule
;
763 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
765 /* Now try the built-in even if disabled */
766 if ((hModule
= fnBUILTIN_LoadModule( name
, TRUE
)))
768 MSG( "Could not load Windows DLL '%s', using built-in module.\n",
772 return 2; /* File not found */
775 /* Create the module structure */
777 hModule
= NE_LoadExeHeader( hFile
, &ofs
);
779 if (hModule
< 32) return hModule
;
780 pModule
= NE_GetPtr( hModule
);
782 /* Allocate the segments for this module */
784 if (!NE_CreateSegments( pModule
) ||
785 !(hInstance
= NE_CreateInstance( pModule
, NULL
, lib_only
)))
787 GlobalFreeAll( hModule
);
788 return 8; /* Insufficient memory */
791 /* Load the referenced DLLs */
793 if (!NE_LoadDLLs( pModule
))
794 return 2; /* File not found (FIXME: free everything) */
796 /* Load the segments */
798 NE_LoadAllSegments( pModule
);
800 /* Fixup the functions prologs */
802 NE_FixupPrologs( pModule
);
804 /* Make sure the usage count is 1 on the first loading of */
805 /* the module, even if it contains circular DLL references */
809 /* Call initialization rountines for all loaded DLLs. Note that
810 * when we load implicitly linked DLLs this will be done by InitTask().
813 if (!implicit
&& (pModule
->flags
& NE_FFLAGS_LIBMODULE
))
814 NE_InitializeDLLs( hModule
);
820 /***********************************************************************
821 * LoadLibrary (KERNEL.95)
823 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
829 TRACE(module
, "(%08x) %s\n", (int)libname
, libname
);
831 /* Check for an extension */
833 if ((p
= strrchr( libname
, '.')) && !strchr( p
, '/' ) && !strchr( p
, '\\'))
835 /* An extension is present -> use the name as is */
836 return NE_LoadModule( libname
, NULL
, FALSE
, TRUE
);
839 /* Now append .dll before loading */
841 if (!(new_name
= HeapAlloc( GetProcessHeap(), 0, strlen(libname
) + 4 )))
843 strcpy( new_name
, libname
);
844 strcat( new_name
, ".dll" );
845 handle
= NE_LoadModule( new_name
, NULL
, FALSE
, TRUE
);
846 HeapFree( GetProcessHeap(), 0, new_name
);
851 /**********************************************************************
854 * Call a DLL's WEP, allowing it to shut down.
855 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
857 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
859 FARPROC16 WEP
= (FARPROC16
)0;
860 WORD ordinal
= NE_GetOrdinal( hModule
, "WEP" );
862 if (ordinal
) WEP
= NE_GetEntryPoint( hModule
, ordinal
);
865 WARN(module
, "module %04x doesn't have a WEP\n", hModule
);
868 return Callbacks
->CallWindowsExitProc( WEP
, WEP_FREE_DLL
);
872 /**********************************************************************
875 * Implementation of FreeModule16().
877 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL32 call_wep
)
879 HMODULE16
*hPrevModule
;
884 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
885 hModule
= pModule
->self
;
887 TRACE( module
, "%04x count %d\n", hModule
, pModule
->count
);
889 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
890 else pModule
->count
= 0;
892 if (pModule
->flags
& NE_FFLAGS_BUILTIN
)
893 return FALSE
; /* Can't free built-in module */
897 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
)
899 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
900 MODULE_CallWEP( hModule
);
902 /* Free the objects owned by the DLL module */
904 if (pTask
&& pTask
->userhandler
)
905 pTask
->userhandler( hModule
, USIG_DLL_UNLOAD
, 0,
906 pTask
->hInstance
, pTask
->hQueue
);
909 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
913 /* Clear magic number just in case */
915 pModule
->magic
= pModule
->self
= 0;
917 /* Remove it from the linked list */
919 hPrevModule
= &hFirstModule
;
920 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
922 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
924 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
926 /* Free the referenced modules */
928 pModRef
= (HMODULE16
*)NE_MODULE_TABLE( pModule
);
929 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
931 NE_FreeModule( *pModRef
, call_wep
);
934 /* Free the module storage */
936 GlobalFreeAll( hModule
);
938 /* Remove module from cache */
940 if (pCachedModule
== pModule
) pCachedModule
= NULL
;
945 /**********************************************************************
946 * FreeModule16 (KERNEL.46)
948 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
950 return NE_FreeModule( hModule
, TRUE
);
954 /***********************************************************************
955 * FreeLibrary16 (KERNEL.96)
957 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
959 TRACE(module
,"%04x\n", handle
);
960 FreeModule16( handle
);
964 /**********************************************************************
965 * GetModuleName (KERNEL.27)
967 BOOL16 WINAPI
GetModuleName( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
972 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
973 p
= (BYTE
*)pModule
+ pModule
->name_table
;
974 if (count
> *p
) count
= *p
+ 1;
977 memcpy( buf
, p
+ 1, count
- 1 );
984 /**********************************************************************
985 * GetModuleUsage (KERNEL.48)
987 INT16 WINAPI
GetModuleUsage( HINSTANCE16 hModule
)
989 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
990 return pModule
? pModule
->count
: 0;
994 /**********************************************************************
995 * GetExpWinVer (KERNEL.167)
997 WORD WINAPI
GetExpWinVer( HMODULE16 hModule
)
999 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1000 return pModule
? pModule
->expected_version
: 0;
1004 /**********************************************************************
1005 * GetModuleFileName16 (KERNEL.49)
1007 INT16 WINAPI
GetModuleFileName16( HINSTANCE16 hModule
, LPSTR lpFileName
,
1012 if (!hModule
) hModule
= GetCurrentTask();
1013 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
1014 lstrcpyn32A( lpFileName
, NE_MODULE_NAME(pModule
), nSize
);
1015 TRACE(module
, "%s\n", lpFileName
);
1016 return strlen(lpFileName
);
1020 /**********************************************************************
1021 * GetModuleHandle16 (KERNEL.47)
1023 * Find a module from a path name.
1027 * the win16 module handle if found
1029 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1030 * Always hFirstModule
1032 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1034 if (HIWORD(name
) == 0)
1035 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1036 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name
)), hFirstModule
);
1039 HMODULE16 WINAPI
GetModuleHandle16( LPCSTR name
)
1041 HMODULE16 hModule
= hFirstModule
;
1042 LPCSTR filename
, dotptr
, modulepath
, modulename
;
1043 BYTE len
, *name_table
;
1045 if (!(filename
= strrchr( name
, '\\' ))) filename
= name
;
1047 if ((dotptr
= strrchr( filename
, '.' )) != NULL
)
1048 len
= (BYTE
)(dotptr
- filename
);
1049 else len
= strlen( filename
);
1053 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1054 if (!pModule
) break;
1055 modulepath
= NE_MODULE_NAME(pModule
);
1056 if (!(modulename
= strrchr( modulepath
, '\\' )))
1057 modulename
= modulepath
;
1059 if (!lstrcmpi32A( modulename
, filename
)) return hModule
;
1061 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1062 if ((*name_table
== len
) && !lstrncmpi32A(filename
, name_table
+1, len
))
1064 hModule
= pModule
->next
;
1070 /**********************************************************************
1071 * ModuleFirst (TOOLHELP.59)
1073 BOOL16 WINAPI
ModuleFirst( MODULEENTRY
*lpme
)
1075 lpme
->wNext
= hFirstModule
;
1076 return ModuleNext( lpme
);
1080 /**********************************************************************
1081 * ModuleNext (TOOLHELP.60)
1083 BOOL16 WINAPI
ModuleNext( MODULEENTRY
*lpme
)
1088 if (!lpme
->wNext
) return FALSE
;
1089 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1090 name
= (char *)pModule
+ pModule
->name_table
;
1091 memcpy( lpme
->szModule
, name
+ 1, *name
);
1092 lpme
->szModule
[(BYTE
)*name
] = '\0';
1093 lpme
->hModule
= lpme
->wNext
;
1094 lpme
->wcUsage
= pModule
->count
;
1095 strncpy( lpme
->szExePath
, NE_MODULE_NAME(pModule
), MAX_PATH
);
1096 lpme
->szExePath
[MAX_PATH
] = '\0';
1097 lpme
->wNext
= pModule
->next
;
1102 /**********************************************************************
1103 * ModuleFindName (TOOLHELP.61)
1105 BOOL16 WINAPI
ModuleFindName( MODULEENTRY
*lpme
, LPCSTR name
)
1107 lpme
->wNext
= GetModuleHandle16( name
);
1108 return ModuleNext( lpme
);
1112 /**********************************************************************
1113 * ModuleFindHandle (TOOLHELP.62)
1115 BOOL16 WINAPI
ModuleFindHandle( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1117 hModule
= GetExePtr( hModule
);
1118 lpme
->wNext
= hModule
;
1119 return ModuleNext( lpme
);
1122 /***************************************************************************
1123 * MapHModuleLS (KERNEL32.520)
1125 HMODULE16 WINAPI
MapHModuleLS(HMODULE32 hmod
) {
1129 return ((TDB
*)GlobalLock16(GetCurrentTask()))->hInstance
;
1131 return hmod
; /* we already have a 16 bit module handle */
1132 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
1134 if (pModule
->module32
== hmod
)
1135 return pModule
->self
;
1136 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
1141 /***************************************************************************
1142 * MapHModuleSL (KERNEL32.521)
1144 HMODULE32 WINAPI
MapHModuleSL(HMODULE16 hmod
) {
1148 TDB
*pTask
= (TDB
*)GlobalLock16(GetCurrentTask());
1150 hmod
= pTask
->hInstance
;
1152 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
1153 if ( (pModule
->magic
!=IMAGE_OS2_SIGNATURE
) ||
1154 !(pModule
->flags
& NE_FFLAGS_WIN32
)
1157 return pModule
->module32
;
1160 /***************************************************************************
1161 * MapHInstLS (KERNEL32.516)
1163 REGS_ENTRYPOINT(MapHInstLS
) {
1164 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1167 /***************************************************************************
1168 * MapHInstLS (KERNEL32.518)
1170 REGS_ENTRYPOINT(MapHInstSL
) {
1171 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));
1174 /***************************************************************************
1175 * WIN16_MapHInstLS (KERNEL.472)
1177 VOID WINAPI
WIN16_MapHInstLS( CONTEXT
*context
) {
1178 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1181 /***************************************************************************
1182 * WIN16_MapHInstSL (KERNEL.473)
1184 VOID WINAPI
WIN16_MapHInstSL( CONTEXT
*context
) {
1185 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));