msi: Add support for the Unicode version of the global UI handler.
[wine.git] / dlls / kernel32 / ne_module.c
blob8f22ccb6acbf51a9349f9cb9eb7691fb0eb0c95c
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 "kernel_private.h"
40 #include "kernel16_private.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(module);
45 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 #include "pshpack1.h"
49 typedef struct _GPHANDLERDEF
51 WORD selector;
52 WORD rangeStart;
53 WORD rangeEnd;
54 WORD handler;
55 } GPHANDLERDEF;
56 #include "poppack.h"
59 * Segment table entry
61 struct ne_segment_table_entry_s
63 WORD seg_data_offset; /* Sector offset of segment data */
64 WORD seg_data_length; /* Length of segment data */
65 WORD seg_flags; /* Flags associated with this segment */
66 WORD min_alloc; /* Minimum allocation size for this */
69 #define hFirstModule (pThhook->hExeHead)
71 struct builtin_dll
73 const IMAGE_DOS_HEADER *header; /* module headers */
74 const char *file_name; /* module file name */
77 /* Table of all built-in DLLs */
79 #define MAX_DLLS 50
81 static struct builtin_dll builtin_dlls[MAX_DLLS];
83 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
84 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
86 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
88 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
91 /* patch all the flat cs references of the code segment if necessary */
92 static inline void patch_code_segment( NE_MODULE *pModule )
94 #ifdef __i386__
95 int i;
96 CALLFROM16 *call;
97 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
99 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
100 if (!(pSeg->flags & NE_SEGFLAGS_DATA)) break; /* found the code segment */
102 call = GlobalLock16( pSeg->hSeg );
104 /* patch glue code address and code selector */
105 for (i = 0; call[i].pushl == 0x68; i++)
107 if (call[i].ret[0] == 0xca66 || call[i].ret[0] == 0xcb66) /* register entry point? */
108 call[i].glue = __wine_call_from_16_regs;
109 else
110 call[i].glue = __wine_call_from_16;
111 call[i].flatcs = wine_get_cs();
114 if (TRACE_ON(relay)) /* patch relay functions to all point to relay_call_from_16 */
115 for (i = 0; call[i].pushl == 0x68; i++) call[i].relay = relay_call_from_16;
116 #endif
120 /***********************************************************************
121 * contains_path
123 static inline int contains_path( LPCSTR name )
125 return ((*name && (name[1] == ':')) || strchr(name, '/') || strchr(name, '\\'));
129 /***********************************************************************
130 * NE_strcasecmp
132 * locale-independent case conversion for module lookups
134 static int NE_strcasecmp( const char *str1, const char *str2 )
136 int ret = 0;
137 for ( ; ; str1++, str2++)
138 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
139 return ret;
143 /***********************************************************************
144 * NE_strncasecmp
146 * locale-independent case conversion for module lookups
148 static int NE_strncasecmp( const char *str1, const char *str2, int len )
150 int ret = 0;
151 for ( ; len > 0; len--, str1++, str2++)
152 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
153 return ret;
157 /***********************************************************************
158 * find_dll_descr
160 * Find a descriptor in the list
162 static const IMAGE_DOS_HEADER *find_dll_descr( const char *dllname, const char **file_name )
164 int i;
165 const IMAGE_DOS_HEADER *mz_header;
166 const IMAGE_OS2_HEADER *ne_header;
167 const BYTE *name_table;
169 for (i = 0; i < MAX_DLLS; i++)
171 mz_header = builtin_dlls[i].header;
172 if (mz_header)
174 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mz_header + mz_header->e_lfanew);
175 name_table = (const BYTE *)ne_header + ne_header->ne_restab;
177 /* check the dll file name */
178 if (!NE_strcasecmp( builtin_dlls[i].file_name, dllname ) ||
179 /* check the dll module name (without extension) */
180 (!NE_strncasecmp( dllname, (const char*)name_table+1, *name_table ) &&
181 !strcmp( dllname + *name_table, ".dll" )))
183 *file_name = builtin_dlls[i].file_name;
184 return builtin_dlls[i].header;
188 return NULL;
192 /***********************************************************************
193 * __wine_dll_register_16 (KERNEL32.@)
195 * Register a built-in DLL descriptor.
197 void __wine_dll_register_16( const IMAGE_DOS_HEADER *header, const char *file_name )
199 int i;
201 for (i = 0; i < MAX_DLLS; i++)
203 if (builtin_dlls[i].header) continue;
204 builtin_dlls[i].header = header;
205 builtin_dlls[i].file_name = file_name;
206 break;
208 assert( i < MAX_DLLS );
212 /***********************************************************************
213 * __wine_dll_unregister_16 (KERNEL32.@)
215 * Unregister a built-in DLL descriptor.
217 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER *header )
219 int i;
221 for (i = 0; i < MAX_DLLS; i++)
223 if (builtin_dlls[i].header != header) continue;
224 builtin_dlls[i].header = NULL;
225 break;
230 /***********************************************************************
231 * NE_GetPtr
233 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
235 return GlobalLock16( GetExePtr(hModule) );
239 /**********************************************************************
240 * NE_RegisterModule
242 static void NE_RegisterModule( NE_MODULE *pModule )
244 pModule->next = hFirstModule;
245 hFirstModule = pModule->self;
249 /***********************************************************************
250 * NE_DumpModule
252 void NE_DumpModule( HMODULE16 hModule )
254 int i, ordinal;
255 SEGTABLEENTRY *pSeg;
256 BYTE *pstr;
257 WORD *pword;
258 NE_MODULE *pModule;
259 ET_BUNDLE *bundle;
260 ET_ENTRY *entry;
262 if (!(pModule = NE_GetPtr( hModule )))
264 ERR( "**** %04x is not a module handle\n", hModule );
265 return;
268 /* Dump the module info */
269 TRACE( "---\n" );
270 TRACE( "Module %04x:\n", hModule );
271 TRACE( "count=%d flags=%04x heap=%d stack=%d\n",
272 pModule->count, pModule->ne_flags,
273 pModule->ne_heap, pModule->ne_stack );
274 TRACE( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
275 SELECTOROF(pModule->ne_csip), OFFSETOF(pModule->ne_csip),
276 SELECTOROF(pModule->ne_sssp), OFFSETOF(pModule->ne_sssp),
277 pModule->ne_autodata, pModule->ne_cseg, pModule->ne_cmod );
278 TRACE( "os_flags=%d swap_area=%d version=%04x\n",
279 pModule->ne_exetyp, pModule->ne_swaparea, pModule->ne_expver );
280 if (pModule->ne_flags & NE_FFLAGS_WIN32)
281 TRACE( "PE module=%p\n", pModule->module32 );
283 /* Dump the file info */
284 TRACE( "---\n" );
285 TRACE( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
287 /* Dump the segment table */
288 TRACE( "---\n" );
289 TRACE( "Segment table:\n" );
290 pSeg = NE_SEG_TABLE( pModule );
291 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
292 TRACE( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
293 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
294 pSeg->minsize, pSeg->hSeg );
296 /* Dump the resource table */
297 TRACE( "---\n" );
298 TRACE( "Resource table:\n" );
299 if (pModule->ne_rsrctab)
301 pword = (WORD *)((BYTE *)pModule + pModule->ne_rsrctab);
302 TRACE( "Alignment: %d\n", *pword++ );
303 while (*pword)
305 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
306 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
307 TRACE( "id=%04x count=%d\n", ptr->type_id, ptr->count );
308 for (i = 0; i < ptr->count; i++, pname++)
309 TRACE( "offset=%d len=%d id=%04x\n",
310 pname->offset, pname->length, pname->id );
311 pword = (WORD *)pname;
314 else TRACE( "None\n" );
316 /* Dump the resident name table */
317 TRACE( "---\n" );
318 TRACE( "Resident-name table:\n" );
319 pstr = (BYTE*) pModule + pModule->ne_restab;
320 while (*pstr)
322 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
323 *(WORD *)(pstr + *pstr + 1) );
324 pstr += *pstr + 1 + sizeof(WORD);
327 /* Dump the module reference table */
328 TRACE( "---\n" );
329 TRACE( "Module ref table:\n" );
330 if (pModule->ne_modtab)
332 pword = (WORD *)((BYTE *)pModule + pModule->ne_modtab);
333 for (i = 0; i < pModule->ne_cmod; i++, pword++)
335 char name[10];
336 GetModuleName16( *pword, name, sizeof(name) );
337 TRACE( "%d: %04x -> '%s'\n", i, *pword, name );
340 else TRACE( "None\n" );
342 /* Dump the entry table */
343 TRACE( "---\n" );
344 TRACE( "Entry table:\n" );
345 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->ne_enttab);
346 do {
347 entry = (ET_ENTRY *)((BYTE *)bundle+6);
348 TRACE( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
349 ordinal = bundle->first;
350 while (ordinal < bundle->last)
352 if (entry->type == 0xff)
353 TRACE("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
354 else
355 TRACE("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
356 entry++;
358 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
360 /* Dump the non-resident names table */
361 TRACE( "---\n" );
362 TRACE( "Non-resident names table:\n" );
363 if (pModule->nrname_handle)
365 pstr = GlobalLock16( pModule->nrname_handle );
366 while (*pstr)
368 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
369 *(WORD *)(pstr + *pstr + 1) );
370 pstr += *pstr + 1 + sizeof(WORD);
373 TRACE( "\n" );
377 /***********************************************************************
378 * NE_WalkModules
380 * Walk the module list and print the modules.
382 void NE_WalkModules(void)
384 HMODULE16 hModule = hFirstModule;
385 MESSAGE( "Module Flags Name\n" );
386 while (hModule)
388 NE_MODULE *pModule = NE_GetPtr( hModule );
389 if (!pModule)
391 MESSAGE( "Bad module %04x in list\n", hModule );
392 return;
394 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->ne_flags,
395 *((char *)pModule + pModule->ne_restab),
396 (char *)pModule + pModule->ne_restab + 1 );
397 hModule = pModule->next;
402 /***********************************************************************
403 * NE_InitResourceHandler
405 * Fill in 'resloader' fields in the resource table.
407 static void NE_InitResourceHandler( HMODULE16 hModule )
409 static FARPROC16 proc;
411 NE_TYPEINFO *pTypeInfo;
412 NE_MODULE *pModule;
414 if (!(pModule = NE_GetPtr( hModule )) || !pModule->ne_rsrctab) return;
416 TRACE("InitResourceHandler[%04x]\n", hModule );
418 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
420 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
421 while(pTypeInfo->type_id)
423 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
424 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
429 /***********************************************************************
430 * NE_GetOrdinal
432 * Lookup the ordinal for a given name.
434 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
436 char buffer[256], *p;
437 BYTE *cpnt;
438 BYTE len;
439 NE_MODULE *pModule;
441 if (!(pModule = NE_GetPtr( hModule ))) return 0;
442 if (pModule->ne_flags & NE_FFLAGS_WIN32) return 0;
444 TRACE("(%04x,'%s')\n", hModule, name );
446 /* First handle names of the form '#xxxx' */
448 if (name[0] == '#') return atoi( name + 1 );
450 /* Now copy and uppercase the string */
452 strcpy( buffer, name );
453 for (p = buffer; *p; p++) *p = RtlUpperChar(*p);
454 len = p - buffer;
456 /* First search the resident names */
458 cpnt = (BYTE *)pModule + pModule->ne_restab;
460 /* Skip the first entry (module name) */
461 cpnt += *cpnt + 1 + sizeof(WORD);
462 while (*cpnt)
464 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
466 WORD ordinal;
467 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
468 TRACE(" Found: ordinal=%d\n", ordinal );
469 return ordinal;
471 cpnt += *cpnt + 1 + sizeof(WORD);
474 /* Now search the non-resident names table */
476 if (!pModule->nrname_handle) return 0; /* No non-resident table */
477 cpnt = GlobalLock16( pModule->nrname_handle );
479 /* Skip the first entry (module description string) */
480 cpnt += *cpnt + 1 + sizeof(WORD);
481 while (*cpnt)
483 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
485 WORD ordinal;
486 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
487 TRACE(" Found: ordinal=%d\n", ordinal );
488 return ordinal;
490 cpnt += *cpnt + 1 + sizeof(WORD);
492 return 0;
496 /***********************************************************************
497 * NE_GetEntryPoint
499 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
501 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
504 /***********************************************************************
505 * NE_GetEntryPointEx
507 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
509 NE_MODULE *pModule;
510 WORD sel, offset, i;
512 ET_ENTRY *entry;
513 ET_BUNDLE *bundle;
515 if (!(pModule = NE_GetPtr( hModule ))) return 0;
516 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
518 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
519 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
521 if (!(bundle->next))
522 return 0;
523 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
526 entry = (ET_ENTRY *)((BYTE *)bundle+6);
527 for (i=0; i < (ordinal - bundle->first - 1); i++)
528 entry++;
530 sel = entry->segnum;
531 memcpy( &offset, &entry->offs, sizeof(WORD) );
533 if (sel == 0xfe) sel = 0xffff; /* constant entry */
534 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
535 if (sel==0xffff)
536 return (FARPROC16)MAKESEGPTR( sel, offset );
537 if (!snoop)
538 return (FARPROC16)MAKESEGPTR( sel, offset );
539 else
540 return SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
544 /***********************************************************************
545 * EntryAddrProc (KERNEL.667) Wine-specific export
547 * Return the entry point for a given ordinal.
549 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
551 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
552 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
553 return ret;
556 /***********************************************************************
557 * NE_SetEntryPoint
559 * Change the value of an entry point. Use with caution!
560 * It can only change the offset value, not the selector.
562 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
564 NE_MODULE *pModule;
565 ET_ENTRY *entry;
566 ET_BUNDLE *bundle;
567 int i;
569 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
570 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
572 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
573 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
575 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
576 if (!(bundle->next)) return 0;
579 entry = (ET_ENTRY *)((BYTE *)bundle+6);
580 for (i=0; i < (ordinal - bundle->first - 1); i++)
581 entry++;
583 memcpy( &entry->offs, &offset, sizeof(WORD) );
584 return TRUE;
588 /***********************************************************************
589 * build_bundle_data
591 * Build the entry table bundle data from the on-disk format. Helper for build_module.
593 static void *build_bundle_data( NE_MODULE *pModule, void *dest, const BYTE *table )
595 ET_BUNDLE *oldbundle, *bundle = dest;
596 ET_ENTRY *entry;
597 BYTE nr_entries, type;
599 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
600 entry = (ET_ENTRY *)((BYTE *)bundle+6);
602 while ((nr_entries = *table++))
604 if ((type = *table++))
606 bundle->last += nr_entries;
607 if (type == 0xff)
609 while (nr_entries--)
611 entry->type = type;
612 entry->flags = *table++;
613 table += sizeof(WORD);
614 entry->segnum = *table++;
615 entry->offs = *(const WORD *)table;
616 table += sizeof(WORD);
617 entry++;
620 else
622 while (nr_entries--)
624 entry->type = type;
625 entry->flags = *table++;
626 entry->segnum = type;
627 entry->offs = *(const WORD *)table;
628 table += sizeof(WORD);
629 entry++;
633 else
635 if (bundle->first == bundle->last)
637 bundle->first += nr_entries;
638 bundle->last += nr_entries;
640 else
642 oldbundle = bundle;
643 oldbundle->next = (char *)entry - (char *)pModule;
644 bundle = (ET_BUNDLE *)entry;
645 bundle->first = bundle->last = oldbundle->last + nr_entries;
646 bundle->next = 0;
647 entry = (ET_ENTRY*)(((BYTE*)entry)+sizeof(ET_BUNDLE));
651 return entry;
655 /***********************************************************************
656 * build_module
658 * Build the in-memory module from the on-disk data.
660 static HMODULE16 build_module( const void *mapping, SIZE_T mapping_size, LPCSTR path )
662 const IMAGE_DOS_HEADER *mz_header = mapping;
663 const IMAGE_OS2_HEADER *ne_header;
664 const struct ne_segment_table_entry_s *pSeg;
665 const void *ptr;
666 int i;
667 size_t size;
668 HMODULE16 hModule;
669 NE_MODULE *pModule;
670 BYTE *buffer, *pData, *end;
671 OFSTRUCT *ofs;
673 if (mapping_size < sizeof(*mz_header)) return ERROR_BAD_FORMAT;
674 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE) return ERROR_BAD_FORMAT;
675 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mapping + mz_header->e_lfanew);
676 if (mz_header->e_lfanew + sizeof(*ne_header) > mapping_size) return ERROR_BAD_FORMAT;
677 if (ne_header->ne_magic == IMAGE_NT_SIGNATURE) return 21; /* win32 exe */
678 if (ne_header->ne_magic == IMAGE_OS2_SIGNATURE_LX)
680 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path);
681 return 12;
683 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE) return ERROR_BAD_FORMAT;
685 /* We now have a valid NE header */
687 /* check to be able to fall back to loading OS/2 programs as DOS
688 * FIXME: should this check be reversed in order to be less strict?
689 * (only fail for OS/2 ne_exetyp 0x01 here?) */
690 if ((ne_header->ne_exetyp != 0x02 /* Windows */)
691 && (ne_header->ne_exetyp != 0x04) /* Windows 386 */)
692 return ERROR_BAD_FORMAT;
694 size = sizeof(NE_MODULE) +
695 /* segment table */
696 ne_header->ne_cseg * sizeof(SEGTABLEENTRY) +
697 /* resource table */
698 ne_header->ne_restab - ne_header->ne_rsrctab +
699 /* resident names table */
700 ne_header->ne_modtab - ne_header->ne_restab +
701 /* module ref table */
702 ne_header->ne_cmod * sizeof(WORD) +
703 /* imported names table */
704 ne_header->ne_enttab - ne_header->ne_imptab +
705 /* entry table length */
706 ne_header->ne_cbenttab +
707 /* entry table extra conversion space */
708 sizeof(ET_BUNDLE) +
709 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6) +
710 /* loaded file info */
711 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
713 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
714 if (!hModule) return ERROR_BAD_FORMAT;
716 FarSetOwner16( hModule, hModule );
717 pModule = GlobalLock16( hModule );
718 memcpy( pModule, ne_header, sizeof(*ne_header) );
719 pModule->count = 0;
720 /* check programs for default minimal stack size */
721 if (!(pModule->ne_flags & NE_FFLAGS_LIBMODULE) && (pModule->ne_stack < 0x1400))
722 pModule->ne_stack = 0x1400;
724 pModule->self = hModule;
725 pModule->mapping = mapping;
726 pModule->mapping_size = mapping_size;
728 pData = (BYTE *)(pModule + 1);
730 /* Clear internal Wine flags in case they are set in the EXE file */
732 pModule->ne_flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
734 /* Get the segment table */
736 pModule->ne_segtab = pData - (BYTE *)pModule;
737 if (!(pSeg = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_segtab,
738 ne_header->ne_cseg * sizeof(struct ne_segment_table_entry_s) )))
739 goto failed;
740 for (i = ne_header->ne_cseg; i > 0; i--, pSeg++)
742 memcpy( pData, pSeg, sizeof(*pSeg) );
743 pData += sizeof(SEGTABLEENTRY);
746 /* Get the resource table */
748 if (ne_header->ne_rsrctab < ne_header->ne_restab)
750 pModule->ne_rsrctab = pData - (BYTE *)pModule;
751 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_rsrctab,
752 ne_header->ne_restab - ne_header->ne_rsrctab )) goto failed;
753 pData += ne_header->ne_restab - ne_header->ne_rsrctab;
755 else pModule->ne_rsrctab = 0; /* No resource table */
757 /* Get the resident names table */
759 pModule->ne_restab = pData - (BYTE *)pModule;
760 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_restab,
761 ne_header->ne_modtab - ne_header->ne_restab )) goto failed;
762 pData += ne_header->ne_modtab - ne_header->ne_restab;
764 /* Get the module references table */
766 if (ne_header->ne_cmod > 0)
768 pModule->ne_modtab = pData - (BYTE *)pModule;
769 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_modtab,
770 ne_header->ne_cmod * sizeof(WORD) )) goto failed;
771 pData += ne_header->ne_cmod * sizeof(WORD);
773 else pModule->ne_modtab = 0; /* No module references */
775 /* Get the imported names table */
777 pModule->ne_imptab = pData - (BYTE *)pModule;
778 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_imptab,
779 ne_header->ne_enttab - ne_header->ne_imptab )) goto failed;
780 pData += ne_header->ne_enttab - ne_header->ne_imptab;
782 /* Load entry table, convert it to the optimized version used by Windows */
784 pModule->ne_enttab = pData - (BYTE *)pModule;
785 if (!(ptr = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_enttab,
786 ne_header->ne_cbenttab ))) goto failed;
787 end = build_bundle_data( pModule, pData, ptr );
789 pData += ne_header->ne_cbenttab + sizeof(ET_BUNDLE) +
790 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6);
792 if (end > pData)
794 FIXME( "not enough space for entry table for %s\n", debugstr_a(path) );
795 goto failed;
798 /* Store the filename information */
800 pModule->fileinfo = pData - (BYTE *)pModule;
801 ofs = (OFSTRUCT *)pData;
802 ofs->cBytes = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path);
803 ofs->fFixedDisk = 1;
804 strcpy( ofs->szPathName, path );
805 pData += ofs->cBytes + 1;
806 assert( (BYTE *)pModule + size <= pData );
808 /* Get the non-resident names table */
810 if (ne_header->ne_cbnrestab)
812 pModule->nrname_handle = GlobalAlloc16( 0, ne_header->ne_cbnrestab );
813 if (!pModule->nrname_handle) goto failed;
814 FarSetOwner16( pModule->nrname_handle, hModule );
815 buffer = GlobalLock16( pModule->nrname_handle );
816 if (!NE_READ_DATA( pModule, buffer, ne_header->ne_nrestab, ne_header->ne_cbnrestab ))
818 GlobalFree16( pModule->nrname_handle );
819 goto failed;
822 else pModule->nrname_handle = 0;
824 /* Allocate a segment for the implicitly-loaded DLLs */
826 if (pModule->ne_cmod)
828 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
829 (pModule->ne_cmod+1)*sizeof(HMODULE16) );
830 if (!pModule->dlls_to_init)
832 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
833 goto failed;
835 FarSetOwner16( pModule->dlls_to_init, hModule );
837 else pModule->dlls_to_init = 0;
839 NE_RegisterModule( pModule );
840 return hModule;
842 failed:
843 GlobalFree16( hModule );
844 return ERROR_BAD_FORMAT;
848 /***********************************************************************
849 * NE_LoadDLLs
851 * Load all DLLs implicitly linked to a module.
853 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
855 int i;
856 WORD *pModRef = (WORD *)((char *)pModule + pModule->ne_modtab);
857 WORD *pDLLs = GlobalLock16( pModule->dlls_to_init );
859 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
861 char buffer[260], *p;
862 BYTE *pstr = (BYTE *)pModule + pModule->ne_imptab + *pModRef;
863 memcpy( buffer, pstr + 1, *pstr );
864 *(buffer + *pstr) = 0; /* terminate it */
866 TRACE("Loading '%s'\n", buffer );
867 if (!(*pModRef = GetModuleHandle16( buffer )))
869 /* If the DLL is not loaded yet, load it and store */
870 /* its handle in the list of DLLs to initialize. */
871 HMODULE16 hDLL;
873 /* Append .DLL to name if no extension present */
874 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
875 strcat( buffer, ".DLL" );
877 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
879 /* FIXME: cleanup what was done */
881 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
882 buffer, *((BYTE*)pModule + pModule->ne_restab),
883 (char *)pModule + pModule->ne_restab + 1, hDLL );
884 return FALSE;
886 *pModRef = GetExePtr( hDLL );
887 *pDLLs++ = *pModRef;
889 else /* Increment the reference count of the DLL */
891 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
892 if (pOldDLL) pOldDLL->count++;
895 return TRUE;
899 /**********************************************************************
900 * NE_DoLoadModule
902 * Load first instance of NE module from file.
904 * pModule must point to a module structure prepared by build_module.
905 * This routine must never be called twice on a module.
907 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
909 /* Allocate the segments for this module */
911 if (!NE_CreateAllSegments( pModule ))
912 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
914 /* Load the referenced DLLs */
916 if (!NE_LoadDLLs( pModule ))
917 return ERROR_FILE_NOT_FOUND; /* 2 */
919 /* Load the segments */
921 NE_LoadAllSegments( pModule );
923 /* Make sure the usage count is 1 on the first loading of */
924 /* the module, even if it contains circular DLL references */
926 pModule->count = 1;
928 return NE_GetInstance( pModule );
931 /**********************************************************************
932 * NE_LoadModule
934 * Load first instance of NE module. (Note: caller is responsible for
935 * ensuring the module isn't already loaded!)
937 * If the module turns out to be an executable module, only a
938 * handle to a module stub is returned; this needs to be initialized
939 * by calling NE_DoLoadModule later, in the context of the newly
940 * created process.
942 * If lib_only is TRUE, however, the module is perforce treated
943 * like a DLL module, even if it is an executable module.
946 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
948 NE_MODULE *pModule;
949 HMODULE16 hModule;
950 HINSTANCE16 hInstance;
951 HFILE16 hFile;
952 OFSTRUCT ofs;
953 HANDLE mapping;
954 void *ptr;
955 MEMORY_BASIC_INFORMATION info;
957 /* Open file */
958 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
959 return ERROR_FILE_NOT_FOUND;
961 mapping = CreateFileMappingW( DosFileHandleToWin32Handle(hFile), NULL, PAGE_WRITECOPY, 0, 0, NULL );
962 _lclose16( hFile );
963 if (!mapping) return ERROR_BAD_FORMAT;
965 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 0 );
966 CloseHandle( mapping );
967 if (!ptr) return ERROR_BAD_FORMAT;
969 VirtualQuery( ptr, &info, sizeof(info) );
970 hModule = build_module( ptr, info.RegionSize, ofs.szPathName );
972 if (hModule < 32)
974 UnmapViewOfFile( ptr );
975 return hModule;
978 SNOOP16_RegisterDLL( hModule, ofs.szPathName );
979 NE_InitResourceHandler( hModule );
981 pModule = NE_GetPtr( hModule );
983 if ( !lib_only && !( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) )
984 return hModule;
986 hInstance = NE_DoLoadModule( pModule );
987 if ( hInstance < 32 )
989 /* cleanup ... */
990 NE_FreeModule( hModule, 0 );
993 return hInstance;
997 /***********************************************************************
998 * NE_DoLoadBuiltinModule
1000 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1002 static HMODULE16 NE_DoLoadBuiltinModule( const IMAGE_DOS_HEADER *mz_header, const char *file_name,
1003 HMODULE owner32 )
1005 NE_MODULE *pModule;
1006 HMODULE16 hModule;
1007 HINSTANCE16 hInstance;
1008 OSVERSIONINFOW versionInfo;
1009 SIZE_T mapping_size = ~0UL; /* assume builtins don't contain invalid offsets... */
1011 hModule = build_module( mz_header, mapping_size, file_name );
1012 if (hModule < 32) return hModule;
1013 pModule = GlobalLock16( hModule );
1014 pModule->ne_flags |= NE_FFLAGS_BUILTIN;
1015 pModule->owner32 = owner32;
1017 /* fake the expected version the module should have according to the current Windows version */
1018 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1019 if (GetVersionExW( &versionInfo ))
1020 pModule->ne_expver = MAKEWORD( versionInfo.dwMinorVersion, versionInfo.dwMajorVersion );
1022 hInstance = NE_DoLoadModule( pModule );
1023 if (hInstance < 32) NE_FreeModule( hModule, 0 );
1025 NE_InitResourceHandler( hModule );
1027 if (pModule->ne_heap)
1029 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1;
1030 unsigned int size = pSeg->minsize + pModule->ne_heap;
1031 if (size > 0xfffe) size = 0xfffe;
1032 LocalInit16( GlobalHandleToSel16(pSeg->hSeg), pSeg->minsize, size );
1035 patch_code_segment( pModule );
1037 return hInstance;
1041 /**********************************************************************
1042 * MODULE_LoadModule16
1044 * Load a NE module in the order of the loadorder specification.
1045 * The caller is responsible that the module is not loaded already.
1048 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1050 HINSTANCE16 hinst = 2;
1051 HMODULE16 hModule;
1052 HMODULE mod32 = 0;
1053 NE_MODULE *pModule;
1054 const IMAGE_DOS_HEADER *descr = NULL;
1055 const char *file_name = NULL;
1056 char dllname[32], owner[20], *p;
1057 const char *basename, *main_module;
1058 int owner_exists = FALSE;
1060 /* strip path information */
1062 basename = libname;
1063 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1064 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
1065 if ((p = strrchr( basename, '/' ))) basename = p + 1;
1067 if (strlen(basename) < sizeof(dllname)-6)
1069 strcpy( dllname, basename );
1070 p = strrchr( dllname, '.' );
1071 if (!p) strcat( dllname, ".dll" );
1072 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
1074 strcpy( p, "16" );
1075 if ((mod32 = LoadLibraryA( dllname )))
1077 if (!(descr = (void *)GetProcAddress( mod32, "__wine_spec_dos_header" )))
1079 WARN( "loaded %s but does not contain a 16-bit module\n", debugstr_a(dllname) );
1080 FreeLibrary( mod32 );
1082 else
1084 TRACE( "found %s with embedded 16-bit module\n", debugstr_a(dllname) );
1085 file_name = basename;
1087 /* if module has a 32-bit owner, match the load order of the owner */
1088 if ((main_module = (void *)GetProcAddress( mod32, "__wine_spec_main_module" )))
1090 LDR_MODULE *ldr;
1091 HMODULE main_owner = LoadLibraryA( main_module );
1093 if (!main_owner)
1095 WARN( "couldn't load owner %s for 16-bit dll %s\n", main_module, dllname );
1096 FreeLibrary( mod32 );
1097 return ERROR_FILE_NOT_FOUND;
1099 /* check if module was loaded native */
1100 if (LdrFindEntryForAddress( main_owner, &ldr ) || !(ldr->Flags & LDR_WINE_INTERNAL))
1102 FreeLibrary( mod32 );
1103 descr = NULL;
1105 FreeLibrary( main_owner );
1109 *p = 0;
1111 /* old-style 16-bit placeholders support, to be removed at some point */
1112 if (!mod32 && wine_dll_get_owner( dllname, owner, sizeof(owner), &owner_exists ) != -1)
1114 mod32 = LoadLibraryA( owner );
1115 if (mod32)
1117 if (!(descr = find_dll_descr( dllname, &file_name )))
1119 FreeLibrary( mod32 );
1120 owner_exists = 0;
1122 /* loading the 32-bit library can have the side effect of loading the module */
1123 /* if so, simply incr the ref count and return the module */
1124 if ((hModule = GetModuleHandle16( libname )))
1126 TRACE( "module %s already loaded by owner\n", libname );
1127 pModule = NE_GetPtr( hModule );
1128 if (pModule) pModule->count++;
1129 FreeLibrary( mod32 );
1130 return hModule;
1133 else
1135 /* it's probably disabled by the load order config */
1136 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner, dllname );
1137 return ERROR_FILE_NOT_FOUND;
1142 if (descr)
1144 TRACE("Trying built-in '%s'\n", libname);
1145 hinst = NE_DoLoadBuiltinModule( descr, file_name, mod32 );
1146 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : builtin\n", debugstr_a(file_name));
1148 else
1150 TRACE("Trying native dll '%s'\n", libname);
1151 hinst = NE_LoadModule(libname, lib_only);
1152 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : native\n", debugstr_a(libname));
1153 if (hinst == ERROR_FILE_NOT_FOUND && owner_exists) hinst = 21; /* win32 module */
1156 if (hinst > 32 && !implicit)
1158 hModule = GetModuleHandle16(libname);
1159 if(!hModule)
1161 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1162 libname, hinst);
1163 return ERROR_INVALID_HANDLE;
1166 pModule = NE_GetPtr(hModule);
1167 if(!pModule)
1169 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1170 libname, hinst);
1171 return ERROR_INVALID_HANDLE;
1174 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1177 * Call initialization routines for all loaded DLLs. Note that
1178 * when we load implicitly linked DLLs this will be done by InitTask().
1180 if(pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1182 NE_InitializeDLLs(hModule);
1183 NE_DllProcessAttach(hModule);
1186 return hinst; /* The last error that occurred */
1190 /**********************************************************************
1191 * NE_CreateThread
1193 * Create the thread for a 16-bit module.
1195 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1197 HANDLE hThread;
1198 TDB *pTask;
1199 HTASK16 hTask;
1200 HINSTANCE16 instance = 0;
1202 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1203 return 0;
1205 /* Post event to start the task */
1206 PostEvent16( hTask );
1208 /* Wait until we get the instance handle */
1211 DirectedYield16( hTask );
1212 if (!IsTask16( hTask )) /* thread has died */
1214 DWORD exit_code;
1215 WaitForSingleObject( hThread, INFINITE );
1216 GetExitCodeThread( hThread, &exit_code );
1217 CloseHandle( hThread );
1218 return exit_code;
1220 if (!(pTask = GlobalLock16( hTask ))) break;
1221 instance = pTask->hInstance;
1222 GlobalUnlock16( hTask );
1223 } while (!instance);
1225 CloseHandle( hThread );
1226 return instance;
1230 /**********************************************************************
1231 * LoadModule (KERNEL.45)
1233 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1235 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1236 LOADPARAMS16 *params;
1237 HMODULE16 hModule;
1238 NE_MODULE *pModule;
1239 LPSTR cmdline;
1240 WORD cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
1242 if (name == NULL) return 0;
1244 TRACE("name %s, paramBlock %p\n", name, paramBlock);
1246 /* Load module */
1248 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1250 /* Special case: second instance of an already loaded NE module */
1252 if ( !( pModule = NE_GetPtr( hModule ) ) ) return ERROR_BAD_FORMAT;
1253 if ( pModule->module32 ) return (HINSTANCE16)21;
1255 /* Increment refcount */
1257 pModule->count++;
1259 else
1261 /* Main case: load first instance of NE module */
1263 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1264 return hModule;
1266 if ( !(pModule = NE_GetPtr( hModule )) )
1267 return ERROR_BAD_FORMAT;
1270 /* If library module, we just retrieve the instance handle */
1272 if ( ( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1273 return NE_GetInstance( pModule );
1276 * At this point, we need to create a new process.
1278 * pModule points either to an already loaded module, whose refcount
1279 * has already been incremented (to avoid having the module vanish
1280 * in the meantime), or else to a stub module which contains only header
1281 * information.
1283 params = paramBlock;
1284 if (params->showCmd)
1285 cmdShow = ((WORD *)MapSL( params->showCmd ))[1];
1286 cmdline = MapSL( params->cmdLine );
1287 return NE_CreateThread( pModule, cmdShow, cmdline );
1291 /**********************************************************************
1292 * NE_StartTask
1294 * Startup code for a new 16-bit task.
1296 DWORD NE_StartTask(void)
1298 TDB *pTask = TASK_GetCurrent();
1299 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1300 HINSTANCE16 hInstance, hPrevInstance;
1301 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1302 WORD sp;
1304 if ( pModule->count > 0 )
1306 /* Second instance of an already loaded NE module */
1307 /* Note that the refcount was already incremented by the parent */
1309 hPrevInstance = NE_GetInstance( pModule );
1311 if ( pModule->ne_autodata )
1312 if ( NE_CreateSegment( pModule, pModule->ne_autodata ) )
1313 NE_LoadSegment( pModule, pModule->ne_autodata );
1315 hInstance = NE_GetInstance( pModule );
1316 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->ne_autodata, hPrevInstance);
1319 else
1321 /* Load first instance of NE module */
1323 pModule->ne_flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1325 hInstance = NE_DoLoadModule( pModule );
1326 hPrevInstance = 0;
1329 if ( hInstance >= 32 )
1331 CONTEXT86 context;
1333 /* Enter instance handles into task struct */
1335 pTask->hInstance = hInstance;
1336 pTask->hPrevInstance = hPrevInstance;
1338 /* Use DGROUP for 16-bit stack */
1340 if (!(sp = OFFSETOF(pModule->ne_sssp)))
1341 sp = pSegTable[SELECTOROF(pModule->ne_sssp)-1].minsize + pModule->ne_stack;
1342 sp &= ~1;
1343 sp -= sizeof(STACK16FRAME);
1344 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1346 /* Registers at initialization must be:
1347 * ax zero
1348 * bx stack size in bytes
1349 * cx heap size in bytes
1350 * si previous app instance
1351 * di current app instance
1352 * bp zero
1353 * es selector to the PSP
1354 * ds dgroup of the application
1355 * ss stack selector
1356 * sp top of the stack
1358 memset( &context, 0, sizeof(context) );
1359 context.SegCs = GlobalHandleToSel16(pSegTable[SELECTOROF(pModule->ne_csip) - 1].hSeg);
1360 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1361 context.SegEs = pTask->hPDB;
1362 context.SegFs = wine_get_fs();
1363 context.SegGs = wine_get_gs();
1364 context.Eip = OFFSETOF(pModule->ne_csip);
1365 context.Ebx = pModule->ne_stack;
1366 context.Ecx = pModule->ne_heap;
1367 context.Edi = pTask->hInstance;
1368 context.Esi = pTask->hPrevInstance;
1370 /* Now call 16-bit entry point */
1372 TRACE("Starting main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
1373 context.SegCs, context.Eip, context.SegDs,
1374 SELECTOROF(NtCurrentTeb()->WOW32Reserved),
1375 OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
1377 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1378 ExitThread( LOWORD(context.Eax) );
1380 return hInstance; /* error code */
1383 /***********************************************************************
1384 * LoadLibrary (KERNEL.95)
1385 * LoadLibrary16 (KERNEL32.35)
1387 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1389 return LoadModule16(libname, (LPVOID)-1 );
1393 /**********************************************************************
1394 * MODULE_CallWEP
1396 * Call a DLL's WEP, allowing it to shut down.
1397 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1399 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1401 BOOL16 ret;
1402 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1403 if (!WEP) return FALSE;
1405 __TRY
1407 WORD args[1];
1408 DWORD dwRet;
1410 args[0] = WEP_FREE_DLL;
1411 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1412 ret = LOWORD(dwRet);
1414 __EXCEPT_PAGE_FAULT
1416 WARN("Page fault\n");
1417 ret = 0;
1419 __ENDTRY
1421 return ret;
1425 /**********************************************************************
1426 * NE_FreeModule
1428 * Implementation of FreeModule16().
1430 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1432 HMODULE16 *hPrevModule;
1433 NE_MODULE *pModule;
1434 HMODULE16 *pModRef;
1435 int i;
1437 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1438 hModule = pModule->self;
1440 TRACE("%04x count %d\n", hModule, pModule->count );
1442 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1443 else pModule->count = 0;
1445 if (call_wep && !(pModule->ne_flags & NE_FFLAGS_WIN32))
1447 /* Free the objects owned by the DLL module */
1448 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1450 if (pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1451 MODULE_CallWEP( hModule );
1452 else
1453 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1456 TRACE_(loaddll)("Unloaded module %s : %s\n", debugstr_a(NE_MODULE_NAME(pModule)),
1457 (pModule->ne_flags & NE_FFLAGS_BUILTIN) ? "builtin" : "native");
1459 /* Clear magic number just in case */
1461 pModule->ne_magic = pModule->self = 0;
1462 if (pModule->owner32) FreeLibrary( pModule->owner32 );
1463 else if (pModule->mapping) UnmapViewOfFile( pModule->mapping );
1465 /* Remove it from the linked list */
1467 hPrevModule = &hFirstModule;
1468 while (*hPrevModule && (*hPrevModule != hModule))
1470 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1472 if (*hPrevModule) *hPrevModule = pModule->next;
1474 /* Free the referenced modules */
1476 pModRef = (HMODULE16*)((char *)pModule + pModule->ne_modtab);
1477 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
1479 NE_FreeModule( *pModRef, call_wep );
1482 /* Free the module storage */
1484 GlobalFreeAll16( hModule );
1485 return TRUE;
1489 /**********************************************************************
1490 * FreeModule (KERNEL.46)
1492 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1494 return NE_FreeModule( hModule, TRUE );
1498 /***********************************************************************
1499 * FreeLibrary (KERNEL.96)
1500 * FreeLibrary16 (KERNEL32.36)
1502 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1504 TRACE("%04x\n", handle );
1505 FreeModule16( handle );
1509 /***********************************************************************
1510 * GetModuleHandle16 (KERNEL32.@)
1512 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1514 HMODULE16 hModule = hFirstModule;
1515 LPSTR s;
1516 BYTE len, *name_table;
1517 char tmpstr[MAX_PATH];
1518 NE_MODULE *pModule;
1520 TRACE("(%s)\n", name);
1522 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1524 len = strlen(name);
1525 if (!len) return 0;
1527 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1529 /* If 'name' matches exactly the module name of a module:
1530 * Return its handle.
1532 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1534 pModule = NE_GetPtr( hModule );
1535 if (!pModule) break;
1536 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1538 name_table = (BYTE *)pModule + pModule->ne_restab;
1539 if ((*name_table == len) && !strncmp(name, (char*) name_table+1, len))
1540 return hModule;
1543 /* If uppercased 'name' matches exactly the module name of a module:
1544 * Return its handle
1546 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1548 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1550 pModule = NE_GetPtr( hModule );
1551 if (!pModule) break;
1552 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1554 name_table = (BYTE *)pModule + pModule->ne_restab;
1555 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1556 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1557 * lowercased module names, but try to load uppercase DLLs, so this
1558 * 'i' compare is just a quickfix until the loader handles that
1559 * correctly. -MM 990705
1561 if ((*name_table == len) && !NE_strncasecmp(tmpstr, (const char*)name_table+1, len))
1562 return hModule;
1565 /* If the base filename of 'name' matches the base filename of the module
1566 * filename of some module (case-insensitive compare):
1567 * Return its handle.
1570 /* basename: search backwards in passed name to \ / or : */
1571 s = tmpstr + strlen(tmpstr);
1572 while (s > tmpstr)
1574 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1575 break;
1576 s--;
1579 /* search this in loaded filename list */
1580 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1582 char *loadedfn;
1583 OFSTRUCT *ofs;
1585 pModule = NE_GetPtr( hModule );
1586 if (!pModule) break;
1587 if (!pModule->fileinfo) continue;
1588 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1590 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1591 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1592 /* basename: search backwards in pathname to \ / or : */
1593 while (loadedfn > (char*)ofs->szPathName)
1595 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1596 break;
1597 loadedfn--;
1599 /* case insensitive compare ... */
1600 if (!NE_strcasecmp(loadedfn, s))
1601 return hModule;
1603 return 0;
1607 /**********************************************************************
1608 * GetModuleName (KERNEL.27)
1610 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1612 NE_MODULE *pModule;
1613 BYTE *p;
1615 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1616 p = (BYTE *)pModule + pModule->ne_restab;
1617 if (count > *p) count = *p + 1;
1618 if (count > 0)
1620 memcpy( buf, p + 1, count - 1 );
1621 buf[count-1] = '\0';
1623 return TRUE;
1627 /**********************************************************************
1628 * GetModuleFileName (KERNEL.49)
1630 * Comment: see GetModuleFileNameA
1632 * Even if invoked by second instance of a program,
1633 * it still returns path of first one.
1635 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1636 INT16 nSize )
1638 NE_MODULE *pModule;
1640 /* Win95 does not query hModule if set to 0 !
1641 * Is this wrong or maybe Win3.1 only ? */
1642 if (!hModule) hModule = GetCurrentTask();
1644 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1645 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1646 if (pModule->ne_expver < 0x400)
1647 GetShortPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1648 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1649 return strlen(lpFileName);
1653 /**********************************************************************
1654 * GetModuleUsage (KERNEL.48)
1656 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1658 NE_MODULE *pModule = NE_GetPtr( hModule );
1659 return pModule ? pModule->count : 0;
1663 /**********************************************************************
1664 * GetExpWinVer (KERNEL.167)
1666 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1668 NE_MODULE *pModule = NE_GetPtr( hModule );
1669 if ( !pModule ) return 0;
1670 return pModule->ne_expver;
1674 /***********************************************************************
1675 * WinExec (KERNEL.166)
1677 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1679 LPCSTR p, args = NULL;
1680 LPCSTR name_beg, name_end;
1681 LPSTR name, cmdline;
1682 int arglen;
1683 HINSTANCE16 ret;
1684 char buffer[MAX_PATH];
1685 LOADPARAMS16 params;
1686 WORD showCmd[2];
1688 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1690 name_beg = lpCmdLine+1;
1691 p = strchr ( lpCmdLine+1, '"' );
1692 if (p)
1694 name_end = p;
1695 args = strchr ( p, ' ' );
1697 else /* yes, even valid with trailing '"' missing */
1698 name_end = lpCmdLine+strlen(lpCmdLine);
1700 else
1702 name_beg = lpCmdLine;
1703 args = strchr( lpCmdLine, ' ' );
1704 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1707 if ((name_beg == lpCmdLine) && (!args))
1708 { /* just use the original cmdline string as file name */
1709 name = (LPSTR)lpCmdLine;
1711 else
1713 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1714 return ERROR_NOT_ENOUGH_MEMORY;
1715 memcpy( name, name_beg, name_end - name_beg );
1716 name[name_end - name_beg] = '\0';
1719 if (args)
1721 args++;
1722 arglen = strlen(args);
1723 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1724 cmdline[0] = (BYTE)arglen;
1725 strcpy( cmdline + 1, args );
1727 else
1729 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1730 cmdline[0] = cmdline[1] = 0;
1733 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1735 showCmd[0] = 2;
1736 showCmd[1] = nCmdShow;
1738 params.hEnvironment = 0;
1739 params.cmdLine = MapLS( cmdline );
1740 params.showCmd = MapLS( showCmd );
1741 params.reserved = 0;
1743 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1745 ret = LoadModule16( buffer, &params );
1747 else if (!contains_path( name )) /* try 16-bit builtin */
1749 lstrcpynA( buffer, name, sizeof(buffer) );
1750 if (strlen( buffer ) < sizeof(buffer) - 4 && !strchr( buffer, '.' )) strcat( buffer, ".exe" );
1751 ret = LoadModule16( buffer, &params );
1752 if (ret == ERROR_FILE_NOT_FOUND) ret = 21; /* it might be a 32-bit builtin too */
1754 else ret = ERROR_FILE_NOT_FOUND;
1756 UnMapLS( params.cmdLine );
1757 UnMapLS( params.showCmd );
1759 HeapFree( GetProcessHeap(), 0, cmdline );
1760 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1762 if (ret == 21 || ret == ERROR_BAD_FORMAT) /* 32-bit module or unknown executable*/
1764 LOADPARAMS16 params;
1765 WORD showCmd[2];
1766 showCmd[0] = 2;
1767 showCmd[1] = nCmdShow;
1769 arglen = strlen( lpCmdLine );
1770 cmdline = HeapAlloc( GetProcessHeap(), 0, arglen + 1 );
1771 cmdline[0] = (BYTE)arglen;
1772 memcpy( cmdline + 1, lpCmdLine, arglen );
1774 params.hEnvironment = 0;
1775 params.cmdLine = MapLS( cmdline );
1776 params.showCmd = MapLS( showCmd );
1777 params.reserved = 0;
1779 ret = LoadModule16( "winoldap.mod", &params );
1780 UnMapLS( params.cmdLine );
1781 UnMapLS( params.showCmd );
1783 return ret;
1786 /***********************************************************************
1787 * GetProcAddress (KERNEL.50)
1789 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1791 WORD ordinal;
1792 FARPROC16 ret;
1794 if (!hModule) hModule = GetCurrentTask();
1795 hModule = GetExePtr( hModule );
1797 if (HIWORD(name) != 0)
1799 ordinal = NE_GetOrdinal( hModule, name );
1800 TRACE("%04x '%s'\n", hModule, name );
1802 else
1804 ordinal = LOWORD(name);
1805 TRACE("%04x %04x\n", hModule, ordinal );
1807 if (!ordinal) return NULL;
1809 ret = NE_GetEntryPoint( hModule, ordinal );
1811 TRACE("returning %p\n", ret );
1812 return ret;
1816 /***************************************************************************
1817 * HasGPHandler (KERNEL.338)
1819 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1821 HMODULE16 hModule;
1822 int gpOrdinal;
1823 SEGPTR gpPtr;
1824 GPHANDLERDEF *gpHandler;
1826 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1827 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1828 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1829 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1830 && (gpHandler = MapSL( gpPtr )) != NULL )
1832 while (gpHandler->selector)
1834 if ( SELECTOROF(address) == gpHandler->selector
1835 && OFFSETOF(address) >= gpHandler->rangeStart
1836 && OFFSETOF(address) < gpHandler->rangeEnd )
1837 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1838 gpHandler++;
1842 return 0;
1846 /**********************************************************************
1847 * GetModuleHandle (KERNEL.47)
1849 * Find a module from a module name.
1851 * NOTE: The current implementation works the same way the Windows 95 one
1852 * does. Do not try to 'fix' it, fix the callers.
1853 * + It does not do ANY extension handling (except that strange .EXE bit)!
1854 * + It does not care about paths, just about basenames. (same as Windows)
1856 * RETURNS
1857 * LOWORD:
1858 * the win16 module handle if found
1859 * 0 if not
1860 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1861 * Always hFirstModule
1863 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1865 if (HIWORD(name) == 0)
1866 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1867 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1870 /**********************************************************************
1871 * NE_GetModuleByFilename
1873 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1875 HMODULE16 hModule;
1876 LPSTR s, p;
1877 BYTE len, *name_table;
1878 char tmpstr[MAX_PATH];
1879 NE_MODULE *pModule;
1881 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1883 /* If the base filename of 'name' matches the base filename of the module
1884 * filename of some module (case-insensitive compare):
1885 * Return its handle.
1888 /* basename: search backwards in passed name to \ / or : */
1889 s = tmpstr + strlen(tmpstr);
1890 while (s > tmpstr)
1892 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1893 break;
1894 s--;
1897 /* search this in loaded filename list */
1898 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1900 char *loadedfn;
1901 OFSTRUCT *ofs;
1903 pModule = NE_GetPtr( hModule );
1904 if (!pModule) break;
1905 if (!pModule->fileinfo) continue;
1906 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1908 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1909 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1910 /* basename: search backwards in pathname to \ / or : */
1911 while (loadedfn > (char*)ofs->szPathName)
1913 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1914 break;
1915 loadedfn--;
1917 /* case insensitive compare ... */
1918 if (!NE_strcasecmp(loadedfn, s))
1919 return hModule;
1921 /* If basename (without ext) matches the module name of a module:
1922 * Return its handle.
1925 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1926 len = strlen(s);
1928 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1930 pModule = NE_GetPtr( hModule );
1931 if (!pModule) break;
1932 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1934 name_table = (BYTE *)pModule + pModule->ne_restab;
1935 if ((*name_table == len) && !NE_strncasecmp(s, (const char*)name_table+1, len))
1936 return hModule;
1939 return 0;
1942 /***********************************************************************
1943 * GetProcAddress16 (KERNEL32.37)
1944 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1946 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1948 if (!hModule) return 0;
1949 if (HIWORD(hModule))
1951 WARN("hModule is Win32 handle (%p)\n", hModule );
1952 return 0;
1954 return GetProcAddress16( LOWORD(hModule), name );
1957 /***************************************************************************
1958 * IsRomModule (KERNEL.323)
1960 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1962 return FALSE;
1965 /***************************************************************************
1966 * IsRomFile (KERNEL.326)
1968 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1970 return FALSE;
1973 /***********************************************************************
1974 * create_dummy_module
1976 * Create a dummy NE module for Win32 or Winelib.
1978 static HMODULE16 create_dummy_module( HMODULE module32 )
1980 HMODULE16 hModule;
1981 NE_MODULE *pModule;
1982 SEGTABLEENTRY *pSegment;
1983 char *pStr,*s;
1984 unsigned int len;
1985 const char* basename;
1986 OFSTRUCT *ofs;
1987 int of_size, size;
1988 char filename[MAX_PATH];
1989 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
1991 if (!nt) return ERROR_BAD_FORMAT;
1993 /* Extract base filename */
1994 len = GetModuleFileNameA( module32, filename, sizeof(filename) );
1995 if (!len || len >= sizeof(filename)) return ERROR_BAD_FORMAT;
1996 basename = strrchr(filename, '\\');
1997 if (!basename) basename = filename;
1998 else basename++;
1999 len = strlen(basename);
2000 if ((s = strchr(basename, '.'))) len = s - basename;
2002 /* Allocate module */
2003 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
2004 + strlen(filename) + 1;
2005 size = sizeof(NE_MODULE) +
2006 /* loaded file info */
2007 ((of_size + 3) & ~3) +
2008 /* segment table: DS,CS */
2009 2 * sizeof(SEGTABLEENTRY) +
2010 /* name table */
2011 len + 2 +
2012 /* several empty tables */
2015 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2016 if (!hModule) return ERROR_BAD_FORMAT;
2018 FarSetOwner16( hModule, hModule );
2019 pModule = GlobalLock16( hModule );
2021 /* Set all used entries */
2022 pModule->ne_magic = IMAGE_OS2_SIGNATURE;
2023 pModule->count = 1;
2024 pModule->next = 0;
2025 pModule->ne_flags = NE_FFLAGS_WIN32;
2026 pModule->ne_autodata = 0;
2027 pModule->ne_sssp = MAKESEGPTR( 0, 1 );
2028 pModule->ne_csip = MAKESEGPTR( 0, 2 );
2029 pModule->ne_heap = 0;
2030 pModule->ne_stack = 0;
2031 pModule->ne_cseg = 2;
2032 pModule->ne_cmod = 0;
2033 pModule->ne_cbnrestab = 0;
2034 pModule->fileinfo = sizeof(NE_MODULE);
2035 pModule->ne_exetyp = NE_OSFLAGS_WINDOWS;
2036 pModule->self = hModule;
2037 pModule->module32 = module32;
2039 /* Set version and flags */
2040 pModule->ne_expver = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2041 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2042 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2043 pModule->ne_flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2045 /* Set loaded file information */
2046 ofs = (OFSTRUCT *)(pModule + 1);
2047 memset( ofs, 0, of_size );
2048 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2049 strcpy( ofs->szPathName, filename );
2051 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2052 pModule->ne_segtab = (char *)pSegment - (char *)pModule;
2053 /* Data segment */
2054 pSegment->size = 0;
2055 pSegment->flags = NE_SEGFLAGS_DATA;
2056 pSegment->minsize = 0x1000;
2057 pSegment++;
2058 /* Code segment */
2059 pSegment->flags = 0;
2060 pSegment++;
2062 /* Module name */
2063 pStr = (char *)pSegment;
2064 pModule->ne_restab = pStr - (char *)pModule;
2065 assert(len<256);
2066 *pStr = len;
2067 lstrcpynA( pStr+1, basename, len+1 );
2068 pStr += len+2;
2070 /* All tables zero terminated */
2071 pModule->ne_rsrctab = pModule->ne_imptab = pModule->ne_enttab = pStr - (char *)pModule;
2073 NE_RegisterModule( pModule );
2074 pModule->owner32 = LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2075 return hModule;
2078 /***********************************************************************
2079 * PrivateLoadLibrary (KERNEL32.@)
2081 * FIXME: rough guesswork, don't know what "Private" means
2083 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2085 return LoadLibrary16(libname);
2088 /***********************************************************************
2089 * PrivateFreeLibrary (KERNEL32.@)
2091 * FIXME: rough guesswork, don't know what "Private" means
2093 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2095 FreeLibrary16(handle);
2098 /***********************************************************************
2099 * LoadLibrary32 (KERNEL.452)
2100 * LoadSystemLibrary32 (KERNEL.482)
2102 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2104 HMODULE hModule;
2105 DWORD count;
2107 ReleaseThunkLock( &count );
2108 hModule = LoadLibraryA( libname );
2109 RestoreThunkLock( count );
2110 return hModule;
2113 /***************************************************************************
2114 * MapHModuleLS (KERNEL32.@)
2116 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2118 HMODULE16 ret;
2119 NE_MODULE *pModule;
2121 if (!hmod)
2122 return TASK_GetCurrent()->hInstance;
2123 if (!HIWORD(hmod))
2124 return LOWORD(hmod); /* we already have a 16 bit module handle */
2125 pModule = GlobalLock16(hFirstModule);
2126 while (pModule) {
2127 if (pModule->module32 == hmod)
2128 return pModule->self;
2129 pModule = GlobalLock16(pModule->next);
2131 if ((ret = create_dummy_module( hmod )) < 32)
2133 SetLastError(ret);
2134 ret = 0;
2136 return ret;
2139 /***************************************************************************
2140 * MapHModuleSL (KERNEL32.@)
2142 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2144 NE_MODULE *pModule;
2146 if (!hmod) {
2147 TDB *pTask = TASK_GetCurrent();
2148 hmod = pTask->hModule;
2150 pModule = GlobalLock16(hmod);
2151 if ((pModule->ne_magic != IMAGE_OS2_SIGNATURE) || !(pModule->ne_flags & NE_FFLAGS_WIN32))
2152 return 0;
2153 return pModule->module32;
2156 /***************************************************************************
2157 * MapHInstLS (KERNEL.472)
2159 void WINAPI MapHInstLS16( CONTEXT86 *context )
2161 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2164 /***************************************************************************
2165 * MapHInstSL (KERNEL.473)
2167 void WINAPI MapHInstSL16( CONTEXT86 *context )
2169 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2172 #ifdef __i386__
2174 /***************************************************************************
2175 * MapHInstLS (KERNEL32.@)
2177 __ASM_STDCALL_FUNC( MapHInstLS, 0,
2178 "pushl %eax\n\t"
2179 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n\t"
2180 "ret" )
2182 /***************************************************************************
2183 * MapHInstSL (KERNEL32.@)
2185 __ASM_STDCALL_FUNC( MapHInstSL, 0,
2186 "pushl %eax\n\t"
2187 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n\t"
2188 "ret" )
2190 /***************************************************************************
2191 * MapHInstLS_PN (KERNEL32.@)
2193 __ASM_STDCALL_FUNC( MapHInstLS_PN, 0,
2194 "testl %eax,%eax\n\t"
2195 "jz 1f\n\t"
2196 "pushl %eax\n\t"
2197 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n"
2198 "1:\tret" )
2200 /***************************************************************************
2201 * MapHInstSL_PN (KERNEL32.@)
2203 __ASM_STDCALL_FUNC( MapHInstSL_PN, 0,
2204 "andl $0xffff,%eax\n\t"
2205 "jz 1f\n\t"
2206 "pushl %eax\n\t"
2207 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n"
2208 "1:\tret" )
2210 #endif /* __i386__ */