Renamed __wine_(un)register_dll_16 to __wine_dll_(un)register_16 for
[wine/testsucceed.git] / dlls / kernel / ne_module.c
blobe2e4866f3c226f2246b7cccc0bcae064c151a29e
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 const void *rsrc; /* resources data */
76 } BUILTIN16_DESCRIPTOR;
78 struct builtin_dll
80 const BUILTIN16_DESCRIPTOR *descr; /* module descriptor */
81 const char *file_name; /* module file name */
84 /* Table of all built-in DLLs */
86 #define MAX_DLLS 50
88 static struct builtin_dll builtin_dlls[MAX_DLLS];
90 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
91 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
93 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
95 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
98 static WINE_EXCEPTION_FILTER(page_fault)
100 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
101 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
102 return EXCEPTION_EXECUTE_HANDLER;
103 return EXCEPTION_CONTINUE_SEARCH;
107 /* patch all the flat cs references of the code segment if necessary */
108 inline static void patch_code_segment( void *code_segment )
110 #ifdef __i386__
111 CALLFROM16 *call = code_segment;
112 if (call->flatcs == wine_get_cs()) return; /* nothing to patch */
113 while (call->pushl == 0x68)
115 call->flatcs = wine_get_cs();
116 call++;
118 #endif
122 /***********************************************************************
123 * NE_strcasecmp
125 * locale-independent case conversion for module lookups
127 static int NE_strcasecmp( const char *str1, const char *str2 )
129 int ret = 0;
130 for ( ; ; str1++, str2++)
131 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
132 return ret;
136 /***********************************************************************
137 * NE_strncasecmp
139 * locale-independent case conversion for module lookups
141 static int NE_strncasecmp( const char *str1, const char *str2, int len )
143 int ret = 0;
144 for ( ; len > 0; len--, str1++, str2++)
145 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
146 return ret;
150 /***********************************************************************
151 * find_dll_descr
153 * Find a descriptor in the list
155 static const BUILTIN16_DESCRIPTOR *find_dll_descr( const char *dllname, const char **file_name )
157 int i;
158 const IMAGE_DOS_HEADER *mz_header;
159 const IMAGE_OS2_HEADER *ne_header;
160 BYTE *name_table;
162 for (i = 0; i < MAX_DLLS; i++)
164 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i].descr;
165 if (descr)
167 mz_header = descr->module;
168 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mz_header + mz_header->e_lfanew);
169 name_table = (BYTE *)ne_header + ne_header->ne_restab;
171 /* check the dll file name */
172 if (!NE_strcasecmp( builtin_dlls[i].file_name, dllname ) ||
173 /* check the dll module name (without extension) */
174 (!NE_strncasecmp( dllname, name_table+1, *name_table ) &&
175 !strcmp( dllname + *name_table, ".dll" )))
177 *file_name = builtin_dlls[i].file_name;
178 return builtin_dlls[i].descr;
182 return NULL;
186 /***********************************************************************
187 * __wine_dll_register_16 (KERNEL32.@)
189 * Register a built-in DLL descriptor.
191 void __wine_dll_register_16( const BUILTIN16_DESCRIPTOR *descr, const char *file_name )
193 int i;
195 for (i = 0; i < MAX_DLLS; i++)
197 if (builtin_dlls[i].descr) continue;
198 builtin_dlls[i].descr = descr;
199 builtin_dlls[i].file_name = file_name;
200 break;
202 assert( i < MAX_DLLS );
206 /***********************************************************************
207 * __wine_dll_unregister_16 (KERNEL32.@)
209 * Unregister a built-in DLL descriptor.
211 void __wine_dll_unregister_16( const BUILTIN16_DESCRIPTOR *descr )
213 int i;
215 for (i = 0; i < MAX_DLLS; i++)
217 if (builtin_dlls[i].descr != descr) continue;
218 builtin_dlls[i].descr = NULL;
219 break;
224 /***********************************************************************
225 * NE_GetPtr
227 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
229 return (NE_MODULE *)GlobalLock16( GetExePtr(hModule) );
233 /**********************************************************************
234 * NE_RegisterModule
236 static void NE_RegisterModule( NE_MODULE *pModule )
238 pModule->next = hFirstModule;
239 hFirstModule = pModule->self;
243 /***********************************************************************
244 * NE_DumpModule
246 void NE_DumpModule( HMODULE16 hModule )
248 int i, ordinal;
249 SEGTABLEENTRY *pSeg;
250 BYTE *pstr;
251 WORD *pword;
252 NE_MODULE *pModule;
253 ET_BUNDLE *bundle;
254 ET_ENTRY *entry;
256 if (!(pModule = NE_GetPtr( hModule )))
258 MESSAGE( "**** %04x is not a module handle\n", hModule );
259 return;
262 /* Dump the module info */
263 DPRINTF( "---\n" );
264 DPRINTF( "Module %04x:\n", hModule );
265 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
266 pModule->count, pModule->ne_flags,
267 pModule->ne_heap, pModule->ne_stack );
268 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
269 SELECTOROF(pModule->ne_csip), OFFSETOF(pModule->ne_csip),
270 SELECTOROF(pModule->ne_sssp), OFFSETOF(pModule->ne_sssp),
271 pModule->ne_autodata, pModule->ne_cseg, pModule->ne_cmod );
272 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
273 pModule->ne_exetyp, pModule->ne_swaparea, pModule->ne_expver );
274 if (pModule->ne_flags & NE_FFLAGS_WIN32)
275 DPRINTF( "PE module=%p\n", pModule->module32 );
277 /* Dump the file info */
278 DPRINTF( "---\n" );
279 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
281 /* Dump the segment table */
282 DPRINTF( "---\n" );
283 DPRINTF( "Segment table:\n" );
284 pSeg = NE_SEG_TABLE( pModule );
285 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
286 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
287 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
288 pSeg->minsize, pSeg->hSeg );
290 /* Dump the resource table */
291 DPRINTF( "---\n" );
292 DPRINTF( "Resource table:\n" );
293 if (pModule->ne_rsrctab)
295 pword = (WORD *)((BYTE *)pModule + pModule->ne_rsrctab);
296 DPRINTF( "Alignment: %d\n", *pword++ );
297 while (*pword)
299 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
300 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
301 DPRINTF( "id=%04x count=%d\n", ptr->type_id, ptr->count );
302 for (i = 0; i < ptr->count; i++, pname++)
303 DPRINTF( "offset=%d len=%d id=%04x\n",
304 pname->offset, pname->length, pname->id );
305 pword = (WORD *)pname;
308 else DPRINTF( "None\n" );
310 /* Dump the resident name table */
311 DPRINTF( "---\n" );
312 DPRINTF( "Resident-name table:\n" );
313 pstr = (char *)pModule + pModule->ne_restab;
314 while (*pstr)
316 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
317 *(WORD *)(pstr + *pstr + 1) );
318 pstr += *pstr + 1 + sizeof(WORD);
321 /* Dump the module reference table */
322 DPRINTF( "---\n" );
323 DPRINTF( "Module ref table:\n" );
324 if (pModule->ne_modtab)
326 pword = (WORD *)((BYTE *)pModule + pModule->ne_modtab);
327 for (i = 0; i < pModule->ne_cmod; i++, pword++)
329 char name[10];
330 GetModuleName16( *pword, name, sizeof(name) );
331 DPRINTF( "%d: %04x -> '%s'\n", i, *pword, name );
334 else DPRINTF( "None\n" );
336 /* Dump the entry table */
337 DPRINTF( "---\n" );
338 DPRINTF( "Entry table:\n" );
339 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->ne_enttab);
340 do {
341 entry = (ET_ENTRY *)((BYTE *)bundle+6);
342 DPRINTF( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
343 ordinal = bundle->first;
344 while (ordinal < bundle->last)
346 if (entry->type == 0xff)
347 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
348 else
349 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
350 entry++;
352 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
354 /* Dump the non-resident names table */
355 DPRINTF( "---\n" );
356 DPRINTF( "Non-resident names table:\n" );
357 if (pModule->nrname_handle)
359 pstr = (char *)GlobalLock16( pModule->nrname_handle );
360 while (*pstr)
362 DPRINTF( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
363 *(WORD *)(pstr + *pstr + 1) );
364 pstr += *pstr + 1 + sizeof(WORD);
367 DPRINTF( "\n" );
371 /***********************************************************************
372 * NE_WalkModules
374 * Walk the module list and print the modules.
376 void NE_WalkModules(void)
378 HMODULE16 hModule = hFirstModule;
379 MESSAGE( "Module Flags Name\n" );
380 while (hModule)
382 NE_MODULE *pModule = NE_GetPtr( hModule );
383 if (!pModule)
385 MESSAGE( "Bad module %04x in list\n", hModule );
386 return;
388 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->ne_flags,
389 *((char *)pModule + pModule->ne_restab),
390 (char *)pModule + pModule->ne_restab + 1 );
391 hModule = pModule->next;
396 /***********************************************************************
397 * NE_InitResourceHandler
399 * Fill in 'resloader' fields in the resource table.
401 static void NE_InitResourceHandler( HMODULE16 hModule )
403 static FARPROC16 proc;
405 NE_TYPEINFO *pTypeInfo;
406 NE_MODULE *pModule;
408 if (!(pModule = NE_GetPtr( hModule )) || !pModule->ne_rsrctab) return;
410 TRACE("InitResourceHandler[%04x]\n", hModule );
412 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
414 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
415 while(pTypeInfo->type_id)
417 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
418 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
423 /***********************************************************************
424 * NE_GetOrdinal
426 * Lookup the ordinal for a given name.
428 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
430 unsigned char buffer[256], *cpnt;
431 BYTE len;
432 NE_MODULE *pModule;
434 if (!(pModule = NE_GetPtr( hModule ))) return 0;
435 if (pModule->ne_flags & NE_FFLAGS_WIN32) return 0;
437 TRACE("(%04x,'%s')\n", hModule, name );
439 /* First handle names of the form '#xxxx' */
441 if (name[0] == '#') return atoi( name + 1 );
443 /* Now copy and uppercase the string */
445 strcpy( buffer, name );
446 for (cpnt = buffer; *cpnt; cpnt++) *cpnt = RtlUpperChar(*cpnt);
447 len = cpnt - buffer;
449 /* First search the resident names */
451 cpnt = (char *)pModule + pModule->ne_restab;
453 /* Skip the first entry (module name) */
454 cpnt += *cpnt + 1 + sizeof(WORD);
455 while (*cpnt)
457 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
459 WORD ordinal;
460 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
461 TRACE(" Found: ordinal=%d\n", ordinal );
462 return ordinal;
464 cpnt += *cpnt + 1 + sizeof(WORD);
467 /* Now search the non-resident names table */
469 if (!pModule->nrname_handle) return 0; /* No non-resident table */
470 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
472 /* Skip the first entry (module description string) */
473 cpnt += *cpnt + 1 + sizeof(WORD);
474 while (*cpnt)
476 if (((BYTE)*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
478 WORD ordinal;
479 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
480 TRACE(" Found: ordinal=%d\n", ordinal );
481 return ordinal;
483 cpnt += *cpnt + 1 + sizeof(WORD);
485 return 0;
489 /***********************************************************************
490 * NE_GetEntryPoint
492 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
494 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
497 /***********************************************************************
498 * NE_GetEntryPointEx
500 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
502 NE_MODULE *pModule;
503 WORD sel, offset, i;
505 ET_ENTRY *entry;
506 ET_BUNDLE *bundle;
508 if (!(pModule = NE_GetPtr( hModule ))) return 0;
509 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
511 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
512 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
514 if (!(bundle->next))
515 return 0;
516 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
519 entry = (ET_ENTRY *)((BYTE *)bundle+6);
520 for (i=0; i < (ordinal - bundle->first - 1); i++)
521 entry++;
523 sel = entry->segnum;
524 memcpy( &offset, &entry->offs, sizeof(WORD) );
526 if (sel == 0xfe) sel = 0xffff; /* constant entry */
527 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
528 if (sel==0xffff)
529 return (FARPROC16)MAKESEGPTR( sel, offset );
530 if (!snoop)
531 return (FARPROC16)MAKESEGPTR( sel, offset );
532 else
533 return (FARPROC16)SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
537 /***********************************************************************
538 * EntryAddrProc (KERNEL.667) Wine-specific export
540 * Return the entry point for a given ordinal.
542 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
544 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
545 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
546 return ret;
549 /***********************************************************************
550 * NE_SetEntryPoint
552 * Change the value of an entry point. Use with caution!
553 * It can only change the offset value, not the selector.
555 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
557 NE_MODULE *pModule;
558 ET_ENTRY *entry;
559 ET_BUNDLE *bundle;
560 int i;
562 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
563 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
565 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
566 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
568 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
569 if (!(bundle->next)) return 0;
572 entry = (ET_ENTRY *)((BYTE *)bundle+6);
573 for (i=0; i < (ordinal - bundle->first - 1); i++)
574 entry++;
576 memcpy( &entry->offs, &offset, sizeof(WORD) );
577 return TRUE;
581 /***********************************************************************
582 * build_bundle_data
584 * Build the entry table bundle data from the on-disk format. Helper for build_module.
586 static void *build_bundle_data( NE_MODULE *pModule, void *dest, const BYTE *table )
588 ET_BUNDLE *oldbundle, *bundle = dest;
589 ET_ENTRY *entry;
590 BYTE nr_entries, type;
592 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
593 entry = (ET_ENTRY *)((BYTE *)bundle+6);
595 while ((nr_entries = *table++))
597 if ((type = *table++))
599 bundle->last += nr_entries;
600 if (type == 0xff)
602 while (nr_entries--)
604 entry->type = type;
605 entry->flags = *table++;
606 table += sizeof(WORD);
607 entry->segnum = *table++;
608 entry->offs = *(WORD *)table;
609 table += sizeof(WORD);
610 entry++;
613 else
615 while (nr_entries--)
617 entry->type = type;
618 entry->flags = *table++;
619 entry->segnum = type;
620 entry->offs = *(WORD *)table;
621 table += sizeof(WORD);
622 entry++;
626 else
628 if (bundle->first == bundle->last)
630 bundle->first += nr_entries;
631 bundle->last += nr_entries;
633 else
635 oldbundle = bundle;
636 oldbundle->next = (char *)entry - (char *)pModule;
637 bundle = (ET_BUNDLE *)entry;
638 bundle->first = bundle->last = oldbundle->last + nr_entries;
639 bundle->next = 0;
640 entry = (ET_ENTRY*)(((BYTE*)entry)+sizeof(ET_BUNDLE));
644 return entry;
648 /***********************************************************************
649 * build_module
651 * Build the in-memory module from the on-disk data.
653 static HMODULE16 build_module( const void *mapping, SIZE_T mapping_size, LPCSTR path )
655 const IMAGE_DOS_HEADER *mz_header = mapping;
656 const IMAGE_OS2_HEADER *ne_header;
657 const struct ne_segment_table_entry_s *pSeg;
658 const void *ptr;
659 int i;
660 size_t size;
661 HMODULE16 hModule;
662 NE_MODULE *pModule;
663 BYTE *buffer, *pData, *end;
664 OFSTRUCT *ofs;
666 if (mapping_size < sizeof(*mz_header)) return ERROR_BAD_FORMAT;
667 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE) return ERROR_BAD_FORMAT;
668 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mapping + mz_header->e_lfanew);
669 if (mz_header->e_lfanew + sizeof(*ne_header) > mapping_size) return ERROR_BAD_FORMAT;
670 if (ne_header->ne_magic == IMAGE_NT_SIGNATURE) return 21; /* win32 exe */
671 if (ne_header->ne_magic == IMAGE_OS2_SIGNATURE_LX)
673 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path);
674 return 12;
676 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE) return ERROR_BAD_FORMAT;
678 /* We now have a valid NE header */
680 /* check to be able to fall back to loading OS/2 programs as DOS
681 * FIXME: should this check be reversed in order to be less strict?
682 * (only fail for OS/2 ne_exetyp 0x01 here?) */
683 if ((ne_header->ne_exetyp != 0x02 /* Windows */)
684 && (ne_header->ne_exetyp != 0x04) /* Windows 386 */)
685 return ERROR_BAD_FORMAT;
687 size = sizeof(NE_MODULE) +
688 /* segment table */
689 ne_header->ne_cseg * sizeof(SEGTABLEENTRY) +
690 /* resource table */
691 ne_header->ne_restab - ne_header->ne_rsrctab +
692 /* resident names table */
693 ne_header->ne_modtab - ne_header->ne_restab +
694 /* module ref table */
695 ne_header->ne_cmod * sizeof(WORD) +
696 /* imported names table */
697 ne_header->ne_enttab - ne_header->ne_imptab +
698 /* entry table length */
699 ne_header->ne_cbenttab +
700 /* entry table extra conversion space */
701 sizeof(ET_BUNDLE) +
702 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6) +
703 /* loaded file info */
704 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
706 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
707 if (!hModule) return ERROR_BAD_FORMAT;
709 FarSetOwner16( hModule, hModule );
710 pModule = (NE_MODULE *)GlobalLock16( hModule );
711 memcpy( pModule, ne_header, sizeof(*ne_header) );
712 pModule->count = 0;
713 /* check programs for default minimal stack size */
714 if (!(pModule->ne_flags & NE_FFLAGS_LIBMODULE) && (pModule->ne_stack < 0x1400))
715 pModule->ne_stack = 0x1400;
717 pModule->self = hModule;
718 pModule->mapping = mapping;
719 pModule->mapping_size = mapping_size;
721 pData = (BYTE *)(pModule + 1);
723 /* Clear internal Wine flags in case they are set in the EXE file */
725 pModule->ne_flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
727 /* Get the segment table */
729 pModule->ne_segtab = pData - (BYTE *)pModule;
730 if (!(pSeg = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_segtab,
731 ne_header->ne_cseg * sizeof(struct ne_segment_table_entry_s) )))
732 goto failed;
733 for (i = ne_header->ne_cseg; i > 0; i--, pSeg++)
735 memcpy( pData, pSeg, sizeof(*pSeg) );
736 pData += sizeof(SEGTABLEENTRY);
739 /* Get the resource table */
741 if (ne_header->ne_rsrctab < ne_header->ne_restab)
743 pModule->ne_rsrctab = pData - (BYTE *)pModule;
744 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_rsrctab,
745 ne_header->ne_restab - ne_header->ne_rsrctab )) goto failed;
746 pData += ne_header->ne_restab - ne_header->ne_rsrctab;
748 else pModule->ne_rsrctab = 0; /* No resource table */
750 /* Get the resident names table */
752 pModule->ne_restab = pData - (BYTE *)pModule;
753 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_restab,
754 ne_header->ne_modtab - ne_header->ne_restab )) goto failed;
755 pData += ne_header->ne_modtab - ne_header->ne_restab;
757 /* Get the module references table */
759 if (ne_header->ne_cmod > 0)
761 pModule->ne_modtab = pData - (BYTE *)pModule;
762 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_modtab,
763 ne_header->ne_cmod * sizeof(WORD) )) goto failed;
764 pData += ne_header->ne_cmod * sizeof(WORD);
766 else pModule->ne_modtab = 0; /* No module references */
768 /* Get the imported names table */
770 pModule->ne_imptab = pData - (BYTE *)pModule;
771 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_imptab,
772 ne_header->ne_enttab - ne_header->ne_imptab )) goto failed;
773 pData += ne_header->ne_enttab - ne_header->ne_imptab;
775 /* Load entry table, convert it to the optimized version used by Windows */
777 pModule->ne_enttab = pData - (BYTE *)pModule;
778 if (!(ptr = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_enttab,
779 ne_header->ne_cbenttab ))) goto failed;
780 end = build_bundle_data( pModule, pData, ptr );
782 pData += ne_header->ne_cbenttab + sizeof(ET_BUNDLE) +
783 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6);
785 if (end > pData)
787 FIXME( "not enough space for entry table for %s\n", debugstr_a(path) );
788 goto failed;
791 /* Store the filename information */
793 pModule->fileinfo = pData - (BYTE *)pModule;
794 ofs = (OFSTRUCT *)pData;
795 ofs->cBytes = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path);
796 ofs->fFixedDisk = 1;
797 strcpy( ofs->szPathName, path );
798 pData += ofs->cBytes + 1;
799 assert( (BYTE *)pModule + size <= pData );
801 /* Get the non-resident names table */
803 if (ne_header->ne_cbnrestab)
805 pModule->nrname_handle = GlobalAlloc16( 0, ne_header->ne_cbnrestab );
806 if (!pModule->nrname_handle) goto failed;
807 FarSetOwner16( pModule->nrname_handle, hModule );
808 buffer = GlobalLock16( pModule->nrname_handle );
809 if (!NE_READ_DATA( pModule, buffer, ne_header->ne_nrestab, ne_header->ne_cbnrestab ))
811 GlobalFree16( pModule->nrname_handle );
812 goto failed;
815 else pModule->nrname_handle = 0;
817 /* Allocate a segment for the implicitly-loaded DLLs */
819 if (pModule->ne_cmod)
821 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
822 (pModule->ne_cmod+1)*sizeof(HMODULE16) );
823 if (!pModule->dlls_to_init)
825 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
826 goto failed;
828 FarSetOwner16( pModule->dlls_to_init, hModule );
830 else pModule->dlls_to_init = 0;
832 NE_RegisterModule( pModule );
833 return hModule;
835 failed:
836 GlobalFree16( hModule );
837 return ERROR_BAD_FORMAT;
841 /***********************************************************************
842 * NE_LoadDLLs
844 * Load all DLLs implicitly linked to a module.
846 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
848 int i;
849 WORD *pModRef = (WORD *)((char *)pModule + pModule->ne_modtab);
850 WORD *pDLLs = (WORD *)GlobalLock16( pModule->dlls_to_init );
852 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
854 char buffer[260], *p;
855 BYTE *pstr = (BYTE *)pModule + pModule->ne_imptab + *pModRef;
856 memcpy( buffer, pstr + 1, *pstr );
857 *(buffer + *pstr) = 0; /* terminate it */
859 TRACE("Loading '%s'\n", buffer );
860 if (!(*pModRef = GetModuleHandle16( buffer )))
862 /* If the DLL is not loaded yet, load it and store */
863 /* its handle in the list of DLLs to initialize. */
864 HMODULE16 hDLL;
866 /* Append .DLL to name if no extension present */
867 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
868 strcat( buffer, ".DLL" );
870 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
872 /* FIXME: cleanup what was done */
874 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
875 buffer, *((BYTE*)pModule + pModule->ne_restab),
876 (char *)pModule + pModule->ne_restab + 1, hDLL );
877 return FALSE;
879 *pModRef = GetExePtr( hDLL );
880 *pDLLs++ = *pModRef;
882 else /* Increment the reference count of the DLL */
884 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
885 if (pOldDLL) pOldDLL->count++;
888 return TRUE;
892 /**********************************************************************
893 * NE_DoLoadModule
895 * Load first instance of NE module from file.
897 * pModule must point to a module structure prepared by build_module_data.
898 * This routine must never be called twice on a module.
901 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
903 /* Allocate the segments for this module */
905 if (!NE_CreateAllSegments( pModule ))
906 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
908 /* Load the referenced DLLs */
910 if (!NE_LoadDLLs( pModule ))
911 return ERROR_FILE_NOT_FOUND; /* 2 */
913 /* Load the segments */
915 NE_LoadAllSegments( pModule );
917 /* Make sure the usage count is 1 on the first loading of */
918 /* the module, even if it contains circular DLL references */
920 pModule->count = 1;
922 return NE_GetInstance( pModule );
925 /**********************************************************************
926 * NE_LoadModule
928 * Load first instance of NE module. (Note: caller is responsible for
929 * ensuring the module isn't already loaded!)
931 * If the module turns out to be an executable module, only a
932 * handle to a module stub is returned; this needs to be initialized
933 * by calling NE_DoLoadModule later, in the context of the newly
934 * created process.
936 * If lib_only is TRUE, however, the module is perforce treated
937 * like a DLL module, even if it is an executable module.
940 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
942 NE_MODULE *pModule;
943 HMODULE16 hModule;
944 HINSTANCE16 hInstance;
945 HFILE16 hFile;
946 OFSTRUCT ofs;
947 HANDLE mapping;
948 void *ptr;
949 MEMORY_BASIC_INFORMATION info;
951 /* Open file */
952 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
953 return ERROR_FILE_NOT_FOUND;
955 mapping = CreateFileMappingW( DosFileHandleToWin32Handle(hFile), NULL, PAGE_WRITECOPY, 0, 0, NULL );
956 _lclose16( hFile );
957 if (!mapping) return ERROR_BAD_FORMAT;
959 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 0 );
960 CloseHandle( mapping );
961 if (!ptr) return ERROR_BAD_FORMAT;
963 VirtualQuery( ptr, &info, sizeof(info) );
964 hModule = build_module( ptr, info.RegionSize, ofs.szPathName );
966 if (hModule < 32)
968 UnmapViewOfFile( ptr );
969 return hModule;
972 SNOOP16_RegisterDLL( hModule, ofs.szPathName );
973 NE_InitResourceHandler( hModule );
975 pModule = NE_GetPtr( hModule );
977 if ( !lib_only && !( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) )
978 return hModule;
980 hInstance = NE_DoLoadModule( pModule );
981 if ( hInstance < 32 )
983 /* cleanup ... */
984 NE_FreeModule( hModule, 0 );
987 return hInstance;
991 /***********************************************************************
992 * NE_DoLoadBuiltinModule
994 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
996 static HMODULE16 NE_DoLoadBuiltinModule( const BUILTIN16_DESCRIPTOR *descr, const char *file_name )
998 NE_MODULE *pModule;
999 HMODULE16 hModule;
1000 SEGTABLEENTRY *pSegTable;
1001 const IMAGE_DOS_HEADER *mz_header;
1002 const IMAGE_OS2_HEADER *ne_header;
1003 SIZE_T mapping_size;
1005 mz_header = descr->module;
1006 ne_header = (const IMAGE_OS2_HEADER *)((const BYTE *)mz_header + mz_header->e_lfanew);
1007 mapping_size = ne_header->ne_psegrefbytes << ne_header->ne_align;
1008 hModule = build_module( descr->module, mapping_size, file_name );
1009 if (hModule < 32) return hModule;
1010 pModule = GlobalLock16( hModule );
1011 pModule->ne_flags |= NE_FFLAGS_BUILTIN;
1012 pModule->count = 1;
1013 /* NOTE: (Ab)use the rsrc32_map parameter for resource data pointer */
1014 pModule->rsrc32_map = (void *)descr->rsrc;
1016 /* Allocate the code segment */
1018 pSegTable = NE_SEG_TABLE( pModule );
1019 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
1020 pSegTable->minsize, hModule,
1021 WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT );
1022 if (!pSegTable->hSeg) return ERROR_NOT_ENOUGH_MEMORY;
1023 patch_code_segment( descr->code_start );
1024 pSegTable->flags |= NE_SEGFLAGS_ALLOCATED | NE_SEGFLAGS_LOADED;
1025 pSegTable++;
1027 /* Allocate the data segment */
1029 if (!NE_CreateSegment( pModule, 2 )) return ERROR_NOT_ENOUGH_MEMORY;
1030 pModule->dgroup_entry = (char *)pSegTable - (char *)pModule;
1031 memcpy( GlobalLock16( pSegTable->hSeg ),
1032 (const char *)descr->module + (pSegTable->filepos << pModule->ne_align),
1033 pSegTable->minsize);
1034 pSegTable->flags |= NE_SEGFLAGS_LOADED;
1036 if (pModule->ne_heap)
1038 unsigned int size = pSegTable->minsize + pModule->ne_heap;
1039 if (size > 0xfffe) size = 0xfffe;
1040 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg), pSegTable->minsize, size );
1043 NE_InitResourceHandler( hModule );
1044 return hModule;
1048 /**********************************************************************
1049 * MODULE_LoadModule16
1051 * Load a NE module in the order of the loadorder specification.
1052 * The caller is responsible that the module is not loaded already.
1055 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1057 HINSTANCE16 hinst = 2;
1058 HMODULE16 hModule;
1059 NE_MODULE *pModule;
1060 const BUILTIN16_DESCRIPTOR *descr = NULL;
1061 const char *file_name = NULL;
1062 char dllname[20], owner[20], *p;
1063 const char *basename;
1064 int owner_exists;
1066 /* strip path information */
1068 basename = libname;
1069 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1070 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
1071 if ((p = strrchr( basename, '/' ))) basename = p + 1;
1073 if (strlen(basename) < sizeof(dllname)-4)
1075 strcpy( dllname, basename );
1076 p = strrchr( dllname, '.' );
1077 if (!p) strcat( dllname, ".dll" );
1078 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
1080 if (wine_dll_get_owner( dllname, owner, sizeof(owner), &owner_exists ) != -1)
1082 HMODULE mod32 = LoadLibraryA( owner );
1083 if (mod32)
1085 if (!(descr = find_dll_descr( dllname, &file_name )))
1087 FreeLibrary( mod32 );
1088 owner_exists = 0;
1090 /* loading the 32-bit library can have the side effect of loading the module */
1091 /* if so, simply incr the ref count and return the module */
1092 if ((hModule = GetModuleHandle16( libname )))
1094 TRACE( "module %s already loaded by owner\n", libname );
1095 pModule = NE_GetPtr( hModule );
1096 if (pModule) pModule->count++;
1097 return hModule;
1100 else
1102 /* it's probably disabled by the load order config */
1103 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner, dllname );
1104 return ERROR_FILE_NOT_FOUND;
1109 if (descr)
1111 TRACE("Trying built-in '%s'\n", libname);
1112 hinst = NE_DoLoadBuiltinModule( descr, file_name );
1113 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : builtin\n", debugstr_a(file_name));
1115 else
1117 TRACE("Trying native dll '%s'\n", libname);
1118 hinst = NE_LoadModule(libname, lib_only);
1119 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : native\n", debugstr_a(libname));
1120 if (hinst == ERROR_FILE_NOT_FOUND && owner_exists) hinst = 21; /* win32 module */
1123 if (hinst > 32 && !implicit)
1125 hModule = GetModuleHandle16(libname);
1126 if(!hModule)
1128 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1129 libname, hinst);
1130 return ERROR_INVALID_HANDLE;
1133 pModule = NE_GetPtr(hModule);
1134 if(!pModule)
1136 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1137 libname, hinst);
1138 return ERROR_INVALID_HANDLE;
1141 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1144 * Call initialization routines for all loaded DLLs. Note that
1145 * when we load implicitly linked DLLs this will be done by InitTask().
1147 if(pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1149 NE_InitializeDLLs(hModule);
1150 NE_DllProcessAttach(hModule);
1153 return hinst; /* The last error that occurred */
1157 /**********************************************************************
1158 * NE_CreateThread
1160 * Create the thread for a 16-bit module.
1162 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1164 HANDLE hThread;
1165 TDB *pTask;
1166 HTASK16 hTask;
1167 HINSTANCE16 instance = 0;
1169 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1170 return 0;
1172 /* Post event to start the task */
1173 PostEvent16( hTask );
1175 /* Wait until we get the instance handle */
1178 DirectedYield16( hTask );
1179 if (!IsTask16( hTask )) /* thread has died */
1181 DWORD exit_code;
1182 WaitForSingleObject( hThread, INFINITE );
1183 GetExitCodeThread( hThread, &exit_code );
1184 CloseHandle( hThread );
1185 return exit_code;
1187 if (!(pTask = GlobalLock16( hTask ))) break;
1188 instance = pTask->hInstance;
1189 GlobalUnlock16( hTask );
1190 } while (!instance);
1192 CloseHandle( hThread );
1193 return instance;
1197 /**********************************************************************
1198 * LoadModule (KERNEL.45)
1200 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1202 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1203 LOADPARAMS16 *params;
1204 HMODULE16 hModule;
1205 NE_MODULE *pModule;
1206 LPSTR cmdline;
1207 WORD cmdShow;
1209 /* Load module */
1211 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1213 /* Special case: second instance of an already loaded NE module */
1215 if ( !( pModule = NE_GetPtr( hModule ) ) ) return ERROR_BAD_FORMAT;
1216 if ( pModule->module32 ) return (HINSTANCE16)21;
1218 /* Increment refcount */
1220 pModule->count++;
1222 else
1224 /* Main case: load first instance of NE module */
1226 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1227 return hModule;
1229 if ( !(pModule = NE_GetPtr( hModule )) )
1230 return ERROR_BAD_FORMAT;
1233 /* If library module, we just retrieve the instance handle */
1235 if ( ( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1236 return NE_GetInstance( pModule );
1239 * At this point, we need to create a new process.
1241 * pModule points either to an already loaded module, whose refcount
1242 * has already been incremented (to avoid having the module vanish
1243 * in the meantime), or else to a stub module which contains only header
1244 * information.
1246 params = (LOADPARAMS16 *)paramBlock;
1247 cmdShow = ((WORD *)MapSL(params->showCmd))[1];
1248 cmdline = MapSL( params->cmdLine );
1249 return NE_CreateThread( pModule, cmdShow, cmdline );
1253 /**********************************************************************
1254 * NE_StartTask
1256 * Startup code for a new 16-bit task.
1258 DWORD NE_StartTask(void)
1260 TDB *pTask = TASK_GetCurrent();
1261 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1262 HINSTANCE16 hInstance, hPrevInstance;
1263 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1264 WORD sp;
1266 if ( pModule->count > 0 )
1268 /* Second instance of an already loaded NE module */
1269 /* Note that the refcount was already incremented by the parent */
1271 hPrevInstance = NE_GetInstance( pModule );
1273 if ( pModule->ne_autodata )
1274 if ( NE_CreateSegment( pModule, pModule->ne_autodata ) )
1275 NE_LoadSegment( pModule, pModule->ne_autodata );
1277 hInstance = NE_GetInstance( pModule );
1278 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->ne_autodata, hPrevInstance);
1281 else
1283 /* Load first instance of NE module */
1285 pModule->ne_flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1287 hInstance = NE_DoLoadModule( pModule );
1288 hPrevInstance = 0;
1291 if ( hInstance >= 32 )
1293 CONTEXT86 context;
1295 /* Enter instance handles into task struct */
1297 pTask->hInstance = hInstance;
1298 pTask->hPrevInstance = hPrevInstance;
1300 /* Use DGROUP for 16-bit stack */
1302 if (!(sp = OFFSETOF(pModule->ne_sssp)))
1303 sp = pSegTable[SELECTOROF(pModule->ne_sssp)-1].minsize + pModule->ne_stack;
1304 sp &= ~1;
1305 sp -= sizeof(STACK16FRAME);
1306 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1308 /* Registers at initialization must be:
1309 * ax zero
1310 * bx stack size in bytes
1311 * cx heap size in bytes
1312 * si previous app instance
1313 * di current app instance
1314 * bp zero
1315 * es selector to the PSP
1316 * ds dgroup of the application
1317 * ss stack selector
1318 * sp top of the stack
1320 memset( &context, 0, sizeof(context) );
1321 context.SegCs = GlobalHandleToSel16(pSegTable[SELECTOROF(pModule->ne_csip) - 1].hSeg);
1322 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1323 context.SegEs = pTask->hPDB;
1324 context.SegFs = wine_get_fs();
1325 context.SegGs = wine_get_gs();
1326 context.Eip = OFFSETOF(pModule->ne_csip);
1327 context.Ebx = pModule->ne_stack;
1328 context.Ecx = pModule->ne_heap;
1329 context.Edi = pTask->hInstance;
1330 context.Esi = pTask->hPrevInstance;
1332 /* Now call 16-bit entry point */
1334 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1335 context.SegCs, context.Eip, context.SegDs,
1336 SELECTOROF(NtCurrentTeb()->WOW32Reserved),
1337 OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
1339 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1340 ExitThread( LOWORD(context.Eax) );
1342 return hInstance; /* error code */
1345 /***********************************************************************
1346 * LoadLibrary (KERNEL.95)
1347 * LoadLibrary16 (KERNEL32.35)
1349 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1351 return LoadModule16(libname, (LPVOID)-1 );
1355 /**********************************************************************
1356 * MODULE_CallWEP
1358 * Call a DLL's WEP, allowing it to shut down.
1359 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1361 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1363 BOOL16 ret;
1364 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1365 if (!WEP) return FALSE;
1367 __TRY
1369 WORD args[1];
1370 DWORD dwRet;
1372 args[0] = WEP_FREE_DLL;
1373 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1374 ret = LOWORD(dwRet);
1376 __EXCEPT(page_fault)
1378 WARN("Page fault\n");
1379 ret = 0;
1381 __ENDTRY
1383 return ret;
1387 /**********************************************************************
1388 * NE_FreeModule
1390 * Implementation of FreeModule16().
1392 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1394 HMODULE16 *hPrevModule;
1395 NE_MODULE *pModule;
1396 HMODULE16 *pModRef;
1397 int i;
1399 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1400 hModule = pModule->self;
1402 TRACE("%04x count %d\n", hModule, pModule->count );
1404 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1405 else pModule->count = 0;
1407 if (pModule->ne_flags & NE_FFLAGS_BUILTIN)
1408 return FALSE; /* Can't free built-in module */
1410 if (call_wep && !(pModule->ne_flags & NE_FFLAGS_WIN32))
1412 /* Free the objects owned by the DLL module */
1413 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1415 if (pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1416 MODULE_CallWEP( hModule );
1417 else
1418 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1422 /* Clear magic number just in case */
1424 pModule->ne_magic = pModule->self = 0;
1425 if (!(pModule->ne_flags & NE_FFLAGS_BUILTIN)) UnmapViewOfFile( (void *)pModule->mapping );
1427 /* Remove it from the linked list */
1429 hPrevModule = &hFirstModule;
1430 while (*hPrevModule && (*hPrevModule != hModule))
1432 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1434 if (*hPrevModule) *hPrevModule = pModule->next;
1436 /* Free the referenced modules */
1438 pModRef = (HMODULE16*)((char *)pModule + pModule->ne_modtab);
1439 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
1441 NE_FreeModule( *pModRef, call_wep );
1444 /* Free the module storage */
1446 GlobalFreeAll16( hModule );
1447 return TRUE;
1451 /**********************************************************************
1452 * FreeModule (KERNEL.46)
1454 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1456 return NE_FreeModule( hModule, TRUE );
1460 /***********************************************************************
1461 * FreeLibrary (KERNEL.96)
1462 * FreeLibrary16 (KERNEL32.36)
1464 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1466 TRACE("%04x\n", handle );
1467 FreeModule16( handle );
1471 /***********************************************************************
1472 * GetModuleHandle16 (KERNEL32.@)
1474 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1476 HMODULE16 hModule = hFirstModule;
1477 LPSTR s;
1478 BYTE len, *name_table;
1479 char tmpstr[MAX_PATH];
1480 NE_MODULE *pModule;
1482 TRACE("(%s)\n", name);
1484 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1486 len = strlen(name);
1487 if (!len) return 0;
1489 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1491 /* If 'name' matches exactly the module name of a module:
1492 * Return its handle.
1494 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1496 pModule = NE_GetPtr( hModule );
1497 if (!pModule) break;
1498 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1500 name_table = (BYTE *)pModule + pModule->ne_restab;
1501 if ((*name_table == len) && !strncmp(name, name_table+1, len))
1502 return hModule;
1505 /* If uppercased 'name' matches exactly the module name of a module:
1506 * Return its handle
1508 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1510 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1512 pModule = NE_GetPtr( hModule );
1513 if (!pModule) break;
1514 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1516 name_table = (BYTE *)pModule + pModule->ne_restab;
1517 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1518 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1519 * lowercased module names, but try to load uppercase DLLs, so this
1520 * 'i' compare is just a quickfix until the loader handles that
1521 * correctly. -MM 990705
1523 if ((*name_table == len) && !NE_strncasecmp(tmpstr, name_table+1, len))
1524 return hModule;
1527 /* If the base filename of 'name' matches the base filename of the module
1528 * filename of some module (case-insensitive compare):
1529 * Return its handle.
1532 /* basename: search backwards in passed name to \ / or : */
1533 s = tmpstr + strlen(tmpstr);
1534 while (s > tmpstr)
1536 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1537 break;
1538 s--;
1541 /* search this in loaded filename list */
1542 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1544 char *loadedfn;
1545 OFSTRUCT *ofs;
1547 pModule = NE_GetPtr( hModule );
1548 if (!pModule) break;
1549 if (!pModule->fileinfo) continue;
1550 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1552 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1553 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1554 /* basename: search backwards in pathname to \ / or : */
1555 while (loadedfn > (char*)ofs->szPathName)
1557 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1558 break;
1559 loadedfn--;
1561 /* case insensitive compare ... */
1562 if (!NE_strcasecmp(loadedfn, s))
1563 return hModule;
1565 return 0;
1569 /**********************************************************************
1570 * GetModuleName (KERNEL.27)
1572 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1574 NE_MODULE *pModule;
1575 BYTE *p;
1577 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1578 p = (BYTE *)pModule + pModule->ne_restab;
1579 if (count > *p) count = *p + 1;
1580 if (count > 0)
1582 memcpy( buf, p + 1, count - 1 );
1583 buf[count-1] = '\0';
1585 return TRUE;
1589 /**********************************************************************
1590 * GetModuleFileName (KERNEL.49)
1592 * Comment: see GetModuleFileNameA
1594 * Even if invoked by second instance of a program,
1595 * it still returns path of first one.
1597 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1598 INT16 nSize )
1600 NE_MODULE *pModule;
1602 /* Win95 does not query hModule if set to 0 !
1603 * Is this wrong or maybe Win3.1 only ? */
1604 if (!hModule) hModule = GetCurrentTask();
1606 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1607 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1608 if (pModule->ne_expver >= 0x400)
1609 GetLongPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1610 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1611 return strlen(lpFileName);
1615 /**********************************************************************
1616 * GetModuleUsage (KERNEL.48)
1618 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1620 NE_MODULE *pModule = NE_GetPtr( hModule );
1621 return pModule ? pModule->count : 0;
1625 /**********************************************************************
1626 * GetExpWinVer (KERNEL.167)
1628 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1630 NE_MODULE *pModule = NE_GetPtr( hModule );
1631 if ( !pModule ) return 0;
1634 * For built-in modules, fake the expected version the module should
1635 * have according to the Windows version emulated by Wine
1637 if ( !pModule->ne_expver )
1639 OSVERSIONINFOA versionInfo;
1640 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1642 if ( GetVersionExA( &versionInfo ) )
1643 pModule->ne_expver =
1644 (versionInfo.dwMajorVersion & 0xff) << 8
1645 | (versionInfo.dwMinorVersion & 0xff);
1648 return pModule->ne_expver;
1652 /***********************************************************************
1653 * WinExec (KERNEL.166)
1655 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1657 LPCSTR p, args = NULL;
1658 LPCSTR name_beg, name_end;
1659 LPSTR name, cmdline;
1660 int arglen;
1661 HINSTANCE16 ret;
1662 char buffer[MAX_PATH];
1664 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1666 name_beg = lpCmdLine+1;
1667 p = strchr ( lpCmdLine+1, '"' );
1668 if (p)
1670 name_end = p;
1671 args = strchr ( p, ' ' );
1673 else /* yes, even valid with trailing '"' missing */
1674 name_end = lpCmdLine+strlen(lpCmdLine);
1676 else
1678 name_beg = lpCmdLine;
1679 args = strchr( lpCmdLine, ' ' );
1680 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1683 if ((name_beg == lpCmdLine) && (!args))
1684 { /* just use the original cmdline string as file name */
1685 name = (LPSTR)lpCmdLine;
1687 else
1689 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1690 return ERROR_NOT_ENOUGH_MEMORY;
1691 memcpy( name, name_beg, name_end - name_beg );
1692 name[name_end - name_beg] = '\0';
1695 if (args)
1697 args++;
1698 arglen = strlen(args);
1699 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1700 cmdline[0] = (BYTE)arglen;
1701 strcpy( cmdline + 1, args );
1703 else
1705 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1706 cmdline[0] = cmdline[1] = 0;
1709 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1711 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1713 LOADPARAMS16 params;
1714 WORD showCmd[2];
1715 showCmd[0] = 2;
1716 showCmd[1] = nCmdShow;
1718 params.hEnvironment = 0;
1719 params.cmdLine = MapLS( cmdline );
1720 params.showCmd = MapLS( showCmd );
1721 params.reserved = 0;
1723 ret = LoadModule16( buffer, &params );
1724 UnMapLS( params.cmdLine );
1725 UnMapLS( params.showCmd );
1727 else ret = GetLastError();
1729 HeapFree( GetProcessHeap(), 0, cmdline );
1730 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1732 if (ret == 21 || ret == ERROR_BAD_FORMAT) /* 32-bit module or unknown executable*/
1734 DWORD count;
1735 ReleaseThunkLock( &count );
1736 ret = LOWORD( WinExec( lpCmdLine, nCmdShow ) );
1737 RestoreThunkLock( count );
1739 return ret;
1742 /***********************************************************************
1743 * GetProcAddress (KERNEL.50)
1745 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1747 WORD ordinal;
1748 FARPROC16 ret;
1750 if (!hModule) hModule = GetCurrentTask();
1751 hModule = GetExePtr( hModule );
1753 if (HIWORD(name) != 0)
1755 ordinal = NE_GetOrdinal( hModule, name );
1756 TRACE("%04x '%s'\n", hModule, name );
1758 else
1760 ordinal = LOWORD(name);
1761 TRACE("%04x %04x\n", hModule, ordinal );
1763 if (!ordinal) return (FARPROC16)0;
1765 ret = NE_GetEntryPoint( hModule, ordinal );
1767 TRACE("returning %08x\n", (UINT)ret );
1768 return ret;
1772 /***************************************************************************
1773 * HasGPHandler (KERNEL.338)
1775 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1777 HMODULE16 hModule;
1778 int gpOrdinal;
1779 SEGPTR gpPtr;
1780 GPHANDLERDEF *gpHandler;
1782 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1783 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1784 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1785 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1786 && (gpHandler = MapSL( gpPtr )) != NULL )
1788 while (gpHandler->selector)
1790 if ( SELECTOROF(address) == gpHandler->selector
1791 && OFFSETOF(address) >= gpHandler->rangeStart
1792 && OFFSETOF(address) < gpHandler->rangeEnd )
1793 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1794 gpHandler++;
1798 return 0;
1802 /**********************************************************************
1803 * GetModuleHandle (KERNEL.47)
1805 * Find a module from a module name.
1807 * NOTE: The current implementation works the same way the Windows 95 one
1808 * does. Do not try to 'fix' it, fix the callers.
1809 * + It does not do ANY extension handling (except that strange .EXE bit)!
1810 * + It does not care about paths, just about basenames. (same as Windows)
1812 * RETURNS
1813 * LOWORD:
1814 * the win16 module handle if found
1815 * 0 if not
1816 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1817 * Always hFirstModule
1819 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1821 if (HIWORD(name) == 0)
1822 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1823 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1826 /**********************************************************************
1827 * NE_GetModuleByFilename
1829 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1831 HMODULE16 hModule;
1832 LPSTR s, p;
1833 BYTE len, *name_table;
1834 char tmpstr[MAX_PATH];
1835 NE_MODULE *pModule;
1837 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1839 /* If the base filename of 'name' matches the base filename of the module
1840 * filename of some module (case-insensitive compare):
1841 * Return its handle.
1844 /* basename: search backwards in passed name to \ / or : */
1845 s = tmpstr + strlen(tmpstr);
1846 while (s > tmpstr)
1848 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1849 break;
1850 s--;
1853 /* search this in loaded filename list */
1854 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1856 char *loadedfn;
1857 OFSTRUCT *ofs;
1859 pModule = NE_GetPtr( hModule );
1860 if (!pModule) break;
1861 if (!pModule->fileinfo) continue;
1862 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1864 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1865 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1866 /* basename: search backwards in pathname to \ / or : */
1867 while (loadedfn > (char*)ofs->szPathName)
1869 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1870 break;
1871 loadedfn--;
1873 /* case insensitive compare ... */
1874 if (!NE_strcasecmp(loadedfn, s))
1875 return hModule;
1877 /* If basename (without ext) matches the module name of a module:
1878 * Return its handle.
1881 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1882 len = strlen(s);
1884 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1886 pModule = NE_GetPtr( hModule );
1887 if (!pModule) break;
1888 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1890 name_table = (BYTE *)pModule + pModule->ne_restab;
1891 if ((*name_table == len) && !NE_strncasecmp(s, name_table+1, len))
1892 return hModule;
1895 return 0;
1898 /***********************************************************************
1899 * GetProcAddress16 (KERNEL32.37)
1900 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1902 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1904 if (!hModule) return 0;
1905 if (HIWORD(hModule))
1907 WARN("hModule is Win32 handle (%p)\n", hModule );
1908 return 0;
1910 return GetProcAddress16( LOWORD(hModule), name );
1913 /**********************************************************************
1914 * ModuleFirst (TOOLHELP.59)
1916 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1918 lpme->wNext = hFirstModule;
1919 return ModuleNext16( lpme );
1923 /**********************************************************************
1924 * ModuleNext (TOOLHELP.60)
1926 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1928 NE_MODULE *pModule;
1929 char *name;
1931 if (!lpme->wNext) return FALSE;
1932 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1933 name = (char *)pModule + pModule->ne_restab;
1934 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1935 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1936 lpme->hModule = lpme->wNext;
1937 lpme->wcUsage = pModule->count;
1938 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1939 lpme->wNext = pModule->next;
1940 return TRUE;
1944 /**********************************************************************
1945 * ModuleFindName (TOOLHELP.61)
1947 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1949 lpme->wNext = GetModuleHandle16( name );
1950 return ModuleNext16( lpme );
1954 /**********************************************************************
1955 * ModuleFindHandle (TOOLHELP.62)
1957 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
1959 hModule = GetExePtr( hModule );
1960 lpme->wNext = hModule;
1961 return ModuleNext16( lpme );
1965 /***************************************************************************
1966 * IsRomModule (KERNEL.323)
1968 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1970 return FALSE;
1973 /***************************************************************************
1974 * IsRomFile (KERNEL.326)
1976 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1978 return FALSE;
1981 /***********************************************************************
1982 * create_dummy_module
1984 * Create a dummy NE module for Win32 or Winelib.
1986 static HMODULE16 create_dummy_module( HMODULE module32 )
1988 HMODULE16 hModule;
1989 NE_MODULE *pModule;
1990 SEGTABLEENTRY *pSegment;
1991 char *pStr,*s;
1992 unsigned int len;
1993 const char* basename;
1994 OFSTRUCT *ofs;
1995 int of_size, size;
1996 char filename[MAX_PATH];
1997 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
1999 if (!nt) return ERROR_BAD_FORMAT;
2001 /* Extract base filename */
2002 len = GetModuleFileNameA( module32, filename, sizeof(filename) );
2003 if (!len || len >= sizeof(filename)) return ERROR_BAD_FORMAT;
2004 basename = strrchr(filename, '\\');
2005 if (!basename) basename = filename;
2006 else basename++;
2007 len = strlen(basename);
2008 if ((s = strchr(basename, '.'))) len = s - basename;
2010 /* Allocate module */
2011 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
2012 + strlen(filename) + 1;
2013 size = sizeof(NE_MODULE) +
2014 /* loaded file info */
2015 ((of_size + 3) & ~3) +
2016 /* segment table: DS,CS */
2017 2 * sizeof(SEGTABLEENTRY) +
2018 /* name table */
2019 len + 2 +
2020 /* several empty tables */
2023 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2024 if (!hModule) return ERROR_BAD_FORMAT;
2026 FarSetOwner16( hModule, hModule );
2027 pModule = (NE_MODULE *)GlobalLock16( hModule );
2029 /* Set all used entries */
2030 pModule->ne_magic = IMAGE_OS2_SIGNATURE;
2031 pModule->count = 1;
2032 pModule->next = 0;
2033 pModule->ne_flags = NE_FFLAGS_WIN32;
2034 pModule->ne_autodata = 0;
2035 pModule->ne_sssp = MAKESEGPTR( 0, 1 );
2036 pModule->ne_csip = MAKESEGPTR( 0, 2 );
2037 pModule->ne_heap = 0;
2038 pModule->ne_stack = 0;
2039 pModule->ne_cseg = 2;
2040 pModule->ne_cmod = 0;
2041 pModule->ne_cbnrestab = 0;
2042 pModule->fileinfo = sizeof(NE_MODULE);
2043 pModule->ne_exetyp = NE_OSFLAGS_WINDOWS;
2044 pModule->self = hModule;
2045 pModule->module32 = module32;
2047 /* Set version and flags */
2048 pModule->ne_expver = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2049 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2050 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2051 pModule->ne_flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2053 /* Set loaded file information */
2054 ofs = (OFSTRUCT *)(pModule + 1);
2055 memset( ofs, 0, of_size );
2056 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2057 strcpy( ofs->szPathName, filename );
2059 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2060 pModule->ne_segtab = (char *)pSegment - (char *)pModule;
2061 /* Data segment */
2062 pSegment->size = 0;
2063 pSegment->flags = NE_SEGFLAGS_DATA;
2064 pSegment->minsize = 0x1000;
2065 pSegment++;
2066 /* Code segment */
2067 pSegment->flags = 0;
2068 pSegment++;
2070 /* Module name */
2071 pStr = (char *)pSegment;
2072 pModule->ne_restab = pStr - (char *)pModule;
2073 assert(len<256);
2074 *pStr = len;
2075 lstrcpynA( pStr+1, basename, len+1 );
2076 pStr += len+2;
2078 /* All tables zero terminated */
2079 pModule->ne_rsrctab = pModule->ne_imptab = pModule->ne_enttab = (char *)pStr - (char *)pModule;
2081 NE_RegisterModule( pModule );
2082 LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2083 return hModule;
2086 /***********************************************************************
2087 * PrivateLoadLibrary (KERNEL32.@)
2089 * FIXME: rough guesswork, don't know what "Private" means
2091 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2093 return LoadLibrary16(libname);
2096 /***********************************************************************
2097 * PrivateFreeLibrary (KERNEL32.@)
2099 * FIXME: rough guesswork, don't know what "Private" means
2101 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2103 FreeLibrary16(handle);
2106 /***********************************************************************
2107 * LoadLibrary32 (KERNEL.452)
2108 * LoadSystemLibrary32 (KERNEL.482)
2110 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2112 HMODULE hModule;
2113 DWORD count;
2115 ReleaseThunkLock( &count );
2116 hModule = LoadLibraryA( libname );
2117 RestoreThunkLock( count );
2118 return hModule;
2121 /***************************************************************************
2122 * MapHModuleLS (KERNEL32.@)
2124 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2126 HMODULE16 ret;
2127 NE_MODULE *pModule;
2129 if (!hmod)
2130 return TASK_GetCurrent()->hInstance;
2131 if (!HIWORD(hmod))
2132 return LOWORD(hmod); /* we already have a 16 bit module handle */
2133 pModule = (NE_MODULE*)GlobalLock16(hFirstModule);
2134 while (pModule) {
2135 if (pModule->module32 == hmod)
2136 return pModule->self;
2137 pModule = (NE_MODULE*)GlobalLock16(pModule->next);
2139 if ((ret = create_dummy_module( hmod )) < 32)
2141 SetLastError(ret);
2142 ret = 0;
2144 return ret;
2147 /***************************************************************************
2148 * MapHModuleSL (KERNEL32.@)
2150 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2152 NE_MODULE *pModule;
2154 if (!hmod) {
2155 TDB *pTask = TASK_GetCurrent();
2156 hmod = pTask->hModule;
2158 pModule = (NE_MODULE*)GlobalLock16(hmod);
2159 if ((pModule->ne_magic != IMAGE_OS2_SIGNATURE) || !(pModule->ne_flags & NE_FFLAGS_WIN32))
2160 return 0;
2161 return pModule->module32;
2164 /***************************************************************************
2165 * MapHInstLS (KERNEL32.@)
2166 * MapHInstLS (KERNEL.472)
2168 void WINAPI __regs_MapHInstLS( CONTEXT86 *context )
2170 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2172 #ifdef DEFINE_REGS_ENTRYPOINT
2173 DEFINE_REGS_ENTRYPOINT( MapHInstLS, 0, 0 );
2174 #endif
2176 /***************************************************************************
2177 * MapHInstSL (KERNEL32.@)
2178 * MapHInstSL (KERNEL.473)
2180 void WINAPI __regs_MapHInstSL( CONTEXT86 *context )
2182 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2184 #ifdef DEFINE_REGS_ENTRYPOINT
2185 DEFINE_REGS_ENTRYPOINT( MapHInstSL, 0, 0 );
2186 #endif
2188 /***************************************************************************
2189 * MapHInstLS_PN (KERNEL32.@)
2191 void WINAPI __regs_MapHInstLS_PN( CONTEXT86 *context )
2193 if (context->Eax) context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2195 #ifdef DEFINE_REGS_ENTRYPOINT
2196 DEFINE_REGS_ENTRYPOINT( MapHInstLS_PN, 0, 0 );
2197 #endif
2199 /***************************************************************************
2200 * MapHInstSL_PN (KERNEL32.@)
2202 void WINAPI __regs_MapHInstSL_PN( CONTEXT86 *context )
2204 if (context->Eax) context->Eax = (DWORD)MapHModuleSL( context->Eax );
2206 #ifdef DEFINE_REGS_ENTRYPOINT
2207 DEFINE_REGS_ENTRYPOINT( MapHInstSL_PN, 0, 0 );
2208 #endif