4 * Copyright 1995 Alexandre Julliard
17 #include "wine/winbase16.h"
18 #include "wine/library.h"
25 #include "builtin16.h"
26 #include "stackframe.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(module);
30 DECLARE_DEBUG_CHANNEL(loaddll);
35 struct ne_segment_table_entry_s
37 WORD seg_data_offset; /* Sector offset of segment data */
38 WORD seg_data_length; /* Length of segment data */
39 WORD seg_flags; /* Flags associated with this segment */
40 WORD min_alloc; /* Minimum allocation size for this */
43 #define hFirstModule (pThhook->hExeHead)
45 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
47 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
48 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
50 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
52 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
54 /* ### start build ### */
55 extern WORD CALLBACK NE_CallTo16_word_w(FARPROC16,WORD);
56 /* ### stop build ### */
58 /***********************************************************************
61 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
63 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
67 /***********************************************************************
70 void NE_DumpModule( HMODULE16 hModule )
80 if (!(pModule = NE_GetPtr( hModule )))
82 MESSAGE( "**** %04x is not a module handle\n", hModule );
86 /* Dump the module info */
88 DPRINTF( "Module %04x:\n", hModule );
89 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
90 pModule->count, pModule->flags,
91 pModule->heap_size, pModule->stack_size );
92 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
93 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
94 pModule->seg_count, pModule->modref_count );
95 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
96 pModule->os_flags, pModule->min_swap_area,
97 pModule->expected_version );
98 if (pModule->flags & NE_FFLAGS_WIN32)
99 DPRINTF( "PE module=%08x\n", pModule->module32 );
101 /* Dump the file info */
103 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
105 /* Dump the segment table */
107 DPRINTF( "Segment table:\n" );
108 pSeg = NE_SEG_TABLE( pModule );
109 for (i = 0; i < pModule->seg_count; i++, pSeg++)
110 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
111 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
112 pSeg->minsize, pSeg->hSeg );
114 /* Dump the resource table */
116 DPRINTF( "Resource table:\n" );
117 if (pModule->res_table)
119 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
120 DPRINTF( "Alignment: %d\n", *pword++ );
123 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
124 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
125 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
126 for (i = 0; i < ptr->count; i++, pname++)
127 DPRINTF( "offset=%d len=%d id=%04x\n",
128 pname->offset, pname->length, pname->id );
129 pword = (WORD *)pname;
132 else DPRINTF( "None\n" );
134 /* Dump the resident name table */
136 DPRINTF( "Resident-name table:\n" );
137 pstr = (char *)pModule + pModule->name_table;
140 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
141 *(WORD *)(pstr + *pstr + 1) );
142 pstr += *pstr + 1 + sizeof(WORD);
145 /* Dump the module reference table */
147 DPRINTF( "Module ref table:\n" );
148 if (pModule->modref_table)
150 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
151 for (i = 0; i < pModule->modref_count; i++, pword++)
154 GetModuleName16( *pword, name, sizeof(name) );
155 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
158 else DPRINTF( "None\n" );
160 /* Dump the entry table */
162 DPRINTF( "Entry table:\n" );
163 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
165 entry = (ET_ENTRY *)((BYTE *)bundle+6);
166 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
167 ordinal = bundle->first;
168 while (ordinal < bundle->last)
170 if (entry->type == 0xff)
171 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
173 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
176 } while ( (bundle->next)
177 && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
179 /* Dump the non-resident names table */
181 DPRINTF( "Non-resident names table:\n" );
182 if (pModule->nrname_handle)
184 pstr = (char *)GlobalLock16( pModule->nrname_handle );
187 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
188 *(WORD *)(pstr + *pstr + 1) );
189 pstr += *pstr + 1 + sizeof(WORD);
196 /***********************************************************************
199 * Walk the module list and print the modules.
201 void NE_WalkModules(void)
203 HMODULE16 hModule = hFirstModule;
204 MESSAGE( "Module Flags Name\n" );
207 NE_MODULE *pModule = NE_GetPtr( hModule );
210 MESSAGE( "Bad module %04x in list\n", hModule );
213 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->flags,
214 *((char *)pModule + pModule->name_table),
215 (char *)pModule + pModule->name_table + 1 );
216 hModule = pModule->next;
221 /**********************************************************************
224 void NE_RegisterModule( NE_MODULE *pModule )
226 pModule->next = hFirstModule;
227 hFirstModule = pModule->self;
231 /***********************************************************************
234 * Lookup the ordinal for a given name.
236 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
238 unsigned char buffer[256], *cpnt;
242 if (!(pModule = NE_GetPtr( hModule ))) return 0;
243 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
245 TRACE("(%04x,'%s')\n", hModule, name );
247 /* First handle names of the form '#xxxx' */
249 if (name[0] == '#') return atoi( name + 1 );
251 /* Now copy and uppercase the string */
253 strcpy( buffer, name );
254 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = FILE_toupper(*cpnt);
257 /* First search the resident names */
259 cpnt = (char *)pModule + pModule->name_table;
261 /* Skip the first entry (module name) */
262 cpnt += *cpnt + 1 + sizeof(WORD);
265 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
267 WORD ordinal = GET_UA_WORD( cpnt + *cpnt + 1 );
268 TRACE(" Found: ordinal=%d\n", ordinal );
271 cpnt += *cpnt + 1 + sizeof(WORD);
274 /* Now search the non-resident names table */
276 if (!pModule->nrname_handle) return 0; /* No non-resident table */
277 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
279 /* Skip the first entry (module description string) */
280 cpnt += *cpnt + 1 + sizeof(WORD);
283 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
285 WORD ordinal = GET_UA_WORD( cpnt + *cpnt + 1 );
286 TRACE(" Found: ordinal=%d\n", ordinal );
289 cpnt += *cpnt + 1 + sizeof(WORD);
295 /***********************************************************************
296 * EntryAddrProc (KERNEL.667) Wine-specific export
298 * Return the entry point for a given ordinal.
300 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
302 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
303 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
307 /***********************************************************************
310 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
312 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
315 /***********************************************************************
318 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
326 if (!(pModule = NE_GetPtr( hModule ))) return 0;
327 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
329 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
330 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
334 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
337 entry = (ET_ENTRY *)((BYTE *)bundle+6);
338 for (i=0; i < (ordinal - bundle->first - 1); i++)
342 offset = GET_UA_WORD( &entry->offs );
344 if (sel == 0xfe) sel = 0xffff; /* constant entry */
345 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
347 return (FARPROC16)MAKESEGPTR( sel, offset );
349 return (FARPROC16)MAKESEGPTR( sel, offset );
351 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
355 /***********************************************************************
358 * Change the value of an entry point. Use with caution!
359 * It can only change the offset value, not the selector.
361 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
368 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
369 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
371 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
372 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
374 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
379 entry = (ET_ENTRY *)((BYTE *)bundle+6);
380 for (i=0; i < (ordinal - bundle->first - 1); i++)
383 PUT_UA_WORD( &entry->offs, offset );
388 /***********************************************************************
391 HANDLE NE_OpenFile( NE_MODULE *pModule )
395 static HANDLE cachedfd = INVALID_HANDLE_VALUE;
397 TRACE("(%p) cache: mod=%p fd=%d\n",
398 pModule, pCachedModule, cachedfd );
399 if (pCachedModule == pModule) return cachedfd;
400 CloseHandle( cachedfd );
401 pCachedModule = pModule;
402 name = NE_MODULE_NAME( pModule );
403 if ((cachedfd = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
404 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
405 MESSAGE( "Can't open file '%s' for module %04x\n", name, pModule->self );
406 TRACE("opened '%s' -> %d\n",
412 /***********************************************************************
415 static HMODULE16 NE_LoadExeHeader( HANDLE hFile, LPCSTR path )
417 IMAGE_DOS_HEADER mz_header;
418 IMAGE_OS2_HEADER ne_header;
422 BYTE *pData, *pTempEntryTable;
423 char *buffer, *fastload = NULL;
424 int fastload_offset = 0, fastload_length = 0;
426 ET_BUNDLE *bundle, *oldbundle;
429 /* Read a block from either the file or the fast-load area. */
430 #define READ(offset,size,buffer) \
431 ((fastload && ((offset) >= fastload_offset) && \
432 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
433 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
434 (_llseek( hFile, (offset), SEEK_SET), \
435 _lread( hFile, (buffer), (size) ) == (size)))
437 _llseek( hFile, 0, SEEK_SET );
438 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
439 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
440 return (HMODULE16)11; /* invalid exe */
442 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
443 if (_lread( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
444 return (HMODULE16)11; /* invalid exe */
446 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
447 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
448 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
449 return (HMODULE16)12;
451 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
453 /* We now have a valid NE header */
455 size = sizeof(NE_MODULE) +
457 ne_header.ne_cseg * sizeof(SEGTABLEENTRY) +
459 ne_header.ne_restab - ne_header.ne_rsrctab +
460 /* resident names table */
461 ne_header.ne_modtab - ne_header.ne_restab +
462 /* module ref table */
463 ne_header.ne_cmod * sizeof(WORD) +
464 /* imported names table */
465 ne_header.ne_enttab - ne_header.ne_imptab +
466 /* entry table length */
467 ne_header.ne_cbenttab +
468 /* entry table extra conversion space */
470 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6) +
471 /* loaded file info */
472 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
474 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
475 if (!hModule) return (HMODULE16)11; /* invalid exe */
477 FarSetOwner16( hModule, hModule );
478 pModule = (NE_MODULE *)GlobalLock16( hModule );
479 memcpy( pModule, &ne_header, sizeof(ne_header) );
481 /* check *programs* for default minimal stack size */
482 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
483 && (pModule->stack_size < 0x1400) )
484 pModule->stack_size = 0x1400;
485 pModule->module32 = 0;
486 pModule->self = hModule;
487 pModule->self_loading_sel = 0;
488 pData = (BYTE *)(pModule + 1);
490 /* Clear internal Wine flags in case they are set in the EXE file */
492 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
494 /* Read the fast-load area */
496 if (ne_header.ne_flagsothers & NE_AFLAGS_FASTLOAD)
498 fastload_offset=ne_header.fastload_offset << ne_header.ne_align;
499 fastload_length=ne_header.fastload_length << ne_header.ne_align;
500 TRACE("Using fast-load area offset=%x len=%d\n",
501 fastload_offset, fastload_length );
502 if ((fastload = HeapAlloc( GetProcessHeap(), 0, fastload_length )) != NULL)
504 _llseek( hFile, fastload_offset, SEEK_SET);
505 if (_lread(hFile, fastload, fastload_length) != fastload_length)
507 HeapFree( GetProcessHeap(), 0, fastload );
508 WARN("Error reading fast-load area!\n");
514 /* Get the segment table */
516 pModule->seg_table = pData - (BYTE *)pModule;
517 buffer = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cseg *
518 sizeof(struct ne_segment_table_entry_s));
522 struct ne_segment_table_entry_s *pSeg;
524 if (!READ( mz_header.e_lfanew + ne_header.ne_segtab,
525 ne_header.ne_cseg * sizeof(struct ne_segment_table_entry_s),
528 HeapFree( GetProcessHeap(), 0, buffer );
530 HeapFree( GetProcessHeap(), 0, fastload );
531 GlobalFree16( hModule );
532 return (HMODULE16)11; /* invalid exe */
534 pSeg = (struct ne_segment_table_entry_s *)buffer;
535 for (i = ne_header.ne_cseg; i > 0; i--, pSeg++)
537 memcpy( pData, pSeg, sizeof(*pSeg) );
538 pData += sizeof(SEGTABLEENTRY);
540 HeapFree( GetProcessHeap(), 0, buffer );
545 HeapFree( GetProcessHeap(), 0, fastload );
546 GlobalFree16( hModule );
547 return (HMODULE16)11; /* invalid exe */
550 /* Get the resource table */
552 if (ne_header.ne_rsrctab < ne_header.ne_restab)
554 pModule->res_table = pData - (BYTE *)pModule;
555 if (!READ(mz_header.e_lfanew + ne_header.ne_rsrctab,
556 ne_header.ne_restab - ne_header.ne_rsrctab,
558 return (HMODULE16)11; /* invalid exe */
559 pData += ne_header.ne_restab - ne_header.ne_rsrctab;
560 NE_InitResourceHandler( hModule );
562 else pModule->res_table = 0; /* No resource table */
564 /* Get the resident names table */
566 pModule->name_table = pData - (BYTE *)pModule;
567 if (!READ( mz_header.e_lfanew + ne_header.ne_restab,
568 ne_header.ne_modtab - ne_header.ne_restab,
572 HeapFree( GetProcessHeap(), 0, fastload );
573 GlobalFree16( hModule );
574 return (HMODULE16)11; /* invalid exe */
576 pData += ne_header.ne_modtab - ne_header.ne_restab;
578 /* Get the module references table */
580 if (ne_header.ne_cmod > 0)
582 pModule->modref_table = pData - (BYTE *)pModule;
583 if (!READ( mz_header.e_lfanew + ne_header.ne_modtab,
584 ne_header.ne_cmod * sizeof(WORD),
588 HeapFree( GetProcessHeap(), 0, fastload );
589 GlobalFree16( hModule );
590 return (HMODULE16)11; /* invalid exe */
592 pData += ne_header.ne_cmod * sizeof(WORD);
594 else pModule->modref_table = 0; /* No module references */
596 /* Get the imported names table */
598 pModule->import_table = pData - (BYTE *)pModule;
599 if (!READ( mz_header.e_lfanew + ne_header.ne_imptab,
600 ne_header.ne_enttab - ne_header.ne_imptab,
604 HeapFree( GetProcessHeap(), 0, fastload );
605 GlobalFree16( hModule );
606 return (HMODULE16)11; /* invalid exe */
608 pData += ne_header.ne_enttab - ne_header.ne_imptab;
610 /* Load entry table, convert it to the optimized version used by Windows */
612 if ((pTempEntryTable = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cbenttab)) != NULL)
614 BYTE nr_entries, type, *s;
616 TRACE("Converting entry table.\n");
617 pModule->entry_table = pData - (BYTE *)pModule;
618 if (!READ( mz_header.e_lfanew + ne_header.ne_enttab,
619 ne_header.ne_cbenttab, pTempEntryTable ))
621 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
623 HeapFree( GetProcessHeap(), 0, fastload );
624 GlobalFree16( hModule );
625 return (HMODULE16)11; /* invalid exe */
629 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header.ne_enttab, ne_header.ne_cbenttab, *s);
631 bundle = (ET_BUNDLE *)pData;
632 TRACE("first bundle: %p\n", bundle);
633 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
634 entry = (ET_ENTRY *)((BYTE *)bundle+6);
636 while ((nr_entries = *s++))
640 bundle->last += nr_entries;
647 entry->segnum = *s++;
648 entry->offs = *(WORD *)s; s += 2;
649 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
657 entry->segnum = type;
658 entry->offs = *(WORD *)s; s += 2;
659 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
665 if (bundle->first == bundle->last)
667 bundle->first += nr_entries;
668 bundle->last += nr_entries;
673 oldbundle->next = ((int)entry - (int)pModule);
674 bundle = (ET_BUNDLE *)entry;
675 TRACE("new bundle: %p\n", bundle);
676 bundle->first = bundle->last =
677 oldbundle->last + nr_entries;
679 (BYTE *)entry += sizeof(ET_BUNDLE);
683 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
688 HeapFree( GetProcessHeap(), 0, fastload );
689 GlobalFree16( hModule );
690 return (HMODULE16)11; /* invalid exe */
693 pData += ne_header.ne_cbenttab + sizeof(ET_BUNDLE) +
694 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6);
696 if ((DWORD)entry > (DWORD)pData)
697 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
699 /* Store the filename information */
701 pModule->fileinfo = pData - (BYTE *)pModule;
702 size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
703 ofs = (OFSTRUCT *)pData;
704 ofs->cBytes = size - 1;
706 strcpy( ofs->szPathName, path );
709 /* Free the fast-load area */
713 HeapFree( GetProcessHeap(), 0, fastload );
715 /* Get the non-resident names table */
717 if (ne_header.ne_cbnrestab)
719 pModule->nrname_handle = GlobalAlloc16( 0, ne_header.ne_cbnrestab );
720 if (!pModule->nrname_handle)
722 GlobalFree16( hModule );
723 return (HMODULE16)11; /* invalid exe */
725 FarSetOwner16( pModule->nrname_handle, hModule );
726 buffer = GlobalLock16( pModule->nrname_handle );
727 _llseek( hFile, ne_header.ne_nrestab, SEEK_SET );
728 if (_lread( hFile, buffer, ne_header.ne_cbnrestab )
729 != ne_header.ne_cbnrestab)
731 GlobalFree16( pModule->nrname_handle );
732 GlobalFree16( hModule );
733 return (HMODULE16)11; /* invalid exe */
736 else pModule->nrname_handle = 0;
738 /* Allocate a segment for the implicitly-loaded DLLs */
740 if (pModule->modref_count)
742 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
743 (pModule->modref_count+1)*sizeof(HMODULE16) );
744 if (!pModule->dlls_to_init)
746 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
747 GlobalFree16( hModule );
748 return (HMODULE16)11; /* invalid exe */
750 FarSetOwner16( pModule->dlls_to_init, hModule );
752 else pModule->dlls_to_init = 0;
754 NE_RegisterModule( pModule );
755 SNOOP16_RegisterDLL(pModule,path);
760 /***********************************************************************
763 * Load all DLLs implicitly linked to a module.
765 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
768 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
769 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
771 for (i = 0; i < pModule->modref_count; i++, pModRef++)
773 char buffer[260], *p;
774 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
775 memcpy( buffer, pstr + 1, *pstr );
776 *(buffer + *pstr) = 0; /* terminate it */
778 TRACE("Loading '%s'\n", buffer );
779 if (!(*pModRef = GetModuleHandle16( buffer )))
781 /* If the DLL is not loaded yet, load it and store */
782 /* its handle in the list of DLLs to initialize. */
785 /* Append .DLL to name if no extension present */
786 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
787 strcat( buffer, ".DLL" );
789 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
791 /* FIXME: cleanup what was done */
793 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
794 buffer, *((BYTE*)pModule + pModule->name_table),
795 (char *)pModule + pModule->name_table + 1, hDLL );
798 *pModRef = GetExePtr( hDLL );
801 else /* Increment the reference count of the DLL */
805 pOldDLL = NE_GetPtr( *pModRef );
806 if (pOldDLL) pOldDLL->count++;
813 /**********************************************************************
816 * Load first instance of NE module from file.
818 * pModule must point to a module structure prepared by NE_LoadExeHeader.
819 * This routine must never be called twice on a module.
822 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
824 /* Allocate the segments for this module */
826 if (!NE_CreateAllSegments( pModule ))
827 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
829 /* Load the referenced DLLs */
831 if (!NE_LoadDLLs( pModule ))
832 return ERROR_FILE_NOT_FOUND; /* 2 */
834 /* Load the segments */
836 NE_LoadAllSegments( pModule );
838 /* Make sure the usage count is 1 on the first loading of */
839 /* the module, even if it contains circular DLL references */
843 return NE_GetInstance( pModule );
846 /**********************************************************************
849 * Load first instance of NE module. (Note: caller is responsible for
850 * ensuring the module isn't already loaded!)
852 * If the module turns out to be an executable module, only a
853 * handle to a module stub is returned; this needs to be initialized
854 * by calling NE_DoLoadModule later, in the context of the newly
857 * If lib_only is TRUE, however, the module is perforce treated
858 * like a DLL module, even if it is an executable module.
861 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
865 HINSTANCE16 hInstance;
870 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
871 return (HMODULE16)2; /* File not found */
873 hModule = NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile), ofs.szPathName );
875 if (hModule < 32) return hModule;
877 pModule = NE_GetPtr( hModule );
878 if ( !pModule ) return hModule;
880 if ( !lib_only && !( pModule->flags & NE_FFLAGS_LIBMODULE ) )
883 hInstance = NE_DoLoadModule( pModule );
884 if ( hInstance < 32 )
887 NE_FreeModule( hModule, 0 );
894 /**********************************************************************
895 * MODULE_LoadModule16
897 * Load a NE module in the order of the loadorder specification.
898 * The caller is responsible that the module is not loaded already.
901 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
903 HINSTANCE16 hinst = 2;
904 enum loadorder_type loadorder[LOADORDER_NTYPES];
906 const char *filetype = "";
908 MODULE_GetLoadOrder(loadorder, libname, FALSE);
910 for(i = 0; i < LOADORDER_NTYPES; i++)
912 if (loadorder[i] == LOADORDER_INVALID) break;
917 TRACE("Trying native dll '%s'\n", libname);
918 hinst = NE_LoadModule(libname, lib_only);
923 TRACE("Trying built-in '%s'\n", libname);
924 hinst = BUILTIN_LoadModule(libname);
925 filetype = "builtin";
928 case LOADORDER_SO: /* This is not supported for NE modules */
941 hModule = GetModuleHandle16(libname);
944 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
946 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
949 pModule = NE_GetPtr(hModule);
952 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
954 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
957 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
958 if (!TRACE_ON(module))
959 TRACE_(loaddll)("Loaded module '%s' : %s\n", libname, filetype);
962 * Call initialization routines for all loaded DLLs. Note that
963 * when we load implicitly linked DLLs this will be done by InitTask().
965 if(pModule->flags & NE_FFLAGS_LIBMODULE)
967 NE_InitializeDLLs(hModule);
968 NE_DllProcessAttach(hModule);
976 /* We quit searching when we get another error than 'File not found' */
980 return hinst; /* The last error that occured */
984 /**********************************************************************
987 * Create the thread for a 16-bit module.
989 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
994 HINSTANCE16 instance = 0;
996 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
999 /* Post event to start the task */
1000 PostEvent16( hTask );
1002 /* Wait until we get the instance handle */
1005 DirectedYield16( hTask );
1006 if (!IsTask16( hTask )) /* thread has died */
1009 WaitForSingleObject( hThread, INFINITE );
1010 GetExitCodeThread( hThread, &exit_code );
1011 CloseHandle( hThread );
1014 if (!(pTask = TASK_GetPtr( hTask ))) break;
1015 instance = pTask->hInstance;
1016 GlobalUnlock16( hTask );
1017 } while (!instance);
1019 CloseHandle( hThread );
1024 /**********************************************************************
1025 * LoadModule (KERNEL.45)
1026 * LoadModule16 (KERNEL32.@)
1028 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1030 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1031 LOADPARAMS16 *params;
1039 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1041 /* Special case: second instance of an already loaded NE module */
1043 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
1044 if ( pModule->module32 ) return (HINSTANCE16)21;
1046 /* Increment refcount */
1052 /* Main case: load first instance of NE module */
1054 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1057 if ( !(pModule = NE_GetPtr( hModule )) )
1058 return (HINSTANCE16)11;
1061 /* If library module, we just retrieve the instance handle */
1063 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1064 return NE_GetInstance( pModule );
1067 * At this point, we need to create a new process.
1069 * pModule points either to an already loaded module, whose refcount
1070 * has already been incremented (to avoid having the module vanish
1071 * in the meantime), or else to a stub module which contains only header
1074 params = (LOADPARAMS16 *)paramBlock;
1075 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1076 cmdline = MapSL( params->cmdLine );
1077 return NE_CreateThread( pModule, cmdShow, cmdline );
1081 /**********************************************************************
1084 * Start the main NE task.
1086 HINSTANCE16 NE_StartMain( LPCSTR name, HANDLE file )
1092 LPSTR pCmdLine, cmdline = GetCommandLineA();
1094 if ((hModule = NE_LoadExeHeader( file, name )) < 32) return hModule;
1096 if (!(pModule = NE_GetPtr( hModule ))) return (HINSTANCE16)11;
1097 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1099 MESSAGE( "%s is not a valid Win16 executable\n", name );
1100 ExitProcess( ERROR_BAD_EXE_FORMAT );
1103 while (*cmdline && *cmdline != ' ') cmdline++;
1104 if (*cmdline) cmdline++;
1105 len = strlen(cmdline);
1106 pCmdLine = HeapAlloc(GetProcessHeap(), 0, len+2);
1109 strcpy(pCmdLine+1, cmdline);
1112 GetStartupInfoA( &info );
1113 if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = 1;
1115 return NE_CreateThread( pModule, info.wShowWindow, pCmdLine );
1119 /**********************************************************************
1122 * Startup code for a new 16-bit task.
1124 DWORD NE_StartTask(void)
1126 TDB *pTask = TASK_GetCurrent();
1127 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1128 HINSTANCE16 hInstance, hPrevInstance;
1129 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1132 if ( pModule->count > 0 )
1134 /* Second instance of an already loaded NE module */
1135 /* Note that the refcount was already incremented by the parent */
1137 hPrevInstance = NE_GetInstance( pModule );
1139 if ( pModule->dgroup )
1140 if ( NE_CreateSegment( pModule, pModule->dgroup ) )
1141 NE_LoadSegment( pModule, pModule->dgroup );
1143 hInstance = NE_GetInstance( pModule );
1144 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->dgroup, hPrevInstance);
1149 /* Load first instance of NE module */
1151 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1153 hInstance = NE_DoLoadModule( pModule );
1157 if ( hInstance >= 32 )
1161 /* Enter instance handles into task struct */
1163 pTask->hInstance = hInstance;
1164 pTask->hPrevInstance = hPrevInstance;
1166 /* Free the previous stack selector */
1167 FreeSelector16( SELECTOROF(pTask->teb->cur_stack) );
1169 /* Use DGROUP for 16-bit stack */
1171 if (!(sp = pModule->sp))
1172 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
1174 sp -= sizeof(STACK16FRAME);
1175 pTask->teb->cur_stack = MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1177 /* Registers at initialization must be:
1179 * bx stack size in bytes
1180 * cx heap size in bytes
1181 * si previous app instance
1182 * di current app instance
1184 * es selector to the PSP
1185 * ds dgroup of the application
1187 * sp top of the stack
1189 memset( &context, 0, sizeof(context) );
1190 context.SegCs = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
1191 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1192 context.SegEs = pTask->hPDB;
1193 context.Eip = pModule->ip;
1194 context.Ebx = pModule->stack_size;
1195 context.Ecx = pModule->heap_size;
1196 context.Edi = pTask->hInstance;
1197 context.Esi = pTask->hPrevInstance;
1199 /* Now call 16-bit entry point */
1201 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1202 context.SegCs, context.Eip, context.SegDs,
1203 SELECTOROF(pTask->teb->cur_stack),
1204 OFFSETOF(pTask->teb->cur_stack) );
1206 wine_call_to_16_regs_short( &context, 0 );
1207 ExitThread( LOWORD(context.Eax) );
1209 return hInstance; /* error code */
1212 /***********************************************************************
1213 * LoadLibrary (KERNEL.95)
1214 * LoadLibrary16 (KERNEL32.35)
1216 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1218 return LoadModule16(libname, (LPVOID)-1 );
1222 /**********************************************************************
1225 * Call a DLL's WEP, allowing it to shut down.
1226 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1228 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1230 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1231 if (!WEP) return FALSE;
1232 return NE_CallTo16_word_w( WEP, WEP_FREE_DLL );
1236 /**********************************************************************
1239 * Implementation of FreeModule16().
1241 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1243 HMODULE16 *hPrevModule;
1248 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1249 hModule = pModule->self;
1251 TRACE("%04x count %d\n", hModule, pModule->count );
1253 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1254 else pModule->count = 0;
1256 if (pModule->flags & NE_FFLAGS_BUILTIN)
1257 return FALSE; /* Can't free built-in module */
1259 if (call_wep && !(pModule->flags & NE_FFLAGS_WIN32))
1261 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1263 MODULE_CallWEP( hModule );
1265 /* Free the objects owned by the DLL module */
1266 TASK_CallTaskSignalProc( USIG16_DLL_UNLOAD, hModule );
1267 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16, hModule );
1270 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1274 /* Clear magic number just in case */
1276 pModule->magic = pModule->self = 0;
1278 /* Remove it from the linked list */
1280 hPrevModule = &hFirstModule;
1281 while (*hPrevModule && (*hPrevModule != hModule))
1283 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1285 if (*hPrevModule) *hPrevModule = pModule->next;
1287 /* Free the referenced modules */
1289 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1290 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1292 NE_FreeModule( *pModRef, call_wep );
1295 /* Free the module storage */
1297 GlobalFreeAll16( hModule );
1299 /* Remove module from cache */
1301 if (pCachedModule == pModule) pCachedModule = NULL;
1306 /**********************************************************************
1307 * FreeModule (KERNEL.46)
1309 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1311 return NE_FreeModule( hModule, TRUE );
1315 /***********************************************************************
1316 * FreeLibrary (KERNEL.96)
1317 * FreeLibrary16 (KERNEL32.36)
1319 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1321 TRACE("%04x\n", handle );
1322 FreeModule16( handle );
1326 /**********************************************************************
1327 * GetModuleName (KERNEL.27)
1329 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1334 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1335 p = (BYTE *)pModule + pModule->name_table;
1336 if (count > *p) count = *p + 1;
1339 memcpy( buf, p + 1, count - 1 );
1340 buf[count-1] = '\0';
1346 /**********************************************************************
1347 * GetModuleUsage (KERNEL.48)
1349 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1351 NE_MODULE *pModule = NE_GetPtr( hModule );
1352 return pModule ? pModule->count : 0;
1356 /**********************************************************************
1357 * GetExpWinVer (KERNEL.167)
1359 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1361 NE_MODULE *pModule = NE_GetPtr( hModule );
1362 if ( !pModule ) return 0;
1365 * For built-in modules, fake the expected version the module should
1366 * have according to the Windows version emulated by Wine
1368 if ( !pModule->expected_version )
1370 OSVERSIONINFOA versionInfo;
1371 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1373 if ( GetVersionExA( &versionInfo ) )
1374 pModule->expected_version =
1375 (versionInfo.dwMajorVersion & 0xff) << 8
1376 | (versionInfo.dwMinorVersion & 0xff);
1379 return pModule->expected_version;
1383 /**********************************************************************
1384 * GetModuleFileName (KERNEL.49)
1385 * GetModuleFileName16 (KERNEL32.@)
1387 * Comment: see GetModuleFileNameA
1389 * Even if invoked by second instance of a program,
1390 * it still returns path of first one.
1392 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1397 /* Win95 does not query hModule if set to 0 !
1398 * Is this wrong or maybe Win3.1 only ? */
1399 if (!hModule) hModule = GetCurrentTask();
1401 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1402 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1403 if (pModule->expected_version >= 0x400)
1404 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1405 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1406 return strlen(lpFileName);
1410 /**********************************************************************
1411 * GetModuleHandle (KERNEL.47)
1413 * Find a module from a module name.
1415 * NOTE: The current implementation works the same way the Windows 95 one
1416 * does. Do not try to 'fix' it, fix the callers.
1417 * + It does not do ANY extension handling (except that strange .EXE bit)!
1418 * + It does not care about paths, just about basenames. (same as Windows)
1422 * the win16 module handle if found
1424 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1425 * Always hFirstModule
1427 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1429 if (HIWORD(name) == 0)
1430 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1431 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1434 /***********************************************************************
1435 * GetModuleHandle16 (KERNEL32.@)
1437 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1439 HMODULE16 hModule = hFirstModule;
1441 BYTE len, *name_table;
1442 char tmpstr[MAX_PATH];
1445 TRACE("(%s)\n", name);
1448 return GetExePtr(LOWORD(name));
1454 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1456 /* If 'name' matches exactly the module name of a module:
1457 * Return its handle.
1459 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1461 pModule = NE_GetPtr( hModule );
1462 if (!pModule) break;
1463 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1465 name_table = (BYTE *)pModule + pModule->name_table;
1466 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1470 /* If uppercased 'name' matches exactly the module name of a module:
1473 for (s = tmpstr; *s; s++) *s = FILE_toupper(*s);
1475 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1477 pModule = NE_GetPtr( hModule );
1478 if (!pModule) break;
1479 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1481 name_table = (BYTE *)pModule + pModule->name_table;
1482 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1483 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1484 * lowercased module names, but try to load uppercase DLLs, so this
1485 * 'i' compare is just a quickfix until the loader handles that
1486 * correctly. -MM 990705
1488 if ((*name_table == len) && !FILE_strncasecmp(tmpstr, name_table+1, len))
1492 /* If the base filename of 'name' matches the base filename of the module
1493 * filename of some module (case-insensitive compare):
1494 * Return its handle.
1497 /* basename: search backwards in passed name to \ / or : */
1498 s = tmpstr + strlen(tmpstr);
1501 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1506 /* search this in loaded filename list */
1507 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1512 pModule = NE_GetPtr( hModule );
1513 if (!pModule) break;
1514 if (!pModule->fileinfo) continue;
1515 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1517 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1518 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1519 /* basename: search backwards in pathname to \ / or : */
1520 while (loadedfn > (char*)ofs->szPathName)
1522 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1526 /* case insensitive compare ... */
1527 if (!FILE_strcasecmp(loadedfn, s))
1531 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1532 * matches the base filename of the module filename of some 32-bit module:
1533 * Return the corresponding 16-bit dummy module handle.
1535 if (len >= 4 && !FILE_strcasecmp(name+len-4, ".EXE"))
1537 HMODULE hModule = GetModuleHandleA( name );
1539 return MapHModuleLS( hModule );
1542 if (!strcmp(tmpstr,"MSDOS"))
1545 if (!strcmp(tmpstr,"TIMER"))
1547 FIXME("Eh... Should return caller's code segment, expect crash\n");
1554 /**********************************************************************
1555 * NE_GetModuleByFilename
1557 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1561 BYTE len, *name_table;
1562 char tmpstr[MAX_PATH];
1565 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1567 /* If the base filename of 'name' matches the base filename of the module
1568 * filename of some module (case-insensitive compare):
1569 * Return its handle.
1572 /* basename: search backwards in passed name to \ / or : */
1573 s = tmpstr + strlen(tmpstr);
1576 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1581 /* search this in loaded filename list */
1582 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1587 pModule = NE_GetPtr( hModule );
1588 if (!pModule) break;
1589 if (!pModule->fileinfo) continue;
1590 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1592 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1593 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1594 /* basename: search backwards in pathname to \ / or : */
1595 while (loadedfn > (char*)ofs->szPathName)
1597 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1601 /* case insensitive compare ... */
1602 if (!FILE_strcasecmp(loadedfn, s))
1605 /* If basename (without ext) matches the module name of a module:
1606 * Return its handle.
1609 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1612 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1614 pModule = NE_GetPtr( hModule );
1615 if (!pModule) break;
1616 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1618 name_table = (BYTE *)pModule + pModule->name_table;
1619 if ((*name_table == len) && !FILE_strncasecmp(s, name_table+1, len))
1626 /**********************************************************************
1627 * ModuleFirst (TOOLHELP.59)
1629 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1631 lpme->wNext = hFirstModule;
1632 return ModuleNext16( lpme );
1636 /**********************************************************************
1637 * ModuleNext (TOOLHELP.60)
1639 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1644 if (!lpme->wNext) return FALSE;
1645 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1646 name = (char *)pModule + pModule->name_table;
1647 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1648 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1649 lpme->hModule = lpme->wNext;
1650 lpme->wcUsage = pModule->count;
1651 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1652 lpme->wNext = pModule->next;
1657 /**********************************************************************
1658 * ModuleFindName (TOOLHELP.61)
1660 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1662 lpme->wNext = GetModuleHandle16( name );
1663 return ModuleNext16( lpme );
1667 /**********************************************************************
1668 * ModuleFindHandle (TOOLHELP.62)
1670 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1672 hModule = GetExePtr( hModule );
1673 lpme->wNext = hModule;
1674 return ModuleNext16( lpme );
1678 /***************************************************************************
1679 * IsRomModule (KERNEL.323)
1681 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1686 /***************************************************************************
1687 * IsRomFile (KERNEL.326)
1689 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1694 /***************************************************************************
1695 * MapHModuleLS (KERNEL32.@)
1697 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod) {
1701 return TASK_GetCurrent()->hInstance;
1703 return hmod; /* we already have a 16 bit module handle */
1704 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1706 if (pModule->module32 == hmod)
1707 return pModule->self;
1708 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1713 /***************************************************************************
1714 * MapHModuleSL (KERNEL32.@)
1716 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1720 TDB *pTask = TASK_GetCurrent();
1721 hmod = pTask->hModule;
1723 pModule = (NE_MODULE*)GlobalLock16(hmod);
1724 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1725 !(pModule->flags & NE_FFLAGS_WIN32)
1728 return pModule->module32;
1731 /***************************************************************************
1732 * MapHInstLS (KERNEL32.@)
1733 * MapHInstLS (KERNEL.472)
1735 void WINAPI MapHInstLS( CONTEXT86 *context )
1737 context->Eax = MapHModuleLS(context->Eax);
1740 /***************************************************************************
1741 * MapHInstSL (KERNEL32.@)
1742 * MapHInstSL (KERNEL.473)
1744 void WINAPI MapHInstSL( CONTEXT86 *context )
1746 context->Eax = MapHModuleSL(context->Eax);
1749 /***************************************************************************
1750 * MapHInstLS_PN (KERNEL32.@)
1752 void WINAPI MapHInstLS_PN( CONTEXT86 *context )
1754 if (context->Eax) context->Eax = MapHModuleLS(context->Eax);
1757 /***************************************************************************
1758 * MapHInstSL_PN (KERNEL32.@)
1760 void WINAPI MapHInstSL_PN( CONTEXT86 *context )
1762 if (context->Eax) context->Eax = MapHModuleSL(context->Eax);