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 /* check *programs* for default minimal stack size */
484 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
485 && (pModule->stack_size < 0x1400) )
486 pModule->stack_size = 0x1400;
487 pModule->module32 = 0;
488 pModule->self = hModule;
489 pModule->self_loading_sel = 0;
490 pData = (BYTE *)(pModule + 1);
492 /* Clear internal Wine flags in case they are set in the EXE file */
494 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
496 /* Read the fast-load area */
498 if (ne_header.additional_flags & NE_AFLAGS_FASTLOAD)
500 fastload_offset=ne_header.fastload_offset<<ne_header.align_shift_count;
501 fastload_length=ne_header.fastload_length<<ne_header.align_shift_count;
502 TRACE(module, "Using fast-load area offset=%x len=%d\n",
503 fastload_offset, fastload_length );
504 if ((fastload = HeapAlloc( SystemHeap, 0, fastload_length )) != NULL)
506 _llseek16( hFile, fastload_offset, SEEK_SET);
507 if (_hread16(hFile, fastload, fastload_length) != fastload_length)
509 HeapFree( SystemHeap, 0, fastload );
510 WARN( module, "Error reading fast-load area!\n");
516 /* Get the segment table */
518 pModule->seg_table = (int)pData - (int)pModule;
519 buffer = HeapAlloc( SystemHeap, 0, ne_header.n_segment_tab *
520 sizeof(struct ne_segment_table_entry_s));
524 struct ne_segment_table_entry_s *pSeg;
526 if (!READ( mz_header.e_lfanew + ne_header.segment_tab_offset,
527 ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s),
530 HeapFree( SystemHeap, 0, buffer );
531 if (fastload) HeapFree( SystemHeap, 0, fastload );
532 GlobalFree16( hModule );
533 return (HMODULE16)11; /* invalid exe */
535 pSeg = (struct ne_segment_table_entry_s *)buffer;
536 for (i = ne_header.n_segment_tab; i > 0; i--, pSeg++)
538 memcpy( pData, pSeg, sizeof(*pSeg) );
539 pData += sizeof(SEGTABLEENTRY);
541 HeapFree( SystemHeap, 0, buffer );
545 if (fastload) HeapFree( SystemHeap, 0, fastload );
546 GlobalFree16( hModule );
547 return (HMODULE16)11; /* invalid exe */
550 /* Get the resource table */
552 if (ne_header.resource_tab_offset < ne_header.rname_tab_offset)
554 pModule->res_table = (int)pData - (int)pModule;
555 if (!READ(mz_header.e_lfanew + ne_header.resource_tab_offset,
556 ne_header.rname_tab_offset - ne_header.resource_tab_offset,
557 pData )) return (HMODULE16)11; /* invalid exe */
558 pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
559 NE_InitResourceHandler( hModule );
561 else pModule->res_table = 0; /* No resource table */
563 /* Get the resident names table */
565 pModule->name_table = (int)pData - (int)pModule;
566 if (!READ( mz_header.e_lfanew + ne_header.rname_tab_offset,
567 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset,
570 if (fastload) HeapFree( SystemHeap, 0, fastload );
571 GlobalFree16( hModule );
572 return (HMODULE16)11; /* invalid exe */
574 pData += ne_header.moduleref_tab_offset - ne_header.rname_tab_offset;
576 /* Get the module references table */
578 if (ne_header.n_mod_ref_tab > 0)
580 pModule->modref_table = (int)pData - (int)pModule;
581 if (!READ( mz_header.e_lfanew + ne_header.moduleref_tab_offset,
582 ne_header.n_mod_ref_tab * sizeof(WORD),
585 if (fastload) HeapFree( SystemHeap, 0, fastload );
586 GlobalFree16( hModule );
587 return (HMODULE16)11; /* invalid exe */
589 pData += ne_header.n_mod_ref_tab * sizeof(WORD);
591 else pModule->modref_table = 0; /* No module references */
593 /* Get the imported names table */
595 pModule->import_table = (int)pData - (int)pModule;
596 if (!READ( mz_header.e_lfanew + ne_header.iname_tab_offset,
597 ne_header.entry_tab_offset - ne_header.iname_tab_offset,
600 if (fastload) HeapFree( SystemHeap, 0, fastload );
601 GlobalFree16( hModule );
602 return (HMODULE16)11; /* invalid exe */
604 pData += ne_header.entry_tab_offset - ne_header.iname_tab_offset;
606 /* Get the entry table */
608 pModule->entry_table = (int)pData - (int)pModule;
609 if (!READ( mz_header.e_lfanew + ne_header.entry_tab_offset,
610 ne_header.entry_tab_length,
613 if (fastload) HeapFree( SystemHeap, 0, fastload );
614 GlobalFree16( hModule );
615 return (HMODULE16)11; /* invalid exe */
617 pData += ne_header.entry_tab_length;
619 /* Store the filename information */
621 pModule->fileinfo = (int)pData - (int)pModule;
622 size = sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
623 memcpy( pData, ofs, size );
624 ((OFSTRUCT *)pData)->cBytes = size - 1;
627 /* Free the fast-load area */
630 if (fastload) HeapFree( SystemHeap, 0, fastload );
632 /* Get the non-resident names table */
634 if (ne_header.nrname_tab_length)
636 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.nrname_tab_length,
637 hModule, FALSE, FALSE, FALSE );
638 if (!pModule->nrname_handle)
640 GlobalFree16( hModule );
641 return (HMODULE16)11; /* invalid exe */
643 buffer = GlobalLock16( pModule->nrname_handle );
644 _llseek16( hFile, ne_header.nrname_tab_offset, SEEK_SET );
645 if (_hread16( hFile, buffer, ne_header.nrname_tab_length )
646 != ne_header.nrname_tab_length)
648 GlobalFree16( pModule->nrname_handle );
649 GlobalFree16( hModule );
650 return (HMODULE16)11; /* invalid exe */
653 else pModule->nrname_handle = 0;
655 /* Allocate a segment for the implicitly-loaded DLLs */
657 if (pModule->modref_count)
659 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
660 (pModule->modref_count+1)*sizeof(HMODULE16),
661 hModule, FALSE, FALSE, FALSE );
662 if (!pModule->dlls_to_init)
664 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
665 GlobalFree16( hModule );
666 return (HMODULE16)11; /* invalid exe */
669 else pModule->dlls_to_init = 0;
671 NE_RegisterModule( pModule );
672 if (fnSNOOP16_RegisterDLL)
673 fnSNOOP16_RegisterDLL(pModule,ofs->szPathName);
678 /***********************************************************************
681 * Load all DLLs implicitly linked to a module.
683 static BOOL32 NE_LoadDLLs( NE_MODULE *pModule )
686 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
687 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
689 for (i = 0; i < pModule->modref_count; i++, pModRef++)
692 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
693 memcpy( buffer, pstr + 1, *pstr );
694 strcpy( buffer + *pstr, ".dll" );
695 TRACE(module, "Loading '%s'\n", buffer );
696 if (!(*pModRef = GetModuleHandle16( buffer )))
698 /* If the DLL is not loaded yet, load it and store */
699 /* its handle in the list of DLLs to initialize. */
702 if ((hDLL = NE_LoadModule( buffer, NULL, TRUE, TRUE )) == 2)
707 /* Try with prepending the path of the current module */
708 GetModuleFileName16( pModule->self, buffer, sizeof(buffer) );
709 if (!(p = strrchr( buffer, '\\' ))) p = buffer;
710 memcpy( p + 1, pstr + 1, *pstr );
711 strcpy( p + 1 + *pstr, ".dll" );
712 hDLL = NE_LoadModule( buffer, NULL, TRUE, TRUE );
716 /* FIXME: cleanup what was done */
718 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
719 buffer, *((BYTE*)pModule + pModule->name_table),
720 (char *)pModule + pModule->name_table + 1, hDLL );
723 *pModRef = GetExePtr( hDLL );
726 else /* Increment the reference count of the DLL */
728 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
729 if (pOldDLL) pOldDLL->count++;
736 /**********************************************************************
739 * Implementation of LoadModule16().
741 HINSTANCE16 NE_LoadModule( LPCSTR name, HINSTANCE16 *hPrevInstance,
742 BOOL32 implicit, BOOL32 lib_only )
745 HINSTANCE16 hInstance;
750 /* Check if the module is already loaded */
752 if ((hModule = GetModuleHandle16( name )) != 0)
755 pModule = NE_GetPtr( hModule );
756 if ( pModule->module32 ) return (HINSTANCE16)21;
758 hInstance = NE_CreateInstance( pModule, &prev, lib_only );
759 if (hInstance != prev) /* not a library */
760 NE_LoadSegment( pModule, pModule->dgroup );
762 if (hPrevInstance) *hPrevInstance = prev;
765 if (hPrevInstance) *hPrevInstance = 0;
767 /* Try to load the built-in first if not disabled */
769 if ((hModule = fnBUILTIN_LoadModule( name, FALSE ))) return hModule;
771 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
773 /* Now try the built-in even if disabled */
774 if ((hModule = fnBUILTIN_LoadModule( name, TRUE )))
776 MSG( "Could not load Windows DLL '%s', using built-in module.\n",
780 return 2; /* File not found */
783 /* Create the module structure */
785 hModule = NE_LoadExeHeader( hFile, &ofs );
787 if (hModule < 32) return hModule;
788 pModule = NE_GetPtr( hModule );
790 /* Allocate the segments for this module */
792 if (!NE_CreateSegments( pModule ) ||
793 !(hInstance = NE_CreateInstance( pModule, NULL, lib_only )))
795 GlobalFreeAll( hModule );
796 return 8; /* Insufficient memory */
799 /* Load the referenced DLLs */
801 if (!NE_LoadDLLs( pModule ))
802 return 2; /* File not found (FIXME: free everything) */
804 /* Load the segments */
806 NE_LoadAllSegments( pModule );
808 /* Fixup the functions prologs */
810 NE_FixupPrologs( pModule );
812 /* Make sure the usage count is 1 on the first loading of */
813 /* the module, even if it contains circular DLL references */
817 /* Call initialization rountines for all loaded DLLs. Note that
818 * when we load implicitly linked DLLs this will be done by InitTask().
821 if (!implicit && (pModule->flags & NE_FFLAGS_LIBMODULE))
822 NE_InitializeDLLs( hModule );
828 /***********************************************************************
829 * LoadLibrary (KERNEL.95)
831 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
837 TRACE(module, "(%08x) %s\n", (int)libname, libname);
839 /* Check for an extension */
841 if ((p = strrchr( libname, '.')) && !strchr( p, '/' ) && !strchr( p, '\\'))
843 /* An extension is present -> use the name as is */
844 return NE_LoadModule( libname, NULL, FALSE, TRUE );
847 /* Now append .dll before loading */
849 if (!(new_name = HeapAlloc( GetProcessHeap(), 0, strlen(libname) + 4 )))
851 strcpy( new_name, libname );
852 strcat( new_name, ".dll" );
853 handle = NE_LoadModule( new_name, NULL, FALSE, TRUE );
854 HeapFree( GetProcessHeap(), 0, new_name );
859 /**********************************************************************
862 * Call a DLL's WEP, allowing it to shut down.
863 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
865 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
867 FARPROC16 WEP = (FARPROC16)0;
868 WORD ordinal = NE_GetOrdinal( hModule, "WEP" );
870 if (ordinal) WEP = NE_GetEntryPoint( hModule, ordinal );
873 WARN(module, "module %04x doesn't have a WEP\n", hModule );
876 return Callbacks->CallWindowsExitProc( WEP, WEP_FREE_DLL );
880 /**********************************************************************
883 * Implementation of FreeModule16().
885 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL32 call_wep )
887 HMODULE16 *hPrevModule;
892 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
893 hModule = pModule->self;
895 TRACE( module, "%04x count %d\n", hModule, pModule->count );
897 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
898 else pModule->count = 0;
900 if (pModule->flags & NE_FFLAGS_BUILTIN)
901 return FALSE; /* Can't free built-in module */
905 if (pModule->flags & NE_FFLAGS_LIBMODULE)
907 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
908 MODULE_CallWEP( hModule );
910 /* Free the objects owned by the DLL module */
912 if (pTask && pTask->userhandler)
913 pTask->userhandler( hModule, USIG_DLL_UNLOAD, 0,
914 pTask->hInstance, pTask->hQueue );
917 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
921 /* Clear magic number just in case */
923 pModule->magic = pModule->self = 0;
925 /* Remove it from the linked list */
927 hPrevModule = &hFirstModule;
928 while (*hPrevModule && (*hPrevModule != hModule))
930 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
932 if (*hPrevModule) *hPrevModule = pModule->next;
934 /* Free the referenced modules */
936 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
937 for (i = 0; i < pModule->modref_count; i++, pModRef++)
939 NE_FreeModule( *pModRef, call_wep );
942 /* Free the module storage */
944 GlobalFreeAll( hModule );
946 /* Remove module from cache */
948 if (pCachedModule == pModule) pCachedModule = NULL;
953 /**********************************************************************
954 * FreeModule16 (KERNEL.46)
956 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
958 return NE_FreeModule( hModule, TRUE );
962 /***********************************************************************
963 * FreeLibrary16 (KERNEL.96)
965 void WINAPI FreeLibrary16( HINSTANCE16 handle )
967 TRACE(module,"%04x\n", handle );
968 FreeModule16( handle );
972 /**********************************************************************
973 * GetModuleName (KERNEL.27)
975 BOOL16 WINAPI GetModuleName( HINSTANCE16 hinst, LPSTR buf, INT16 count )
980 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
981 p = (BYTE *)pModule + pModule->name_table;
982 if (count > *p) count = *p + 1;
985 memcpy( buf, p + 1, count - 1 );
992 /**********************************************************************
993 * GetModuleUsage (KERNEL.48)
995 INT16 WINAPI GetModuleUsage( HINSTANCE16 hModule )
997 NE_MODULE *pModule = NE_GetPtr( hModule );
998 return pModule ? pModule->count : 0;
1002 /**********************************************************************
1003 * GetExpWinVer (KERNEL.167)
1005 WORD WINAPI GetExpWinVer( HMODULE16 hModule )
1007 NE_MODULE *pModule = NE_GetPtr( hModule );
1008 return pModule ? pModule->expected_version : 0;
1012 /**********************************************************************
1013 * GetModuleFileName16 (KERNEL.49)
1015 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1020 if (!hModule) hModule = GetCurrentTask();
1021 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1022 lstrcpyn32A( lpFileName, NE_MODULE_NAME(pModule), nSize );
1023 TRACE(module, "%s\n", lpFileName );
1024 return strlen(lpFileName);
1028 /**********************************************************************
1029 * GetModuleHandle16 (KERNEL.47)
1031 * Find a module from a path name.
1035 * the win16 module handle if found
1037 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1038 * Always hFirstModule
1040 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1042 if (HIWORD(name) == 0)
1043 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1044 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name)), hFirstModule );
1047 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1049 HMODULE16 hModule = hFirstModule;
1050 LPCSTR filename, dotptr, modulepath, modulename;
1051 BYTE len, *name_table;
1053 if (!(filename = strrchr( name, '\\' ))) filename = name;
1055 if ((dotptr = strrchr( filename, '.' )) != NULL)
1056 len = (BYTE)(dotptr - filename);
1057 else len = strlen( filename );
1061 NE_MODULE *pModule = NE_GetPtr( hModule );
1062 if (!pModule) break;
1063 modulepath = NE_MODULE_NAME(pModule);
1064 if (!(modulename = strrchr( modulepath, '\\' )))
1065 modulename = modulepath;
1067 if (!lstrcmpi32A( modulename, filename )) return hModule;
1069 name_table = (BYTE *)pModule + pModule->name_table;
1070 if ((*name_table == len) && !lstrncmpi32A(filename, name_table+1, len))
1072 hModule = pModule->next;
1078 /**********************************************************************
1079 * ModuleFirst (TOOLHELP.59)
1081 BOOL16 WINAPI ModuleFirst( MODULEENTRY *lpme )
1083 lpme->wNext = hFirstModule;
1084 return ModuleNext( lpme );
1088 /**********************************************************************
1089 * ModuleNext (TOOLHELP.60)
1091 BOOL16 WINAPI ModuleNext( MODULEENTRY *lpme )
1096 if (!lpme->wNext) return FALSE;
1097 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1098 name = (char *)pModule + pModule->name_table;
1099 memcpy( lpme->szModule, name + 1, *name );
1100 lpme->szModule[(BYTE)*name] = '\0';
1101 lpme->hModule = lpme->wNext;
1102 lpme->wcUsage = pModule->count;
1103 strncpy( lpme->szExePath, NE_MODULE_NAME(pModule), MAX_PATH );
1104 lpme->szExePath[MAX_PATH] = '\0';
1105 lpme->wNext = pModule->next;
1110 /**********************************************************************
1111 * ModuleFindName (TOOLHELP.61)
1113 BOOL16 WINAPI ModuleFindName( MODULEENTRY *lpme, LPCSTR name )
1115 lpme->wNext = GetModuleHandle16( name );
1116 return ModuleNext( lpme );
1120 /**********************************************************************
1121 * ModuleFindHandle (TOOLHELP.62)
1123 BOOL16 WINAPI ModuleFindHandle( MODULEENTRY *lpme, HMODULE16 hModule )
1125 hModule = GetExePtr( hModule );
1126 lpme->wNext = hModule;
1127 return ModuleNext( lpme );
1130 /***************************************************************************
1131 * MapHModuleLS (KERNEL32.520)
1133 HMODULE16 WINAPI MapHModuleLS(HMODULE32 hmod) {
1137 return ((TDB*)GlobalLock16(GetCurrentTask()))->hInstance;
1139 return hmod; /* we already have a 16 bit module handle */
1140 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1142 if (pModule->module32 == hmod)
1143 return pModule->self;
1144 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1149 /***************************************************************************
1150 * MapHModuleSL (KERNEL32.521)
1152 HMODULE32 WINAPI MapHModuleSL(HMODULE16 hmod) {
1156 TDB *pTask = (TDB*)GlobalLock16(GetCurrentTask());
1158 hmod = pTask->hInstance;
1160 pModule = (NE_MODULE*)GlobalLock16(hmod);
1161 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1162 !(pModule->flags & NE_FFLAGS_WIN32)
1165 return pModule->module32;
1168 /***************************************************************************
1169 * MapHInstLS (KERNEL32.516)
1171 REGS_ENTRYPOINT(MapHInstLS) {
1172 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1175 /***************************************************************************
1176 * MapHInstSL (KERNEL32.518)
1178 REGS_ENTRYPOINT(MapHInstSL) {
1179 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1182 /***************************************************************************
1183 * MapHInstLS_PN (KERNEL32.517)
1185 REGS_ENTRYPOINT(MapHInstLS_PN) {
1186 if (EAX_reg(context))
1187 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1190 /***************************************************************************
1191 * MapHInstSL_PN (KERNEL32.519)
1193 REGS_ENTRYPOINT(MapHInstSL_PN) {
1194 if (EAX_reg(context))
1195 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1198 /***************************************************************************
1199 * WIN16_MapHInstLS (KERNEL.472)
1201 VOID WINAPI WIN16_MapHInstLS( CONTEXT *context ) {
1202 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1205 /***************************************************************************
1206 * WIN16_MapHInstSL (KERNEL.473)
1208 VOID WINAPI WIN16_MapHInstSL( CONTEXT *context ) {
1209 EAX_reg(context) = MapHModuleSL(EAX_reg(context));