4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
27 #include "stackframe.h"
31 FARPROC16 (*fnSNOOP16_GetProcAddress16)(HMODULE16,DWORD,FARPROC16) = NULL;
32 void (*fnSNOOP16_RegisterDLL)(NE_MODULE*,LPCSTR) = NULL;
34 #define hFirstModule (pThhook->hExeHead)
36 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
37 static HMODULE16 GetModuleFromPath(LPCSTR name);
39 static HMODULE16 NE_LoadBuiltin(LPCSTR name,BOOL force) { return 0; }
40 HMODULE16 (*fnBUILTIN_LoadModule)(LPCSTR name,BOOL force) = NE_LoadBuiltin;
43 /***********************************************************************
46 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
48 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
52 /***********************************************************************
55 void NE_DumpModule( HMODULE16 hModule )
63 if (!(pModule = NE_GetPtr( hModule )))
65 MSG( "**** %04x is not a module handle\n", hModule );
69 /* Dump the module info */
71 DUMP( "Module %04x:\n", hModule );
72 DUMP( "count=%d flags=%04x heap=%d stack=%d\n",
73 pModule->count, pModule->flags,
74 pModule->heap_size, pModule->stack_size );
75 DUMP( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
76 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
77 pModule->seg_count, pModule->modref_count );
78 DUMP( "os_flags=%d swap_area=%d version=%04x\n",
79 pModule->os_flags, pModule->min_swap_area,
80 pModule->expected_version );
81 if (pModule->flags & NE_FFLAGS_WIN32)
82 DUMP( "PE module=%08x\n", pModule->module32 );
84 /* Dump the file info */
86 DUMP( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
88 /* Dump the segment table */
90 DUMP( "Segment table:\n" );
91 pSeg = NE_SEG_TABLE( pModule );
92 for (i = 0; i < pModule->seg_count; i++, pSeg++)
93 DUMP( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
94 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
95 pSeg->minsize, pSeg->hSeg );
97 /* Dump the resource table */
99 DUMP( "Resource table:\n" );
100 if (pModule->res_table)
102 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
103 DUMP( "Alignment: %d\n", *pword++ );
106 struct resource_typeinfo_s *ptr = (struct resource_typeinfo_s *)pword;
107 struct resource_nameinfo_s *pname = (struct resource_nameinfo_s *)(ptr + 1);
108 DUMP( "id=%04x count=%d\n", ptr->type_id, ptr->count );
109 for (i = 0; i < ptr->count; i++, pname++)
110 DUMP( "offset=%d len=%d id=%04x\n",
111 pname->offset, pname->length, pname->id );
112 pword = (WORD *)pname;
115 else DUMP( "None\n" );
117 /* Dump the resident name table */
119 DUMP( "Resident-name table:\n" );
120 pstr = (char *)pModule + pModule->name_table;
123 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
124 *(WORD *)(pstr + *pstr + 1) );
125 pstr += *pstr + 1 + sizeof(WORD);
128 /* Dump the module reference table */
130 DUMP( "Module ref table:\n" );
131 if (pModule->modref_table)
133 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
134 for (i = 0; i < pModule->modref_count; i++, pword++)
137 GetModuleName16( *pword, name, sizeof(name) );
138 DUMP( "%d: %04x -> '%s'\n", i, *pword, name );
141 else DUMP( "None\n" );
143 /* Dump the entry table */
145 DUMP( "Entry table:\n" );
146 pstr = (char *)pModule + pModule->entry_table;
150 DUMP( "Bundle %d-%d: %02x\n", ordinal, ordinal + *pstr - 1, pstr[1]);
156 else if ((BYTE)pstr[1] == 0xff) /* moveable */
162 DUMP( "%d: %02x:%04x (moveable)\n",
163 ordinal++, pstr[3], *(WORD *)(pstr + 4) );
173 DUMP( "%d: %04x (fixed)\n",
174 ordinal++, *(WORD *)(pstr + 1) );
180 /* Dump the non-resident names table */
182 DUMP( "Non-resident names table:\n" );
183 if (pModule->nrname_handle)
185 pstr = (char *)GlobalLock16( pModule->nrname_handle );
188 DUMP( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
189 *(WORD *)(pstr + *pstr + 1) );
190 pstr += *pstr + 1 + sizeof(WORD);
197 /***********************************************************************
200 * Walk the module list and print the modules.
202 void NE_WalkModules(void)
204 HMODULE16 hModule = hFirstModule;
205 MSG( "Module Flags Name\n" );
208 NE_MODULE *pModule = NE_GetPtr( hModule );
211 MSG( "Bad module %04x in list\n", hModule );
214 MSG( " %04x %04x %.*s\n", hModule, pModule->flags,
215 *((char *)pModule + pModule->name_table),
216 (char *)pModule + pModule->name_table + 1 );
217 hModule = pModule->next;
222 /**********************************************************************
225 void NE_RegisterModule( NE_MODULE *pModule )
227 pModule->next = hFirstModule;
228 hFirstModule = pModule->self;
232 /***********************************************************************
235 * Lookup the ordinal for a given name.
237 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
239 unsigned char buffer[256], *cpnt;
243 if (!(pModule = NE_GetPtr( hModule ))) return 0;
244 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
246 TRACE( module, "(%04x,'%s')\n", hModule, name );
248 /* First handle names of the form '#xxxx' */
250 if (name[0] == '#') return atoi( name + 1 );
252 /* Now copy and uppercase the string */
254 strcpy( buffer, name );
255 CharUpperA( buffer );
256 len = strlen( buffer );
258 /* First search the resident names */
260 cpnt = (char *)pModule + pModule->name_table;
262 /* Skip the first entry (module name) */
263 cpnt += *cpnt + 1 + sizeof(WORD);
266 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
268 TRACE(module, " Found: ordinal=%d\n",
269 *(WORD *)(cpnt + *cpnt + 1) );
270 return *(WORD *)(cpnt + *cpnt + 1);
272 cpnt += *cpnt + 1 + sizeof(WORD);
275 /* Now search the non-resident names table */
277 if (!pModule->nrname_handle) return 0; /* No non-resident table */
278 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
280 /* Skip the first entry (module description string) */
281 cpnt += *cpnt + 1 + sizeof(WORD);
284 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
286 TRACE(module, " Found: ordinal=%d\n",
287 *(WORD *)(cpnt + *cpnt + 1) );
288 return *(WORD *)(cpnt + *cpnt + 1);
290 cpnt += *cpnt + 1 + sizeof(WORD);
296 /***********************************************************************
297 * NE_GetEntryPoint (WPROCS.27)
299 * Return the entry point for a given ordinal.
301 FARPROC16 NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
303 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
305 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
312 if (!(pModule = NE_GetPtr( hModule ))) return 0;
313 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
315 p = (BYTE *)pModule + pModule->entry_table;
316 while (*p && (curOrdinal + *p <= ordinal))
318 /* Skipping this bundle */
322 case 0: p += 2; break; /* unused */
323 case 0xff: p += 2 + *p * 6; break; /* moveable */
324 default: p += 2 + *p * 3; break; /* fixed */
333 case 0xff: /* moveable */
334 p += 2 + 6 * (ordinal - curOrdinal);
336 offset = *(WORD *)(p + 4);
340 p += 2 + 3 * (ordinal - curOrdinal);
341 offset = *(WORD *)(p + 1);
345 if (sel == 0xfe) sel = 0xffff; /* constant entry */
346 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
348 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
349 if (!snoop || !fnSNOOP16_GetProcAddress16)
350 return (FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset );
352 return (FARPROC16)fnSNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)PTR_SEG_OFF_TO_SEGPTR( sel, offset ));
356 /***********************************************************************
359 * Change the value of an entry point. Use with caution!
360 * It can only change the offset value, not the selector.
362 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
368 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
369 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
371 p = (BYTE *)pModule + pModule->entry_table;
372 while (*p && (curOrdinal + *p <= ordinal))
374 /* Skipping this bundle */
378 case 0: p += 2; break; /* unused */
379 case 0xff: p += 2 + *p * 6; break; /* moveable */
380 default: p += 2 + *p * 3; break; /* fixed */
383 if (!*p) return FALSE;
389 case 0xff: /* moveable */
390 p += 2 + 6 * (ordinal - curOrdinal);
391 *(WORD *)(p + 4) = offset;
394 p += 2 + 3 * (ordinal - curOrdinal);
395 *(WORD *)(p + 1) = offset;
402 /***********************************************************************
405 HANDLE NE_OpenFile( NE_MODULE *pModule )
409 static HANDLE cachedfd = -1;
411 TRACE( module, "(%p) cache: mod=%p fd=%d\n",
412 pModule, pCachedModule, cachedfd );
413 if (pCachedModule == pModule) return cachedfd;
414 CloseHandle( cachedfd );
415 pCachedModule = pModule;
416 name = NE_MODULE_NAME( pModule );
417 if ((cachedfd = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
418 NULL, OPEN_EXISTING, 0, -1 )) == -1)
419 MSG( "Can't open file '%s' for module %04x\n", name, pModule->self );
421 /* FIXME: should not be necessary */
422 cachedfd = ConvertToGlobalHandle(cachedfd);
423 TRACE(module, "opened '%s' -> %d\n",
429 /***********************************************************************
432 static HMODULE16 NE_LoadExeHeader( HFILE16 hFile, OFSTRUCT *ofs )
434 IMAGE_DOS_HEADER mz_header;
435 IMAGE_OS2_HEADER ne_header;
440 char *buffer, *fastload = NULL;
441 int fastload_offset = 0, fastload_length = 0;
443 /* Read a block from either the file or the fast-load area. */
444 #define READ(offset,size,buffer) \
445 ((fastload && ((offset) >= fastload_offset) && \
446 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
447 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
448 (_llseek16( hFile, (offset), SEEK_SET), \
449 _hread16( hFile, (buffer), (size) ) == (size)))
451 _llseek16( hFile, 0, SEEK_SET );
452 if ((_hread16(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) ||
453 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
454 return (HMODULE16)11; /* invalid exe */
456 _llseek16( hFile, mz_header.e_lfanew, SEEK_SET );
457 if (_hread16( hFile, &ne_header, sizeof(ne_header) ) != sizeof(ne_header))
458 return (HMODULE16)11; /* invalid exe */
460 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
461 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
463 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
464 MSG("Sorry, this is an OS/2 linear executable (LX) file !\n");
465 return (HMODULE16)12;
468 /* We now have a valid NE header */
470 size = sizeof(NE_MODULE) +
472 ne_header.n_segment_tab * sizeof(SEGTABLEENTRY) +
474 ne_header.rname_tab_offset - ne_header.resource_tab_offset +
475 /* resident names table */
476 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset +
477 /* module ref table */
478 ne_header.n_mod_ref_tab * sizeof(WORD) +
479 /* imported names table */
480 ne_header.entry_tab_offset - ne_header.iname_tab_offset +
481 /* entry table length */
482 ne_header.entry_tab_length +
483 /* loaded file info */
484 sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
486 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
487 if (!hModule) return (HMODULE16)11; /* invalid exe */
488 FarSetOwner16( hModule, hModule );
489 pModule = (NE_MODULE *)GlobalLock16( hModule );
490 memcpy( pModule, &ne_header, sizeof(ne_header) );
492 /* check *programs* for default minimal stack size */
493 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
494 && (pModule->stack_size < 0x1400) )
495 pModule->stack_size = 0x1400;
496 pModule->module32 = 0;
497 pModule->self = hModule;
498 pModule->self_loading_sel = 0;
499 pData = (BYTE *)(pModule + 1);
501 /* Clear internal Wine flags in case they are set in the EXE file */
503 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
505 /* Read the fast-load area */
507 if (ne_header.additional_flags & NE_AFLAGS_FASTLOAD)
509 fastload_offset=ne_header.fastload_offset<<ne_header.align_shift_count;
510 fastload_length=ne_header.fastload_length<<ne_header.align_shift_count;
511 TRACE(module, "Using fast-load area offset=%x len=%d\n",
512 fastload_offset, fastload_length );
513 if ((fastload = HeapAlloc( SystemHeap, 0, fastload_length )) != NULL)
515 _llseek16( hFile, fastload_offset, SEEK_SET);
516 if (_hread16(hFile, fastload, fastload_length) != fastload_length)
518 HeapFree( SystemHeap, 0, fastload );
519 WARN( module, "Error reading fast-load area!\n");
525 /* Get the segment table */
527 pModule->seg_table = (int)pData - (int)pModule;
528 buffer = HeapAlloc( SystemHeap, 0, ne_header.n_segment_tab *
529 sizeof(struct ne_segment_table_entry_s));
533 struct ne_segment_table_entry_s *pSeg;
535 if (!READ( mz_header.e_lfanew + ne_header.segment_tab_offset,
536 ne_header.n_segment_tab * sizeof(struct ne_segment_table_entry_s),
539 HeapFree( SystemHeap, 0, buffer );
540 if (fastload) HeapFree( SystemHeap, 0, fastload );
541 GlobalFree16( hModule );
542 return (HMODULE16)11; /* invalid exe */
544 pSeg = (struct ne_segment_table_entry_s *)buffer;
545 for (i = ne_header.n_segment_tab; i > 0; i--, pSeg++)
547 memcpy( pData, pSeg, sizeof(*pSeg) );
548 pData += sizeof(SEGTABLEENTRY);
550 HeapFree( SystemHeap, 0, buffer );
554 if (fastload) HeapFree( SystemHeap, 0, fastload );
555 GlobalFree16( hModule );
556 return (HMODULE16)11; /* invalid exe */
559 /* Get the resource table */
561 if (ne_header.resource_tab_offset < ne_header.rname_tab_offset)
563 pModule->res_table = (int)pData - (int)pModule;
564 if (!READ(mz_header.e_lfanew + ne_header.resource_tab_offset,
565 ne_header.rname_tab_offset - ne_header.resource_tab_offset,
566 pData )) return (HMODULE16)11; /* invalid exe */
567 pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
568 NE_InitResourceHandler( hModule );
570 else pModule->res_table = 0; /* No resource table */
572 /* Get the resident names table */
574 pModule->name_table = (int)pData - (int)pModule;
575 if (!READ( mz_header.e_lfanew + ne_header.rname_tab_offset,
576 ne_header.moduleref_tab_offset - ne_header.rname_tab_offset,
579 if (fastload) HeapFree( SystemHeap, 0, fastload );
580 GlobalFree16( hModule );
581 return (HMODULE16)11; /* invalid exe */
583 pData += ne_header.moduleref_tab_offset - ne_header.rname_tab_offset;
585 /* Get the module references table */
587 if (ne_header.n_mod_ref_tab > 0)
589 pModule->modref_table = (int)pData - (int)pModule;
590 if (!READ( mz_header.e_lfanew + ne_header.moduleref_tab_offset,
591 ne_header.n_mod_ref_tab * sizeof(WORD),
594 if (fastload) HeapFree( SystemHeap, 0, fastload );
595 GlobalFree16( hModule );
596 return (HMODULE16)11; /* invalid exe */
598 pData += ne_header.n_mod_ref_tab * sizeof(WORD);
600 else pModule->modref_table = 0; /* No module references */
602 /* Get the imported names table */
604 pModule->import_table = (int)pData - (int)pModule;
605 if (!READ( mz_header.e_lfanew + ne_header.iname_tab_offset,
606 ne_header.entry_tab_offset - ne_header.iname_tab_offset,
609 if (fastload) HeapFree( SystemHeap, 0, fastload );
610 GlobalFree16( hModule );
611 return (HMODULE16)11; /* invalid exe */
613 pData += ne_header.entry_tab_offset - ne_header.iname_tab_offset;
615 /* Get the entry table */
617 pModule->entry_table = (int)pData - (int)pModule;
618 if (!READ( mz_header.e_lfanew + ne_header.entry_tab_offset,
619 ne_header.entry_tab_length,
622 if (fastload) HeapFree( SystemHeap, 0, fastload );
623 GlobalFree16( hModule );
624 return (HMODULE16)11; /* invalid exe */
626 pData += ne_header.entry_tab_length;
628 /* Store the filename information */
630 pModule->fileinfo = (int)pData - (int)pModule;
631 size = sizeof(OFSTRUCT)-sizeof(ofs->szPathName)+strlen(ofs->szPathName)+1;
632 memcpy( pData, ofs, size );
633 ((OFSTRUCT *)pData)->cBytes = size - 1;
636 /* Free the fast-load area */
639 if (fastload) HeapFree( SystemHeap, 0, fastload );
641 /* Get the non-resident names table */
643 if (ne_header.nrname_tab_length)
645 pModule->nrname_handle = GLOBAL_Alloc( 0, ne_header.nrname_tab_length,
646 hModule, FALSE, FALSE, FALSE );
647 if (!pModule->nrname_handle)
649 GlobalFree16( hModule );
650 return (HMODULE16)11; /* invalid exe */
652 buffer = GlobalLock16( pModule->nrname_handle );
653 _llseek16( hFile, ne_header.nrname_tab_offset, SEEK_SET );
654 if (_hread16( hFile, buffer, ne_header.nrname_tab_length )
655 != ne_header.nrname_tab_length)
657 GlobalFree16( pModule->nrname_handle );
658 GlobalFree16( hModule );
659 return (HMODULE16)11; /* invalid exe */
662 else pModule->nrname_handle = 0;
664 /* Allocate a segment for the implicitly-loaded DLLs */
666 if (pModule->modref_count)
668 pModule->dlls_to_init = GLOBAL_Alloc(GMEM_ZEROINIT,
669 (pModule->modref_count+1)*sizeof(HMODULE16),
670 hModule, FALSE, FALSE, FALSE );
671 if (!pModule->dlls_to_init)
673 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
674 GlobalFree16( hModule );
675 return (HMODULE16)11; /* invalid exe */
678 else pModule->dlls_to_init = 0;
680 NE_RegisterModule( pModule );
681 if (fnSNOOP16_RegisterDLL)
682 fnSNOOP16_RegisterDLL(pModule,ofs->szPathName);
687 /***********************************************************************
690 * Load all DLLs implicitly linked to a module.
692 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
695 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
696 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
698 for (i = 0; i < pModule->modref_count; i++, pModRef++)
701 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
702 memcpy( buffer, pstr + 1, *pstr );
703 *(buffer + *pstr) = 0; /* terminate it */
704 if (!strchr(buffer,'.')) /* only append .dll if no extension yet.
705 handles a request for krnl386.exe*/
706 strcpy( buffer + *pstr, ".dll" );
707 TRACE(module, "Loading '%s'\n", buffer );
708 if (!(*pModRef = GetModuleHandle16( buffer )))
710 /* If the DLL is not loaded yet, load it and store */
711 /* its handle in the list of DLLs to initialize. */
714 if ((hDLL = NE_LoadModule( buffer, TRUE )) == 2)
719 /* Try with prepending the path of the current module */
720 GetModuleFileName16( pModule->self, buffer, sizeof(buffer) );
721 if (!(p = strrchr( buffer, '\\' ))) p = buffer;
722 memcpy( p + 1, pstr + 1, *pstr );
723 strcpy( p + 1 + *pstr, ".dll" );
724 hDLL = NE_LoadModule( buffer, TRUE );
728 /* FIXME: cleanup what was done */
730 MSG( "Could not load '%s' required by '%.*s', error=%d\n",
731 buffer, *((BYTE*)pModule + pModule->name_table),
732 (char *)pModule + pModule->name_table + 1, hDLL );
735 *pModRef = GetExePtr( hDLL );
738 else /* Increment the reference count of the DLL */
740 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
741 if (pOldDLL) pOldDLL->count++;
748 /**********************************************************************
751 * Load first instance of NE module from file.
752 * (Note: caller is responsible for ensuring the module isn't
755 static HINSTANCE16 NE_LoadFileModule( HFILE16 hFile, OFSTRUCT *ofs,
758 HINSTANCE16 hInstance;
762 /* Create the module structure */
764 hModule = NE_LoadExeHeader( hFile, ofs );
765 if (hModule < 32) return hModule;
766 pModule = NE_GetPtr( hModule );
768 /* Allocate the segments for this module */
770 if (!NE_CreateSegments( pModule ) ||
771 !(hInstance = NE_CreateInstance( pModule, NULL, FALSE )))
773 GlobalFreeAll16( hModule );
774 return 8; /* Insufficient memory */
777 /* Load the referenced DLLs */
779 if (!NE_LoadDLLs( pModule ))
780 return 2; /* File not found (FIXME: free everything) */
782 /* Load the segments */
784 NE_LoadAllSegments( pModule );
786 /* Fixup the functions prologs */
788 NE_FixupPrologs( pModule );
790 /* Make sure the usage count is 1 on the first loading of */
791 /* the module, even if it contains circular DLL references */
795 /* Call initialization rountines for all loaded DLLs. Note that
796 * when we load implicitly linked DLLs this will be done by InitTask().
799 if (!implicit && (pModule->flags & NE_FFLAGS_LIBMODULE))
800 NE_InitializeDLLs( hModule );
805 /**********************************************************************
808 * Load first instance of NE module, deciding whether to use
809 * built-in module or load module from file.
810 * (Note: caller is responsible for ensuring the module isn't
813 HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL implicit )
815 HINSTANCE16 hInstance;
820 /* Try to load the built-in first if not disabled */
822 if ((hModule = fnBUILTIN_LoadModule( name, FALSE ))) return hModule;
824 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
826 /* Now try the built-in even if disabled */
827 if ((hModule = fnBUILTIN_LoadModule( name, TRUE )))
829 MSG( "Could not load Windows DLL '%s', using built-in module.\n",
833 return 2; /* File not found */
836 hInstance = NE_LoadFileModule( hFile, &ofs, implicit );
842 /**********************************************************************
843 * LoadModule16 (KERNEL.45)
845 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
847 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
848 LOADPARAMS16 *params;
849 LPSTR cmd_line, new_cmd_line;
851 STARTUPINFOA startup;
852 PROCESS_INFORMATION info;
853 HINSTANCE16 hInstance, hPrevInstance = 0;
860 if ( (hModule = GetModuleFromPath(name) ) != 0 )
862 /* Special case: second instance of an already loaded NE module */
864 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
865 if ( pModule->module32 ) return (HINSTANCE16)21;
867 hInstance = NE_CreateInstance( pModule, &hPrevInstance, lib_only );
868 if ( hInstance != hPrevInstance ) /* not a library */
869 NE_LoadSegment( pModule, pModule->dgroup );
875 /* Main case: load first instance of NE module */
877 if ( (hInstance = NE_LoadModule( name, FALSE )) < 32 )
880 if ( !(pModule = NE_GetPtr( hInstance )) )
881 return (HINSTANCE16)11;
884 /* If library module, we're finished */
886 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
889 /* Create a task for this instance */
891 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
893 params = (LOADPARAMS16 *)paramBlock;
894 cmd_line = (LPSTR)PTR_SEG_TO_LIN( params->cmdLine );
895 if (!cmd_line) cmd_line = "";
896 else if (*cmd_line) cmd_line++; /* skip the length byte */
898 if (!(new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
899 strlen(cmd_line)+strlen(name)+2 )))
901 strcpy( new_cmd_line, name );
902 strcat( new_cmd_line, " " );
903 strcat( new_cmd_line, cmd_line );
905 if (params->hEnvironment) env = GlobalLock16( params->hEnvironment );
907 memset( &info, '\0', sizeof(info) );
908 memset( &startup, '\0', sizeof(startup) );
909 startup.cb = sizeof(startup);
912 startup.dwFlags = STARTF_USESHOWWINDOW;
913 startup.wShowWindow = ((UINT16 *)PTR_SEG_TO_LIN(params->showCmd))[1];
916 pdb = PROCESS_Create( pModule, new_cmd_line, env,
917 hInstance, hPrevInstance,
918 NULL, NULL, TRUE, &startup, &info );
920 CloseHandle( info.hThread );
921 CloseHandle( info.hProcess );
923 if (params->hEnvironment) GlobalUnlock16( params->hEnvironment );
924 HeapFree( GetProcessHeap(), 0, new_cmd_line );
928 if (pdb) TASK_StartTask( pdb->task );
933 /**********************************************************************
936 BOOL NE_CreateProcess( HFILE hFile, OFSTRUCT *ofs, LPCSTR cmd_line, LPCSTR env,
937 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
938 BOOL inherit, LPSTARTUPINFOA startup,
939 LPPROCESS_INFORMATION info )
941 HINSTANCE16 hInstance, hPrevInstance = 0;
946 /* Special case: second instance of an already loaded NE module */
948 if ( ( hModule = GetModuleFromPath( ofs->szPathName ) ) != 0 )
950 if ( !( pModule = NE_GetPtr( hModule) )
951 || ( pModule->flags & NE_FFLAGS_LIBMODULE )
952 || pModule->module32 )
954 SetLastError( ERROR_BAD_FORMAT );
958 hInstance = NE_CreateInstance( pModule, &hPrevInstance, FALSE );
959 if ( hInstance != hPrevInstance ) /* not a library */
960 NE_LoadSegment( pModule, pModule->dgroup );
965 /* Main case: load first instance of NE module */
968 /* If we didn't get a file handle, return */
970 if ( hFile == HFILE_ERROR )
973 /* Allocate temporary HFILE16 for NE_LoadFileModule */
975 if (!DuplicateHandle( GetCurrentProcess(), hFile,
976 GetCurrentProcess(), &hFile,
977 0, FALSE, DUPLICATE_SAME_ACCESS ))
979 SetLastError( ERROR_INVALID_HANDLE );
982 hFile16 = FILE_AllocDosHandle( hFile );
986 hInstance = NE_LoadFileModule( hFile16, ofs, TRUE );
987 _lclose16( hFile16 );
989 if ( hInstance < 32 )
991 SetLastError( hInstance );
995 if ( !( pModule = NE_GetPtr( hInstance ) )
996 || ( pModule->flags & NE_FFLAGS_LIBMODULE) )
999 SetLastError( ERROR_BAD_FORMAT );
1004 /* Create a task for this instance */
1006 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1008 if ( !PROCESS_Create( pModule, cmd_line, env,
1009 hInstance, hPrevInstance,
1010 psa, tsa, inherit, startup, info ) )
1016 /***********************************************************************
1017 * LoadLibrary16 (KERNEL.95)
1019 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1021 char dllname[256], *p;
1023 TRACE( module, "(%08x) %s\n", (int)libname, libname );
1025 /* Append .DLL to name if no extension present */
1027 strcpy( dllname, libname );
1028 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
1029 strcat( dllname, ".DLL" );
1031 /* Load library module */
1033 return LoadModule16( dllname, (LPVOID)-1 );
1037 /**********************************************************************
1040 * Call a DLL's WEP, allowing it to shut down.
1041 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1043 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1045 FARPROC16 WEP = (FARPROC16)0;
1046 WORD ordinal = NE_GetOrdinal( hModule, "WEP" );
1048 if (ordinal) WEP = NE_GetEntryPoint( hModule, ordinal );
1051 WARN(module, "module %04x doesn't have a WEP\n", hModule );
1054 return Callbacks->CallWindowsExitProc( WEP, WEP_FREE_DLL );
1058 /**********************************************************************
1061 * Implementation of FreeModule16().
1063 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1065 HMODULE16 *hPrevModule;
1070 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1071 hModule = pModule->self;
1073 TRACE( module, "%04x count %d\n", hModule, pModule->count );
1075 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1076 else pModule->count = 0;
1078 if (pModule->flags & NE_FFLAGS_BUILTIN)
1079 return FALSE; /* Can't free built-in module */
1083 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1085 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1086 MODULE_CallWEP( hModule );
1088 /* Free the objects owned by the DLL module */
1090 if (pTask && pTask->userhandler)
1091 pTask->userhandler( hModule, USIG_DLL_UNLOAD, 0,
1092 pTask->hInstance, pTask->hQueue );
1095 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1099 /* Clear magic number just in case */
1101 pModule->magic = pModule->self = 0;
1103 /* Remove it from the linked list */
1105 hPrevModule = &hFirstModule;
1106 while (*hPrevModule && (*hPrevModule != hModule))
1108 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1110 if (*hPrevModule) *hPrevModule = pModule->next;
1112 /* Free the referenced modules */
1114 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1115 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1117 NE_FreeModule( *pModRef, call_wep );
1120 /* Free the module storage */
1122 GlobalFreeAll16( hModule );
1124 /* Remove module from cache */
1126 if (pCachedModule == pModule) pCachedModule = NULL;
1131 /**********************************************************************
1132 * FreeModule16 (KERNEL.46)
1134 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1136 return NE_FreeModule( hModule, TRUE );
1140 /***********************************************************************
1141 * FreeLibrary16 (KERNEL.96)
1143 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1145 TRACE(module,"%04x\n", handle );
1146 FreeModule16( handle );
1150 /**********************************************************************
1151 * GetModuleName (KERNEL.27)
1153 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1158 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1159 p = (BYTE *)pModule + pModule->name_table;
1160 if (count > *p) count = *p + 1;
1163 memcpy( buf, p + 1, count - 1 );
1164 buf[count-1] = '\0';
1170 /**********************************************************************
1171 * GetModuleUsage (KERNEL.48)
1173 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1175 NE_MODULE *pModule = NE_GetPtr( hModule );
1176 return pModule ? pModule->count : 0;
1180 /**********************************************************************
1181 * GetExpWinVer (KERNEL.167)
1183 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1185 NE_MODULE *pModule = NE_GetPtr( hModule );
1186 return pModule ? pModule->expected_version : 0;
1190 /**********************************************************************
1191 * GetModuleFileName16 (KERNEL.49)
1193 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1198 if (!hModule) hModule = GetCurrentTask();
1199 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1200 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1201 TRACE(module, "%s\n", lpFileName );
1202 return strlen(lpFileName);
1206 /**********************************************************************
1207 * GetModuleHandle16 (KERNEL.47)
1209 * Find a module from a module name.
1213 * the win16 module handle if found
1215 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1216 * Always hFirstModule
1218 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1220 if (HIWORD(name) == 0)
1221 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1222 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name)), hFirstModule );
1225 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1227 HMODULE16 hModule = hFirstModule;
1228 LPCSTR filename, dotptr;
1229 BYTE len, *name_table;
1231 if (!(filename = strrchr( name, '\\' )))
1235 FIXME(module,"illegal usage of GetModuleHandle16\n");
1238 if ((dotptr = strrchr( filename, '.' )) != NULL)
1239 len = (BYTE)(dotptr - filename);
1240 else len = strlen( filename );
1244 NE_MODULE *pModule = NE_GetPtr( hModule );
1245 if (!pModule) break;
1248 modulepath = NE_MODULE_NAME(pModule);
1249 TRACE(module,"name %s modulepath %s\n",name,modulepath);
1250 if (!(modulename = strrchr( modulepath, '\\' )))
1251 modulename = modulepath;
1253 if (!lstrcmpiA( modulename, filename )) return hModule;
1256 name_table = (BYTE *)pModule + pModule->name_table;
1257 if ((*name_table == len) && !lstrncmpiA(filename, name_table+1, len))
1259 hModule = pModule->next;
1265 /**********************************************************************
1266 * ModuleFirst (TOOLHELP.59)
1268 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1270 lpme->wNext = hFirstModule;
1271 return ModuleNext16( lpme );
1275 /**********************************************************************
1276 * ModuleNext (TOOLHELP.60)
1278 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1283 if (!lpme->wNext) return FALSE;
1284 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1285 name = (char *)pModule + pModule->name_table;
1286 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1287 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1288 lpme->hModule = lpme->wNext;
1289 lpme->wcUsage = pModule->count;
1290 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1291 lpme->wNext = pModule->next;
1296 /**********************************************************************
1297 * ModuleFindName (TOOLHELP.61)
1299 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1301 lpme->wNext = GetModuleHandle16( name );
1302 return ModuleNext16( lpme );
1306 /**********************************************************************
1307 * ModuleFindHandle (TOOLHELP.62)
1309 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1311 hModule = GetExePtr( hModule );
1312 lpme->wNext = hModule;
1313 return ModuleNext16( lpme );
1315 /**********************************************************************
1317 * try to find a ne-module with the same path as a given name
1319 static HMODULE16 GetModuleFromPath(LPCSTR name)
1321 MODULEENTRY lookforit;
1323 DOS_FULL_NAME nametoload, nametocompare;
1328 if (!DOSFS_GetFullName(name,TRUE,&nametoload))
1329 /* we don't have a full qualified path */
1330 return GetModuleHandle16( name );
1331 lookforit.dwSize=sizeof(MODULEENTRY);
1332 for (ModuleFirst16(&lookforit);
1333 ModuleNext16(&lookforit);)
1335 pModule = NE_GetPtr( lookforit.hModule );
1338 modulepath = NE_MODULE_NAME(pModule);
1341 /* Only comparing the Unix path will give valid results*/
1342 if (DOSFS_GetFullName(modulepath,TRUE,&nametocompare))
1343 if (!strcmp(nametoload.long_name,nametocompare.long_name))
1344 return lookforit.hModule;
1351 /***************************************************************************
1352 * MapHModuleLS (KERNEL32.520)
1354 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod) {
1358 return ((TDB*)GlobalLock16(GetCurrentTask()))->hInstance;
1360 return hmod; /* we already have a 16 bit module handle */
1361 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
1363 if (pModule->module32 == hmod)
1364 return pModule->self;
1365 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
1370 /***************************************************************************
1371 * MapHModuleSL (KERNEL32.521)
1373 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod) {
1377 TDB *pTask = (TDB*)GlobalLock16(GetCurrentTask());
1379 hmod = pTask->hModule;
1381 pModule = (NE_MODULE*)GlobalLock16(hmod);
1382 if ( (pModule->magic!=IMAGE_OS2_SIGNATURE) ||
1383 !(pModule->flags & NE_FFLAGS_WIN32)
1386 return pModule->module32;
1389 /***************************************************************************
1390 * MapHInstLS (KERNEL32.516)
1392 REGS_ENTRYPOINT(MapHInstLS) {
1393 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1396 /***************************************************************************
1397 * MapHInstSL (KERNEL32.518)
1399 REGS_ENTRYPOINT(MapHInstSL) {
1400 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1403 /***************************************************************************
1404 * MapHInstLS_PN (KERNEL32.517)
1406 REGS_ENTRYPOINT(MapHInstLS_PN) {
1407 if (EAX_reg(context))
1408 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1411 /***************************************************************************
1412 * MapHInstSL_PN (KERNEL32.519)
1414 REGS_ENTRYPOINT(MapHInstSL_PN) {
1415 if (EAX_reg(context))
1416 EAX_reg(context) = MapHModuleSL(EAX_reg(context));
1419 /***************************************************************************
1420 * WIN16_MapHInstLS (KERNEL.472)
1422 VOID WINAPI WIN16_MapHInstLS( CONTEXT *context ) {
1423 EAX_reg(context) = MapHModuleLS(EAX_reg(context));
1426 /***************************************************************************
1427 * WIN16_MapHInstSL (KERNEL.473)
1429 VOID WINAPI WIN16_MapHInstSL( CONTEXT *context ) {
1430 EAX_reg(context) = MapHModuleSL(EAX_reg(context));