Got rid of THREAD_InitDone.
[wine/multimedia.git] / loader / ne / module.c
blobf715efad27d234d7b561922ef1f79254264a9ecf
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 "peexe.h"
18 #include "toolhelp.h"
19 #include "file.h"
20 #include "ldt.h"
21 #include "callback.h"
22 #include "heap.h"
23 #include "task.h"
24 #include "global.h"
25 #include "process.h"
26 #include "toolhelp.h"
27 #include "snoop.h"
28 #include "stackframe.h"
29 #include "debug.h"
30 #include "file.h"
31 #include "loadorder.h"
32 #include "elfdll.h"
34 DEFAULT_DEBUG_CHANNEL(module)
36 FARPROC16 (*fnSNOOP16_GetProcAddress16)(HMODULE16,DWORD,FARPROC16) = NULL;
37 void (*fnSNOOP16_RegisterDLL)(NE_MODULE*,LPCSTR) = NULL;
39 #define hFirstModule (pThhook->hExeHead)
41 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
43 static HMODULE16 NE_LoadBuiltin(LPCSTR name,BOOL force) { return 0; }
44 HMODULE16 (*fnBUILTIN_LoadModule)(LPCSTR name,BOOL force) = NE_LoadBuiltin;
47 /***********************************************************************
48 * NE_GetPtr
50 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
52 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
56 /***********************************************************************
57 * NE_DumpModule
59 void NE_DumpModule( HMODULE16 hModule )
61 int i, ordinal;
62 SEGTABLEENTRY *pSeg;
63 BYTE *pstr;
64 WORD *pword;
65 NE_MODULE *pModule;
66 ET_BUNDLE *bundle;
67 ET_ENTRY *entry;
69 if (!(pModule = NE_GetPtr( hModule )))
71 MSG( "**** %04x is not a module handle\n", hModule );
72 return;
75 /* Dump the module info */
76 DUMP( "---\n" );
77 DUMP( "Module %04x:\n", hModule );
78 DUMP( "count=%d flags=%04x heap=%d stack=%d\n",
79 pModule->count, pModule->flags,
80 pModule->heap_size, pModule->stack_size );
81 DUMP( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
82 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
83 pModule->seg_count, pModule->modref_count );
84 DUMP( "os_flags=%d swap_area=%d version=%04x\n",
85 pModule->os_flags, pModule->min_swap_area,
86 pModule->expected_version );
87 if (pModule->flags & NE_FFLAGS_WIN32)
88 DUMP( "PE module=%08x\n", pModule->module32 );
90 /* Dump the file info */
91 DUMP( "---\n" );
92 DUMP( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
94 /* Dump the segment table */
95 DUMP( "---\n" );
96 DUMP( "Segment table:\n" );
97 pSeg = NE_SEG_TABLE( pModule );
98 for (i = 0; i < pModule->seg_count; i++, pSeg++)
99 DUMP( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
100 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
101 pSeg->minsize, pSeg->hSeg );
103 /* Dump the resource table */
104 DUMP( "---\n" );
105 DUMP( "Resource table:\n" );
106 if (pModule->res_table)
108 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
109 DUMP( "Alignment: %d\n", *pword++ );
110 while (*pword)
112 struct resource_typeinfo_s *ptr = (struct resource_typeinfo_s *)pword;
113 struct resource_nameinfo_s *pname = (struct resource_nameinfo_s *)(ptr + 1);
114 DUMP( "id=%04x count=%d\n", ptr->type_id, ptr->count );
115 for (i = 0; i < ptr->count; i++, pname++)
116 DUMP( "offset=%d len=%d id=%04x\n",
117 pname->offset, pname->length, pname->id );
118 pword = (WORD *)pname;
121 else DUMP( "None\n" );
123 /* Dump the resident name table */
124 DUMP( "---\n" );
125 DUMP( "Resident-name table:\n" );
126 pstr = (char *)pModule + pModule->name_table;
127 while (*pstr)
129 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
130 *(WORD *)(pstr + *pstr + 1) );
131 pstr += *pstr + 1 + sizeof(WORD);
134 /* Dump the module reference table */
135 DUMP( "---\n" );
136 DUMP( "Module ref table:\n" );
137 if (pModule->modref_table)
139 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
140 for (i = 0; i < pModule->modref_count; i++, pword++)
142 char name[10];
143 GetModuleName16( *pword, name, sizeof(name) );
144 DUMP( "%d: %04x -> '%s'\n", i, *pword, name );
147 else DUMP( "None\n" );
149 /* Dump the entry table */
150 DUMP( "---\n" );
151 DUMP( "Entry table:\n" );
152 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
153 do {
154 entry = (ET_ENTRY *)((BYTE *)bundle+6);
155 DUMP( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
156 ordinal = bundle->first;
157 while (ordinal < bundle->last)
159 if (entry->type == 0xff)
160 DUMP("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
161 else
162 DUMP("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
163 entry++;
165 } while ( (bundle->next)
166 && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
168 /* Dump the non-resident names table */
169 DUMP( "---\n" );
170 DUMP( "Non-resident names table:\n" );
171 if (pModule->nrname_handle)
173 pstr = (char *)GlobalLock16( pModule->nrname_handle );
174 while (*pstr)
176 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
177 *(WORD *)(pstr + *pstr + 1) );
178 pstr += *pstr + 1 + sizeof(WORD);
181 DUMP( "\n" );
185 /***********************************************************************
186 * NE_WalkModules
188 * Walk the module list and print the modules.
190 void NE_WalkModules(void)
192 HMODULE16 hModule = hFirstModule;
193 MSG( "Module Flags Name\n" );
194 while (hModule)
196 NE_MODULE *pModule = NE_GetPtr( hModule );
197 if (!pModule)
199 MSG( "Bad module %04x in list\n", hModule );
200 return;
202 MSG( " %04x %04x %.*s\n", hModule, pModule->flags,
203 *((char *)pModule + pModule->name_table),
204 (char *)pModule + pModule->name_table + 1 );
205 hModule = pModule->next;
210 /**********************************************************************
211 * NE_RegisterModule
213 void NE_RegisterModule( NE_MODULE *pModule )
215 pModule->next = hFirstModule;
216 hFirstModule = pModule->self;
220 /***********************************************************************
221 * NE_GetOrdinal
223 * Lookup the ordinal for a given name.
225 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
227 unsigned char buffer[256], *cpnt;
228 BYTE len;
229 NE_MODULE *pModule;
231 if (!(pModule = NE_GetPtr( hModule ))) return 0;
232 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
234 TRACE( module, "(%04x,'%s')\n", hModule, name );
236 /* First handle names of the form '#xxxx' */
238 if (name[0] == '#') return atoi( name + 1 );
240 /* Now copy and uppercase the string */
242 strcpy( buffer, name );
243 CharUpperA( buffer );
244 len = strlen( buffer );
246 /* First search the resident names */
248 cpnt = (char *)pModule + pModule->name_table;
250 /* Skip the first entry (module name) */
251 cpnt += *cpnt + 1 + sizeof(WORD);
252 while (*cpnt)
254 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
256 TRACE(module, " Found: ordinal=%d\n",
257 *(WORD *)(cpnt + *cpnt + 1) );
258 return *(WORD *)(cpnt + *cpnt + 1);
260 cpnt += *cpnt + 1 + sizeof(WORD);
263 /* Now search the non-resident names table */
265 if (!pModule->nrname_handle) return 0; /* No non-resident table */
266 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
268 /* Skip the first entry (module description string) */
269 cpnt += *cpnt + 1 + sizeof(WORD);
270 while (*cpnt)
272 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
274 TRACE(module, " Found: ordinal=%d\n",
275 *(WORD *)(cpnt + *cpnt + 1) );
276 return *(WORD *)(cpnt + *cpnt + 1);
278 cpnt += *cpnt + 1 + sizeof(WORD);
280 return 0;
284 /***********************************************************************
285 * NE_GetEntryPoint (WPROCS.27)
287 * Return the entry point for a given ordinal.
289 FARPROC16 NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
291 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
293 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
295 NE_MODULE *pModule;
296 WORD sel, offset, i;
298 ET_ENTRY *entry;
299 ET_BUNDLE *bundle;
301 if (!(pModule = NE_GetPtr( hModule ))) return 0;
302 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
304 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
305 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
307 if (!(bundle->next))
308 return 0;
309 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
312 entry = (ET_ENTRY *)((BYTE *)bundle+6);
313 for (i=0; i < (ordinal - bundle->first - 1); i++)
314 entry++;
316 sel = entry->segnum;
317 offset = entry->offs;
319 if (sel == 0xfe) sel = 0xffff; /* constant entry */
320 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
321 if (sel==0xffff)
322 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
323 if (!snoop || !fnSNOOP16_GetProcAddress16)
324 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
325 else
326 return (FARPROC16)fnSNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset ));
330 /***********************************************************************
331 * NE_SetEntryPoint
333 * Change the value of an entry point. Use with caution!
334 * It can only change the offset value, not the selector.
336 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
338 NE_MODULE *pModule;
339 ET_ENTRY *entry;
340 ET_BUNDLE *bundle;
341 int i;
343 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
344 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
346 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
347 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
349 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
350 if (!(bundle->next))
351 return 0;
354 entry = (ET_ENTRY *)((BYTE *)bundle+6);
355 for (i=0; i < (ordinal - bundle->first - 1); i++)
356 entry++;
358 entry->offs = offset;
359 return TRUE;
363 /***********************************************************************
364 * NE_OpenFile
366 HANDLE NE_OpenFile( NE_MODULE *pModule )
368 char *name;
370 static HANDLE cachedfd = -1;
372 TRACE( module, "(%p) cache: mod=%p fd=%d\n",
373 pModule, pCachedModule, cachedfd );
374 if (pCachedModule == pModule) return cachedfd;
375 CloseHandle( cachedfd );
376 pCachedModule = pModule;
377 name = NE_MODULE_NAME( pModule );
378 if ((cachedfd = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
379 NULL, OPEN_EXISTING, 0, -1 )) == -1)
380 MSG( "Can't open file '%s' for module %04x\n", name, pModule->self );
381 else
382 /* FIXME: should not be necessary */
383 cachedfd = ConvertToGlobalHandle(cachedfd);
384 TRACE(module, "opened '%s' -> %d\n",
385 name, cachedfd );
386 return cachedfd;
390 /***********************************************************************
391 * NE_LoadExeHeader
393 static HMODULE16 NE_LoadExeHeader( HFILE16 hFile, OFSTRUCT *ofs )
395 IMAGE_DOS_HEADER mz_header;
396 IMAGE_OS2_HEADER ne_header;
397 int size;
398 HMODULE16 hModule;
399 NE_MODULE *pModule;
400 BYTE *pData, *pTempEntryTable;
401 char *buffer, *fastload = NULL;
402 int fastload_offset = 0, fastload_length = 0;
403 ET_ENTRY *entry;
404 ET_BUNDLE *bundle, *oldbundle;
406 /* Read a block from either the file or the fast-load area. */
407 #define READ(offset,size,buffer) \
408 ((fastload && ((offset) >= fastload_offset) && \
409 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
410 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
411 (_llseek16( hFile, (offset), SEEK_SET), \
412 _hread16( hFile, (buffer), (size) ) == (size)))
414 _llseek16( hFile, 0, SEEK_SET );
415 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
416 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
417 return (HMODULE16)11; /* invalid exe */
419 _llseek16( hFile, mz_header.e_lfanew, SEEK_SET );
420 if (_hread16( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
421 return (HMODULE16)11; /* invalid exe */
423 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
424 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
426 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
427 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
428 return (HMODULE16)12;
431 /* We now have a valid NE header */
433 size = sizeof(NE_MODULE) +
434 /* segment table */
435 ne_header.n_segment_tab * sizeof(SEGTABLEENTRY) +
436 /* resource table */
437 ne_header.rname_tab_offset - ne_header.resource_tab_offset +
438 /* resident names table */
439 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset +
440 /* module ref table */
441 ne_header.n_mod_ref_tab * sizeof(WORD) +
442 /* imported names table */
443 ne_header.entry_tab_offset - ne_header.iname_tab_offset +
444 /* entry table length */
445 ne_header.entry_tab_length +
446 /* entry table extra conversion space */
447 sizeof(ET_BUNDLE) +
448 2 * (ne_header.entry_tab_length - ne_header.n_mov_entry_points*6) +
449 /* loaded file info */
450 sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
452 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
453 if (!hModule) return (HMODULE16)11; /* invalid exe */
454 FarSetOwner16( hModule, hModule );
455 pModule = (NE_MODULE *)GlobalLock16( hModule );
456 memcpy( pModule, &ne_header, sizeof(ne_header) );
457 pModule->count = 0;
458 /* check *programs* for default minimal stack size */
459 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
460 && (pModule->stack_size < 0x1400) )
461 pModule->stack_size = 0x1400;
462 pModule->module32 = 0;
463 pModule->self = hModule;
464 pModule->self_loading_sel = 0;
465 pData = (BYTE *)(pModule + 1);
467 /* Clear internal Wine flags in case they are set in the EXE file */
469 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
471 /* Read the fast-load area */
473 if (ne_header.additional_flags & NE_AFLAGS_FASTLOAD)
475 fastload_offset=ne_header.fastload_offset<<ne_header.align_shift_count;
476 fastload_length=ne_header.fastload_length<<ne_header.align_shift_count;
477 TRACE(module, "Using fast-load area offset=%x len=%d\n",
478 fastload_offset, fastload_length );
479 if ((fastload = HeapAlloc( SystemHeap, 0, fastload_length )) != NULL)
481 _llseek16( hFile, fastload_offset, SEEK_SET);
482 if (_hread16(hFile, fastload, fastload_length) != fastload_length)
484 HeapFree( SystemHeap, 0, fastload );
485 WARN( module, "Error reading fast-load area!\n");
486 fastload = NULL;
491 /* Get the segment table */
493 pModule->seg_table = (int)pData - (int)pModule;
494 buffer = HeapAlloc( SystemHeap, 0, ne_header.n_segment_tab *
495 sizeof(struct ne_segment_table_entry_s));
496 if (buffer)
498 int i;
499 struct ne_segment_table_entry_s *pSeg;
501 if (!READ( mz_header.e_lfanew + ne_header.segment_tab_offset,
502 ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s),
503 buffer ))
505 HeapFree( SystemHeap, 0, buffer );
506 if (fastload)
507 HeapFree( SystemHeap, 0, fastload );
508 GlobalFree16( hModule );
509 return (HMODULE16)11; /* invalid exe */
511 pSeg = (struct ne_segment_table_entry_s *)buffer;
512 for (i = ne_header.n_segment_tab; i > 0; i--, pSeg++)
514 memcpy( pData, pSeg, sizeof(*pSeg) );
515 pData += sizeof(SEGTABLEENTRY);
517 HeapFree( SystemHeap, 0, buffer );
519 else
521 if (fastload)
522 HeapFree( SystemHeap, 0, fastload );
523 GlobalFree16( hModule );
524 return (HMODULE16)11; /* invalid exe */
527 /* Get the resource table */
529 if (ne_header.resource_tab_offset < ne_header.rname_tab_offset)
531 pModule->res_table = (int)pData - (int)pModule;
532 if (!READ(mz_header.e_lfanew + ne_header.resource_tab_offset,
533 ne_header.rname_tab_offset - ne_header.resource_tab_offset,
534 pData )) return (HMODULE16)11; /* invalid exe */
535 pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
536 NE_InitResourceHandler( hModule );
538 else pModule->res_table = 0; /* No resource table */
540 /* Get the resident names table */
542 pModule->name_table = (int)pData - (int)pModule;
543 if (!READ( mz_header.e_lfanew + ne_header.rname_tab_offset,
544 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset,
545 pData ))
547 if (fastload)
548 HeapFree( SystemHeap, 0, fastload );
549 GlobalFree16( hModule );
550 return (HMODULE16)11; /* invalid exe */
552 pData += ne_header.moduleref_tab_offset - ne_header.rname_tab_offset;
554 /* Get the module references table */
556 if (ne_header.n_mod_ref_tab > 0)
558 pModule->modref_table = (int)pData - (int)pModule;
559 if (!READ( mz_header.e_lfanew + ne_header.moduleref_tab_offset,
560 ne_header.n_mod_ref_tab * sizeof(WORD),
561 pData ))
563 if (fastload)
564 HeapFree( SystemHeap, 0, fastload );
565 GlobalFree16( hModule );
566 return (HMODULE16)11; /* invalid exe */
568 pData += ne_header.n_mod_ref_tab * sizeof(WORD);
570 else pModule->modref_table = 0; /* No module references */
572 /* Get the imported names table */
574 pModule->import_table = (int)pData - (int)pModule;
575 if (!READ( mz_header.e_lfanew + ne_header.iname_tab_offset,
576 ne_header.entry_tab_offset - ne_header.iname_tab_offset,
577 pData ))
579 if (fastload)
580 HeapFree( SystemHeap, 0, fastload );
581 GlobalFree16( hModule );
582 return (HMODULE16)11; /* invalid exe */
584 pData += ne_header.entry_tab_offset - ne_header.iname_tab_offset;
586 /* Load entry table, convert it to the optimized version used by Windows */
588 if ((pTempEntryTable = HeapAlloc( SystemHeap, 0, ne_header.entry_tab_length)) != NULL)
590 BYTE nr_entries, type, *s;
592 TRACE(module, "Converting entry table.\n");
593 pModule->entry_table = (int)pData - (int)pModule;
594 if (!READ( mz_header.e_lfanew + ne_header.entry_tab_offset,
595 ne_header.entry_tab_length, pTempEntryTable ))
597 HeapFree( SystemHeap, 0, pTempEntryTable );
598 if (fastload)
599 HeapFree( SystemHeap, 0, fastload );
600 GlobalFree16( hModule );
601 return (HMODULE16)11; /* invalid exe */
604 s = pTempEntryTable;
605 TRACE(module, "entry table: offs %04x, len %04x, entries %d\n", ne_header.entry_tab_offset, ne_header.entry_tab_length, *s);
607 bundle = (ET_BUNDLE *)pData;
608 TRACE(module, "first bundle: %p\n", bundle);
609 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
610 entry = (ET_ENTRY *)((BYTE *)bundle+6);
612 while ((nr_entries = *s++))
614 if ((type = *s++))
616 bundle->last += nr_entries;
617 if (type == 0xff)
618 while (nr_entries--)
620 entry->type = type;
621 entry->flags = *s++;
622 s += 2;
623 entry->segnum = *s++;
624 entry->offs = *(WORD *)s; s += 2;
625 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
626 entry++;
628 else
629 while (nr_entries--)
631 entry->type = type;
632 entry->flags = *s++;
633 entry->segnum = type;
634 entry->offs = *(WORD *)s; s += 2;
635 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
636 entry++;
639 else
641 if (bundle->first == bundle->last)
643 bundle->first += nr_entries;
644 bundle->last += nr_entries;
646 else
648 oldbundle = bundle;
649 oldbundle->next = ((int)entry - (int)pModule);
650 bundle = (ET_BUNDLE *)entry;
651 TRACE(module, "new bundle: %p\n", bundle);
652 bundle->first = bundle->last =
653 oldbundle->last + nr_entries;
654 bundle->next = 0;
655 (BYTE *)entry += sizeof(ET_BUNDLE);
659 HeapFree( SystemHeap, 0, pTempEntryTable );
661 else
663 if (fastload)
664 HeapFree( SystemHeap, 0, fastload );
665 GlobalFree16( hModule );
666 return (HMODULE16)11; /* invalid exe */
669 pData += ne_header.entry_tab_length + sizeof(ET_BUNDLE) +
670 2 * (ne_header.entry_tab_length - ne_header.n_mov_entry_points*6);
672 if ((DWORD)entry > (DWORD)pData)
673 ERR(module, "converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
675 /* Store the filename information */
677 pModule->fileinfo = (int)pData - (int)pModule;
678 size = sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
679 memcpy( pData, ofs, size );
680 ((OFSTRUCT *)pData)->cBytes = size - 1;
681 pData += size;
683 /* Free the fast-load area */
685 #undef READ
686 if (fastload)
687 HeapFree( SystemHeap, 0, fastload );
689 /* Get the non-resident names table */
691 if (ne_header.nrname_tab_length)
693 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.nrname_tab_length,
694 hModule, FALSE, FALSE, FALSE );
695 if (!pModule->nrname_handle)
697 GlobalFree16( hModule );
698 return (HMODULE16)11; /* invalid exe */
700 buffer = GlobalLock16( pModule->nrname_handle );
701 _llseek16( hFile, ne_header.nrname_tab_offset, SEEK_SET );
702 if (_hread16( hFile, buffer, ne_header.nrname_tab_length )
703 != ne_header.nrname_tab_length)
705 GlobalFree16( pModule->nrname_handle );
706 GlobalFree16( hModule );
707 return (HMODULE16)11; /* invalid exe */
710 else pModule->nrname_handle = 0;
712 /* Allocate a segment for the implicitly-loaded DLLs */
714 if (pModule->modref_count)
716 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
717 (pModule->modref_count+1)*sizeof(HMODULE16),
718 hModule, FALSE, FALSE, FALSE );
719 if (!pModule->dlls_to_init)
721 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
722 GlobalFree16( hModule );
723 return (HMODULE16)11; /* invalid exe */
726 else pModule->dlls_to_init = 0;
728 NE_RegisterModule( pModule );
729 if (fnSNOOP16_RegisterDLL)
730 fnSNOOP16_RegisterDLL(pModule,ofs->szPathName);
731 return hModule;
735 /***********************************************************************
736 * NE_LoadDLLs
738 * Load all DLLs implicitly linked to a module.
740 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
742 int i;
743 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
744 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
746 for (i = 0; i < pModule->modref_count; i++, pModRef++)
748 char buffer[260];
749 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
750 memcpy( buffer, pstr + 1, *pstr );
751 *(buffer + *pstr) = 0; /* terminate it */
753 TRACE(module, "Loading '%s'\n", buffer );
754 if (!(*pModRef = GetModuleHandle16( buffer )))
756 /* If the DLL is not loaded yet, load it and store */
757 /* its handle in the list of DLLs to initialize. */
758 HMODULE16 hDLL;
760 if ((hDLL = MODULE_LoadModule16( buffer, TRUE )) < 32)
762 /* FIXME: cleanup what was done */
764 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
765 buffer, *((BYTE*)pModule + pModule->name_table),
766 (char *)pModule + pModule->name_table + 1, hDLL );
767 return FALSE;
769 *pModRef = GetExePtr( hDLL );
770 *pDLLs++ = *pModRef;
772 else /* Increment the reference count of the DLL */
774 NE_MODULE *pOldDLL;
776 pOldDLL = NE_GetPtr( *pModRef );
777 if (pOldDLL) pOldDLL->count++;
780 return TRUE;
784 /**********************************************************************
785 * NE_LoadFileModule
787 * Load first instance of NE module from file.
788 * (Note: caller is responsible for ensuring the module isn't
789 * already loaded!)
791 static HINSTANCE16 NE_LoadFileModule( HFILE16 hFile, OFSTRUCT *ofs,
792 BOOL implicit )
794 HINSTANCE16 hInstance;
795 HMODULE16 hModule;
796 NE_MODULE *pModule;
798 /* Create the module structure */
800 hModule = NE_LoadExeHeader( hFile, ofs );
801 if (hModule < 32) return hModule;
802 pModule = NE_GetPtr( hModule );
804 /* Allocate the segments for this module */
806 if (!NE_CreateSegments( pModule ) ||
807 !(hInstance = NE_CreateInstance( pModule, NULL, FALSE )))
809 GlobalFreeAll16( hModule );
810 return 8; /* Insufficient memory */
813 /* Load the referenced DLLs */
815 if (!NE_LoadDLLs( pModule ))
816 return 2; /* File not found (FIXME: free everything) */
818 /* Load the segments */
820 NE_LoadAllSegments( pModule );
822 /* Fixup the functions prologs */
824 NE_FixupPrologs( pModule );
826 /* Make sure the usage count is 1 on the first loading of */
827 /* the module, even if it contains circular DLL references */
829 pModule->count = 1;
831 return hInstance;
834 /**********************************************************************
835 * NE_LoadModule
837 * Load first instance of NE module, deciding whether to use
838 * built-in module or load module from file.
839 * (Note: caller is responsible for ensuring the module isn't
840 * already loaded!)
842 HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL implicit )
844 HINSTANCE16 hInstance;
845 HFILE16 hFile;
846 OFSTRUCT ofs;
848 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
850 char buffer[260];
852 if(implicit)
854 /* 4 == strlen(".dll") */
855 strncpy(buffer, name, sizeof(buffer) - 1 - 4);
856 strcat(buffer, ".dll");
857 if ((hFile = OpenFile16( buffer, &ofs, OF_READ )) == HFILE_ERROR16)
858 return 2; /* File not found */
862 hInstance = NE_LoadFileModule( hFile, &ofs, implicit );
863 _lclose16( hFile );
865 return hInstance;
869 /**********************************************************************
870 * MODULE_LoadModule16
872 * Load a NE module in the order of the loadorder specification.
873 * The caller is responsible that the module is not loaded already.
876 HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit )
878 HINSTANCE16 hinst;
879 int i;
880 module_loadorder_t *plo;
882 plo = MODULE_GetLoadOrder(libname);
884 for(i = 0; i < MODULE_LOADORDER_NTYPES; i++)
886 switch(plo->loadorder[i])
888 case MODULE_LOADORDER_DLL:
889 TRACE(module, "Trying native dll '%s'\n", libname);
890 hinst = NE_LoadModule(libname, implicit);
891 break;
893 case MODULE_LOADORDER_ELFDLL:
894 TRACE(module, "Trying elfdll '%s'\n", libname);
895 hinst = ELFDLL_LoadModule16(libname, implicit);
896 break;
898 case MODULE_LOADORDER_BI:
899 TRACE(module, "Trying built-in '%s'\n", libname);
900 hinst = fnBUILTIN_LoadModule(libname, TRUE);
901 break;
903 default:
904 ERR(module, "Got invalid loadorder type %d (%s index %d)\n", plo->loadorder[i], plo->modulename, i);
905 /* Fall through */
907 case MODULE_LOADORDER_SO: /* This is not supported for NE modules */
908 case MODULE_LOADORDER_INVALID: /* We ignore this as it is an empty entry */
909 hinst = 2;
910 break;
913 if(hinst >= 32)
915 if(!implicit)
917 HMODULE16 hModule;
918 NE_MODULE *pModule;
920 hModule = GetModuleHandle16(libname);
921 if(!hModule)
923 ERR(module, "Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle\n",
924 libname, hinst);
925 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
928 pModule = NE_GetPtr(hModule);
929 if(!pModule)
931 ERR(module, "Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
932 libname, hinst);
933 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
936 TRACE(module, "Loaded module '%s' at 0x%04x, \n", libname, hinst);
939 * Call initialization routines for all loaded DLLs. Note that
940 * when we load implicitly linked DLLs this will be done by InitTask().
942 if(pModule->flags & NE_FFLAGS_LIBMODULE)
943 NE_InitializeDLLs(hModule);
945 return hinst;
948 if(hinst != 2)
950 /* We quit searching when we get another error than 'File not found' */
951 break;
954 return hinst; /* The last error that occured */
958 /**********************************************************************
959 * LoadModule16 (KERNEL.45)
961 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
963 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
964 LOADPARAMS16 *params;
965 LPSTR cmd_line, new_cmd_line;
966 LPCVOID env = NULL;
967 STARTUPINFOA startup;
968 PROCESS_INFORMATION info;
969 HINSTANCE16 hInstance, hPrevInstance = 0;
970 HMODULE16 hModule;
971 NE_MODULE *pModule;
972 PDB *pdb;
974 /* Load module */
976 if ( (hModule = GetModuleHandle16(name) ) != 0 )
978 /* Special case: second instance of an already loaded NE module */
980 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
981 if ( pModule->module32 ) return (HINSTANCE16)21;
983 hInstance = NE_CreateInstance( pModule, &hPrevInstance, lib_only );
984 if ( hInstance != hPrevInstance ) /* not a library */
985 NE_LoadSegment( pModule, pModule->dgroup );
987 pModule->count++;
989 else
991 /* Main case: load first instance of NE module */
993 if ( (hInstance = MODULE_LoadModule16( name, FALSE )) < 32 )
994 return hInstance;
996 if ( !(pModule = NE_GetPtr( hInstance )) )
997 return (HINSTANCE16)11;
1000 /* If library module, we're finished */
1002 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1003 return hInstance;
1005 /* Create a task for this instance */
1007 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1009 params = (LOADPARAMS16 *)paramBlock;
1010 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
1011 if (!cmd_line) cmd_line = "";
1012 else if (*cmd_line) cmd_line++; /* skip the length byte */
1014 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1015 strlen(cmd_line)+strlen(name)+2 )))
1016 return 0;
1017 strcpy( new_cmd_line, name );
1018 strcat( new_cmd_line, " " );
1019 strcat( new_cmd_line, cmd_line );
1021 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
1023 memset( &info, '\0', sizeof(info) );
1024 memset( &startup, '\0', sizeof(startup) );
1025 startup.cb = sizeof(startup);
1026 if (params->showCmd)
1028 startup.dwFlags = STARTF_USESHOWWINDOW;
1029 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
1032 pdb = PROCESS_Create( pModule, new_cmd_line, env,
1033 hInstance, hPrevInstance,
1034 NULL, NULL, TRUE, 0, &startup, &info );
1036 CloseHandle( info.hThread );
1037 CloseHandle( info.hProcess );
1039 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
1040 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1041 return hInstance;
1044 /**********************************************************************
1045 * NE_CreateProcess
1047 BOOL NE_CreateProcess( HFILE hFile, OFSTRUCT *ofs, LPCSTR cmd_line, LPCSTR env,
1048 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1049 BOOL inherit, DWORD flags, LPSTARTUPINFOA startup,
1050 LPPROCESS_INFORMATION info )
1052 HINSTANCE16 hInstance, hPrevInstance = 0;
1053 HMODULE16 hModule;
1054 NE_MODULE *pModule;
1055 HFILE16 hFile16;
1057 /* Special case: second instance of an already loaded NE module */
1059 if ( ( hModule = GetModuleHandle16( ofs->szPathName ) ) != 0 )
1061 if ( !( pModule = NE_GetPtr( hModule) )
1062 || ( pModule->flags & NE_FFLAGS_LIBMODULE )
1063 || pModule->module32 )
1065 SetLastError( ERROR_BAD_FORMAT );
1066 return FALSE;
1069 hInstance = NE_CreateInstance( pModule, &hPrevInstance, FALSE );
1070 if ( hInstance != hPrevInstance ) /* not a library */
1071 NE_LoadSegment( pModule, pModule->dgroup );
1073 pModule->count++;
1076 /* Main case: load first instance of NE module */
1077 else
1079 /* If we didn't get a file handle, return */
1081 if ( hFile == HFILE_ERROR )
1082 return FALSE;
1084 /* Allocate temporary HFILE16 for NE_LoadFileModule */
1086 if (!DuplicateHandle( GetCurrentProcess(), hFile,
1087 GetCurrentProcess(), &hFile,
1088 0, FALSE, DUPLICATE_SAME_ACCESS ))
1090 SetLastError( ERROR_INVALID_HANDLE );
1091 return FALSE;
1093 hFile16 = FILE_AllocDosHandle( hFile );
1095 /* Load module */
1097 hInstance = NE_LoadFileModule( hFile16, ofs, TRUE );
1098 _lclose16( hFile16 );
1100 if ( hInstance < 32 )
1102 SetLastError( hInstance );
1103 return FALSE;
1106 if ( !( pModule = NE_GetPtr( hInstance ) )
1107 || ( pModule->flags & NE_FFLAGS_LIBMODULE) )
1109 /* FIXME: cleanup */
1110 SetLastError( ERROR_BAD_FORMAT );
1111 return FALSE;
1115 /* Create a task for this instance */
1117 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1119 if ( !PROCESS_Create( pModule, cmd_line, env,
1120 hInstance, hPrevInstance,
1121 psa, tsa, inherit, flags, startup, info ) )
1122 return FALSE;
1124 return TRUE;
1127 /***********************************************************************
1128 * LoadLibrary16 (KERNEL.95)
1130 * In Win95 LoadLibrary16("c:/junkname/user.foo") returns the HINSTANCE
1131 * to user.exe. As GetModuleHandle as of 990425 explicitly asks _not_
1132 * to change its handling of extensions, we have to try a stripped down
1133 * libname here too (bon 990425)
1135 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1137 char strippedname[256];
1138 char *dirsep1,*dirsep2;
1139 HINSTANCE16 ret;
1141 dirsep1=strrchr(libname,'\\');
1142 dirsep2=strrchr(libname,'/');
1143 dirsep1=MAX(dirsep1,dirsep2);
1144 if (!dirsep1)
1145 dirsep1 =(LPSTR)libname;
1146 else
1147 dirsep1++;
1148 lstrcpynA(strippedname,dirsep1,256);
1149 dirsep1=strchr(strippedname,'.');
1150 if (dirsep1)
1151 *dirsep1=0;
1153 TRACE( module, "looking for (%p) %s and %s \n",
1154 libname, libname,strippedname );
1156 /* Load library module */
1157 ret= LoadModule16( strippedname, (LPVOID)-1 );
1158 if (ret > HINSTANCE_ERROR)
1159 return ret;
1160 return LoadModule16(libname, (LPVOID)-1 );
1164 /**********************************************************************
1165 * MODULE_CallWEP
1167 * Call a DLL's WEP, allowing it to shut down.
1168 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1170 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1172 FARPROC16 WEP = (FARPROC16)0;
1173 WORD ordinal = NE_GetOrdinal( hModule, "WEP" );
1175 if (ordinal) WEP = NE_GetEntryPoint( hModule, ordinal );
1176 if (!WEP)
1178 WARN(module, "module %04x doesn't have a WEP\n", hModule );
1179 return FALSE;
1181 return Callbacks->CallWindowsExitProc( WEP, WEP_FREE_DLL );
1185 /**********************************************************************
1186 * NE_FreeModule
1188 * Implementation of FreeModule16().
1190 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1192 HMODULE16 *hPrevModule;
1193 NE_MODULE *pModule;
1194 HMODULE16 *pModRef;
1195 int i;
1197 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1198 hModule = pModule->self;
1200 TRACE( module, "%04x count %d\n", hModule, pModule->count );
1202 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1203 else pModule->count = 0;
1205 if (pModule->flags & NE_FFLAGS_BUILTIN)
1206 return FALSE; /* Can't free built-in module */
1208 if (call_wep)
1210 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1212 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1213 MODULE_CallWEP( hModule );
1215 /* Free the objects owned by the DLL module */
1217 if (pTask && pTask->userhandler)
1218 pTask->userhandler( hModule, USIG16_DLL_UNLOAD, 0,
1219 pTask->hInstance, pTask->hQueue );
1221 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16, hModule );
1223 else
1224 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1228 /* Clear magic number just in case */
1230 pModule->magic = pModule->self = 0;
1232 /* Remove it from the linked list */
1234 hPrevModule = &hFirstModule;
1235 while (*hPrevModule && (*hPrevModule != hModule))
1237 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1239 if (*hPrevModule) *hPrevModule = pModule->next;
1241 /* Free the referenced modules */
1243 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1244 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1246 NE_FreeModule( *pModRef, call_wep );
1249 /* Free the module storage */
1251 GlobalFreeAll16( hModule );
1253 /* Remove module from cache */
1255 if (pCachedModule == pModule) pCachedModule = NULL;
1256 return TRUE;
1260 /**********************************************************************
1261 * FreeModule16 (KERNEL.46)
1263 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1265 return NE_FreeModule( hModule, TRUE );
1269 /***********************************************************************
1270 * FreeLibrary16 (KERNEL.96)
1272 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1274 TRACE(module,"%04x\n", handle );
1275 FreeModule16( handle );
1279 /**********************************************************************
1280 * GetModuleName (KERNEL.27)
1282 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1284 NE_MODULE *pModule;
1285 BYTE *p;
1287 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1288 p = (BYTE *)pModule + pModule->name_table;
1289 if (count > *p) count = *p + 1;
1290 if (count > 0)
1292 memcpy( buf, p + 1, count - 1 );
1293 buf[count-1] = '\0';
1295 return TRUE;
1299 /**********************************************************************
1300 * GetModuleUsage (KERNEL.48)
1302 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1304 NE_MODULE *pModule = NE_GetPtr( hModule );
1305 return pModule ? pModule->count : 0;
1309 /**********************************************************************
1310 * GetExpWinVer (KERNEL.167)
1312 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1314 NE_MODULE *pModule = NE_GetPtr( hModule );
1315 return pModule ? pModule->expected_version : 0;
1319 /**********************************************************************
1320 * GetModuleFileName16 (KERNEL.49)
1322 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1323 INT16 nSize )
1325 NE_MODULE *pModule;
1327 if (!hModule) hModule = GetCurrentTask();
1328 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1329 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1330 TRACE(module, "%s\n", lpFileName );
1331 return strlen(lpFileName);
1335 /**********************************************************************
1336 * GetModuleHandle16 (KERNEL.47)
1338 * Find a module from a module name.
1340 * NOTE: The current implementation works the same way the Windows 95 one
1341 * does. Do not try to 'fix' it, fix the callers.
1342 * + It does not do ANY extension handling (except that strange .EXE bit)!
1343 * + It does not care about paths, just about basenames. (same as Windows)
1345 * RETURNS
1346 * LOWORD:
1347 * the win16 module handle if found
1348 * 0 if not
1349 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1350 * Always hFirstModule
1352 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1354 if (HIWORD(name) == 0)
1355 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1356 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name)), hFirstModule );
1359 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1361 HMODULE16 hModule = hFirstModule;
1362 LPSTR s;
1363 BYTE len, *name_table;
1364 char tmpstr[128];
1365 NE_MODULE *pModule;
1367 TRACE(module, "(%s)\n", name);
1369 if (!HIWORD(name))
1370 return GetExePtr(LOWORD(name));
1372 len = strlen(name);
1373 if (!len)
1374 return 0;
1376 strncpy(tmpstr, name, sizeof(tmpstr));
1377 tmpstr[sizeof(tmpstr)-1] = '\0';
1379 /* If 'name' matches exactly the module name of a module:
1380 * Return its handle.
1382 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1384 pModule = NE_GetPtr( hModule );
1385 if (!pModule) break;
1387 name_table = (BYTE *)pModule + pModule->name_table;
1388 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1389 return hModule;
1392 /* If uppercased 'name' matches exactly the module name of a module:
1393 * Return its handle
1395 for (s = tmpstr; *s; s++)
1396 *s = toupper(*s);
1398 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1400 pModule = NE_GetPtr( hModule );
1401 if (!pModule) break;
1403 name_table = (BYTE *)pModule + pModule->name_table;
1404 if ((*name_table == len) && !strncmp(tmpstr, name_table+1, len))
1405 return hModule;
1408 /* If the base filename of 'name' matches the base filename of the module
1409 * filename of some module (case-insensitive compare):
1410 * Return its handle.
1413 /* basename: search backwards in passed name to \ / or : */
1414 s = tmpstr + strlen(tmpstr);
1415 while (s > tmpstr)
1417 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1418 break;
1419 s--;
1422 /* search this in loaded filename list */
1423 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1425 char *loadedfn;
1426 OFSTRUCT *ofs;
1428 pModule = NE_GetPtr( hModule );
1429 if (!pModule) break;
1430 if (!pModule->fileinfo) continue;
1432 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1433 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1434 /* basename: search backwards in pathname to \ / or : */
1435 while (loadedfn > (char*)ofs->szPathName)
1437 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1438 break;
1439 loadedfn--;
1441 /* case insensitive compare ... */
1442 if (!lstrcmpiA(loadedfn, s))
1443 return hModule;
1446 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1447 * matches the base filename of the module filename of some 32-bit module:
1448 * Return the corresponding 16-bit dummy module handle.
1450 if (len >= 4 && !strcasecmp(name+len-4, ".EXE"))
1452 HMODULE hModule = GetModuleHandleA( name );
1453 if ( hModule )
1454 return MapHModuleLS( hModule );
1457 if (!strcmp(tmpstr,"MSDOS"))
1458 return 1;
1460 if (!strcmp(tmpstr,"TIMER"))
1462 FIXME(module, "Eh... Should return caller's code segment, expect crash\n");
1463 return 0;
1466 return 0;
1470 /**********************************************************************
1471 * ModuleFirst (TOOLHELP.59)
1473 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1475 lpme->wNext = hFirstModule;
1476 return ModuleNext16( lpme );
1480 /**********************************************************************
1481 * ModuleNext (TOOLHELP.60)
1483 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1485 NE_MODULE *pModule;
1486 char *name;
1488 if (!lpme->wNext) return FALSE;
1489 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1490 name = (char *)pModule + pModule->name_table;
1491 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1492 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1493 lpme->hModule = lpme->wNext;
1494 lpme->wcUsage = pModule->count;
1495 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1496 lpme->wNext = pModule->next;
1497 return TRUE;
1501 /**********************************************************************
1502 * ModuleFindName (TOOLHELP.61)
1504 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1506 lpme->wNext = GetModuleHandle16( name );
1507 return ModuleNext16( lpme );
1511 /**********************************************************************
1512 * ModuleFindHandle (TOOLHELP.62)
1514 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1516 hModule = GetExePtr( hModule );
1517 lpme->wNext = hModule;
1518 return ModuleNext16( lpme );
1522 /***************************************************************************
1523 * MapHModuleLS (KERNEL32.520)
1525 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod) {
1526 NE_MODULE *pModule;
1528 if (!hmod)
1529 return ((TDB*)GlobalLock16(GetCurrentTask()))->hInstance;
1530 if (!HIWORD(hmod))
1531 return hmod; /* we already have a 16 bit module handle */
1532 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1533 while (pModule) {
1534 if (pModule->module32 == hmod)
1535 return pModule->self;
1536 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1538 return 0;
1541 /***************************************************************************
1542 * MapHModuleSL (KERNEL32.521)
1544 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1545 NE_MODULE *pModule;
1547 if (!hmod) {
1548 TDB *pTask = (TDB*)GlobalLock16(GetCurrentTask());
1550 hmod = pTask->hModule;
1552 pModule = (NE_MODULE*)GlobalLock16(hmod);
1553 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1554 !(pModule->flags & NE_FFLAGS_WIN32)
1556 return 0;
1557 return pModule->module32;
1560 /***************************************************************************
1561 * MapHInstLS (KERNEL32.516)
1563 void WINAPI REGS_FUNC(MapHInstLS)( CONTEXT *context )
1565 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1568 /***************************************************************************
1569 * MapHInstSL (KERNEL32.518)
1571 void WINAPI REGS_FUNC(MapHInstSL)( CONTEXT *context )
1573 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1576 /***************************************************************************
1577 * MapHInstLS_PN (KERNEL32.517)
1579 void WINAPI REGS_FUNC(MapHInstLS_PN)( CONTEXT *context )
1581 if (EAX_reg(context))
1582 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1585 /***************************************************************************
1586 * MapHInstSL_PN (KERNEL32.519)
1588 void WINAPI REGS_FUNC(MapHInstSL_PN)( CONTEXT *context )
1590 if (EAX_reg(context))
1591 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1594 /***************************************************************************
1595 * WIN16_MapHInstLS (KERNEL.472)
1597 VOID WINAPI WIN16_MapHInstLS( CONTEXT *context ) {
1598 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1601 /***************************************************************************
1602 * WIN16_MapHInstSL (KERNEL.473)
1604 VOID WINAPI WIN16_MapHInstSL( CONTEXT *context ) {
1605 EAX_reg(context) = MapHModuleSL(EAX_reg(context));