Removed trailing whitespace.
[wine/wine-gecko.git] / loader / ne / module.c
blob2eebe40f71e1d90c261c97e2b0d3d74fee8dcf45
1 /*
2 * NE modules
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <ctype.h>
31 #include "wine/winbase16.h"
32 #include "wine/library.h"
33 #include "winerror.h"
34 #include "module.h"
35 #include "toolhelp.h"
36 #include "file.h"
37 #include "task.h"
38 #include "snoop.h"
39 #include "builtin16.h"
40 #include "stackframe.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(module);
44 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
47 * Segment table entry
49 struct ne_segment_table_entry_s
51 WORD seg_data_offset; /* Sector offset of segment data */
52 WORD seg_data_length; /* Length of segment data */
53 WORD seg_flags; /* Flags associated with this segment */
54 WORD min_alloc; /* Minimum allocation size for this */
57 #define hFirstModule (pThhook->hExeHead)
59 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
61 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
62 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
64 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
66 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
68 /* ### start build ### */
69 extern WORD CALLBACK NE_CallTo16_word_w(FARPROC16,WORD);
70 /* ### stop build ### */
72 /***********************************************************************
73 * NE_GetPtr
75 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
77 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
81 /***********************************************************************
82 * NE_DumpModule
84 void NE_DumpModule( HMODULE16 hModule )
86 int i, ordinal;
87 SEGTABLEENTRY *pSeg;
88 BYTE *pstr;
89 WORD *pword;
90 NE_MODULE *pModule;
91 ET_BUNDLE *bundle;
92 ET_ENTRY *entry;
94 if (!(pModule = NE_GetPtr( hModule )))
96 MESSAGE( "**** %04x is not a module handle\n", hModule );
97 return;
100 /* Dump the module info */
101 DPRINTF( "---\n" );
102 DPRINTF( "Module %04x:\n", hModule );
103 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
104 pModule->count, pModule->flags,
105 pModule->heap_size, pModule->stack_size );
106 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
107 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
108 pModule->seg_count, pModule->modref_count );
109 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
110 pModule->os_flags, pModule->min_swap_area,
111 pModule->expected_version );
112 if (pModule->flags & NE_FFLAGS_WIN32)
113 DPRINTF( "PE module=%08x\n", pModule->module32 );
115 /* Dump the file info */
116 DPRINTF( "---\n" );
117 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
119 /* Dump the segment table */
120 DPRINTF( "---\n" );
121 DPRINTF( "Segment table:\n" );
122 pSeg = NE_SEG_TABLE( pModule );
123 for (i = 0; i < pModule->seg_count; i++, pSeg++)
124 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
125 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
126 pSeg->minsize, pSeg->hSeg );
128 /* Dump the resource table */
129 DPRINTF( "---\n" );
130 DPRINTF( "Resource table:\n" );
131 if (pModule->res_table)
133 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
134 DPRINTF( "Alignment: %d\n", *pword++ );
135 while (*pword)
137 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
138 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
139 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
140 for (i = 0; i < ptr->count; i++, pname++)
141 DPRINTF( "offset=%d len=%d id=%04x\n",
142 pname->offset, pname->length, pname->id );
143 pword = (WORD *)pname;
146 else DPRINTF( "None\n" );
148 /* Dump the resident name table */
149 DPRINTF( "---\n" );
150 DPRINTF( "Resident-name table:\n" );
151 pstr = (char *)pModule + pModule->name_table;
152 while (*pstr)
154 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
155 *(WORD *)(pstr + *pstr + 1) );
156 pstr += *pstr + 1 + sizeof(WORD);
159 /* Dump the module reference table */
160 DPRINTF( "---\n" );
161 DPRINTF( "Module ref table:\n" );
162 if (pModule->modref_table)
164 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
165 for (i = 0; i < pModule->modref_count; i++, pword++)
167 char name[10];
168 GetModuleName16( *pword, name, sizeof(name) );
169 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
172 else DPRINTF( "None\n" );
174 /* Dump the entry table */
175 DPRINTF( "---\n" );
176 DPRINTF( "Entry table:\n" );
177 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
178 do {
179 entry = (ET_ENTRY *)((BYTE *)bundle+6);
180 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
181 ordinal = bundle->first;
182 while (ordinal < bundle->last)
184 if (entry->type == 0xff)
185 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
186 else
187 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
188 entry++;
190 } while ( (bundle->next)
191 && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
193 /* Dump the non-resident names table */
194 DPRINTF( "---\n" );
195 DPRINTF( "Non-resident names table:\n" );
196 if (pModule->nrname_handle)
198 pstr = (char *)GlobalLock16( pModule->nrname_handle );
199 while (*pstr)
201 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
202 *(WORD *)(pstr + *pstr + 1) );
203 pstr += *pstr + 1 + sizeof(WORD);
206 DPRINTF( "\n" );
210 /***********************************************************************
211 * NE_WalkModules
213 * Walk the module list and print the modules.
215 void NE_WalkModules(void)
217 HMODULE16 hModule = hFirstModule;
218 MESSAGE( "Module Flags Name\n" );
219 while (hModule)
221 NE_MODULE *pModule = NE_GetPtr( hModule );
222 if (!pModule)
224 MESSAGE( "Bad module %04x in list\n", hModule );
225 return;
227 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->flags,
228 *((char *)pModule + pModule->name_table),
229 (char *)pModule + pModule->name_table + 1 );
230 hModule = pModule->next;
235 /**********************************************************************
236 * NE_RegisterModule
238 void NE_RegisterModule( NE_MODULE *pModule )
240 pModule->next = hFirstModule;
241 hFirstModule = pModule->self;
245 /***********************************************************************
246 * NE_GetOrdinal
248 * Lookup the ordinal for a given name.
250 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
252 unsigned char buffer[256], *cpnt;
253 BYTE len;
254 NE_MODULE *pModule;
256 if (!(pModule = NE_GetPtr( hModule ))) return 0;
257 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
259 TRACE("(%04x,'%s')\n", hModule, name );
261 /* First handle names of the form '#xxxx' */
263 if (name[0] == '#') return atoi( name + 1 );
265 /* Now copy and uppercase the string */
267 strcpy( buffer, name );
268 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = FILE_toupper(*cpnt);
269 len = cpnt - buffer;
271 /* First search the resident names */
273 cpnt = (char *)pModule + pModule->name_table;
275 /* Skip the first entry (module name) */
276 cpnt += *cpnt + 1 + sizeof(WORD);
277 while (*cpnt)
279 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
281 WORD ordinal;
282 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
283 TRACE(" Found: ordinal=%d\n", ordinal );
284 return ordinal;
286 cpnt += *cpnt + 1 + sizeof(WORD);
289 /* Now search the non-resident names table */
291 if (!pModule->nrname_handle) return 0; /* No non-resident table */
292 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
294 /* Skip the first entry (module description string) */
295 cpnt += *cpnt + 1 + sizeof(WORD);
296 while (*cpnt)
298 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
300 WORD ordinal;
301 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
302 TRACE(" Found: ordinal=%d\n", ordinal );
303 return ordinal;
305 cpnt += *cpnt + 1 + sizeof(WORD);
307 return 0;
311 /***********************************************************************
312 * EntryAddrProc (KERNEL.667) Wine-specific export
314 * Return the entry point for a given ordinal.
316 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
318 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
319 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
320 return ret;
323 /***********************************************************************
324 * NE_GetEntryPoint
326 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
328 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
331 /***********************************************************************
332 * NE_GetEntryPointEx
334 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
336 NE_MODULE *pModule;
337 WORD sel, offset, i;
339 ET_ENTRY *entry;
340 ET_BUNDLE *bundle;
342 if (!(pModule = NE_GetPtr( hModule ))) return 0;
343 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
345 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
346 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
348 if (!(bundle->next))
349 return 0;
350 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
353 entry = (ET_ENTRY *)((BYTE *)bundle+6);
354 for (i=0; i < (ordinal - bundle->first - 1); i++)
355 entry++;
357 sel = entry->segnum;
358 memcpy( &offset, &entry->offs, sizeof(WORD) );
360 if (sel == 0xfe) sel = 0xffff; /* constant entry */
361 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
362 if (sel==0xffff)
363 return (FARPROC16)MAKESEGPTR( sel, offset );
364 if (!snoop)
365 return (FARPROC16)MAKESEGPTR( sel, offset );
366 else
367 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
371 /***********************************************************************
372 * NE_SetEntryPoint
374 * Change the value of an entry point. Use with caution!
375 * It can only change the offset value, not the selector.
377 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
379 NE_MODULE *pModule;
380 ET_ENTRY *entry;
381 ET_BUNDLE *bundle;
382 int i;
384 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
385 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
387 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
388 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
390 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
391 if (!(bundle->next))
392 return 0;
395 entry = (ET_ENTRY *)((BYTE *)bundle+6);
396 for (i=0; i < (ordinal - bundle->first - 1); i++)
397 entry++;
399 memcpy( &entry->offs, &offset, sizeof(WORD) );
400 return TRUE;
404 /***********************************************************************
405 * NE_OpenFile
407 HANDLE NE_OpenFile( NE_MODULE *pModule )
409 char *name;
411 static HANDLE cachedfd = INVALID_HANDLE_VALUE;
413 TRACE("(%p) cache: mod=%p fd=%d\n",
414 pModule, pCachedModule, cachedfd );
415 if (pCachedModule == pModule) return cachedfd;
416 CloseHandle( cachedfd );
417 pCachedModule = pModule;
418 name = NE_MODULE_NAME( pModule );
419 if ((cachedfd = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
420 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
421 MESSAGE( "Can't open file '%s' for module %04x\n", name, pModule->self );
422 TRACE("opened '%s' -> %d\n",
423 name, cachedfd );
424 return cachedfd;
428 /***********************************************************************
429 * NE_LoadExeHeader
431 static HMODULE16 NE_LoadExeHeader( HANDLE hFile, LPCSTR path )
433 IMAGE_DOS_HEADER mz_header;
434 IMAGE_OS2_HEADER ne_header;
435 int size;
436 HMODULE16 hModule;
437 NE_MODULE *pModule;
438 BYTE *pData, *pTempEntryTable;
439 char *buffer, *fastload = NULL;
440 int fastload_offset = 0, fastload_length = 0;
441 ET_ENTRY *entry;
442 ET_BUNDLE *bundle, *oldbundle;
443 OFSTRUCT *ofs;
445 /* Read a block from either the file or the fast-load area. */
446 #define READ(offset,size,buffer) \
447 ((fastload && ((offset) >= fastload_offset) && \
448 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
449 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
450 (_llseek( hFile, (offset), SEEK_SET), \
451 _lread( hFile, (buffer), (size) ) == (size)))
453 _llseek( hFile, 0, SEEK_SET );
454 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
455 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
456 return (HMODULE16)11; /* invalid exe */
458 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
459 if (_lread( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
460 return (HMODULE16)11; /* invalid exe */
462 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
463 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
464 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
465 return (HMODULE16)12;
467 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
469 /* We now have a valid NE header */
471 size = sizeof(NE_MODULE) +
472 /* segment table */
473 ne_header.ne_cseg * sizeof(SEGTABLEENTRY) +
474 /* resource table */
475 ne_header.ne_restab - ne_header.ne_rsrctab +
476 /* resident names table */
477 ne_header.ne_modtab - ne_header.ne_restab +
478 /* module ref table */
479 ne_header.ne_cmod * sizeof(WORD) +
480 /* imported names table */
481 ne_header.ne_enttab - ne_header.ne_imptab +
482 /* entry table length */
483 ne_header.ne_cbenttab +
484 /* entry table extra conversion space */
485 sizeof(ET_BUNDLE) +
486 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6) +
487 /* loaded file info */
488 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
490 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
491 if (!hModule) return (HMODULE16)11; /* invalid exe */
493 FarSetOwner16( hModule, hModule );
494 pModule = (NE_MODULE *)GlobalLock16( hModule );
495 memcpy( pModule, &ne_header, sizeof(ne_header) );
496 pModule->count = 0;
497 /* check *programs* for default minimal stack size */
498 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
499 && (pModule->stack_size < 0x1400) )
500 pModule->stack_size = 0x1400;
501 pModule->module32 = 0;
502 pModule->self = hModule;
503 pModule->self_loading_sel = 0;
504 pData = (BYTE *)(pModule + 1);
506 /* Clear internal Wine flags in case they are set in the EXE file */
508 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
510 /* Read the fast-load area */
512 if (ne_header.ne_flagsothers & NE_AFLAGS_FASTLOAD)
514 fastload_offset=ne_header.fastload_offset << ne_header.ne_align;
515 fastload_length=ne_header.fastload_length << ne_header.ne_align;
516 TRACE("Using fast-load area offset=%x len=%d\n",
517 fastload_offset, fastload_length );
518 if ((fastload = HeapAlloc( GetProcessHeap(), 0, fastload_length )) != NULL)
520 _llseek( hFile, fastload_offset, SEEK_SET);
521 if (_lread(hFile, fastload, fastload_length) != fastload_length)
523 HeapFree( GetProcessHeap(), 0, fastload );
524 WARN("Error reading fast-load area!\n");
525 fastload = NULL;
530 /* Get the segment table */
532 pModule->seg_table = pData - (BYTE *)pModule;
533 buffer = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cseg *
534 sizeof(struct ne_segment_table_entry_s));
535 if (buffer)
537 int i;
538 struct ne_segment_table_entry_s *pSeg;
540 if (!READ( mz_header.e_lfanew + ne_header.ne_segtab,
541 ne_header.ne_cseg * sizeof(struct ne_segment_table_entry_s),
542 buffer ))
544 HeapFree( GetProcessHeap(), 0, buffer );
545 if (fastload)
546 HeapFree( GetProcessHeap(), 0, fastload );
547 GlobalFree16( hModule );
548 return (HMODULE16)11; /* invalid exe */
550 pSeg = (struct ne_segment_table_entry_s *)buffer;
551 for (i = ne_header.ne_cseg; i > 0; i--, pSeg++)
553 memcpy( pData, pSeg, sizeof(*pSeg) );
554 pData += sizeof(SEGTABLEENTRY);
556 HeapFree( GetProcessHeap(), 0, buffer );
558 else
560 if (fastload)
561 HeapFree( GetProcessHeap(), 0, fastload );
562 GlobalFree16( hModule );
563 return (HMODULE16)11; /* invalid exe */
566 /* Get the resource table */
568 if (ne_header.ne_rsrctab < ne_header.ne_restab)
570 pModule->res_table = pData - (BYTE *)pModule;
571 if (!READ(mz_header.e_lfanew + ne_header.ne_rsrctab,
572 ne_header.ne_restab - ne_header.ne_rsrctab,
573 pData ))
574 return (HMODULE16)11; /* invalid exe */
575 pData += ne_header.ne_restab - ne_header.ne_rsrctab;
576 NE_InitResourceHandler( hModule );
578 else pModule->res_table = 0; /* No resource table */
580 /* Get the resident names table */
582 pModule->name_table = pData - (BYTE *)pModule;
583 if (!READ( mz_header.e_lfanew + ne_header.ne_restab,
584 ne_header.ne_modtab - ne_header.ne_restab,
585 pData ))
587 if (fastload)
588 HeapFree( GetProcessHeap(), 0, fastload );
589 GlobalFree16( hModule );
590 return (HMODULE16)11; /* invalid exe */
592 pData += ne_header.ne_modtab - ne_header.ne_restab;
594 /* Get the module references table */
596 if (ne_header.ne_cmod > 0)
598 pModule->modref_table = pData - (BYTE *)pModule;
599 if (!READ( mz_header.e_lfanew + ne_header.ne_modtab,
600 ne_header.ne_cmod * sizeof(WORD),
601 pData ))
603 if (fastload)
604 HeapFree( GetProcessHeap(), 0, fastload );
605 GlobalFree16( hModule );
606 return (HMODULE16)11; /* invalid exe */
608 pData += ne_header.ne_cmod * sizeof(WORD);
610 else pModule->modref_table = 0; /* No module references */
612 /* Get the imported names table */
614 pModule->import_table = pData - (BYTE *)pModule;
615 if (!READ( mz_header.e_lfanew + ne_header.ne_imptab,
616 ne_header.ne_enttab - ne_header.ne_imptab,
617 pData ))
619 if (fastload)
620 HeapFree( GetProcessHeap(), 0, fastload );
621 GlobalFree16( hModule );
622 return (HMODULE16)11; /* invalid exe */
624 pData += ne_header.ne_enttab - ne_header.ne_imptab;
626 /* Load entry table, convert it to the optimized version used by Windows */
628 if ((pTempEntryTable = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cbenttab)) != NULL)
630 BYTE nr_entries, type, *s;
632 TRACE("Converting entry table.\n");
633 pModule->entry_table = pData - (BYTE *)pModule;
634 if (!READ( mz_header.e_lfanew + ne_header.ne_enttab,
635 ne_header.ne_cbenttab, pTempEntryTable ))
637 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
638 if (fastload)
639 HeapFree( GetProcessHeap(), 0, fastload );
640 GlobalFree16( hModule );
641 return (HMODULE16)11; /* invalid exe */
644 s = pTempEntryTable;
645 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header.ne_enttab, ne_header.ne_cbenttab, *s);
647 bundle = (ET_BUNDLE *)pData;
648 TRACE("first bundle: %p\n", bundle);
649 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
650 entry = (ET_ENTRY *)((BYTE *)bundle+6);
652 while ((nr_entries = *s++))
654 if ((type = *s++))
656 bundle->last += nr_entries;
657 if (type == 0xff)
658 while (nr_entries--)
660 entry->type = type;
661 entry->flags = *s++;
662 s += 2;
663 entry->segnum = *s++;
664 entry->offs = *(WORD *)s; s += 2;
665 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
666 entry++;
668 else
669 while (nr_entries--)
671 entry->type = type;
672 entry->flags = *s++;
673 entry->segnum = type;
674 entry->offs = *(WORD *)s; s += 2;
675 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
676 entry++;
679 else
681 if (bundle->first == bundle->last)
683 bundle->first += nr_entries;
684 bundle->last += nr_entries;
686 else
688 oldbundle = bundle;
689 oldbundle->next = ((int)entry - (int)pModule);
690 bundle = (ET_BUNDLE *)entry;
691 TRACE("new bundle: %p\n", bundle);
692 bundle->first = bundle->last =
693 oldbundle->last + nr_entries;
694 bundle->next = 0;
695 (BYTE *)entry += sizeof(ET_BUNDLE);
699 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
701 else
703 if (fastload)
704 HeapFree( GetProcessHeap(), 0, fastload );
705 GlobalFree16( hModule );
706 return (HMODULE16)11; /* invalid exe */
709 pData += ne_header.ne_cbenttab + sizeof(ET_BUNDLE) +
710 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6);
712 if ((DWORD)entry > (DWORD)pData)
713 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
715 /* Store the filename information */
717 pModule->fileinfo = pData - (BYTE *)pModule;
718 size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
719 ofs = (OFSTRUCT *)pData;
720 ofs->cBytes = size - 1;
721 ofs->fFixedDisk = 1;
722 strcpy( ofs->szPathName, path );
723 pData += size;
725 /* Free the fast-load area */
727 #undef READ
728 if (fastload)
729 HeapFree( GetProcessHeap(), 0, fastload );
731 /* Get the non-resident names table */
733 if (ne_header.ne_cbnrestab)
735 pModule->nrname_handle = GlobalAlloc16( 0, ne_header.ne_cbnrestab );
736 if (!pModule->nrname_handle)
738 GlobalFree16( hModule );
739 return (HMODULE16)11; /* invalid exe */
741 FarSetOwner16( pModule->nrname_handle, hModule );
742 buffer = GlobalLock16( pModule->nrname_handle );
743 _llseek( hFile, ne_header.ne_nrestab, SEEK_SET );
744 if (_lread( hFile, buffer, ne_header.ne_cbnrestab )
745 != ne_header.ne_cbnrestab)
747 GlobalFree16( pModule->nrname_handle );
748 GlobalFree16( hModule );
749 return (HMODULE16)11; /* invalid exe */
752 else pModule->nrname_handle = 0;
754 /* Allocate a segment for the implicitly-loaded DLLs */
756 if (pModule->modref_count)
758 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
759 (pModule->modref_count+1)*sizeof(HMODULE16) );
760 if (!pModule->dlls_to_init)
762 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
763 GlobalFree16( hModule );
764 return (HMODULE16)11; /* invalid exe */
766 FarSetOwner16( pModule->dlls_to_init, hModule );
768 else pModule->dlls_to_init = 0;
770 NE_RegisterModule( pModule );
771 SNOOP16_RegisterDLL(pModule,path);
772 return hModule;
776 /***********************************************************************
777 * NE_LoadDLLs
779 * Load all DLLs implicitly linked to a module.
781 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
783 int i;
784 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
785 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
787 for (i = 0; i < pModule->modref_count; i++, pModRef++)
789 char buffer[260], *p;
790 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
791 memcpy( buffer, pstr + 1, *pstr );
792 *(buffer + *pstr) = 0; /* terminate it */
794 TRACE("Loading '%s'\n", buffer );
795 if (!(*pModRef = GetModuleHandle16( buffer )))
797 /* If the DLL is not loaded yet, load it and store */
798 /* its handle in the list of DLLs to initialize. */
799 HMODULE16 hDLL;
801 /* Append .DLL to name if no extension present */
802 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
803 strcat( buffer, ".DLL" );
805 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
807 /* FIXME: cleanup what was done */
809 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
810 buffer, *((BYTE*)pModule + pModule->name_table),
811 (char *)pModule + pModule->name_table + 1, hDLL );
812 return FALSE;
814 *pModRef = GetExePtr( hDLL );
815 *pDLLs++ = *pModRef;
817 else /* Increment the reference count of the DLL */
819 NE_MODULE *pOldDLL;
821 pOldDLL = NE_GetPtr( *pModRef );
822 if (pOldDLL) pOldDLL->count++;
825 return TRUE;
829 /**********************************************************************
830 * NE_DoLoadModule
832 * Load first instance of NE module from file.
834 * pModule must point to a module structure prepared by NE_LoadExeHeader.
835 * This routine must never be called twice on a module.
838 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
840 /* Allocate the segments for this module */
842 if (!NE_CreateAllSegments( pModule ))
843 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
845 /* Load the referenced DLLs */
847 if (!NE_LoadDLLs( pModule ))
848 return ERROR_FILE_NOT_FOUND; /* 2 */
850 /* Load the segments */
852 NE_LoadAllSegments( pModule );
854 /* Make sure the usage count is 1 on the first loading of */
855 /* the module, even if it contains circular DLL references */
857 pModule->count = 1;
859 return NE_GetInstance( pModule );
862 /**********************************************************************
863 * NE_LoadModule
865 * Load first instance of NE module. (Note: caller is responsible for
866 * ensuring the module isn't already loaded!)
868 * If the module turns out to be an executable module, only a
869 * handle to a module stub is returned; this needs to be initialized
870 * by calling NE_DoLoadModule later, in the context of the newly
871 * created process.
873 * If lib_only is TRUE, however, the module is perforce treated
874 * like a DLL module, even if it is an executable module.
877 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
879 NE_MODULE *pModule;
880 HMODULE16 hModule;
881 HINSTANCE16 hInstance;
882 HFILE16 hFile;
883 OFSTRUCT ofs;
885 /* Open file */
886 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
887 return (HMODULE16)2; /* File not found */
889 hModule = NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile), ofs.szPathName );
890 _lclose16( hFile );
891 if (hModule < 32) return hModule;
893 pModule = NE_GetPtr( hModule );
894 if ( !pModule ) return hModule;
896 if ( !lib_only && !( pModule->flags & NE_FFLAGS_LIBMODULE ) )
897 return hModule;
899 hInstance = NE_DoLoadModule( pModule );
900 if ( hInstance < 32 )
902 /* cleanup ... */
903 NE_FreeModule( hModule, 0 );
906 return hInstance;
910 /**********************************************************************
911 * MODULE_LoadModule16
913 * Load a NE module in the order of the loadorder specification.
914 * The caller is responsible that the module is not loaded already.
917 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
919 HINSTANCE16 hinst = 2;
920 enum loadorder_type loadorder[LOADORDER_NTYPES];
921 int i;
922 const char *filetype = "";
924 MODULE_GetLoadOrder(loadorder, libname, FALSE);
926 for(i = 0; i < LOADORDER_NTYPES; i++)
928 if (loadorder[i] == LOADORDER_INVALID) break;
930 switch(loadorder[i])
932 case LOADORDER_DLL:
933 TRACE("Trying native dll '%s'\n", libname);
934 hinst = NE_LoadModule(libname, lib_only);
935 filetype = "native";
936 break;
938 case LOADORDER_BI:
939 TRACE("Trying built-in '%s'\n", libname);
940 hinst = BUILTIN_LoadModule(libname);
941 filetype = "builtin";
942 break;
944 case LOADORDER_SO: /* This is not supported for NE modules */
945 default:
946 hinst = 2;
947 break;
950 if(hinst >= 32)
952 if(!implicit)
954 HMODULE16 hModule;
955 NE_MODULE *pModule;
957 hModule = GetModuleHandle16(libname);
958 if(!hModule)
960 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
961 libname, hinst);
962 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
965 pModule = NE_GetPtr(hModule);
966 if(!pModule)
968 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
969 libname, hinst);
970 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
973 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
974 if (!TRACE_ON(module))
975 TRACE_(loaddll)("Loaded module '%s' : %s\n", libname, filetype);
978 * Call initialization routines for all loaded DLLs. Note that
979 * when we load implicitly linked DLLs this will be done by InitTask().
981 if(pModule->flags & NE_FFLAGS_LIBMODULE)
983 NE_InitializeDLLs(hModule);
984 NE_DllProcessAttach(hModule);
987 return hinst;
990 if(hinst != 2)
992 /* We quit searching when we get another error than 'File not found' */
993 break;
996 return hinst; /* The last error that occured */
1000 /**********************************************************************
1001 * NE_CreateThread
1003 * Create the thread for a 16-bit module.
1005 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1007 HANDLE hThread;
1008 TDB *pTask;
1009 HTASK16 hTask;
1010 HINSTANCE16 instance = 0;
1012 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1013 return 0;
1015 /* Post event to start the task */
1016 PostEvent16( hTask );
1018 /* Wait until we get the instance handle */
1021 DirectedYield16( hTask );
1022 if (!IsTask16( hTask )) /* thread has died */
1024 DWORD exit_code;
1025 WaitForSingleObject( hThread, INFINITE );
1026 GetExitCodeThread( hThread, &exit_code );
1027 CloseHandle( hThread );
1028 return exit_code;
1030 if (!(pTask = TASK_GetPtr( hTask ))) break;
1031 instance = pTask->hInstance;
1032 GlobalUnlock16( hTask );
1033 } while (!instance);
1035 CloseHandle( hThread );
1036 return instance;
1040 /**********************************************************************
1041 * LoadModule (KERNEL.45)
1042 * LoadModule16 (KERNEL32.@)
1044 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1046 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1047 LOADPARAMS16 *params;
1048 HMODULE16 hModule;
1049 NE_MODULE *pModule;
1050 LPSTR cmdline;
1051 WORD cmdShow;
1053 /* Load module */
1055 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1057 /* Special case: second instance of an already loaded NE module */
1059 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
1060 if ( pModule->module32 ) return (HINSTANCE16)21;
1062 /* Increment refcount */
1064 pModule->count++;
1066 else
1068 /* Main case: load first instance of NE module */
1070 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1071 return hModule;
1073 if ( !(pModule = NE_GetPtr( hModule )) )
1074 return (HINSTANCE16)11;
1077 /* If library module, we just retrieve the instance handle */
1079 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1080 return NE_GetInstance( pModule );
1083 * At this point, we need to create a new process.
1085 * pModule points either to an already loaded module, whose refcount
1086 * has already been incremented (to avoid having the module vanish
1087 * in the meantime), or else to a stub module which contains only header
1088 * information.
1090 params = (LOADPARAMS16 *)paramBlock;
1091 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1092 cmdline = MapSL( params->cmdLine );
1093 return NE_CreateThread( pModule, cmdShow, cmdline );
1097 /**********************************************************************
1098 * NE_StartMain
1100 * Start the main NE task.
1102 HINSTANCE16 NE_StartMain( LPCSTR name, HANDLE file )
1104 STARTUPINFOA info;
1105 HMODULE16 hModule;
1106 NE_MODULE *pModule;
1107 INT len;
1108 LPSTR pCmdLine, cmdline = GetCommandLineA();
1110 if ((hModule = NE_LoadExeHeader( file, name )) < 32) return hModule;
1112 if (!(pModule = NE_GetPtr( hModule ))) return (HINSTANCE16)11;
1113 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1115 MESSAGE( "%s is not a valid Win16 executable\n", name );
1116 ExitProcess( ERROR_BAD_EXE_FORMAT );
1119 while (*cmdline && *cmdline != ' ') cmdline++;
1120 if (*cmdline) cmdline++;
1121 len = strlen(cmdline);
1122 pCmdLine = HeapAlloc(GetProcessHeap(), 0, len+2);
1123 if (pCmdLine)
1125 strcpy(pCmdLine+1, cmdline);
1126 *pCmdLine = len;
1128 GetStartupInfoA( &info );
1129 if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = 1;
1131 return NE_CreateThread( pModule, info.wShowWindow, pCmdLine );
1135 /**********************************************************************
1136 * NE_StartTask
1138 * Startup code for a new 16-bit task.
1140 DWORD NE_StartTask(void)
1142 TDB *pTask = TASK_GetCurrent();
1143 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1144 HINSTANCE16 hInstance, hPrevInstance;
1145 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1146 WORD sp;
1148 if ( pModule->count > 0 )
1150 /* Second instance of an already loaded NE module */
1151 /* Note that the refcount was already incremented by the parent */
1153 hPrevInstance = NE_GetInstance( pModule );
1155 if ( pModule->dgroup )
1156 if ( NE_CreateSegment( pModule, pModule->dgroup ) )
1157 NE_LoadSegment( pModule, pModule->dgroup );
1159 hInstance = NE_GetInstance( pModule );
1160 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->dgroup, hPrevInstance);
1163 else
1165 /* Load first instance of NE module */
1167 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1169 hInstance = NE_DoLoadModule( pModule );
1170 hPrevInstance = 0;
1173 if ( hInstance >= 32 )
1175 CONTEXT86 context;
1177 /* Enter instance handles into task struct */
1179 pTask->hInstance = hInstance;
1180 pTask->hPrevInstance = hPrevInstance;
1182 /* Free the previous stack selector */
1183 FreeSelector16( SELECTOROF(pTask->teb->cur_stack) );
1185 /* Use DGROUP for 16-bit stack */
1187 if (!(sp = pModule->sp))
1188 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
1189 sp &= ~1;
1190 sp -= sizeof(STACK16FRAME);
1191 pTask->teb->cur_stack = MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1193 /* Registers at initialization must be:
1194 * ax zero
1195 * bx stack size in bytes
1196 * cx heap size in bytes
1197 * si previous app instance
1198 * di current app instance
1199 * bp zero
1200 * es selector to the PSP
1201 * ds dgroup of the application
1202 * ss stack selector
1203 * sp top of the stack
1205 memset( &context, 0, sizeof(context) );
1206 context.SegCs = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
1207 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1208 context.SegEs = pTask->hPDB;
1209 context.Eip = pModule->ip;
1210 context.Ebx = pModule->stack_size;
1211 context.Ecx = pModule->heap_size;
1212 context.Edi = pTask->hInstance;
1213 context.Esi = pTask->hPrevInstance;
1215 /* Now call 16-bit entry point */
1217 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1218 context.SegCs, context.Eip, context.SegDs,
1219 SELECTOROF(pTask->teb->cur_stack),
1220 OFFSETOF(pTask->teb->cur_stack) );
1222 wine_call_to_16_regs_short( &context, 0 );
1223 ExitThread( LOWORD(context.Eax) );
1225 return hInstance; /* error code */
1228 /***********************************************************************
1229 * LoadLibrary (KERNEL.95)
1230 * LoadLibrary16 (KERNEL32.35)
1232 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1234 return LoadModule16(libname, (LPVOID)-1 );
1238 /**********************************************************************
1239 * MODULE_CallWEP
1241 * Call a DLL's WEP, allowing it to shut down.
1242 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1244 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1246 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1247 if (!WEP) return FALSE;
1248 return NE_CallTo16_word_w( WEP, WEP_FREE_DLL );
1252 /**********************************************************************
1253 * NE_FreeModule
1255 * Implementation of FreeModule16().
1257 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1259 HMODULE16 *hPrevModule;
1260 NE_MODULE *pModule;
1261 HMODULE16 *pModRef;
1262 int i;
1264 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1265 hModule = pModule->self;
1267 TRACE("%04x count %d\n", hModule, pModule->count );
1269 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1270 else pModule->count = 0;
1272 if (pModule->flags & NE_FFLAGS_BUILTIN)
1273 return FALSE; /* Can't free built-in module */
1275 if (call_wep && !(pModule->flags & NE_FFLAGS_WIN32))
1277 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1279 MODULE_CallWEP( hModule );
1281 /* Free the objects owned by the DLL module */
1282 TASK_CallTaskSignalProc( USIG16_DLL_UNLOAD, hModule );
1283 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16, hModule );
1285 else
1286 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1290 /* Clear magic number just in case */
1292 pModule->magic = pModule->self = 0;
1294 /* Remove it from the linked list */
1296 hPrevModule = &hFirstModule;
1297 while (*hPrevModule && (*hPrevModule != hModule))
1299 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1301 if (*hPrevModule) *hPrevModule = pModule->next;
1303 /* Free the referenced modules */
1305 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1306 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1308 NE_FreeModule( *pModRef, call_wep );
1311 /* Free the module storage */
1313 GlobalFreeAll16( hModule );
1315 /* Remove module from cache */
1317 if (pCachedModule == pModule) pCachedModule = NULL;
1318 return TRUE;
1322 /**********************************************************************
1323 * FreeModule (KERNEL.46)
1325 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1327 return NE_FreeModule( hModule, TRUE );
1331 /***********************************************************************
1332 * FreeLibrary (KERNEL.96)
1333 * FreeLibrary16 (KERNEL32.36)
1335 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1337 TRACE("%04x\n", handle );
1338 FreeModule16( handle );
1342 /**********************************************************************
1343 * GetModuleName (KERNEL.27)
1345 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1347 NE_MODULE *pModule;
1348 BYTE *p;
1350 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1351 p = (BYTE *)pModule + pModule->name_table;
1352 if (count > *p) count = *p + 1;
1353 if (count > 0)
1355 memcpy( buf, p + 1, count - 1 );
1356 buf[count-1] = '\0';
1358 return TRUE;
1362 /**********************************************************************
1363 * GetModuleUsage (KERNEL.48)
1365 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1367 NE_MODULE *pModule = NE_GetPtr( hModule );
1368 return pModule ? pModule->count : 0;
1372 /**********************************************************************
1373 * GetExpWinVer (KERNEL.167)
1375 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1377 NE_MODULE *pModule = NE_GetPtr( hModule );
1378 if ( !pModule ) return 0;
1381 * For built-in modules, fake the expected version the module should
1382 * have according to the Windows version emulated by Wine
1384 if ( !pModule->expected_version )
1386 OSVERSIONINFOA versionInfo;
1387 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1389 if ( GetVersionExA( &versionInfo ) )
1390 pModule->expected_version =
1391 (versionInfo.dwMajorVersion & 0xff) << 8
1392 | (versionInfo.dwMinorVersion & 0xff);
1395 return pModule->expected_version;
1399 /**********************************************************************
1400 * GetModuleFileName (KERNEL.49)
1401 * GetModuleFileName16 (KERNEL32.@)
1403 * Comment: see GetModuleFileNameA
1405 * Even if invoked by second instance of a program,
1406 * it still returns path of first one.
1408 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1409 INT16 nSize )
1411 NE_MODULE *pModule;
1413 /* Win95 does not query hModule if set to 0 !
1414 * Is this wrong or maybe Win3.1 only ? */
1415 if (!hModule) hModule = GetCurrentTask();
1417 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1418 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1419 if (pModule->expected_version >= 0x400)
1420 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1421 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1422 return strlen(lpFileName);
1426 /**********************************************************************
1427 * GetModuleHandle (KERNEL.47)
1429 * Find a module from a module name.
1431 * NOTE: The current implementation works the same way the Windows 95 one
1432 * does. Do not try to 'fix' it, fix the callers.
1433 * + It does not do ANY extension handling (except that strange .EXE bit)!
1434 * + It does not care about paths, just about basenames. (same as Windows)
1436 * RETURNS
1437 * LOWORD:
1438 * the win16 module handle if found
1439 * 0 if not
1440 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1441 * Always hFirstModule
1443 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1445 if (HIWORD(name) == 0)
1446 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1447 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1450 /***********************************************************************
1451 * GetModuleHandle16 (KERNEL32.@)
1453 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1455 HMODULE16 hModule = hFirstModule;
1456 LPSTR s;
1457 BYTE len, *name_table;
1458 char tmpstr[MAX_PATH];
1459 NE_MODULE *pModule;
1461 TRACE("(%s)\n", name);
1463 if (!HIWORD(name))
1464 return GetExePtr(LOWORD(name));
1466 len = strlen(name);
1467 if (!len)
1468 return 0;
1470 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1472 /* If 'name' matches exactly the module name of a module:
1473 * Return its handle.
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 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1483 return hModule;
1486 /* If uppercased 'name' matches exactly the module name of a module:
1487 * Return its handle
1489 for (s = tmpstr; *s; s++) *s = FILE_toupper(*s);
1491 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1493 pModule = NE_GetPtr( hModule );
1494 if (!pModule) break;
1495 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1497 name_table = (BYTE *)pModule + pModule->name_table;
1498 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1499 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1500 * lowercased module names, but try to load uppercase DLLs, so this
1501 * 'i' compare is just a quickfix until the loader handles that
1502 * correctly. -MM 990705
1504 if ((*name_table == len) && !FILE_strncasecmp(tmpstr, name_table+1, len))
1505 return hModule;
1508 /* If the base filename of 'name' matches the base filename of the module
1509 * filename of some module (case-insensitive compare):
1510 * Return its handle.
1513 /* basename: search backwards in passed name to \ / or : */
1514 s = tmpstr + strlen(tmpstr);
1515 while (s > tmpstr)
1517 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1518 break;
1519 s--;
1522 /* search this in loaded filename list */
1523 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1525 char *loadedfn;
1526 OFSTRUCT *ofs;
1528 pModule = NE_GetPtr( hModule );
1529 if (!pModule) break;
1530 if (!pModule->fileinfo) continue;
1531 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1533 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1534 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1535 /* basename: search backwards in pathname to \ / or : */
1536 while (loadedfn > (char*)ofs->szPathName)
1538 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1539 break;
1540 loadedfn--;
1542 /* case insensitive compare ... */
1543 if (!FILE_strcasecmp(loadedfn, s))
1544 return hModule;
1547 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1548 * matches the base filename of the module filename of some 32-bit module:
1549 * Return the corresponding 16-bit dummy module handle.
1551 if (len >= 4 && !FILE_strcasecmp(name+len-4, ".EXE"))
1553 HMODULE hModule = GetModuleHandleA( name );
1554 if ( hModule )
1555 return MapHModuleLS( hModule );
1558 if (!strcmp(tmpstr,"MSDOS"))
1559 return 1;
1561 if (!strcmp(tmpstr,"TIMER"))
1563 FIXME("Eh... Should return caller's code segment, expect crash\n");
1564 return 0;
1567 return 0;
1570 /**********************************************************************
1571 * NE_GetModuleByFilename
1573 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1575 HMODULE16 hModule;
1576 LPSTR s, p;
1577 BYTE len, *name_table;
1578 char tmpstr[MAX_PATH];
1579 NE_MODULE *pModule;
1581 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1583 /* If the base filename of 'name' matches the base filename of the module
1584 * filename of some module (case-insensitive compare):
1585 * Return its handle.
1588 /* basename: search backwards in passed name to \ / or : */
1589 s = tmpstr + strlen(tmpstr);
1590 while (s > tmpstr)
1592 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1593 break;
1594 s--;
1597 /* search this in loaded filename list */
1598 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1600 char *loadedfn;
1601 OFSTRUCT *ofs;
1603 pModule = NE_GetPtr( hModule );
1604 if (!pModule) break;
1605 if (!pModule->fileinfo) continue;
1606 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1608 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1609 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1610 /* basename: search backwards in pathname to \ / or : */
1611 while (loadedfn > (char*)ofs->szPathName)
1613 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1614 break;
1615 loadedfn--;
1617 /* case insensitive compare ... */
1618 if (!FILE_strcasecmp(loadedfn, s))
1619 return hModule;
1621 /* If basename (without ext) matches the module name of a module:
1622 * Return its handle.
1625 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1626 len = strlen(s);
1628 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1630 pModule = NE_GetPtr( hModule );
1631 if (!pModule) break;
1632 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1634 name_table = (BYTE *)pModule + pModule->name_table;
1635 if ((*name_table == len) && !FILE_strncasecmp(s, name_table+1, len))
1636 return hModule;
1639 return 0;
1642 /**********************************************************************
1643 * ModuleFirst (TOOLHELP.59)
1645 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1647 lpme->wNext = hFirstModule;
1648 return ModuleNext16( lpme );
1652 /**********************************************************************
1653 * ModuleNext (TOOLHELP.60)
1655 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1657 NE_MODULE *pModule;
1658 char *name;
1660 if (!lpme->wNext) return FALSE;
1661 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1662 name = (char *)pModule + pModule->name_table;
1663 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1664 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1665 lpme->hModule = lpme->wNext;
1666 lpme->wcUsage = pModule->count;
1667 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1668 lpme->wNext = pModule->next;
1669 return TRUE;
1673 /**********************************************************************
1674 * ModuleFindName (TOOLHELP.61)
1676 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1678 lpme->wNext = GetModuleHandle16( name );
1679 return ModuleNext16( lpme );
1683 /**********************************************************************
1684 * ModuleFindHandle (TOOLHELP.62)
1686 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1688 hModule = GetExePtr( hModule );
1689 lpme->wNext = hModule;
1690 return ModuleNext16( lpme );
1694 /***************************************************************************
1695 * IsRomModule (KERNEL.323)
1697 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1699 return FALSE;
1702 /***************************************************************************
1703 * IsRomFile (KERNEL.326)
1705 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1707 return FALSE;
1710 /***************************************************************************
1711 * MapHModuleLS (KERNEL32.@)
1713 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod) {
1714 NE_MODULE *pModule;
1716 if (!hmod)
1717 return TASK_GetCurrent()->hInstance;
1718 if (!HIWORD(hmod))
1719 return hmod; /* we already have a 16 bit module handle */
1720 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1721 while (pModule) {
1722 if (pModule->module32 == hmod)
1723 return pModule->self;
1724 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1726 return 0;
1729 /***************************************************************************
1730 * MapHModuleSL (KERNEL32.@)
1732 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1733 NE_MODULE *pModule;
1735 if (!hmod) {
1736 TDB *pTask = TASK_GetCurrent();
1737 hmod = pTask->hModule;
1739 pModule = (NE_MODULE*)GlobalLock16(hmod);
1740 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1741 !(pModule->flags & NE_FFLAGS_WIN32)
1743 return 0;
1744 return pModule->module32;
1747 /***************************************************************************
1748 * MapHInstLS (KERNEL32.@)
1749 * MapHInstLS (KERNEL.472)
1751 void WINAPI MapHInstLS( CONTEXT86 *context )
1753 context->Eax = MapHModuleLS(context->Eax);
1756 /***************************************************************************
1757 * MapHInstSL (KERNEL32.@)
1758 * MapHInstSL (KERNEL.473)
1760 void WINAPI MapHInstSL( CONTEXT86 *context )
1762 context->Eax = MapHModuleSL(context->Eax);
1765 /***************************************************************************
1766 * MapHInstLS_PN (KERNEL32.@)
1768 void WINAPI MapHInstLS_PN( CONTEXT86 *context )
1770 if (context->Eax) context->Eax = MapHModuleLS(context->Eax);
1773 /***************************************************************************
1774 * MapHInstSL_PN (KERNEL32.@)
1776 void WINAPI MapHInstSL_PN( CONTEXT86 *context )
1778 if (context->Eax) context->Eax = MapHModuleSL(context->Eax);