shell32: Move the RunDLL_CallEntry16 implementation to shell.dll.
[wine/multimedia.git] / dlls / kernel32 / ne_module.c
blob51e2eb5ee22fff763d3325217fae2c502f61d070
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winternl.h"
39 #include "kernel16_private.h"
40 #include "wine/exception.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(module);
44 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
45 WINE_DECLARE_DEBUG_CHANNEL(relay);
47 #include "pshpack1.h"
48 typedef struct _GPHANDLERDEF
50 WORD selector;
51 WORD rangeStart;
52 WORD rangeEnd;
53 WORD handler;
54 } GPHANDLERDEF;
55 #include "poppack.h"
58 * Segment table entry
60 struct ne_segment_table_entry_s
62 WORD seg_data_offset; /* Sector offset of segment data */
63 WORD seg_data_length; /* Length of segment data */
64 WORD seg_flags; /* Flags associated with this segment */
65 WORD min_alloc; /* Minimum allocation size for this */
68 #define hFirstModule (pThhook->hExeHead)
70 struct builtin_dll
72 const IMAGE_DOS_HEADER *header; /* module headers */
73 const char *file_name; /* module file name */
76 /* Table of all built-in DLLs */
78 #define MAX_DLLS 50
80 static struct builtin_dll builtin_dlls[MAX_DLLS];
82 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
83 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
85 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
87 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
90 /* patch all the flat cs references of the code segment if necessary */
91 static inline void patch_code_segment( NE_MODULE *pModule )
93 #ifdef __i386__
94 int i;
95 CALLFROM16 *call;
96 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
98 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
99 if (!(pSeg->flags & NE_SEGFLAGS_DATA)) break; /* found the code segment */
101 call = GlobalLock16( pSeg->hSeg );
103 /* patch glue code address and code selector */
104 for (i = 0; call[i].pushl == 0x68; i++)
106 if (call[i].ret[0] == 0xca66 || call[i].ret[0] == 0xcb66) /* register entry point? */
107 call[i].glue = __wine_call_from_16_regs;
108 else
109 call[i].glue = __wine_call_from_16;
110 call[i].flatcs = wine_get_cs();
113 if (TRACE_ON(relay)) /* patch relay functions to all point to relay_call_from_16 */
114 for (i = 0; call[i].pushl == 0x68; i++) call[i].relay = relay_call_from_16;
115 #endif
119 /***********************************************************************
120 * contains_path
122 static inline int contains_path( LPCSTR name )
124 return ((*name && (name[1] == ':')) || strchr(name, '/') || strchr(name, '\\'));
128 /***********************************************************************
129 * NE_strcasecmp
131 * locale-independent case conversion for module lookups
133 static int NE_strcasecmp( const char *str1, const char *str2 )
135 int ret = 0;
136 for ( ; ; str1++, str2++)
137 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
138 return ret;
142 /***********************************************************************
143 * NE_strncasecmp
145 * locale-independent case conversion for module lookups
147 static int NE_strncasecmp( const char *str1, const char *str2, int len )
149 int ret = 0;
150 for ( ; len > 0; len--, str1++, str2++)
151 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
152 return ret;
156 /***********************************************************************
157 * find_dll_descr
159 * Find a descriptor in the list
161 static const IMAGE_DOS_HEADER *find_dll_descr( const char *dllname, const char **file_name )
163 int i;
164 const IMAGE_DOS_HEADER *mz_header;
165 const IMAGE_OS2_HEADER *ne_header;
166 const BYTE *name_table;
168 for (i = 0; i < MAX_DLLS; i++)
170 mz_header = builtin_dlls[i].header;
171 if (mz_header)
173 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mz_header + mz_header->e_lfanew);
174 name_table = (const BYTE *)ne_header + ne_header->ne_restab;
176 /* check the dll file name */
177 if (!NE_strcasecmp( builtin_dlls[i].file_name, dllname ) ||
178 /* check the dll module name (without extension) */
179 (!NE_strncasecmp( dllname, (const char*)name_table+1, *name_table ) &&
180 !strcmp( dllname + *name_table, ".dll" )))
182 *file_name = builtin_dlls[i].file_name;
183 return builtin_dlls[i].header;
187 return NULL;
191 /***********************************************************************
192 * __wine_dll_register_16 (KERNEL32.@)
194 * Register a built-in DLL descriptor.
196 void __wine_dll_register_16( const IMAGE_DOS_HEADER *header, const char *file_name )
198 int i;
200 for (i = 0; i < MAX_DLLS; i++)
202 if (builtin_dlls[i].header) continue;
203 builtin_dlls[i].header = header;
204 builtin_dlls[i].file_name = file_name;
205 break;
207 assert( i < MAX_DLLS );
211 /***********************************************************************
212 * __wine_dll_unregister_16 (KERNEL32.@)
214 * Unregister a built-in DLL descriptor.
216 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER *header )
218 int i;
220 for (i = 0; i < MAX_DLLS; i++)
222 if (builtin_dlls[i].header != header) continue;
223 builtin_dlls[i].header = NULL;
224 break;
229 /***********************************************************************
230 * NE_GetPtr
232 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
234 return GlobalLock16( GetExePtr(hModule) );
238 /**********************************************************************
239 * NE_RegisterModule
241 static void NE_RegisterModule( NE_MODULE *pModule )
243 pModule->next = hFirstModule;
244 hFirstModule = pModule->self;
248 /***********************************************************************
249 * NE_DumpModule
251 void NE_DumpModule( HMODULE16 hModule )
253 int i, ordinal;
254 SEGTABLEENTRY *pSeg;
255 BYTE *pstr;
256 WORD *pword;
257 NE_MODULE *pModule;
258 ET_BUNDLE *bundle;
259 ET_ENTRY *entry;
261 if (!(pModule = NE_GetPtr( hModule )))
263 ERR( "**** %04x is not a module handle\n", hModule );
264 return;
267 /* Dump the module info */
268 TRACE( "---\n" );
269 TRACE( "Module %04x:\n", hModule );
270 TRACE( "count=%d flags=%04x heap=%d stack=%d\n",
271 pModule->count, pModule->ne_flags,
272 pModule->ne_heap, pModule->ne_stack );
273 TRACE( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
274 SELECTOROF(pModule->ne_csip), OFFSETOF(pModule->ne_csip),
275 SELECTOROF(pModule->ne_sssp), OFFSETOF(pModule->ne_sssp),
276 pModule->ne_autodata, pModule->ne_cseg, pModule->ne_cmod );
277 TRACE( "os_flags=%d swap_area=%d version=%04x\n",
278 pModule->ne_exetyp, pModule->ne_swaparea, pModule->ne_expver );
279 if (pModule->ne_flags & NE_FFLAGS_WIN32)
280 TRACE( "PE module=%p\n", pModule->module32 );
282 /* Dump the file info */
283 TRACE( "---\n" );
284 TRACE( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
286 /* Dump the segment table */
287 TRACE( "---\n" );
288 TRACE( "Segment table:\n" );
289 pSeg = NE_SEG_TABLE( pModule );
290 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
291 TRACE( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
292 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
293 pSeg->minsize, pSeg->hSeg );
295 /* Dump the resource table */
296 TRACE( "---\n" );
297 TRACE( "Resource table:\n" );
298 if (pModule->ne_rsrctab)
300 pword = (WORD *)((BYTE *)pModule + pModule->ne_rsrctab);
301 TRACE( "Alignment: %d\n", *pword++ );
302 while (*pword)
304 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
305 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
306 TRACE( "id=%04x count=%d\n", ptr->type_id, ptr->count );
307 for (i = 0; i < ptr->count; i++, pname++)
308 TRACE( "offset=%d len=%d id=%04x\n",
309 pname->offset, pname->length, pname->id );
310 pword = (WORD *)pname;
313 else TRACE( "None\n" );
315 /* Dump the resident name table */
316 TRACE( "---\n" );
317 TRACE( "Resident-name table:\n" );
318 pstr = (BYTE*) pModule + pModule->ne_restab;
319 while (*pstr)
321 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
322 *(WORD *)(pstr + *pstr + 1) );
323 pstr += *pstr + 1 + sizeof(WORD);
326 /* Dump the module reference table */
327 TRACE( "---\n" );
328 TRACE( "Module ref table:\n" );
329 if (pModule->ne_modtab)
331 pword = (WORD *)((BYTE *)pModule + pModule->ne_modtab);
332 for (i = 0; i < pModule->ne_cmod; i++, pword++)
334 char name[10];
335 GetModuleName16( *pword, name, sizeof(name) );
336 TRACE( "%d: %04x -> '%s'\n", i, *pword, name );
339 else TRACE( "None\n" );
341 /* Dump the entry table */
342 TRACE( "---\n" );
343 TRACE( "Entry table:\n" );
344 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->ne_enttab);
345 do {
346 entry = (ET_ENTRY *)((BYTE *)bundle+6);
347 TRACE( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
348 ordinal = bundle->first;
349 while (ordinal < bundle->last)
351 if (entry->type == 0xff)
352 TRACE("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
353 else
354 TRACE("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
355 entry++;
357 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
359 /* Dump the non-resident names table */
360 TRACE( "---\n" );
361 TRACE( "Non-resident names table:\n" );
362 if (pModule->nrname_handle)
364 pstr = GlobalLock16( pModule->nrname_handle );
365 while (*pstr)
367 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
368 *(WORD *)(pstr + *pstr + 1) );
369 pstr += *pstr + 1 + sizeof(WORD);
372 TRACE( "\n" );
376 /***********************************************************************
377 * NE_WalkModules
379 * Walk the module list and print the modules.
381 void NE_WalkModules(void)
383 HMODULE16 hModule = hFirstModule;
384 MESSAGE( "Module Flags Name\n" );
385 while (hModule)
387 NE_MODULE *pModule = NE_GetPtr( hModule );
388 if (!pModule)
390 MESSAGE( "Bad module %04x in list\n", hModule );
391 return;
393 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->ne_flags,
394 *((char *)pModule + pModule->ne_restab),
395 (char *)pModule + pModule->ne_restab + 1 );
396 hModule = pModule->next;
401 /***********************************************************************
402 * NE_InitResourceHandler
404 * Fill in 'resloader' fields in the resource table.
406 static void NE_InitResourceHandler( HMODULE16 hModule )
408 static FARPROC16 proc;
410 NE_TYPEINFO *pTypeInfo;
411 NE_MODULE *pModule;
413 if (!(pModule = NE_GetPtr( hModule )) || !pModule->ne_rsrctab) return;
415 TRACE("InitResourceHandler[%04x]\n", hModule );
417 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
419 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
420 while(pTypeInfo->type_id)
422 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
423 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
428 /***********************************************************************
429 * NE_GetOrdinal
431 * Lookup the ordinal for a given name.
433 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
435 char buffer[256], *p;
436 BYTE *cpnt;
437 BYTE len;
438 NE_MODULE *pModule;
440 if (!(pModule = NE_GetPtr( hModule ))) return 0;
441 if (pModule->ne_flags & NE_FFLAGS_WIN32) return 0;
443 TRACE("(%04x,'%s')\n", hModule, name );
445 /* First handle names of the form '#xxxx' */
447 if (name[0] == '#') return atoi( name + 1 );
449 /* Now copy and uppercase the string */
451 strcpy( buffer, name );
452 for (p = buffer; *p; p++) *p = RtlUpperChar(*p);
453 len = p - buffer;
455 /* First search the resident names */
457 cpnt = (BYTE *)pModule + pModule->ne_restab;
459 /* Skip the first entry (module name) */
460 cpnt += *cpnt + 1 + sizeof(WORD);
461 while (*cpnt)
463 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
465 WORD ordinal;
466 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
467 TRACE(" Found: ordinal=%d\n", ordinal );
468 return ordinal;
470 cpnt += *cpnt + 1 + sizeof(WORD);
473 /* Now search the non-resident names table */
475 if (!pModule->nrname_handle) return 0; /* No non-resident table */
476 cpnt = GlobalLock16( pModule->nrname_handle );
478 /* Skip the first entry (module description string) */
479 cpnt += *cpnt + 1 + sizeof(WORD);
480 while (*cpnt)
482 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
484 WORD ordinal;
485 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
486 TRACE(" Found: ordinal=%d\n", ordinal );
487 return ordinal;
489 cpnt += *cpnt + 1 + sizeof(WORD);
491 return 0;
495 /***********************************************************************
496 * NE_GetEntryPoint
498 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
500 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
503 /***********************************************************************
504 * NE_GetEntryPointEx
506 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
508 NE_MODULE *pModule;
509 WORD sel, offset, i;
511 ET_ENTRY *entry;
512 ET_BUNDLE *bundle;
514 if (!(pModule = NE_GetPtr( hModule ))) return 0;
515 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
517 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
518 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
520 if (!(bundle->next))
521 return 0;
522 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
525 entry = (ET_ENTRY *)((BYTE *)bundle+6);
526 for (i=0; i < (ordinal - bundle->first - 1); i++)
527 entry++;
529 sel = entry->segnum;
530 memcpy( &offset, &entry->offs, sizeof(WORD) );
532 if (sel == 0xfe) sel = 0xffff; /* constant entry */
533 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
534 if (sel==0xffff)
535 return (FARPROC16)MAKESEGPTR( sel, offset );
536 if (!snoop)
537 return (FARPROC16)MAKESEGPTR( sel, offset );
538 else
539 return SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
543 /***********************************************************************
544 * EntryAddrProc (KERNEL.667) Wine-specific export
546 * Return the entry point for a given ordinal.
548 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
550 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
551 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
552 return ret;
555 /***********************************************************************
556 * NE_SetEntryPoint
558 * Change the value of an entry point. Use with caution!
559 * It can only change the offset value, not the selector.
561 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
563 NE_MODULE *pModule;
564 ET_ENTRY *entry;
565 ET_BUNDLE *bundle;
566 int i;
568 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
569 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
571 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
572 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
574 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
575 if (!(bundle->next)) return 0;
578 entry = (ET_ENTRY *)((BYTE *)bundle+6);
579 for (i=0; i < (ordinal - bundle->first - 1); i++)
580 entry++;
582 memcpy( &entry->offs, &offset, sizeof(WORD) );
583 return TRUE;
587 /***********************************************************************
588 * build_bundle_data
590 * Build the entry table bundle data from the on-disk format. Helper for build_module.
592 static void *build_bundle_data( NE_MODULE *pModule, void *dest, const BYTE *table )
594 ET_BUNDLE *oldbundle, *bundle = dest;
595 ET_ENTRY *entry;
596 BYTE nr_entries, type;
598 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
599 entry = (ET_ENTRY *)((BYTE *)bundle+6);
601 while ((nr_entries = *table++))
603 if ((type = *table++))
605 bundle->last += nr_entries;
606 if (type == 0xff)
608 while (nr_entries--)
610 entry->type = type;
611 entry->flags = *table++;
612 table += sizeof(WORD);
613 entry->segnum = *table++;
614 entry->offs = *(const WORD *)table;
615 table += sizeof(WORD);
616 entry++;
619 else
621 while (nr_entries--)
623 entry->type = type;
624 entry->flags = *table++;
625 entry->segnum = type;
626 entry->offs = *(const WORD *)table;
627 table += sizeof(WORD);
628 entry++;
632 else
634 if (bundle->first == bundle->last)
636 bundle->first += nr_entries;
637 bundle->last += nr_entries;
639 else
641 oldbundle = bundle;
642 oldbundle->next = (char *)entry - (char *)pModule;
643 bundle = (ET_BUNDLE *)entry;
644 bundle->first = bundle->last = oldbundle->last + nr_entries;
645 bundle->next = 0;
646 entry = (ET_ENTRY*)(((BYTE*)entry)+sizeof(ET_BUNDLE));
650 return entry;
654 /***********************************************************************
655 * build_module
657 * Build the in-memory module from the on-disk data.
659 static HMODULE16 build_module( const void *mapping, SIZE_T mapping_size, LPCSTR path )
661 const IMAGE_DOS_HEADER *mz_header = mapping;
662 const IMAGE_OS2_HEADER *ne_header;
663 const struct ne_segment_table_entry_s *pSeg;
664 const void *ptr;
665 int i;
666 size_t size;
667 HMODULE16 hModule;
668 NE_MODULE *pModule;
669 BYTE *buffer, *pData, *end;
670 OFSTRUCT *ofs;
672 if (mapping_size < sizeof(*mz_header)) return ERROR_BAD_FORMAT;
673 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE) return ERROR_BAD_FORMAT;
674 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mapping + mz_header->e_lfanew);
675 if (mz_header->e_lfanew + sizeof(*ne_header) > mapping_size) return ERROR_BAD_FORMAT;
676 if (ne_header->ne_magic == IMAGE_NT_SIGNATURE) return 21; /* win32 exe */
677 if (ne_header->ne_magic == IMAGE_OS2_SIGNATURE_LX)
679 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path);
680 return 12;
682 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE) return ERROR_BAD_FORMAT;
684 /* We now have a valid NE header */
686 /* check to be able to fall back to loading OS/2 programs as DOS
687 * FIXME: should this check be reversed in order to be less strict?
688 * (only fail for OS/2 ne_exetyp 0x01 here?) */
689 if ((ne_header->ne_exetyp != 0x02 /* Windows */)
690 && (ne_header->ne_exetyp != 0x04) /* Windows 386 */)
691 return ERROR_BAD_FORMAT;
693 size = sizeof(NE_MODULE) +
694 /* segment table */
695 ne_header->ne_cseg * sizeof(SEGTABLEENTRY) +
696 /* resource table */
697 ne_header->ne_restab - ne_header->ne_rsrctab +
698 /* resident names table */
699 ne_header->ne_modtab - ne_header->ne_restab +
700 /* module ref table */
701 ne_header->ne_cmod * sizeof(WORD) +
702 /* imported names table */
703 ne_header->ne_enttab - ne_header->ne_imptab +
704 /* entry table length */
705 ne_header->ne_cbenttab +
706 /* entry table extra conversion space */
707 sizeof(ET_BUNDLE) +
708 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6) +
709 /* loaded file info */
710 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
712 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
713 if (!hModule) return ERROR_BAD_FORMAT;
715 FarSetOwner16( hModule, hModule );
716 pModule = GlobalLock16( hModule );
717 memcpy( pModule, ne_header, sizeof(*ne_header) );
718 pModule->count = 0;
719 /* check programs for default minimal stack size */
720 if (!(pModule->ne_flags & NE_FFLAGS_LIBMODULE) && (pModule->ne_stack < 0x1400))
721 pModule->ne_stack = 0x1400;
723 pModule->self = hModule;
724 pModule->mapping = mapping;
725 pModule->mapping_size = mapping_size;
727 pData = (BYTE *)(pModule + 1);
729 /* Clear internal Wine flags in case they are set in the EXE file */
731 pModule->ne_flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
733 /* Get the segment table */
735 pModule->ne_segtab = pData - (BYTE *)pModule;
736 if (!(pSeg = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_segtab,
737 ne_header->ne_cseg * sizeof(struct ne_segment_table_entry_s) )))
738 goto failed;
739 for (i = ne_header->ne_cseg; i > 0; i--, pSeg++)
741 memcpy( pData, pSeg, sizeof(*pSeg) );
742 pData += sizeof(SEGTABLEENTRY);
745 /* Get the resource table */
747 if (ne_header->ne_rsrctab < ne_header->ne_restab)
749 pModule->ne_rsrctab = pData - (BYTE *)pModule;
750 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_rsrctab,
751 ne_header->ne_restab - ne_header->ne_rsrctab )) goto failed;
752 pData += ne_header->ne_restab - ne_header->ne_rsrctab;
754 else pModule->ne_rsrctab = 0; /* No resource table */
756 /* Get the resident names table */
758 pModule->ne_restab = pData - (BYTE *)pModule;
759 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_restab,
760 ne_header->ne_modtab - ne_header->ne_restab )) goto failed;
761 pData += ne_header->ne_modtab - ne_header->ne_restab;
763 /* Get the module references table */
765 if (ne_header->ne_cmod > 0)
767 pModule->ne_modtab = pData - (BYTE *)pModule;
768 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_modtab,
769 ne_header->ne_cmod * sizeof(WORD) )) goto failed;
770 pData += ne_header->ne_cmod * sizeof(WORD);
772 else pModule->ne_modtab = 0; /* No module references */
774 /* Get the imported names table */
776 pModule->ne_imptab = pData - (BYTE *)pModule;
777 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_imptab,
778 ne_header->ne_enttab - ne_header->ne_imptab )) goto failed;
779 pData += ne_header->ne_enttab - ne_header->ne_imptab;
781 /* Load entry table, convert it to the optimized version used by Windows */
783 pModule->ne_enttab = pData - (BYTE *)pModule;
784 if (!(ptr = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_enttab,
785 ne_header->ne_cbenttab ))) goto failed;
786 end = build_bundle_data( pModule, pData, ptr );
788 pData += ne_header->ne_cbenttab + sizeof(ET_BUNDLE) +
789 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6);
791 if (end > pData)
793 FIXME( "not enough space for entry table for %s\n", debugstr_a(path) );
794 goto failed;
797 /* Store the filename information */
799 pModule->fileinfo = pData - (BYTE *)pModule;
800 ofs = (OFSTRUCT *)pData;
801 ofs->cBytes = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path);
802 ofs->fFixedDisk = 1;
803 strcpy( ofs->szPathName, path );
804 pData += ofs->cBytes + 1;
805 assert( (BYTE *)pModule + size <= pData );
807 /* Get the non-resident names table */
809 if (ne_header->ne_cbnrestab)
811 pModule->nrname_handle = GlobalAlloc16( 0, ne_header->ne_cbnrestab );
812 if (!pModule->nrname_handle) goto failed;
813 FarSetOwner16( pModule->nrname_handle, hModule );
814 buffer = GlobalLock16( pModule->nrname_handle );
815 if (!NE_READ_DATA( pModule, buffer, ne_header->ne_nrestab, ne_header->ne_cbnrestab ))
817 GlobalFree16( pModule->nrname_handle );
818 goto failed;
821 else pModule->nrname_handle = 0;
823 /* Allocate a segment for the implicitly-loaded DLLs */
825 if (pModule->ne_cmod)
827 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
828 (pModule->ne_cmod+1)*sizeof(HMODULE16) );
829 if (!pModule->dlls_to_init)
831 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
832 goto failed;
834 FarSetOwner16( pModule->dlls_to_init, hModule );
836 else pModule->dlls_to_init = 0;
838 NE_RegisterModule( pModule );
839 return hModule;
841 failed:
842 GlobalFree16( hModule );
843 return ERROR_BAD_FORMAT;
847 /***********************************************************************
848 * NE_LoadDLLs
850 * Load all DLLs implicitly linked to a module.
852 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
854 int i;
855 WORD *pModRef = (WORD *)((char *)pModule + pModule->ne_modtab);
856 WORD *pDLLs = GlobalLock16( pModule->dlls_to_init );
858 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
860 char buffer[260], *p;
861 BYTE *pstr = (BYTE *)pModule + pModule->ne_imptab + *pModRef;
862 memcpy( buffer, pstr + 1, *pstr );
863 *(buffer + *pstr) = 0; /* terminate it */
865 TRACE("Loading '%s'\n", buffer );
866 if (!(*pModRef = GetModuleHandle16( buffer )))
868 /* If the DLL is not loaded yet, load it and store */
869 /* its handle in the list of DLLs to initialize. */
870 HMODULE16 hDLL;
872 /* Append .DLL to name if no extension present */
873 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
874 strcat( buffer, ".DLL" );
876 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
878 /* FIXME: cleanup what was done */
880 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
881 buffer, *((BYTE*)pModule + pModule->ne_restab),
882 (char *)pModule + pModule->ne_restab + 1, hDLL );
883 return FALSE;
885 *pModRef = GetExePtr( hDLL );
886 *pDLLs++ = *pModRef;
888 else /* Increment the reference count of the DLL */
890 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
891 if (pOldDLL) pOldDLL->count++;
894 return TRUE;
898 /**********************************************************************
899 * NE_DoLoadModule
901 * Load first instance of NE module from file.
903 * pModule must point to a module structure prepared by build_module.
904 * This routine must never be called twice on a module.
906 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
908 /* Allocate the segments for this module */
910 if (!NE_CreateAllSegments( pModule ))
911 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
913 /* Load the referenced DLLs */
915 if (!NE_LoadDLLs( pModule ))
916 return ERROR_FILE_NOT_FOUND; /* 2 */
918 /* Load the segments */
920 NE_LoadAllSegments( pModule );
922 /* Make sure the usage count is 1 on the first loading of */
923 /* the module, even if it contains circular DLL references */
925 pModule->count = 1;
927 return NE_GetInstance( pModule );
930 /**********************************************************************
931 * NE_LoadModule
933 * Load first instance of NE module. (Note: caller is responsible for
934 * ensuring the module isn't already loaded!)
936 * If the module turns out to be an executable module, only a
937 * handle to a module stub is returned; this needs to be initialized
938 * by calling NE_DoLoadModule later, in the context of the newly
939 * created process.
941 * If lib_only is TRUE, however, the module is perforce treated
942 * like a DLL module, even if it is an executable module.
945 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
947 NE_MODULE *pModule;
948 HMODULE16 hModule;
949 HINSTANCE16 hInstance;
950 HFILE16 hFile;
951 OFSTRUCT ofs;
952 HANDLE mapping;
953 void *ptr;
954 MEMORY_BASIC_INFORMATION info;
956 /* Open file */
957 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
958 return ERROR_FILE_NOT_FOUND;
960 mapping = CreateFileMappingW( DosFileHandleToWin32Handle(hFile), NULL, PAGE_WRITECOPY, 0, 0, NULL );
961 _lclose16( hFile );
962 if (!mapping) return ERROR_BAD_FORMAT;
964 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 0 );
965 CloseHandle( mapping );
966 if (!ptr) return ERROR_BAD_FORMAT;
968 VirtualQuery( ptr, &info, sizeof(info) );
969 hModule = build_module( ptr, info.RegionSize, ofs.szPathName );
971 if (hModule < 32)
973 UnmapViewOfFile( ptr );
974 return hModule;
977 SNOOP16_RegisterDLL( hModule, ofs.szPathName );
978 NE_InitResourceHandler( hModule );
980 pModule = NE_GetPtr( hModule );
982 if ( !lib_only && !( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) )
983 return hModule;
985 hInstance = NE_DoLoadModule( pModule );
986 if ( hInstance < 32 )
988 /* cleanup ... */
989 NE_FreeModule( hModule, 0 );
992 return hInstance;
996 /***********************************************************************
997 * NE_DoLoadBuiltinModule
999 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1001 static HMODULE16 NE_DoLoadBuiltinModule( const IMAGE_DOS_HEADER *mz_header, const char *file_name,
1002 HMODULE owner32 )
1004 NE_MODULE *pModule;
1005 HMODULE16 hModule;
1006 HINSTANCE16 hInstance;
1007 OSVERSIONINFOW versionInfo;
1008 SIZE_T mapping_size = ~0UL; /* assume builtins don't contain invalid offsets... */
1010 hModule = build_module( mz_header, mapping_size, file_name );
1011 if (hModule < 32) return hModule;
1012 pModule = GlobalLock16( hModule );
1013 pModule->ne_flags |= NE_FFLAGS_BUILTIN;
1014 pModule->owner32 = owner32;
1016 /* fake the expected version the module should have according to the current Windows version */
1017 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1018 if (GetVersionExW( &versionInfo ))
1019 pModule->ne_expver = MAKEWORD( versionInfo.dwMinorVersion, versionInfo.dwMajorVersion );
1021 hInstance = NE_DoLoadModule( pModule );
1022 if (hInstance < 32) NE_FreeModule( hModule, 0 );
1024 NE_InitResourceHandler( hModule );
1026 if (pModule->ne_heap)
1028 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1;
1029 unsigned int size = pSeg->minsize + pModule->ne_heap;
1030 if (size > 0xfffe) size = 0xfffe;
1031 LocalInit16( GlobalHandleToSel16(pSeg->hSeg), pSeg->minsize, size );
1034 patch_code_segment( pModule );
1036 return hInstance;
1040 /**********************************************************************
1041 * MODULE_LoadModule16
1043 * Load a NE module in the order of the loadorder specification.
1044 * The caller is responsible that the module is not loaded already.
1047 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1049 HINSTANCE16 hinst = 2;
1050 HMODULE16 hModule;
1051 HMODULE mod32 = 0;
1052 NE_MODULE *pModule;
1053 const IMAGE_DOS_HEADER *descr = NULL;
1054 const char *file_name = NULL;
1055 char dllname[32], owner[20], *p;
1056 const char *basename, *main_module;
1057 int owner_exists = FALSE;
1059 /* strip path information */
1061 basename = libname;
1062 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1063 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
1064 if ((p = strrchr( basename, '/' ))) basename = p + 1;
1066 if (strlen(basename) < sizeof(dllname)-6)
1068 strcpy( dllname, basename );
1069 p = strrchr( dllname, '.' );
1070 if (!p) strcat( dllname, ".dll" );
1071 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
1073 strcpy( p, "16" );
1074 if ((mod32 = LoadLibraryA( dllname )))
1076 if (!(descr = (void *)GetProcAddress( mod32, "__wine_spec_dos_header" )))
1078 WARN( "loaded %s but does not contain a 16-bit module\n", debugstr_a(dllname) );
1079 FreeLibrary( mod32 );
1081 else
1083 TRACE( "found %s with embedded 16-bit module\n", debugstr_a(dllname) );
1084 file_name = basename;
1086 /* if module has a 32-bit owner, match the load order of the owner */
1087 if ((main_module = (void *)GetProcAddress( mod32, "__wine_spec_main_module" )))
1089 LDR_MODULE *ldr;
1090 HMODULE main_owner = LoadLibraryA( main_module );
1092 if (!main_owner)
1094 WARN( "couldn't load owner %s for 16-bit dll %s\n", main_module, dllname );
1095 FreeLibrary( mod32 );
1096 return ERROR_FILE_NOT_FOUND;
1098 /* check if module was loaded native */
1099 if (LdrFindEntryForAddress( main_owner, &ldr ) || !(ldr->Flags & LDR_WINE_INTERNAL))
1101 FreeLibrary( mod32 );
1102 descr = NULL;
1104 FreeLibrary( main_owner );
1108 *p = 0;
1110 /* old-style 16-bit placeholders support, to be removed at some point */
1111 if (!mod32 && wine_dll_get_owner( dllname, owner, sizeof(owner), &owner_exists ) != -1)
1113 mod32 = LoadLibraryA( owner );
1114 if (mod32)
1116 if (!(descr = find_dll_descr( dllname, &file_name )))
1118 FreeLibrary( mod32 );
1119 owner_exists = 0;
1122 else
1124 /* it's probably disabled by the load order config */
1125 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner, dllname );
1126 return ERROR_FILE_NOT_FOUND;
1129 /* loading the 32-bit library can have the side effect of loading the module */
1130 /* if so, simply incr the ref count and return the module */
1131 if (descr && (hModule = GetModuleHandle16( libname )))
1133 TRACE( "module %s already loaded by owner\n", libname );
1134 pModule = NE_GetPtr( hModule );
1135 if (pModule) pModule->count++;
1136 FreeLibrary( mod32 );
1137 return hModule;
1141 if (descr)
1143 TRACE("Trying built-in '%s'\n", libname);
1144 hinst = NE_DoLoadBuiltinModule( descr, file_name, mod32 );
1145 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : builtin\n", debugstr_a(file_name));
1147 else
1149 TRACE("Trying native dll '%s'\n", libname);
1150 hinst = NE_LoadModule(libname, lib_only);
1151 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : native\n", debugstr_a(libname));
1152 if (hinst == ERROR_FILE_NOT_FOUND && owner_exists) hinst = 21; /* win32 module */
1155 if (hinst > 32 && !implicit)
1157 hModule = GetModuleHandle16(libname);
1158 if(!hModule)
1160 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1161 libname, hinst);
1162 return ERROR_INVALID_HANDLE;
1165 pModule = NE_GetPtr(hModule);
1166 if(!pModule)
1168 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1169 libname, hinst);
1170 return ERROR_INVALID_HANDLE;
1173 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1176 * Call initialization routines for all loaded DLLs. Note that
1177 * when we load implicitly linked DLLs this will be done by InitTask().
1179 if(pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1181 NE_InitializeDLLs(hModule);
1182 NE_DllProcessAttach(hModule);
1185 return hinst; /* The last error that occurred */
1189 /**********************************************************************
1190 * NE_CreateThread
1192 * Create the thread for a 16-bit module.
1194 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1196 HANDLE hThread;
1197 TDB *pTask;
1198 HTASK16 hTask;
1199 HINSTANCE16 instance = 0;
1201 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1202 return 0;
1204 /* Post event to start the task */
1205 PostEvent16( hTask );
1207 /* Wait until we get the instance handle */
1210 DirectedYield16( hTask );
1211 if (!IsTask16( hTask )) /* thread has died */
1213 DWORD exit_code;
1214 WaitForSingleObject( hThread, INFINITE );
1215 GetExitCodeThread( hThread, &exit_code );
1216 CloseHandle( hThread );
1217 return exit_code;
1219 if (!(pTask = GlobalLock16( hTask ))) break;
1220 instance = pTask->hInstance;
1221 GlobalUnlock16( hTask );
1222 } while (!instance);
1224 CloseHandle( hThread );
1225 return instance;
1229 /**********************************************************************
1230 * LoadModule (KERNEL.45)
1232 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1234 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1235 LOADPARAMS16 *params;
1236 HMODULE16 hModule;
1237 NE_MODULE *pModule;
1238 LPSTR cmdline;
1239 WORD cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
1241 if (name == NULL) return 0;
1243 TRACE("name %s, paramBlock %p\n", name, paramBlock);
1245 /* Load module */
1247 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1249 /* Special case: second instance of an already loaded NE module */
1251 if ( !( pModule = NE_GetPtr( hModule ) ) ) return ERROR_BAD_FORMAT;
1252 if ( pModule->module32 ) return (HINSTANCE16)21;
1254 /* Increment refcount */
1256 pModule->count++;
1258 else
1260 /* Main case: load first instance of NE module */
1262 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1263 return hModule;
1265 if ( !(pModule = NE_GetPtr( hModule )) )
1266 return ERROR_BAD_FORMAT;
1269 /* If library module, we just retrieve the instance handle */
1271 if ( ( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1272 return NE_GetInstance( pModule );
1275 * At this point, we need to create a new process.
1277 * pModule points either to an already loaded module, whose refcount
1278 * has already been incremented (to avoid having the module vanish
1279 * in the meantime), or else to a stub module which contains only header
1280 * information.
1282 params = paramBlock;
1283 if (params->showCmd)
1284 cmdShow = ((WORD *)MapSL( params->showCmd ))[1];
1285 cmdline = MapSL( params->cmdLine );
1286 return NE_CreateThread( pModule, cmdShow, cmdline );
1290 /**********************************************************************
1291 * NE_StartTask
1293 * Startup code for a new 16-bit task.
1295 DWORD NE_StartTask(void)
1297 TDB *pTask = TASK_GetCurrent();
1298 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1299 HINSTANCE16 hInstance, hPrevInstance;
1300 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1301 WORD sp;
1303 if ( pModule->count > 0 )
1305 /* Second instance of an already loaded NE module */
1306 /* Note that the refcount was already incremented by the parent */
1308 hPrevInstance = NE_GetInstance( pModule );
1310 if ( pModule->ne_autodata )
1311 if ( NE_CreateSegment( pModule, pModule->ne_autodata ) )
1312 NE_LoadSegment( pModule, pModule->ne_autodata );
1314 hInstance = NE_GetInstance( pModule );
1315 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->ne_autodata, hPrevInstance);
1318 else
1320 /* Load first instance of NE module */
1322 pModule->ne_flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1324 hInstance = NE_DoLoadModule( pModule );
1325 hPrevInstance = 0;
1328 if ( hInstance >= 32 )
1330 CONTEXT86 context;
1332 /* Enter instance handles into task struct */
1334 pTask->hInstance = hInstance;
1335 pTask->hPrevInstance = hPrevInstance;
1337 /* Use DGROUP for 16-bit stack */
1339 if (!(sp = OFFSETOF(pModule->ne_sssp)))
1340 sp = pSegTable[SELECTOROF(pModule->ne_sssp)-1].minsize + pModule->ne_stack;
1341 sp &= ~1;
1342 sp -= sizeof(STACK16FRAME);
1343 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1345 /* Registers at initialization must be:
1346 * ax zero
1347 * bx stack size in bytes
1348 * cx heap size in bytes
1349 * si previous app instance
1350 * di current app instance
1351 * bp zero
1352 * es selector to the PSP
1353 * ds dgroup of the application
1354 * ss stack selector
1355 * sp top of the stack
1357 memset( &context, 0, sizeof(context) );
1358 context.SegCs = GlobalHandleToSel16(pSegTable[SELECTOROF(pModule->ne_csip) - 1].hSeg);
1359 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1360 context.SegEs = pTask->hPDB;
1361 context.SegFs = wine_get_fs();
1362 context.SegGs = wine_get_gs();
1363 context.Eip = OFFSETOF(pModule->ne_csip);
1364 context.Ebx = pModule->ne_stack;
1365 context.Ecx = pModule->ne_heap;
1366 context.Edi = pTask->hInstance;
1367 context.Esi = pTask->hPrevInstance;
1369 /* Now call 16-bit entry point */
1371 TRACE("Starting main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
1372 context.SegCs, context.Eip, context.SegDs,
1373 SELECTOROF(NtCurrentTeb()->WOW32Reserved),
1374 OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
1376 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1377 ExitThread( LOWORD(context.Eax) );
1379 return hInstance; /* error code */
1382 /***********************************************************************
1383 * LoadLibrary (KERNEL.95)
1384 * LoadLibrary16 (KERNEL32.35)
1386 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1388 return LoadModule16(libname, (LPVOID)-1 );
1392 /**********************************************************************
1393 * MODULE_CallWEP
1395 * Call a DLL's WEP, allowing it to shut down.
1396 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1398 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1400 BOOL16 ret;
1401 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1402 if (!WEP) return FALSE;
1404 __TRY
1406 WORD args[1];
1407 DWORD dwRet;
1409 args[0] = WEP_FREE_DLL;
1410 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1411 ret = LOWORD(dwRet);
1413 __EXCEPT_PAGE_FAULT
1415 WARN("Page fault\n");
1416 ret = 0;
1418 __ENDTRY
1420 return ret;
1424 /**********************************************************************
1425 * NE_FreeModule
1427 * Implementation of FreeModule16().
1429 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1431 HMODULE16 *hPrevModule;
1432 NE_MODULE *pModule;
1433 HMODULE16 *pModRef;
1434 int i;
1436 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1437 hModule = pModule->self;
1439 TRACE("%04x count %d\n", hModule, pModule->count );
1441 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1442 else pModule->count = 0;
1444 if (call_wep && !(pModule->ne_flags & NE_FFLAGS_WIN32))
1446 /* Free the objects owned by the DLL module */
1447 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1449 if (pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1450 MODULE_CallWEP( hModule );
1451 else
1452 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1455 TRACE_(loaddll)("Unloaded module %s : %s\n", debugstr_a(NE_MODULE_NAME(pModule)),
1456 (pModule->ne_flags & NE_FFLAGS_BUILTIN) ? "builtin" : "native");
1458 /* Clear magic number just in case */
1460 pModule->ne_magic = pModule->self = 0;
1461 if (pModule->owner32) FreeLibrary( pModule->owner32 );
1462 else if (pModule->mapping) UnmapViewOfFile( pModule->mapping );
1464 /* Remove it from the linked list */
1466 hPrevModule = &hFirstModule;
1467 while (*hPrevModule && (*hPrevModule != hModule))
1469 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1471 if (*hPrevModule) *hPrevModule = pModule->next;
1473 /* Free the referenced modules */
1475 pModRef = (HMODULE16*)((char *)pModule + pModule->ne_modtab);
1476 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
1478 NE_FreeModule( *pModRef, call_wep );
1481 /* Free the module storage */
1483 GlobalFreeAll16( hModule );
1484 return TRUE;
1488 /**********************************************************************
1489 * FreeModule (KERNEL.46)
1491 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1493 return NE_FreeModule( hModule, TRUE );
1497 /***********************************************************************
1498 * FreeLibrary (KERNEL.96)
1499 * FreeLibrary16 (KERNEL32.36)
1501 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1503 TRACE("%04x\n", handle );
1504 FreeModule16( handle );
1508 /***********************************************************************
1509 * GetModuleHandle16 (KERNEL32.@)
1511 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1513 HMODULE16 hModule = hFirstModule;
1514 LPSTR s;
1515 BYTE len, *name_table;
1516 char tmpstr[MAX_PATH];
1517 NE_MODULE *pModule;
1519 TRACE("(%s)\n", name);
1521 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1523 len = strlen(name);
1524 if (!len) return 0;
1526 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1528 /* If 'name' matches exactly the module name of a module:
1529 * Return its handle.
1531 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1533 pModule = NE_GetPtr( hModule );
1534 if (!pModule) break;
1535 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1537 name_table = (BYTE *)pModule + pModule->ne_restab;
1538 if ((*name_table == len) && !strncmp(name, (char*) name_table+1, len))
1539 return hModule;
1542 /* If uppercased 'name' matches exactly the module name of a module:
1543 * Return its handle
1545 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1547 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1549 pModule = NE_GetPtr( hModule );
1550 if (!pModule) break;
1551 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1553 name_table = (BYTE *)pModule + pModule->ne_restab;
1554 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1555 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1556 * lowercased module names, but try to load uppercase DLLs, so this
1557 * 'i' compare is just a quickfix until the loader handles that
1558 * correctly. -MM 990705
1560 if ((*name_table == len) && !NE_strncasecmp(tmpstr, (const char*)name_table+1, len))
1561 return hModule;
1564 /* If the base filename of 'name' matches the base filename of the module
1565 * filename of some module (case-insensitive compare):
1566 * Return its handle.
1569 /* basename: search backwards in passed name to \ / or : */
1570 s = tmpstr + strlen(tmpstr);
1571 while (s > tmpstr)
1573 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1574 break;
1575 s--;
1578 /* search this in loaded filename list */
1579 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1581 char *loadedfn;
1582 OFSTRUCT *ofs;
1584 pModule = NE_GetPtr( hModule );
1585 if (!pModule) break;
1586 if (!pModule->fileinfo) continue;
1587 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1589 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1590 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1591 /* basename: search backwards in pathname to \ / or : */
1592 while (loadedfn > (char*)ofs->szPathName)
1594 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1595 break;
1596 loadedfn--;
1598 /* case insensitive compare ... */
1599 if (!NE_strcasecmp(loadedfn, s))
1600 return hModule;
1602 return 0;
1606 /**********************************************************************
1607 * GetModuleName (KERNEL.27)
1609 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1611 NE_MODULE *pModule;
1612 BYTE *p;
1614 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1615 p = (BYTE *)pModule + pModule->ne_restab;
1616 if (count > *p) count = *p + 1;
1617 if (count > 0)
1619 memcpy( buf, p + 1, count - 1 );
1620 buf[count-1] = '\0';
1622 return TRUE;
1626 /**********************************************************************
1627 * GetModuleFileName (KERNEL.49)
1629 * Comment: see GetModuleFileNameA
1631 * Even if invoked by second instance of a program,
1632 * it still returns path of first one.
1634 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1635 INT16 nSize )
1637 NE_MODULE *pModule;
1639 /* Win95 does not query hModule if set to 0 !
1640 * Is this wrong or maybe Win3.1 only ? */
1641 if (!hModule) hModule = GetCurrentTask();
1643 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1644 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1645 if (pModule->ne_expver < 0x400)
1646 GetShortPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1647 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1648 return strlen(lpFileName);
1652 /**********************************************************************
1653 * GetModuleUsage (KERNEL.48)
1655 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1657 NE_MODULE *pModule = NE_GetPtr( hModule );
1658 return pModule ? pModule->count : 0;
1662 /**********************************************************************
1663 * GetExpWinVer (KERNEL.167)
1665 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1667 NE_MODULE *pModule = NE_GetPtr( hModule );
1668 if ( !pModule ) return 0;
1669 return pModule->ne_expver;
1673 /***********************************************************************
1674 * WinExec (KERNEL.166)
1676 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1678 LPCSTR p, args = NULL;
1679 LPCSTR name_beg, name_end;
1680 LPSTR name, cmdline;
1681 int arglen;
1682 HINSTANCE16 ret;
1683 char buffer[MAX_PATH];
1684 LOADPARAMS16 params;
1685 WORD showCmd[2];
1687 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1689 name_beg = lpCmdLine+1;
1690 p = strchr ( lpCmdLine+1, '"' );
1691 if (p)
1693 name_end = p;
1694 args = strchr ( p, ' ' );
1696 else /* yes, even valid with trailing '"' missing */
1697 name_end = lpCmdLine+strlen(lpCmdLine);
1699 else
1701 name_beg = lpCmdLine;
1702 args = strchr( lpCmdLine, ' ' );
1703 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1706 if ((name_beg == lpCmdLine) && (!args))
1707 { /* just use the original cmdline string as file name */
1708 name = (LPSTR)lpCmdLine;
1710 else
1712 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1713 return ERROR_NOT_ENOUGH_MEMORY;
1714 memcpy( name, name_beg, name_end - name_beg );
1715 name[name_end - name_beg] = '\0';
1718 if (args)
1720 args++;
1721 arglen = strlen(args);
1722 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1723 cmdline[0] = (BYTE)arglen;
1724 strcpy( cmdline + 1, args );
1726 else
1728 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1729 cmdline[0] = cmdline[1] = 0;
1732 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1734 showCmd[0] = 2;
1735 showCmd[1] = nCmdShow;
1737 params.hEnvironment = 0;
1738 params.cmdLine = MapLS( cmdline );
1739 params.showCmd = MapLS( showCmd );
1740 params.reserved = 0;
1742 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1744 ret = LoadModule16( buffer, &params );
1746 else if (!contains_path( name )) /* try 16-bit builtin */
1748 lstrcpynA( buffer, name, sizeof(buffer) );
1749 if (strlen( buffer ) < sizeof(buffer) - 4 && !strchr( buffer, '.' )) strcat( buffer, ".exe" );
1750 ret = LoadModule16( buffer, &params );
1751 if (ret == ERROR_FILE_NOT_FOUND) ret = 21; /* it might be a 32-bit builtin too */
1753 else ret = ERROR_FILE_NOT_FOUND;
1755 UnMapLS( params.cmdLine );
1756 UnMapLS( params.showCmd );
1758 HeapFree( GetProcessHeap(), 0, cmdline );
1759 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1761 if (ret == 21 || ret == ERROR_BAD_FORMAT) /* 32-bit module or unknown executable*/
1763 LOADPARAMS16 params;
1764 WORD showCmd[2];
1765 showCmd[0] = 2;
1766 showCmd[1] = nCmdShow;
1768 arglen = strlen( lpCmdLine );
1769 cmdline = HeapAlloc( GetProcessHeap(), 0, arglen + 1 );
1770 cmdline[0] = (BYTE)arglen;
1771 memcpy( cmdline + 1, lpCmdLine, arglen );
1773 params.hEnvironment = 0;
1774 params.cmdLine = MapLS( cmdline );
1775 params.showCmd = MapLS( showCmd );
1776 params.reserved = 0;
1778 ret = LoadModule16( "winoldap.mod", &params );
1779 UnMapLS( params.cmdLine );
1780 UnMapLS( params.showCmd );
1782 return ret;
1785 /***********************************************************************
1786 * GetProcAddress (KERNEL.50)
1788 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1790 WORD ordinal;
1791 FARPROC16 ret;
1793 if (!hModule) hModule = GetCurrentTask();
1794 hModule = GetExePtr( hModule );
1796 if (HIWORD(name) != 0)
1798 ordinal = NE_GetOrdinal( hModule, name );
1799 TRACE("%04x '%s'\n", hModule, name );
1801 else
1803 ordinal = LOWORD(name);
1804 TRACE("%04x %04x\n", hModule, ordinal );
1806 if (!ordinal) return NULL;
1808 ret = NE_GetEntryPoint( hModule, ordinal );
1810 TRACE("returning %p\n", ret );
1811 return ret;
1815 /***************************************************************************
1816 * HasGPHandler (KERNEL.338)
1818 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1820 HMODULE16 hModule;
1821 int gpOrdinal;
1822 SEGPTR gpPtr;
1823 GPHANDLERDEF *gpHandler;
1825 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1826 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1827 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1828 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1829 && (gpHandler = MapSL( gpPtr )) != NULL )
1831 while (gpHandler->selector)
1833 if ( SELECTOROF(address) == gpHandler->selector
1834 && OFFSETOF(address) >= gpHandler->rangeStart
1835 && OFFSETOF(address) < gpHandler->rangeEnd )
1836 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1837 gpHandler++;
1841 return 0;
1845 /**********************************************************************
1846 * GetModuleHandle (KERNEL.47)
1848 * Find a module from a module name.
1850 * NOTE: The current implementation works the same way the Windows 95 one
1851 * does. Do not try to 'fix' it, fix the callers.
1852 * + It does not do ANY extension handling (except that strange .EXE bit)!
1853 * + It does not care about paths, just about basenames. (same as Windows)
1855 * RETURNS
1856 * LOWORD:
1857 * the win16 module handle if found
1858 * 0 if not
1859 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1860 * Always hFirstModule
1862 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1864 if (HIWORD(name) == 0)
1865 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1866 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1869 /**********************************************************************
1870 * NE_GetModuleByFilename
1872 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1874 HMODULE16 hModule;
1875 LPSTR s, p;
1876 BYTE len, *name_table;
1877 char tmpstr[MAX_PATH];
1878 NE_MODULE *pModule;
1880 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1882 /* If the base filename of 'name' matches the base filename of the module
1883 * filename of some module (case-insensitive compare):
1884 * Return its handle.
1887 /* basename: search backwards in passed name to \ / or : */
1888 s = tmpstr + strlen(tmpstr);
1889 while (s > tmpstr)
1891 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1892 break;
1893 s--;
1896 /* search this in loaded filename list */
1897 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1899 char *loadedfn;
1900 OFSTRUCT *ofs;
1902 pModule = NE_GetPtr( hModule );
1903 if (!pModule) break;
1904 if (!pModule->fileinfo) continue;
1905 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1907 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1908 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1909 /* basename: search backwards in pathname to \ / or : */
1910 while (loadedfn > (char*)ofs->szPathName)
1912 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1913 break;
1914 loadedfn--;
1916 /* case insensitive compare ... */
1917 if (!NE_strcasecmp(loadedfn, s))
1918 return hModule;
1920 /* If basename (without ext) matches the module name of a module:
1921 * Return its handle.
1924 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1925 len = strlen(s);
1927 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1929 pModule = NE_GetPtr( hModule );
1930 if (!pModule) break;
1931 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1933 name_table = (BYTE *)pModule + pModule->ne_restab;
1934 if ((*name_table == len) && !NE_strncasecmp(s, (const char*)name_table+1, len))
1935 return hModule;
1938 return 0;
1941 /***********************************************************************
1942 * GetProcAddress16 (KERNEL32.37)
1943 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1945 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1947 if (!hModule) return 0;
1948 if (HIWORD(hModule))
1950 WARN("hModule is Win32 handle (%p)\n", hModule );
1951 return 0;
1953 return GetProcAddress16( LOWORD(hModule), name );
1956 /***************************************************************************
1957 * IsRomModule (KERNEL.323)
1959 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1961 return FALSE;
1964 /***************************************************************************
1965 * IsRomFile (KERNEL.326)
1967 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1969 return FALSE;
1972 /***********************************************************************
1973 * create_dummy_module
1975 * Create a dummy NE module for Win32 or Winelib.
1977 static HMODULE16 create_dummy_module( HMODULE module32 )
1979 HMODULE16 hModule;
1980 NE_MODULE *pModule;
1981 SEGTABLEENTRY *pSegment;
1982 char *pStr,*s;
1983 unsigned int len;
1984 const char* basename;
1985 OFSTRUCT *ofs;
1986 int of_size, size;
1987 char filename[MAX_PATH];
1988 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
1990 if (!nt) return ERROR_BAD_FORMAT;
1992 /* Extract base filename */
1993 len = GetModuleFileNameA( module32, filename, sizeof(filename) );
1994 if (!len || len >= sizeof(filename)) return ERROR_BAD_FORMAT;
1995 basename = strrchr(filename, '\\');
1996 if (!basename) basename = filename;
1997 else basename++;
1998 len = strlen(basename);
1999 if ((s = strchr(basename, '.'))) len = s - basename;
2001 /* Allocate module */
2002 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
2003 + strlen(filename) + 1;
2004 size = sizeof(NE_MODULE) +
2005 /* loaded file info */
2006 ((of_size + 3) & ~3) +
2007 /* segment table: DS,CS */
2008 2 * sizeof(SEGTABLEENTRY) +
2009 /* name table */
2010 len + 2 +
2011 /* several empty tables */
2014 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2015 if (!hModule) return ERROR_BAD_FORMAT;
2017 FarSetOwner16( hModule, hModule );
2018 pModule = GlobalLock16( hModule );
2020 /* Set all used entries */
2021 pModule->ne_magic = IMAGE_OS2_SIGNATURE;
2022 pModule->count = 1;
2023 pModule->next = 0;
2024 pModule->ne_flags = NE_FFLAGS_WIN32;
2025 pModule->ne_autodata = 0;
2026 pModule->ne_sssp = MAKESEGPTR( 0, 1 );
2027 pModule->ne_csip = MAKESEGPTR( 0, 2 );
2028 pModule->ne_heap = 0;
2029 pModule->ne_stack = 0;
2030 pModule->ne_cseg = 2;
2031 pModule->ne_cmod = 0;
2032 pModule->ne_cbnrestab = 0;
2033 pModule->fileinfo = sizeof(NE_MODULE);
2034 pModule->ne_exetyp = NE_OSFLAGS_WINDOWS;
2035 pModule->self = hModule;
2036 pModule->module32 = module32;
2038 /* Set version and flags */
2039 pModule->ne_expver = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2040 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2041 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2042 pModule->ne_flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2044 /* Set loaded file information */
2045 ofs = (OFSTRUCT *)(pModule + 1);
2046 memset( ofs, 0, of_size );
2047 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2048 strcpy( ofs->szPathName, filename );
2050 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2051 pModule->ne_segtab = (char *)pSegment - (char *)pModule;
2052 /* Data segment */
2053 pSegment->size = 0;
2054 pSegment->flags = NE_SEGFLAGS_DATA;
2055 pSegment->minsize = 0x1000;
2056 pSegment++;
2057 /* Code segment */
2058 pSegment->flags = 0;
2059 pSegment++;
2061 /* Module name */
2062 pStr = (char *)pSegment;
2063 pModule->ne_restab = pStr - (char *)pModule;
2064 assert(len<256);
2065 *pStr = len;
2066 lstrcpynA( pStr+1, basename, len+1 );
2067 pStr += len+2;
2069 /* All tables zero terminated */
2070 pModule->ne_rsrctab = pModule->ne_imptab = pModule->ne_enttab = pStr - (char *)pModule;
2072 NE_RegisterModule( pModule );
2073 pModule->owner32 = LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2074 return hModule;
2077 /***********************************************************************
2078 * PrivateLoadLibrary (KERNEL32.@)
2080 * FIXME: rough guesswork, don't know what "Private" means
2082 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2084 return LoadLibrary16(libname);
2087 /***********************************************************************
2088 * PrivateFreeLibrary (KERNEL32.@)
2090 * FIXME: rough guesswork, don't know what "Private" means
2092 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2094 FreeLibrary16(handle);
2097 /***********************************************************************
2098 * LoadLibrary32 (KERNEL.452)
2099 * LoadSystemLibrary32 (KERNEL.482)
2101 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2103 HMODULE hModule;
2104 DWORD count;
2106 ReleaseThunkLock( &count );
2107 hModule = LoadLibraryA( libname );
2108 RestoreThunkLock( count );
2109 return hModule;
2112 /***************************************************************************
2113 * MapHModuleLS (KERNEL32.@)
2115 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2117 HMODULE16 ret;
2118 NE_MODULE *pModule;
2120 if (!hmod)
2121 return TASK_GetCurrent()->hInstance;
2122 if (!HIWORD(hmod))
2123 return LOWORD(hmod); /* we already have a 16 bit module handle */
2124 pModule = GlobalLock16(hFirstModule);
2125 while (pModule) {
2126 if (pModule->module32 == hmod)
2127 return pModule->self;
2128 pModule = GlobalLock16(pModule->next);
2130 if ((ret = create_dummy_module( hmod )) < 32)
2132 SetLastError(ret);
2133 ret = 0;
2135 return ret;
2138 /***************************************************************************
2139 * MapHModuleSL (KERNEL32.@)
2141 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2143 NE_MODULE *pModule;
2145 if (!hmod) {
2146 TDB *pTask = TASK_GetCurrent();
2147 hmod = pTask->hModule;
2149 pModule = GlobalLock16(hmod);
2150 if ((pModule->ne_magic != IMAGE_OS2_SIGNATURE) || !(pModule->ne_flags & NE_FFLAGS_WIN32))
2151 return 0;
2152 return pModule->module32;
2155 /***************************************************************************
2156 * MapHInstLS (KERNEL.472)
2158 void WINAPI MapHInstLS16( CONTEXT86 *context )
2160 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2163 /***************************************************************************
2164 * MapHInstSL (KERNEL.473)
2166 void WINAPI MapHInstSL16( CONTEXT86 *context )
2168 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2171 #ifdef __i386__
2173 /***************************************************************************
2174 * MapHInstLS (KERNEL32.@)
2176 __ASM_STDCALL_FUNC( MapHInstLS, 0,
2177 "pushl %eax\n\t"
2178 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n\t"
2179 "ret" )
2181 /***************************************************************************
2182 * MapHInstSL (KERNEL32.@)
2184 __ASM_STDCALL_FUNC( MapHInstSL, 0,
2185 "pushl %eax\n\t"
2186 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n\t"
2187 "ret" )
2189 /***************************************************************************
2190 * MapHInstLS_PN (KERNEL32.@)
2192 __ASM_STDCALL_FUNC( MapHInstLS_PN, 0,
2193 "testl %eax,%eax\n\t"
2194 "jz 1f\n\t"
2195 "pushl %eax\n\t"
2196 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n"
2197 "1:\tret" )
2199 /***************************************************************************
2200 * MapHInstSL_PN (KERNEL32.@)
2202 __ASM_STDCALL_FUNC( MapHInstSL_PN, 0,
2203 "andl $0xffff,%eax\n\t"
2204 "jz 1f\n\t"
2205 "pushl %eax\n\t"
2206 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n"
2207 "1:\tret" )
2209 #endif /* __i386__ */