push c89211be2889db25484c69db361f8abc539b86e2
[wine/hacks.git] / dlls / kernel32 / ne_module.c
blob87a1a86737b5410444eee00029f9809a41b571f7
1 /*
2 * NE modules
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <ctype.h>
35 #include "windef.h"
36 #include "wine/winbase16.h"
37 #include "wownt32.h"
38 #include "winternl.h"
39 #include "toolhelp.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);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
49 #include "pshpack1.h"
50 typedef struct _GPHANDLERDEF
52 WORD selector;
53 WORD rangeStart;
54 WORD rangeEnd;
55 WORD handler;
56 } GPHANDLERDEF;
57 #include "poppack.h"
60 * Segment table entry
62 struct ne_segment_table_entry_s
64 WORD seg_data_offset; /* Sector offset of segment data */
65 WORD seg_data_length; /* Length of segment data */
66 WORD seg_flags; /* Flags associated with this segment */
67 WORD min_alloc; /* Minimum allocation size for this */
70 #define hFirstModule (pThhook->hExeHead)
72 struct builtin_dll
74 const IMAGE_DOS_HEADER *header; /* module headers */
75 const char *file_name; /* module file name */
78 /* Table of all built-in DLLs */
80 #define MAX_DLLS 50
82 static struct builtin_dll builtin_dlls[MAX_DLLS];
84 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
85 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
87 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
89 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
92 /* patch all the flat cs references of the code segment if necessary */
93 static inline void patch_code_segment( NE_MODULE *pModule )
95 #ifdef __i386__
96 int i;
97 CALLFROM16 *call;
98 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
100 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
101 if (!(pSeg->flags & NE_SEGFLAGS_DATA)) break; /* found the code segment */
103 call = GlobalLock16( pSeg->hSeg );
105 /* patch glue code address and code selector */
106 for (i = 0; call[i].pushl == 0x68; i++)
108 if (call[i].ret[0] == 0xca66 || call[i].ret[0] == 0xcb66) /* register entry point? */
109 call[i].glue = __wine_call_from_16_regs;
110 else
111 call[i].glue = __wine_call_from_16;
112 call[i].flatcs = wine_get_cs();
115 if (TRACE_ON(relay)) /* patch relay functions to all point to relay_call_from_16 */
116 for (i = 0; call[i].pushl == 0x68; i++) call[i].relay = relay_call_from_16;
117 #endif
121 /***********************************************************************
122 * contains_path
124 static inline int contains_path( LPCSTR name )
126 return ((*name && (name[1] == ':')) || strchr(name, '/') || strchr(name, '\\'));
130 /***********************************************************************
131 * NE_strcasecmp
133 * locale-independent case conversion for module lookups
135 static int NE_strcasecmp( const char *str1, const char *str2 )
137 int ret = 0;
138 for ( ; ; str1++, str2++)
139 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
140 return ret;
144 /***********************************************************************
145 * NE_strncasecmp
147 * locale-independent case conversion for module lookups
149 static int NE_strncasecmp( const char *str1, const char *str2, int len )
151 int ret = 0;
152 for ( ; len > 0; len--, str1++, str2++)
153 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
154 return ret;
158 /***********************************************************************
159 * find_dll_descr
161 * Find a descriptor in the list
163 static const IMAGE_DOS_HEADER *find_dll_descr( const char *dllname, const char **file_name )
165 int i;
166 const IMAGE_DOS_HEADER *mz_header;
167 const IMAGE_OS2_HEADER *ne_header;
168 const BYTE *name_table;
170 for (i = 0; i < MAX_DLLS; i++)
172 mz_header = builtin_dlls[i].header;
173 if (mz_header)
175 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mz_header + mz_header->e_lfanew);
176 name_table = (const BYTE *)ne_header + ne_header->ne_restab;
178 /* check the dll file name */
179 if (!NE_strcasecmp( builtin_dlls[i].file_name, dllname ) ||
180 /* check the dll module name (without extension) */
181 (!NE_strncasecmp( dllname, (const char*)name_table+1, *name_table ) &&
182 !strcmp( dllname + *name_table, ".dll" )))
184 *file_name = builtin_dlls[i].file_name;
185 return builtin_dlls[i].header;
189 return NULL;
193 /***********************************************************************
194 * __wine_dll_register_16 (KERNEL32.@)
196 * Register a built-in DLL descriptor.
198 void __wine_dll_register_16( const IMAGE_DOS_HEADER *header, const char *file_name )
200 int i;
202 for (i = 0; i < MAX_DLLS; i++)
204 if (builtin_dlls[i].header) continue;
205 builtin_dlls[i].header = header;
206 builtin_dlls[i].file_name = file_name;
207 break;
209 assert( i < MAX_DLLS );
213 /***********************************************************************
214 * __wine_dll_unregister_16 (KERNEL32.@)
216 * Unregister a built-in DLL descriptor.
218 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER *header )
220 int i;
222 for (i = 0; i < MAX_DLLS; i++)
224 if (builtin_dlls[i].header != header) continue;
225 builtin_dlls[i].header = NULL;
226 break;
231 /***********************************************************************
232 * NE_GetPtr
234 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
236 return GlobalLock16( GetExePtr(hModule) );
240 /**********************************************************************
241 * NE_RegisterModule
243 static void NE_RegisterModule( NE_MODULE *pModule )
245 pModule->next = hFirstModule;
246 hFirstModule = pModule->self;
250 /***********************************************************************
251 * NE_DumpModule
253 void NE_DumpModule( HMODULE16 hModule )
255 int i, ordinal;
256 SEGTABLEENTRY *pSeg;
257 BYTE *pstr;
258 WORD *pword;
259 NE_MODULE *pModule;
260 ET_BUNDLE *bundle;
261 ET_ENTRY *entry;
263 if (!(pModule = NE_GetPtr( hModule )))
265 ERR( "**** %04x is not a module handle\n", hModule );
266 return;
269 /* Dump the module info */
270 TRACE( "---\n" );
271 TRACE( "Module %04x:\n", hModule );
272 TRACE( "count=%d flags=%04x heap=%d stack=%d\n",
273 pModule->count, pModule->ne_flags,
274 pModule->ne_heap, pModule->ne_stack );
275 TRACE( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
276 SELECTOROF(pModule->ne_csip), OFFSETOF(pModule->ne_csip),
277 SELECTOROF(pModule->ne_sssp), OFFSETOF(pModule->ne_sssp),
278 pModule->ne_autodata, pModule->ne_cseg, pModule->ne_cmod );
279 TRACE( "os_flags=%d swap_area=%d version=%04x\n",
280 pModule->ne_exetyp, pModule->ne_swaparea, pModule->ne_expver );
281 if (pModule->ne_flags & NE_FFLAGS_WIN32)
282 TRACE( "PE module=%p\n", pModule->module32 );
284 /* Dump the file info */
285 TRACE( "---\n" );
286 TRACE( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
288 /* Dump the segment table */
289 TRACE( "---\n" );
290 TRACE( "Segment table:\n" );
291 pSeg = NE_SEG_TABLE( pModule );
292 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
293 TRACE( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
294 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
295 pSeg->minsize, pSeg->hSeg );
297 /* Dump the resource table */
298 TRACE( "---\n" );
299 TRACE( "Resource table:\n" );
300 if (pModule->ne_rsrctab)
302 pword = (WORD *)((BYTE *)pModule + pModule->ne_rsrctab);
303 TRACE( "Alignment: %d\n", *pword++ );
304 while (*pword)
306 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
307 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
308 TRACE( "id=%04x count=%d\n", ptr->type_id, ptr->count );
309 for (i = 0; i < ptr->count; i++, pname++)
310 TRACE( "offset=%d len=%d id=%04x\n",
311 pname->offset, pname->length, pname->id );
312 pword = (WORD *)pname;
315 else TRACE( "None\n" );
317 /* Dump the resident name table */
318 TRACE( "---\n" );
319 TRACE( "Resident-name table:\n" );
320 pstr = (BYTE*) pModule + pModule->ne_restab;
321 while (*pstr)
323 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
324 *(WORD *)(pstr + *pstr + 1) );
325 pstr += *pstr + 1 + sizeof(WORD);
328 /* Dump the module reference table */
329 TRACE( "---\n" );
330 TRACE( "Module ref table:\n" );
331 if (pModule->ne_modtab)
333 pword = (WORD *)((BYTE *)pModule + pModule->ne_modtab);
334 for (i = 0; i < pModule->ne_cmod; i++, pword++)
336 char name[10];
337 GetModuleName16( *pword, name, sizeof(name) );
338 TRACE( "%d: %04x -> '%s'\n", i, *pword, name );
341 else TRACE( "None\n" );
343 /* Dump the entry table */
344 TRACE( "---\n" );
345 TRACE( "Entry table:\n" );
346 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->ne_enttab);
347 do {
348 entry = (ET_ENTRY *)((BYTE *)bundle+6);
349 TRACE( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
350 ordinal = bundle->first;
351 while (ordinal < bundle->last)
353 if (entry->type == 0xff)
354 TRACE("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
355 else
356 TRACE("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
357 entry++;
359 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
361 /* Dump the non-resident names table */
362 TRACE( "---\n" );
363 TRACE( "Non-resident names table:\n" );
364 if (pModule->nrname_handle)
366 pstr = GlobalLock16( pModule->nrname_handle );
367 while (*pstr)
369 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
370 *(WORD *)(pstr + *pstr + 1) );
371 pstr += *pstr + 1 + sizeof(WORD);
374 TRACE( "\n" );
378 /***********************************************************************
379 * NE_WalkModules
381 * Walk the module list and print the modules.
383 void NE_WalkModules(void)
385 HMODULE16 hModule = hFirstModule;
386 MESSAGE( "Module Flags Name\n" );
387 while (hModule)
389 NE_MODULE *pModule = NE_GetPtr( hModule );
390 if (!pModule)
392 MESSAGE( "Bad module %04x in list\n", hModule );
393 return;
395 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->ne_flags,
396 *((char *)pModule + pModule->ne_restab),
397 (char *)pModule + pModule->ne_restab + 1 );
398 hModule = pModule->next;
403 /***********************************************************************
404 * NE_InitResourceHandler
406 * Fill in 'resloader' fields in the resource table.
408 static void NE_InitResourceHandler( HMODULE16 hModule )
410 static FARPROC16 proc;
412 NE_TYPEINFO *pTypeInfo;
413 NE_MODULE *pModule;
415 if (!(pModule = NE_GetPtr( hModule )) || !pModule->ne_rsrctab) return;
417 TRACE("InitResourceHandler[%04x]\n", hModule );
419 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
421 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
422 while(pTypeInfo->type_id)
424 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
425 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
430 /***********************************************************************
431 * NE_GetOrdinal
433 * Lookup the ordinal for a given name.
435 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
437 char buffer[256], *p;
438 BYTE *cpnt;
439 BYTE len;
440 NE_MODULE *pModule;
442 if (!(pModule = NE_GetPtr( hModule ))) return 0;
443 if (pModule->ne_flags & NE_FFLAGS_WIN32) return 0;
445 TRACE("(%04x,'%s')\n", hModule, name );
447 /* First handle names of the form '#xxxx' */
449 if (name[0] == '#') return atoi( name + 1 );
451 /* Now copy and uppercase the string */
453 strcpy( buffer, name );
454 for (p = buffer; *p; p++) *p = RtlUpperChar(*p);
455 len = p - buffer;
457 /* First search the resident names */
459 cpnt = (BYTE *)pModule + pModule->ne_restab;
461 /* Skip the first entry (module name) */
462 cpnt += *cpnt + 1 + sizeof(WORD);
463 while (*cpnt)
465 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
467 WORD ordinal;
468 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
469 TRACE(" Found: ordinal=%d\n", ordinal );
470 return ordinal;
472 cpnt += *cpnt + 1 + sizeof(WORD);
475 /* Now search the non-resident names table */
477 if (!pModule->nrname_handle) return 0; /* No non-resident table */
478 cpnt = GlobalLock16( pModule->nrname_handle );
480 /* Skip the first entry (module description string) */
481 cpnt += *cpnt + 1 + sizeof(WORD);
482 while (*cpnt)
484 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
486 WORD ordinal;
487 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
488 TRACE(" Found: ordinal=%d\n", ordinal );
489 return ordinal;
491 cpnt += *cpnt + 1 + sizeof(WORD);
493 return 0;
497 /***********************************************************************
498 * NE_GetEntryPoint
500 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
502 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
505 /***********************************************************************
506 * NE_GetEntryPointEx
508 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
510 NE_MODULE *pModule;
511 WORD sel, offset, i;
513 ET_ENTRY *entry;
514 ET_BUNDLE *bundle;
516 if (!(pModule = NE_GetPtr( hModule ))) return 0;
517 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
519 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
520 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
522 if (!(bundle->next))
523 return 0;
524 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
527 entry = (ET_ENTRY *)((BYTE *)bundle+6);
528 for (i=0; i < (ordinal - bundle->first - 1); i++)
529 entry++;
531 sel = entry->segnum;
532 memcpy( &offset, &entry->offs, sizeof(WORD) );
534 if (sel == 0xfe) sel = 0xffff; /* constant entry */
535 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
536 if (sel==0xffff)
537 return (FARPROC16)MAKESEGPTR( sel, offset );
538 if (!snoop)
539 return (FARPROC16)MAKESEGPTR( sel, offset );
540 else
541 return SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
545 /***********************************************************************
546 * EntryAddrProc (KERNEL.667) Wine-specific export
548 * Return the entry point for a given ordinal.
550 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
552 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
553 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
554 return ret;
557 /***********************************************************************
558 * NE_SetEntryPoint
560 * Change the value of an entry point. Use with caution!
561 * It can only change the offset value, not the selector.
563 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
565 NE_MODULE *pModule;
566 ET_ENTRY *entry;
567 ET_BUNDLE *bundle;
568 int i;
570 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
571 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
573 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
574 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
576 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
577 if (!(bundle->next)) return 0;
580 entry = (ET_ENTRY *)((BYTE *)bundle+6);
581 for (i=0; i < (ordinal - bundle->first - 1); i++)
582 entry++;
584 memcpy( &entry->offs, &offset, sizeof(WORD) );
585 return TRUE;
589 /***********************************************************************
590 * build_bundle_data
592 * Build the entry table bundle data from the on-disk format. Helper for build_module.
594 static void *build_bundle_data( NE_MODULE *pModule, void *dest, const BYTE *table )
596 ET_BUNDLE *oldbundle, *bundle = dest;
597 ET_ENTRY *entry;
598 BYTE nr_entries, type;
600 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
601 entry = (ET_ENTRY *)((BYTE *)bundle+6);
603 while ((nr_entries = *table++))
605 if ((type = *table++))
607 bundle->last += nr_entries;
608 if (type == 0xff)
610 while (nr_entries--)
612 entry->type = type;
613 entry->flags = *table++;
614 table += sizeof(WORD);
615 entry->segnum = *table++;
616 entry->offs = *(const WORD *)table;
617 table += sizeof(WORD);
618 entry++;
621 else
623 while (nr_entries--)
625 entry->type = type;
626 entry->flags = *table++;
627 entry->segnum = type;
628 entry->offs = *(const WORD *)table;
629 table += sizeof(WORD);
630 entry++;
634 else
636 if (bundle->first == bundle->last)
638 bundle->first += nr_entries;
639 bundle->last += nr_entries;
641 else
643 oldbundle = bundle;
644 oldbundle->next = (char *)entry - (char *)pModule;
645 bundle = (ET_BUNDLE *)entry;
646 bundle->first = bundle->last = oldbundle->last + nr_entries;
647 bundle->next = 0;
648 entry = (ET_ENTRY*)(((BYTE*)entry)+sizeof(ET_BUNDLE));
652 return entry;
656 /***********************************************************************
657 * build_module
659 * Build the in-memory module from the on-disk data.
661 static HMODULE16 build_module( const void *mapping, SIZE_T mapping_size, LPCSTR path )
663 const IMAGE_DOS_HEADER *mz_header = mapping;
664 const IMAGE_OS2_HEADER *ne_header;
665 const struct ne_segment_table_entry_s *pSeg;
666 const void *ptr;
667 int i;
668 size_t size;
669 HMODULE16 hModule;
670 NE_MODULE *pModule;
671 BYTE *buffer, *pData, *end;
672 OFSTRUCT *ofs;
674 if (mapping_size < sizeof(*mz_header)) return ERROR_BAD_FORMAT;
675 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE) return ERROR_BAD_FORMAT;
676 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mapping + mz_header->e_lfanew);
677 if (mz_header->e_lfanew + sizeof(*ne_header) > mapping_size) return ERROR_BAD_FORMAT;
678 if (ne_header->ne_magic == IMAGE_NT_SIGNATURE) return 21; /* win32 exe */
679 if (ne_header->ne_magic == IMAGE_OS2_SIGNATURE_LX)
681 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path);
682 return 12;
684 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE) return ERROR_BAD_FORMAT;
686 /* We now have a valid NE header */
688 /* check to be able to fall back to loading OS/2 programs as DOS
689 * FIXME: should this check be reversed in order to be less strict?
690 * (only fail for OS/2 ne_exetyp 0x01 here?) */
691 if ((ne_header->ne_exetyp != 0x02 /* Windows */)
692 && (ne_header->ne_exetyp != 0x04) /* Windows 386 */)
693 return ERROR_BAD_FORMAT;
695 size = sizeof(NE_MODULE) +
696 /* segment table */
697 ne_header->ne_cseg * sizeof(SEGTABLEENTRY) +
698 /* resource table */
699 ne_header->ne_restab - ne_header->ne_rsrctab +
700 /* resident names table */
701 ne_header->ne_modtab - ne_header->ne_restab +
702 /* module ref table */
703 ne_header->ne_cmod * sizeof(WORD) +
704 /* imported names table */
705 ne_header->ne_enttab - ne_header->ne_imptab +
706 /* entry table length */
707 ne_header->ne_cbenttab +
708 /* entry table extra conversion space */
709 sizeof(ET_BUNDLE) +
710 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6) +
711 /* loaded file info */
712 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
714 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
715 if (!hModule) return ERROR_BAD_FORMAT;
717 FarSetOwner16( hModule, hModule );
718 pModule = GlobalLock16( hModule );
719 memcpy( pModule, ne_header, sizeof(*ne_header) );
720 pModule->count = 0;
721 /* check programs for default minimal stack size */
722 if (!(pModule->ne_flags & NE_FFLAGS_LIBMODULE) && (pModule->ne_stack < 0x1400))
723 pModule->ne_stack = 0x1400;
725 pModule->self = hModule;
726 pModule->mapping = mapping;
727 pModule->mapping_size = mapping_size;
729 pData = (BYTE *)(pModule + 1);
731 /* Clear internal Wine flags in case they are set in the EXE file */
733 pModule->ne_flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
735 /* Get the segment table */
737 pModule->ne_segtab = pData - (BYTE *)pModule;
738 if (!(pSeg = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_segtab,
739 ne_header->ne_cseg * sizeof(struct ne_segment_table_entry_s) )))
740 goto failed;
741 for (i = ne_header->ne_cseg; i > 0; i--, pSeg++)
743 memcpy( pData, pSeg, sizeof(*pSeg) );
744 pData += sizeof(SEGTABLEENTRY);
747 /* Get the resource table */
749 if (ne_header->ne_rsrctab < ne_header->ne_restab)
751 pModule->ne_rsrctab = pData - (BYTE *)pModule;
752 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_rsrctab,
753 ne_header->ne_restab - ne_header->ne_rsrctab )) goto failed;
754 pData += ne_header->ne_restab - ne_header->ne_rsrctab;
756 else pModule->ne_rsrctab = 0; /* No resource table */
758 /* Get the resident names table */
760 pModule->ne_restab = pData - (BYTE *)pModule;
761 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_restab,
762 ne_header->ne_modtab - ne_header->ne_restab )) goto failed;
763 pData += ne_header->ne_modtab - ne_header->ne_restab;
765 /* Get the module references table */
767 if (ne_header->ne_cmod > 0)
769 pModule->ne_modtab = pData - (BYTE *)pModule;
770 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_modtab,
771 ne_header->ne_cmod * sizeof(WORD) )) goto failed;
772 pData += ne_header->ne_cmod * sizeof(WORD);
774 else pModule->ne_modtab = 0; /* No module references */
776 /* Get the imported names table */
778 pModule->ne_imptab = pData - (BYTE *)pModule;
779 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_imptab,
780 ne_header->ne_enttab - ne_header->ne_imptab )) goto failed;
781 pData += ne_header->ne_enttab - ne_header->ne_imptab;
783 /* Load entry table, convert it to the optimized version used by Windows */
785 pModule->ne_enttab = pData - (BYTE *)pModule;
786 if (!(ptr = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_enttab,
787 ne_header->ne_cbenttab ))) goto failed;
788 end = build_bundle_data( pModule, pData, ptr );
790 pData += ne_header->ne_cbenttab + sizeof(ET_BUNDLE) +
791 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6);
793 if (end > pData)
795 FIXME( "not enough space for entry table for %s\n", debugstr_a(path) );
796 goto failed;
799 /* Store the filename information */
801 pModule->fileinfo = pData - (BYTE *)pModule;
802 ofs = (OFSTRUCT *)pData;
803 ofs->cBytes = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path);
804 ofs->fFixedDisk = 1;
805 strcpy( ofs->szPathName, path );
806 pData += ofs->cBytes + 1;
807 assert( (BYTE *)pModule + size <= pData );
809 /* Get the non-resident names table */
811 if (ne_header->ne_cbnrestab)
813 pModule->nrname_handle = GlobalAlloc16( 0, ne_header->ne_cbnrestab );
814 if (!pModule->nrname_handle) goto failed;
815 FarSetOwner16( pModule->nrname_handle, hModule );
816 buffer = GlobalLock16( pModule->nrname_handle );
817 if (!NE_READ_DATA( pModule, buffer, ne_header->ne_nrestab, ne_header->ne_cbnrestab ))
819 GlobalFree16( pModule->nrname_handle );
820 goto failed;
823 else pModule->nrname_handle = 0;
825 /* Allocate a segment for the implicitly-loaded DLLs */
827 if (pModule->ne_cmod)
829 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
830 (pModule->ne_cmod+1)*sizeof(HMODULE16) );
831 if (!pModule->dlls_to_init)
833 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
834 goto failed;
836 FarSetOwner16( pModule->dlls_to_init, hModule );
838 else pModule->dlls_to_init = 0;
840 NE_RegisterModule( pModule );
841 return hModule;
843 failed:
844 GlobalFree16( hModule );
845 return ERROR_BAD_FORMAT;
849 /***********************************************************************
850 * NE_LoadDLLs
852 * Load all DLLs implicitly linked to a module.
854 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
856 int i;
857 WORD *pModRef = (WORD *)((char *)pModule + pModule->ne_modtab);
858 WORD *pDLLs = GlobalLock16( pModule->dlls_to_init );
860 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
862 char buffer[260], *p;
863 BYTE *pstr = (BYTE *)pModule + pModule->ne_imptab + *pModRef;
864 memcpy( buffer, pstr + 1, *pstr );
865 *(buffer + *pstr) = 0; /* terminate it */
867 TRACE("Loading '%s'\n", buffer );
868 if (!(*pModRef = GetModuleHandle16( buffer )))
870 /* If the DLL is not loaded yet, load it and store */
871 /* its handle in the list of DLLs to initialize. */
872 HMODULE16 hDLL;
874 /* Append .DLL to name if no extension present */
875 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
876 strcat( buffer, ".DLL" );
878 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
880 /* FIXME: cleanup what was done */
882 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
883 buffer, *((BYTE*)pModule + pModule->ne_restab),
884 (char *)pModule + pModule->ne_restab + 1, hDLL );
885 return FALSE;
887 *pModRef = GetExePtr( hDLL );
888 *pDLLs++ = *pModRef;
890 else /* Increment the reference count of the DLL */
892 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
893 if (pOldDLL) pOldDLL->count++;
896 return TRUE;
900 /**********************************************************************
901 * NE_DoLoadModule
903 * Load first instance of NE module from file.
905 * pModule must point to a module structure prepared by build_module.
906 * This routine must never be called twice on a module.
908 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
910 /* Allocate the segments for this module */
912 if (!NE_CreateAllSegments( pModule ))
913 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
915 /* Load the referenced DLLs */
917 if (!NE_LoadDLLs( pModule ))
918 return ERROR_FILE_NOT_FOUND; /* 2 */
920 /* Load the segments */
922 NE_LoadAllSegments( pModule );
924 /* Make sure the usage count is 1 on the first loading of */
925 /* the module, even if it contains circular DLL references */
927 pModule->count = 1;
929 return NE_GetInstance( pModule );
932 /**********************************************************************
933 * NE_LoadModule
935 * Load first instance of NE module. (Note: caller is responsible for
936 * ensuring the module isn't already loaded!)
938 * If the module turns out to be an executable module, only a
939 * handle to a module stub is returned; this needs to be initialized
940 * by calling NE_DoLoadModule later, in the context of the newly
941 * created process.
943 * If lib_only is TRUE, however, the module is perforce treated
944 * like a DLL module, even if it is an executable module.
947 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
949 NE_MODULE *pModule;
950 HMODULE16 hModule;
951 HINSTANCE16 hInstance;
952 HFILE16 hFile;
953 OFSTRUCT ofs;
954 HANDLE mapping;
955 void *ptr;
956 MEMORY_BASIC_INFORMATION info;
958 /* Open file */
959 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
960 return ERROR_FILE_NOT_FOUND;
962 mapping = CreateFileMappingW( DosFileHandleToWin32Handle(hFile), NULL, PAGE_WRITECOPY, 0, 0, NULL );
963 _lclose16( hFile );
964 if (!mapping) return ERROR_BAD_FORMAT;
966 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 0 );
967 CloseHandle( mapping );
968 if (!ptr) return ERROR_BAD_FORMAT;
970 VirtualQuery( ptr, &info, sizeof(info) );
971 hModule = build_module( ptr, info.RegionSize, ofs.szPathName );
973 if (hModule < 32)
975 UnmapViewOfFile( ptr );
976 return hModule;
979 SNOOP16_RegisterDLL( hModule, ofs.szPathName );
980 NE_InitResourceHandler( hModule );
982 pModule = NE_GetPtr( hModule );
984 if ( !lib_only && !( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) )
985 return hModule;
987 hInstance = NE_DoLoadModule( pModule );
988 if ( hInstance < 32 )
990 /* cleanup ... */
991 NE_FreeModule( hModule, 0 );
994 return hInstance;
998 /***********************************************************************
999 * NE_DoLoadBuiltinModule
1001 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1003 static HMODULE16 NE_DoLoadBuiltinModule( const IMAGE_DOS_HEADER *mz_header, const char *file_name,
1004 HMODULE owner32 )
1006 NE_MODULE *pModule;
1007 HMODULE16 hModule;
1008 HINSTANCE16 hInstance;
1009 OSVERSIONINFOW versionInfo;
1010 SIZE_T mapping_size = ~0UL; /* assume builtins don't contain invalid offsets... */
1012 hModule = build_module( mz_header, mapping_size, file_name );
1013 if (hModule < 32) return hModule;
1014 pModule = GlobalLock16( hModule );
1015 pModule->ne_flags |= NE_FFLAGS_BUILTIN;
1016 pModule->owner32 = owner32;
1018 /* fake the expected version the module should have according to the current Windows version */
1019 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1020 if (GetVersionExW( &versionInfo ))
1021 pModule->ne_expver = MAKEWORD( versionInfo.dwMinorVersion, versionInfo.dwMajorVersion );
1023 hInstance = NE_DoLoadModule( pModule );
1024 if (hInstance < 32) NE_FreeModule( hModule, 0 );
1026 NE_InitResourceHandler( hModule );
1028 if (pModule->ne_heap)
1030 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1;
1031 unsigned int size = pSeg->minsize + pModule->ne_heap;
1032 if (size > 0xfffe) size = 0xfffe;
1033 LocalInit16( GlobalHandleToSel16(pSeg->hSeg), pSeg->minsize, size );
1036 patch_code_segment( pModule );
1038 return hInstance;
1042 /**********************************************************************
1043 * MODULE_LoadModule16
1045 * Load a NE module in the order of the loadorder specification.
1046 * The caller is responsible that the module is not loaded already.
1049 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1051 HINSTANCE16 hinst = 2;
1052 HMODULE16 hModule;
1053 HMODULE mod32 = 0;
1054 NE_MODULE *pModule;
1055 const IMAGE_DOS_HEADER *descr = NULL;
1056 const char *file_name = NULL;
1057 char dllname[32], owner[20], *p;
1058 const char *basename, *main_module;
1059 int owner_exists = FALSE;
1061 /* strip path information */
1063 basename = libname;
1064 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1065 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
1066 if ((p = strrchr( basename, '/' ))) basename = p + 1;
1068 if (strlen(basename) < sizeof(dllname)-6)
1070 strcpy( dllname, basename );
1071 p = strrchr( dllname, '.' );
1072 if (!p) strcat( dllname, ".dll" );
1073 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
1075 strcpy( p, "16" );
1076 if ((mod32 = LoadLibraryA( dllname )))
1078 if (!(descr = (void *)GetProcAddress( mod32, "__wine_spec_dos_header" )))
1080 WARN( "loaded %s but does not contain a 16-bit module\n", debugstr_a(dllname) );
1081 FreeLibrary( mod32 );
1083 else
1085 TRACE( "found %s with embedded 16-bit module\n", debugstr_a(dllname) );
1086 file_name = basename;
1088 /* if module has a 32-bit owner, match the load order of the owner */
1089 if ((main_module = (void *)GetProcAddress( mod32, "__wine_spec_main_module" )))
1091 LDR_MODULE *ldr;
1092 HMODULE main_owner = LoadLibraryA( main_module );
1094 if (!main_owner)
1096 WARN( "couldn't load owner %s for 16-bit dll %s\n", main_module, dllname );
1097 FreeLibrary( mod32 );
1098 return ERROR_FILE_NOT_FOUND;
1100 /* check if module was loaded native */
1101 if (LdrFindEntryForAddress( main_owner, &ldr ) || !(ldr->Flags & LDR_WINE_INTERNAL))
1103 FreeLibrary( mod32 );
1104 descr = NULL;
1106 FreeLibrary( main_owner );
1110 *p = 0;
1112 /* old-style 16-bit placeholders support, to be removed at some point */
1113 if (!mod32 && wine_dll_get_owner( dllname, owner, sizeof(owner), &owner_exists ) != -1)
1115 mod32 = LoadLibraryA( owner );
1116 if (mod32)
1118 if (!(descr = find_dll_descr( dllname, &file_name )))
1120 FreeLibrary( mod32 );
1121 owner_exists = 0;
1123 /* loading the 32-bit library can have the side effect of loading the module */
1124 /* if so, simply incr the ref count and return the module */
1125 if ((hModule = GetModuleHandle16( libname )))
1127 TRACE( "module %s already loaded by owner\n", libname );
1128 pModule = NE_GetPtr( hModule );
1129 if (pModule) pModule->count++;
1130 FreeLibrary( mod32 );
1131 return hModule;
1134 else
1136 /* it's probably disabled by the load order config */
1137 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner, dllname );
1138 return ERROR_FILE_NOT_FOUND;
1143 if (descr)
1145 TRACE("Trying built-in '%s'\n", libname);
1146 hinst = NE_DoLoadBuiltinModule( descr, file_name, mod32 );
1147 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : builtin\n", debugstr_a(file_name));
1149 else
1151 TRACE("Trying native dll '%s'\n", libname);
1152 hinst = NE_LoadModule(libname, lib_only);
1153 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : native\n", debugstr_a(libname));
1154 if (hinst == ERROR_FILE_NOT_FOUND && owner_exists) hinst = 21; /* win32 module */
1157 if (hinst > 32 && !implicit)
1159 hModule = GetModuleHandle16(libname);
1160 if(!hModule)
1162 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1163 libname, hinst);
1164 return ERROR_INVALID_HANDLE;
1167 pModule = NE_GetPtr(hModule);
1168 if(!pModule)
1170 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1171 libname, hinst);
1172 return ERROR_INVALID_HANDLE;
1175 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1178 * Call initialization routines for all loaded DLLs. Note that
1179 * when we load implicitly linked DLLs this will be done by InitTask().
1181 if(pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1183 NE_InitializeDLLs(hModule);
1184 NE_DllProcessAttach(hModule);
1187 return hinst; /* The last error that occurred */
1191 /**********************************************************************
1192 * NE_CreateThread
1194 * Create the thread for a 16-bit module.
1196 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1198 HANDLE hThread;
1199 TDB *pTask;
1200 HTASK16 hTask;
1201 HINSTANCE16 instance = 0;
1203 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1204 return 0;
1206 /* Post event to start the task */
1207 PostEvent16( hTask );
1209 /* Wait until we get the instance handle */
1212 DirectedYield16( hTask );
1213 if (!IsTask16( hTask )) /* thread has died */
1215 DWORD exit_code;
1216 WaitForSingleObject( hThread, INFINITE );
1217 GetExitCodeThread( hThread, &exit_code );
1218 CloseHandle( hThread );
1219 return exit_code;
1221 if (!(pTask = GlobalLock16( hTask ))) break;
1222 instance = pTask->hInstance;
1223 GlobalUnlock16( hTask );
1224 } while (!instance);
1226 CloseHandle( hThread );
1227 return instance;
1231 /**********************************************************************
1232 * LoadModule (KERNEL.45)
1234 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1236 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1237 LOADPARAMS16 *params;
1238 HMODULE16 hModule;
1239 NE_MODULE *pModule;
1240 LPSTR cmdline;
1241 WORD cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
1243 if (name == NULL) return 0;
1245 TRACE("name %s, paramBlock %p\n", name, paramBlock);
1247 /* Load module */
1249 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1251 /* Special case: second instance of an already loaded NE module */
1253 if ( !( pModule = NE_GetPtr( hModule ) ) ) return ERROR_BAD_FORMAT;
1254 if ( pModule->module32 ) return (HINSTANCE16)21;
1256 /* Increment refcount */
1258 pModule->count++;
1260 else
1262 /* Main case: load first instance of NE module */
1264 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1265 return hModule;
1267 if ( !(pModule = NE_GetPtr( hModule )) )
1268 return ERROR_BAD_FORMAT;
1271 /* If library module, we just retrieve the instance handle */
1273 if ( ( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1274 return NE_GetInstance( pModule );
1277 * At this point, we need to create a new process.
1279 * pModule points either to an already loaded module, whose refcount
1280 * has already been incremented (to avoid having the module vanish
1281 * in the meantime), or else to a stub module which contains only header
1282 * information.
1284 params = paramBlock;
1285 if (params->showCmd)
1286 cmdShow = ((WORD *)MapSL( params->showCmd ))[1];
1287 cmdline = MapSL( params->cmdLine );
1288 return NE_CreateThread( pModule, cmdShow, cmdline );
1292 /**********************************************************************
1293 * NE_StartTask
1295 * Startup code for a new 16-bit task.
1297 DWORD NE_StartTask(void)
1299 TDB *pTask = TASK_GetCurrent();
1300 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1301 HINSTANCE16 hInstance, hPrevInstance;
1302 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1303 WORD sp;
1305 if ( pModule->count > 0 )
1307 /* Second instance of an already loaded NE module */
1308 /* Note that the refcount was already incremented by the parent */
1310 hPrevInstance = NE_GetInstance( pModule );
1312 if ( pModule->ne_autodata )
1313 if ( NE_CreateSegment( pModule, pModule->ne_autodata ) )
1314 NE_LoadSegment( pModule, pModule->ne_autodata );
1316 hInstance = NE_GetInstance( pModule );
1317 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->ne_autodata, hPrevInstance);
1320 else
1322 /* Load first instance of NE module */
1324 pModule->ne_flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1326 hInstance = NE_DoLoadModule( pModule );
1327 hPrevInstance = 0;
1330 if ( hInstance >= 32 )
1332 CONTEXT86 context;
1334 /* Enter instance handles into task struct */
1336 pTask->hInstance = hInstance;
1337 pTask->hPrevInstance = hPrevInstance;
1339 /* Use DGROUP for 16-bit stack */
1341 if (!(sp = OFFSETOF(pModule->ne_sssp)))
1342 sp = pSegTable[SELECTOROF(pModule->ne_sssp)-1].minsize + pModule->ne_stack;
1343 sp &= ~1;
1344 sp -= sizeof(STACK16FRAME);
1345 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1347 /* Registers at initialization must be:
1348 * ax zero
1349 * bx stack size in bytes
1350 * cx heap size in bytes
1351 * si previous app instance
1352 * di current app instance
1353 * bp zero
1354 * es selector to the PSP
1355 * ds dgroup of the application
1356 * ss stack selector
1357 * sp top of the stack
1359 memset( &context, 0, sizeof(context) );
1360 context.SegCs = GlobalHandleToSel16(pSegTable[SELECTOROF(pModule->ne_csip) - 1].hSeg);
1361 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1362 context.SegEs = pTask->hPDB;
1363 context.SegFs = wine_get_fs();
1364 context.SegGs = wine_get_gs();
1365 context.Eip = OFFSETOF(pModule->ne_csip);
1366 context.Ebx = pModule->ne_stack;
1367 context.Ecx = pModule->ne_heap;
1368 context.Edi = pTask->hInstance;
1369 context.Esi = pTask->hPrevInstance;
1371 /* Now call 16-bit entry point */
1373 TRACE("Starting main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
1374 context.SegCs, context.Eip, context.SegDs,
1375 SELECTOROF(NtCurrentTeb()->WOW32Reserved),
1376 OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
1378 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1379 ExitThread( LOWORD(context.Eax) );
1381 return hInstance; /* error code */
1384 /***********************************************************************
1385 * LoadLibrary (KERNEL.95)
1386 * LoadLibrary16 (KERNEL32.35)
1388 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1390 return LoadModule16(libname, (LPVOID)-1 );
1394 /**********************************************************************
1395 * MODULE_CallWEP
1397 * Call a DLL's WEP, allowing it to shut down.
1398 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1400 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1402 BOOL16 ret;
1403 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1404 if (!WEP) return FALSE;
1406 __TRY
1408 WORD args[1];
1409 DWORD dwRet;
1411 args[0] = WEP_FREE_DLL;
1412 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1413 ret = LOWORD(dwRet);
1415 __EXCEPT_PAGE_FAULT
1417 WARN("Page fault\n");
1418 ret = 0;
1420 __ENDTRY
1422 return ret;
1426 /**********************************************************************
1427 * NE_FreeModule
1429 * Implementation of FreeModule16().
1431 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1433 HMODULE16 *hPrevModule;
1434 NE_MODULE *pModule;
1435 HMODULE16 *pModRef;
1436 int i;
1438 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1439 hModule = pModule->self;
1441 TRACE("%04x count %d\n", hModule, pModule->count );
1443 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1444 else pModule->count = 0;
1446 if (call_wep && !(pModule->ne_flags & NE_FFLAGS_WIN32))
1448 /* Free the objects owned by the DLL module */
1449 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1451 if (pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1452 MODULE_CallWEP( hModule );
1453 else
1454 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1457 TRACE_(loaddll)("Unloaded module %s : %s\n", debugstr_a(NE_MODULE_NAME(pModule)),
1458 (pModule->ne_flags & NE_FFLAGS_BUILTIN) ? "builtin" : "native");
1460 /* Clear magic number just in case */
1462 pModule->ne_magic = pModule->self = 0;
1463 if (pModule->owner32) FreeLibrary( pModule->owner32 );
1464 else if (pModule->mapping) UnmapViewOfFile( pModule->mapping );
1466 /* Remove it from the linked list */
1468 hPrevModule = &hFirstModule;
1469 while (*hPrevModule && (*hPrevModule != hModule))
1471 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1473 if (*hPrevModule) *hPrevModule = pModule->next;
1475 /* Free the referenced modules */
1477 pModRef = (HMODULE16*)((char *)pModule + pModule->ne_modtab);
1478 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
1480 NE_FreeModule( *pModRef, call_wep );
1483 /* Free the module storage */
1485 GlobalFreeAll16( hModule );
1486 return TRUE;
1490 /**********************************************************************
1491 * FreeModule (KERNEL.46)
1493 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1495 return NE_FreeModule( hModule, TRUE );
1499 /***********************************************************************
1500 * FreeLibrary (KERNEL.96)
1501 * FreeLibrary16 (KERNEL32.36)
1503 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1505 TRACE("%04x\n", handle );
1506 FreeModule16( handle );
1510 /***********************************************************************
1511 * GetModuleHandle16 (KERNEL32.@)
1513 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1515 HMODULE16 hModule = hFirstModule;
1516 LPSTR s;
1517 BYTE len, *name_table;
1518 char tmpstr[MAX_PATH];
1519 NE_MODULE *pModule;
1521 TRACE("(%s)\n", name);
1523 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1525 len = strlen(name);
1526 if (!len) return 0;
1528 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1530 /* If 'name' matches exactly the module name of a module:
1531 * Return its handle.
1533 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1535 pModule = NE_GetPtr( hModule );
1536 if (!pModule) break;
1537 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1539 name_table = (BYTE *)pModule + pModule->ne_restab;
1540 if ((*name_table == len) && !strncmp(name, (char*) name_table+1, len))
1541 return hModule;
1544 /* If uppercased 'name' matches exactly the module name of a module:
1545 * Return its handle
1547 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1549 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1551 pModule = NE_GetPtr( hModule );
1552 if (!pModule) break;
1553 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1555 name_table = (BYTE *)pModule + pModule->ne_restab;
1556 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1557 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1558 * lowercased module names, but try to load uppercase DLLs, so this
1559 * 'i' compare is just a quickfix until the loader handles that
1560 * correctly. -MM 990705
1562 if ((*name_table == len) && !NE_strncasecmp(tmpstr, (const char*)name_table+1, len))
1563 return hModule;
1566 /* If the base filename of 'name' matches the base filename of the module
1567 * filename of some module (case-insensitive compare):
1568 * Return its handle.
1571 /* basename: search backwards in passed name to \ / or : */
1572 s = tmpstr + strlen(tmpstr);
1573 while (s > tmpstr)
1575 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1576 break;
1577 s--;
1580 /* search this in loaded filename list */
1581 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1583 char *loadedfn;
1584 OFSTRUCT *ofs;
1586 pModule = NE_GetPtr( hModule );
1587 if (!pModule) break;
1588 if (!pModule->fileinfo) continue;
1589 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1591 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1592 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1593 /* basename: search backwards in pathname to \ / or : */
1594 while (loadedfn > (char*)ofs->szPathName)
1596 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1597 break;
1598 loadedfn--;
1600 /* case insensitive compare ... */
1601 if (!NE_strcasecmp(loadedfn, s))
1602 return hModule;
1604 return 0;
1608 /**********************************************************************
1609 * GetModuleName (KERNEL.27)
1611 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1613 NE_MODULE *pModule;
1614 BYTE *p;
1616 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1617 p = (BYTE *)pModule + pModule->ne_restab;
1618 if (count > *p) count = *p + 1;
1619 if (count > 0)
1621 memcpy( buf, p + 1, count - 1 );
1622 buf[count-1] = '\0';
1624 return TRUE;
1628 /**********************************************************************
1629 * GetModuleFileName (KERNEL.49)
1631 * Comment: see GetModuleFileNameA
1633 * Even if invoked by second instance of a program,
1634 * it still returns path of first one.
1636 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1637 INT16 nSize )
1639 NE_MODULE *pModule;
1641 /* Win95 does not query hModule if set to 0 !
1642 * Is this wrong or maybe Win3.1 only ? */
1643 if (!hModule) hModule = GetCurrentTask();
1645 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1646 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1647 if (pModule->ne_expver < 0x400)
1648 GetShortPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1649 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1650 return strlen(lpFileName);
1654 /**********************************************************************
1655 * GetModuleUsage (KERNEL.48)
1657 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1659 NE_MODULE *pModule = NE_GetPtr( hModule );
1660 return pModule ? pModule->count : 0;
1664 /**********************************************************************
1665 * GetExpWinVer (KERNEL.167)
1667 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1669 NE_MODULE *pModule = NE_GetPtr( hModule );
1670 if ( !pModule ) return 0;
1671 return pModule->ne_expver;
1675 /***********************************************************************
1676 * WinExec (KERNEL.166)
1678 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1680 LPCSTR p, args = NULL;
1681 LPCSTR name_beg, name_end;
1682 LPSTR name, cmdline;
1683 int arglen;
1684 HINSTANCE16 ret;
1685 char buffer[MAX_PATH];
1686 LOADPARAMS16 params;
1687 WORD showCmd[2];
1689 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1691 name_beg = lpCmdLine+1;
1692 p = strchr ( lpCmdLine+1, '"' );
1693 if (p)
1695 name_end = p;
1696 args = strchr ( p, ' ' );
1698 else /* yes, even valid with trailing '"' missing */
1699 name_end = lpCmdLine+strlen(lpCmdLine);
1701 else
1703 name_beg = lpCmdLine;
1704 args = strchr( lpCmdLine, ' ' );
1705 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1708 if ((name_beg == lpCmdLine) && (!args))
1709 { /* just use the original cmdline string as file name */
1710 name = (LPSTR)lpCmdLine;
1712 else
1714 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1715 return ERROR_NOT_ENOUGH_MEMORY;
1716 memcpy( name, name_beg, name_end - name_beg );
1717 name[name_end - name_beg] = '\0';
1720 if (args)
1722 args++;
1723 arglen = strlen(args);
1724 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1725 cmdline[0] = (BYTE)arglen;
1726 strcpy( cmdline + 1, args );
1728 else
1730 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1731 cmdline[0] = cmdline[1] = 0;
1734 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1736 showCmd[0] = 2;
1737 showCmd[1] = nCmdShow;
1739 params.hEnvironment = 0;
1740 params.cmdLine = MapLS( cmdline );
1741 params.showCmd = MapLS( showCmd );
1742 params.reserved = 0;
1744 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1746 ret = LoadModule16( buffer, &params );
1748 else if (!contains_path( name )) /* try 16-bit builtin */
1750 lstrcpynA( buffer, name, sizeof(buffer) );
1751 if (strlen( buffer ) < sizeof(buffer) - 4 && !strchr( buffer, '.' )) strcat( buffer, ".exe" );
1752 ret = LoadModule16( buffer, &params );
1753 if (ret == ERROR_FILE_NOT_FOUND) ret = 21; /* it might be a 32-bit builtin too */
1755 else ret = ERROR_FILE_NOT_FOUND;
1757 UnMapLS( params.cmdLine );
1758 UnMapLS( params.showCmd );
1760 HeapFree( GetProcessHeap(), 0, cmdline );
1761 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1763 if (ret == 21 || ret == ERROR_BAD_FORMAT) /* 32-bit module or unknown executable*/
1765 LOADPARAMS16 params;
1766 WORD showCmd[2];
1767 showCmd[0] = 2;
1768 showCmd[1] = nCmdShow;
1770 arglen = strlen( lpCmdLine );
1771 cmdline = HeapAlloc( GetProcessHeap(), 0, arglen + 1 );
1772 cmdline[0] = (BYTE)arglen;
1773 memcpy( cmdline + 1, lpCmdLine, arglen );
1775 params.hEnvironment = 0;
1776 params.cmdLine = MapLS( cmdline );
1777 params.showCmd = MapLS( showCmd );
1778 params.reserved = 0;
1780 ret = LoadModule16( "winoldap.mod", &params );
1781 UnMapLS( params.cmdLine );
1782 UnMapLS( params.showCmd );
1784 return ret;
1787 /***********************************************************************
1788 * GetProcAddress (KERNEL.50)
1790 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1792 WORD ordinal;
1793 FARPROC16 ret;
1795 if (!hModule) hModule = GetCurrentTask();
1796 hModule = GetExePtr( hModule );
1798 if (HIWORD(name) != 0)
1800 ordinal = NE_GetOrdinal( hModule, name );
1801 TRACE("%04x '%s'\n", hModule, name );
1803 else
1805 ordinal = LOWORD(name);
1806 TRACE("%04x %04x\n", hModule, ordinal );
1808 if (!ordinal) return NULL;
1810 ret = NE_GetEntryPoint( hModule, ordinal );
1812 TRACE("returning %p\n", ret );
1813 return ret;
1817 /***************************************************************************
1818 * HasGPHandler (KERNEL.338)
1820 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1822 HMODULE16 hModule;
1823 int gpOrdinal;
1824 SEGPTR gpPtr;
1825 GPHANDLERDEF *gpHandler;
1827 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1828 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1829 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1830 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1831 && (gpHandler = MapSL( gpPtr )) != NULL )
1833 while (gpHandler->selector)
1835 if ( SELECTOROF(address) == gpHandler->selector
1836 && OFFSETOF(address) >= gpHandler->rangeStart
1837 && OFFSETOF(address) < gpHandler->rangeEnd )
1838 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1839 gpHandler++;
1843 return 0;
1847 /**********************************************************************
1848 * GetModuleHandle (KERNEL.47)
1850 * Find a module from a module name.
1852 * NOTE: The current implementation works the same way the Windows 95 one
1853 * does. Do not try to 'fix' it, fix the callers.
1854 * + It does not do ANY extension handling (except that strange .EXE bit)!
1855 * + It does not care about paths, just about basenames. (same as Windows)
1857 * RETURNS
1858 * LOWORD:
1859 * the win16 module handle if found
1860 * 0 if not
1861 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1862 * Always hFirstModule
1864 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1866 if (HIWORD(name) == 0)
1867 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1868 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1871 /**********************************************************************
1872 * NE_GetModuleByFilename
1874 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1876 HMODULE16 hModule;
1877 LPSTR s, p;
1878 BYTE len, *name_table;
1879 char tmpstr[MAX_PATH];
1880 NE_MODULE *pModule;
1882 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1884 /* If the base filename of 'name' matches the base filename of the module
1885 * filename of some module (case-insensitive compare):
1886 * Return its handle.
1889 /* basename: search backwards in passed name to \ / or : */
1890 s = tmpstr + strlen(tmpstr);
1891 while (s > tmpstr)
1893 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1894 break;
1895 s--;
1898 /* search this in loaded filename list */
1899 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1901 char *loadedfn;
1902 OFSTRUCT *ofs;
1904 pModule = NE_GetPtr( hModule );
1905 if (!pModule) break;
1906 if (!pModule->fileinfo) continue;
1907 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1909 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1910 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1911 /* basename: search backwards in pathname to \ / or : */
1912 while (loadedfn > (char*)ofs->szPathName)
1914 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1915 break;
1916 loadedfn--;
1918 /* case insensitive compare ... */
1919 if (!NE_strcasecmp(loadedfn, s))
1920 return hModule;
1922 /* If basename (without ext) matches the module name of a module:
1923 * Return its handle.
1926 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1927 len = strlen(s);
1929 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1931 pModule = NE_GetPtr( hModule );
1932 if (!pModule) break;
1933 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1935 name_table = (BYTE *)pModule + pModule->ne_restab;
1936 if ((*name_table == len) && !NE_strncasecmp(s, (const char*)name_table+1, len))
1937 return hModule;
1940 return 0;
1943 /***********************************************************************
1944 * GetProcAddress16 (KERNEL32.37)
1945 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1947 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1949 if (!hModule) return 0;
1950 if (HIWORD(hModule))
1952 WARN("hModule is Win32 handle (%p)\n", hModule );
1953 return 0;
1955 return GetProcAddress16( LOWORD(hModule), name );
1958 /**********************************************************************
1959 * ModuleFirst (TOOLHELP.59)
1961 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1963 lpme->wNext = hFirstModule;
1964 return ModuleNext16( lpme );
1968 /**********************************************************************
1969 * ModuleNext (TOOLHELP.60)
1971 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1973 NE_MODULE *pModule;
1974 char *name;
1976 if (!lpme->wNext) return FALSE;
1977 if (!(pModule = NE_GetPtr( lpme->wNext ))) return FALSE;
1978 name = (char *)pModule + pModule->ne_restab;
1979 memcpy( lpme->szModule, name + 1, min(*name, MAX_MODULE_NAME) );
1980 lpme->szModule[min(*name, MAX_MODULE_NAME)] = '\0';
1981 lpme->hModule = lpme->wNext;
1982 lpme->wcUsage = pModule->count;
1983 lstrcpynA( lpme->szExePath, NE_MODULE_NAME(pModule), sizeof(lpme->szExePath) );
1984 lpme->wNext = pModule->next;
1985 return TRUE;
1989 /**********************************************************************
1990 * ModuleFindName (TOOLHELP.61)
1992 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1994 lpme->wNext = GetModuleHandle16( name );
1995 return ModuleNext16( lpme );
1999 /**********************************************************************
2000 * ModuleFindHandle (TOOLHELP.62)
2002 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
2004 hModule = GetExePtr( hModule );
2005 lpme->wNext = hModule;
2006 return ModuleNext16( lpme );
2010 /***************************************************************************
2011 * IsRomModule (KERNEL.323)
2013 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
2015 return FALSE;
2018 /***************************************************************************
2019 * IsRomFile (KERNEL.326)
2021 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
2023 return FALSE;
2026 /***********************************************************************
2027 * create_dummy_module
2029 * Create a dummy NE module for Win32 or Winelib.
2031 static HMODULE16 create_dummy_module( HMODULE module32 )
2033 HMODULE16 hModule;
2034 NE_MODULE *pModule;
2035 SEGTABLEENTRY *pSegment;
2036 char *pStr,*s;
2037 unsigned int len;
2038 const char* basename;
2039 OFSTRUCT *ofs;
2040 int of_size, size;
2041 char filename[MAX_PATH];
2042 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
2044 if (!nt) return ERROR_BAD_FORMAT;
2046 /* Extract base filename */
2047 len = GetModuleFileNameA( module32, filename, sizeof(filename) );
2048 if (!len || len >= sizeof(filename)) return ERROR_BAD_FORMAT;
2049 basename = strrchr(filename, '\\');
2050 if (!basename) basename = filename;
2051 else basename++;
2052 len = strlen(basename);
2053 if ((s = strchr(basename, '.'))) len = s - basename;
2055 /* Allocate module */
2056 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
2057 + strlen(filename) + 1;
2058 size = sizeof(NE_MODULE) +
2059 /* loaded file info */
2060 ((of_size + 3) & ~3) +
2061 /* segment table: DS,CS */
2062 2 * sizeof(SEGTABLEENTRY) +
2063 /* name table */
2064 len + 2 +
2065 /* several empty tables */
2068 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2069 if (!hModule) return ERROR_BAD_FORMAT;
2071 FarSetOwner16( hModule, hModule );
2072 pModule = GlobalLock16( hModule );
2074 /* Set all used entries */
2075 pModule->ne_magic = IMAGE_OS2_SIGNATURE;
2076 pModule->count = 1;
2077 pModule->next = 0;
2078 pModule->ne_flags = NE_FFLAGS_WIN32;
2079 pModule->ne_autodata = 0;
2080 pModule->ne_sssp = MAKESEGPTR( 0, 1 );
2081 pModule->ne_csip = MAKESEGPTR( 0, 2 );
2082 pModule->ne_heap = 0;
2083 pModule->ne_stack = 0;
2084 pModule->ne_cseg = 2;
2085 pModule->ne_cmod = 0;
2086 pModule->ne_cbnrestab = 0;
2087 pModule->fileinfo = sizeof(NE_MODULE);
2088 pModule->ne_exetyp = NE_OSFLAGS_WINDOWS;
2089 pModule->self = hModule;
2090 pModule->module32 = module32;
2092 /* Set version and flags */
2093 pModule->ne_expver = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2094 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2095 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2096 pModule->ne_flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2098 /* Set loaded file information */
2099 ofs = (OFSTRUCT *)(pModule + 1);
2100 memset( ofs, 0, of_size );
2101 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2102 strcpy( ofs->szPathName, filename );
2104 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2105 pModule->ne_segtab = (char *)pSegment - (char *)pModule;
2106 /* Data segment */
2107 pSegment->size = 0;
2108 pSegment->flags = NE_SEGFLAGS_DATA;
2109 pSegment->minsize = 0x1000;
2110 pSegment++;
2111 /* Code segment */
2112 pSegment->flags = 0;
2113 pSegment++;
2115 /* Module name */
2116 pStr = (char *)pSegment;
2117 pModule->ne_restab = pStr - (char *)pModule;
2118 assert(len<256);
2119 *pStr = len;
2120 lstrcpynA( pStr+1, basename, len+1 );
2121 pStr += len+2;
2123 /* All tables zero terminated */
2124 pModule->ne_rsrctab = pModule->ne_imptab = pModule->ne_enttab = pStr - (char *)pModule;
2126 NE_RegisterModule( pModule );
2127 pModule->owner32 = LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2128 return hModule;
2131 /***********************************************************************
2132 * PrivateLoadLibrary (KERNEL32.@)
2134 * FIXME: rough guesswork, don't know what "Private" means
2136 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2138 return LoadLibrary16(libname);
2141 /***********************************************************************
2142 * PrivateFreeLibrary (KERNEL32.@)
2144 * FIXME: rough guesswork, don't know what "Private" means
2146 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2148 FreeLibrary16(handle);
2151 /***********************************************************************
2152 * LoadLibrary32 (KERNEL.452)
2153 * LoadSystemLibrary32 (KERNEL.482)
2155 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2157 HMODULE hModule;
2158 DWORD count;
2160 ReleaseThunkLock( &count );
2161 hModule = LoadLibraryA( libname );
2162 RestoreThunkLock( count );
2163 return hModule;
2166 /***************************************************************************
2167 * MapHModuleLS (KERNEL32.@)
2169 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2171 HMODULE16 ret;
2172 NE_MODULE *pModule;
2174 if (!hmod)
2175 return TASK_GetCurrent()->hInstance;
2176 if (!HIWORD(hmod))
2177 return LOWORD(hmod); /* we already have a 16 bit module handle */
2178 pModule = GlobalLock16(hFirstModule);
2179 while (pModule) {
2180 if (pModule->module32 == hmod)
2181 return pModule->self;
2182 pModule = GlobalLock16(pModule->next);
2184 if ((ret = create_dummy_module( hmod )) < 32)
2186 SetLastError(ret);
2187 ret = 0;
2189 return ret;
2192 /***************************************************************************
2193 * MapHModuleSL (KERNEL32.@)
2195 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2197 NE_MODULE *pModule;
2199 if (!hmod) {
2200 TDB *pTask = TASK_GetCurrent();
2201 hmod = pTask->hModule;
2203 pModule = GlobalLock16(hmod);
2204 if ((pModule->ne_magic != IMAGE_OS2_SIGNATURE) || !(pModule->ne_flags & NE_FFLAGS_WIN32))
2205 return 0;
2206 return pModule->module32;
2209 /***************************************************************************
2210 * MapHInstLS (KERNEL.472)
2212 void WINAPI MapHInstLS16( CONTEXT86 *context )
2214 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2217 /***************************************************************************
2218 * MapHInstSL (KERNEL.473)
2220 void WINAPI MapHInstSL16( CONTEXT86 *context )
2222 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2225 #ifdef __i386__
2227 /***************************************************************************
2228 * MapHInstLS (KERNEL32.@)
2230 __ASM_STDCALL_FUNC( MapHInstLS, 0,
2231 "pushl %eax\n\t"
2232 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n\t"
2233 "ret" )
2235 /***************************************************************************
2236 * MapHInstSL (KERNEL32.@)
2238 __ASM_STDCALL_FUNC( MapHInstSL, 0,
2239 "pushl %eax\n\t"
2240 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n\t"
2241 "ret" )
2243 /***************************************************************************
2244 * MapHInstLS_PN (KERNEL32.@)
2246 __ASM_STDCALL_FUNC( MapHInstLS_PN, 0,
2247 "testl %eax,%eax\n\t"
2248 "jz 1f\n\t"
2249 "pushl %eax\n\t"
2250 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n"
2251 "1:\tret" )
2253 /***************************************************************************
2254 * MapHInstSL_PN (KERNEL32.@)
2256 __ASM_STDCALL_FUNC( MapHInstSL_PN, 0,
2257 "andl $0xffff,%eax\n\t"
2258 "jz 1f\n\t"
2259 "pushl %eax\n\t"
2260 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n"
2261 "1:\tret" )
2263 #endif /* __i386__ */