Specify sizes for stock fonts again; removed the FixStockFontSize
[wine/multimedia.git] / loader / ne / module.c
blob649d449b0290f8dbce85d773f07c38e193d5b0c2
1 /*
2 * NE modules
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <ctype.h>
13 #include "wine/port.h"
14 #include "wine/winbase16.h"
15 #include "wine/library.h"
16 #include "winerror.h"
17 #include "module.h"
18 #include "toolhelp.h"
19 #include "file.h"
20 #include "task.h"
21 #include "snoop.h"
22 #include "builtin16.h"
23 #include "stackframe.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(module);
27 DECLARE_DEBUG_CHANNEL(loaddll);
30 * Segment table entry
32 struct ne_segment_table_entry_s
34 WORD seg_data_offset; /* Sector offset of segment data */
35 WORD seg_data_length; /* Length of segment data */
36 WORD seg_flags; /* Flags associated with this segment */
37 WORD min_alloc; /* Minimum allocation size for this */
40 #define hFirstModule (pThhook->hExeHead)
42 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
44 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
45 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
47 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
49 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
51 /* ### start build ### */
52 extern WORD CALLBACK NE_CallTo16_word_w(FARPROC16,WORD);
53 /* ### stop build ### */
55 /***********************************************************************
56 * NE_GetPtr
58 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
60 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
64 /***********************************************************************
65 * NE_DumpModule
67 void NE_DumpModule( HMODULE16 hModule )
69 int i, ordinal;
70 SEGTABLEENTRY *pSeg;
71 BYTE *pstr;
72 WORD *pword;
73 NE_MODULE *pModule;
74 ET_BUNDLE *bundle;
75 ET_ENTRY *entry;
77 if (!(pModule = NE_GetPtr( hModule )))
79 MESSAGE( "**** %04x is not a module handle\n", hModule );
80 return;
83 /* Dump the module info */
84 DPRINTF( "---\n" );
85 DPRINTF( "Module %04x:\n", hModule );
86 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
87 pModule->count, pModule->flags,
88 pModule->heap_size, pModule->stack_size );
89 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
90 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
91 pModule->seg_count, pModule->modref_count );
92 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
93 pModule->os_flags, pModule->min_swap_area,
94 pModule->expected_version );
95 if (pModule->flags & NE_FFLAGS_WIN32)
96 DPRINTF( "PE module=%08x\n", pModule->module32 );
98 /* Dump the file info */
99 DPRINTF( "---\n" );
100 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
102 /* Dump the segment table */
103 DPRINTF( "---\n" );
104 DPRINTF( "Segment table:\n" );
105 pSeg = NE_SEG_TABLE( pModule );
106 for (i = 0; i < pModule->seg_count; i++, pSeg++)
107 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
108 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
109 pSeg->minsize, pSeg->hSeg );
111 /* Dump the resource table */
112 DPRINTF( "---\n" );
113 DPRINTF( "Resource table:\n" );
114 if (pModule->res_table)
116 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
117 DPRINTF( "Alignment: %d\n", *pword++ );
118 while (*pword)
120 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
121 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
122 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
123 for (i = 0; i < ptr->count; i++, pname++)
124 DPRINTF( "offset=%d len=%d id=%04x\n",
125 pname->offset, pname->length, pname->id );
126 pword = (WORD *)pname;
129 else DPRINTF( "None\n" );
131 /* Dump the resident name table */
132 DPRINTF( "---\n" );
133 DPRINTF( "Resident-name table:\n" );
134 pstr = (char *)pModule + pModule->name_table;
135 while (*pstr)
137 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
138 *(WORD *)(pstr + *pstr + 1) );
139 pstr += *pstr + 1 + sizeof(WORD);
142 /* Dump the module reference table */
143 DPRINTF( "---\n" );
144 DPRINTF( "Module ref table:\n" );
145 if (pModule->modref_table)
147 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
148 for (i = 0; i < pModule->modref_count; i++, pword++)
150 char name[10];
151 GetModuleName16( *pword, name, sizeof(name) );
152 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
155 else DPRINTF( "None\n" );
157 /* Dump the entry table */
158 DPRINTF( "---\n" );
159 DPRINTF( "Entry table:\n" );
160 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
161 do {
162 entry = (ET_ENTRY *)((BYTE *)bundle+6);
163 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
164 ordinal = bundle->first;
165 while (ordinal < bundle->last)
167 if (entry->type == 0xff)
168 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
169 else
170 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
171 entry++;
173 } while ( (bundle->next)
174 && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
176 /* Dump the non-resident names table */
177 DPRINTF( "---\n" );
178 DPRINTF( "Non-resident names table:\n" );
179 if (pModule->nrname_handle)
181 pstr = (char *)GlobalLock16( pModule->nrname_handle );
182 while (*pstr)
184 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
185 *(WORD *)(pstr + *pstr + 1) );
186 pstr += *pstr + 1 + sizeof(WORD);
189 DPRINTF( "\n" );
193 /***********************************************************************
194 * NE_WalkModules
196 * Walk the module list and print the modules.
198 void NE_WalkModules(void)
200 HMODULE16 hModule = hFirstModule;
201 MESSAGE( "Module Flags Name\n" );
202 while (hModule)
204 NE_MODULE *pModule = NE_GetPtr( hModule );
205 if (!pModule)
207 MESSAGE( "Bad module %04x in list\n", hModule );
208 return;
210 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->flags,
211 *((char *)pModule + pModule->name_table),
212 (char *)pModule + pModule->name_table + 1 );
213 hModule = pModule->next;
218 /**********************************************************************
219 * NE_RegisterModule
221 void NE_RegisterModule( NE_MODULE *pModule )
223 pModule->next = hFirstModule;
224 hFirstModule = pModule->self;
228 /***********************************************************************
229 * NE_GetOrdinal
231 * Lookup the ordinal for a given name.
233 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
235 unsigned char buffer[256], *cpnt;
236 BYTE len;
237 NE_MODULE *pModule;
239 if (!(pModule = NE_GetPtr( hModule ))) return 0;
240 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
242 TRACE("(%04x,'%s')\n", hModule, name );
244 /* First handle names of the form '#xxxx' */
246 if (name[0] == '#') return atoi( name + 1 );
248 /* Now copy and uppercase the string */
250 strcpy( buffer, name );
251 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = FILE_toupper(*cpnt);
252 len = cpnt - buffer;
254 /* First search the resident names */
256 cpnt = (char *)pModule + pModule->name_table;
258 /* Skip the first entry (module name) */
259 cpnt += *cpnt + 1 + sizeof(WORD);
260 while (*cpnt)
262 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
264 WORD ordinal = GET_UA_WORD( cpnt + *cpnt + 1 );
265 TRACE(" Found: ordinal=%d\n", ordinal );
266 return ordinal;
268 cpnt += *cpnt + 1 + sizeof(WORD);
271 /* Now search the non-resident names table */
273 if (!pModule->nrname_handle) return 0; /* No non-resident table */
274 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
276 /* Skip the first entry (module description string) */
277 cpnt += *cpnt + 1 + sizeof(WORD);
278 while (*cpnt)
280 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
282 WORD ordinal = GET_UA_WORD( cpnt + *cpnt + 1 );
283 TRACE(" Found: ordinal=%d\n", ordinal );
284 return ordinal;
286 cpnt += *cpnt + 1 + sizeof(WORD);
288 return 0;
292 /***********************************************************************
293 * EntryAddrProc (KERNEL.667) Wine-specific export
295 * Return the entry point for a given ordinal.
297 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
299 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
300 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
301 return ret;
304 /***********************************************************************
305 * NE_GetEntryPoint
307 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
309 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
312 /***********************************************************************
313 * NE_GetEntryPointEx
315 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
317 NE_MODULE *pModule;
318 WORD sel, offset, i;
320 ET_ENTRY *entry;
321 ET_BUNDLE *bundle;
323 if (!(pModule = NE_GetPtr( hModule ))) return 0;
324 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
326 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
327 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
329 if (!(bundle->next))
330 return 0;
331 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
334 entry = (ET_ENTRY *)((BYTE *)bundle+6);
335 for (i=0; i < (ordinal - bundle->first - 1); i++)
336 entry++;
338 sel = entry->segnum;
339 offset = GET_UA_WORD( &entry->offs );
341 if (sel == 0xfe) sel = 0xffff; /* constant entry */
342 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
343 if (sel==0xffff)
344 return (FARPROC16)MAKESEGPTR( sel, offset );
345 if (!snoop)
346 return (FARPROC16)MAKESEGPTR( sel, offset );
347 else
348 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
352 /***********************************************************************
353 * NE_SetEntryPoint
355 * Change the value of an entry point. Use with caution!
356 * It can only change the offset value, not the selector.
358 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
360 NE_MODULE *pModule;
361 ET_ENTRY *entry;
362 ET_BUNDLE *bundle;
363 int i;
365 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
366 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
368 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
369 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
371 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
372 if (!(bundle->next))
373 return 0;
376 entry = (ET_ENTRY *)((BYTE *)bundle+6);
377 for (i=0; i < (ordinal - bundle->first - 1); i++)
378 entry++;
380 PUT_UA_WORD( &entry->offs, offset );
381 return TRUE;
385 /***********************************************************************
386 * NE_OpenFile
388 HANDLE NE_OpenFile( NE_MODULE *pModule )
390 char *name;
392 static HANDLE cachedfd = INVALID_HANDLE_VALUE;
394 TRACE("(%p) cache: mod=%p fd=%d\n",
395 pModule, pCachedModule, cachedfd );
396 if (pCachedModule == pModule) return cachedfd;
397 CloseHandle( cachedfd );
398 pCachedModule = pModule;
399 name = NE_MODULE_NAME( pModule );
400 if ((cachedfd = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
401 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
402 MESSAGE( "Can't open file '%s' for module %04x\n", name, pModule->self );
403 TRACE("opened '%s' -> %d\n",
404 name, cachedfd );
405 return cachedfd;
409 /***********************************************************************
410 * NE_LoadExeHeader
412 static HMODULE16 NE_LoadExeHeader( HANDLE hFile, LPCSTR path )
414 IMAGE_DOS_HEADER mz_header;
415 IMAGE_OS2_HEADER ne_header;
416 int size;
417 HMODULE16 hModule;
418 NE_MODULE *pModule;
419 BYTE *pData, *pTempEntryTable;
420 char *buffer, *fastload = NULL;
421 int fastload_offset = 0, fastload_length = 0;
422 ET_ENTRY *entry;
423 ET_BUNDLE *bundle, *oldbundle;
424 OFSTRUCT *ofs;
426 /* Read a block from either the file or the fast-load area. */
427 #define READ(offset,size,buffer) \
428 ((fastload && ((offset) >= fastload_offset) && \
429 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
430 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
431 (_llseek( hFile, (offset), SEEK_SET), \
432 _lread( hFile, (buffer), (size) ) == (size)))
434 _llseek( hFile, 0, SEEK_SET );
435 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
436 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
437 return (HMODULE16)11; /* invalid exe */
439 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
440 if (_lread( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
441 return (HMODULE16)11; /* invalid exe */
443 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
444 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
445 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
446 return (HMODULE16)12;
448 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
450 /* We now have a valid NE header */
452 size = sizeof(NE_MODULE) +
453 /* segment table */
454 ne_header.ne_cseg * sizeof(SEGTABLEENTRY) +
455 /* resource table */
456 ne_header.ne_restab - ne_header.ne_rsrctab +
457 /* resident names table */
458 ne_header.ne_modtab - ne_header.ne_restab +
459 /* module ref table */
460 ne_header.ne_cmod * sizeof(WORD) +
461 /* imported names table */
462 ne_header.ne_enttab - ne_header.ne_imptab +
463 /* entry table length */
464 ne_header.ne_cbenttab +
465 /* entry table extra conversion space */
466 sizeof(ET_BUNDLE) +
467 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6) +
468 /* loaded file info */
469 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
471 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
472 if (!hModule) return (HMODULE16)11; /* invalid exe */
474 FarSetOwner16( hModule, hModule );
475 pModule = (NE_MODULE *)GlobalLock16( hModule );
476 memcpy( pModule, &ne_header, sizeof(ne_header) );
477 pModule->count = 0;
478 /* check *programs* for default minimal stack size */
479 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
480 && (pModule->stack_size < 0x1400) )
481 pModule->stack_size = 0x1400;
482 pModule->module32 = 0;
483 pModule->self = hModule;
484 pModule->self_loading_sel = 0;
485 pData = (BYTE *)(pModule + 1);
487 /* Clear internal Wine flags in case they are set in the EXE file */
489 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
491 /* Read the fast-load area */
493 if (ne_header.ne_flagsothers & NE_AFLAGS_FASTLOAD)
495 fastload_offset=ne_header.fastload_offset << ne_header.ne_align;
496 fastload_length=ne_header.fastload_length << ne_header.ne_align;
497 TRACE("Using fast-load area offset=%x len=%d\n",
498 fastload_offset, fastload_length );
499 if ((fastload = HeapAlloc( GetProcessHeap(), 0, fastload_length )) != NULL)
501 _llseek( hFile, fastload_offset, SEEK_SET);
502 if (_lread(hFile, fastload, fastload_length) != fastload_length)
504 HeapFree( GetProcessHeap(), 0, fastload );
505 WARN("Error reading fast-load area!\n");
506 fastload = NULL;
511 /* Get the segment table */
513 pModule->seg_table = pData - (BYTE *)pModule;
514 buffer = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cseg *
515 sizeof(struct ne_segment_table_entry_s));
516 if (buffer)
518 int i;
519 struct ne_segment_table_entry_s *pSeg;
521 if (!READ( mz_header.e_lfanew + ne_header.ne_segtab,
522 ne_header.ne_cseg * sizeof(struct ne_segment_table_entry_s),
523 buffer ))
525 HeapFree( GetProcessHeap(), 0, buffer );
526 if (fastload)
527 HeapFree( GetProcessHeap(), 0, fastload );
528 GlobalFree16( hModule );
529 return (HMODULE16)11; /* invalid exe */
531 pSeg = (struct ne_segment_table_entry_s *)buffer;
532 for (i = ne_header.ne_cseg; i > 0; i--, pSeg++)
534 memcpy( pData, pSeg, sizeof(*pSeg) );
535 pData += sizeof(SEGTABLEENTRY);
537 HeapFree( GetProcessHeap(), 0, buffer );
539 else
541 if (fastload)
542 HeapFree( GetProcessHeap(), 0, fastload );
543 GlobalFree16( hModule );
544 return (HMODULE16)11; /* invalid exe */
547 /* Get the resource table */
549 if (ne_header.ne_rsrctab < ne_header.ne_restab)
551 pModule->res_table = pData - (BYTE *)pModule;
552 if (!READ(mz_header.e_lfanew + ne_header.ne_rsrctab,
553 ne_header.ne_restab - ne_header.ne_rsrctab,
554 pData ))
555 return (HMODULE16)11; /* invalid exe */
556 pData += ne_header.ne_restab - ne_header.ne_rsrctab;
557 NE_InitResourceHandler( hModule );
559 else pModule->res_table = 0; /* No resource table */
561 /* Get the resident names table */
563 pModule->name_table = pData - (BYTE *)pModule;
564 if (!READ( mz_header.e_lfanew + ne_header.ne_restab,
565 ne_header.ne_modtab - ne_header.ne_restab,
566 pData ))
568 if (fastload)
569 HeapFree( GetProcessHeap(), 0, fastload );
570 GlobalFree16( hModule );
571 return (HMODULE16)11; /* invalid exe */
573 pData += ne_header.ne_modtab - ne_header.ne_restab;
575 /* Get the module references table */
577 if (ne_header.ne_cmod > 0)
579 pModule->modref_table = pData - (BYTE *)pModule;
580 if (!READ( mz_header.e_lfanew + ne_header.ne_modtab,
581 ne_header.ne_cmod * sizeof(WORD),
582 pData ))
584 if (fastload)
585 HeapFree( GetProcessHeap(), 0, fastload );
586 GlobalFree16( hModule );
587 return (HMODULE16)11; /* invalid exe */
589 pData += ne_header.ne_cmod * sizeof(WORD);
591 else pModule->modref_table = 0; /* No module references */
593 /* Get the imported names table */
595 pModule->import_table = pData - (BYTE *)pModule;
596 if (!READ( mz_header.e_lfanew + ne_header.ne_imptab,
597 ne_header.ne_enttab - ne_header.ne_imptab,
598 pData ))
600 if (fastload)
601 HeapFree( GetProcessHeap(), 0, fastload );
602 GlobalFree16( hModule );
603 return (HMODULE16)11; /* invalid exe */
605 pData += ne_header.ne_enttab - ne_header.ne_imptab;
607 /* Load entry table, convert it to the optimized version used by Windows */
609 if ((pTempEntryTable = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cbenttab)) != NULL)
611 BYTE nr_entries, type, *s;
613 TRACE("Converting entry table.\n");
614 pModule->entry_table = pData - (BYTE *)pModule;
615 if (!READ( mz_header.e_lfanew + ne_header.ne_enttab,
616 ne_header.ne_cbenttab, pTempEntryTable ))
618 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
619 if (fastload)
620 HeapFree( GetProcessHeap(), 0, fastload );
621 GlobalFree16( hModule );
622 return (HMODULE16)11; /* invalid exe */
625 s = pTempEntryTable;
626 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header.ne_enttab, ne_header.ne_cbenttab, *s);
628 bundle = (ET_BUNDLE *)pData;
629 TRACE("first bundle: %p\n", bundle);
630 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
631 entry = (ET_ENTRY *)((BYTE *)bundle+6);
633 while ((nr_entries = *s++))
635 if ((type = *s++))
637 bundle->last += nr_entries;
638 if (type == 0xff)
639 while (nr_entries--)
641 entry->type = type;
642 entry->flags = *s++;
643 s += 2;
644 entry->segnum = *s++;
645 entry->offs = *(WORD *)s; s += 2;
646 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
647 entry++;
649 else
650 while (nr_entries--)
652 entry->type = type;
653 entry->flags = *s++;
654 entry->segnum = type;
655 entry->offs = *(WORD *)s; s += 2;
656 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
657 entry++;
660 else
662 if (bundle->first == bundle->last)
664 bundle->first += nr_entries;
665 bundle->last += nr_entries;
667 else
669 oldbundle = bundle;
670 oldbundle->next = ((int)entry - (int)pModule);
671 bundle = (ET_BUNDLE *)entry;
672 TRACE("new bundle: %p\n", bundle);
673 bundle->first = bundle->last =
674 oldbundle->last + nr_entries;
675 bundle->next = 0;
676 (BYTE *)entry += sizeof(ET_BUNDLE);
680 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
682 else
684 if (fastload)
685 HeapFree( GetProcessHeap(), 0, fastload );
686 GlobalFree16( hModule );
687 return (HMODULE16)11; /* invalid exe */
690 pData += ne_header.ne_cbenttab + sizeof(ET_BUNDLE) +
691 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6);
693 if ((DWORD)entry > (DWORD)pData)
694 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
696 /* Store the filename information */
698 pModule->fileinfo = pData - (BYTE *)pModule;
699 size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
700 ofs = (OFSTRUCT *)pData;
701 ofs->cBytes = size - 1;
702 ofs->fFixedDisk = 1;
703 strcpy( ofs->szPathName, path );
704 pData += size;
706 /* Free the fast-load area */
708 #undef READ
709 if (fastload)
710 HeapFree( GetProcessHeap(), 0, fastload );
712 /* Get the non-resident names table */
714 if (ne_header.ne_cbnrestab)
716 pModule->nrname_handle = GlobalAlloc16( 0, ne_header.ne_cbnrestab );
717 if (!pModule->nrname_handle)
719 GlobalFree16( hModule );
720 return (HMODULE16)11; /* invalid exe */
722 FarSetOwner16( pModule->nrname_handle, hModule );
723 buffer = GlobalLock16( pModule->nrname_handle );
724 _llseek( hFile, ne_header.ne_nrestab, SEEK_SET );
725 if (_lread( hFile, buffer, ne_header.ne_cbnrestab )
726 != ne_header.ne_cbnrestab)
728 GlobalFree16( pModule->nrname_handle );
729 GlobalFree16( hModule );
730 return (HMODULE16)11; /* invalid exe */
733 else pModule->nrname_handle = 0;
735 /* Allocate a segment for the implicitly-loaded DLLs */
737 if (pModule->modref_count)
739 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
740 (pModule->modref_count+1)*sizeof(HMODULE16) );
741 if (!pModule->dlls_to_init)
743 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
744 GlobalFree16( hModule );
745 return (HMODULE16)11; /* invalid exe */
747 FarSetOwner16( pModule->dlls_to_init, hModule );
749 else pModule->dlls_to_init = 0;
751 NE_RegisterModule( pModule );
752 SNOOP16_RegisterDLL(pModule,path);
753 return hModule;
757 /***********************************************************************
758 * NE_LoadDLLs
760 * Load all DLLs implicitly linked to a module.
762 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
764 int i;
765 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
766 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
768 for (i = 0; i < pModule->modref_count; i++, pModRef++)
770 char buffer[260], *p;
771 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
772 memcpy( buffer, pstr + 1, *pstr );
773 *(buffer + *pstr) = 0; /* terminate it */
775 TRACE("Loading '%s'\n", buffer );
776 if (!(*pModRef = GetModuleHandle16( buffer )))
778 /* If the DLL is not loaded yet, load it and store */
779 /* its handle in the list of DLLs to initialize. */
780 HMODULE16 hDLL;
782 /* Append .DLL to name if no extension present */
783 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
784 strcat( buffer, ".DLL" );
786 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
788 /* FIXME: cleanup what was done */
790 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
791 buffer, *((BYTE*)pModule + pModule->name_table),
792 (char *)pModule + pModule->name_table + 1, hDLL );
793 return FALSE;
795 *pModRef = GetExePtr( hDLL );
796 *pDLLs++ = *pModRef;
798 else /* Increment the reference count of the DLL */
800 NE_MODULE *pOldDLL;
802 pOldDLL = NE_GetPtr( *pModRef );
803 if (pOldDLL) pOldDLL->count++;
806 return TRUE;
810 /**********************************************************************
811 * NE_DoLoadModule
813 * Load first instance of NE module from file.
815 * pModule must point to a module structure prepared by NE_LoadExeHeader.
816 * This routine must never be called twice on a module.
819 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
821 /* Allocate the segments for this module */
823 if (!NE_CreateAllSegments( pModule ))
824 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
826 /* Load the referenced DLLs */
828 if (!NE_LoadDLLs( pModule ))
829 return ERROR_FILE_NOT_FOUND; /* 2 */
831 /* Load the segments */
833 NE_LoadAllSegments( pModule );
835 /* Make sure the usage count is 1 on the first loading of */
836 /* the module, even if it contains circular DLL references */
838 pModule->count = 1;
840 return NE_GetInstance( pModule );
843 /**********************************************************************
844 * NE_LoadModule
846 * Load first instance of NE module. (Note: caller is responsible for
847 * ensuring the module isn't already loaded!)
849 * If the module turns out to be an executable module, only a
850 * handle to a module stub is returned; this needs to be initialized
851 * by calling NE_DoLoadModule later, in the context of the newly
852 * created process.
854 * If lib_only is TRUE, however, the module is perforce treated
855 * like a DLL module, even if it is an executable module.
858 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
860 NE_MODULE *pModule;
861 HMODULE16 hModule;
862 HINSTANCE16 hInstance;
863 HFILE16 hFile;
864 OFSTRUCT ofs;
866 /* Open file */
867 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
868 return (HMODULE16)2; /* File not found */
870 hModule = NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile), ofs.szPathName );
871 _lclose16( hFile );
872 if (hModule < 32) return hModule;
874 pModule = NE_GetPtr( hModule );
875 if ( !pModule ) return hModule;
877 if ( !lib_only && !( pModule->flags & NE_FFLAGS_LIBMODULE ) )
878 return hModule;
880 hInstance = NE_DoLoadModule( pModule );
881 if ( hInstance < 32 )
883 /* cleanup ... */
884 NE_FreeModule( hModule, 0 );
887 return hInstance;
891 /**********************************************************************
892 * MODULE_LoadModule16
894 * Load a NE module in the order of the loadorder specification.
895 * The caller is responsible that the module is not loaded already.
898 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
900 HINSTANCE16 hinst = 2;
901 enum loadorder_type loadorder[LOADORDER_NTYPES];
902 int i;
903 const char *filetype = "";
905 MODULE_GetLoadOrder(loadorder, libname, FALSE);
907 for(i = 0; i < LOADORDER_NTYPES; i++)
909 if (loadorder[i] == LOADORDER_INVALID) break;
911 switch(loadorder[i])
913 case LOADORDER_DLL:
914 TRACE("Trying native dll '%s'\n", libname);
915 hinst = NE_LoadModule(libname, lib_only);
916 filetype = "native";
917 break;
919 case LOADORDER_BI:
920 TRACE("Trying built-in '%s'\n", libname);
921 hinst = BUILTIN_LoadModule(libname);
922 filetype = "builtin";
923 break;
925 case LOADORDER_SO: /* This is not supported for NE modules */
926 default:
927 hinst = 2;
928 break;
931 if(hinst >= 32)
933 if(!implicit)
935 HMODULE16 hModule;
936 NE_MODULE *pModule;
938 hModule = GetModuleHandle16(libname);
939 if(!hModule)
941 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
942 libname, hinst);
943 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
946 pModule = NE_GetPtr(hModule);
947 if(!pModule)
949 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
950 libname, hinst);
951 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
954 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
955 if (!TRACE_ON(module))
956 TRACE_(loaddll)("Loaded module '%s' : %s\n", libname, filetype);
959 * Call initialization routines for all loaded DLLs. Note that
960 * when we load implicitly linked DLLs this will be done by InitTask().
962 if(pModule->flags & NE_FFLAGS_LIBMODULE)
964 NE_InitializeDLLs(hModule);
965 NE_DllProcessAttach(hModule);
968 return hinst;
971 if(hinst != 2)
973 /* We quit searching when we get another error than 'File not found' */
974 break;
977 return hinst; /* The last error that occured */
981 /**********************************************************************
982 * NE_CreateThread
984 * Create the thread for a 16-bit module.
986 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
988 HANDLE hThread;
989 TDB *pTask;
990 HTASK16 hTask;
991 HINSTANCE16 instance = 0;
993 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
994 return 0;
996 /* Post event to start the task */
997 PostEvent16( hTask );
999 /* Wait until we get the instance handle */
1002 DirectedYield16( hTask );
1003 if (!IsTask16( hTask )) /* thread has died */
1005 DWORD exit_code;
1006 WaitForSingleObject( hThread, INFINITE );
1007 GetExitCodeThread( hThread, &exit_code );
1008 CloseHandle( hThread );
1009 return exit_code;
1011 if (!(pTask = TASK_GetPtr( hTask ))) break;
1012 instance = pTask->hInstance;
1013 GlobalUnlock16( hTask );
1014 } while (!instance);
1016 CloseHandle( hThread );
1017 return instance;
1021 /**********************************************************************
1022 * LoadModule (KERNEL.45)
1023 * LoadModule16 (KERNEL32.@)
1025 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1027 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1028 LOADPARAMS16 *params;
1029 HMODULE16 hModule;
1030 NE_MODULE *pModule;
1031 LPSTR cmdline;
1032 WORD cmdShow;
1034 /* Load module */
1036 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1038 /* Special case: second instance of an already loaded NE module */
1040 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
1041 if ( pModule->module32 ) return (HINSTANCE16)21;
1043 /* Increment refcount */
1045 pModule->count++;
1047 else
1049 /* Main case: load first instance of NE module */
1051 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1052 return hModule;
1054 if ( !(pModule = NE_GetPtr( hModule )) )
1055 return (HINSTANCE16)11;
1058 /* If library module, we just retrieve the instance handle */
1060 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1061 return NE_GetInstance( pModule );
1064 * At this point, we need to create a new process.
1066 * pModule points either to an already loaded module, whose refcount
1067 * has already been incremented (to avoid having the module vanish
1068 * in the meantime), or else to a stub module which contains only header
1069 * information.
1071 params = (LOADPARAMS16 *)paramBlock;
1072 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1073 cmdline = MapSL( params->cmdLine );
1074 return NE_CreateThread( pModule, cmdShow, cmdline );
1078 /**********************************************************************
1079 * NE_StartMain
1081 * Start the main NE task.
1083 HINSTANCE16 NE_StartMain( LPCSTR name, HANDLE file )
1085 STARTUPINFOA info;
1086 HMODULE16 hModule;
1087 NE_MODULE *pModule;
1088 INT len;
1089 LPSTR pCmdLine, cmdline = GetCommandLineA();
1091 if ((hModule = NE_LoadExeHeader( file, name )) < 32) return hModule;
1093 if (!(pModule = NE_GetPtr( hModule ))) return (HINSTANCE16)11;
1094 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1096 MESSAGE( "%s is not a valid Win16 executable\n", name );
1097 ExitProcess( ERROR_BAD_EXE_FORMAT );
1100 while (*cmdline && *cmdline != ' ') cmdline++;
1101 if (*cmdline) cmdline++;
1102 len = strlen(cmdline);
1103 pCmdLine = HeapAlloc(GetProcessHeap(), 0, len+2);
1104 if (pCmdLine)
1106 strcpy(pCmdLine+1, cmdline);
1107 *pCmdLine = len;
1109 GetStartupInfoA( &info );
1110 if (!(info.dwFlags & STARTF_USESHOWWINDOW)) info.wShowWindow = 1;
1112 return NE_CreateThread( pModule, info.wShowWindow, pCmdLine );
1116 /**********************************************************************
1117 * NE_StartTask
1119 * Startup code for a new 16-bit task.
1121 DWORD NE_StartTask(void)
1123 TDB *pTask = TASK_GetCurrent();
1124 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1125 HINSTANCE16 hInstance, hPrevInstance;
1126 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1127 WORD sp;
1129 if ( pModule->count > 0 )
1131 /* Second instance of an already loaded NE module */
1132 /* Note that the refcount was already incremented by the parent */
1134 hPrevInstance = NE_GetInstance( pModule );
1136 if ( pModule->dgroup )
1137 if ( NE_CreateSegment( pModule, pModule->dgroup ) )
1138 NE_LoadSegment( pModule, pModule->dgroup );
1140 hInstance = NE_GetInstance( pModule );
1141 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->dgroup, hPrevInstance);
1144 else
1146 /* Load first instance of NE module */
1148 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1150 hInstance = NE_DoLoadModule( pModule );
1151 hPrevInstance = 0;
1154 if ( hInstance >= 32 )
1156 CONTEXT86 context;
1158 /* Enter instance handles into task struct */
1160 pTask->hInstance = hInstance;
1161 pTask->hPrevInstance = hPrevInstance;
1163 /* Free the previous stack selector */
1164 FreeSelector16( SELECTOROF(pTask->teb->cur_stack) );
1166 /* Use DGROUP for 16-bit stack */
1168 if (!(sp = pModule->sp))
1169 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
1170 sp &= ~1;
1171 sp -= sizeof(STACK16FRAME);
1172 pTask->teb->cur_stack = MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1174 /* Registers at initialization must be:
1175 * ax zero
1176 * bx stack size in bytes
1177 * cx heap size in bytes
1178 * si previous app instance
1179 * di current app instance
1180 * bp zero
1181 * es selector to the PSP
1182 * ds dgroup of the application
1183 * ss stack selector
1184 * sp top of the stack
1186 memset( &context, 0, sizeof(context) );
1187 context.SegCs = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
1188 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1189 context.SegEs = pTask->hPDB;
1190 context.Eip = pModule->ip;
1191 context.Ebx = pModule->stack_size;
1192 context.Ecx = pModule->heap_size;
1193 context.Edi = pTask->hInstance;
1194 context.Esi = pTask->hPrevInstance;
1196 /* Now call 16-bit entry point */
1198 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1199 context.SegCs, context.Eip, context.SegDs,
1200 SELECTOROF(pTask->teb->cur_stack),
1201 OFFSETOF(pTask->teb->cur_stack) );
1203 wine_call_to_16_regs_short( &context, 0 );
1204 ExitThread( LOWORD(context.Eax) );
1206 return hInstance; /* error code */
1209 /***********************************************************************
1210 * LoadLibrary (KERNEL.95)
1211 * LoadLibrary16 (KERNEL32.35)
1213 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1215 return LoadModule16(libname, (LPVOID)-1 );
1219 /**********************************************************************
1220 * MODULE_CallWEP
1222 * Call a DLL's WEP, allowing it to shut down.
1223 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1225 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1227 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1228 if (!WEP) return FALSE;
1229 return NE_CallTo16_word_w( WEP, WEP_FREE_DLL );
1233 /**********************************************************************
1234 * NE_FreeModule
1236 * Implementation of FreeModule16().
1238 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1240 HMODULE16 *hPrevModule;
1241 NE_MODULE *pModule;
1242 HMODULE16 *pModRef;
1243 int i;
1245 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1246 hModule = pModule->self;
1248 TRACE("%04x count %d\n", hModule, pModule->count );
1250 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1251 else pModule->count = 0;
1253 if (pModule->flags & NE_FFLAGS_BUILTIN)
1254 return FALSE; /* Can't free built-in module */
1256 if (call_wep && !(pModule->flags & NE_FFLAGS_WIN32))
1258 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1260 MODULE_CallWEP( hModule );
1262 /* Free the objects owned by the DLL module */
1263 TASK_CallTaskSignalProc( USIG16_DLL_UNLOAD, hModule );
1264 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16, hModule );
1266 else
1267 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1271 /* Clear magic number just in case */
1273 pModule->magic = pModule->self = 0;
1275 /* Remove it from the linked list */
1277 hPrevModule = &hFirstModule;
1278 while (*hPrevModule && (*hPrevModule != hModule))
1280 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1282 if (*hPrevModule) *hPrevModule = pModule->next;
1284 /* Free the referenced modules */
1286 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1287 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1289 NE_FreeModule( *pModRef, call_wep );
1292 /* Free the module storage */
1294 GlobalFreeAll16( hModule );
1296 /* Remove module from cache */
1298 if (pCachedModule == pModule) pCachedModule = NULL;
1299 return TRUE;
1303 /**********************************************************************
1304 * FreeModule (KERNEL.46)
1306 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1308 return NE_FreeModule( hModule, TRUE );
1312 /***********************************************************************
1313 * FreeLibrary (KERNEL.96)
1314 * FreeLibrary16 (KERNEL32.36)
1316 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1318 TRACE("%04x\n", handle );
1319 FreeModule16( handle );
1323 /**********************************************************************
1324 * GetModuleName (KERNEL.27)
1326 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1328 NE_MODULE *pModule;
1329 BYTE *p;
1331 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1332 p = (BYTE *)pModule + pModule->name_table;
1333 if (count > *p) count = *p + 1;
1334 if (count > 0)
1336 memcpy( buf, p + 1, count - 1 );
1337 buf[count-1] = '\0';
1339 return TRUE;
1343 /**********************************************************************
1344 * GetModuleUsage (KERNEL.48)
1346 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1348 NE_MODULE *pModule = NE_GetPtr( hModule );
1349 return pModule ? pModule->count : 0;
1353 /**********************************************************************
1354 * GetExpWinVer (KERNEL.167)
1356 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1358 NE_MODULE *pModule = NE_GetPtr( hModule );
1359 if ( !pModule ) return 0;
1362 * For built-in modules, fake the expected version the module should
1363 * have according to the Windows version emulated by Wine
1365 if ( !pModule->expected_version )
1367 OSVERSIONINFOA versionInfo;
1368 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1370 if ( GetVersionExA( &versionInfo ) )
1371 pModule->expected_version =
1372 (versionInfo.dwMajorVersion & 0xff) << 8
1373 | (versionInfo.dwMinorVersion & 0xff);
1376 return pModule->expected_version;
1380 /**********************************************************************
1381 * GetModuleFileName (KERNEL.49)
1382 * GetModuleFileName16 (KERNEL32.@)
1384 * Comment: see GetModuleFileNameA
1386 * Even if invoked by second instance of a program,
1387 * it still returns path of first one.
1389 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1390 INT16 nSize )
1392 NE_MODULE *pModule;
1394 /* Win95 does not query hModule if set to 0 !
1395 * Is this wrong or maybe Win3.1 only ? */
1396 if (!hModule) hModule = GetCurrentTask();
1398 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1399 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1400 if (pModule->expected_version >= 0x400)
1401 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1402 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1403 return strlen(lpFileName);
1407 /**********************************************************************
1408 * GetModuleHandle (KERNEL.47)
1410 * Find a module from a module name.
1412 * NOTE: The current implementation works the same way the Windows 95 one
1413 * does. Do not try to 'fix' it, fix the callers.
1414 * + It does not do ANY extension handling (except that strange .EXE bit)!
1415 * + It does not care about paths, just about basenames. (same as Windows)
1417 * RETURNS
1418 * LOWORD:
1419 * the win16 module handle if found
1420 * 0 if not
1421 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1422 * Always hFirstModule
1424 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1426 if (HIWORD(name) == 0)
1427 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1428 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1431 /***********************************************************************
1432 * GetModuleHandle16 (KERNEL32.@)
1434 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1436 HMODULE16 hModule = hFirstModule;
1437 LPSTR s;
1438 BYTE len, *name_table;
1439 char tmpstr[MAX_PATH];
1440 NE_MODULE *pModule;
1442 TRACE("(%s)\n", name);
1444 if (!HIWORD(name))
1445 return GetExePtr(LOWORD(name));
1447 len = strlen(name);
1448 if (!len)
1449 return 0;
1451 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1453 /* If 'name' matches exactly the module name of a module:
1454 * Return its handle.
1456 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1458 pModule = NE_GetPtr( hModule );
1459 if (!pModule) break;
1460 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1462 name_table = (BYTE *)pModule + pModule->name_table;
1463 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1464 return hModule;
1467 /* If uppercased 'name' matches exactly the module name of a module:
1468 * Return its handle
1470 for (s = tmpstr; *s; s++) *s = FILE_toupper(*s);
1472 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1474 pModule = NE_GetPtr( hModule );
1475 if (!pModule) break;
1476 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1478 name_table = (BYTE *)pModule + pModule->name_table;
1479 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1480 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1481 * lowercased module names, but try to load uppercase DLLs, so this
1482 * 'i' compare is just a quickfix until the loader handles that
1483 * correctly. -MM 990705
1485 if ((*name_table == len) && !FILE_strncasecmp(tmpstr, name_table+1, len))
1486 return hModule;
1489 /* If the base filename of 'name' matches the base filename of the module
1490 * filename of some module (case-insensitive compare):
1491 * Return its handle.
1494 /* basename: search backwards in passed name to \ / or : */
1495 s = tmpstr + strlen(tmpstr);
1496 while (s > tmpstr)
1498 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1499 break;
1500 s--;
1503 /* search this in loaded filename list */
1504 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1506 char *loadedfn;
1507 OFSTRUCT *ofs;
1509 pModule = NE_GetPtr( hModule );
1510 if (!pModule) break;
1511 if (!pModule->fileinfo) continue;
1512 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1514 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1515 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1516 /* basename: search backwards in pathname to \ / or : */
1517 while (loadedfn > (char*)ofs->szPathName)
1519 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1520 break;
1521 loadedfn--;
1523 /* case insensitive compare ... */
1524 if (!FILE_strcasecmp(loadedfn, s))
1525 return hModule;
1528 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1529 * matches the base filename of the module filename of some 32-bit module:
1530 * Return the corresponding 16-bit dummy module handle.
1532 if (len >= 4 && !FILE_strcasecmp(name+len-4, ".EXE"))
1534 HMODULE hModule = GetModuleHandleA( name );
1535 if ( hModule )
1536 return MapHModuleLS( hModule );
1539 if (!strcmp(tmpstr,"MSDOS"))
1540 return 1;
1542 if (!strcmp(tmpstr,"TIMER"))
1544 FIXME("Eh... Should return caller's code segment, expect crash\n");
1545 return 0;
1548 return 0;
1551 /**********************************************************************
1552 * NE_GetModuleByFilename
1554 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1556 HMODULE16 hModule;
1557 LPSTR s, p;
1558 BYTE len, *name_table;
1559 char tmpstr[MAX_PATH];
1560 NE_MODULE *pModule;
1562 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1564 /* If the base filename of 'name' matches the base filename of the module
1565 * filename of some module (case-insensitive compare):
1566 * Return its handle.
1569 /* basename: search backwards in passed name to \ / or : */
1570 s = tmpstr + strlen(tmpstr);
1571 while (s > tmpstr)
1573 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1574 break;
1575 s--;
1578 /* search this in loaded filename list */
1579 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1581 char *loadedfn;
1582 OFSTRUCT *ofs;
1584 pModule = NE_GetPtr( hModule );
1585 if (!pModule) break;
1586 if (!pModule->fileinfo) continue;
1587 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1589 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1590 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1591 /* basename: search backwards in pathname to \ / or : */
1592 while (loadedfn > (char*)ofs->szPathName)
1594 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1595 break;
1596 loadedfn--;
1598 /* case insensitive compare ... */
1599 if (!FILE_strcasecmp(loadedfn, s))
1600 return hModule;
1602 /* If basename (without ext) matches the module name of a module:
1603 * Return its handle.
1606 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1607 len = strlen(s);
1609 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1611 pModule = NE_GetPtr( hModule );
1612 if (!pModule) break;
1613 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1615 name_table = (BYTE *)pModule + pModule->name_table;
1616 if ((*name_table == len) && !FILE_strncasecmp(s, name_table+1, len))
1617 return hModule;
1620 return 0;
1623 /**********************************************************************
1624 * ModuleFirst (TOOLHELP.59)
1626 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1628 lpme->wNext = hFirstModule;
1629 return ModuleNext16( lpme );
1633 /**********************************************************************
1634 * ModuleNext (TOOLHELP.60)
1636 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1638 NE_MODULE *pModule;
1639 char *name;
1641 if (!lpme->wNext) return FALSE;
1642 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1643 name = (char *)pModule + pModule->name_table;
1644 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1645 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1646 lpme->hModule = lpme->wNext;
1647 lpme->wcUsage = pModule->count;
1648 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1649 lpme->wNext = pModule->next;
1650 return TRUE;
1654 /**********************************************************************
1655 * ModuleFindName (TOOLHELP.61)
1657 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1659 lpme->wNext = GetModuleHandle16( name );
1660 return ModuleNext16( lpme );
1664 /**********************************************************************
1665 * ModuleFindHandle (TOOLHELP.62)
1667 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1669 hModule = GetExePtr( hModule );
1670 lpme->wNext = hModule;
1671 return ModuleNext16( lpme );
1675 /***************************************************************************
1676 * IsRomModule (KERNEL.323)
1678 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1680 return FALSE;
1683 /***************************************************************************
1684 * IsRomFile (KERNEL.326)
1686 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1688 return FALSE;
1691 /***************************************************************************
1692 * MapHModuleLS (KERNEL32.@)
1694 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod) {
1695 NE_MODULE *pModule;
1697 if (!hmod)
1698 return TASK_GetCurrent()->hInstance;
1699 if (!HIWORD(hmod))
1700 return hmod; /* we already have a 16 bit module handle */
1701 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1702 while (pModule) {
1703 if (pModule->module32 == hmod)
1704 return pModule->self;
1705 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1707 return 0;
1710 /***************************************************************************
1711 * MapHModuleSL (KERNEL32.@)
1713 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1714 NE_MODULE *pModule;
1716 if (!hmod) {
1717 TDB *pTask = TASK_GetCurrent();
1718 hmod = pTask->hModule;
1720 pModule = (NE_MODULE*)GlobalLock16(hmod);
1721 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1722 !(pModule->flags & NE_FFLAGS_WIN32)
1724 return 0;
1725 return pModule->module32;
1728 /***************************************************************************
1729 * MapHInstLS (KERNEL32.@)
1730 * MapHInstLS (KERNEL.472)
1732 void WINAPI MapHInstLS( CONTEXT86 *context )
1734 context->Eax = MapHModuleLS(context->Eax);
1737 /***************************************************************************
1738 * MapHInstSL (KERNEL32.@)
1739 * MapHInstSL (KERNEL.473)
1741 void WINAPI MapHInstSL( CONTEXT86 *context )
1743 context->Eax = MapHModuleSL(context->Eax);
1746 /***************************************************************************
1747 * MapHInstLS_PN (KERNEL32.@)
1749 void WINAPI MapHInstLS_PN( CONTEXT86 *context )
1751 if (context->Eax) context->Eax = MapHModuleLS(context->Eax);
1754 /***************************************************************************
1755 * MapHInstSL_PN (KERNEL32.@)
1757 void WINAPI MapHInstSL_PN( CONTEXT86 *context )
1759 if (context->Eax) context->Eax = MapHModuleSL(context->Eax);