Stub implementation for ReadDirectoryChangesW.
[wine/dcerpc.git] / dlls / kernel / ne_module.c
blobca4e61f4bc0a631fab066e16b2ff97dd6769fac8
1 /*
2 * NE modules
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <ctype.h>
35 #include "windef.h"
36 #include "wine/winbase16.h"
37 #include "wownt32.h"
38 #include "module.h"
39 #include "toolhelp.h"
40 #include "builtin16.h"
41 #include "stackframe.h"
42 #include "excpt.h"
43 #include "wine/exception.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(module);
47 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
49 #include "pshpack1.h"
50 typedef struct _GPHANDLERDEF
52 WORD selector;
53 WORD rangeStart;
54 WORD rangeEnd;
55 WORD handler;
56 } GPHANDLERDEF;
57 #include "poppack.h"
60 * Segment table entry
62 struct ne_segment_table_entry_s
64 WORD seg_data_offset; /* Sector offset of segment data */
65 WORD seg_data_length; /* Length of segment data */
66 WORD seg_flags; /* Flags associated with this segment */
67 WORD min_alloc; /* Minimum allocation size for this */
70 #define hFirstModule (pThhook->hExeHead)
72 typedef struct
74 void *module_start; /* 32-bit address of the module data */
75 int module_size; /* Size of the module data */
76 void *code_start; /* 32-bit address of DLL code */
77 void *data_start; /* 32-bit address of DLL data */
78 const char *owner; /* 32-bit dll that contains this dll */
79 const void *rsrc; /* resources data */
80 } BUILTIN16_DESCRIPTOR;
82 /* Table of all built-in DLLs */
84 #define MAX_DLLS 50
86 static const BUILTIN16_DESCRIPTOR *builtin_dlls[MAX_DLLS];
88 extern void SNOOP16_RegisterDLL(NE_MODULE*,LPCSTR);
89 extern FARPROC16 SNOOP16_GetProcAddress16(HMODULE16,DWORD,FARPROC16);
91 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
92 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
94 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
96 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
99 static WINE_EXCEPTION_FILTER(page_fault)
101 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
102 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
103 return EXCEPTION_EXECUTE_HANDLER;
104 return EXCEPTION_CONTINUE_SEARCH;
108 /* patch all the flat cs references of the code segment if necessary */
109 inline static void patch_code_segment( void *code_segment )
111 #ifdef __i386__
112 CALLFROM16 *call = code_segment;
113 if (call->flatcs == wine_get_cs()) return; /* nothing to patch */
114 while (call->pushl == 0x68)
116 call->flatcs = wine_get_cs();
117 call++;
119 #endif
123 /***********************************************************************
124 * NE_strcasecmp
126 * locale-independent case conversion for module lookups
128 static int NE_strcasecmp( const char *str1, const char *str2 )
130 int ret = 0;
131 for ( ; ; str1++, str2++)
132 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
133 return ret;
137 /***********************************************************************
138 * NE_strncasecmp
140 * locale-independent case conversion for module lookups
142 static int NE_strncasecmp( const char *str1, const char *str2, int len )
144 int ret = 0;
145 for ( ; len > 0; len--, str1++, str2++)
146 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
147 return ret;
151 /***********************************************************************
152 * find_dll_descr
154 * Find a descriptor in the list
156 static const BUILTIN16_DESCRIPTOR *find_dll_descr( const char *dllname )
158 int i;
159 for (i = 0; i < MAX_DLLS; i++)
161 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
162 if (descr)
164 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
165 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
166 BYTE *name_table = (BYTE *)pModule + pModule->name_table;
168 /* check the dll file name */
169 if (!NE_strcasecmp( pOfs->szPathName, dllname )) return descr;
170 /* check the dll module name (without extension) */
171 if (!NE_strncasecmp( dllname, name_table+1, *name_table ) &&
172 !strcmp( dllname + *name_table, ".dll" ))
173 return descr;
176 return NULL;
180 /***********************************************************************
181 * __wine_register_dll_16 (KERNEL32.@)
183 * Register a built-in DLL descriptor.
185 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR *descr )
187 int i;
189 for (i = 0; i < MAX_DLLS; i++)
191 if (builtin_dlls[i]) continue;
192 builtin_dlls[i] = descr;
193 break;
195 assert( i < MAX_DLLS );
199 /***********************************************************************
200 * __wine_unregister_dll_16 (KERNEL32.@)
202 * Unregister a built-in DLL descriptor.
204 void __wine_unregister_dll_16( const BUILTIN16_DESCRIPTOR *descr )
206 int i;
208 for (i = 0; i < MAX_DLLS; i++)
210 if (builtin_dlls[i] != descr) continue;
211 builtin_dlls[i] = NULL;
212 break;
217 /***********************************************************************
218 * NE_GetPtr
220 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
222 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
226 /**********************************************************************
227 * NE_RegisterModule
229 static void NE_RegisterModule( NE_MODULE *pModule )
231 pModule->next = hFirstModule;
232 hFirstModule = pModule->self;
236 /***********************************************************************
237 * NE_DumpModule
239 void NE_DumpModule( HMODULE16 hModule )
241 int i, ordinal;
242 SEGTABLEENTRY *pSeg;
243 BYTE *pstr;
244 WORD *pword;
245 NE_MODULE *pModule;
246 ET_BUNDLE *bundle;
247 ET_ENTRY *entry;
249 if (!(pModule = NE_GetPtr( hModule )))
251 MESSAGE( "**** %04x is not a module handle\n", hModule );
252 return;
255 /* Dump the module info */
256 DPRINTF( "---\n" );
257 DPRINTF( "Module %04x:\n", hModule );
258 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
259 pModule->count, pModule->flags,
260 pModule->heap_size, pModule->stack_size );
261 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
262 pModule->cs, pModule->ip, pModule->ss, pModule->sp, pModule->dgroup,
263 pModule->seg_count, pModule->modref_count );
264 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
265 pModule->os_flags, pModule->min_swap_area,
266 pModule->expected_version );
267 if (pModule->flags & NE_FFLAGS_WIN32)
268 DPRINTF( "PE module=%p\n", pModule->module32 );
270 /* Dump the file info */
271 DPRINTF( "---\n" );
272 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
274 /* Dump the segment table */
275 DPRINTF( "---\n" );
276 DPRINTF( "Segment table:\n" );
277 pSeg = NE_SEG_TABLE( pModule );
278 for (i = 0; i < pModule->seg_count; i++, pSeg++)
279 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
280 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
281 pSeg->minsize, pSeg->hSeg );
283 /* Dump the resource table */
284 DPRINTF( "---\n" );
285 DPRINTF( "Resource table:\n" );
286 if (pModule->res_table)
288 pword = (WORD *)((BYTE *)pModule + pModule->res_table);
289 DPRINTF( "Alignment: %d\n", *pword++ );
290 while (*pword)
292 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
293 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
294 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
295 for (i = 0; i < ptr->count; i++, pname++)
296 DPRINTF( "offset=%d len=%d id=%04x\n",
297 pname->offset, pname->length, pname->id );
298 pword = (WORD *)pname;
301 else DPRINTF( "None\n" );
303 /* Dump the resident name table */
304 DPRINTF( "---\n" );
305 DPRINTF( "Resident-name table:\n" );
306 pstr = (char *)pModule + pModule->name_table;
307 while (*pstr)
309 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
310 *(WORD *)(pstr + *pstr + 1) );
311 pstr += *pstr + 1 + sizeof(WORD);
314 /* Dump the module reference table */
315 DPRINTF( "---\n" );
316 DPRINTF( "Module ref table:\n" );
317 if (pModule->modref_table)
319 pword = (WORD *)((BYTE *)pModule + pModule->modref_table);
320 for (i = 0; i < pModule->modref_count; i++, pword++)
322 char name[10];
323 GetModuleName16( *pword, name, sizeof(name) );
324 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
327 else DPRINTF( "None\n" );
329 /* Dump the entry table */
330 DPRINTF( "---\n" );
331 DPRINTF( "Entry table:\n" );
332 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->entry_table);
333 do {
334 entry = (ET_ENTRY *)((BYTE *)bundle+6);
335 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
336 ordinal = bundle->first;
337 while (ordinal < bundle->last)
339 if (entry->type == 0xff)
340 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
341 else
342 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
343 entry++;
345 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
347 /* Dump the non-resident names table */
348 DPRINTF( "---\n" );
349 DPRINTF( "Non-resident names table:\n" );
350 if (pModule->nrname_handle)
352 pstr = (char *)GlobalLock16( pModule->nrname_handle );
353 while (*pstr)
355 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
356 *(WORD *)(pstr + *pstr + 1) );
357 pstr += *pstr + 1 + sizeof(WORD);
360 DPRINTF( "\n" );
364 /***********************************************************************
365 * NE_WalkModules
367 * Walk the module list and print the modules.
369 void NE_WalkModules(void)
371 HMODULE16 hModule = hFirstModule;
372 MESSAGE( "Module Flags Name\n" );
373 while (hModule)
375 NE_MODULE *pModule = NE_GetPtr( hModule );
376 if (!pModule)
378 MESSAGE( "Bad module %04x in list\n", hModule );
379 return;
381 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->flags,
382 *((char *)pModule + pModule->name_table),
383 (char *)pModule + pModule->name_table + 1 );
384 hModule = pModule->next;
389 /***********************************************************************
390 * NE_InitResourceHandler
392 * Fill in 'resloader' fields in the resource table.
394 static void NE_InitResourceHandler( NE_MODULE *pModule )
396 static FARPROC16 proc;
398 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
400 TRACE("InitResourceHandler[%04x]\n", pModule->self );
402 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
404 while(pTypeInfo->type_id)
406 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
407 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
412 /***********************************************************************
413 * NE_GetOrdinal
415 * Lookup the ordinal for a given name.
417 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
419 unsigned char buffer[256], *cpnt;
420 BYTE len;
421 NE_MODULE *pModule;
423 if (!(pModule = NE_GetPtr( hModule ))) return 0;
424 if (pModule->flags & NE_FFLAGS_WIN32) return 0;
426 TRACE("(%04x,'%s')\n", hModule, name );
428 /* First handle names of the form '#xxxx' */
430 if (name[0] == '#') return atoi( name + 1 );
432 /* Now copy and uppercase the string */
434 strcpy( buffer, name );
435 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = RtlUpperChar(*cpnt);
436 len = cpnt - buffer;
438 /* First search the resident names */
440 cpnt = (char *)pModule + pModule->name_table;
442 /* Skip the first entry (module name) */
443 cpnt += *cpnt + 1 + sizeof(WORD);
444 while (*cpnt)
446 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
448 WORD ordinal;
449 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
450 TRACE(" Found: ordinal=%d\n", ordinal );
451 return ordinal;
453 cpnt += *cpnt + 1 + sizeof(WORD);
456 /* Now search the non-resident names table */
458 if (!pModule->nrname_handle) return 0; /* No non-resident table */
459 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
461 /* Skip the first entry (module description string) */
462 cpnt += *cpnt + 1 + sizeof(WORD);
463 while (*cpnt)
465 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
467 WORD ordinal;
468 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
469 TRACE(" Found: ordinal=%d\n", ordinal );
470 return ordinal;
472 cpnt += *cpnt + 1 + sizeof(WORD);
474 return 0;
478 /***********************************************************************
479 * NE_GetEntryPoint
481 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
483 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
486 /***********************************************************************
487 * NE_GetEntryPointEx
489 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
491 NE_MODULE *pModule;
492 WORD sel, offset, i;
494 ET_ENTRY *entry;
495 ET_BUNDLE *bundle;
497 if (!(pModule = NE_GetPtr( hModule ))) return 0;
498 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
500 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
501 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
503 if (!(bundle->next))
504 return 0;
505 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
508 entry = (ET_ENTRY *)((BYTE *)bundle+6);
509 for (i=0; i < (ordinal - bundle->first - 1); i++)
510 entry++;
512 sel = entry->segnum;
513 memcpy( &offset, &entry->offs, sizeof(WORD) );
515 if (sel == 0xfe) sel = 0xffff; /* constant entry */
516 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
517 if (sel==0xffff)
518 return (FARPROC16)MAKESEGPTR( sel, offset );
519 if (!snoop)
520 return (FARPROC16)MAKESEGPTR( sel, offset );
521 else
522 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
526 /***********************************************************************
527 * EntryAddrProc (KERNEL.667) Wine-specific export
529 * Return the entry point for a given ordinal.
531 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
533 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
534 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
535 return ret;
538 /***********************************************************************
539 * NE_SetEntryPoint
541 * Change the value of an entry point. Use with caution!
542 * It can only change the offset value, not the selector.
544 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
546 NE_MODULE *pModule;
547 ET_ENTRY *entry;
548 ET_BUNDLE *bundle;
549 int i;
551 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
552 assert( !(pModule->flags & NE_FFLAGS_WIN32) );
554 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
555 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
557 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
558 if (!(bundle->next)) return 0;
561 entry = (ET_ENTRY *)((BYTE *)bundle+6);
562 for (i=0; i < (ordinal - bundle->first - 1); i++)
563 entry++;
565 memcpy( &entry->offs, &offset, sizeof(WORD) );
566 return TRUE;
570 /***********************************************************************
571 * NE_OpenFile
573 HANDLE NE_OpenFile( NE_MODULE *pModule )
575 HANDLE handle;
576 char *name = NE_MODULE_NAME( pModule );
578 TRACE("(%p)\n", pModule );
580 if (pModule->fd)
582 if (!DuplicateHandle( GetCurrentProcess(), pModule->fd,
583 GetCurrentProcess(), &handle, 0, FALSE,
584 DUPLICATE_SAME_ACCESS )) handle = INVALID_HANDLE_VALUE;
586 else
588 handle = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ,
589 NULL, OPEN_EXISTING, 0, 0 );
591 if (handle == INVALID_HANDLE_VALUE)
592 ERR( "Can't open file '%s' for module %04x\n", name, pModule->self );
594 TRACE("opened '%s' -> %p\n", name, handle);
595 return handle;
599 /* wrapper for SetFilePointer and ReadFile */
600 static BOOL read_data( HANDLE handle, LONG offset, void *buffer, DWORD size )
602 DWORD result;
604 if (SetFilePointer( handle, offset, NULL, SEEK_SET ) == INVALID_SET_FILE_POINTER) return FALSE;
605 if (!ReadFile( handle, buffer, size, &result, NULL )) return FALSE;
606 return (result == size);
609 /***********************************************************************
610 * NE_LoadExeHeader
612 static HMODULE16 NE_LoadExeHeader( HANDLE handle, LPCSTR path )
614 IMAGE_DOS_HEADER mz_header;
615 IMAGE_OS2_HEADER ne_header;
616 int size;
617 HMODULE16 hModule;
618 NE_MODULE *pModule;
619 BYTE *pData, *pTempEntryTable;
620 char *buffer, *fastload = NULL;
621 unsigned int fastload_offset = 0, fastload_length = 0;
622 ET_ENTRY *entry;
623 ET_BUNDLE *bundle, *oldbundle;
624 OFSTRUCT *ofs;
626 /* Read a block from either the file or the fast-load area. */
627 #define READ(offset,size,buffer) \
628 ((fastload && ((offset) >= fastload_offset) && \
629 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
630 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
631 read_data( handle, (offset), (buffer), (size)))
633 if (!read_data( handle, 0, &mz_header, sizeof(mz_header)) ||
634 (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
635 return (HMODULE16)11; /* invalid exe */
637 if (!read_data( handle, mz_header.e_lfanew, &ne_header, sizeof(ne_header) ))
638 return (HMODULE16)11; /* invalid exe */
640 if (ne_header.ne_magic == IMAGE_NT_SIGNATURE) return (HMODULE16)21; /* win32 exe */
641 if (ne_header.ne_magic == IMAGE_OS2_SIGNATURE_LX) {
642 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file!\n");
643 return (HMODULE16)12;
645 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE) return (HMODULE16)11; /* invalid exe */
647 /* We now have a valid NE header */
649 /* check to be able to fall back to loading OS/2 programs as DOS
650 * FIXME: should this check be reversed in order to be less strict?
651 * (only fail for OS/2 ne_exetyp 0x01 here?) */
652 if ((ne_header.ne_exetyp != 0x02 /* Windows */)
653 && (ne_header.ne_exetyp != 0x04) /* Windows 386 */)
654 return (HMODULE16)11; /* invalid exe */
656 size = sizeof(NE_MODULE) +
657 /* segment table */
658 ne_header.ne_cseg * sizeof(SEGTABLEENTRY) +
659 /* resource table */
660 ne_header.ne_restab - ne_header.ne_rsrctab +
661 /* resident names table */
662 ne_header.ne_modtab - ne_header.ne_restab +
663 /* module ref table */
664 ne_header.ne_cmod * sizeof(WORD) +
665 /* imported names table */
666 ne_header.ne_enttab - ne_header.ne_imptab +
667 /* entry table length */
668 ne_header.ne_cbenttab +
669 /* entry table extra conversion space */
670 sizeof(ET_BUNDLE) +
671 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6) +
672 /* loaded file info */
673 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
675 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
676 if (!hModule) return (HMODULE16)11; /* invalid exe */
678 FarSetOwner16( hModule, hModule );
679 pModule = (NE_MODULE *)GlobalLock16( hModule );
680 memcpy( pModule, &ne_header, sizeof(ne_header) );
681 pModule->count = 0;
682 /* check *programs* for default minimal stack size */
683 if ( (!(pModule->flags & NE_FFLAGS_LIBMODULE))
684 && (pModule->stack_size < 0x1400) )
685 pModule->stack_size = 0x1400;
686 pModule->module32 = 0;
687 pModule->self = hModule;
688 pModule->self_loading_sel = 0;
689 pData = (BYTE *)(pModule + 1);
691 /* Clear internal Wine flags in case they are set in the EXE file */
693 pModule->flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
695 /* Read the fast-load area */
697 if (ne_header.ne_flagsothers & NE_AFLAGS_FASTLOAD)
699 fastload_offset=ne_header.ne_pretthunks << ne_header.ne_align;
700 fastload_length=ne_header.ne_psegrefbytes << ne_header.ne_align;
701 TRACE("Using fast-load area offset=%x len=%d\n",
702 fastload_offset, fastload_length );
703 if ((fastload = HeapAlloc( GetProcessHeap(), 0, fastload_length )) != NULL)
705 if (!read_data( handle, fastload_offset, fastload, fastload_length))
707 HeapFree( GetProcessHeap(), 0, fastload );
708 WARN("Error reading fast-load area!\n");
709 fastload = NULL;
714 /* Get the segment table */
716 pModule->seg_table = pData - (BYTE *)pModule;
717 buffer = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cseg *
718 sizeof(struct ne_segment_table_entry_s));
719 if (buffer)
721 int i;
722 struct ne_segment_table_entry_s *pSeg;
724 if (!READ( mz_header.e_lfanew + ne_header.ne_segtab,
725 ne_header.ne_cseg * sizeof(struct ne_segment_table_entry_s),
726 buffer ))
728 HeapFree( GetProcessHeap(), 0, buffer );
729 HeapFree( GetProcessHeap(), 0, fastload );
730 GlobalFree16( hModule );
731 return (HMODULE16)11; /* invalid exe */
733 pSeg = (struct ne_segment_table_entry_s *)buffer;
734 for (i = ne_header.ne_cseg; i > 0; i--, pSeg++)
736 memcpy( pData, pSeg, sizeof(*pSeg) );
737 pData += sizeof(SEGTABLEENTRY);
739 HeapFree( GetProcessHeap(), 0, buffer );
741 else
743 HeapFree( GetProcessHeap(), 0, fastload );
744 GlobalFree16( hModule );
745 return (HMODULE16)11; /* invalid exe */
748 /* Get the resource table */
750 if (ne_header.ne_rsrctab < ne_header.ne_restab)
752 pModule->res_table = pData - (BYTE *)pModule;
753 if (!READ(mz_header.e_lfanew + ne_header.ne_rsrctab,
754 ne_header.ne_restab - ne_header.ne_rsrctab,
755 pData ))
756 return (HMODULE16)11; /* invalid exe */
757 pData += ne_header.ne_restab - ne_header.ne_rsrctab;
758 NE_InitResourceHandler( pModule );
760 else pModule->res_table = 0; /* No resource table */
762 /* Get the resident names table */
764 pModule->name_table = pData - (BYTE *)pModule;
765 if (!READ( mz_header.e_lfanew + ne_header.ne_restab,
766 ne_header.ne_modtab - ne_header.ne_restab,
767 pData ))
769 HeapFree( GetProcessHeap(), 0, fastload );
770 GlobalFree16( hModule );
771 return (HMODULE16)11; /* invalid exe */
773 pData += ne_header.ne_modtab - ne_header.ne_restab;
775 /* Get the module references table */
777 if (ne_header.ne_cmod > 0)
779 pModule->modref_table = pData - (BYTE *)pModule;
780 if (!READ( mz_header.e_lfanew + ne_header.ne_modtab,
781 ne_header.ne_cmod * sizeof(WORD),
782 pData ))
784 HeapFree( GetProcessHeap(), 0, fastload );
785 GlobalFree16( hModule );
786 return (HMODULE16)11; /* invalid exe */
788 pData += ne_header.ne_cmod * sizeof(WORD);
790 else pModule->modref_table = 0; /* No module references */
792 /* Get the imported names table */
794 pModule->import_table = pData - (BYTE *)pModule;
795 if (!READ( mz_header.e_lfanew + ne_header.ne_imptab,
796 ne_header.ne_enttab - ne_header.ne_imptab,
797 pData ))
799 HeapFree( GetProcessHeap(), 0, fastload );
800 GlobalFree16( hModule );
801 return (HMODULE16)11; /* invalid exe */
803 pData += ne_header.ne_enttab - ne_header.ne_imptab;
805 /* Load entry table, convert it to the optimized version used by Windows */
807 if ((pTempEntryTable = HeapAlloc( GetProcessHeap(), 0, ne_header.ne_cbenttab)) != NULL)
809 BYTE nr_entries, type, *s;
811 TRACE("Converting entry table.\n");
812 pModule->entry_table = pData - (BYTE *)pModule;
813 if (!READ( mz_header.e_lfanew + ne_header.ne_enttab,
814 ne_header.ne_cbenttab, pTempEntryTable ))
816 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
817 HeapFree( GetProcessHeap(), 0, fastload );
818 GlobalFree16( hModule );
819 return (HMODULE16)11; /* invalid exe */
822 s = pTempEntryTable;
823 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header.ne_enttab, ne_header.ne_cbenttab, *s);
825 bundle = (ET_BUNDLE *)pData;
826 TRACE("first bundle: %p\n", bundle);
827 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
828 entry = (ET_ENTRY *)((BYTE *)bundle+6);
830 while ((nr_entries = *s++))
832 if ((type = *s++))
834 bundle->last += nr_entries;
835 if (type == 0xff)
836 while (nr_entries--)
838 entry->type = type;
839 entry->flags = *s++;
840 s += 2;
841 entry->segnum = *s++;
842 entry->offs = *(WORD *)s; s += 2;
843 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
844 entry++;
846 else
847 while (nr_entries--)
849 entry->type = type;
850 entry->flags = *s++;
851 entry->segnum = type;
852 entry->offs = *(WORD *)s; s += 2;
853 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
854 entry++;
857 else
859 if (bundle->first == bundle->last)
861 bundle->first += nr_entries;
862 bundle->last += nr_entries;
864 else
866 oldbundle = bundle;
867 oldbundle->next = ((int)entry - (int)pModule);
868 bundle = (ET_BUNDLE *)entry;
869 TRACE("new bundle: %p\n", bundle);
870 bundle->first = bundle->last =
871 oldbundle->last + nr_entries;
872 bundle->next = 0;
873 entry = (ET_ENTRY*)(((BYTE*)entry)+sizeof(ET_BUNDLE));
877 HeapFree( GetProcessHeap(), 0, pTempEntryTable );
879 else
881 HeapFree( GetProcessHeap(), 0, fastload );
882 GlobalFree16( hModule );
883 return (HMODULE16)11; /* invalid exe */
886 pData += ne_header.ne_cbenttab + sizeof(ET_BUNDLE) +
887 2 * (ne_header.ne_cbenttab - ne_header.ne_cmovent*6);
889 if ((DWORD)entry > (DWORD)pData)
890 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry, pData);
892 /* Store the filename information */
894 pModule->fileinfo = pData - (BYTE *)pModule;
895 size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
896 ofs = (OFSTRUCT *)pData;
897 ofs->cBytes = size - 1;
898 ofs->fFixedDisk = 1;
899 strcpy( ofs->szPathName, path );
900 pData += size;
902 /* Free the fast-load area */
904 #undef READ
905 HeapFree( GetProcessHeap(), 0, fastload );
907 /* Get the non-resident names table */
909 if (ne_header.ne_cbnrestab)
911 pModule->nrname_handle = GlobalAlloc16( 0, ne_header.ne_cbnrestab );
912 if (!pModule->nrname_handle)
914 GlobalFree16( hModule );
915 return (HMODULE16)11; /* invalid exe */
917 FarSetOwner16( pModule->nrname_handle, hModule );
918 buffer = GlobalLock16( pModule->nrname_handle );
919 if (!read_data( handle, ne_header.ne_nrestab, buffer, ne_header.ne_cbnrestab ))
921 GlobalFree16( pModule->nrname_handle );
922 GlobalFree16( hModule );
923 return (HMODULE16)11; /* invalid exe */
926 else pModule->nrname_handle = 0;
928 /* Allocate a segment for the implicitly-loaded DLLs */
930 if (pModule->modref_count)
932 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
933 (pModule->modref_count+1)*sizeof(HMODULE16) );
934 if (!pModule->dlls_to_init)
936 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
937 GlobalFree16( hModule );
938 return (HMODULE16)11; /* invalid exe */
940 FarSetOwner16( pModule->dlls_to_init, hModule );
942 else pModule->dlls_to_init = 0;
944 NE_RegisterModule( pModule );
945 SNOOP16_RegisterDLL(pModule,path);
946 return hModule;
950 /***********************************************************************
951 * NE_LoadDLLs
953 * Load all DLLs implicitly linked to a module.
955 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
957 int i;
958 WORD *pModRef = (WORD *)((char *)pModule + pModule->modref_table);
959 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
961 for (i = 0; i < pModule->modref_count; i++, pModRef++)
963 char buffer[260], *p;
964 BYTE *pstr = (BYTE *)pModule + pModule->import_table + *pModRef;
965 memcpy( buffer, pstr + 1, *pstr );
966 *(buffer + *pstr) = 0; /* terminate it */
968 TRACE("Loading '%s'\n", buffer );
969 if (!(*pModRef = GetModuleHandle16( buffer )))
971 /* If the DLL is not loaded yet, load it and store */
972 /* its handle in the list of DLLs to initialize. */
973 HMODULE16 hDLL;
975 /* Append .DLL to name if no extension present */
976 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
977 strcat( buffer, ".DLL" );
979 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
981 /* FIXME: cleanup what was done */
983 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
984 buffer, *((BYTE*)pModule + pModule->name_table),
985 (char *)pModule + pModule->name_table + 1, hDLL );
986 return FALSE;
988 *pModRef = GetExePtr( hDLL );
989 *pDLLs++ = *pModRef;
991 else /* Increment the reference count of the DLL */
993 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
994 if (pOldDLL) pOldDLL->count++;
997 return TRUE;
1001 /**********************************************************************
1002 * NE_DoLoadModule
1004 * Load first instance of NE module from file.
1006 * pModule must point to a module structure prepared by NE_LoadExeHeader.
1007 * This routine must never be called twice on a module.
1010 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
1012 /* Allocate the segments for this module */
1014 if (!NE_CreateAllSegments( pModule ))
1015 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
1017 /* Load the referenced DLLs */
1019 if (!NE_LoadDLLs( pModule ))
1020 return ERROR_FILE_NOT_FOUND; /* 2 */
1022 /* Load the segments */
1024 NE_LoadAllSegments( pModule );
1026 /* Make sure the usage count is 1 on the first loading of */
1027 /* the module, even if it contains circular DLL references */
1029 pModule->count = 1;
1031 return NE_GetInstance( pModule );
1034 /**********************************************************************
1035 * NE_LoadModule
1037 * Load first instance of NE module. (Note: caller is responsible for
1038 * ensuring the module isn't already loaded!)
1040 * If the module turns out to be an executable module, only a
1041 * handle to a module stub is returned; this needs to be initialized
1042 * by calling NE_DoLoadModule later, in the context of the newly
1043 * created process.
1045 * If lib_only is TRUE, however, the module is perforce treated
1046 * like a DLL module, even if it is an executable module.
1049 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
1051 NE_MODULE *pModule;
1052 HMODULE16 hModule;
1053 HINSTANCE16 hInstance;
1054 HFILE16 hFile;
1055 OFSTRUCT ofs;
1056 UINT drive_type;
1058 /* Open file */
1059 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
1060 return ERROR_FILE_NOT_FOUND;
1062 hModule = NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile), ofs.szPathName );
1063 if (hModule < 32)
1065 _lclose16( hFile );
1066 return hModule;
1068 pModule = NE_GetPtr( hModule );
1070 drive_type = GetDriveTypeA( ofs.szPathName );
1071 if (drive_type != DRIVE_REMOVABLE && drive_type != DRIVE_CDROM)
1073 /* keep the file handle open if not on a removable media */
1074 DuplicateHandle( GetCurrentProcess(), DosFileHandleToWin32Handle(hFile),
1075 GetCurrentProcess(), &pModule->fd, 0, FALSE,
1076 DUPLICATE_SAME_ACCESS );
1078 _lclose16( hFile );
1080 if ( !lib_only && !( pModule->flags & NE_FFLAGS_LIBMODULE ) )
1081 return hModule;
1083 hInstance = NE_DoLoadModule( pModule );
1084 if ( hInstance < 32 )
1086 /* cleanup ... */
1087 NE_FreeModule( hModule, 0 );
1090 return hInstance;
1094 /***********************************************************************
1095 * NE_DoLoadBuiltinModule
1097 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1099 static HMODULE16 NE_DoLoadBuiltinModule( const BUILTIN16_DESCRIPTOR *descr )
1101 NE_MODULE *pModule;
1102 int minsize;
1103 SEGTABLEENTRY *pSegTable;
1104 HMODULE16 hModule;
1106 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
1107 descr->module_size, 0, WINE_LDT_FLAGS_DATA );
1108 if (!hModule) return ERROR_NOT_ENOUGH_MEMORY;
1109 FarSetOwner16( hModule, hModule );
1111 pModule = (NE_MODULE *)GlobalLock16( hModule );
1112 pModule->self = hModule;
1113 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
1114 pModule->hRsrcMap = (void *)descr->rsrc;
1116 /* Allocate the code segment */
1118 pSegTable = NE_SEG_TABLE( pModule );
1119 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
1120 pSegTable->minsize, hModule,
1121 WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT );
1122 if (!pSegTable->hSeg) return ERROR_NOT_ENOUGH_MEMORY;
1123 patch_code_segment( descr->code_start );
1124 pSegTable++;
1126 /* Allocate the data segment */
1128 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
1129 minsize += pModule->heap_size;
1130 if (minsize > 0x10000) minsize = 0x10000;
1131 pSegTable->hSeg = GlobalAlloc16( GMEM_FIXED, minsize );
1132 if (!pSegTable->hSeg) return ERROR_NOT_ENOUGH_MEMORY;
1133 FarSetOwner16( pSegTable->hSeg, hModule );
1134 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
1135 descr->data_start, pSegTable->minsize);
1136 if (pModule->heap_size)
1137 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg), pSegTable->minsize, minsize );
1139 if (descr->rsrc) NE_InitResourceHandler(pModule);
1141 NE_RegisterModule( pModule );
1143 return hModule;
1147 /**********************************************************************
1148 * MODULE_LoadModule16
1150 * Load a NE module in the order of the loadorder specification.
1151 * The caller is responsible that the module is not loaded already.
1154 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1156 HINSTANCE16 hinst = 2;
1157 HMODULE16 hModule;
1158 NE_MODULE *pModule;
1159 const BUILTIN16_DESCRIPTOR *descr = NULL;
1160 char dllname[20], owner[20], *p;
1161 const char *basename;
1163 /* strip path information */
1165 basename = libname;
1166 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1167 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
1168 if ((p = strrchr( basename, '/' ))) basename = p + 1;
1170 if (strlen(basename) < sizeof(dllname)-4)
1172 int file_exists;
1174 strcpy( dllname, basename );
1175 p = strrchr( dllname, '.' );
1176 if (!p) strcat( dllname, ".dll" );
1177 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
1179 if (wine_dll_get_owner( dllname, owner, sizeof(owner), &file_exists ) == -1)
1181 if (file_exists) return 21; /* it may be a Win32 module then */
1183 else /* found 32-bit owner, try to load it */
1185 HMODULE mod32 = LoadLibraryA( owner );
1186 if (mod32)
1188 if (!(descr = find_dll_descr( dllname ))) FreeLibrary( mod32 );
1189 /* loading the 32-bit library can have the side effect of loading the module */
1190 /* if so, simply incr the ref count and return the module */
1191 if ((hModule = GetModuleHandle16( libname )))
1193 TRACE( "module %s already loaded by owner\n", libname );
1194 pModule = NE_GetPtr( hModule );
1195 if (pModule) pModule->count++;
1196 return hModule;
1199 else
1201 /* it's probably disabled by the load order config */
1202 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner, dllname );
1203 return ERROR_FILE_NOT_FOUND;
1208 if (descr)
1210 TRACE("Trying built-in '%s'\n", libname);
1211 hinst = NE_DoLoadBuiltinModule( descr );
1212 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : builtin\n", debugstr_a(libname));
1214 else
1216 TRACE("Trying native dll '%s'\n", libname);
1217 hinst = NE_LoadModule(libname, lib_only);
1218 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : native\n", debugstr_a(libname));
1221 if (hinst > 32 && !implicit)
1223 hModule = GetModuleHandle16(libname);
1224 if(!hModule)
1226 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1227 libname, hinst);
1228 return ERROR_INVALID_HANDLE;
1231 pModule = NE_GetPtr(hModule);
1232 if(!pModule)
1234 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1235 libname, hinst);
1236 return ERROR_INVALID_HANDLE;
1239 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1242 * Call initialization routines for all loaded DLLs. Note that
1243 * when we load implicitly linked DLLs this will be done by InitTask().
1245 if(pModule->flags & NE_FFLAGS_LIBMODULE)
1247 NE_InitializeDLLs(hModule);
1248 NE_DllProcessAttach(hModule);
1251 return hinst; /* The last error that occurred */
1255 /**********************************************************************
1256 * NE_CreateThread
1258 * Create the thread for a 16-bit module.
1260 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1262 HANDLE hThread;
1263 TDB *pTask;
1264 HTASK16 hTask;
1265 HINSTANCE16 instance = 0;
1267 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1268 return 0;
1270 /* Post event to start the task */
1271 PostEvent16( hTask );
1273 /* Wait until we get the instance handle */
1276 DirectedYield16( hTask );
1277 if (!IsTask16( hTask )) /* thread has died */
1279 DWORD exit_code;
1280 WaitForSingleObject( hThread, INFINITE );
1281 GetExitCodeThread( hThread, &exit_code );
1282 CloseHandle( hThread );
1283 return exit_code;
1285 if (!(pTask = GlobalLock16( hTask ))) break;
1286 instance = pTask->hInstance;
1287 GlobalUnlock16( hTask );
1288 } while (!instance);
1290 CloseHandle( hThread );
1291 return instance;
1295 /**********************************************************************
1296 * LoadModule (KERNEL.45)
1298 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1300 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1301 LOADPARAMS16 *params;
1302 HMODULE16 hModule;
1303 NE_MODULE *pModule;
1304 LPSTR cmdline;
1305 WORD cmdShow;
1307 /* Load module */
1309 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1311 /* Special case: second instance of an already loaded NE module */
1313 if ( !( pModule = NE_GetPtr( hModule ) ) ) return (HINSTANCE16)11;
1314 if ( pModule->module32 ) return (HINSTANCE16)21;
1316 /* Increment refcount */
1318 pModule->count++;
1320 else
1322 /* Main case: load first instance of NE module */
1324 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1325 return hModule;
1327 if ( !(pModule = NE_GetPtr( hModule )) )
1328 return (HINSTANCE16)11;
1331 /* If library module, we just retrieve the instance handle */
1333 if ( ( pModule->flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1334 return NE_GetInstance( pModule );
1337 * At this point, we need to create a new process.
1339 * pModule points either to an already loaded module, whose refcount
1340 * has already been incremented (to avoid having the module vanish
1341 * in the meantime), or else to a stub module which contains only header
1342 * information.
1344 params = (LOADPARAMS16 *)paramBlock;
1345 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1346 cmdline = MapSL( params->cmdLine );
1347 return NE_CreateThread( pModule, cmdShow, cmdline );
1351 /**********************************************************************
1352 * NE_StartTask
1354 * Startup code for a new 16-bit task.
1356 DWORD NE_StartTask(void)
1358 TDB *pTask = TASK_GetCurrent();
1359 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1360 HINSTANCE16 hInstance, hPrevInstance;
1361 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1362 WORD sp;
1364 if ( pModule->count > 0 )
1366 /* Second instance of an already loaded NE module */
1367 /* Note that the refcount was already incremented by the parent */
1369 hPrevInstance = NE_GetInstance( pModule );
1371 if ( pModule->dgroup )
1372 if ( NE_CreateSegment( pModule, pModule->dgroup ) )
1373 NE_LoadSegment( pModule, pModule->dgroup );
1375 hInstance = NE_GetInstance( pModule );
1376 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->dgroup, hPrevInstance);
1379 else
1381 /* Load first instance of NE module */
1383 pModule->flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1385 hInstance = NE_DoLoadModule( pModule );
1386 hPrevInstance = 0;
1389 if ( hInstance >= 32 )
1391 CONTEXT86 context;
1393 /* Enter instance handles into task struct */
1395 pTask->hInstance = hInstance;
1396 pTask->hPrevInstance = hPrevInstance;
1398 /* Use DGROUP for 16-bit stack */
1400 if (!(sp = pModule->sp))
1401 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
1402 sp &= ~1;
1403 sp -= sizeof(STACK16FRAME);
1404 NtCurrentTeb()->cur_stack = MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1406 /* Registers at initialization must be:
1407 * ax zero
1408 * bx stack size in bytes
1409 * cx heap size in bytes
1410 * si previous app instance
1411 * di current app instance
1412 * bp zero
1413 * es selector to the PSP
1414 * ds dgroup of the application
1415 * ss stack selector
1416 * sp top of the stack
1418 memset( &context, 0, sizeof(context) );
1419 context.SegCs = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
1420 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1421 context.SegEs = pTask->hPDB;
1422 context.SegFs = wine_get_fs();
1423 context.SegGs = wine_get_gs();
1424 context.Eip = pModule->ip;
1425 context.Ebx = pModule->stack_size;
1426 context.Ecx = pModule->heap_size;
1427 context.Edi = pTask->hInstance;
1428 context.Esi = pTask->hPrevInstance;
1430 /* Now call 16-bit entry point */
1432 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1433 context.SegCs, context.Eip, context.SegDs,
1434 SELECTOROF(NtCurrentTeb()->cur_stack),
1435 OFFSETOF(NtCurrentTeb()->cur_stack) );
1437 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1438 ExitThread( LOWORD(context.Eax) );
1440 return hInstance; /* error code */
1443 /***********************************************************************
1444 * LoadLibrary (KERNEL.95)
1445 * LoadLibrary16 (KERNEL32.35)
1447 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1449 return LoadModule16(libname, (LPVOID)-1 );
1453 /**********************************************************************
1454 * MODULE_CallWEP
1456 * Call a DLL's WEP, allowing it to shut down.
1457 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1459 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1461 BOOL16 ret;
1462 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1463 if (!WEP) return FALSE;
1465 __TRY
1467 WORD args[1];
1468 DWORD dwRet;
1470 args[0] = WEP_FREE_DLL;
1471 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1472 ret = LOWORD(dwRet);
1474 __EXCEPT(page_fault)
1476 WARN("Page fault\n");
1477 ret = 0;
1479 __ENDTRY
1481 return ret;
1485 /**********************************************************************
1486 * NE_FreeModule
1488 * Implementation of FreeModule16().
1490 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1492 HMODULE16 *hPrevModule;
1493 NE_MODULE *pModule;
1494 HMODULE16 *pModRef;
1495 int i;
1497 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1498 hModule = pModule->self;
1500 TRACE("%04x count %d\n", hModule, pModule->count );
1502 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1503 else pModule->count = 0;
1505 if (pModule->flags & NE_FFLAGS_BUILTIN)
1506 return FALSE; /* Can't free built-in module */
1508 if (call_wep && !(pModule->flags & NE_FFLAGS_WIN32))
1510 /* Free the objects owned by the DLL module */
1511 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1513 if (pModule->flags & NE_FFLAGS_LIBMODULE)
1514 MODULE_CallWEP( hModule );
1515 else
1516 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1520 /* Clear magic number just in case */
1522 pModule->magic = pModule->self = 0;
1523 if (pModule->fd) CloseHandle( pModule->fd );
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*)((char *)pModule + pModule->modref_table);
1537 for (i = 0; i < pModule->modref_count; i++, pModRef++)
1539 NE_FreeModule( *pModRef, call_wep );
1542 /* Free the module storage */
1544 GlobalFreeAll16( hModule );
1545 return TRUE;
1549 /**********************************************************************
1550 * FreeModule (KERNEL.46)
1552 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1554 return NE_FreeModule( hModule, TRUE );
1558 /***********************************************************************
1559 * FreeLibrary (KERNEL.96)
1560 * FreeLibrary16 (KERNEL32.36)
1562 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1564 TRACE("%04x\n", handle );
1565 FreeModule16( handle );
1569 /***********************************************************************
1570 * GetModuleHandle16 (KERNEL32.@)
1572 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1574 HMODULE16 hModule = hFirstModule;
1575 LPSTR s;
1576 BYTE len, *name_table;
1577 char tmpstr[MAX_PATH];
1578 NE_MODULE *pModule;
1580 TRACE("(%s)\n", name);
1582 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1584 len = strlen(name);
1585 if (!len) return 0;
1587 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1589 /* If 'name' matches exactly the module name of a module:
1590 * Return its handle.
1592 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1594 pModule = NE_GetPtr( hModule );
1595 if (!pModule) break;
1596 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1598 name_table = (BYTE *)pModule + pModule->name_table;
1599 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1600 return hModule;
1603 /* If uppercased 'name' matches exactly the module name of a module:
1604 * Return its handle
1606 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1608 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1610 pModule = NE_GetPtr( hModule );
1611 if (!pModule) break;
1612 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1614 name_table = (BYTE *)pModule + pModule->name_table;
1615 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1616 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1617 * lowercased module names, but try to load uppercase DLLs, so this
1618 * 'i' compare is just a quickfix until the loader handles that
1619 * correctly. -MM 990705
1621 if ((*name_table == len) && !NE_strncasecmp(tmpstr, name_table+1, len))
1622 return hModule;
1625 /* If the base filename of 'name' matches the base filename of the module
1626 * filename of some module (case-insensitive compare):
1627 * Return its handle.
1630 /* basename: search backwards in passed name to \ / or : */
1631 s = tmpstr + strlen(tmpstr);
1632 while (s > tmpstr)
1634 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1635 break;
1636 s--;
1639 /* search this in loaded filename list */
1640 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1642 char *loadedfn;
1643 OFSTRUCT *ofs;
1645 pModule = NE_GetPtr( hModule );
1646 if (!pModule) break;
1647 if (!pModule->fileinfo) continue;
1648 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1650 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1651 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1652 /* basename: search backwards in pathname to \ / or : */
1653 while (loadedfn > (char*)ofs->szPathName)
1655 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1656 break;
1657 loadedfn--;
1659 /* case insensitive compare ... */
1660 if (!NE_strcasecmp(loadedfn, s))
1661 return hModule;
1663 return 0;
1667 /**********************************************************************
1668 * GetModuleName (KERNEL.27)
1670 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1672 NE_MODULE *pModule;
1673 BYTE *p;
1675 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1676 p = (BYTE *)pModule + pModule->name_table;
1677 if (count > *p) count = *p + 1;
1678 if (count > 0)
1680 memcpy( buf, p + 1, count - 1 );
1681 buf[count-1] = '\0';
1683 return TRUE;
1687 /**********************************************************************
1688 * GetModuleFileName (KERNEL.49)
1690 * Comment: see GetModuleFileNameA
1692 * Even if invoked by second instance of a program,
1693 * it still returns path of first one.
1695 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1696 INT16 nSize )
1698 NE_MODULE *pModule;
1700 /* Win95 does not query hModule if set to 0 !
1701 * Is this wrong or maybe Win3.1 only ? */
1702 if (!hModule) hModule = GetCurrentTask();
1704 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1705 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1706 if (pModule->expected_version >= 0x400)
1707 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1708 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1709 return strlen(lpFileName);
1713 /**********************************************************************
1714 * GetModuleUsage (KERNEL.48)
1716 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1718 NE_MODULE *pModule = NE_GetPtr( hModule );
1719 return pModule ? pModule->count : 0;
1723 /**********************************************************************
1724 * GetExpWinVer (KERNEL.167)
1726 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1728 NE_MODULE *pModule = NE_GetPtr( hModule );
1729 if ( !pModule ) return 0;
1732 * For built-in modules, fake the expected version the module should
1733 * have according to the Windows version emulated by Wine
1735 if ( !pModule->expected_version )
1737 OSVERSIONINFOA versionInfo;
1738 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1740 if ( GetVersionExA( &versionInfo ) )
1741 pModule->expected_version =
1742 (versionInfo.dwMajorVersion & 0xff) << 8
1743 | (versionInfo.dwMinorVersion & 0xff);
1746 return pModule->expected_version;
1750 /***********************************************************************
1751 * WinExec (KERNEL.166)
1753 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1755 LPCSTR p, args = NULL;
1756 LPCSTR name_beg, name_end;
1757 LPSTR name, cmdline;
1758 int arglen;
1759 HINSTANCE16 ret;
1760 char buffer[MAX_PATH];
1762 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1764 name_beg = lpCmdLine+1;
1765 p = strchr ( lpCmdLine+1, '"' );
1766 if (p)
1768 name_end = p;
1769 args = strchr ( p, ' ' );
1771 else /* yes, even valid with trailing '"' missing */
1772 name_end = lpCmdLine+strlen(lpCmdLine);
1774 else
1776 name_beg = lpCmdLine;
1777 args = strchr( lpCmdLine, ' ' );
1778 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1781 if ((name_beg == lpCmdLine) && (!args))
1782 { /* just use the original cmdline string as file name */
1783 name = (LPSTR)lpCmdLine;
1785 else
1787 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1788 return ERROR_NOT_ENOUGH_MEMORY;
1789 memcpy( name, name_beg, name_end - name_beg );
1790 name[name_end - name_beg] = '\0';
1793 if (args)
1795 args++;
1796 arglen = strlen(args);
1797 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1798 cmdline[0] = (BYTE)arglen;
1799 strcpy( cmdline + 1, args );
1801 else
1803 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1804 cmdline[0] = cmdline[1] = 0;
1807 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1809 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1811 LOADPARAMS16 params;
1812 WORD showCmd[2];
1813 showCmd[0] = 2;
1814 showCmd[1] = nCmdShow;
1816 params.hEnvironment = 0;
1817 params.cmdLine = MapLS( cmdline );
1818 params.showCmd = MapLS( showCmd );
1819 params.reserved = 0;
1821 ret = LoadModule16( buffer, &params );
1822 UnMapLS( params.cmdLine );
1823 UnMapLS( params.showCmd );
1825 else ret = GetLastError();
1827 HeapFree( GetProcessHeap(), 0, cmdline );
1828 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1830 if (ret == 21) /* 32-bit module */
1832 DWORD count;
1833 ReleaseThunkLock( &count );
1834 ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) );
1835 RestoreThunkLock( count );
1837 return ret;
1840 /***********************************************************************
1841 * GetProcAddress (KERNEL.50)
1843 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1845 WORD ordinal;
1846 FARPROC16 ret;
1848 if (!hModule) hModule = GetCurrentTask();
1849 hModule = GetExePtr( hModule );
1851 if (HIWORD(name) != 0)
1853 ordinal = NE_GetOrdinal( hModule, name );
1854 TRACE("%04x '%s'\n", hModule, name );
1856 else
1858 ordinal = LOWORD(name);
1859 TRACE("%04x %04x\n", hModule, ordinal );
1861 if (!ordinal) return (FARPROC16)0;
1863 ret = NE_GetEntryPoint( hModule, ordinal );
1865 TRACE("returning %08x\n", (UINT)ret );
1866 return ret;
1870 /***************************************************************************
1871 * HasGPHandler (KERNEL.338)
1873 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1875 HMODULE16 hModule;
1876 int gpOrdinal;
1877 SEGPTR gpPtr;
1878 GPHANDLERDEF *gpHandler;
1880 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1881 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1882 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1883 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1884 && (gpHandler = MapSL( gpPtr )) != NULL )
1886 while (gpHandler->selector)
1888 if ( SELECTOROF(address) == gpHandler->selector
1889 && OFFSETOF(address) >= gpHandler->rangeStart
1890 && OFFSETOF(address) < gpHandler->rangeEnd )
1891 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1892 gpHandler++;
1896 return 0;
1900 /**********************************************************************
1901 * GetModuleHandle (KERNEL.47)
1903 * Find a module from a module name.
1905 * NOTE: The current implementation works the same way the Windows 95 one
1906 * does. Do not try to 'fix' it, fix the callers.
1907 * + It does not do ANY extension handling (except that strange .EXE bit)!
1908 * + It does not care about paths, just about basenames. (same as Windows)
1910 * RETURNS
1911 * LOWORD:
1912 * the win16 module handle if found
1913 * 0 if not
1914 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1915 * Always hFirstModule
1917 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1919 if (HIWORD(name) == 0)
1920 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1921 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1924 /**********************************************************************
1925 * NE_GetModuleByFilename
1927 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1929 HMODULE16 hModule;
1930 LPSTR s, p;
1931 BYTE len, *name_table;
1932 char tmpstr[MAX_PATH];
1933 NE_MODULE *pModule;
1935 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1937 /* If the base filename of 'name' matches the base filename of the module
1938 * filename of some module (case-insensitive compare):
1939 * Return its handle.
1942 /* basename: search backwards in passed name to \ / or : */
1943 s = tmpstr + strlen(tmpstr);
1944 while (s > tmpstr)
1946 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1947 break;
1948 s--;
1951 /* search this in loaded filename list */
1952 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1954 char *loadedfn;
1955 OFSTRUCT *ofs;
1957 pModule = NE_GetPtr( hModule );
1958 if (!pModule) break;
1959 if (!pModule->fileinfo) continue;
1960 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1962 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1963 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1964 /* basename: search backwards in pathname to \ / or : */
1965 while (loadedfn > (char*)ofs->szPathName)
1967 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1968 break;
1969 loadedfn--;
1971 /* case insensitive compare ... */
1972 if (!NE_strcasecmp(loadedfn, s))
1973 return hModule;
1975 /* If basename (without ext) matches the module name of a module:
1976 * Return its handle.
1979 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1980 len = strlen(s);
1982 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1984 pModule = NE_GetPtr( hModule );
1985 if (!pModule) break;
1986 if (pModule->flags & NE_FFLAGS_WIN32) continue;
1988 name_table = (BYTE *)pModule + pModule->name_table;
1989 if ((*name_table == len) && !NE_strncasecmp(s, name_table+1, len))
1990 return hModule;
1993 return 0;
1996 /***********************************************************************
1997 * GetProcAddress16 (KERNEL32.37)
1998 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
2000 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
2002 if (!hModule) return 0;
2003 if (HIWORD(hModule))
2005 WARN("hModule is Win32 handle (%p)\n", hModule );
2006 return 0;
2008 return GetProcAddress16( LOWORD(hModule), name );
2011 /**********************************************************************
2012 * ModuleFirst (TOOLHELP.59)
2014 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
2016 lpme->wNext = hFirstModule;
2017 return ModuleNext16( lpme );
2021 /**********************************************************************
2022 * ModuleNext (TOOLHELP.60)
2024 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
2026 NE_MODULE *pModule;
2027 char *name;
2029 if (!lpme->wNext) return FALSE;
2030 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
2031 name = (char *)pModule + pModule->name_table;
2032 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
2033 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
2034 lpme->hModule = lpme->wNext;
2035 lpme->wcUsage = pModule->count;
2036 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
2037 lpme->wNext = pModule->next;
2038 return TRUE;
2042 /**********************************************************************
2043 * ModuleFindName (TOOLHELP.61)
2045 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
2047 lpme->wNext = GetModuleHandle16( name );
2048 return ModuleNext16( lpme );
2052 /**********************************************************************
2053 * ModuleFindHandle (TOOLHELP.62)
2055 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
2057 hModule = GetExePtr( hModule );
2058 lpme->wNext = hModule;
2059 return ModuleNext16( lpme );
2063 /***************************************************************************
2064 * IsRomModule (KERNEL.323)
2066 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
2068 return FALSE;
2071 /***************************************************************************
2072 * IsRomFile (KERNEL.326)
2074 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
2076 return FALSE;
2079 /***********************************************************************
2080 * create_dummy_module
2082 * Create a dummy NE module for Win32 or Winelib.
2084 static HMODULE16 create_dummy_module( HMODULE module32 )
2086 HMODULE16 hModule;
2087 NE_MODULE *pModule;
2088 SEGTABLEENTRY *pSegment;
2089 char *pStr,*s;
2090 unsigned int len;
2091 const char* basename;
2092 OFSTRUCT *ofs;
2093 int of_size, size;
2094 char filename[MAX_PATH];
2095 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
2097 if (!nt) return (HMODULE16)11; /* invalid exe */
2099 /* Extract base filename */
2100 len = GetModuleFileNameA( module32, filename, sizeof(filename) );
2101 if (!len || len >= sizeof(filename)) return (HMODULE16)11; /* invalid exe */
2102 basename = strrchr(filename, '\\');
2103 if (!basename) basename = filename;
2104 else basename++;
2105 len = strlen(basename);
2106 if ((s = strchr(basename, '.'))) len = s - basename;
2108 /* Allocate module */
2109 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
2110 + strlen(filename) + 1;
2111 size = sizeof(NE_MODULE) +
2112 /* loaded file info */
2113 ((of_size + 3) & ~3) +
2114 /* segment table: DS,CS */
2115 2 * sizeof(SEGTABLEENTRY) +
2116 /* name table */
2117 len + 2 +
2118 /* several empty tables */
2121 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2122 if (!hModule) return (HMODULE16)11; /* invalid exe */
2124 FarSetOwner16( hModule, hModule );
2125 pModule = (NE_MODULE *)GlobalLock16( hModule );
2127 /* Set all used entries */
2128 pModule->magic = IMAGE_OS2_SIGNATURE;
2129 pModule->count = 1;
2130 pModule->next = 0;
2131 pModule->flags = NE_FFLAGS_WIN32;
2132 pModule->dgroup = 0;
2133 pModule->ss = 1;
2134 pModule->cs = 2;
2135 pModule->heap_size = 0;
2136 pModule->stack_size = 0;
2137 pModule->seg_count = 2;
2138 pModule->modref_count = 0;
2139 pModule->nrname_size = 0;
2140 pModule->fileinfo = sizeof(NE_MODULE);
2141 pModule->os_flags = NE_OSFLAGS_WINDOWS;
2142 pModule->self = hModule;
2143 pModule->module32 = module32;
2145 /* Set version and flags */
2146 pModule->expected_version = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2147 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2148 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2149 pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2151 /* Set loaded file information */
2152 ofs = (OFSTRUCT *)(pModule + 1);
2153 memset( ofs, 0, of_size );
2154 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2155 strcpy( ofs->szPathName, filename );
2157 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2158 pModule->seg_table = (int)pSegment - (int)pModule;
2159 /* Data segment */
2160 pSegment->size = 0;
2161 pSegment->flags = NE_SEGFLAGS_DATA;
2162 pSegment->minsize = 0x1000;
2163 pSegment++;
2164 /* Code segment */
2165 pSegment->flags = 0;
2166 pSegment++;
2168 /* Module name */
2169 pStr = (char *)pSegment;
2170 pModule->name_table = (int)pStr - (int)pModule;
2171 assert(len<256);
2172 *pStr = len;
2173 lstrcpynA( pStr+1, basename, len+1 );
2174 pStr += len+2;
2176 /* All tables zero terminated */
2177 pModule->res_table = pModule->import_table = pModule->entry_table = (int)pStr - (int)pModule;
2179 NE_RegisterModule( pModule );
2180 LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2181 return hModule;
2184 /***********************************************************************
2185 * PrivateLoadLibrary (KERNEL32.@)
2187 * FIXME: rough guesswork, don't know what "Private" means
2189 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2191 return LoadLibrary16(libname);
2194 /***********************************************************************
2195 * PrivateFreeLibrary (KERNEL32.@)
2197 * FIXME: rough guesswork, don't know what "Private" means
2199 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2201 FreeLibrary16(handle);
2204 /***********************************************************************
2205 * LoadLibrary32 (KERNEL.452)
2206 * LoadSystemLibrary32 (KERNEL.482)
2208 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2210 HMODULE hModule;
2211 DWORD count;
2213 ReleaseThunkLock( &count );
2214 hModule = LoadLibraryA( libname );
2215 RestoreThunkLock( count );
2216 return hModule;
2219 /***************************************************************************
2220 * MapHModuleLS (KERNEL32.@)
2222 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2224 HMODULE16 ret;
2225 NE_MODULE *pModule;
2227 if (!hmod)
2228 return TASK_GetCurrent()->hInstance;
2229 if (!HIWORD(hmod))
2230 return LOWORD(hmod); /* we already have a 16 bit module handle */
2231 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
2232 while (pModule) {
2233 if (pModule->module32 == hmod)
2234 return pModule->self;
2235 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
2237 if ((ret = create_dummy_module( hmod )) < 32)
2239 SetLastError(ret);
2240 ret = 0;
2242 return ret;
2245 /***************************************************************************
2246 * MapHModuleSL (KERNEL32.@)
2248 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2250 NE_MODULE *pModule;
2252 if (!hmod) {
2253 TDB *pTask = TASK_GetCurrent();
2254 hmod = pTask->hModule;
2256 pModule = (NE_MODULE*)GlobalLock16(hmod);
2257 if ((pModule->magic!=IMAGE_OS2_SIGNATURE) || !(pModule->flags & NE_FFLAGS_WIN32))
2258 return 0;
2259 return pModule->module32;
2262 /***************************************************************************
2263 * MapHInstLS (KERNEL32.@)
2264 * MapHInstLS (KERNEL.472)
2266 void MapHInstLS( CONTEXT86 *context )
2268 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2271 /***************************************************************************
2272 * MapHInstSL (KERNEL32.@)
2273 * MapHInstSL (KERNEL.473)
2275 void MapHInstSL( CONTEXT86 *context )
2277 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2280 /***************************************************************************
2281 * MapHInstLS_PN (KERNEL32.@)
2283 void MapHInstLS_PN( CONTEXT86 *context )
2285 if (context->Eax) context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2288 /***************************************************************************
2289 * MapHInstSL_PN (KERNEL32.@)
2291 void MapHInstSL_PN( CONTEXT86 *context )
2293 if (context->Eax) context->Eax = (DWORD)MapHModuleSL( context->Eax );