4 * Copyright 1995 Alexandre Julliard
24 static HMODULE16 hFirstModule
= 0;
25 static NE_MODULE
*pCachedModule
= 0; /* Module cached by NE_OpenFile */
27 static HMODULE16
NE_LoadBuiltin(LPCSTR name
,BOOL32 force
) { return 0; }
28 HMODULE16 (*fnBUILTIN_LoadModule
)(LPCSTR name
,BOOL32 force
) = NE_LoadBuiltin
;
31 /***********************************************************************
34 NE_MODULE
*NE_GetPtr( HMODULE16 hModule
)
36 return (NE_MODULE
*)GlobalLock16( GetExePtr(hModule
) );
40 /***********************************************************************
43 void NE_DumpModule( HMODULE16 hModule
)
51 if (!(pModule
= NE_GetPtr( hModule
)))
53 MSG( "**** %04x is not a module handle\n", hModule
);
57 /* Dump the module info */
59 DUMP( "Module %04x:\n", hModule
);
60 DUMP( "count=%d flags=%04x heap=%d stack=%d\n",
61 pModule
->count
, pModule
->flags
,
62 pModule
->heap_size
, pModule
->stack_size
);
63 DUMP( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
64 pModule
->cs
, pModule
->ip
, pModule
->ss
, pModule
->sp
, pModule
->dgroup
,
65 pModule
->seg_count
, pModule
->modref_count
);
66 DUMP( "os_flags=%d swap_area=%d version=%04x\n",
67 pModule
->os_flags
, pModule
->min_swap_area
,
68 pModule
->expected_version
);
69 if (pModule
->flags
& NE_FFLAGS_WIN32
)
70 DUMP( "PE module=%08x\n", pModule
->module32
);
72 /* Dump the file info */
74 DUMP( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
76 /* Dump the segment table */
78 DUMP( "Segment table:\n" );
79 pSeg
= NE_SEG_TABLE( pModule
);
80 for (i
= 0; i
< pModule
->seg_count
; i
++, pSeg
++)
81 DUMP( "%02x: pos=%d size=%d flags=%04x minsize=%d sel=%04x\n",
82 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
83 pSeg
->minsize
, pSeg
->selector
);
85 /* Dump the resource table */
87 DUMP( "Resource table:\n" );
88 if (pModule
->res_table
)
90 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->res_table
);
91 DUMP( "Alignment: %d\n", *pword
++ );
94 struct resource_typeinfo_s
*ptr
= (struct resource_typeinfo_s
*)pword
;
95 struct resource_nameinfo_s
*pname
= (struct resource_nameinfo_s
*)(ptr
+ 1);
96 DUMP( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
97 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
98 DUMP( "offset=%d len=%d id=%04x\n",
99 pname
->offset
, pname
->length
, pname
->id
);
100 pword
= (WORD
*)pname
;
103 else DUMP( "None\n" );
105 /* Dump the resident name table */
107 DUMP( "Resident-name table:\n" );
108 pstr
= (char *)pModule
+ pModule
->name_table
;
111 DUMP( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
112 *(WORD
*)(pstr
+ *pstr
+ 1) );
113 pstr
+= *pstr
+ 1 + sizeof(WORD
);
116 /* Dump the module reference table */
118 DUMP( "Module ref table:\n" );
119 if (pModule
->modref_table
)
121 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->modref_table
);
122 for (i
= 0; i
< pModule
->modref_count
; i
++, pword
++)
125 GetModuleName( *pword
, name
, sizeof(name
) );
126 DUMP( "%d: %04x -> '%s'\n", i
, *pword
, name
);
129 else DUMP( "None\n" );
131 /* Dump the entry table */
133 DUMP( "Entry table:\n" );
134 pstr
= (char *)pModule
+ pModule
->entry_table
;
138 DUMP( "Bundle %d-%d: %02x\n", ordinal
, ordinal
+ *pstr
- 1, pstr
[1]);
144 else if ((BYTE
)pstr
[1] == 0xff) /* moveable */
150 DUMP( "%d: %02x:%04x (moveable)\n",
151 ordinal
++, pstr
[3], *(WORD
*)(pstr
+ 4) );
161 DUMP( "%d: %04x (fixed)\n",
162 ordinal
++, *(WORD
*)(pstr
+ 1) );
168 /* Dump the non-resident names table */
170 DUMP( "Non-resident names table:\n" );
171 if (pModule
->nrname_handle
)
173 pstr
= (char *)GlobalLock16( pModule
->nrname_handle
);
176 DUMP( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
177 *(WORD
*)(pstr
+ *pstr
+ 1) );
178 pstr
+= *pstr
+ 1 + sizeof(WORD
);
185 /***********************************************************************
188 * Walk the module list and print the modules.
190 void NE_WalkModules(void)
192 HMODULE16 hModule
= hFirstModule
;
193 MSG( "Module Flags Name\n" );
196 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
199 MSG( "Bad module %04x in list\n", hModule
);
202 MSG( " %04x %04x %.*s\n", hModule
, pModule
->flags
,
203 *((char *)pModule
+ pModule
->name_table
),
204 (char *)pModule
+ pModule
->name_table
+ 1 );
205 hModule
= pModule
->next
;
210 /**********************************************************************
213 void NE_RegisterModule( NE_MODULE
*pModule
)
215 pModule
->next
= hFirstModule
;
216 hFirstModule
= pModule
->self
;
220 /***********************************************************************
223 * Lookup the ordinal for a given name.
225 WORD
NE_GetOrdinal( HMODULE16 hModule
, const char *name
)
227 unsigned char buffer
[256], *cpnt
;
231 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
232 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
234 TRACE( module
, "(%04x,'%s')\n", hModule
, name
);
236 /* First handle names of the form '#xxxx' */
238 if (name
[0] == '#') return atoi( name
+ 1 );
240 /* Now copy and uppercase the string */
242 strcpy( buffer
, name
);
243 CharUpper32A( buffer
);
244 len
= strlen( buffer
);
246 /* First search the resident names */
248 cpnt
= (char *)pModule
+ pModule
->name_table
;
250 /* Skip the first entry (module name) */
251 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
254 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
256 TRACE(module
, " Found: ordinal=%d\n",
257 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
258 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
260 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
263 /* Now search the non-resident names table */
265 if (!pModule
->nrname_handle
) return 0; /* No non-resident table */
266 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
268 /* Skip the first entry (module description string) */
269 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
272 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
274 TRACE(module
, " Found: ordinal=%d\n",
275 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
276 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
278 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
284 /***********************************************************************
285 * NE_GetEntryPoint (WPROCS.27)
287 * Return the entry point for a given ordinal.
289 FARPROC16
NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
296 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
297 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
299 p
= (BYTE
*)pModule
+ pModule
->entry_table
;
300 while (*p
&& (curOrdinal
+ *p
<= ordinal
))
302 /* Skipping this bundle */
306 case 0: p
+= 2; break; /* unused */
307 case 0xff: p
+= 2 + *p
* 6; break; /* moveable */
308 default: p
+= 2 + *p
* 3; break; /* fixed */
317 case 0xff: /* moveable */
318 p
+= 2 + 6 * (ordinal
- curOrdinal
);
320 offset
= *(WORD
*)(p
+ 4);
324 p
+= 2 + 3 * (ordinal
- curOrdinal
);
325 offset
= *(WORD
*)(p
+ 1);
329 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
330 else sel
= (WORD
)(DWORD
)NE_SEG_TABLE(pModule
)[sel
-1].selector
;
332 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
334 return (FARPROC16
)SNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
));
338 /***********************************************************************
341 * Change the value of an entry point. Use with caution!
342 * It can only change the offset value, not the selector.
344 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
350 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
351 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
353 p
= (BYTE
*)pModule
+ pModule
->entry_table
;
354 while (*p
&& (curOrdinal
+ *p
<= ordinal
))
356 /* Skipping this bundle */
360 case 0: p
+= 2; break; /* unused */
361 case 0xff: p
+= 2 + *p
* 6; break; /* moveable */
362 default: p
+= 2 + *p
* 3; break; /* fixed */
365 if (!*p
) return FALSE
;
371 case 0xff: /* moveable */
372 p
+= 2 + 6 * (ordinal
- curOrdinal
);
373 *(WORD
*)(p
+ 4) = offset
;
376 p
+= 2 + 3 * (ordinal
- curOrdinal
);
377 *(WORD
*)(p
+ 1) = offset
;
384 /***********************************************************************
387 int NE_OpenFile( NE_MODULE
*pModule
)
389 DOS_FULL_NAME full_name
;
392 static int cachedfd
= -1;
394 TRACE( module
, "(%p) cache: mod=%p fd=%d\n",
395 pModule
, pCachedModule
, cachedfd
);
396 if (pCachedModule
== pModule
) return cachedfd
;
398 pCachedModule
= pModule
;
399 name
= NE_MODULE_NAME( pModule
);
400 if (!DOSFS_GetFullName( name
, TRUE
, &full_name
) ||
401 (cachedfd
= open( full_name
.long_name
, O_RDONLY
)) == -1)
402 MSG( "Can't open file '%s' for module %04x\n", name
, pModule
->self
);
403 TRACE(module
, "opened '%s' -> %d\n",
409 /***********************************************************************
412 static HMODULE16
NE_LoadExeHeader( HFILE16 hFile
, OFSTRUCT
*ofs
)
414 IMAGE_DOS_HEADER mz_header
;
415 IMAGE_OS2_HEADER ne_header
;
420 char *buffer
, *fastload
= NULL
;
421 int fastload_offset
= 0, fastload_length
= 0;
423 /* Read a block from either the file or the fast-load area. */
424 #define READ(offset,size,buffer) \
425 ((fastload && ((offset) >= fastload_offset) && \
426 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
427 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
428 (_llseek16( hFile, (offset), SEEK_SET), \
429 _hread16( hFile, (buffer), (size) ) == (size)))
431 _llseek16( hFile
, 0, SEEK_SET
);
432 if ((_hread16(hFile
,&mz_header
,sizeof(mz_header
)) != sizeof(mz_header
)) ||
433 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
434 return (HMODULE16
)11; /* invalid exe */
436 _llseek16( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
437 if (_hread16( hFile
, &ne_header
, sizeof(ne_header
) ) != sizeof(ne_header
))
438 return (HMODULE16
)11; /* invalid exe */
440 if (ne_header
.ne_magic
== IMAGE_NT_SIGNATURE
) return (HMODULE16
)21; /* win32 exe */
441 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return (HMODULE16
)11; /* invalid exe */
443 if (ne_header
.ne_magic
== IMAGE_OS2_SIGNATURE_LX
) {
444 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
445 return (HMODULE16
)12;
448 /* We now have a valid NE header */
450 size
= sizeof(NE_MODULE
) +
452 ne_header
.n_segment_tab
* sizeof(SEGTABLEENTRY
) +
454 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
+
455 /* resident names table */
456 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
+
457 /* module ref table */
458 ne_header
.n_mod_ref_tab
* sizeof(WORD
) +
459 /* imported names table */
460 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
+
461 /* entry table length */
462 ne_header
.entry_tab_length
+
463 /* loaded file info */
464 sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
466 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
467 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
468 FarSetOwner( hModule
, hModule
);
469 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
470 memcpy( pModule
, &ne_header
, sizeof(ne_header
) );
472 pModule
->module32
= 0;
473 pModule
->self
= hModule
;
474 pModule
->self_loading_sel
= 0;
475 pData
= (BYTE
*)(pModule
+ 1);
477 /* Clear internal Wine flags in case they are set in the EXE file */
479 pModule
->flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
481 /* Read the fast-load area */
483 if (ne_header
.additional_flags
& NE_AFLAGS_FASTLOAD
)
485 fastload_offset
=ne_header
.fastload_offset
<<ne_header
.align_shift_count
;
486 fastload_length
=ne_header
.fastload_length
<<ne_header
.align_shift_count
;
487 TRACE(module
, "Using fast-load area offset=%x len=%d\n",
488 fastload_offset
, fastload_length
);
489 if ((fastload
= HeapAlloc( SystemHeap
, 0, fastload_length
)) != NULL
)
491 _llseek16( hFile
, fastload_offset
, SEEK_SET
);
492 if (_hread16(hFile
, fastload
, fastload_length
) != fastload_length
)
494 HeapFree( SystemHeap
, 0, fastload
);
495 WARN( module
, "Error reading fast-load area!\n");
501 /* Get the segment table */
503 pModule
->seg_table
= (int)pData
- (int)pModule
;
504 buffer
= HeapAlloc( SystemHeap
, 0, ne_header
.n_segment_tab
*
505 sizeof(struct ne_segment_table_entry_s
));
509 struct ne_segment_table_entry_s
*pSeg
;
511 if (!READ( mz_header
.e_lfanew
+ ne_header
.segment_tab_offset
,
512 ne_header
.n_segment_tab
* sizeof(struct ne_segment_table_entry_s
),
515 HeapFree( SystemHeap
, 0, buffer
);
516 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
517 GlobalFree16( hModule
);
518 return (HMODULE16
)11; /* invalid exe */
520 pSeg
= (struct ne_segment_table_entry_s
*)buffer
;
521 for (i
= ne_header
.n_segment_tab
; i
> 0; i
--, pSeg
++)
523 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
524 pData
+= sizeof(SEGTABLEENTRY
);
526 HeapFree( SystemHeap
, 0, buffer
);
530 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
531 GlobalFree16( hModule
);
532 return (HMODULE16
)11; /* invalid exe */
535 /* Get the resource table */
537 if (ne_header
.resource_tab_offset
< ne_header
.rname_tab_offset
)
539 pModule
->res_table
= (int)pData
- (int)pModule
;
540 if (!READ(mz_header
.e_lfanew
+ ne_header
.resource_tab_offset
,
541 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
,
542 pData
)) return (HMODULE16
)11; /* invalid exe */
543 pData
+= ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
;
544 NE_InitResourceHandler( hModule
);
546 else pModule
->res_table
= 0; /* No resource table */
548 /* Get the resident names table */
550 pModule
->name_table
= (int)pData
- (int)pModule
;
551 if (!READ( mz_header
.e_lfanew
+ ne_header
.rname_tab_offset
,
552 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
,
555 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
556 GlobalFree16( hModule
);
557 return (HMODULE16
)11; /* invalid exe */
559 pData
+= ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
;
561 /* Get the module references table */
563 if (ne_header
.n_mod_ref_tab
> 0)
565 pModule
->modref_table
= (int)pData
- (int)pModule
;
566 if (!READ( mz_header
.e_lfanew
+ ne_header
.moduleref_tab_offset
,
567 ne_header
.n_mod_ref_tab
* sizeof(WORD
),
570 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
571 GlobalFree16( hModule
);
572 return (HMODULE16
)11; /* invalid exe */
574 pData
+= ne_header
.n_mod_ref_tab
* sizeof(WORD
);
576 else pModule
->modref_table
= 0; /* No module references */
578 /* Get the imported names table */
580 pModule
->import_table
= (int)pData
- (int)pModule
;
581 if (!READ( mz_header
.e_lfanew
+ ne_header
.iname_tab_offset
,
582 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
,
585 if (fastload
) 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 /* Get the entry table */
593 pModule
->entry_table
= (int)pData
- (int)pModule
;
594 if (!READ( mz_header
.e_lfanew
+ ne_header
.entry_tab_offset
,
595 ne_header
.entry_tab_length
,
598 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
599 GlobalFree16( hModule
);
600 return (HMODULE16
)11; /* invalid exe */
602 pData
+= ne_header
.entry_tab_length
;
604 /* Store the filename information */
606 pModule
->fileinfo
= (int)pData
- (int)pModule
;
607 size
= sizeof(OFSTRUCT
)-sizeof(ofs
->szPathName
)+strlen(ofs
->szPathName
)+1;
608 memcpy( pData
, ofs
, size
);
609 ((OFSTRUCT
*)pData
)->cBytes
= size
- 1;
612 /* Free the fast-load area */
615 if (fastload
) HeapFree( SystemHeap
, 0, fastload
);
617 /* Get the non-resident names table */
619 if (ne_header
.nrname_tab_length
)
621 pModule
->nrname_handle
= GLOBAL_Alloc( 0, ne_header
.nrname_tab_length
,
622 hModule
, FALSE
, FALSE
, FALSE
);
623 if (!pModule
->nrname_handle
)
625 GlobalFree16( hModule
);
626 return (HMODULE16
)11; /* invalid exe */
628 buffer
= GlobalLock16( pModule
->nrname_handle
);
629 _llseek16( hFile
, ne_header
.nrname_tab_offset
, SEEK_SET
);
630 if (_hread16( hFile
, buffer
, ne_header
.nrname_tab_length
)
631 != ne_header
.nrname_tab_length
)
633 GlobalFree16( pModule
->nrname_handle
);
634 GlobalFree16( hModule
);
635 return (HMODULE16
)11; /* invalid exe */
638 else pModule
->nrname_handle
= 0;
640 /* Allocate a segment for the implicitly-loaded DLLs */
642 if (pModule
->modref_count
)
644 pModule
->dlls_to_init
= GLOBAL_Alloc(GMEM_ZEROINIT
,
645 (pModule
->modref_count
+1)*sizeof(HMODULE16
),
646 hModule
, FALSE
, FALSE
, FALSE
);
647 if (!pModule
->dlls_to_init
)
649 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
650 GlobalFree16( hModule
);
651 return (HMODULE16
)11; /* invalid exe */
654 else pModule
->dlls_to_init
= 0;
656 NE_RegisterModule( pModule
);
657 SNOOP16_RegisterDLL(pModule
,ofs
->szPathName
);
662 /***********************************************************************
665 * Load all DLLs implicitly linked to a module.
667 static BOOL32
NE_LoadDLLs( NE_MODULE
*pModule
)
670 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->modref_table
);
671 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
673 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
676 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->import_table
+ *pModRef
;
677 memcpy( buffer
, pstr
+ 1, *pstr
);
678 strcpy( buffer
+ *pstr
, ".dll" );
679 TRACE(module
, "Loading '%s'\n", buffer
);
680 if (!(*pModRef
= GetModuleHandle16( buffer
)))
682 /* If the DLL is not loaded yet, load it and store */
683 /* its handle in the list of DLLs to initialize. */
686 if ((hDLL
= NE_LoadModule( buffer
, NULL
, TRUE
, TRUE
)) == 2)
691 /* Try with prepending the path of the current module */
692 GetModuleFileName16( pModule
->self
, buffer
, sizeof(buffer
) );
693 if (!(p
= strrchr( buffer
, '\\' ))) p
= buffer
;
694 memcpy( p
+ 1, pstr
+ 1, *pstr
);
695 strcpy( p
+ 1 + *pstr
, ".dll" );
696 hDLL
= NE_LoadModule( buffer
, NULL
, TRUE
, TRUE
);
700 /* FIXME: cleanup what was done */
702 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
703 buffer
, *((BYTE
*)pModule
+ pModule
->name_table
),
704 (char *)pModule
+ pModule
->name_table
+ 1, hDLL
);
707 *pModRef
= GetExePtr( hDLL
);
710 else /* Increment the reference count of the DLL */
712 NE_MODULE
*pOldDLL
= NE_GetPtr( *pModRef
);
713 if (pOldDLL
) pOldDLL
->count
++;
720 /**********************************************************************
723 * Implementation of LoadModule16().
725 HINSTANCE16
NE_LoadModule( LPCSTR name
, HINSTANCE16
*hPrevInstance
,
726 BOOL32 implicit
, BOOL32 lib_only
)
729 HINSTANCE16 hInstance
;
734 /* Check if the module is already loaded */
736 if ((hModule
= GetModuleHandle16( name
)) != 0)
739 pModule
= NE_GetPtr( hModule
);
740 hInstance
= NE_CreateInstance( pModule
, &prev
, lib_only
);
741 if (hInstance
!= prev
) /* not a library */
742 NE_LoadSegment( pModule
, pModule
->dgroup
);
744 if (hPrevInstance
) *hPrevInstance
= prev
;
747 if (hPrevInstance
) *hPrevInstance
= 0;
749 /* Try to load the built-in first if not disabled */
751 if ((hModule
= fnBUILTIN_LoadModule( name
, FALSE
))) return hModule
;
753 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
755 /* Now try the built-in even if disabled */
756 if ((hModule
= fnBUILTIN_LoadModule( name
, TRUE
)))
758 MSG( "Could not load Windows DLL '%s', using built-in module.\n",
762 return 2; /* File not found */
765 /* Create the module structure */
767 hModule
= NE_LoadExeHeader( hFile
, &ofs
);
769 if (hModule
< 32) return hModule
;
770 pModule
= NE_GetPtr( hModule
);
772 /* Allocate the segments for this module */
774 if (!NE_CreateSegments( pModule
) ||
775 !(hInstance
= NE_CreateInstance( pModule
, NULL
, lib_only
)))
777 GlobalFreeAll( hModule
);
778 return 8; /* Insufficient memory */
781 /* Load the referenced DLLs */
783 if (!NE_LoadDLLs( pModule
))
784 return 2; /* File not found (FIXME: free everything) */
786 /* Load the segments */
788 NE_LoadAllSegments( pModule
);
790 /* Fixup the functions prologs */
792 NE_FixupPrologs( pModule
);
794 /* Make sure the usage count is 1 on the first loading of */
795 /* the module, even if it contains circular DLL references */
799 /* Call initialization rountines for all loaded DLLs. Note that
800 * when we load implicitly linked DLLs this will be done by InitTask().
803 if (!implicit
&& (pModule
->flags
& NE_FFLAGS_LIBMODULE
))
804 NE_InitializeDLLs( hModule
);
810 /***********************************************************************
811 * LoadLibrary (KERNEL.95)
813 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
819 TRACE(module
, "(%08x) %s\n", (int)libname
, libname
);
821 /* Check for an extension */
823 if ((p
= strrchr( libname
, '.')) && !strchr( p
, '/' ) && !strchr( p
, '\\'))
825 /* An extension is present -> use the name as is */
826 return NE_LoadModule( libname
, NULL
, FALSE
, TRUE
);
829 /* Now append .dll before loading */
831 if (!(new_name
= HeapAlloc( GetProcessHeap(), 0, strlen(libname
) + 4 )))
833 strcpy( new_name
, libname
);
834 strcat( new_name
, ".dll" );
835 handle
= NE_LoadModule( new_name
, NULL
, FALSE
, TRUE
);
836 HeapFree( GetProcessHeap(), 0, new_name
);
841 /**********************************************************************
844 * Call a DLL's WEP, allowing it to shut down.
845 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
847 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
849 FARPROC16 WEP
= (FARPROC16
)0;
850 WORD ordinal
= NE_GetOrdinal( hModule
, "WEP" );
852 if (ordinal
) WEP
= NE_GetEntryPoint( hModule
, ordinal
);
855 WARN(module
, "module %04x doesn't have a WEP\n", hModule
);
858 return Callbacks
->CallWindowsExitProc( WEP
, WEP_FREE_DLL
);
862 /**********************************************************************
865 * Implementation of FreeModule16().
867 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL32 call_wep
)
869 HMODULE16
*hPrevModule
;
874 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
875 hModule
= pModule
->self
;
877 TRACE( module
, "%04x count %d\n", hModule
, pModule
->count
);
879 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
880 else pModule
->count
= 0;
882 if (pModule
->flags
& NE_FFLAGS_BUILTIN
)
883 return FALSE
; /* Can't free built-in module */
887 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
)
889 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
890 MODULE_CallWEP( hModule
);
892 /* Free the objects owned by the DLL module */
894 if (pTask
&& pTask
->userhandler
)
895 pTask
->userhandler( hModule
, USIG_DLL_UNLOAD
, 0,
896 pTask
->hInstance
, pTask
->hQueue
);
899 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
903 /* Clear magic number just in case */
905 pModule
->magic
= pModule
->self
= 0;
907 /* Remove it from the linked list */
909 hPrevModule
= &hFirstModule
;
910 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
912 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
914 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
916 /* Free the referenced modules */
918 pModRef
= (HMODULE16
*)NE_MODULE_TABLE( pModule
);
919 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
921 NE_FreeModule( *pModRef
, call_wep
);
924 /* Free the module storage */
926 GlobalFreeAll( hModule
);
928 /* Remove module from cache */
930 if (pCachedModule
== pModule
) pCachedModule
= NULL
;
935 /**********************************************************************
936 * FreeModule16 (KERNEL.46)
938 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
940 return NE_FreeModule( hModule
, TRUE
);
944 /***********************************************************************
945 * FreeLibrary16 (KERNEL.96)
947 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
949 TRACE(module
,"%04x\n", handle
);
950 FreeModule16( handle
);
954 /**********************************************************************
955 * GetModuleName (KERNEL.27)
957 BOOL16 WINAPI
GetModuleName( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
962 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
963 p
= (BYTE
*)pModule
+ pModule
->name_table
;
964 if (count
> *p
) count
= *p
+ 1;
967 memcpy( buf
, p
+ 1, count
- 1 );
974 /**********************************************************************
975 * GetModuleUsage (KERNEL.48)
977 INT16 WINAPI
GetModuleUsage( HINSTANCE16 hModule
)
979 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
980 return pModule
? pModule
->count
: 0;
984 /**********************************************************************
985 * GetExpWinVer (KERNEL.167)
987 WORD WINAPI
GetExpWinVer( HMODULE16 hModule
)
989 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
990 return pModule
? pModule
->expected_version
: 0;
994 /**********************************************************************
995 * GetModuleFileName16 (KERNEL.49)
997 INT16 WINAPI
GetModuleFileName16( HINSTANCE16 hModule
, LPSTR lpFileName
,
1002 if (!hModule
) hModule
= GetCurrentTask();
1003 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
1004 lstrcpyn32A( lpFileName
, NE_MODULE_NAME(pModule
), nSize
);
1005 TRACE(module
, "%s\n", lpFileName
);
1006 return strlen(lpFileName
);
1010 /**********************************************************************
1011 * GetModuleHandle16 (KERNEL.47)
1013 * Find a module from a path name.
1016 * the win16 module handle if found
1019 HMODULE16 WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1021 if (HIWORD(name
) == 0) return GetExePtr( (HINSTANCE16
)name
);
1022 return GetModuleHandle16( PTR_SEG_TO_LIN(name
) );
1025 HMODULE16 WINAPI
GetModuleHandle16( LPCSTR name
)
1027 HMODULE16 hModule
= hFirstModule
;
1028 LPCSTR filename
, dotptr
, modulepath
, modulename
;
1029 BYTE len
, *name_table
;
1031 if (!(filename
= strrchr( name
, '\\' ))) filename
= name
;
1033 if ((dotptr
= strrchr( filename
, '.' )) != NULL
)
1034 len
= (BYTE
)(dotptr
- filename
);
1035 else len
= strlen( filename
);
1039 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1040 if (!pModule
) break;
1041 modulepath
= NE_MODULE_NAME(pModule
);
1042 if (!(modulename
= strrchr( modulepath
, '\\' )))
1043 modulename
= modulepath
;
1045 if (!lstrcmpi32A( modulename
, filename
)) return hModule
;
1047 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1048 if ((*name_table
== len
) && !lstrncmpi32A(filename
, name_table
+1, len
))
1050 hModule
= pModule
->next
;
1056 /**********************************************************************
1057 * ModuleFirst (TOOLHELP.59)
1059 BOOL16 WINAPI
ModuleFirst( MODULEENTRY
*lpme
)
1061 lpme
->wNext
= hFirstModule
;
1062 return ModuleNext( lpme
);
1066 /**********************************************************************
1067 * ModuleNext (TOOLHELP.60)
1069 BOOL16 WINAPI
ModuleNext( MODULEENTRY
*lpme
)
1074 if (!lpme
->wNext
) return FALSE
;
1075 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1076 name
= (char *)pModule
+ pModule
->name_table
;
1077 memcpy( lpme
->szModule
, name
+ 1, *name
);
1078 lpme
->szModule
[(BYTE
)*name
] = '\0';
1079 lpme
->hModule
= lpme
->wNext
;
1080 lpme
->wcUsage
= pModule
->count
;
1081 strncpy( lpme
->szExePath
, NE_MODULE_NAME(pModule
), MAX_PATH
);
1082 lpme
->szExePath
[MAX_PATH
] = '\0';
1083 lpme
->wNext
= pModule
->next
;
1088 /**********************************************************************
1089 * ModuleFindName (TOOLHELP.61)
1091 BOOL16 WINAPI
ModuleFindName( MODULEENTRY
*lpme
, LPCSTR name
)
1093 lpme
->wNext
= GetModuleHandle16( name
);
1094 return ModuleNext( lpme
);
1098 /**********************************************************************
1099 * ModuleFindHandle (TOOLHELP.62)
1101 BOOL16 WINAPI
ModuleFindHandle( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1103 hModule
= GetExePtr( hModule
);
1104 lpme
->wNext
= hModule
;
1105 return ModuleNext( lpme
);