4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
38 #include "wine/winbase16.h"
41 #include "wine/library.h"
48 #include "builtin16.h"
49 #include "stackframe.h"
51 #include "wine/exception.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(module);
55 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
58 typedef struct _GPHANDLERDEF
70 struct ne_segment_table_entry_s
72 WORD seg_data_offset; /* Sector offset of segment data */
73 WORD seg_data_length; /* Length of segment data */
74 WORD seg_flags; /* Flags associated with this segment */
75 WORD min_alloc; /* Minimum allocation size for this */
78 #define hFirstModule (pThhook->hExeHead)
80 static NE_MODULE *pCachedModule = 0; /* Module cached by NE_OpenFile */
84 void *module_start; /* 32-bit address of the module data */
85 int module_size; /* Size of the module data */
86 void *code_start; /* 32-bit address of DLL code */
87 void *data_start; /* 32-bit address of DLL data */
88 const char *owner; /* 32-bit dll that contains this dll */
89 const void *rsrc; /* resources data */
90 } BUILTIN16_DESCRIPTOR;
92 /* Table of all built-in DLLs */
96 static const BUILTIN16_DESCRIPTOR *builtin_dlls[MAX_DLLS];
99 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
100 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
102 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
104 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
107 static WINE_EXCEPTION_FILTER(page_fault)
109 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
110 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
111 return EXCEPTION_EXECUTE_HANDLER;
112 return EXCEPTION_CONTINUE_SEARCH;
116 /* patch all the flat cs references of the code segment if necessary */
117 inline static void patch_code_segment( void *code_segment )
120 CALLFROM16 *call = code_segment;
121 if (call->flatcs == wine_get_cs()) return; /* nothing to patch */
122 while (call->pushl == 0x68)
124 call->flatcs = wine_get_cs();
131 /***********************************************************************
134 * Find a descriptor in the list
136 static const BUILTIN16_DESCRIPTOR *find_dll_descr( const char *dllname )
139 for (i = 0; i < MAX_DLLS; i++)
141 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
144 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
145 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
146 BYTE *name_table = (BYTE *)pModule + pModule->name_table;
148 /* check the dll file name */
149 if (!FILE_strcasecmp( pOfs->szPathName, dllname )) return descr;
150 /* check the dll module name (without extension) */
151 if (!FILE_strncasecmp( dllname, name_table+1, *name_table ) &&
152 !strcmp( dllname + *name_table, ".dll" ))
160 /***********************************************************************
163 * Check if a builtin dll descriptor is present (because we loaded its 32-bit counterpart).
165 static BOOL is_builtin_present( LPCSTR name )
167 char dllname[20], *p;
169 if (strlen(name) >= sizeof(dllname)-4) return FALSE;
170 strcpy( dllname, name );
171 p = strrchr( dllname, '.' );
172 if (!p) strcat( dllname, ".dll" );
173 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
175 return (find_dll_descr( dllname ) != NULL);
179 /***********************************************************************
180 * __wine_register_dll_16 (KERNEL32.@)
182 * Register a built-in DLL descriptor.
184 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR *descr )
188 for (i = 0; i < MAX_DLLS; i++)
190 if (builtin_dlls[i]) continue;
191 builtin_dlls[i] = descr;
194 assert( i < MAX_DLLS );
198 /***********************************************************************
199 * __wine_unregister_dll_16 (KERNEL32.@)
201 * Unregister a built-in DLL descriptor.
203 void __wine_unregister_dll_16( const BUILTIN16_DESCRIPTOR *descr )
207 for (i = 0; i < MAX_DLLS; i++)
209 if (builtin_dlls[i] != descr) continue;
210 builtin_dlls[i] = NULL;
216 /**********************************************************************
219 void NE_RegisterModule( NE_MODULE *pModule )
221 pModule->next = hFirstModule;
222 hFirstModule = pModule->self;
226 /***********************************************************************
229 void NE_DumpModule( HMODULE16 hModule )
239 if (!(pModule = NE_GetPtr( hModule )))
241 MESSAGE( "**** %04x is not a module handle\n", hModule );
245 /* Dump the module info */
247 DPRINTF( "Module %04x:\n", hModule );
248 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
249 pModule->count, pModule->flags,
250 pModule->heap_size, pModule->stack_size );
251 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
252 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
253 pModule->seg_count, pModule->modref_count );
254 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
255 pModule->os_flags, pModule->min_swap_area,
256 pModule->expected_version );
257 if (pModule->flags & NE_FFLAGS_WIN32)
258 DPRINTF( "PE module=%p\n", pModule->module32 );
260 /* Dump the file info */
262 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
264 /* Dump the segment table */
266 DPRINTF( "Segment table:\n" );
267 pSeg = NE_SEG_TABLE( pModule );
268 for (i = 0; i < pModule->seg_count; i++, pSeg++)
269 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
270 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
271 pSeg->minsize, pSeg->hSeg );
273 /* Dump the resource table */
275 DPRINTF( "Resource table:\n" );
276 if (pModule->res_table)
278 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
279 DPRINTF( "Alignment: %d\n", *pword++ );
282 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
283 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
284 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
285 for (i = 0; i < ptr->count; i++, pname++)
286 DPRINTF( "offset=%d len=%d id=%04x\n",
287 pname->offset, pname->length, pname->id );
288 pword = (WORD *)pname;
291 else DPRINTF( "None\n" );
293 /* Dump the resident name table */
295 DPRINTF( "Resident-name table:\n" );
296 pstr = (char *)pModule + pModule->name_table;
299 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
300 *(WORD *)(pstr + *pstr + 1) );
301 pstr += *pstr + 1 + sizeof(WORD);
304 /* Dump the module reference table */
306 DPRINTF( "Module ref table:\n" );
307 if (pModule->modref_table)
309 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
310 for (i = 0; i < pModule->modref_count; i++, pword++)
313 GetModuleName16( *pword, name, sizeof(name) );
314 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
317 else DPRINTF( "None\n" );
319 /* Dump the entry table */
321 DPRINTF( "Entry table:\n" );
322 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
324 entry = (ET_ENTRY *)((BYTE *)bundle+6);
325 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
326 ordinal = bundle->first;
327 while (ordinal < bundle->last)
329 if (entry->type == 0xff)
330 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
332 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
335 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
337 /* Dump the non-resident names table */
339 DPRINTF( "Non-resident names table:\n" );
340 if (pModule->nrname_handle)
342 pstr = (char *)GlobalLock16( pModule->nrname_handle );
345 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
346 *(WORD *)(pstr + *pstr + 1) );
347 pstr += *pstr + 1 + sizeof(WORD);
354 /***********************************************************************
357 * Walk the module list and print the modules.
359 void NE_WalkModules(void)
361 HMODULE16 hModule = hFirstModule;
362 MESSAGE( "Module Flags Name\n" );
365 NE_MODULE *pModule = NE_GetPtr( hModule );
368 MESSAGE( "Bad module %04x in list\n", hModule );
371 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->flags,
372 *((char *)pModule + pModule->name_table),
373 (char *)pModule + pModule->name_table + 1 );
374 hModule = pModule->next;
379 /***********************************************************************
380 * NE_InitResourceHandler
382 * Fill in 'resloader' fields in the resource table.
384 void NE_InitResourceHandler( NE_MODULE *pModule )
386 static FARPROC16 proc;
388 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
390 TRACE("InitResourceHandler[%04x]\n", pModule->self );
392 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
394 while(pTypeInfo->type_id)
396 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
397 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
402 /***********************************************************************
405 * Lookup the ordinal for a given name.
407 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
409 unsigned char buffer[256], *cpnt;
413 if (!(pModule = NE_GetPtr( hModule ))) return 0;
414 if (pModule->flags & NE_FFLAGS_WIN32) return 0;
416 TRACE("(%04x,'%s')\n", hModule, name );
418 /* First handle names of the form '#xxxx' */
420 if (name[0] == '#') return atoi( name + 1 );
422 /* Now copy and uppercase the string */
424 strcpy( buffer, name );
425 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = FILE_toupper(*cpnt);
428 /* First search the resident names */
430 cpnt = (char *)pModule + pModule->name_table;
432 /* Skip the first entry (module name) */
433 cpnt += *cpnt + 1 + sizeof(WORD);
436 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
439 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
440 TRACE(" Found: ordinal=%d\n", ordinal );
443 cpnt += *cpnt + 1 + sizeof(WORD);
446 /* Now search the non-resident names table */
448 if (!pModule->nrname_handle) return 0; /* No non-resident table */
449 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
451 /* Skip the first entry (module description string) */
452 cpnt += *cpnt + 1 + sizeof(WORD);
455 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
458 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
459 TRACE(" Found: ordinal=%d\n", ordinal );
462 cpnt += *cpnt + 1 + sizeof(WORD);
468 /***********************************************************************
471 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
473 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
476 /***********************************************************************
479 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
487 if (!(pModule = NE_GetPtr( hModule ))) return 0;
488 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
490 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
491 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
495 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
498 entry = (ET_ENTRY *)((BYTE *)bundle+6);
499 for (i=0; i < (ordinal - bundle->first - 1); i++)
503 memcpy( &offset, &entry->offs, sizeof(WORD) );
505 if (sel == 0xfe) sel = 0xffff; /* constant entry */
506 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
508 return (FARPROC16)MAKESEGPTR( sel, offset );
510 return (FARPROC16)MAKESEGPTR( sel, offset );
512 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
516 /***********************************************************************
517 * EntryAddrProc (KERNEL.667) Wine-specific export
519 * Return the entry point for a given ordinal.
521 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
523 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
524 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
528 /***********************************************************************
531 * Change the value of an entry point. Use with caution!
532 * It can only change the offset value, not the selector.
534 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
541 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
542 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
544 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
545 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
547 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
548 if (!(bundle->next)) return 0;
551 entry = (ET_ENTRY *)((BYTE *)bundle+6);
552 for (i=0; i < (ordinal - bundle->first - 1); i++)
555 memcpy( &entry->offs, &offset, sizeof(WORD) );
560 /***********************************************************************
563 HANDLE NE_OpenFile( NE_MODULE *pModule )
568 TRACE("(%p)\n", pModule );
569 /* mjm - removed module caching because it keeps open file handles
570 thus preventing CDROMs from being ejected */
571 name = NE_MODULE_NAME( pModule );
572 if ((handle = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
573 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
574 MESSAGE( "Can't open file '%s' for module %04x\n", name, pModule->self );
575 TRACE("opened '%s' -> %p\n", name, handle);
580 /* wrapper for SetFilePointer and ReadFile */
581 static BOOL read_data( HANDLE handle, LONG offset, void *buffer, DWORD size )
585 if (SetFilePointer( handle, offset, NULL, SEEK_SET ) == INVALID_SET_FILE_POINTER) return FALSE;
586 if (!ReadFile( handle, buffer, size, &result, NULL )) return FALSE;
587 return (result == size);
590 /***********************************************************************
593 static HMODULE16 NE_LoadExeHeader( HANDLE handle, LPCSTR path )
595 IMAGE_DOS_HEADER mz_header;
596 IMAGE_OS2_HEADER ne_header;
600 BYTE *pData, *pTempEntryTable;
601 char *buffer, *fastload = NULL;
602 int fastload_offset = 0, fastload_length = 0;
604 ET_BUNDLE *bundle, *oldbundle;
607 /* Read a block from either the file or the fast-load area. */
608 #define READ(offset,size,buffer) \
609 ((fastload && ((offset) >= fastload_offset) && \
610 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
611 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
612 read_data( handle, (offset), (buffer), (size)))
614 if (!read_data( handle, 0, &mz_header, sizeof(mz_header)) ||
615 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
616 return (HMODULE16)11; /* invalid exe */
618 if (!read_data( handle, mz_header.e_lfanew, &ne_header, sizeof(ne_header) ))
619 return (HMODULE16)11; /* invalid exe */
621 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
622 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
623 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
624 return (HMODULE16)12;
626 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
628 /* We now have a valid NE header */
630 size = sizeof(NE_MODULE) +
632 ne_header.ne_cseg * sizeof(SEGTABLEENTRY) +
634 ne_header.ne_restab - ne_header.ne_rsrctab +
635 /* resident names table */
636 ne_header.ne_modtab - ne_header.ne_restab +
637 /* module ref table */
638 ne_header.ne_cmod * sizeof(WORD) +
639 /* imported names table */
640 ne_header.ne_enttab - ne_header.ne_imptab +
641 /* entry table length */
642 ne_header.ne_cbenttab +
643 /* entry table extra conversion space */
645 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6) +
646 /* loaded file info */
647 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
649 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
650 if (!hModule) return (HMODULE16)11; /* invalid exe */
652 FarSetOwner16( hModule, hModule );
653 pModule = (NE_MODULE *)GlobalLock16( hModule );
654 memcpy( pModule, &ne_header, sizeof(ne_header) );
656 /* check *programs* for default minimal stack size */
657 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
658 && (pModule->stack_size < 0x1400) )
659 pModule->stack_size = 0x1400;
660 pModule->module32 = 0;
661 pModule->self = hModule;
662 pModule->self_loading_sel = 0;
663 pData = (BYTE *)(pModule + 1);
665 /* Clear internal Wine flags in case they are set in the EXE file */
667 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
669 /* Read the fast-load area */
671 if (ne_header.ne_flagsothers & NE_AFLAGS_FASTLOAD)
673 fastload_offset=ne_header.ne_pretthunks << ne_header.ne_align;
674 fastload_length=ne_header.ne_psegrefbytes << ne_header.ne_align;
675 TRACE("Using fast-load area offset=%x len=%d\n",
676 fastload_offset, fastload_length );
677 if ((fastload = HeapAlloc( GetProcessHeap(), 0, fastload_length )) != NULL)
679 if (!read_data( handle, fastload_offset, fastload, fastload_length))
681 HeapFree( GetProcessHeap(), 0, fastload );
682 WARN("Error reading fast-load area!\n");
688 /* Get the segment table */
690 pModule->seg_table = pData - (BYTE *)pModule;
691 buffer = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cseg *
692 sizeof(struct ne_segment_table_entry_s));
696 struct ne_segment_table_entry_s *pSeg;
698 if (!READ( mz_header.e_lfanew + ne_header.ne_segtab,
699 ne_header.ne_cseg * sizeof(struct ne_segment_table_entry_s),
702 HeapFree( GetProcessHeap(), 0, buffer );
703 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
704 GlobalFree16( hModule );
705 return (HMODULE16)11; /* invalid exe */
707 pSeg = (struct ne_segment_table_entry_s *)buffer;
708 for (i = ne_header.ne_cseg; i > 0; i--, pSeg++)
710 memcpy( pData, pSeg, sizeof(*pSeg) );
711 pData += sizeof(SEGTABLEENTRY);
713 HeapFree( GetProcessHeap(), 0, buffer );
717 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
718 GlobalFree16( hModule );
719 return (HMODULE16)11; /* invalid exe */
722 /* Get the resource table */
724 if (ne_header.ne_rsrctab < ne_header.ne_restab)
726 pModule->res_table = pData - (BYTE *)pModule;
727 if (!READ(mz_header.e_lfanew + ne_header.ne_rsrctab,
728 ne_header.ne_restab - ne_header.ne_rsrctab,
730 return (HMODULE16)11; /* invalid exe */
731 pData += ne_header.ne_restab - ne_header.ne_rsrctab;
732 NE_InitResourceHandler( pModule );
734 else pModule->res_table = 0; /* No resource table */
736 /* Get the resident names table */
738 pModule->name_table = pData - (BYTE *)pModule;
739 if (!READ( mz_header.e_lfanew + ne_header.ne_restab,
740 ne_header.ne_modtab - ne_header.ne_restab,
743 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
744 GlobalFree16( hModule );
745 return (HMODULE16)11; /* invalid exe */
747 pData += ne_header.ne_modtab - ne_header.ne_restab;
749 /* Get the module references table */
751 if (ne_header.ne_cmod > 0)
753 pModule->modref_table = pData - (BYTE *)pModule;
754 if (!READ( mz_header.e_lfanew + ne_header.ne_modtab,
755 ne_header.ne_cmod * sizeof(WORD),
758 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
759 GlobalFree16( hModule );
760 return (HMODULE16)11; /* invalid exe */
762 pData += ne_header.ne_cmod * sizeof(WORD);
764 else pModule->modref_table = 0; /* No module references */
766 /* Get the imported names table */
768 pModule->import_table = pData - (BYTE *)pModule;
769 if (!READ( mz_header.e_lfanew + ne_header.ne_imptab,
770 ne_header.ne_enttab - ne_header.ne_imptab,
773 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
774 GlobalFree16( hModule );
775 return (HMODULE16)11; /* invalid exe */
777 pData += ne_header.ne_enttab - ne_header.ne_imptab;
779 /* Load entry table, convert it to the optimized version used by Windows */
781 if ((pTempEntryTable = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cbenttab)) != NULL)
783 BYTE nr_entries, type, *s;
785 TRACE("Converting entry table.\n");
786 pModule->entry_table = pData - (BYTE *)pModule;
787 if (!READ( mz_header.e_lfanew + ne_header.ne_enttab,
788 ne_header.ne_cbenttab, pTempEntryTable ))
790 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
791 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
792 GlobalFree16( hModule );
793 return (HMODULE16)11; /* invalid exe */
797 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header.ne_enttab, ne_header.ne_cbenttab, *s);
799 bundle = (ET_BUNDLE *)pData;
800 TRACE("first bundle: %p\n", bundle);
801 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
802 entry = (ET_ENTRY *)((BYTE *)bundle+6);
804 while ((nr_entries = *s++))
808 bundle->last += nr_entries;
815 entry->segnum = *s++;
816 entry->offs = *(WORD *)s; s += 2;
817 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
825 entry->segnum = type;
826 entry->offs = *(WORD *)s; s += 2;
827 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
833 if (bundle->first == bundle->last)
835 bundle->first += nr_entries;
836 bundle->last += nr_entries;
841 oldbundle->next = ((int)entry - (int)pModule);
842 bundle = (ET_BUNDLE *)entry;
843 TRACE("new bundle: %p\n", bundle);
844 bundle->first = bundle->last =
845 oldbundle->last + nr_entries;
847 (BYTE *)entry += sizeof(ET_BUNDLE);
851 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
855 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
856 GlobalFree16( hModule );
857 return (HMODULE16)11; /* invalid exe */
860 pData += ne_header.ne_cbenttab + sizeof(ET_BUNDLE) +
861 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6);
863 if ((DWORD)entry > (DWORD)pData)
864 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
866 /* Store the filename information */
868 pModule->fileinfo = pData - (BYTE *)pModule;
869 size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
870 ofs = (OFSTRUCT *)pData;
871 ofs->cBytes = size - 1;
873 strcpy( ofs->szPathName, path );
876 /* Free the fast-load area */
879 if (fastload) HeapFree( GetProcessHeap(), 0, fastload );
881 /* Get the non-resident names table */
883 if (ne_header.ne_cbnrestab)
885 pModule->nrname_handle = GlobalAlloc16( 0, ne_header.ne_cbnrestab );
886 if (!pModule->nrname_handle)
888 GlobalFree16( hModule );
889 return (HMODULE16)11; /* invalid exe */
891 FarSetOwner16( pModule->nrname_handle, hModule );
892 buffer = GlobalLock16( pModule->nrname_handle );
893 if (!read_data( handle, ne_header.ne_nrestab, buffer, ne_header.ne_cbnrestab ))
895 GlobalFree16( pModule->nrname_handle );
896 GlobalFree16( hModule );
897 return (HMODULE16)11; /* invalid exe */
900 else pModule->nrname_handle = 0;
902 /* Allocate a segment for the implicitly-loaded DLLs */
904 if (pModule->modref_count)
906 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
907 (pModule->modref_count+1)*sizeof(HMODULE16) );
908 if (!pModule->dlls_to_init)
910 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
911 GlobalFree16( hModule );
912 return (HMODULE16)11; /* invalid exe */
914 FarSetOwner16( pModule->dlls_to_init, hModule );
916 else pModule->dlls_to_init = 0;
918 NE_RegisterModule( pModule );
919 SNOOP16_RegisterDLL(pModule,path);
924 /***********************************************************************
927 * Load all DLLs implicitly linked to a module.
929 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
932 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
933 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
935 for (i = 0; i < pModule->modref_count; i++, pModRef++)
937 char buffer[260], *p;
938 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
939 memcpy( buffer, pstr + 1, *pstr );
940 *(buffer + *pstr) = 0; /* terminate it */
942 TRACE("Loading '%s'\n", buffer );
943 if (!(*pModRef = GetModuleHandle16( buffer )))
945 /* If the DLL is not loaded yet, load it and store */
946 /* its handle in the list of DLLs to initialize. */
949 /* Append .DLL to name if no extension present */
950 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
951 strcat( buffer, ".DLL" );
953 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
955 /* FIXME: cleanup what was done */
957 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
958 buffer, *((BYTE*)pModule + pModule->name_table),
959 (char *)pModule + pModule->name_table + 1, hDLL );
962 *pModRef = GetExePtr( hDLL );
965 else /* Increment the reference count of the DLL */
967 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
968 if (pOldDLL) pOldDLL->count++;
975 /**********************************************************************
978 * Load first instance of NE module from file.
980 * pModule must point to a module structure prepared by NE_LoadExeHeader.
981 * This routine must never be called twice on a module.
984 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
986 /* Allocate the segments for this module */
988 if (!NE_CreateAllSegments( pModule ))
989 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
991 /* Load the referenced DLLs */
993 if (!NE_LoadDLLs( pModule ))
994 return ERROR_FILE_NOT_FOUND; /* 2 */
996 /* Load the segments */
998 NE_LoadAllSegments( pModule );
1000 /* Make sure the usage count is 1 on the first loading of */
1001 /* the module, even if it contains circular DLL references */
1005 return NE_GetInstance( pModule );
1008 /**********************************************************************
1011 * Load first instance of NE module. (Note: caller is responsible for
1012 * ensuring the module isn't already loaded!)
1014 * If the module turns out to be an executable module, only a
1015 * handle to a module stub is returned; this needs to be initialized
1016 * by calling NE_DoLoadModule later, in the context of the newly
1019 * If lib_only is TRUE, however, the module is perforce treated
1020 * like a DLL module, even if it is an executable module.
1023 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
1027 HINSTANCE16 hInstance;
1032 if ((hFile = OpenFile16( name, &ofs, OF_READ )) == HFILE_ERROR16)
1033 return (HMODULE16)2; /* File not found */
1035 hModule = NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile), ofs.szPathName );
1037 if (hModule < 32) return hModule;
1039 pModule = NE_GetPtr( hModule );
1040 if ( !pModule ) return hModule;
1042 if ( !lib_only && !( pModule->flags & NE_FFLAGS_LIBMODULE ) )
1045 hInstance = NE_DoLoadModule( pModule );
1046 if ( hInstance < 32 )
1049 NE_FreeModule( hModule, 0 );
1056 /***********************************************************************
1057 * NE_DoLoadBuiltinModule
1059 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1061 static HMODULE16 NE_DoLoadBuiltinModule( const BUILTIN16_DESCRIPTOR *descr )
1065 SEGTABLEENTRY *pSegTable;
1068 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
1069 descr->module_size, 0, WINE_LDT_FLAGS_DATA );
1070 if (!hModule) return 0;
1071 FarSetOwner16( hModule, hModule );
1073 pModule = (NE_MODULE *)GlobalLock16( hModule );
1074 pModule->self = hModule;
1075 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
1076 pModule->hRsrcMap = (void *)descr->rsrc;
1078 /* Allocate the code segment */
1080 pSegTable = NE_SEG_TABLE( pModule );
1081 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
1082 pSegTable->minsize, hModule,
1083 WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT );
1084 if (!pSegTable->hSeg) return 0;
1085 patch_code_segment( descr->code_start );
1088 /* Allocate the data segment */
1090 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
1091 minsize += pModule->heap_size;
1092 if (minsize > 0x10000) minsize = 0x10000;
1093 pSegTable->hSeg = GlobalAlloc16( GMEM_FIXED, minsize );
1094 if (!pSegTable->hSeg) return 0;
1095 FarSetOwner16( pSegTable->hSeg, hModule );
1096 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
1097 descr->data_start, pSegTable->minsize);
1098 if (pModule->heap_size)
1099 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg), pSegTable->minsize, minsize );
1101 if (descr->rsrc) NE_InitResourceHandler(pModule);
1103 NE_RegisterModule( pModule );
1105 /* make sure the 32-bit library containing this one is loaded too */
1106 LoadLibraryA( descr->owner );
1112 /***********************************************************************
1113 * NE_LoadBuiltinModule
1115 * Load a built-in module.
1117 static HMODULE16 NE_LoadBuiltinModule( LPCSTR name )
1119 const BUILTIN16_DESCRIPTOR *descr;
1120 char error[256], dllname[20], *p;
1124 /* Fix the name in case we have a full path and extension */
1126 if ((p = strrchr( name, '\\' ))) name = p + 1;
1127 if ((p = strrchr( name, '/' ))) name = p + 1;
1129 if (strlen(name) >= sizeof(dllname)-4) return (HMODULE16)2;
1131 strcpy( dllname, name );
1132 p = strrchr( dllname, '.' );
1133 if (!p) strcat( dllname, ".dll" );
1134 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
1136 if ((descr = find_dll_descr( dllname )))
1137 return NE_DoLoadBuiltinModule( descr );
1139 if ((handle = wine_dll_load( dllname, error, sizeof(error), &file_exists )))
1141 if ((descr = find_dll_descr( dllname )))
1142 return NE_DoLoadBuiltinModule( descr );
1144 ERR( "loaded .so but dll %s still not found\n", dllname );
1148 if (!file_exists) WARN("cannot open .so lib for 16-bit builtin %s: %s\n", name, error);
1149 else ERR("failed to load .so lib for 16-bit builtin %s: %s\n", name, error );
1151 return (HMODULE16)2;
1155 /**********************************************************************
1156 * MODULE_LoadModule16
1158 * Load a NE module in the order of the loadorder specification.
1159 * The caller is responsible that the module is not loaded already.
1162 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1164 HINSTANCE16 hinst = 2;
1165 enum loadorder_type loadorder[LOADORDER_NTYPES];
1167 const char *filetype = "";
1168 const char *ptr, *basename;
1170 /* strip path information */
1173 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1174 if ((ptr = strrchr( basename, '\\' ))) basename = ptr + 1;
1175 if ((ptr = strrchr( basename, '/' ))) basename = ptr + 1;
1177 if (is_builtin_present(basename))
1179 TRACE( "forcing loadorder to builtin for %s\n", debugstr_a(basename) );
1180 /* force builtin loadorder since the dll is already in memory */
1181 loadorder[0] = LOADORDER_BI;
1182 loadorder[1] = LOADORDER_INVALID;
1184 else MODULE_GetLoadOrder(loadorder, basename, FALSE);
1186 for(i = 0; i < LOADORDER_NTYPES; i++)
1188 if (loadorder[i] == LOADORDER_INVALID) break;
1190 switch(loadorder[i])
1193 TRACE("Trying native dll '%s'\n", libname);
1194 hinst = NE_LoadModule(libname, lib_only);
1195 filetype = "native";
1199 TRACE("Trying built-in '%s'\n", libname);
1200 hinst = NE_LoadBuiltinModule(libname);
1201 filetype = "builtin";
1211 TRACE_(loaddll)("Loaded module '%s' : %s\n", libname, filetype);
1217 hModule = GetModuleHandle16(libname);
1220 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1222 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1225 pModule = NE_GetPtr(hModule);
1228 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1230 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1233 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1236 * Call initialization routines for all loaded DLLs. Note that
1237 * when we load implicitly linked DLLs this will be done by InitTask().
1239 if(pModule->flags & NE_FFLAGS_LIBMODULE)
1241 NE_InitializeDLLs(hModule);
1242 NE_DllProcessAttach(hModule);
1250 /* We quit searching when we get another error than 'File not found' */
1254 return hinst; /* The last error that occurred */
1258 /**********************************************************************
1261 * Create the thread for a 16-bit module.
1263 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1268 HINSTANCE16 instance = 0;
1270 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1273 /* Post event to start the task */
1274 PostEvent16( hTask );
1276 /* Wait until we get the instance handle */
1279 DirectedYield16( hTask );
1280 if (!IsTask16( hTask )) /* thread has died */
1283 WaitForSingleObject( hThread, INFINITE );
1284 GetExitCodeThread( hThread, &exit_code );
1285 CloseHandle( hThread );
1288 if (!(pTask = GlobalLock16( hTask ))) break;
1289 instance = pTask->hInstance;
1290 GlobalUnlock16( hTask );
1291 } while (!instance);
1293 CloseHandle( hThread );
1298 /**********************************************************************
1299 * LoadModule (KERNEL.45)
1301 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1303 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1304 LOADPARAMS16 *params;
1312 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1314 /* Special case: second instance of an already loaded NE module */
1316 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
1317 if ( pModule->module32 ) return (HINSTANCE16)21;
1319 /* Increment refcount */
1325 /* Main case: load first instance of NE module */
1327 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1330 if ( !(pModule = NE_GetPtr( hModule )) )
1331 return (HINSTANCE16)11;
1334 /* If library module, we just retrieve the instance handle */
1336 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1337 return NE_GetInstance( pModule );
1340 * At this point, we need to create a new process.
1342 * pModule points either to an already loaded module, whose refcount
1343 * has already been incremented (to avoid having the module vanish
1344 * in the meantime), or else to a stub module which contains only header
1347 params = (LOADPARAMS16 *)paramBlock;
1348 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1349 cmdline = MapSL( params->cmdLine );
1350 return NE_CreateThread( pModule, cmdShow, cmdline );
1354 /**********************************************************************
1357 * Startup code for a new 16-bit task.
1359 DWORD NE_StartTask(void)
1361 TDB *pTask = TASK_GetCurrent();
1362 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1363 HINSTANCE16 hInstance, hPrevInstance;
1364 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1367 if ( pModule->count > 0 )
1369 /* Second instance of an already loaded NE module */
1370 /* Note that the refcount was already incremented by the parent */
1372 hPrevInstance = NE_GetInstance( pModule );
1374 if ( pModule->dgroup )
1375 if ( NE_CreateSegment( pModule, pModule->dgroup ) )
1376 NE_LoadSegment( pModule, pModule->dgroup );
1378 hInstance = NE_GetInstance( pModule );
1379 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->dgroup, hPrevInstance);
1384 /* Load first instance of NE module */
1386 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1388 hInstance = NE_DoLoadModule( pModule );
1392 if ( hInstance >= 32 )
1396 /* Enter instance handles into task struct */
1398 pTask->hInstance = hInstance;
1399 pTask->hPrevInstance = hPrevInstance;
1401 /* Use DGROUP for 16-bit stack */
1403 if (!(sp = pModule->sp))
1404 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
1406 sp -= sizeof(STACK16FRAME);
1407 NtCurrentTeb()->cur_stack = MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1409 /* Registers at initialization must be:
1411 * bx stack size in bytes
1412 * cx heap size in bytes
1413 * si previous app instance
1414 * di current app instance
1416 * es selector to the PSP
1417 * ds dgroup of the application
1419 * sp top of the stack
1421 memset( &context, 0, sizeof(context) );
1422 context.SegCs = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
1423 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1424 context.SegEs = pTask->hPDB;
1425 context.Eip = pModule->ip;
1426 context.Ebx = pModule->stack_size;
1427 context.Ecx = pModule->heap_size;
1428 context.Edi = pTask->hInstance;
1429 context.Esi = pTask->hPrevInstance;
1431 /* Now call 16-bit entry point */
1433 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1434 context.SegCs, context.Eip, context.SegDs,
1435 SELECTOROF(NtCurrentTeb()->cur_stack),
1436 OFFSETOF(NtCurrentTeb()->cur_stack) );
1438 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1439 ExitThread( LOWORD(context.Eax) );
1441 return hInstance; /* error code */
1444 /***********************************************************************
1445 * LoadLibrary (KERNEL.95)
1446 * LoadLibrary16 (KERNEL32.35)
1448 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1450 return LoadModule16(libname, (LPVOID)-1 );
1454 /**********************************************************************
1457 * Call a DLL's WEP, allowing it to shut down.
1458 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1460 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1463 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1464 if (!WEP) return FALSE;
1471 args[0] = WEP_FREE_DLL;
1472 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1473 ret = LOWORD(dwRet);
1475 __EXCEPT(page_fault)
1477 WARN("Page fault\n");
1486 /**********************************************************************
1489 * Implementation of FreeModule16().
1491 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1493 HMODULE16 *hPrevModule;
1498 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1499 hModule = pModule->self;
1501 TRACE("%04x count %d\n", hModule, pModule->count );
1503 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1504 else pModule->count = 0;
1506 if (pModule->flags & NE_FFLAGS_BUILTIN)
1507 return FALSE; /* Can't free built-in module */
1509 if (call_wep && !(pModule->flags & NE_FFLAGS_WIN32))
1511 /* Free the objects owned by the DLL module */
1512 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1514 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1515 MODULE_CallWEP( hModule );
1517 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1521 /* Clear magic number just in case */
1523 pModule->magic = pModule->self = 0;
1525 /* Remove it from the linked list */
1527 hPrevModule = &hFirstModule;
1528 while (*hPrevModule && (*hPrevModule != hModule))
1530 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1532 if (*hPrevModule) *hPrevModule = pModule->next;
1534 /* Free the referenced modules */
1536 pModRef = (HMODULE16*)NE_MODULE_TABLE( pModule );
1537 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1539 NE_FreeModule( *pModRef, call_wep );
1542 /* Free the module storage */
1544 GlobalFreeAll16( hModule );
1546 /* Remove module from cache */
1548 if (pCachedModule == pModule) pCachedModule = NULL;
1553 /**********************************************************************
1554 * FreeModule (KERNEL.46)
1556 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1558 return NE_FreeModule( hModule, TRUE );
1562 /***********************************************************************
1563 * FreeLibrary (KERNEL.96)
1564 * FreeLibrary16 (KERNEL32.36)
1566 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1568 TRACE("%04x\n", handle );
1569 FreeModule16( handle );
1573 /**********************************************************************
1574 * GetModuleName (KERNEL.27)
1576 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1581 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1582 p = (BYTE *)pModule + pModule->name_table;
1583 if (count > *p) count = *p + 1;
1586 memcpy( buf, p + 1, count - 1 );
1587 buf[count-1] = '\0';
1593 /**********************************************************************
1594 * GetModuleUsage (KERNEL.48)
1596 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1598 NE_MODULE *pModule = NE_GetPtr( hModule );
1599 return pModule ? pModule->count : 0;
1603 /**********************************************************************
1604 * GetExpWinVer (KERNEL.167)
1606 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1608 NE_MODULE *pModule = NE_GetPtr( hModule );
1609 if ( !pModule ) return 0;
1612 * For built-in modules, fake the expected version the module should
1613 * have according to the Windows version emulated by Wine
1615 if ( !pModule->expected_version )
1617 OSVERSIONINFOA versionInfo;
1618 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1620 if ( GetVersionExA( &versionInfo ) )
1621 pModule->expected_version =
1622 (versionInfo.dwMajorVersion & 0xff) << 8
1623 | (versionInfo.dwMinorVersion & 0xff);
1626 return pModule->expected_version;
1630 /***********************************************************************
1631 * WinExec (KERNEL.166)
1633 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1635 LPCSTR p, args = NULL;
1636 LPCSTR name_beg, name_end;
1637 LPSTR name, cmdline;
1640 char buffer[MAX_PATH];
1642 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1644 name_beg = lpCmdLine+1;
1645 p = strchr ( lpCmdLine+1, '"' );
1649 args = strchr ( p, ' ' );
1651 else /* yes, even valid with trailing '"' missing */
1652 name_end = lpCmdLine+strlen(lpCmdLine);
1656 name_beg = lpCmdLine;
1657 args = strchr( lpCmdLine, ' ' );
1658 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1661 if ((name_beg == lpCmdLine) && (!args))
1662 { /* just use the original cmdline string as file name */
1663 name = (LPSTR)lpCmdLine;
1667 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1668 return ERROR_NOT_ENOUGH_MEMORY;
1669 memcpy( name, name_beg, name_end - name_beg );
1670 name[name_end - name_beg] = '\0';
1676 arglen = strlen(args);
1677 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1678 cmdline[0] = (BYTE)arglen;
1679 strcpy( cmdline + 1, args );
1683 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1684 cmdline[0] = cmdline[1] = 0;
1687 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1689 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1691 LOADPARAMS16 params;
1694 showCmd[1] = nCmdShow;
1696 params.hEnvironment = 0;
1697 params.cmdLine = MapLS( cmdline );
1698 params.showCmd = MapLS( showCmd );
1699 params.reserved = 0;
1701 ret = LoadModule16( buffer, ¶ms );
1702 UnMapLS( params.cmdLine );
1703 UnMapLS( params.showCmd );
1705 else ret = GetLastError();
1707 HeapFree( GetProcessHeap(), 0, cmdline );
1708 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1710 if (ret == 21) /* 32-bit module */
1713 ReleaseThunkLock( &count );
1714 ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) );
1715 RestoreThunkLock( count );
1720 /***********************************************************************
1721 * GetProcAddress (KERNEL.50)
1723 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1728 if (!hModule) hModule = GetCurrentTask();
1729 hModule = GetExePtr( hModule );
1731 if (HIWORD(name) != 0)
1733 ordinal = NE_GetOrdinal( hModule, name );
1734 TRACE("%04x '%s'\n", hModule, name );
1738 ordinal = LOWORD(name);
1739 TRACE("%04x %04x\n", hModule, ordinal );
1741 if (!ordinal) return (FARPROC16)0;
1743 ret = NE_GetEntryPoint( hModule, ordinal );
1745 TRACE("returning %08x\n", (UINT)ret );
1750 /***************************************************************************
1751 * HasGPHandler (KERNEL.338)
1753 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1758 GPHANDLERDEF *gpHandler;
1760 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1761 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1762 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1763 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1764 && (gpHandler = MapSL( gpPtr )) != NULL )
1766 while (gpHandler->selector)
1768 if ( SELECTOROF(address) == gpHandler->selector
1769 && OFFSETOF(address) >= gpHandler->rangeStart
1770 && OFFSETOF(address) < gpHandler->rangeEnd )
1771 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1780 /**********************************************************************
1781 * GetModuleHandle (KERNEL.47)
1783 * Find a module from a module name.
1785 * NOTE: The current implementation works the same way the Windows 95 one
1786 * does. Do not try to 'fix' it, fix the callers.
1787 * + It does not do ANY extension handling (except that strange .EXE bit)!
1788 * + It does not care about paths, just about basenames. (same as Windows)
1792 * the win16 module handle if found
1794 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1795 * Always hFirstModule
1797 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1799 if (HIWORD(name) == 0)
1800 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1801 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1804 /**********************************************************************
1805 * NE_GetModuleByFilename
1807 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1811 BYTE len, *name_table;
1812 char tmpstr[MAX_PATH];
1815 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1817 /* If the base filename of 'name' matches the base filename of the module
1818 * filename of some module (case-insensitive compare):
1819 * Return its handle.
1822 /* basename: search backwards in passed name to \ / or : */
1823 s = tmpstr + strlen(tmpstr);
1826 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1831 /* search this in loaded filename list */
1832 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1837 pModule = NE_GetPtr( hModule );
1838 if (!pModule) break;
1839 if (!pModule->fileinfo) continue;
1840 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1842 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1843 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1844 /* basename: search backwards in pathname to \ / or : */
1845 while (loadedfn > (char*)ofs->szPathName)
1847 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1851 /* case insensitive compare ... */
1852 if (!FILE_strcasecmp(loadedfn, s))
1855 /* If basename (without ext) matches the module name of a module:
1856 * Return its handle.
1859 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1862 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1864 pModule = NE_GetPtr( hModule );
1865 if (!pModule) break;
1866 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1868 name_table = (BYTE *)pModule + pModule->name_table;
1869 if ((*name_table == len) && !FILE_strncasecmp(s, name_table+1, len))
1876 /***********************************************************************
1877 * GetProcAddress16 (KERNEL32.37)
1878 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1880 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1882 if (!hModule) return 0;
1883 if (HIWORD(hModule))
1885 WARN("hModule is Win32 handle (%p)\n", hModule );
1888 return GetProcAddress16( LOWORD(hModule), name );
1891 /**********************************************************************
1892 * ModuleFirst (TOOLHELP.59)
1894 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1896 lpme->wNext = hFirstModule;
1897 return ModuleNext16( lpme );
1901 /**********************************************************************
1902 * ModuleNext (TOOLHELP.60)
1904 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1909 if (!lpme->wNext) return FALSE;
1910 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1911 name = (char *)pModule + pModule->name_table;
1912 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1913 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1914 lpme->hModule = lpme->wNext;
1915 lpme->wcUsage = pModule->count;
1916 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1917 lpme->wNext = pModule->next;
1922 /**********************************************************************
1923 * ModuleFindName (TOOLHELP.61)
1925 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1927 lpme->wNext = GetModuleHandle16( name );
1928 return ModuleNext16( lpme );
1932 /**********************************************************************
1933 * ModuleFindHandle (TOOLHELP.62)
1935 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1937 hModule = GetExePtr( hModule );
1938 lpme->wNext = hModule;
1939 return ModuleNext16( lpme );
1943 /***************************************************************************
1944 * IsRomModule (KERNEL.323)
1946 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1951 /***************************************************************************
1952 * IsRomFile (KERNEL.326)
1954 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1959 /***********************************************************************
1960 * create_dummy_module
1962 * Create a dummy NE module for Win32 or Winelib.
1964 static HMODULE16 create_dummy_module( HMODULE module32 )
1968 SEGTABLEENTRY *pSegment;
1971 const char* basename;
1974 char filename[MAX_PATH];
1975 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
1977 if (!nt) return (HMODULE16)11; /* invalid exe */
1979 /* Extract base filename */
1980 GetModuleFileNameA( module32, filename, sizeof(filename) );
1981 basename = strrchr(filename, '\\');
1982 if (!basename) basename = filename;
1984 len = strlen(basename);
1985 if ((s = strchr(basename, '.'))) len = s - basename;
1987 /* Allocate module */
1988 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
1989 + strlen(filename) + 1;
1990 size = sizeof(NE_MODULE) +
1991 /* loaded file info */
1992 ((of_size + 3) & ~3) +
1993 /* segment table: DS,CS */
1994 2 * sizeof(SEGTABLEENTRY) +
1997 /* several empty tables */
2000 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2001 if (!hModule) return (HMODULE16)11; /* invalid exe */
2003 FarSetOwner16( hModule, hModule );
2004 pModule = (NE_MODULE *)GlobalLock16( hModule );
2006 /* Set all used entries */
2007 pModule->magic = IMAGE_OS2_SIGNATURE;
2010 pModule->flags = NE_FFLAGS_WIN32;
2011 pModule->dgroup = 0;
2014 pModule->heap_size = 0;
2015 pModule->stack_size = 0;
2016 pModule->seg_count = 2;
2017 pModule->modref_count = 0;
2018 pModule->nrname_size = 0;
2019 pModule->fileinfo = sizeof(NE_MODULE);
2020 pModule->os_flags = NE_OSFLAGS_WINDOWS;
2021 pModule->self = hModule;
2022 pModule->module32 = module32;
2024 /* Set version and flags */
2025 pModule->expected_version = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2026 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2027 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2028 pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2030 /* Set loaded file information */
2031 ofs = (OFSTRUCT *)(pModule + 1);
2032 memset( ofs, 0, of_size );
2033 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2034 strcpy( ofs->szPathName, filename );
2036 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2037 pModule->seg_table = (int)pSegment - (int)pModule;
2040 pSegment->flags = NE_SEGFLAGS_DATA;
2041 pSegment->minsize = 0x1000;
2044 pSegment->flags = 0;
2048 pStr = (char *)pSegment;
2049 pModule->name_table = (int)pStr - (int)pModule;
2052 lstrcpynA( pStr+1, basename, len+1 );
2055 /* All tables zero terminated */
2056 pModule->res_table = pModule->import_table = pModule->entry_table = (int)pStr - (int)pModule;
2058 NE_RegisterModule( pModule );
2059 LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2063 /***********************************************************************
2064 * PrivateLoadLibrary (KERNEL32.@)
2066 * FIXME: rough guesswork, don't know what "Private" means
2068 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2070 return LoadLibrary16(libname);
2073 /***********************************************************************
2074 * PrivateFreeLibrary (KERNEL32.@)
2076 * FIXME: rough guesswork, don't know what "Private" means
2078 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2080 FreeLibrary16(handle);
2083 /***************************************************************************
2084 * MapHModuleLS (KERNEL32.@)
2086 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2092 return TASK_GetCurrent()->hInstance;
2094 return LOWORD(hmod); /* we already have a 16 bit module handle */
2095 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
2097 if (pModule->module32 == hmod)
2098 return pModule->self;
2099 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
2101 if ((ret = create_dummy_module( hmod )) < 32)
2109 /***************************************************************************
2110 * MapHModuleSL (KERNEL32.@)
2112 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2117 TDB *pTask = TASK_GetCurrent();
2118 hmod = pTask->hModule;
2120 pModule = (NE_MODULE*)GlobalLock16(hmod);
2121 if ((pModule->magic!=IMAGE_OS2_SIGNATURE) || !(pModule->flags & NE_FFLAGS_WIN32))
2123 return pModule->module32;
2126 /***************************************************************************
2127 * MapHInstLS (KERNEL32.@)
2128 * MapHInstLS (KERNEL.472)
2130 void WINAPI MapHInstLS( CONTEXT86 *context )
2132 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2135 /***************************************************************************
2136 * MapHInstSL (KERNEL32.@)
2137 * MapHInstSL (KERNEL.473)
2139 void WINAPI MapHInstSL( CONTEXT86 *context )
2141 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2144 /***************************************************************************
2145 * MapHInstLS_PN (KERNEL32.@)
2147 void WINAPI MapHInstLS_PN( CONTEXT86 *context )
2149 if (context->Eax) context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2152 /***************************************************************************
2153 * MapHInstSL_PN (KERNEL32.@)
2155 void WINAPI MapHInstSL_PN( CONTEXT86 *context )
2157 if (context->Eax) context->Eax = (DWORD)MapHModuleSL( context->Eax );