4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
35 #include "wine/winbase16.h"
36 #include "wine/library.h"
43 #include "builtin16.h"
44 #include "stackframe.h"
46 #include "wine/exception.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(module);
50 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
55 struct ne_segment_table_entry_s
57 WORD seg_data_offset; /* Sector offset of segment data */
58 WORD seg_data_length; /* Length of segment data */
59 WORD seg_flags; /* Flags associated with this segment */
60 WORD min_alloc; /* Minimum allocation size for this */
63 #define hFirstModule (pThhook->hExeHead)
65 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
67 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
68 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
70 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
72 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
74 /* ### start build ### */
75 extern WORD CALLBACK NE_CallTo16_word_w(FARPROC16,WORD);
76 /* ### stop build ### */
79 static WINE_EXCEPTION_FILTER(page_fault)
81 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
82 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
83 return EXCEPTION_EXECUTE_HANDLER;
84 return EXCEPTION_CONTINUE_SEARCH;
88 /***********************************************************************
91 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
93 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
97 /***********************************************************************
100 void NE_DumpModule( HMODULE16 hModule )
110 if (!(pModule = NE_GetPtr( hModule )))
112 MESSAGE( "**** %04x is not a module handle\n", hModule );
116 /* Dump the module info */
118 DPRINTF( "Module %04x:\n", hModule );
119 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
120 pModule->count, pModule->flags,
121 pModule->heap_size, pModule->stack_size );
122 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
123 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
124 pModule->seg_count, pModule->modref_count );
125 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
126 pModule->os_flags, pModule->min_swap_area,
127 pModule->expected_version );
128 if (pModule->flags & NE_FFLAGS_WIN32)
129 DPRINTF( "PE module=%p\n", pModule->module32 );
131 /* Dump the file info */
133 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
135 /* Dump the segment table */
137 DPRINTF( "Segment table:\n" );
138 pSeg = NE_SEG_TABLE( pModule );
139 for (i = 0; i < pModule->seg_count; i++, pSeg++)
140 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
141 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
142 pSeg->minsize, pSeg->hSeg );
144 /* Dump the resource table */
146 DPRINTF( "Resource table:\n" );
147 if (pModule->res_table)
149 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
150 DPRINTF( "Alignment: %d\n", *pword++ );
153 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
154 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
155 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
156 for (i = 0; i < ptr->count; i++, pname++)
157 DPRINTF( "offset=%d len=%d id=%04x\n",
158 pname->offset, pname->length, pname->id );
159 pword = (WORD *)pname;
162 else DPRINTF( "None\n" );
164 /* Dump the resident name table */
166 DPRINTF( "Resident-name table:\n" );
167 pstr = (char *)pModule + pModule->name_table;
170 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
171 *(WORD *)(pstr + *pstr + 1) );
172 pstr += *pstr + 1 + sizeof(WORD);
175 /* Dump the module reference table */
177 DPRINTF( "Module ref table:\n" );
178 if (pModule->modref_table)
180 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
181 for (i = 0; i < pModule->modref_count; i++, pword++)
184 GetModuleName16( *pword, name, sizeof(name) );
185 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
188 else DPRINTF( "None\n" );
190 /* Dump the entry table */
192 DPRINTF( "Entry table:\n" );
193 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
195 entry = (ET_ENTRY *)((BYTE *)bundle+6);
196 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
197 ordinal = bundle->first;
198 while (ordinal < bundle->last)
200 if (entry->type == 0xff)
201 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
203 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
206 } while ( (bundle->next)
207 && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
209 /* Dump the non-resident names table */
211 DPRINTF( "Non-resident names table:\n" );
212 if (pModule->nrname_handle)
214 pstr = (char *)GlobalLock16( pModule->nrname_handle );
217 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
218 *(WORD *)(pstr + *pstr + 1) );
219 pstr += *pstr + 1 + sizeof(WORD);
226 /***********************************************************************
229 * Walk the module list and print the modules.
231 void NE_WalkModules(void)
233 HMODULE16 hModule = hFirstModule;
234 MESSAGE( "Module Flags Name\n" );
237 NE_MODULE *pModule = NE_GetPtr( hModule );
240 MESSAGE( "Bad module %04x in list\n", hModule );
243 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->flags,
244 *((char *)pModule + pModule->name_table),
245 (char *)pModule + pModule->name_table + 1 );
246 hModule = pModule->next;
251 /**********************************************************************
254 void NE_RegisterModule( NE_MODULE *pModule )
256 pModule->next = hFirstModule;
257 hFirstModule = pModule->self;
261 /***********************************************************************
262 * NE_InitResourceHandler
264 * Fill in 'resloader' fields in the resource table.
266 void NE_InitResourceHandler( NE_MODULE *pModule )
268 static FARPROC16 proc;
270 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
272 TRACE("InitResourceHandler[%04x]\n", pModule->self );
274 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
276 while(pTypeInfo->type_id)
278 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
279 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
284 /***********************************************************************
287 * Lookup the ordinal for a given name.
289 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
291 unsigned char buffer[256], *cpnt;
295 if (!(pModule = NE_GetPtr( hModule ))) return 0;
296 if (pModule->flags & NE_FFLAGS_WIN32) return 0;
298 TRACE("(%04x,'%s')\n", hModule, name );
300 /* First handle names of the form '#xxxx' */
302 if (name[0] == '#') return atoi( name + 1 );
304 /* Now copy and uppercase the string */
306 strcpy( buffer, name );
307 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = FILE_toupper(*cpnt);
310 /* First search the resident names */
312 cpnt = (char *)pModule + pModule->name_table;
314 /* Skip the first entry (module name) */
315 cpnt += *cpnt + 1 + sizeof(WORD);
318 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
321 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
322 TRACE(" Found: ordinal=%d\n", ordinal );
325 cpnt += *cpnt + 1 + sizeof(WORD);
328 /* Now search the non-resident names table */
330 if (!pModule->nrname_handle) return 0; /* No non-resident table */
331 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
333 /* Skip the first entry (module description string) */
334 cpnt += *cpnt + 1 + sizeof(WORD);
337 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
340 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
341 TRACE(" Found: ordinal=%d\n", ordinal );
344 cpnt += *cpnt + 1 + sizeof(WORD);
350 /***********************************************************************
351 * EntryAddrProc (KERNEL.667) Wine-specific export
353 * Return the entry point for a given ordinal.
355 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
357 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
358 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
362 /***********************************************************************
365 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
367 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
370 /***********************************************************************
373 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
381 if (!(pModule = NE_GetPtr( hModule ))) return 0;
382 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
384 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
385 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
389 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
392 entry = (ET_ENTRY *)((BYTE *)bundle+6);
393 for (i=0; i < (ordinal - bundle->first - 1); i++)
397 memcpy( &offset, &entry->offs, sizeof(WORD) );
399 if (sel == 0xfe) sel = 0xffff; /* constant entry */
400 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
402 return (FARPROC16)MAKESEGPTR( sel, offset );
404 return (FARPROC16)MAKESEGPTR( sel, offset );
406 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
410 /***********************************************************************
413 * Change the value of an entry point. Use with caution!
414 * It can only change the offset value, not the selector.
416 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
423 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
424 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
426 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
427 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
429 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
434 entry = (ET_ENTRY *)((BYTE *)bundle+6);
435 for (i=0; i < (ordinal - bundle->first - 1); i++)
438 memcpy( &entry->offs, &offset, sizeof(WORD) );
443 /***********************************************************************
446 HANDLE NE_OpenFile( NE_MODULE *pModule )
451 TRACE("(%p)\n", pModule );
452 /* mjm - removed module caching because it keeps open file handles
453 thus preventing CDROMs from being ejected */
454 name = NE_MODULE_NAME( pModule );
455 if ((handle = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
456 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
457 MESSAGE( "Can't open file '%s' for module %04x\n", name, pModule->self );
458 TRACE("opened '%s' -> %p\n", name, handle);
463 /* wrapper for SetFilePointer and ReadFile */
464 static BOOL read_data( HANDLE handle, LONG offset, void *buffer, DWORD size )
468 if (SetFilePointer( handle, offset, NULL, SEEK_SET ) == INVALID_SET_FILE_POINTER) return FALSE;
469 if (!ReadFile( handle, buffer, size, &result, NULL )) return FALSE;
470 return (result == size);
473 /***********************************************************************
476 static HMODULE16 NE_LoadExeHeader( HANDLE handle, LPCSTR path )
478 IMAGE_DOS_HEADER mz_header;
479 IMAGE_OS2_HEADER ne_header;
483 BYTE *pData, *pTempEntryTable;
484 char *buffer, *fastload = NULL;
485 int fastload_offset = 0, fastload_length = 0;
487 ET_BUNDLE *bundle, *oldbundle;
490 /* Read a block from either the file or the fast-load area. */
491 #define READ(offset,size,buffer) \
492 ((fastload && ((offset) >= fastload_offset) && \
493 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
494 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
495 read_data( handle, (offset), (buffer), (size)))
497 if (!read_data( handle, 0, &mz_header, sizeof(mz_header)) ||
498 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
499 return (HMODULE16)11; /* invalid exe */
501 if (!read_data( handle, mz_header.e_lfanew, &ne_header, sizeof(ne_header) ))
502 return (HMODULE16)11; /* invalid exe */
504 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
505 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
506 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
507 return (HMODULE16)12;
509 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
511 /* We now have a valid NE header */
513 size = sizeof(NE_MODULE) +
515 ne_header.ne_cseg * sizeof(SEGTABLEENTRY) +
517 ne_header.ne_restab - ne_header.ne_rsrctab +
518 /* resident names table */
519 ne_header.ne_modtab - ne_header.ne_restab +
520 /* module ref table */
521 ne_header.ne_cmod * sizeof(WORD) +
522 /* imported names table */
523 ne_header.ne_enttab - ne_header.ne_imptab +
524 /* entry table length */
525 ne_header.ne_cbenttab +
526 /* entry table extra conversion space */
528 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6) +
529 /* loaded file info */
530 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
532 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
533 if (!hModule) return (HMODULE16)11; /* invalid exe */
535 FarSetOwner16( hModule, hModule );
536 pModule = (NE_MODULE *)GlobalLock16( hModule );
537 memcpy( pModule, &ne_header, sizeof(ne_header) );
539 /* check *programs* for default minimal stack size */
540 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
541 && (pModule->stack_size < 0x1400) )
542 pModule->stack_size = 0x1400;
543 pModule->module32 = 0;
544 pModule->self = hModule;
545 pModule->self_loading_sel = 0;
546 pData = (BYTE *)(pModule + 1);
548 /* Clear internal Wine flags in case they are set in the EXE file */
550 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
552 /* Read the fast-load area */
554 if (ne_header.ne_flagsothers & NE_AFLAGS_FASTLOAD)
556 fastload_offset=ne_header.ne_pretthunks << ne_header.ne_align;
557 fastload_length=ne_header.ne_psegrefbytes << ne_header.ne_align;
558 TRACE("Using fast-load area offset=%x len=%d\n",
559 fastload_offset, fastload_length );
560 if ((fastload = HeapAlloc( GetProcessHeap(), 0, fastload_length )) != NULL)
562 if (!read_data( handle, fastload_offset, fastload, fastload_length))
564 HeapFree( GetProcessHeap(), 0, fastload );
565 WARN("Error reading fast-load area!\n");
571 /* Get the segment table */
573 pModule->seg_table = pData - (BYTE *)pModule;
574 buffer = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cseg *
575 sizeof(struct ne_segment_table_entry_s));
579 struct ne_segment_table_entry_s *pSeg;
581 if (!READ( mz_header.e_lfanew + ne_header.ne_segtab,
582 ne_header.ne_cseg * sizeof(struct ne_segment_table_entry_s),
585 HeapFree( GetProcessHeap(), 0, buffer );
587 HeapFree( GetProcessHeap(), 0, fastload );
588 GlobalFree16( hModule );
589 return (HMODULE16)11; /* invalid exe */
591 pSeg = (struct ne_segment_table_entry_s *)buffer;
592 for (i = ne_header.ne_cseg; i > 0; i--, pSeg++)
594 memcpy( pData, pSeg, sizeof(*pSeg) );
595 pData += sizeof(SEGTABLEENTRY);
597 HeapFree( GetProcessHeap(), 0, buffer );
602 HeapFree( GetProcessHeap(), 0, fastload );
603 GlobalFree16( hModule );
604 return (HMODULE16)11; /* invalid exe */
607 /* Get the resource table */
609 if (ne_header.ne_rsrctab < ne_header.ne_restab)
611 pModule->res_table = pData - (BYTE *)pModule;
612 if (!READ(mz_header.e_lfanew + ne_header.ne_rsrctab,
613 ne_header.ne_restab - ne_header.ne_rsrctab,
615 return (HMODULE16)11; /* invalid exe */
616 pData += ne_header.ne_restab - ne_header.ne_rsrctab;
617 NE_InitResourceHandler( pModule );
619 else pModule->res_table = 0; /* No resource table */
621 /* Get the resident names table */
623 pModule->name_table = pData - (BYTE *)pModule;
624 if (!READ( mz_header.e_lfanew + ne_header.ne_restab,
625 ne_header.ne_modtab - ne_header.ne_restab,
629 HeapFree( GetProcessHeap(), 0, fastload );
630 GlobalFree16( hModule );
631 return (HMODULE16)11; /* invalid exe */
633 pData += ne_header.ne_modtab - ne_header.ne_restab;
635 /* Get the module references table */
637 if (ne_header.ne_cmod > 0)
639 pModule->modref_table = pData - (BYTE *)pModule;
640 if (!READ( mz_header.e_lfanew + ne_header.ne_modtab,
641 ne_header.ne_cmod * sizeof(WORD),
645 HeapFree( GetProcessHeap(), 0, fastload );
646 GlobalFree16( hModule );
647 return (HMODULE16)11; /* invalid exe */
649 pData += ne_header.ne_cmod * sizeof(WORD);
651 else pModule->modref_table = 0; /* No module references */
653 /* Get the imported names table */
655 pModule->import_table = pData - (BYTE *)pModule;
656 if (!READ( mz_header.e_lfanew + ne_header.ne_imptab,
657 ne_header.ne_enttab - ne_header.ne_imptab,
661 HeapFree( GetProcessHeap(), 0, fastload );
662 GlobalFree16( hModule );
663 return (HMODULE16)11; /* invalid exe */
665 pData += ne_header.ne_enttab - ne_header.ne_imptab;
667 /* Load entry table, convert it to the optimized version used by Windows */
669 if ((pTempEntryTable = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cbenttab)) != NULL)
671 BYTE nr_entries, type, *s;
673 TRACE("Converting entry table.\n");
674 pModule->entry_table = pData - (BYTE *)pModule;
675 if (!READ( mz_header.e_lfanew + ne_header.ne_enttab,
676 ne_header.ne_cbenttab, pTempEntryTable ))
678 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
680 HeapFree( GetProcessHeap(), 0, fastload );
681 GlobalFree16( hModule );
682 return (HMODULE16)11; /* invalid exe */
686 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header.ne_enttab, ne_header.ne_cbenttab, *s);
688 bundle = (ET_BUNDLE *)pData;
689 TRACE("first bundle: %p\n", bundle);
690 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
691 entry = (ET_ENTRY *)((BYTE *)bundle+6);
693 while ((nr_entries = *s++))
697 bundle->last += nr_entries;
704 entry->segnum = *s++;
705 entry->offs = *(WORD *)s; s += 2;
706 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
714 entry->segnum = type;
715 entry->offs = *(WORD *)s; s += 2;
716 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
722 if (bundle->first == bundle->last)
724 bundle->first += nr_entries;
725 bundle->last += nr_entries;
730 oldbundle->next = ((int)entry - (int)pModule);
731 bundle = (ET_BUNDLE *)entry;
732 TRACE("new bundle: %p\n", bundle);
733 bundle->first = bundle->last =
734 oldbundle->last + nr_entries;
736 (BYTE *)entry += sizeof(ET_BUNDLE);
740 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
745 HeapFree( GetProcessHeap(), 0, fastload );
746 GlobalFree16( hModule );
747 return (HMODULE16)11; /* invalid exe */
750 pData += ne_header.ne_cbenttab + sizeof(ET_BUNDLE) +
751 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6);
753 if ((DWORD)entry > (DWORD)pData)
754 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
756 /* Store the filename information */
758 pModule->fileinfo = pData - (BYTE *)pModule;
759 size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
760 ofs = (OFSTRUCT *)pData;
761 ofs->cBytes = size - 1;
763 strcpy( ofs->szPathName, path );
766 /* Free the fast-load area */
770 HeapFree( GetProcessHeap(), 0, fastload );
772 /* Get the non-resident names table */
774 if (ne_header.ne_cbnrestab)
776 pModule->nrname_handle = GlobalAlloc16( 0, ne_header.ne_cbnrestab );
777 if (!pModule->nrname_handle)
779 GlobalFree16( hModule );
780 return (HMODULE16)11; /* invalid exe */
782 FarSetOwner16( pModule->nrname_handle, hModule );
783 buffer = GlobalLock16( pModule->nrname_handle );
784 if (!read_data( handle, ne_header.ne_nrestab, buffer, ne_header.ne_cbnrestab ))
786 GlobalFree16( pModule->nrname_handle );
787 GlobalFree16( hModule );
788 return (HMODULE16)11; /* invalid exe */
791 else pModule->nrname_handle = 0;
793 /* Allocate a segment for the implicitly-loaded DLLs */
795 if (pModule->modref_count)
797 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
798 (pModule->modref_count+1)*sizeof(HMODULE16) );
799 if (!pModule->dlls_to_init)
801 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
802 GlobalFree16( hModule );
803 return (HMODULE16)11; /* invalid exe */
805 FarSetOwner16( pModule->dlls_to_init, hModule );
807 else pModule->dlls_to_init = 0;
809 NE_RegisterModule( pModule );
810 SNOOP16_RegisterDLL(pModule,path);
815 /***********************************************************************
818 * Load all DLLs implicitly linked to a module.
820 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
823 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
824 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
826 for (i = 0; i < pModule->modref_count; i++, pModRef++)
828 char buffer[260], *p;
829 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
830 memcpy( buffer, pstr + 1, *pstr );
831 *(buffer + *pstr) = 0; /* terminate it */
833 TRACE("Loading '%s'\n", buffer );
834 if (!(*pModRef = GetModuleHandle16( buffer )))
836 /* If the DLL is not loaded yet, load it and store */
837 /* its handle in the list of DLLs to initialize. */
840 /* Append .DLL to name if no extension present */
841 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
842 strcat( buffer, ".DLL" );
844 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
846 /* FIXME: cleanup what was done */
848 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
849 buffer, *((BYTE*)pModule + pModule->name_table),
850 (char *)pModule + pModule->name_table + 1, hDLL );
853 *pModRef = GetExePtr( hDLL );
856 else /* Increment the reference count of the DLL */
860 pOldDLL = NE_GetPtr( *pModRef );
861 if (pOldDLL) pOldDLL->count++;
868 /**********************************************************************
871 * Load first instance of NE module from file.
873 * pModule must point to a module structure prepared by NE_LoadExeHeader.
874 * This routine must never be called twice on a module.
877 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
879 /* Allocate the segments for this module */
881 if (!NE_CreateAllSegments( pModule ))
882 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
884 /* Load the referenced DLLs */
886 if (!NE_LoadDLLs( pModule ))
887 return ERROR_FILE_NOT_FOUND; /* 2 */
889 /* Load the segments */
891 NE_LoadAllSegments( pModule );
893 /* Make sure the usage count is 1 on the first loading of */
894 /* the module, even if it contains circular DLL references */
898 return NE_GetInstance( pModule );
901 /**********************************************************************
904 * Load first instance of NE module. (Note: caller is responsible for
905 * ensuring the module isn't already loaded!)
907 * If the module turns out to be an executable module, only a
908 * handle to a module stub is returned; this needs to be initialized
909 * by calling NE_DoLoadModule later, in the context of the newly
912 * If lib_only is TRUE, however, the module is perforce treated
913 * like a DLL module, even if it is an executable module.
916 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
920 HINSTANCE16 hInstance;
925 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
926 return (HMODULE16)2; /* File not found */
928 hModule = NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile), ofs.szPathName );
930 if (hModule < 32) return hModule;
932 pModule = NE_GetPtr( hModule );
933 if ( !pModule ) return hModule;
935 if ( !lib_only && !( pModule->flags & NE_FFLAGS_LIBMODULE ) )
938 hInstance = NE_DoLoadModule( pModule );
939 if ( hInstance < 32 )
942 NE_FreeModule( hModule, 0 );
949 /**********************************************************************
950 * MODULE_LoadModule16
952 * Load a NE module in the order of the loadorder specification.
953 * The caller is responsible that the module is not loaded already.
956 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
958 HINSTANCE16 hinst = 2;
959 enum loadorder_type loadorder[LOADORDER_NTYPES];
961 const char *filetype = "";
963 MODULE_GetLoadOrder(loadorder, libname, FALSE);
965 for(i = 0; i < LOADORDER_NTYPES; i++)
967 if (loadorder[i] == LOADORDER_INVALID) break;
972 TRACE("Trying native dll '%s'\n", libname);
973 hinst = NE_LoadModule(libname, lib_only);
978 TRACE("Trying built-in '%s'\n", libname);
979 hinst = BUILTIN_LoadModule(libname);
980 filetype = "builtin";
990 TRACE_(loaddll)("Loaded module '%s' : %s\n", libname, filetype);
996 hModule = GetModuleHandle16(libname);
999 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1001 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1004 pModule = NE_GetPtr(hModule);
1007 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1009 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1012 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1015 * Call initialization routines for all loaded DLLs. Note that
1016 * when we load implicitly linked DLLs this will be done by InitTask().
1018 if(pModule->flags & NE_FFLAGS_LIBMODULE)
1020 NE_InitializeDLLs(hModule);
1021 NE_DllProcessAttach(hModule);
1029 /* We quit searching when we get another error than 'File not found' */
1033 return hinst; /* The last error that occured */
1037 /**********************************************************************
1040 * Create the thread for a 16-bit module.
1042 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1047 HINSTANCE16 instance = 0;
1049 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1052 /* Post event to start the task */
1053 PostEvent16( hTask );
1055 /* Wait until we get the instance handle */
1058 DirectedYield16( hTask );
1059 if (!IsTask16( hTask )) /* thread has died */
1062 WaitForSingleObject( hThread, INFINITE );
1063 GetExitCodeThread( hThread, &exit_code );
1064 CloseHandle( hThread );
1067 if (!(pTask = TASK_GetPtr( hTask ))) break;
1068 instance = pTask->hInstance;
1069 GlobalUnlock16( hTask );
1070 } while (!instance);
1072 CloseHandle( hThread );
1077 /**********************************************************************
1078 * LoadModule (KERNEL.45)
1080 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1082 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1083 LOADPARAMS16 *params;
1091 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1093 /* Special case: second instance of an already loaded NE module */
1095 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
1096 if ( pModule->module32 ) return (HINSTANCE16)21;
1098 /* Increment refcount */
1104 /* Main case: load first instance of NE module */
1106 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1109 if ( !(pModule = NE_GetPtr( hModule )) )
1110 return (HINSTANCE16)11;
1113 /* If library module, we just retrieve the instance handle */
1115 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1116 return NE_GetInstance( pModule );
1119 * At this point, we need to create a new process.
1121 * pModule points either to an already loaded module, whose refcount
1122 * has already been incremented (to avoid having the module vanish
1123 * in the meantime), or else to a stub module which contains only header
1126 params = (LOADPARAMS16 *)paramBlock;
1127 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1128 cmdline = MapSL( params->cmdLine );
1129 return NE_CreateThread( pModule, cmdShow, cmdline );
1133 /**********************************************************************
1136 * Startup code for a new 16-bit task.
1138 DWORD NE_StartTask(void)
1140 TDB *pTask = TASK_GetCurrent();
1141 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1142 HINSTANCE16 hInstance, hPrevInstance;
1143 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1146 if ( pModule->count > 0 )
1148 /* Second instance of an already loaded NE module */
1149 /* Note that the refcount was already incremented by the parent */
1151 hPrevInstance = NE_GetInstance( pModule );
1153 if ( pModule->dgroup )
1154 if ( NE_CreateSegment( pModule, pModule->dgroup ) )
1155 NE_LoadSegment( pModule, pModule->dgroup );
1157 hInstance = NE_GetInstance( pModule );
1158 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->dgroup, hPrevInstance);
1163 /* Load first instance of NE module */
1165 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1167 hInstance = NE_DoLoadModule( pModule );
1171 if ( hInstance >= 32 )
1175 /* Enter instance handles into task struct */
1177 pTask->hInstance = hInstance;
1178 pTask->hPrevInstance = hPrevInstance;
1180 /* Use DGROUP for 16-bit stack */
1182 if (!(sp = pModule->sp))
1183 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
1185 sp -= sizeof(STACK16FRAME);
1186 pTask->teb->cur_stack = MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1188 /* Registers at initialization must be:
1190 * bx stack size in bytes
1191 * cx heap size in bytes
1192 * si previous app instance
1193 * di current app instance
1195 * es selector to the PSP
1196 * ds dgroup of the application
1198 * sp top of the stack
1200 memset( &context, 0, sizeof(context) );
1201 context.SegCs = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
1202 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1203 context.SegEs = pTask->hPDB;
1204 context.Eip = pModule->ip;
1205 context.Ebx = pModule->stack_size;
1206 context.Ecx = pModule->heap_size;
1207 context.Edi = pTask->hInstance;
1208 context.Esi = pTask->hPrevInstance;
1210 /* Now call 16-bit entry point */
1212 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1213 context.SegCs, context.Eip, context.SegDs,
1214 SELECTOROF(pTask->teb->cur_stack),
1215 OFFSETOF(pTask->teb->cur_stack) );
1217 wine_call_to_16_regs_short( &context, 0 );
1218 ExitThread( LOWORD(context.Eax) );
1220 return hInstance; /* error code */
1223 /***********************************************************************
1224 * LoadLibrary (KERNEL.95)
1225 * LoadLibrary16 (KERNEL32.35)
1227 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1229 return LoadModule16(libname, (LPVOID)-1 );
1233 /**********************************************************************
1236 * Call a DLL's WEP, allowing it to shut down.
1237 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1239 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1242 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1243 if (!WEP) return FALSE;
1247 ret = NE_CallTo16_word_w( WEP, WEP_FREE_DLL );
1249 __EXCEPT(page_fault)
1251 WARN("Page fault\n");
1260 /**********************************************************************
1263 * Implementation of FreeModule16().
1265 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1267 HMODULE16 *hPrevModule;
1272 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1273 hModule = pModule->self;
1275 TRACE("%04x count %d\n", hModule, pModule->count );
1277 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1278 else pModule->count = 0;
1280 if (pModule->flags & NE_FFLAGS_BUILTIN)
1281 return FALSE; /* Can't free built-in module */
1283 if (call_wep && !(pModule->flags & NE_FFLAGS_WIN32))
1285 /* Free the objects owned by the DLL module */
1286 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1288 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1289 MODULE_CallWEP( hModule );
1291 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1295 /* Clear magic number just in case */
1297 pModule->magic = pModule->self = 0;
1299 /* Remove it from the linked list */
1301 hPrevModule = &hFirstModule;
1302 while (*hPrevModule && (*hPrevModule != hModule))
1304 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1306 if (*hPrevModule) *hPrevModule = pModule->next;
1308 /* Free the referenced modules */
1310 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1311 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1313 NE_FreeModule( *pModRef, call_wep );
1316 /* Free the module storage */
1318 GlobalFreeAll16( hModule );
1320 /* Remove module from cache */
1322 if (pCachedModule == pModule) pCachedModule = NULL;
1327 /**********************************************************************
1328 * FreeModule (KERNEL.46)
1330 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1332 return NE_FreeModule( hModule, TRUE );
1336 /***********************************************************************
1337 * FreeLibrary (KERNEL.96)
1338 * FreeLibrary16 (KERNEL32.36)
1340 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1342 TRACE("%04x\n", handle );
1343 FreeModule16( handle );
1347 /**********************************************************************
1348 * GetModuleName (KERNEL.27)
1350 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1355 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1356 p = (BYTE *)pModule + pModule->name_table;
1357 if (count > *p) count = *p + 1;
1360 memcpy( buf, p + 1, count - 1 );
1361 buf[count-1] = '\0';
1367 /**********************************************************************
1368 * GetModuleUsage (KERNEL.48)
1370 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1372 NE_MODULE *pModule = NE_GetPtr( hModule );
1373 return pModule ? pModule->count : 0;
1377 /**********************************************************************
1378 * GetExpWinVer (KERNEL.167)
1380 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1382 NE_MODULE *pModule = NE_GetPtr( hModule );
1383 if ( !pModule ) return 0;
1386 * For built-in modules, fake the expected version the module should
1387 * have according to the Windows version emulated by Wine
1389 if ( !pModule->expected_version )
1391 OSVERSIONINFOA versionInfo;
1392 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1394 if ( GetVersionExA( &versionInfo ) )
1395 pModule->expected_version =
1396 (versionInfo.dwMajorVersion & 0xff) << 8
1397 | (versionInfo.dwMinorVersion & 0xff);
1400 return pModule->expected_version;
1404 /**********************************************************************
1405 * GetModuleFileName (KERNEL.49)
1407 * Comment: see GetModuleFileNameA
1409 * Even if invoked by second instance of a program,
1410 * it still returns path of first one.
1412 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1417 /* Win95 does not query hModule if set to 0 !
1418 * Is this wrong or maybe Win3.1 only ? */
1419 if (!hModule) hModule = GetCurrentTask();
1421 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1422 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1423 if (pModule->expected_version >= 0x400)
1424 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1425 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1426 return strlen(lpFileName);
1430 /**********************************************************************
1431 * GetModuleHandle (KERNEL.47)
1433 * Find a module from a module name.
1435 * NOTE: The current implementation works the same way the Windows 95 one
1436 * does. Do not try to 'fix' it, fix the callers.
1437 * + It does not do ANY extension handling (except that strange .EXE bit)!
1438 * + It does not care about paths, just about basenames. (same as Windows)
1442 * the win16 module handle if found
1444 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1445 * Always hFirstModule
1447 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1449 if (HIWORD(name) == 0)
1450 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1451 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1454 /***********************************************************************
1455 * GetModuleHandle16 (KERNEL32.@)
1457 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1459 HMODULE16 hModule = hFirstModule;
1461 BYTE len, *name_table;
1462 char tmpstr[MAX_PATH];
1465 TRACE("(%s)\n", name);
1468 return GetExePtr(LOWORD(name));
1474 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1476 /* If 'name' matches exactly the module name of a module:
1477 * Return its handle.
1479 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1481 pModule = NE_GetPtr( hModule );
1482 if (!pModule) break;
1483 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1485 name_table = (BYTE *)pModule + pModule->name_table;
1486 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1490 /* If uppercased 'name' matches exactly the module name of a module:
1493 for (s = tmpstr; *s; s++) *s = FILE_toupper(*s);
1495 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1497 pModule = NE_GetPtr( hModule );
1498 if (!pModule) break;
1499 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1501 name_table = (BYTE *)pModule + pModule->name_table;
1502 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1503 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1504 * lowercased module names, but try to load uppercase DLLs, so this
1505 * 'i' compare is just a quickfix until the loader handles that
1506 * correctly. -MM 990705
1508 if ((*name_table == len) && !FILE_strncasecmp(tmpstr, name_table+1, len))
1512 /* If the base filename of 'name' matches the base filename of the module
1513 * filename of some module (case-insensitive compare):
1514 * Return its handle.
1517 /* basename: search backwards in passed name to \ / or : */
1518 s = tmpstr + strlen(tmpstr);
1521 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1526 /* search this in loaded filename list */
1527 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1532 pModule = NE_GetPtr( hModule );
1533 if (!pModule) break;
1534 if (!pModule->fileinfo) continue;
1535 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1537 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1538 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1539 /* basename: search backwards in pathname to \ / or : */
1540 while (loadedfn > (char*)ofs->szPathName)
1542 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1546 /* case insensitive compare ... */
1547 if (!FILE_strcasecmp(loadedfn, s))
1551 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1552 * matches the base filename of the module filename of some 32-bit module:
1553 * Return the corresponding 16-bit dummy module handle.
1555 if (len >= 4 && !FILE_strcasecmp(name+len-4, ".EXE"))
1557 HMODULE hModule = GetModuleHandleA( name );
1559 return MapHModuleLS( hModule );
1562 if (!strcmp(tmpstr,"MSDOS"))
1565 if (!strcmp(tmpstr,"TIMER"))
1567 FIXME("Eh... Should return caller's code segment, expect crash\n");
1574 /**********************************************************************
1575 * NE_GetModuleByFilename
1577 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1581 BYTE len, *name_table;
1582 char tmpstr[MAX_PATH];
1585 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1587 /* If the base filename of 'name' matches the base filename of the module
1588 * filename of some module (case-insensitive compare):
1589 * Return its handle.
1592 /* basename: search backwards in passed name to \ / or : */
1593 s = tmpstr + strlen(tmpstr);
1596 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1601 /* search this in loaded filename list */
1602 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1607 pModule = NE_GetPtr( hModule );
1608 if (!pModule) break;
1609 if (!pModule->fileinfo) continue;
1610 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1612 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1613 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1614 /* basename: search backwards in pathname to \ / or : */
1615 while (loadedfn > (char*)ofs->szPathName)
1617 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1621 /* case insensitive compare ... */
1622 if (!FILE_strcasecmp(loadedfn, s))
1625 /* If basename (without ext) matches the module name of a module:
1626 * Return its handle.
1629 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1632 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1634 pModule = NE_GetPtr( hModule );
1635 if (!pModule) break;
1636 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1638 name_table = (BYTE *)pModule + pModule->name_table;
1639 if ((*name_table == len) && !FILE_strncasecmp(s, name_table+1, len))
1646 /**********************************************************************
1647 * ModuleFirst (TOOLHELP.59)
1649 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1651 lpme->wNext = hFirstModule;
1652 return ModuleNext16( lpme );
1656 /**********************************************************************
1657 * ModuleNext (TOOLHELP.60)
1659 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1664 if (!lpme->wNext) return FALSE;
1665 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1666 name = (char *)pModule + pModule->name_table;
1667 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1668 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1669 lpme->hModule = lpme->wNext;
1670 lpme->wcUsage = pModule->count;
1671 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1672 lpme->wNext = pModule->next;
1677 /**********************************************************************
1678 * ModuleFindName (TOOLHELP.61)
1680 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1682 lpme->wNext = GetModuleHandle16( name );
1683 return ModuleNext16( lpme );
1687 /**********************************************************************
1688 * ModuleFindHandle (TOOLHELP.62)
1690 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1692 hModule = GetExePtr( hModule );
1693 lpme->wNext = hModule;
1694 return ModuleNext16( lpme );
1698 /***************************************************************************
1699 * IsRomModule (KERNEL.323)
1701 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1706 /***************************************************************************
1707 * IsRomFile (KERNEL.326)
1709 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1714 /***************************************************************************
1715 * MapHModuleLS (KERNEL32.@)
1717 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
1723 return TASK_GetCurrent()->hInstance;
1725 return LOWORD(hmod); /* we already have a 16 bit module handle */
1726 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1728 if (pModule->module32 == hmod)
1729 return pModule->self;
1730 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1732 if ((ret = MODULE_CreateDummyModule( hmod )) < 32)
1740 /***************************************************************************
1741 * MapHModuleSL (KERNEL32.@)
1743 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1747 TDB *pTask = TASK_GetCurrent();
1748 hmod = pTask->hModule;
1750 pModule = (NE_MODULE*)GlobalLock16(hmod);
1751 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1752 !(pModule->flags & NE_FFLAGS_WIN32)
1755 return pModule->module32;
1758 /***************************************************************************
1759 * MapHInstLS (KERNEL32.@)
1760 * MapHInstLS (KERNEL.472)
1762 void WINAPI MapHInstLS( CONTEXT86 *context )
1764 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
1767 /***************************************************************************
1768 * MapHInstSL (KERNEL32.@)
1769 * MapHInstSL (KERNEL.473)
1771 void WINAPI MapHInstSL( CONTEXT86 *context )
1773 context->Eax = (DWORD)MapHModuleSL( context->Eax );
1776 /***************************************************************************
1777 * MapHInstLS_PN (KERNEL32.@)
1779 void WINAPI MapHInstLS_PN( CONTEXT86 *context )
1781 if (context->Eax) context->Eax = MapHModuleLS( (HMODULE)context->Eax );
1784 /***************************************************************************
1785 * MapHInstSL_PN (KERNEL32.@)
1787 void WINAPI MapHInstSL_PN( CONTEXT86 *context )
1789 if (context->Eax) context->Eax = (DWORD)MapHModuleSL( context->Eax );