Generate 16-bit resources in the proper format inside the module data,
[wine/dcerpc.git] / dlls / kernel / ne_module.c
blob4dc2a652c6fbc386436c12b27b5712edff618d92
1 /*
2 * NE modules
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <ctype.h>
35 #include "windef.h"
36 #include "wine/winbase16.h"
37 #include "wownt32.h"
38 #include "toolhelp.h"
39 #include "excpt.h"
40 #include "kernel_private.h"
41 #include "kernel16_private.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(module);
46 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
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 typedef struct
73 const void *module; /* module header */
74 void *code_start; /* 32-bit address of DLL code */
75 } BUILTIN16_DESCRIPTOR;
77 struct builtin_dll
79 const BUILTIN16_DESCRIPTOR *descr; /* module descriptor */
80 const char *file_name; /* module file name */
83 /* Table of all built-in DLLs */
85 #define MAX_DLLS 50
87 static struct builtin_dll builtin_dlls[MAX_DLLS];
89 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
90 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
92 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
94 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
97 static WINE_EXCEPTION_FILTER(page_fault)
99 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
100 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
101 return EXCEPTION_EXECUTE_HANDLER;
102 return EXCEPTION_CONTINUE_SEARCH;
106 /* patch all the flat cs references of the code segment if necessary */
107 inline static void patch_code_segment( void *code_segment )
109 #ifdef __i386__
110 CALLFROM16 *call = code_segment;
111 if (call->flatcs == wine_get_cs()) return; /* nothing to patch */
112 while (call->pushl == 0x68)
114 call->flatcs = wine_get_cs();
115 call++;
117 #endif
121 /***********************************************************************
122 * NE_strcasecmp
124 * locale-independent case conversion for module lookups
126 static int NE_strcasecmp( const char *str1, const char *str2 )
128 int ret = 0;
129 for ( ; ; str1++, str2++)
130 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
131 return ret;
135 /***********************************************************************
136 * NE_strncasecmp
138 * locale-independent case conversion for module lookups
140 static int NE_strncasecmp( const char *str1, const char *str2, int len )
142 int ret = 0;
143 for ( ; len > 0; len--, str1++, str2++)
144 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
145 return ret;
149 /***********************************************************************
150 * find_dll_descr
152 * Find a descriptor in the list
154 static const BUILTIN16_DESCRIPTOR *find_dll_descr( const char *dllname, const char **file_name )
156 int i;
157 const IMAGE_DOS_HEADER *mz_header;
158 const IMAGE_OS2_HEADER *ne_header;
159 BYTE *name_table;
161 for (i = 0; i < MAX_DLLS; i++)
163 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i].descr;
164 if (descr)
166 mz_header = descr->module;
167 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mz_header + mz_header->e_lfanew);
168 name_table = (BYTE *)ne_header + ne_header->ne_restab;
170 /* check the dll file name */
171 if (!NE_strcasecmp( builtin_dlls[i].file_name, dllname ) ||
172 /* check the dll module name (without extension) */
173 (!NE_strncasecmp( dllname, name_table+1, *name_table ) &&
174 !strcmp( dllname + *name_table, ".dll" )))
176 *file_name = builtin_dlls[i].file_name;
177 return builtin_dlls[i].descr;
181 return NULL;
185 /***********************************************************************
186 * __wine_dll_register_16 (KERNEL32.@)
188 * Register a built-in DLL descriptor.
190 void __wine_dll_register_16( const BUILTIN16_DESCRIPTOR *descr, const char *file_name )
192 int i;
194 for (i = 0; i < MAX_DLLS; i++)
196 if (builtin_dlls[i].descr) continue;
197 builtin_dlls[i].descr = descr;
198 builtin_dlls[i].file_name = file_name;
199 break;
201 assert( i < MAX_DLLS );
205 /***********************************************************************
206 * __wine_dll_unregister_16 (KERNEL32.@)
208 * Unregister a built-in DLL descriptor.
210 void __wine_dll_unregister_16( const BUILTIN16_DESCRIPTOR *descr )
212 int i;
214 for (i = 0; i < MAX_DLLS; i++)
216 if (builtin_dlls[i].descr != descr) continue;
217 builtin_dlls[i].descr = NULL;
218 break;
223 /***********************************************************************
224 * NE_GetPtr
226 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
228 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
232 /**********************************************************************
233 * NE_RegisterModule
235 static void NE_RegisterModule( NE_MODULE *pModule )
237 pModule->next = hFirstModule;
238 hFirstModule = pModule->self;
242 /***********************************************************************
243 * NE_DumpModule
245 void NE_DumpModule( HMODULE16 hModule )
247 int i, ordinal;
248 SEGTABLEENTRY *pSeg;
249 BYTE *pstr;
250 WORD *pword;
251 NE_MODULE *pModule;
252 ET_BUNDLE *bundle;
253 ET_ENTRY *entry;
255 if (!(pModule = NE_GetPtr( hModule )))
257 MESSAGE( "**** %04x is not a module handle\n", hModule );
258 return;
261 /* Dump the module info */
262 DPRINTF( "---\n" );
263 DPRINTF( "Module %04x:\n", hModule );
264 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
265 pModule->count, pModule->ne_flags,
266 pModule->ne_heap, pModule->ne_stack );
267 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
268 SELECTOROF(pModule->ne_csip), OFFSETOF(pModule->ne_csip),
269 SELECTOROF(pModule->ne_sssp), OFFSETOF(pModule->ne_sssp),
270 pModule->ne_autodata, pModule->ne_cseg, pModule->ne_cmod );
271 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
272 pModule->ne_exetyp, pModule->ne_swaparea, pModule->ne_expver );
273 if (pModule->ne_flags & NE_FFLAGS_WIN32)
274 DPRINTF( "PE module=%p\n", pModule->module32 );
276 /* Dump the file info */
277 DPRINTF( "---\n" );
278 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
280 /* Dump the segment table */
281 DPRINTF( "---\n" );
282 DPRINTF( "Segment table:\n" );
283 pSeg = NE_SEG_TABLE( pModule );
284 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
285 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
286 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
287 pSeg->minsize, pSeg->hSeg );
289 /* Dump the resource table */
290 DPRINTF( "---\n" );
291 DPRINTF( "Resource table:\n" );
292 if (pModule->ne_rsrctab)
294 pword = (WORD *)((BYTE *)pModule + pModule->ne_rsrctab);
295 DPRINTF( "Alignment: %d\n", *pword++ );
296 while (*pword)
298 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
299 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
300 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
301 for (i = 0; i < ptr->count; i++, pname++)
302 DPRINTF( "offset=%d len=%d id=%04x\n",
303 pname->offset, pname->length, pname->id );
304 pword = (WORD *)pname;
307 else DPRINTF( "None\n" );
309 /* Dump the resident name table */
310 DPRINTF( "---\n" );
311 DPRINTF( "Resident-name table:\n" );
312 pstr = (char *)pModule + pModule->ne_restab;
313 while (*pstr)
315 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
316 *(WORD *)(pstr + *pstr + 1) );
317 pstr += *pstr + 1 + sizeof(WORD);
320 /* Dump the module reference table */
321 DPRINTF( "---\n" );
322 DPRINTF( "Module ref table:\n" );
323 if (pModule->ne_modtab)
325 pword = (WORD *)((BYTE *)pModule + pModule->ne_modtab);
326 for (i = 0; i < pModule->ne_cmod; i++, pword++)
328 char name[10];
329 GetModuleName16( *pword, name, sizeof(name) );
330 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
333 else DPRINTF( "None\n" );
335 /* Dump the entry table */
336 DPRINTF( "---\n" );
337 DPRINTF( "Entry table:\n" );
338 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->ne_enttab);
339 do {
340 entry = (ET_ENTRY *)((BYTE *)bundle+6);
341 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
342 ordinal = bundle->first;
343 while (ordinal < bundle->last)
345 if (entry->type == 0xff)
346 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
347 else
348 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
349 entry++;
351 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
353 /* Dump the non-resident names table */
354 DPRINTF( "---\n" );
355 DPRINTF( "Non-resident names table:\n" );
356 if (pModule->nrname_handle)
358 pstr = (char *)GlobalLock16( pModule->nrname_handle );
359 while (*pstr)
361 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
362 *(WORD *)(pstr + *pstr + 1) );
363 pstr += *pstr + 1 + sizeof(WORD);
366 DPRINTF( "\n" );
370 /***********************************************************************
371 * NE_WalkModules
373 * Walk the module list and print the modules.
375 void NE_WalkModules(void)
377 HMODULE16 hModule = hFirstModule;
378 MESSAGE( "Module Flags Name\n" );
379 while (hModule)
381 NE_MODULE *pModule = NE_GetPtr( hModule );
382 if (!pModule)
384 MESSAGE( "Bad module %04x in list\n", hModule );
385 return;
387 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->ne_flags,
388 *((char *)pModule + pModule->ne_restab),
389 (char *)pModule + pModule->ne_restab + 1 );
390 hModule = pModule->next;
395 /***********************************************************************
396 * NE_InitResourceHandler
398 * Fill in 'resloader' fields in the resource table.
400 static void NE_InitResourceHandler( HMODULE16 hModule )
402 static FARPROC16 proc;
404 NE_TYPEINFO *pTypeInfo;
405 NE_MODULE *pModule;
407 if (!(pModule = NE_GetPtr( hModule )) || !pModule->ne_rsrctab) return;
409 TRACE("InitResourceHandler[%04x]\n", hModule );
411 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
413 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
414 while(pTypeInfo->type_id)
416 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
417 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
422 /***********************************************************************
423 * NE_GetOrdinal
425 * Lookup the ordinal for a given name.
427 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
429 unsigned char buffer[256], *cpnt;
430 BYTE len;
431 NE_MODULE *pModule;
433 if (!(pModule = NE_GetPtr( hModule ))) return 0;
434 if (pModule->ne_flags & NE_FFLAGS_WIN32) return 0;
436 TRACE("(%04x,'%s')\n", hModule, name );
438 /* First handle names of the form '#xxxx' */
440 if (name[0] == '#') return atoi( name + 1 );
442 /* Now copy and uppercase the string */
444 strcpy( buffer, name );
445 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = RtlUpperChar(*cpnt);
446 len = cpnt - buffer;
448 /* First search the resident names */
450 cpnt = (char *)pModule + pModule->ne_restab;
452 /* Skip the first entry (module name) */
453 cpnt += *cpnt + 1 + sizeof(WORD);
454 while (*cpnt)
456 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
458 WORD ordinal;
459 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
460 TRACE(" Found: ordinal=%d\n", ordinal );
461 return ordinal;
463 cpnt += *cpnt + 1 + sizeof(WORD);
466 /* Now search the non-resident names table */
468 if (!pModule->nrname_handle) return 0; /* No non-resident table */
469 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
471 /* Skip the first entry (module description string) */
472 cpnt += *cpnt + 1 + sizeof(WORD);
473 while (*cpnt)
475 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
477 WORD ordinal;
478 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
479 TRACE(" Found: ordinal=%d\n", ordinal );
480 return ordinal;
482 cpnt += *cpnt + 1 + sizeof(WORD);
484 return 0;
488 /***********************************************************************
489 * NE_GetEntryPoint
491 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
493 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
496 /***********************************************************************
497 * NE_GetEntryPointEx
499 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
501 NE_MODULE *pModule;
502 WORD sel, offset, i;
504 ET_ENTRY *entry;
505 ET_BUNDLE *bundle;
507 if (!(pModule = NE_GetPtr( hModule ))) return 0;
508 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
510 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
511 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
513 if (!(bundle->next))
514 return 0;
515 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
518 entry = (ET_ENTRY *)((BYTE *)bundle+6);
519 for (i=0; i < (ordinal - bundle->first - 1); i++)
520 entry++;
522 sel = entry->segnum;
523 memcpy( &offset, &entry->offs, sizeof(WORD) );
525 if (sel == 0xfe) sel = 0xffff; /* constant entry */
526 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
527 if (sel==0xffff)
528 return (FARPROC16)MAKESEGPTR( sel, offset );
529 if (!snoop)
530 return (FARPROC16)MAKESEGPTR( sel, offset );
531 else
532 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
536 /***********************************************************************
537 * EntryAddrProc (KERNEL.667) Wine-specific export
539 * Return the entry point for a given ordinal.
541 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
543 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
544 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
545 return ret;
548 /***********************************************************************
549 * NE_SetEntryPoint
551 * Change the value of an entry point. Use with caution!
552 * It can only change the offset value, not the selector.
554 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
556 NE_MODULE *pModule;
557 ET_ENTRY *entry;
558 ET_BUNDLE *bundle;
559 int i;
561 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
562 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
564 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
565 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
567 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
568 if (!(bundle->next)) return 0;
571 entry = (ET_ENTRY *)((BYTE *)bundle+6);
572 for (i=0; i < (ordinal - bundle->first - 1); i++)
573 entry++;
575 memcpy( &entry->offs, &offset, sizeof(WORD) );
576 return TRUE;
580 /***********************************************************************
581 * build_bundle_data
583 * Build the entry table bundle data from the on-disk format. Helper for build_module.
585 static void *build_bundle_data( NE_MODULE *pModule, void *dest, const BYTE *table )
587 ET_BUNDLE *oldbundle, *bundle = dest;
588 ET_ENTRY *entry;
589 BYTE nr_entries, type;
591 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
592 entry = (ET_ENTRY *)((BYTE *)bundle+6);
594 while ((nr_entries = *table++))
596 if ((type = *table++))
598 bundle->last += nr_entries;
599 if (type == 0xff)
601 while (nr_entries--)
603 entry->type = type;
604 entry->flags = *table++;
605 table += sizeof(WORD);
606 entry->segnum = *table++;
607 entry->offs = *(WORD *)table;
608 table += sizeof(WORD);
609 entry++;
612 else
614 while (nr_entries--)
616 entry->type = type;
617 entry->flags = *table++;
618 entry->segnum = type;
619 entry->offs = *(WORD *)table;
620 table += sizeof(WORD);
621 entry++;
625 else
627 if (bundle->first == bundle->last)
629 bundle->first += nr_entries;
630 bundle->last += nr_entries;
632 else
634 oldbundle = bundle;
635 oldbundle->next = (char *)entry - (char *)pModule;
636 bundle = (ET_BUNDLE *)entry;
637 bundle->first = bundle->last = oldbundle->last + nr_entries;
638 bundle->next = 0;
639 entry = (ET_ENTRY*)(((BYTE*)entry)+sizeof(ET_BUNDLE));
643 return entry;
647 /***********************************************************************
648 * build_module
650 * Build the in-memory module from the on-disk data.
652 static HMODULE16 build_module( const void *mapping, SIZE_T mapping_size, LPCSTR path )
654 const IMAGE_DOS_HEADER *mz_header = mapping;
655 const IMAGE_OS2_HEADER *ne_header;
656 const struct ne_segment_table_entry_s *pSeg;
657 const void *ptr;
658 int i;
659 size_t size;
660 HMODULE16 hModule;
661 NE_MODULE *pModule;
662 BYTE *buffer, *pData, *end;
663 OFSTRUCT *ofs;
665 if (mapping_size < sizeof(*mz_header)) return ERROR_BAD_FORMAT;
666 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE) return ERROR_BAD_FORMAT;
667 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mapping + mz_header->e_lfanew);
668 if (mz_header->e_lfanew + sizeof(*ne_header) > mapping_size) return ERROR_BAD_FORMAT;
669 if (ne_header->ne_magic == IMAGE_NT_SIGNATURE) return 21; /* win32 exe */
670 if (ne_header->ne_magic == IMAGE_OS2_SIGNATURE_LX)
672 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path);
673 return 12;
675 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE) return ERROR_BAD_FORMAT;
677 /* We now have a valid NE header */
679 /* check to be able to fall back to loading OS/2 programs as DOS
680 * FIXME: should this check be reversed in order to be less strict?
681 * (only fail for OS/2 ne_exetyp 0x01 here?) */
682 if ((ne_header->ne_exetyp != 0x02 /* Windows */)
683 && (ne_header->ne_exetyp != 0x04) /* Windows 386 */)
684 return ERROR_BAD_FORMAT;
686 size = sizeof(NE_MODULE) +
687 /* segment table */
688 ne_header->ne_cseg * sizeof(SEGTABLEENTRY) +
689 /* resource table */
690 ne_header->ne_restab - ne_header->ne_rsrctab +
691 /* resident names table */
692 ne_header->ne_modtab - ne_header->ne_restab +
693 /* module ref table */
694 ne_header->ne_cmod * sizeof(WORD) +
695 /* imported names table */
696 ne_header->ne_enttab - ne_header->ne_imptab +
697 /* entry table length */
698 ne_header->ne_cbenttab +
699 /* entry table extra conversion space */
700 sizeof(ET_BUNDLE) +
701 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6) +
702 /* loaded file info */
703 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
705 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
706 if (!hModule) return ERROR_BAD_FORMAT;
708 FarSetOwner16( hModule, hModule );
709 pModule = (NE_MODULE *)GlobalLock16( hModule );
710 memcpy( pModule, ne_header, sizeof(*ne_header) );
711 pModule->count = 0;
712 /* check programs for default minimal stack size */
713 if (!(pModule->ne_flags & NE_FFLAGS_LIBMODULE) && (pModule->ne_stack < 0x1400))
714 pModule->ne_stack = 0x1400;
716 pModule->self = hModule;
717 pModule->mapping = mapping;
718 pModule->mapping_size = mapping_size;
720 pData = (BYTE *)(pModule + 1);
722 /* Clear internal Wine flags in case they are set in the EXE file */
724 pModule->ne_flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
726 /* Get the segment table */
728 pModule->ne_segtab = pData - (BYTE *)pModule;
729 if (!(pSeg = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_segtab,
730 ne_header->ne_cseg * sizeof(struct ne_segment_table_entry_s) )))
731 goto failed;
732 for (i = ne_header->ne_cseg; i > 0; i--, pSeg++)
734 memcpy( pData, pSeg, sizeof(*pSeg) );
735 pData += sizeof(SEGTABLEENTRY);
738 /* Get the resource table */
740 if (ne_header->ne_rsrctab < ne_header->ne_restab)
742 pModule->ne_rsrctab = pData - (BYTE *)pModule;
743 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_rsrctab,
744 ne_header->ne_restab - ne_header->ne_rsrctab )) goto failed;
745 pData += ne_header->ne_restab - ne_header->ne_rsrctab;
747 else pModule->ne_rsrctab = 0; /* No resource table */
749 /* Get the resident names table */
751 pModule->ne_restab = pData - (BYTE *)pModule;
752 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_restab,
753 ne_header->ne_modtab - ne_header->ne_restab )) goto failed;
754 pData += ne_header->ne_modtab - ne_header->ne_restab;
756 /* Get the module references table */
758 if (ne_header->ne_cmod > 0)
760 pModule->ne_modtab = pData - (BYTE *)pModule;
761 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_modtab,
762 ne_header->ne_cmod * sizeof(WORD) )) goto failed;
763 pData += ne_header->ne_cmod * sizeof(WORD);
765 else pModule->ne_modtab = 0; /* No module references */
767 /* Get the imported names table */
769 pModule->ne_imptab = pData - (BYTE *)pModule;
770 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_imptab,
771 ne_header->ne_enttab - ne_header->ne_imptab )) goto failed;
772 pData += ne_header->ne_enttab - ne_header->ne_imptab;
774 /* Load entry table, convert it to the optimized version used by Windows */
776 pModule->ne_enttab = pData - (BYTE *)pModule;
777 if (!(ptr = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_enttab,
778 ne_header->ne_cbenttab ))) goto failed;
779 end = build_bundle_data( pModule, pData, ptr );
781 pData += ne_header->ne_cbenttab + sizeof(ET_BUNDLE) +
782 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6);
784 if (end > pData)
786 FIXME( "not enough space for entry table for %s\n", debugstr_a(path) );
787 goto failed;
790 /* Store the filename information */
792 pModule->fileinfo = pData - (BYTE *)pModule;
793 ofs = (OFSTRUCT *)pData;
794 ofs->cBytes = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path);
795 ofs->fFixedDisk = 1;
796 strcpy( ofs->szPathName, path );
797 pData += ofs->cBytes + 1;
798 assert( (BYTE *)pModule + size <= pData );
800 /* Get the non-resident names table */
802 if (ne_header->ne_cbnrestab)
804 pModule->nrname_handle = GlobalAlloc16( 0, ne_header->ne_cbnrestab );
805 if (!pModule->nrname_handle) goto failed;
806 FarSetOwner16( pModule->nrname_handle, hModule );
807 buffer = GlobalLock16( pModule->nrname_handle );
808 if (!NE_READ_DATA( pModule, buffer, ne_header->ne_nrestab, ne_header->ne_cbnrestab ))
810 GlobalFree16( pModule->nrname_handle );
811 goto failed;
814 else pModule->nrname_handle = 0;
816 /* Allocate a segment for the implicitly-loaded DLLs */
818 if (pModule->ne_cmod)
820 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
821 (pModule->ne_cmod+1)*sizeof(HMODULE16) );
822 if (!pModule->dlls_to_init)
824 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
825 goto failed;
827 FarSetOwner16( pModule->dlls_to_init, hModule );
829 else pModule->dlls_to_init = 0;
831 NE_RegisterModule( pModule );
832 return hModule;
834 failed:
835 GlobalFree16( hModule );
836 return ERROR_BAD_FORMAT;
840 /***********************************************************************
841 * NE_LoadDLLs
843 * Load all DLLs implicitly linked to a module.
845 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
847 int i;
848 WORD *pModRef = (WORD *)((char *)pModule + pModule->ne_modtab);
849 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
851 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
853 char buffer[260], *p;
854 BYTE *pstr = (BYTE *)pModule + pModule->ne_imptab + *pModRef;
855 memcpy( buffer, pstr + 1, *pstr );
856 *(buffer + *pstr) = 0; /* terminate it */
858 TRACE("Loading '%s'\n", buffer );
859 if (!(*pModRef = GetModuleHandle16( buffer )))
861 /* If the DLL is not loaded yet, load it and store */
862 /* its handle in the list of DLLs to initialize. */
863 HMODULE16 hDLL;
865 /* Append .DLL to name if no extension present */
866 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
867 strcat( buffer, ".DLL" );
869 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
871 /* FIXME: cleanup what was done */
873 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
874 buffer, *((BYTE*)pModule + pModule->ne_restab),
875 (char *)pModule + pModule->ne_restab + 1, hDLL );
876 return FALSE;
878 *pModRef = GetExePtr( hDLL );
879 *pDLLs++ = *pModRef;
881 else /* Increment the reference count of the DLL */
883 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
884 if (pOldDLL) pOldDLL->count++;
887 return TRUE;
891 /**********************************************************************
892 * NE_DoLoadModule
894 * Load first instance of NE module from file.
896 * pModule must point to a module structure prepared by build_module_data.
897 * This routine must never be called twice on a module.
900 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
902 /* Allocate the segments for this module */
904 if (!NE_CreateAllSegments( pModule ))
905 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
907 /* Load the referenced DLLs */
909 if (!NE_LoadDLLs( pModule ))
910 return ERROR_FILE_NOT_FOUND; /* 2 */
912 /* Load the segments */
914 NE_LoadAllSegments( pModule );
916 /* Make sure the usage count is 1 on the first loading of */
917 /* the module, even if it contains circular DLL references */
919 pModule->count = 1;
921 return NE_GetInstance( pModule );
924 /**********************************************************************
925 * NE_LoadModule
927 * Load first instance of NE module. (Note: caller is responsible for
928 * ensuring the module isn't already loaded!)
930 * If the module turns out to be an executable module, only a
931 * handle to a module stub is returned; this needs to be initialized
932 * by calling NE_DoLoadModule later, in the context of the newly
933 * created process.
935 * If lib_only is TRUE, however, the module is perforce treated
936 * like a DLL module, even if it is an executable module.
939 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
941 NE_MODULE *pModule;
942 HMODULE16 hModule;
943 HINSTANCE16 hInstance;
944 HFILE16 hFile;
945 OFSTRUCT ofs;
946 HANDLE mapping;
947 void *ptr;
948 MEMORY_BASIC_INFORMATION info;
950 /* Open file */
951 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
952 return ERROR_FILE_NOT_FOUND;
954 mapping = CreateFileMappingW( DosFileHandleToWin32Handle(hFile), NULL, PAGE_WRITECOPY, 0, 0, NULL );
955 _lclose16( hFile );
956 if (!mapping) return ERROR_BAD_FORMAT;
958 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 0 );
959 CloseHandle( mapping );
960 if (!ptr) return ERROR_BAD_FORMAT;
962 VirtualQuery( ptr, &info, sizeof(info) );
963 hModule = build_module( ptr, info.RegionSize, ofs.szPathName );
965 if (hModule < 32)
967 UnmapViewOfFile( ptr );
968 return hModule;
971 SNOOP16_RegisterDLL( hModule, ofs.szPathName );
972 NE_InitResourceHandler( hModule );
974 pModule = NE_GetPtr( hModule );
976 if ( !lib_only && !( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) )
977 return hModule;
979 hInstance = NE_DoLoadModule( pModule );
980 if ( hInstance < 32 )
982 /* cleanup ... */
983 NE_FreeModule( hModule, 0 );
986 return hInstance;
990 /***********************************************************************
991 * NE_DoLoadBuiltinModule
993 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
995 static HMODULE16 NE_DoLoadBuiltinModule( const BUILTIN16_DESCRIPTOR *descr, const char *file_name )
997 NE_MODULE *pModule;
998 HMODULE16 hModule;
999 SEGTABLEENTRY *pSegTable;
1000 const IMAGE_DOS_HEADER *mz_header;
1001 const IMAGE_OS2_HEADER *ne_header;
1002 SIZE_T mapping_size;
1004 mz_header = descr->module;
1005 ne_header = (const IMAGE_OS2_HEADER *)((const BYTE *)mz_header + mz_header->e_lfanew);
1006 mapping_size = ne_header->ne_psegrefbytes << ne_header->ne_align;
1007 hModule = build_module( descr->module, mapping_size, file_name );
1008 if (hModule < 32) return hModule;
1009 pModule = GlobalLock16( hModule );
1010 pModule->ne_flags |= NE_FFLAGS_BUILTIN;
1011 pModule->count = 1;
1013 /* Allocate the code segment */
1015 pSegTable = NE_SEG_TABLE( pModule );
1016 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
1017 pSegTable->minsize, hModule,
1018 WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT );
1019 if (!pSegTable->hSeg) return ERROR_NOT_ENOUGH_MEMORY;
1020 patch_code_segment( descr->code_start );
1021 pSegTable->flags |= NE_SEGFLAGS_ALLOCATED | NE_SEGFLAGS_LOADED;
1022 pSegTable++;
1024 /* Allocate the data segment */
1026 if (!NE_CreateSegment( pModule, 2 )) return ERROR_NOT_ENOUGH_MEMORY;
1027 pModule->dgroup_entry = (char *)pSegTable - (char *)pModule;
1028 memcpy( GlobalLock16( pSegTable->hSeg ),
1029 (const char *)descr->module + (pSegTable->filepos << pModule->ne_align),
1030 pSegTable->minsize);
1031 pSegTable->flags |= NE_SEGFLAGS_LOADED;
1033 if (pModule->ne_heap)
1035 unsigned int size = pSegTable->minsize + pModule->ne_heap;
1036 if (size > 0xfffe) size = 0xfffe;
1037 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg), pSegTable->minsize, size );
1040 NE_InitResourceHandler( hModule );
1041 return hModule;
1045 /**********************************************************************
1046 * MODULE_LoadModule16
1048 * Load a NE module in the order of the loadorder specification.
1049 * The caller is responsible that the module is not loaded already.
1052 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1054 HINSTANCE16 hinst = 2;
1055 HMODULE16 hModule;
1056 NE_MODULE *pModule;
1057 const BUILTIN16_DESCRIPTOR *descr = NULL;
1058 const char *file_name = NULL;
1059 char dllname[20], owner[20], *p;
1060 const char *basename;
1061 int owner_exists;
1063 /* strip path information */
1065 basename = libname;
1066 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1067 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
1068 if ((p = strrchr( basename, '/' ))) basename = p + 1;
1070 if (strlen(basename) < sizeof(dllname)-4)
1072 strcpy( dllname, basename );
1073 p = strrchr( dllname, '.' );
1074 if (!p) strcat( dllname, ".dll" );
1075 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
1077 if (wine_dll_get_owner( dllname, owner, sizeof(owner), &owner_exists ) != -1)
1079 HMODULE mod32 = LoadLibraryA( owner );
1080 if (mod32)
1082 if (!(descr = find_dll_descr( dllname, &file_name )))
1084 FreeLibrary( mod32 );
1085 owner_exists = 0;
1087 /* loading the 32-bit library can have the side effect of loading the module */
1088 /* if so, simply incr the ref count and return the module */
1089 if ((hModule = GetModuleHandle16( libname )))
1091 TRACE( "module %s already loaded by owner\n", libname );
1092 pModule = NE_GetPtr( hModule );
1093 if (pModule) pModule->count++;
1094 return hModule;
1097 else
1099 /* it's probably disabled by the load order config */
1100 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner, dllname );
1101 return ERROR_FILE_NOT_FOUND;
1106 if (descr)
1108 TRACE("Trying built-in '%s'\n", libname);
1109 hinst = NE_DoLoadBuiltinModule( descr, file_name );
1110 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : builtin\n", debugstr_a(file_name));
1112 else
1114 TRACE("Trying native dll '%s'\n", libname);
1115 hinst = NE_LoadModule(libname, lib_only);
1116 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : native\n", debugstr_a(libname));
1117 if (hinst == ERROR_FILE_NOT_FOUND && owner_exists) hinst = 21; /* win32 module */
1120 if (hinst > 32 && !implicit)
1122 hModule = GetModuleHandle16(libname);
1123 if(!hModule)
1125 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1126 libname, hinst);
1127 return ERROR_INVALID_HANDLE;
1130 pModule = NE_GetPtr(hModule);
1131 if(!pModule)
1133 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1134 libname, hinst);
1135 return ERROR_INVALID_HANDLE;
1138 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1141 * Call initialization routines for all loaded DLLs. Note that
1142 * when we load implicitly linked DLLs this will be done by InitTask().
1144 if(pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1146 NE_InitializeDLLs(hModule);
1147 NE_DllProcessAttach(hModule);
1150 return hinst; /* The last error that occurred */
1154 /**********************************************************************
1155 * NE_CreateThread
1157 * Create the thread for a 16-bit module.
1159 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1161 HANDLE hThread;
1162 TDB *pTask;
1163 HTASK16 hTask;
1164 HINSTANCE16 instance = 0;
1166 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1167 return 0;
1169 /* Post event to start the task */
1170 PostEvent16( hTask );
1172 /* Wait until we get the instance handle */
1175 DirectedYield16( hTask );
1176 if (!IsTask16( hTask )) /* thread has died */
1178 DWORD exit_code;
1179 WaitForSingleObject( hThread, INFINITE );
1180 GetExitCodeThread( hThread, &exit_code );
1181 CloseHandle( hThread );
1182 return exit_code;
1184 if (!(pTask = GlobalLock16( hTask ))) break;
1185 instance = pTask->hInstance;
1186 GlobalUnlock16( hTask );
1187 } while (!instance);
1189 CloseHandle( hThread );
1190 return instance;
1194 /**********************************************************************
1195 * LoadModule (KERNEL.45)
1197 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1199 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1200 LOADPARAMS16 *params;
1201 HMODULE16 hModule;
1202 NE_MODULE *pModule;
1203 LPSTR cmdline;
1204 WORD cmdShow;
1206 /* Load module */
1208 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1210 /* Special case: second instance of an already loaded NE module */
1212 if ( !( pModule = NE_GetPtr( hModule ) ) ) return ERROR_BAD_FORMAT;
1213 if ( pModule->module32 ) return (HINSTANCE16)21;
1215 /* Increment refcount */
1217 pModule->count++;
1219 else
1221 /* Main case: load first instance of NE module */
1223 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1224 return hModule;
1226 if ( !(pModule = NE_GetPtr( hModule )) )
1227 return ERROR_BAD_FORMAT;
1230 /* If library module, we just retrieve the instance handle */
1232 if ( ( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1233 return NE_GetInstance( pModule );
1236 * At this point, we need to create a new process.
1238 * pModule points either to an already loaded module, whose refcount
1239 * has already been incremented (to avoid having the module vanish
1240 * in the meantime), or else to a stub module which contains only header
1241 * information.
1243 params = (LOADPARAMS16 *)paramBlock;
1244 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1245 cmdline = MapSL( params->cmdLine );
1246 return NE_CreateThread( pModule, cmdShow, cmdline );
1250 /**********************************************************************
1251 * NE_StartTask
1253 * Startup code for a new 16-bit task.
1255 DWORD NE_StartTask(void)
1257 TDB *pTask = TASK_GetCurrent();
1258 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1259 HINSTANCE16 hInstance, hPrevInstance;
1260 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1261 WORD sp;
1263 if ( pModule->count > 0 )
1265 /* Second instance of an already loaded NE module */
1266 /* Note that the refcount was already incremented by the parent */
1268 hPrevInstance = NE_GetInstance( pModule );
1270 if ( pModule->ne_autodata )
1271 if ( NE_CreateSegment( pModule, pModule->ne_autodata ) )
1272 NE_LoadSegment( pModule, pModule->ne_autodata );
1274 hInstance = NE_GetInstance( pModule );
1275 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->ne_autodata, hPrevInstance);
1278 else
1280 /* Load first instance of NE module */
1282 pModule->ne_flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1284 hInstance = NE_DoLoadModule( pModule );
1285 hPrevInstance = 0;
1288 if ( hInstance >= 32 )
1290 CONTEXT86 context;
1292 /* Enter instance handles into task struct */
1294 pTask->hInstance = hInstance;
1295 pTask->hPrevInstance = hPrevInstance;
1297 /* Use DGROUP for 16-bit stack */
1299 if (!(sp = OFFSETOF(pModule->ne_sssp)))
1300 sp = pSegTable[SELECTOROF(pModule->ne_sssp)-1].minsize + pModule->ne_stack;
1301 sp &= ~1;
1302 sp -= sizeof(STACK16FRAME);
1303 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1305 /* Registers at initialization must be:
1306 * ax zero
1307 * bx stack size in bytes
1308 * cx heap size in bytes
1309 * si previous app instance
1310 * di current app instance
1311 * bp zero
1312 * es selector to the PSP
1313 * ds dgroup of the application
1314 * ss stack selector
1315 * sp top of the stack
1317 memset( &context, 0, sizeof(context) );
1318 context.SegCs = GlobalHandleToSel16(pSegTable[SELECTOROF(pModule->ne_csip) - 1].hSeg);
1319 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1320 context.SegEs = pTask->hPDB;
1321 context.SegFs = wine_get_fs();
1322 context.SegGs = wine_get_gs();
1323 context.Eip = OFFSETOF(pModule->ne_csip);
1324 context.Ebx = pModule->ne_stack;
1325 context.Ecx = pModule->ne_heap;
1326 context.Edi = pTask->hInstance;
1327 context.Esi = pTask->hPrevInstance;
1329 /* Now call 16-bit entry point */
1331 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1332 context.SegCs, context.Eip, context.SegDs,
1333 SELECTOROF(NtCurrentTeb()->WOW32Reserved),
1334 OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
1336 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1337 ExitThread( LOWORD(context.Eax) );
1339 return hInstance; /* error code */
1342 /***********************************************************************
1343 * LoadLibrary (KERNEL.95)
1344 * LoadLibrary16 (KERNEL32.35)
1346 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1348 return LoadModule16(libname, (LPVOID)-1 );
1352 /**********************************************************************
1353 * MODULE_CallWEP
1355 * Call a DLL's WEP, allowing it to shut down.
1356 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1358 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1360 BOOL16 ret;
1361 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1362 if (!WEP) return FALSE;
1364 __TRY
1366 WORD args[1];
1367 DWORD dwRet;
1369 args[0] = WEP_FREE_DLL;
1370 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1371 ret = LOWORD(dwRet);
1373 __EXCEPT(page_fault)
1375 WARN("Page fault\n");
1376 ret = 0;
1378 __ENDTRY
1380 return ret;
1384 /**********************************************************************
1385 * NE_FreeModule
1387 * Implementation of FreeModule16().
1389 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1391 HMODULE16 *hPrevModule;
1392 NE_MODULE *pModule;
1393 HMODULE16 *pModRef;
1394 int i;
1396 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1397 hModule = pModule->self;
1399 TRACE("%04x count %d\n", hModule, pModule->count );
1401 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1402 else pModule->count = 0;
1404 if (pModule->ne_flags & NE_FFLAGS_BUILTIN)
1405 return FALSE; /* Can't free built-in module */
1407 if (call_wep && !(pModule->ne_flags & NE_FFLAGS_WIN32))
1409 /* Free the objects owned by the DLL module */
1410 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1412 if (pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1413 MODULE_CallWEP( hModule );
1414 else
1415 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1419 /* Clear magic number just in case */
1421 pModule->ne_magic = pModule->self = 0;
1422 if (!(pModule->ne_flags & NE_FFLAGS_BUILTIN)) UnmapViewOfFile( (void *)pModule->mapping );
1424 /* Remove it from the linked list */
1426 hPrevModule = &hFirstModule;
1427 while (*hPrevModule && (*hPrevModule != hModule))
1429 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1431 if (*hPrevModule) *hPrevModule = pModule->next;
1433 /* Free the referenced modules */
1435 pModRef = (HMODULE16*)((char *)pModule + pModule->ne_modtab);
1436 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
1438 NE_FreeModule( *pModRef, call_wep );
1441 /* Free the module storage */
1443 GlobalFreeAll16( hModule );
1444 return TRUE;
1448 /**********************************************************************
1449 * FreeModule (KERNEL.46)
1451 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1453 return NE_FreeModule( hModule, TRUE );
1457 /***********************************************************************
1458 * FreeLibrary (KERNEL.96)
1459 * FreeLibrary16 (KERNEL32.36)
1461 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1463 TRACE("%04x\n", handle );
1464 FreeModule16( handle );
1468 /***********************************************************************
1469 * GetModuleHandle16 (KERNEL32.@)
1471 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1473 HMODULE16 hModule = hFirstModule;
1474 LPSTR s;
1475 BYTE len, *name_table;
1476 char tmpstr[MAX_PATH];
1477 NE_MODULE *pModule;
1479 TRACE("(%s)\n", name);
1481 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1483 len = strlen(name);
1484 if (!len) return 0;
1486 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1488 /* If 'name' matches exactly the module name of a module:
1489 * Return its handle.
1491 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1493 pModule = NE_GetPtr( hModule );
1494 if (!pModule) break;
1495 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1497 name_table = (BYTE *)pModule + pModule->ne_restab;
1498 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1499 return hModule;
1502 /* If uppercased 'name' matches exactly the module name of a module:
1503 * Return its handle
1505 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1507 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1509 pModule = NE_GetPtr( hModule );
1510 if (!pModule) break;
1511 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1513 name_table = (BYTE *)pModule + pModule->ne_restab;
1514 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1515 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1516 * lowercased module names, but try to load uppercase DLLs, so this
1517 * 'i' compare is just a quickfix until the loader handles that
1518 * correctly. -MM 990705
1520 if ((*name_table == len) && !NE_strncasecmp(tmpstr, name_table+1, len))
1521 return hModule;
1524 /* If the base filename of 'name' matches the base filename of the module
1525 * filename of some module (case-insensitive compare):
1526 * Return its handle.
1529 /* basename: search backwards in passed name to \ / or : */
1530 s = tmpstr + strlen(tmpstr);
1531 while (s > tmpstr)
1533 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1534 break;
1535 s--;
1538 /* search this in loaded filename list */
1539 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1541 char *loadedfn;
1542 OFSTRUCT *ofs;
1544 pModule = NE_GetPtr( hModule );
1545 if (!pModule) break;
1546 if (!pModule->fileinfo) continue;
1547 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1549 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1550 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1551 /* basename: search backwards in pathname to \ / or : */
1552 while (loadedfn > (char*)ofs->szPathName)
1554 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1555 break;
1556 loadedfn--;
1558 /* case insensitive compare ... */
1559 if (!NE_strcasecmp(loadedfn, s))
1560 return hModule;
1562 return 0;
1566 /**********************************************************************
1567 * GetModuleName (KERNEL.27)
1569 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1571 NE_MODULE *pModule;
1572 BYTE *p;
1574 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1575 p = (BYTE *)pModule + pModule->ne_restab;
1576 if (count > *p) count = *p + 1;
1577 if (count > 0)
1579 memcpy( buf, p + 1, count - 1 );
1580 buf[count-1] = '\0';
1582 return TRUE;
1586 /**********************************************************************
1587 * GetModuleFileName (KERNEL.49)
1589 * Comment: see GetModuleFileNameA
1591 * Even if invoked by second instance of a program,
1592 * it still returns path of first one.
1594 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1595 INT16 nSize )
1597 NE_MODULE *pModule;
1599 /* Win95 does not query hModule if set to 0 !
1600 * Is this wrong or maybe Win3.1 only ? */
1601 if (!hModule) hModule = GetCurrentTask();
1603 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1604 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1605 if (pModule->ne_expver >= 0x400)
1606 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1607 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1608 return strlen(lpFileName);
1612 /**********************************************************************
1613 * GetModuleUsage (KERNEL.48)
1615 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1617 NE_MODULE *pModule = NE_GetPtr( hModule );
1618 return pModule ? pModule->count : 0;
1622 /**********************************************************************
1623 * GetExpWinVer (KERNEL.167)
1625 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1627 NE_MODULE *pModule = NE_GetPtr( hModule );
1628 if ( !pModule ) return 0;
1631 * For built-in modules, fake the expected version the module should
1632 * have according to the Windows version emulated by Wine
1634 if ( !pModule->ne_expver )
1636 OSVERSIONINFOA versionInfo;
1637 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1639 if ( GetVersionExA( &versionInfo ) )
1640 pModule->ne_expver =
1641 (versionInfo.dwMajorVersion & 0xff) << 8
1642 | (versionInfo.dwMinorVersion & 0xff);
1645 return pModule->ne_expver;
1649 /***********************************************************************
1650 * WinExec (KERNEL.166)
1652 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1654 LPCSTR p, args = NULL;
1655 LPCSTR name_beg, name_end;
1656 LPSTR name, cmdline;
1657 int arglen;
1658 HINSTANCE16 ret;
1659 char buffer[MAX_PATH];
1661 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1663 name_beg = lpCmdLine+1;
1664 p = strchr ( lpCmdLine+1, '"' );
1665 if (p)
1667 name_end = p;
1668 args = strchr ( p, ' ' );
1670 else /* yes, even valid with trailing '"' missing */
1671 name_end = lpCmdLine+strlen(lpCmdLine);
1673 else
1675 name_beg = lpCmdLine;
1676 args = strchr( lpCmdLine, ' ' );
1677 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1680 if ((name_beg == lpCmdLine) && (!args))
1681 { /* just use the original cmdline string as file name */
1682 name = (LPSTR)lpCmdLine;
1684 else
1686 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1687 return ERROR_NOT_ENOUGH_MEMORY;
1688 memcpy( name, name_beg, name_end - name_beg );
1689 name[name_end - name_beg] = '\0';
1692 if (args)
1694 args++;
1695 arglen = strlen(args);
1696 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1697 cmdline[0] = (BYTE)arglen;
1698 strcpy( cmdline + 1, args );
1700 else
1702 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1703 cmdline[0] = cmdline[1] = 0;
1706 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1708 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1710 LOADPARAMS16 params;
1711 WORD showCmd[2];
1712 showCmd[0] = 2;
1713 showCmd[1] = nCmdShow;
1715 params.hEnvironment = 0;
1716 params.cmdLine = MapLS( cmdline );
1717 params.showCmd = MapLS( showCmd );
1718 params.reserved = 0;
1720 ret = LoadModule16( buffer, &params );
1721 UnMapLS( params.cmdLine );
1722 UnMapLS( params.showCmd );
1724 else ret = GetLastError();
1726 HeapFree( GetProcessHeap(), 0, cmdline );
1727 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1729 if (ret == 21 || ret == ERROR_BAD_FORMAT) /* 32-bit module or unknown executable*/
1731 DWORD count;
1732 ReleaseThunkLock( &count );
1733 ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) );
1734 RestoreThunkLock( count );
1736 return ret;
1739 /***********************************************************************
1740 * GetProcAddress (KERNEL.50)
1742 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1744 WORD ordinal;
1745 FARPROC16 ret;
1747 if (!hModule) hModule = GetCurrentTask();
1748 hModule = GetExePtr( hModule );
1750 if (HIWORD(name) != 0)
1752 ordinal = NE_GetOrdinal( hModule, name );
1753 TRACE("%04x '%s'\n", hModule, name );
1755 else
1757 ordinal = LOWORD(name);
1758 TRACE("%04x %04x\n", hModule, ordinal );
1760 if (!ordinal) return (FARPROC16)0;
1762 ret = NE_GetEntryPoint( hModule, ordinal );
1764 TRACE("returning %08x\n", (UINT)ret );
1765 return ret;
1769 /***************************************************************************
1770 * HasGPHandler (KERNEL.338)
1772 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1774 HMODULE16 hModule;
1775 int gpOrdinal;
1776 SEGPTR gpPtr;
1777 GPHANDLERDEF *gpHandler;
1779 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1780 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1781 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1782 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1783 && (gpHandler = MapSL( gpPtr )) != NULL )
1785 while (gpHandler->selector)
1787 if ( SELECTOROF(address) == gpHandler->selector
1788 && OFFSETOF(address) >= gpHandler->rangeStart
1789 && OFFSETOF(address) < gpHandler->rangeEnd )
1790 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1791 gpHandler++;
1795 return 0;
1799 /**********************************************************************
1800 * GetModuleHandle (KERNEL.47)
1802 * Find a module from a module name.
1804 * NOTE: The current implementation works the same way the Windows 95 one
1805 * does. Do not try to 'fix' it, fix the callers.
1806 * + It does not do ANY extension handling (except that strange .EXE bit)!
1807 * + It does not care about paths, just about basenames. (same as Windows)
1809 * RETURNS
1810 * LOWORD:
1811 * the win16 module handle if found
1812 * 0 if not
1813 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1814 * Always hFirstModule
1816 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1818 if (HIWORD(name) == 0)
1819 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1820 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1823 /**********************************************************************
1824 * NE_GetModuleByFilename
1826 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1828 HMODULE16 hModule;
1829 LPSTR s, p;
1830 BYTE len, *name_table;
1831 char tmpstr[MAX_PATH];
1832 NE_MODULE *pModule;
1834 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1836 /* If the base filename of 'name' matches the base filename of the module
1837 * filename of some module (case-insensitive compare):
1838 * Return its handle.
1841 /* basename: search backwards in passed name to \ / or : */
1842 s = tmpstr + strlen(tmpstr);
1843 while (s > tmpstr)
1845 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1846 break;
1847 s--;
1850 /* search this in loaded filename list */
1851 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1853 char *loadedfn;
1854 OFSTRUCT *ofs;
1856 pModule = NE_GetPtr( hModule );
1857 if (!pModule) break;
1858 if (!pModule->fileinfo) continue;
1859 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1861 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1862 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1863 /* basename: search backwards in pathname to \ / or : */
1864 while (loadedfn > (char*)ofs->szPathName)
1866 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1867 break;
1868 loadedfn--;
1870 /* case insensitive compare ... */
1871 if (!NE_strcasecmp(loadedfn, s))
1872 return hModule;
1874 /* If basename (without ext) matches the module name of a module:
1875 * Return its handle.
1878 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1879 len = strlen(s);
1881 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1883 pModule = NE_GetPtr( hModule );
1884 if (!pModule) break;
1885 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1887 name_table = (BYTE *)pModule + pModule->ne_restab;
1888 if ((*name_table == len) && !NE_strncasecmp(s, name_table+1, len))
1889 return hModule;
1892 return 0;
1895 /***********************************************************************
1896 * GetProcAddress16 (KERNEL32.37)
1897 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1899 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1901 if (!hModule) return 0;
1902 if (HIWORD(hModule))
1904 WARN("hModule is Win32 handle (%p)\n", hModule );
1905 return 0;
1907 return GetProcAddress16( LOWORD(hModule), name );
1910 /**********************************************************************
1911 * ModuleFirst (TOOLHELP.59)
1913 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1915 lpme->wNext = hFirstModule;
1916 return ModuleNext16( lpme );
1920 /**********************************************************************
1921 * ModuleNext (TOOLHELP.60)
1923 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1925 NE_MODULE *pModule;
1926 char *name;
1928 if (!lpme->wNext) return FALSE;
1929 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1930 name = (char *)pModule + pModule->ne_restab;
1931 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1932 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1933 lpme->hModule = lpme->wNext;
1934 lpme->wcUsage = pModule->count;
1935 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1936 lpme->wNext = pModule->next;
1937 return TRUE;
1941 /**********************************************************************
1942 * ModuleFindName (TOOLHELP.61)
1944 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1946 lpme->wNext = GetModuleHandle16( name );
1947 return ModuleNext16( lpme );
1951 /**********************************************************************
1952 * ModuleFindHandle (TOOLHELP.62)
1954 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1956 hModule = GetExePtr( hModule );
1957 lpme->wNext = hModule;
1958 return ModuleNext16( lpme );
1962 /***************************************************************************
1963 * IsRomModule (KERNEL.323)
1965 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1967 return FALSE;
1970 /***************************************************************************
1971 * IsRomFile (KERNEL.326)
1973 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1975 return FALSE;
1978 /***********************************************************************
1979 * create_dummy_module
1981 * Create a dummy NE module for Win32 or Winelib.
1983 static HMODULE16 create_dummy_module( HMODULE module32 )
1985 HMODULE16 hModule;
1986 NE_MODULE *pModule;
1987 SEGTABLEENTRY *pSegment;
1988 char *pStr,*s;
1989 unsigned int len;
1990 const char* basename;
1991 OFSTRUCT *ofs;
1992 int of_size, size;
1993 char filename[MAX_PATH];
1994 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
1996 if (!nt) return ERROR_BAD_FORMAT;
1998 /* Extract base filename */
1999 len = GetModuleFileNameA( module32, filename, sizeof(filename) );
2000 if (!len || len >= sizeof(filename)) return ERROR_BAD_FORMAT;
2001 basename = strrchr(filename, '\\');
2002 if (!basename) basename = filename;
2003 else basename++;
2004 len = strlen(basename);
2005 if ((s = strchr(basename, '.'))) len = s - basename;
2007 /* Allocate module */
2008 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
2009 + strlen(filename) + 1;
2010 size = sizeof(NE_MODULE) +
2011 /* loaded file info */
2012 ((of_size + 3) & ~3) +
2013 /* segment table: DS,CS */
2014 2 * sizeof(SEGTABLEENTRY) +
2015 /* name table */
2016 len + 2 +
2017 /* several empty tables */
2020 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2021 if (!hModule) return ERROR_BAD_FORMAT;
2023 FarSetOwner16( hModule, hModule );
2024 pModule = (NE_MODULE *)GlobalLock16( hModule );
2026 /* Set all used entries */
2027 pModule->ne_magic = IMAGE_OS2_SIGNATURE;
2028 pModule->count = 1;
2029 pModule->next = 0;
2030 pModule->ne_flags = NE_FFLAGS_WIN32;
2031 pModule->ne_autodata = 0;
2032 pModule->ne_sssp = MAKESEGPTR( 0, 1 );
2033 pModule->ne_csip = MAKESEGPTR( 0, 2 );
2034 pModule->ne_heap = 0;
2035 pModule->ne_stack = 0;
2036 pModule->ne_cseg = 2;
2037 pModule->ne_cmod = 0;
2038 pModule->ne_cbnrestab = 0;
2039 pModule->fileinfo = sizeof(NE_MODULE);
2040 pModule->ne_exetyp = NE_OSFLAGS_WINDOWS;
2041 pModule->self = hModule;
2042 pModule->module32 = module32;
2044 /* Set version and flags */
2045 pModule->ne_expver = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2046 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2047 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2048 pModule->ne_flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2050 /* Set loaded file information */
2051 ofs = (OFSTRUCT *)(pModule + 1);
2052 memset( ofs, 0, of_size );
2053 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2054 strcpy( ofs->szPathName, filename );
2056 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2057 pModule->ne_segtab = (char *)pSegment - (char *)pModule;
2058 /* Data segment */
2059 pSegment->size = 0;
2060 pSegment->flags = NE_SEGFLAGS_DATA;
2061 pSegment->minsize = 0x1000;
2062 pSegment++;
2063 /* Code segment */
2064 pSegment->flags = 0;
2065 pSegment++;
2067 /* Module name */
2068 pStr = (char *)pSegment;
2069 pModule->ne_restab = pStr - (char *)pModule;
2070 assert(len<256);
2071 *pStr = len;
2072 lstrcpynA( pStr+1, basename, len+1 );
2073 pStr += len+2;
2075 /* All tables zero terminated */
2076 pModule->ne_rsrctab = pModule->ne_imptab = pModule->ne_enttab = (char *)pStr - (char *)pModule;
2078 NE_RegisterModule( pModule );
2079 LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2080 return hModule;
2083 /***********************************************************************
2084 * PrivateLoadLibrary (KERNEL32.@)
2086 * FIXME: rough guesswork, don't know what "Private" means
2088 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2090 return LoadLibrary16(libname);
2093 /***********************************************************************
2094 * PrivateFreeLibrary (KERNEL32.@)
2096 * FIXME: rough guesswork, don't know what "Private" means
2098 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2100 FreeLibrary16(handle);
2103 /***********************************************************************
2104 * LoadLibrary32 (KERNEL.452)
2105 * LoadSystemLibrary32 (KERNEL.482)
2107 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2109 HMODULE hModule;
2110 DWORD count;
2112 ReleaseThunkLock( &count );
2113 hModule = LoadLibraryA( libname );
2114 RestoreThunkLock( count );
2115 return hModule;
2118 /***************************************************************************
2119 * MapHModuleLS (KERNEL32.@)
2121 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2123 HMODULE16 ret;
2124 NE_MODULE *pModule;
2126 if (!hmod)
2127 return TASK_GetCurrent()->hInstance;
2128 if (!HIWORD(hmod))
2129 return LOWORD(hmod); /* we already have a 16 bit module handle */
2130 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
2131 while (pModule) {
2132 if (pModule->module32 == hmod)
2133 return pModule->self;
2134 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
2136 if ((ret = create_dummy_module( hmod )) < 32)
2138 SetLastError(ret);
2139 ret = 0;
2141 return ret;
2144 /***************************************************************************
2145 * MapHModuleSL (KERNEL32.@)
2147 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2149 NE_MODULE *pModule;
2151 if (!hmod) {
2152 TDB *pTask = TASK_GetCurrent();
2153 hmod = pTask->hModule;
2155 pModule = (NE_MODULE*)GlobalLock16(hmod);
2156 if ((pModule->ne_magic != IMAGE_OS2_SIGNATURE) || !(pModule->ne_flags & NE_FFLAGS_WIN32))
2157 return 0;
2158 return pModule->module32;
2161 /***************************************************************************
2162 * MapHInstLS (KERNEL32.@)
2163 * MapHInstLS (KERNEL.472)
2165 void WINAPI __regs_MapHInstLS( CONTEXT86 *context )
2167 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2169 #ifdef DEFINE_REGS_ENTRYPOINT
2170 DEFINE_REGS_ENTRYPOINT( MapHInstLS, 0, 0 );
2171 #endif
2173 /***************************************************************************
2174 * MapHInstSL (KERNEL32.@)
2175 * MapHInstSL (KERNEL.473)
2177 void WINAPI __regs_MapHInstSL( CONTEXT86 *context )
2179 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2181 #ifdef DEFINE_REGS_ENTRYPOINT
2182 DEFINE_REGS_ENTRYPOINT( MapHInstSL, 0, 0 );
2183 #endif
2185 /***************************************************************************
2186 * MapHInstLS_PN (KERNEL32.@)
2188 void WINAPI __regs_MapHInstLS_PN( CONTEXT86 *context )
2190 if (context->Eax) context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2192 #ifdef DEFINE_REGS_ENTRYPOINT
2193 DEFINE_REGS_ENTRYPOINT( MapHInstLS_PN, 0, 0 );
2194 #endif
2196 /***************************************************************************
2197 * MapHInstSL_PN (KERNEL32.@)
2199 void WINAPI __regs_MapHInstSL_PN( CONTEXT86 *context )
2201 if (context->Eax) context->Eax = (DWORD)MapHModuleSL( context->Eax );
2203 #ifdef DEFINE_REGS_ENTRYPOINT
2204 DEFINE_REGS_ENTRYPOINT( MapHInstSL_PN, 0, 0 );
2205 #endif