Abey George (of Macadamian/Corel)
[wine.git] / loader / ne / module.c
blobe1fec11647595e343921c1b08cc2a8a414d37588
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/winbase16.h"
14 #include "winerror.h"
15 #include "module.h"
16 #include "neexe.h"
17 #include "toolhelp.h"
18 #include "file.h"
19 #include "ldt.h"
20 #include "callback.h"
21 #include "heap.h"
22 #include "task.h"
23 #include "global.h"
24 #include "process.h"
25 #include "snoop.h"
26 #include "syslevel.h"
27 #include "builtin16.h"
28 #include "stackframe.h"
29 #include "debugtools.h"
30 #include "loadorder.h"
31 #include "elfdll.h"
33 DEFAULT_DEBUG_CHANNEL(module)
35 #define hFirstModule (pThhook->hExeHead)
37 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
39 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
40 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
42 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
44 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
46 /***********************************************************************
47 * NE_GetPtr
49 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
51 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
55 /***********************************************************************
56 * NE_DumpModule
58 void NE_DumpModule( HMODULE16 hModule )
60 int i, ordinal;
61 SEGTABLEENTRY *pSeg;
62 BYTE *pstr;
63 WORD *pword;
64 NE_MODULE *pModule;
65 ET_BUNDLE *bundle;
66 ET_ENTRY *entry;
68 if (!(pModule = NE_GetPtr( hModule )))
70 MESSAGE( "**** %04x is not a module handle\n", hModule );
71 return;
74 /* Dump the module info */
75 DPRINTF( "---\n" );
76 DPRINTF( "Module %04x:\n", hModule );
77 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
78 pModule->count, pModule->flags,
79 pModule->heap_size, pModule->stack_size );
80 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
81 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
82 pModule->seg_count, pModule->modref_count );
83 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
84 pModule->os_flags, pModule->min_swap_area,
85 pModule->expected_version );
86 if (pModule->flags & NE_FFLAGS_WIN32)
87 DPRINTF( "PE module=%08x\n", pModule->module32 );
89 /* Dump the file info */
90 DPRINTF( "---\n" );
91 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
93 /* Dump the segment table */
94 DPRINTF( "---\n" );
95 DPRINTF( "Segment table:\n" );
96 pSeg = NE_SEG_TABLE( pModule );
97 for (i = 0; i < pModule->seg_count; i++, pSeg++)
98 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
99 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
100 pSeg->minsize, pSeg->hSeg );
102 /* Dump the resource table */
103 DPRINTF( "---\n" );
104 DPRINTF( "Resource table:\n" );
105 if (pModule->res_table)
107 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
108 DPRINTF( "Alignment: %d\n", *pword++ );
109 while (*pword)
111 struct resource_typeinfo_s *ptr = (struct resource_typeinfo_s *)pword;
112 struct resource_nameinfo_s *pname = (struct resource_nameinfo_s *)(ptr + 1);
113 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
114 for (i = 0; i < ptr->count; i++, pname++)
115 DPRINTF( "offset=%d len=%d id=%04x\n",
116 pname->offset, pname->length, pname->id );
117 pword = (WORD *)pname;
120 else DPRINTF( "None\n" );
122 /* Dump the resident name table */
123 DPRINTF( "---\n" );
124 DPRINTF( "Resident-name table:\n" );
125 pstr = (char *)pModule + pModule->name_table;
126 while (*pstr)
128 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
129 *(WORD *)(pstr + *pstr + 1) );
130 pstr += *pstr + 1 + sizeof(WORD);
133 /* Dump the module reference table */
134 DPRINTF( "---\n" );
135 DPRINTF( "Module ref table:\n" );
136 if (pModule->modref_table)
138 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
139 for (i = 0; i < pModule->modref_count; i++, pword++)
141 char name[10];
142 GetModuleName16( *pword, name, sizeof(name) );
143 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
146 else DPRINTF( "None\n" );
148 /* Dump the entry table */
149 DPRINTF( "---\n" );
150 DPRINTF( "Entry table:\n" );
151 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
152 do {
153 entry = (ET_ENTRY *)((BYTE *)bundle+6);
154 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
155 ordinal = bundle->first;
156 while (ordinal < bundle->last)
158 if (entry->type == 0xff)
159 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
160 else
161 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
162 entry++;
164 } while ( (bundle->next)
165 && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
167 /* Dump the non-resident names table */
168 DPRINTF( "---\n" );
169 DPRINTF( "Non-resident names table:\n" );
170 if (pModule->nrname_handle)
172 pstr = (char *)GlobalLock16( pModule->nrname_handle );
173 while (*pstr)
175 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
176 *(WORD *)(pstr + *pstr + 1) );
177 pstr += *pstr + 1 + sizeof(WORD);
180 DPRINTF( "\n" );
184 /***********************************************************************
185 * NE_WalkModules
187 * Walk the module list and print the modules.
189 void NE_WalkModules(void)
191 HMODULE16 hModule = hFirstModule;
192 MESSAGE( "Module Flags Name\n" );
193 while (hModule)
195 NE_MODULE *pModule = NE_GetPtr( hModule );
196 if (!pModule)
198 MESSAGE( "Bad module %04x in list\n", hModule );
199 return;
201 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->flags,
202 *((char *)pModule + pModule->name_table),
203 (char *)pModule + pModule->name_table + 1 );
204 hModule = pModule->next;
209 /**********************************************************************
210 * NE_RegisterModule
212 void NE_RegisterModule( NE_MODULE *pModule )
214 pModule->next = hFirstModule;
215 hFirstModule = pModule->self;
219 /***********************************************************************
220 * NE_GetOrdinal
222 * Lookup the ordinal for a given name.
224 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
226 unsigned char buffer[256], *cpnt;
227 BYTE len;
228 NE_MODULE *pModule;
230 if (!(pModule = NE_GetPtr( hModule ))) return 0;
231 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
233 TRACE("(%04x,'%s')\n", hModule, name );
235 /* First handle names of the form '#xxxx' */
237 if (name[0] == '#') return atoi( name + 1 );
239 /* Now copy and uppercase the string */
241 strcpy( buffer, name );
242 CharUpperA( buffer );
243 len = strlen( buffer );
245 /* First search the resident names */
247 cpnt = (char *)pModule + pModule->name_table;
249 /* Skip the first entry (module name) */
250 cpnt += *cpnt + 1 + sizeof(WORD);
251 while (*cpnt)
253 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
255 TRACE(" Found: ordinal=%d\n",
256 *(WORD *)(cpnt + *cpnt + 1) );
257 return *(WORD *)(cpnt + *cpnt + 1);
259 cpnt += *cpnt + 1 + sizeof(WORD);
262 /* Now search the non-resident names table */
264 if (!pModule->nrname_handle) return 0; /* No non-resident table */
265 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
267 /* Skip the first entry (module description string) */
268 cpnt += *cpnt + 1 + sizeof(WORD);
269 while (*cpnt)
271 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
273 TRACE(" Found: ordinal=%d\n",
274 *(WORD *)(cpnt + *cpnt + 1) );
275 return *(WORD *)(cpnt + *cpnt + 1);
277 cpnt += *cpnt + 1 + sizeof(WORD);
279 return 0;
283 /***********************************************************************
284 * NE_GetEntryPoint (WPROCS.27)
286 * Return the entry point for a given ordinal.
288 FARPROC16 WINAPI WIN16_NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
290 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
291 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
292 return ret;
294 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
296 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
298 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
300 NE_MODULE *pModule;
301 WORD sel, offset, i;
303 ET_ENTRY *entry;
304 ET_BUNDLE *bundle;
306 if (!(pModule = NE_GetPtr( hModule ))) return 0;
307 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
309 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
310 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
312 if (!(bundle->next))
313 return 0;
314 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
317 entry = (ET_ENTRY *)((BYTE *)bundle+6);
318 for (i=0; i < (ordinal - bundle->first - 1); i++)
319 entry++;
321 sel = entry->segnum;
322 offset = entry->offs;
324 if (sel == 0xfe) sel = 0xffff; /* constant entry */
325 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
326 if (sel==0xffff)
327 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
328 if (!snoop)
329 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
330 else
331 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset ));
335 /***********************************************************************
336 * NE_SetEntryPoint
338 * Change the value of an entry point. Use with caution!
339 * It can only change the offset value, not the selector.
341 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
343 NE_MODULE *pModule;
344 ET_ENTRY *entry;
345 ET_BUNDLE *bundle;
346 int i;
348 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
349 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
351 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
352 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
354 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
355 if (!(bundle->next))
356 return 0;
359 entry = (ET_ENTRY *)((BYTE *)bundle+6);
360 for (i=0; i < (ordinal - bundle->first - 1); i++)
361 entry++;
363 entry->offs = offset;
364 return TRUE;
368 /***********************************************************************
369 * NE_OpenFile
371 HANDLE NE_OpenFile( NE_MODULE *pModule )
373 char *name;
375 static HANDLE cachedfd = -1;
377 TRACE("(%p) cache: mod=%p fd=%d\n",
378 pModule, pCachedModule, cachedfd );
379 if (pCachedModule == pModule) return cachedfd;
380 CloseHandle( cachedfd );
381 pCachedModule = pModule;
382 name = NE_MODULE_NAME( pModule );
383 if ((cachedfd = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
384 NULL, OPEN_EXISTING, 0, -1 )) == -1)
385 MESSAGE( "Can't open file '%s' for module %04x\n", name, pModule->self );
386 TRACE("opened '%s' -> %d\n",
387 name, cachedfd );
388 return cachedfd;
392 /***********************************************************************
393 * NE_LoadExeHeader
395 static HMODULE16 NE_LoadExeHeader( LPCSTR filename )
397 IMAGE_DOS_HEADER mz_header;
398 IMAGE_OS2_HEADER ne_header;
399 int size;
400 HMODULE16 hModule;
401 NE_MODULE *pModule;
402 BYTE *pData, *pTempEntryTable;
403 char *buffer, *fastload = NULL;
404 int fastload_offset = 0, fastload_length = 0;
405 ET_ENTRY *entry;
406 ET_BUNDLE *bundle, *oldbundle;
407 HFILE16 hFile;
408 OFSTRUCT ofs;
410 /* Open file */
411 if ((hFile = OpenFile16( filename, &ofs, OF_READ )) == HFILE_ERROR16)
412 return (HMODULE16)2; /* File not found */
414 /* Read a block from either the file or the fast-load area. */
415 #define READ(offset,size,buffer) \
416 ((fastload && ((offset) >= fastload_offset) && \
417 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
418 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
419 (_llseek16( hFile, (offset), SEEK_SET), \
420 _hread16( hFile, (buffer), (size) ) == (size)))
422 _llseek16( hFile, 0, SEEK_SET );
423 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
424 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
426 _lclose16( hFile );
427 return (HMODULE16)11; /* invalid exe */
430 _llseek16( hFile, mz_header.e_lfanew, SEEK_SET );
431 if (_hread16( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
433 _lclose16( hFile );
434 return (HMODULE16)11; /* invalid exe */
437 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
438 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
440 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
441 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
442 _lclose16( hFile );
443 return (HMODULE16)12;
446 /* We now have a valid NE header */
448 size = sizeof(NE_MODULE) +
449 /* segment table */
450 ne_header.ne_cseg * sizeof(SEGTABLEENTRY) +
451 /* resource table */
452 ne_header.ne_restab - ne_header.ne_rsrctab +
453 /* resident names table */
454 ne_header.ne_modtab - ne_header.ne_restab +
455 /* module ref table */
456 ne_header.ne_cmod * sizeof(WORD) +
457 /* imported names table */
458 ne_header.ne_enttab - ne_header.ne_imptab +
459 /* entry table length */
460 ne_header.ne_cbenttab +
461 /* entry table extra conversion space */
462 sizeof(ET_BUNDLE) +
463 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6) +
464 /* loaded file info */
465 sizeof(OFSTRUCT)-sizeof(ofs.szPathName)+strlen(ofs.szPathName)+1;
467 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
468 if (!hModule)
470 _lclose16( hFile );
471 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 _llseek16( hFile, fastload_offset, SEEK_SET);
502 if (_hread16(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 = (int)pData - (int)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 _lclose16( hFile );
530 return (HMODULE16)11; /* invalid exe */
532 pSeg = (struct ne_segment_table_entry_s *)buffer;
533 for (i = ne_header.ne_cseg; i > 0; i--, pSeg++)
535 memcpy( pData, pSeg, sizeof(*pSeg) );
536 pData += sizeof(SEGTABLEENTRY);
538 HeapFree( GetProcessHeap(), 0, buffer );
540 else
542 if (fastload)
543 HeapFree( GetProcessHeap(), 0, fastload );
544 GlobalFree16( hModule );
545 _lclose16( hFile );
546 return (HMODULE16)11; /* invalid exe */
549 /* Get the resource table */
551 if (ne_header.ne_rsrctab < ne_header.ne_restab)
553 pModule->res_table = (int)pData - (int)pModule;
554 if (!READ(mz_header.e_lfanew + ne_header.ne_rsrctab,
555 ne_header.ne_restab - ne_header.ne_rsrctab,
556 pData ))
558 _lclose16( hFile );
559 return (HMODULE16)11; /* invalid exe */
561 pData += ne_header.ne_restab - ne_header.ne_rsrctab;
562 NE_InitResourceHandler( hModule );
564 else pModule->res_table = 0; /* No resource table */
566 /* Get the resident names table */
568 pModule->name_table = (int)pData - (int)pModule;
569 if (!READ( mz_header.e_lfanew + ne_header.ne_restab,
570 ne_header.ne_modtab - ne_header.ne_restab,
571 pData ))
573 if (fastload)
574 HeapFree( GetProcessHeap(), 0, fastload );
575 GlobalFree16( hModule );
576 _lclose16( hFile );
577 return (HMODULE16)11; /* invalid exe */
579 pData += ne_header.ne_modtab - ne_header.ne_restab;
581 /* Get the module references table */
583 if (ne_header.ne_cmod > 0)
585 pModule->modref_table = (int)pData - (int)pModule;
586 if (!READ( mz_header.e_lfanew + ne_header.ne_modtab,
587 ne_header.ne_cmod * sizeof(WORD),
588 pData ))
590 if (fastload)
591 HeapFree( GetProcessHeap(), 0, fastload );
592 GlobalFree16( hModule );
593 _lclose16( hFile );
594 return (HMODULE16)11; /* invalid exe */
596 pData += ne_header.ne_cmod * sizeof(WORD);
598 else pModule->modref_table = 0; /* No module references */
600 /* Get the imported names table */
602 pModule->import_table = (int)pData - (int)pModule;
603 if (!READ( mz_header.e_lfanew + ne_header.ne_imptab,
604 ne_header.ne_enttab - ne_header.ne_imptab,
605 pData ))
607 if (fastload)
608 HeapFree( GetProcessHeap(), 0, fastload );
609 GlobalFree16( hModule );
610 _lclose16( hFile );
611 return (HMODULE16)11; /* invalid exe */
613 pData += ne_header.ne_enttab - ne_header.ne_imptab;
615 /* Load entry table, convert it to the optimized version used by Windows */
617 if ((pTempEntryTable = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cbenttab)) != NULL)
619 BYTE nr_entries, type, *s;
621 TRACE("Converting entry table.\n");
622 pModule->entry_table = (int)pData - (int)pModule;
623 if (!READ( mz_header.e_lfanew + ne_header.ne_enttab,
624 ne_header.ne_cbenttab, pTempEntryTable ))
626 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
627 if (fastload)
628 HeapFree( GetProcessHeap(), 0, fastload );
629 GlobalFree16( hModule );
630 _lclose16( hFile );
631 return (HMODULE16)11; /* invalid exe */
634 s = pTempEntryTable;
635 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header.ne_enttab, ne_header.ne_cbenttab, *s);
637 bundle = (ET_BUNDLE *)pData;
638 TRACE("first bundle: %p\n", bundle);
639 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
640 entry = (ET_ENTRY *)((BYTE *)bundle+6);
642 while ((nr_entries = *s++))
644 if ((type = *s++))
646 bundle->last += nr_entries;
647 if (type == 0xff)
648 while (nr_entries--)
650 entry->type = type;
651 entry->flags = *s++;
652 s += 2;
653 entry->segnum = *s++;
654 entry->offs = *(WORD *)s; s += 2;
655 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
656 entry++;
658 else
659 while (nr_entries--)
661 entry->type = type;
662 entry->flags = *s++;
663 entry->segnum = type;
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++;
669 else
671 if (bundle->first == bundle->last)
673 bundle->first += nr_entries;
674 bundle->last += nr_entries;
676 else
678 oldbundle = bundle;
679 oldbundle->next = ((int)entry - (int)pModule);
680 bundle = (ET_BUNDLE *)entry;
681 TRACE("new bundle: %p\n", bundle);
682 bundle->first = bundle->last =
683 oldbundle->last + nr_entries;
684 bundle->next = 0;
685 (BYTE *)entry += sizeof(ET_BUNDLE);
689 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
691 else
693 if (fastload)
694 HeapFree( GetProcessHeap(), 0, fastload );
695 GlobalFree16( hModule );
696 _lclose16( hFile );
697 return (HMODULE16)11; /* invalid exe */
700 pData += ne_header.ne_cbenttab + sizeof(ET_BUNDLE) +
701 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6);
703 if ((DWORD)entry > (DWORD)pData)
704 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
706 /* Store the filename information */
708 pModule->fileinfo = (int)pData - (int)pModule;
709 size = sizeof(OFSTRUCT)-sizeof(ofs.szPathName)+strlen(ofs.szPathName)+1;
710 ofs.cBytes = size - 1;
711 memcpy( pData, &ofs, size );
712 pData += size;
714 /* Free the fast-load area */
716 #undef READ
717 if (fastload)
718 HeapFree( GetProcessHeap(), 0, fastload );
720 /* Get the non-resident names table */
722 if (ne_header.ne_cbnrestab)
724 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.ne_cbnrestab,
725 hModule, FALSE, FALSE, FALSE );
726 if (!pModule->nrname_handle)
728 GlobalFree16( hModule );
729 _lclose16( hFile );
730 return (HMODULE16)11; /* invalid exe */
732 buffer = GlobalLock16( pModule->nrname_handle );
733 _llseek16( hFile, ne_header.ne_nrestab, SEEK_SET );
734 if (_hread16( hFile, buffer, ne_header.ne_cbnrestab )
735 != ne_header.ne_cbnrestab)
737 GlobalFree16( pModule->nrname_handle );
738 GlobalFree16( hModule );
739 _lclose16( hFile );
740 return (HMODULE16)11; /* invalid exe */
743 else pModule->nrname_handle = 0;
745 /* Allocate a segment for the implicitly-loaded DLLs */
747 if (pModule->modref_count)
749 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
750 (pModule->modref_count+1)*sizeof(HMODULE16),
751 hModule, FALSE, FALSE, FALSE );
752 if (!pModule->dlls_to_init)
754 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
755 GlobalFree16( hModule );
756 _lclose16( hFile );
757 return (HMODULE16)11; /* invalid exe */
760 else pModule->dlls_to_init = 0;
762 NE_RegisterModule( pModule );
763 SNOOP16_RegisterDLL(pModule,ofs.szPathName);
765 _lclose16( hFile );
766 return hModule;
770 /***********************************************************************
771 * NE_LoadDLLs
773 * Load all DLLs implicitly linked to a module.
775 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
777 int i;
778 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
779 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
781 for (i = 0; i < pModule->modref_count; i++, pModRef++)
783 char buffer[260], *p;
784 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
785 memcpy( buffer, pstr + 1, *pstr );
786 *(buffer + *pstr) = 0; /* terminate it */
788 TRACE("Loading '%s'\n", buffer );
789 if (!(*pModRef = GetModuleHandle16( buffer )))
791 /* If the DLL is not loaded yet, load it and store */
792 /* its handle in the list of DLLs to initialize. */
793 HMODULE16 hDLL;
795 /* Append .DLL to name if no extension present */
796 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
797 strcat( buffer, ".DLL" );
799 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
801 /* FIXME: cleanup what was done */
803 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
804 buffer, *((BYTE*)pModule + pModule->name_table),
805 (char *)pModule + pModule->name_table + 1, hDLL );
806 return FALSE;
808 *pModRef = GetExePtr( hDLL );
809 *pDLLs++ = *pModRef;
811 else /* Increment the reference count of the DLL */
813 NE_MODULE *pOldDLL;
815 pOldDLL = NE_GetPtr( *pModRef );
816 if (pOldDLL) pOldDLL->count++;
819 return TRUE;
823 /**********************************************************************
824 * NE_DoLoadModule
826 * Load first instance of NE module from file.
828 * pModule must point to a module structure prepared by NE_LoadExeHeader.
829 * This routine must never be called twice on a module.
832 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
834 /* Allocate the segments for this module */
836 if (!NE_CreateAllSegments( pModule ))
837 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
839 /* Load the referenced DLLs */
841 if (!NE_LoadDLLs( pModule ))
842 return ERROR_FILE_NOT_FOUND; /* 2 */
844 /* Load the segments */
846 NE_LoadAllSegments( pModule );
848 /* Make sure the usage count is 1 on the first loading of */
849 /* the module, even if it contains circular DLL references */
851 pModule->count = 1;
853 return NE_GetInstance( pModule );
856 /**********************************************************************
857 * NE_LoadModule
859 * Load first instance of NE module. (Note: caller is responsible for
860 * ensuring the module isn't already loaded!)
862 * If the module turns out to be an executable module, only a
863 * handle to a module stub is returned; this needs to be initialized
864 * by calling NE_DoLoadModule later, in the context of the newly
865 * created process.
867 * If lib_only is TRUE, however, the module is perforce treated
868 * like a DLL module, even if it is an executable module.
871 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
873 NE_MODULE *pModule;
874 HMODULE16 hModule;
875 HINSTANCE16 hInstance;
877 hModule = NE_LoadExeHeader( name );
878 if (hModule < 32) return hModule;
880 pModule = NE_GetPtr( hModule );
881 if ( !pModule ) return hModule;
883 if ( !lib_only && !( pModule->flags & NE_FFLAGS_LIBMODULE ) )
884 return hModule;
886 hInstance = NE_DoLoadModule( pModule );
887 if ( hInstance < 32 )
889 /* cleanup ... */
890 NE_FreeModule( hModule, 0 );
893 return hInstance;
897 /**********************************************************************
898 * MODULE_LoadModule16
900 * Load a NE module in the order of the loadorder specification.
901 * The caller is responsible that the module is not loaded already.
904 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
906 HINSTANCE16 hinst;
907 int i;
908 module_loadorder_t *plo;
910 plo = MODULE_GetLoadOrder(libname);
912 for(i = 0; i < MODULE_LOADORDER_NTYPES; i++)
914 switch(plo->loadorder[i])
916 case MODULE_LOADORDER_DLL:
917 TRACE("Trying native dll '%s'\n", libname);
918 hinst = NE_LoadModule(libname, lib_only);
919 break;
921 case MODULE_LOADORDER_ELFDLL:
922 TRACE("Trying elfdll '%s'\n", libname);
923 hinst = ELFDLL_LoadModule16(libname);
924 break;
926 case MODULE_LOADORDER_BI:
927 TRACE("Trying built-in '%s'\n", libname);
928 hinst = BUILTIN_LoadModule(libname);
929 break;
931 default:
932 ERR("Got invalid loadorder type %d (%s index %d)\n", plo->loadorder[i], plo->modulename, i);
933 /* Fall through */
935 case MODULE_LOADORDER_SO: /* This is not supported for NE modules */
936 case MODULE_LOADORDER_INVALID: /* We ignore this as it is an empty entry */
937 hinst = 2;
938 break;
941 if(hinst >= 32)
943 if(!implicit)
945 HMODULE16 hModule;
946 NE_MODULE *pModule;
948 hModule = GetModuleHandle16(libname);
949 if(!hModule)
951 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle\n",
952 libname, hinst);
953 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
956 pModule = NE_GetPtr(hModule);
957 if(!pModule)
959 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
960 libname, hinst);
961 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
964 TRACE("Loaded module '%s' at 0x%04x, \n", libname, hinst);
967 * Call initialization routines for all loaded DLLs. Note that
968 * when we load implicitly linked DLLs this will be done by InitTask().
970 if(pModule->flags & NE_FFLAGS_LIBMODULE)
972 NE_InitializeDLLs(hModule);
973 NE_DllProcessAttach(hModule);
976 return hinst;
979 if(hinst != 2)
981 /* We quit searching when we get another error than 'File not found' */
982 break;
985 return hinst; /* The last error that occured */
989 /**********************************************************************
990 * LoadModule16 (KERNEL.45)
992 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
994 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
995 LOADPARAMS16 *params;
996 LPSTR cmd_line, new_cmd_line;
997 LPCVOID env = NULL;
998 STARTUPINFOA startup;
999 PROCESS_INFORMATION info;
1000 HMODULE16 hModule;
1001 NE_MODULE *pModule;
1002 PDB *pdb;
1004 /* Load module */
1006 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1008 /* Special case: second instance of an already loaded NE module */
1010 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
1011 if ( pModule->module32 ) return (HINSTANCE16)21;
1013 /* Increment refcount */
1015 pModule->count++;
1017 else
1019 /* Main case: load first instance of NE module */
1021 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1022 return hModule;
1024 if ( !(pModule = NE_GetPtr( hModule )) )
1025 return (HINSTANCE16)11;
1028 /* If library module, we just retrieve the instance handle */
1030 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1031 return NE_GetInstance( pModule );
1034 * At this point, we need to create a new process.
1036 * pModule points either to an already loaded module, whose refcount
1037 * has already been incremented (to avoid having the module vanish
1038 * in the meantime), or else to a stub module which contains only header
1039 * information.
1041 * All remaining initialization (really loading the module in the second
1042 * case, and creating the new instance in both cases) are to be done in
1043 * the context of the new process. This happens in the NE_InitProcess
1044 * routine, which will be called from the 32-bit process initialization.
1048 params = (LOADPARAMS16 *)paramBlock;
1049 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
1050 if (!cmd_line) cmd_line = "";
1051 else if (*cmd_line) cmd_line++; /* skip the length byte */
1053 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1054 strlen(cmd_line)+strlen(name)+2 )))
1055 return 0;
1056 strcpy( new_cmd_line, name );
1057 strcat( new_cmd_line, " " );
1058 strcat( new_cmd_line, cmd_line );
1060 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
1062 memset( &info, '\0', sizeof(info) );
1063 memset( &startup, '\0', sizeof(startup) );
1064 startup.cb = sizeof(startup);
1065 if (params->showCmd)
1067 startup.dwFlags = STARTF_USESHOWWINDOW;
1068 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
1071 SYSLEVEL_ReleaseWin16Lock();
1072 pdb = PROCESS_Create( pModule, -1, new_cmd_line, env,
1073 NULL, NULL, TRUE, 0, &startup, &info );
1074 SYSLEVEL_RestoreWin16Lock();
1076 CloseHandle( info.hThread );
1077 CloseHandle( info.hProcess );
1079 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
1080 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1082 return GetProcessDword( info.dwProcessId, GPD_HINSTANCE16 );
1085 /**********************************************************************
1086 * NE_CreateProcess
1088 BOOL NE_CreateProcess( HANDLE hFile, LPCSTR filename, LPCSTR cmd_line, LPCSTR env,
1089 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1090 BOOL inherit, DWORD flags, LPSTARTUPINFOA startup,
1091 LPPROCESS_INFORMATION info )
1093 HMODULE16 hModule;
1094 NE_MODULE *pModule;
1096 SYSLEVEL_EnterWin16Lock();
1098 /* Special case: second instance of an already loaded NE module
1099 * FIXME: maybe we should mark the module in a special way during
1100 * "second instance" loading stage ?
1101 * NE_CreateSegment and NE_LoadSegment might get confused without it,
1102 * especially when it comes to self-loaders */
1104 if ( ( hModule = NE_GetModuleByFilename( filename ) ) != 0 )
1106 if ( !( pModule = NE_GetPtr( hModule) )
1107 || ( pModule->flags & NE_FFLAGS_LIBMODULE )
1108 || pModule->module32 )
1110 SetLastError( ERROR_BAD_FORMAT );
1111 goto error;
1114 pModule->count++;
1117 /* Main case: load first instance of NE module */
1118 else
1120 /* Load module */
1122 hModule = NE_LoadExeHeader( filename );
1123 if ( hModule < 32 )
1125 SetLastError( hModule );
1126 goto error;
1129 if ( !( pModule = NE_GetPtr( hModule ) )
1130 || ( pModule->flags & NE_FFLAGS_LIBMODULE) )
1132 GlobalFreeAll16( hModule );
1133 SetLastError( ERROR_BAD_FORMAT );
1134 goto error;
1138 SYSLEVEL_LeaveWin16Lock();
1140 if ( !PROCESS_Create( pModule, hFile, cmd_line, env,
1141 psa, tsa, inherit, flags, startup, info ) )
1142 return FALSE;
1144 return TRUE;
1146 error:
1147 SYSLEVEL_LeaveWin16Lock();
1148 return FALSE;
1151 /**********************************************************************
1152 * NE_InitProcess
1154 BOOL NE_InitProcess( NE_MODULE *pModule )
1156 HINSTANCE16 hInstance, hPrevInstance;
1157 BOOL retv = TRUE;
1159 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1160 WORD sp;
1161 TDB *pTask;
1163 SYSLEVEL_EnterWin16Lock();
1165 if ( pModule->count > 0 )
1167 /* Second instance of an already loaded NE module */
1168 /* Note that the refcount was already incremented by the parent */
1170 hPrevInstance = NE_GetInstance( pModule );
1172 if ( pModule->dgroup )
1173 if ( NE_CreateSegment( pModule, pModule->dgroup ) )
1174 NE_LoadSegment( pModule, pModule->dgroup );
1176 hInstance = NE_GetInstance( pModule );
1177 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->dgroup, hPrevInstance);
1180 else
1182 /* Load first instance of NE module */
1184 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1186 hInstance = NE_DoLoadModule( pModule );
1187 hPrevInstance = 0;
1190 if ( hInstance < 32 )
1192 SYSLEVEL_LeaveWin16Lock();
1194 SetLastError( hInstance );
1195 return FALSE;
1198 /* Enter instance handles into task struct */
1200 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1201 pTask->hInstance = hInstance;
1202 pTask->hPrevInstance = hPrevInstance;
1204 /* Use DGROUP for 16-bit stack */
1206 if (!(sp = pModule->sp))
1207 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
1208 sp &= ~1; sp -= sizeof(STACK16FRAME);
1209 pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(hInstance), sp );
1211 SYSLEVEL_LeaveWin16Lock();
1213 return retv;
1216 /***********************************************************************
1217 * LoadLibrary16 (KERNEL.95)
1219 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1221 return LoadModule16(libname, (LPVOID)-1 );
1225 /**********************************************************************
1226 * MODULE_CallWEP
1228 * Call a DLL's WEP, allowing it to shut down.
1229 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1231 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1233 FARPROC16 WEP = (FARPROC16)0;
1234 WORD ordinal = NE_GetOrdinal( hModule, "WEP" );
1236 if (ordinal) WEP = NE_GetEntryPoint( hModule, ordinal );
1237 if (!WEP)
1239 WARN("module %04x doesn't have a WEP\n", hModule );
1240 return FALSE;
1242 return Callbacks->CallWindowsExitProc( WEP, WEP_FREE_DLL );
1246 /**********************************************************************
1247 * NE_FreeModule
1249 * Implementation of FreeModule16().
1251 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1253 HMODULE16 *hPrevModule;
1254 NE_MODULE *pModule;
1255 HMODULE16 *pModRef;
1256 int i;
1258 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1259 hModule = pModule->self;
1261 TRACE("%04x count %d\n", hModule, pModule->count );
1263 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1264 else pModule->count = 0;
1266 if (pModule->flags & NE_FFLAGS_BUILTIN)
1267 return FALSE; /* Can't free built-in module */
1269 if (call_wep && !(pModule->flags & NE_FFLAGS_WIN32))
1271 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1273 MODULE_CallWEP( hModule );
1275 /* Free the objects owned by the DLL module */
1276 TASK_CallTaskSignalProc( USIG16_DLL_UNLOAD, hModule );
1277 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16, hModule );
1279 else
1280 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1284 /* Clear magic number just in case */
1286 pModule->magic = pModule->self = 0;
1288 /* Remove it from the linked list */
1290 hPrevModule = &hFirstModule;
1291 while (*hPrevModule && (*hPrevModule != hModule))
1293 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1295 if (*hPrevModule) *hPrevModule = pModule->next;
1297 /* Free the referenced modules */
1299 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1300 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1302 NE_FreeModule( *pModRef, call_wep );
1305 /* Free the module storage */
1307 GlobalFreeAll16( hModule );
1309 /* Remove module from cache */
1311 if (pCachedModule == pModule) pCachedModule = NULL;
1312 return TRUE;
1316 /**********************************************************************
1317 * FreeModule16 (KERNEL.46)
1319 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1321 return NE_FreeModule( hModule, TRUE );
1325 /***********************************************************************
1326 * FreeLibrary16 (KERNEL.96)
1328 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1330 TRACE("%04x\n", handle );
1331 FreeModule16( handle );
1335 /**********************************************************************
1336 * GetModuleName (KERNEL.27)
1338 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1340 NE_MODULE *pModule;
1341 BYTE *p;
1343 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1344 p = (BYTE *)pModule + pModule->name_table;
1345 if (count > *p) count = *p + 1;
1346 if (count > 0)
1348 memcpy( buf, p + 1, count - 1 );
1349 buf[count-1] = '\0';
1351 return TRUE;
1355 /**********************************************************************
1356 * GetModuleUsage (KERNEL.48)
1358 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1360 NE_MODULE *pModule = NE_GetPtr( hModule );
1361 return pModule ? pModule->count : 0;
1365 /**********************************************************************
1366 * GetExpWinVer (KERNEL.167)
1368 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1370 NE_MODULE *pModule = NE_GetPtr( hModule );
1371 if ( !pModule ) return 0;
1374 * For built-in modules, fake the expected version the module should
1375 * have according to the Windows version emulated by Wine
1377 if ( !pModule->expected_version )
1379 OSVERSIONINFOA versionInfo;
1380 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1382 if ( GetVersionExA( &versionInfo ) )
1383 pModule->expected_version =
1384 (versionInfo.dwMajorVersion & 0xff) << 8
1385 | (versionInfo.dwMinorVersion & 0xff);
1388 return pModule->expected_version;
1392 /**********************************************************************
1393 * GetModuleFileName16 (KERNEL.49)
1395 * Comment: see GetModuleFileNameA
1397 * Even if invoked by second instance of a program,
1398 * it still returns path of first one.
1400 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1401 INT16 nSize )
1403 NE_MODULE *pModule;
1405 /* Win95 does not query hModule if set to 0 !
1406 * Is this wrong or maybe Win3.1 only ? */
1407 if (!hModule) hModule = GetCurrentTask();
1409 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1410 if (pModule->expected_version >= 0x400)
1411 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1412 else
1413 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1414 TRACE("%s\n", lpFileName );
1415 return strlen(lpFileName);
1419 /**********************************************************************
1420 * GetModuleHandle (KERNEL.47)
1422 * Find a module from a module name.
1424 * NOTE: The current implementation works the same way the Windows 95 one
1425 * does. Do not try to 'fix' it, fix the callers.
1426 * + It does not do ANY extension handling (except that strange .EXE bit)!
1427 * + It does not care about paths, just about basenames. (same as Windows)
1429 * RETURNS
1430 * LOWORD:
1431 * the win16 module handle if found
1432 * 0 if not
1433 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1434 * Always hFirstModule
1436 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1438 if (HIWORD(name) == 0)
1439 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1440 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name)), hFirstModule );
1443 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1445 HMODULE16 hModule = hFirstModule;
1446 LPSTR s;
1447 BYTE len, *name_table;
1448 char tmpstr[MAX_PATH];
1449 NE_MODULE *pModule;
1451 TRACE("(%s)\n", name);
1453 if (!HIWORD(name))
1454 return GetExePtr(LOWORD(name));
1456 len = strlen(name);
1457 if (!len)
1458 return 0;
1460 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1462 /* If 'name' matches exactly the module name of a module:
1463 * Return its handle.
1465 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1467 pModule = NE_GetPtr( hModule );
1468 if (!pModule) break;
1469 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1471 name_table = (BYTE *)pModule + pModule->name_table;
1472 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1473 return hModule;
1476 /* If uppercased 'name' matches exactly the module name of a module:
1477 * Return its handle
1479 for (s = tmpstr; *s; s++)
1480 *s = toupper(*s);
1482 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1484 pModule = NE_GetPtr( hModule );
1485 if (!pModule) break;
1486 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1488 name_table = (BYTE *)pModule + pModule->name_table;
1489 /* FIXME: the lstrncmpiA is WRONG. It should not be case insensitive,
1490 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1491 * lowercased module names, but try to load uppercase DLLs, so this
1492 * 'i' compare is just a quickfix until the loader handles that
1493 * correctly. -MM 990705
1495 if ((*name_table == len) && !lstrncmpiA(tmpstr, name_table+1, len))
1496 return hModule;
1499 /* If the base filename of 'name' matches the base filename of the module
1500 * filename of some module (case-insensitive compare):
1501 * Return its handle.
1504 /* basename: search backwards in passed name to \ / or : */
1505 s = tmpstr + strlen(tmpstr);
1506 while (s > tmpstr)
1508 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1509 break;
1510 s--;
1513 /* search this in loaded filename list */
1514 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1516 char *loadedfn;
1517 OFSTRUCT *ofs;
1519 pModule = NE_GetPtr( hModule );
1520 if (!pModule) break;
1521 if (!pModule->fileinfo) continue;
1522 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1524 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1525 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1526 /* basename: search backwards in pathname to \ / or : */
1527 while (loadedfn > (char*)ofs->szPathName)
1529 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1530 break;
1531 loadedfn--;
1533 /* case insensitive compare ... */
1534 if (!lstrcmpiA(loadedfn, s))
1535 return hModule;
1538 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1539 * matches the base filename of the module filename of some 32-bit module:
1540 * Return the corresponding 16-bit dummy module handle.
1542 if (len >= 4 && !strcasecmp(name+len-4, ".EXE"))
1544 HMODULE hModule = GetModuleHandleA( name );
1545 if ( hModule )
1546 return MapHModuleLS( hModule );
1549 if (!strcmp(tmpstr,"MSDOS"))
1550 return 1;
1552 if (!strcmp(tmpstr,"TIMER"))
1554 FIXME("Eh... Should return caller's code segment, expect crash\n");
1555 return 0;
1558 return 0;
1561 /**********************************************************************
1562 * NE_GetModuleByFilename
1564 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1566 HMODULE16 hModule;
1567 LPSTR s, p;
1568 BYTE len, *name_table;
1569 char tmpstr[MAX_PATH];
1570 NE_MODULE *pModule;
1572 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1574 /* If the base filename of 'name' matches the base filename of the module
1575 * filename of some module (case-insensitive compare):
1576 * Return its handle.
1579 /* basename: search backwards in passed name to \ / or : */
1580 s = tmpstr + strlen(tmpstr);
1581 while (s > tmpstr)
1583 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1584 break;
1585 s--;
1588 /* search this in loaded filename list */
1589 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1591 char *loadedfn;
1592 OFSTRUCT *ofs;
1594 pModule = NE_GetPtr( hModule );
1595 if (!pModule) break;
1596 if (!pModule->fileinfo) continue;
1597 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1599 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1600 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1601 /* basename: search backwards in pathname to \ / or : */
1602 while (loadedfn > (char*)ofs->szPathName)
1604 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1605 break;
1606 loadedfn--;
1608 /* case insensitive compare ... */
1609 if (!lstrcmpiA(loadedfn, s))
1610 return hModule;
1613 /* If basename (without ext) matches the module name of a module:
1614 * Return its handle.
1617 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1618 len = strlen(s);
1620 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1622 pModule = NE_GetPtr( hModule );
1623 if (!pModule) break;
1624 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1626 name_table = (BYTE *)pModule + pModule->name_table;
1627 if ((*name_table == len) && !lstrncmpiA(s, name_table+1, len))
1628 return hModule;
1631 return 0;
1634 /**********************************************************************
1635 * ModuleFirst (TOOLHELP.59)
1637 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1639 lpme->wNext = hFirstModule;
1640 return ModuleNext16( lpme );
1644 /**********************************************************************
1645 * ModuleNext (TOOLHELP.60)
1647 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1649 NE_MODULE *pModule;
1650 char *name;
1652 if (!lpme->wNext) return FALSE;
1653 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1654 name = (char *)pModule + pModule->name_table;
1655 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1656 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1657 lpme->hModule = lpme->wNext;
1658 lpme->wcUsage = pModule->count;
1659 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1660 lpme->wNext = pModule->next;
1661 return TRUE;
1665 /**********************************************************************
1666 * ModuleFindName (TOOLHELP.61)
1668 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1670 lpme->wNext = GetModuleHandle16( name );
1671 return ModuleNext16( lpme );
1675 /**********************************************************************
1676 * ModuleFindHandle (TOOLHELP.62)
1678 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1680 hModule = GetExePtr( hModule );
1681 lpme->wNext = hModule;
1682 return ModuleNext16( lpme );
1686 /***************************************************************************
1687 * IsRomModule16 (KERNEL.323)
1689 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1691 return FALSE;
1694 /***************************************************************************
1695 * IsRomFile16 (KERNEL.326)
1697 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1699 return FALSE;
1702 /***************************************************************************
1703 * MapHModuleLS (KERNEL32.520)
1705 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod) {
1706 NE_MODULE *pModule;
1708 if (!hmod)
1709 return ((TDB*)GlobalLock16(GetCurrentTask()))->hInstance;
1710 if (!HIWORD(hmod))
1711 return hmod; /* we already have a 16 bit module handle */
1712 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1713 while (pModule) {
1714 if (pModule->module32 == hmod)
1715 return pModule->self;
1716 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1718 return 0;
1721 /***************************************************************************
1722 * MapHModuleSL (KERNEL32.521)
1724 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1725 NE_MODULE *pModule;
1727 if (!hmod) {
1728 TDB *pTask = (TDB*)GlobalLock16(GetCurrentTask());
1730 hmod = pTask->hModule;
1732 pModule = (NE_MODULE*)GlobalLock16(hmod);
1733 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1734 !(pModule->flags & NE_FFLAGS_WIN32)
1736 return 0;
1737 return pModule->module32;
1740 /***************************************************************************
1741 * MapHInstLS (KERNEL32.516)(KERNEL.472)
1743 void WINAPI MapHInstLS( CONTEXT86 *context )
1745 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1748 /***************************************************************************
1749 * MapHInstSL (KERNEL32.518)(KERNEL.473)
1751 void WINAPI MapHInstSL( CONTEXT86 *context )
1753 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1756 /***************************************************************************
1757 * MapHInstLS_PN (KERNEL32.517)
1759 void WINAPI MapHInstLS_PN( CONTEXT86 *context )
1761 if (EAX_reg(context))
1762 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1765 /***************************************************************************
1766 * MapHInstSL_PN (KERNEL32.519)
1768 void WINAPI MapHInstSL_PN( CONTEXT86 *context )
1770 if (EAX_reg(context))
1771 EAX_reg(context) = MapHModuleSL(EAX_reg(context));