1 /*
2 * NE modules
3 *
4 * Copyright 1995 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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>
34
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"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(module);
46 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
48
49 #include "pshpack1.h"
50 typedef struct _GPHANDLERDEF
51 {
52 WORD selector;
53 WORD rangeStart;
54 WORD rangeEnd;
55 WORD handler;
56 } GPHANDLERDEF;
57 #include "poppack.h"
58
59 /*
60 * Segment table entry
61 */
62 struct ne_segment_table_entry_s
63 {
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 */
68 };
69
70 #define hFirstModule (pThhook->hExeHead)
71
72 struct builtin_dll
73 {
74 const IMAGE_DOS_HEADER *header; /* module headers */
75 const char *file_name; /* module file name */
76 };
77
78 /* Table of all built-in DLLs */
79
80 #define MAX_DLLS 50
81
82 static struct builtin_dll builtin_dlls[MAX_DLLS];
83
84 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
85 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
86
87 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
88
89 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
90
91
92 /* patch all the flat cs references of the code segment if necessary */
93 static inline void patch_code_segment( NE_MODULE *pModule )
94 {
95 #ifdef __i386__
96 int i;
97 CALLFROM16 *call;
98 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
99
100 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
101 if (!(pSeg->flags & NE_SEGFLAGS_DATA)) break; /* found the code segment */
102
103 call = GlobalLock16( pSeg->hSeg );
104
105 /* patch glue code address and code selector */
106 for (i = 0; call[i].pushl == 0x68; i++)
107 {
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();
113 }
114
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
118 }
119
120
121 /***********************************************************************
122 * contains_path
123 */
124 static inline int contains_path( LPCSTR name )
125 {
126 return ((*name && (name[1] == ':')) || strchr(name, '/') || strchr(name, '\\'));
127 }
128
129
130 /***********************************************************************
131 * NE_strcasecmp
132 *
133 * locale-independent case conversion for module lookups
134 */
135 static int NE_strcasecmp( const char *str1, const char *str2 )
136 {
137 int ret = 0;
138 for ( ; ; str1++, str2++)
139 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
140 return ret;
141 }
142
143
144 /***********************************************************************
145 * NE_strncasecmp
146 *
147 * locale-independent case conversion for module lookups
148 */
149 static int NE_strncasecmp( const char *str1, const char *str2, int len )
150 {
151 int ret = 0;
152 for ( ; len > 0; len--, str1++, str2++)
153 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
154 return ret;
155 }
156
157
158 /***********************************************************************
159 * find_dll_descr
160 *
161 * Find a descriptor in the list
162 */
163 static const IMAGE_DOS_HEADER *find_dll_descr( const char *dllname, const char **file_name )
164 {
165 int i;
166 const IMAGE_DOS_HEADER *mz_header;
167 const IMAGE_OS2_HEADER *ne_header;
168 const BYTE *name_table;
169
170 for (i = 0; i < MAX_DLLS; i++)
171 {
172 mz_header = builtin_dlls[i].header;
173 if (mz_header)
174 {
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;
177
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" )))
183 {
184 *file_name = builtin_dlls[i].file_name;
185 return builtin_dlls[i].header;
186 }
187 }
188 }
189 return NULL;
190 }
191
192
193 /***********************************************************************
194 * __wine_dll_register_16 (KERNEL32.@)
195 *
196 * Register a built-in DLL descriptor.
197 */
198 void __wine_dll_register_16( const IMAGE_DOS_HEADER *header, const char *file_name )
199 {
200 int i;
201
202 for (i = 0; i < MAX_DLLS; i++)
203 {
204 if (builtin_dlls[i].header) continue;
205 builtin_dlls[i].header = header;
206 builtin_dlls[i].file_name = file_name;
207 break;
208 }
209 assert( i < MAX_DLLS );
210 }
211
212
213 /***********************************************************************
214 * __wine_dll_unregister_16 (KERNEL32.@)
215 *
216 * Unregister a built-in DLL descriptor.
217 */
218 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER *header )
219 {
220 int i;
221
222 for (i = 0; i < MAX_DLLS; i++)
223 {
224 if (builtin_dlls[i].header != header) continue;
225 builtin_dlls[i].header = NULL;
226 break;
227 }
228 }
229
230
231 /***********************************************************************
232 * NE_GetPtr
233 */
234 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
235 {
236 return GlobalLock16( GetExePtr(hModule) );
237 }
238
239
240 /**********************************************************************
241 * NE_RegisterModule
242 */
243 static void NE_RegisterModule( NE_MODULE *pModule )
244 {
245 pModule->next = hFirstModule;
246 hFirstModule = pModule->self;
247 }
248
249
250 /***********************************************************************
251 * NE_DumpModule
252 */
253 void NE_DumpModule( HMODULE16 hModule )
254 {
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;
262
263 if (!(pModule = NE_GetPtr( hModule )))
264 {
265 ERR( "**** %04x is not a module handle\n", hModule );
266 return;
267 }
268
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 );
283
284 /* Dump the file info */
285 TRACE( "---\n" );
286 TRACE( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
287
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 );
296
297 /* Dump the resource table */
298 TRACE( "---\n" );
299 TRACE( "Resource table:\n" );
300 if (pModule->ne_rsrctab)
301 {
302 pword = (WORD *)((BYTE *)pModule + pModule->ne_rsrctab);
303 TRACE( "Alignment: %d\n", *pword++ );
304 while (*pword)
305 {
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;
313 }
314 }
315 else TRACE( "None\n" );
316
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)
322 {
323 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
324 *(WORD *)(pstr + *pstr + 1) );
325 pstr += *pstr + 1 + sizeof(WORD);
326 }
327
328 /* Dump the module reference table */
329 TRACE( "---\n" );
330 TRACE( "Module ref table:\n" );
331 if (pModule->ne_modtab)
332 {
333 pword = (WORD *)((BYTE *)pModule + pModule->ne_modtab);
334 for (i = 0; i < pModule->ne_cmod; i++, pword++)
335 {
336 char name[10];
337 GetModuleName16( *pword, name, sizeof(name) );
338 TRACE( "%d: %04x -> '%s'\n", i, *pword, name );
339 }
340 }
341 else TRACE( "None\n" );
342
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)
352 {
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++;
358 }
359 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
360
361 /* Dump the non-resident names table */
362 TRACE( "---\n" );
363 TRACE( "Non-resident names table:\n" );
364 if (pModule->nrname_handle)
365 {
366 pstr = GlobalLock16( pModule->nrname_handle );
367 while (*pstr)
368 {
369 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
370 *(WORD *)(pstr + *pstr + 1) );
371 pstr += *pstr + 1 + sizeof(WORD);
372 }
373 }
374 TRACE( "\n" );
375 }
376
377
378 /***********************************************************************
379 * NE_WalkModules
380 *
381 * Walk the module list and print the modules.
382 */
383 void NE_WalkModules(void)
384 {
385 HMODULE16 hModule = hFirstModule;
386 MESSAGE( "Module Flags Name\n" );
387 while (hModule)
388 {
389 NE_MODULE *pModule = NE_GetPtr( hModule );
390 if (!pModule)
391 {
392 MESSAGE( "Bad module %04x in list\n", hModule );
393 return;
394 }
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;
399 }
400 }
401
402
403 /***********************************************************************
404 * NE_InitResourceHandler
405 *
406 * Fill in 'resloader' fields in the resource table.
407 */
408 static void NE_InitResourceHandler( HMODULE16 hModule )
409 {
410 static FARPROC16 proc;
411
412 NE_TYPEINFO *pTypeInfo;
413 NE_MODULE *pModule;
414
415 if (!(pModule = NE_GetPtr( hModule )) || !pModule->ne_rsrctab) return;
416
417 TRACE("InitResourceHandler[%04x]\n", hModule );
418
419 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
420
421 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
422 while(pTypeInfo->type_id)
423 {
424 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
425 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
426 }
427 }
428
429
430 /***********************************************************************
431 * NE_GetOrdinal
432 *
433 * Lookup the ordinal for a given name.
434 */
435 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
436 {
437 char buffer[256], *p;
438 BYTE *cpnt;
439 BYTE len;
440 NE_MODULE *pModule;
441
442 if (!(pModule = NE_GetPtr( hModule ))) return 0;
443 if (pModule->ne_flags & NE_FFLAGS_WIN32) return 0;
444
445 TRACE("(%04x,'%s')\n", hModule, name );
446
447 /* First handle names of the form '#xxxx' */
448
449 if (name[0] == '#') return atoi( name + 1 );
450
451 /* Now copy and uppercase the string */
452
453 strcpy( buffer, name );
454 for (p = buffer; *p; p++) *p = RtlUpperChar(*p);
455 len = p - buffer;
456
457 /* First search the resident names */
458
459 cpnt = (BYTE *)pModule + pModule->ne_restab;
460
461 /* Skip the first entry (module name) */
462 cpnt += *cpnt + 1 + sizeof(WORD);
463 while (*cpnt)
464 {
465 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
466 {
467 WORD ordinal;
468 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
469 TRACE(" Found: ordinal=%d\n", ordinal );
470 return ordinal;
471 }
472 cpnt += *cpnt + 1 + sizeof(WORD);
473 }
474
475 /* Now search the non-resident names table */
476
477 if (!pModule->nrname_handle) return 0; /* No non-resident table */
478 cpnt = GlobalLock16( pModule->nrname_handle );
479
480 /* Skip the first entry (module description string) */
481 cpnt += *cpnt + 1 + sizeof(WORD);
482 while (*cpnt)
483 {
484 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
485 {
486 WORD ordinal;
487 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
488 TRACE(" Found: ordinal=%d\n", ordinal );
489 return ordinal;
490 }
491 cpnt += *cpnt + 1 + sizeof(WORD);
492 }
493 return 0;
494 }
495
496
497 /***********************************************************************
498 * NE_GetEntryPoint
499 */
500 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
501 {
502 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
503 }
504
505 /***********************************************************************
506 * NE_GetEntryPointEx
507 */
508 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
509 {
510 NE_MODULE *pModule;
511 WORD sel, offset, i;
512
513 ET_ENTRY *entry;
514 ET_BUNDLE *bundle;
515
516 if (!(pModule = NE_GetPtr( hModule ))) return 0;
517 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
518
519 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
520 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
521 {
522 if (!(bundle->next))
523 return 0;
524 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
525 }
526
527 entry = (ET_ENTRY *)((BYTE *)bundle+6);
528 for (i=0; i < (ordinal - bundle->first - 1); i++)
529 entry++;
530
531 sel = entry->segnum;
532 memcpy( &offset, &entry->offs, sizeof(WORD) );
533
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 ));
542 }
543
544
545 /***********************************************************************
546 * EntryAddrProc (KERNEL.667) Wine-specific export
547 *
548 * Return the entry point for a given ordinal.
549 */
550 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
551 {
552 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
553 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
554 return ret;
555 }
556
557 /***********************************************************************
558 * NE_SetEntryPoint
559 *
560 * Change the value of an entry point. Use with caution!
561 * It can only change the offset value, not the selector.
562 */
563 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
564 {
565 NE_MODULE *pModule;
566 ET_ENTRY *entry;
567 ET_BUNDLE *bundle;
568 int i;
569
570 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
571 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
572
573 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
574 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
575 {
576 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
577 if (!(bundle->next)) return 0;
578 }
579
580 entry = (ET_ENTRY *)((BYTE *)bundle+6);
581 for (i=0; i < (ordinal - bundle->first - 1); i++)
582 entry++;
583
584 memcpy( &entry->offs, &offset, sizeof(WORD) );
585 return TRUE;
586 }
587
588
589 /***********************************************************************
590 * build_bundle_data
591 *
592 * Build the entry table bundle data from the on-disk format. Helper for build_module.
593 */
594 static void *build_bundle_data( NE_MODULE *pModule, void *dest, const BYTE *table )
595 {
596 ET_BUNDLE *oldbundle, *bundle = dest;
597 ET_ENTRY *entry;
598 BYTE nr_entries, type;
599
600 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
601 entry = (ET_ENTRY *)((BYTE *)bundle+6);
602
603 while ((nr_entries = *table++))
604 {
605 if ((type = *table++))
606 {
607 bundle->last += nr_entries;
608 if (type == 0xff)
609 {
610 while (nr_entries--)
611 {
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++;
619 }
620 }
621 else
622 {
623 while (nr_entries--)
624 {
625 entry->type = type;
626 entry->flags = *table++;
627 entry->segnum = type;
628 entry->offs = *(const WORD *)table;
629 table += sizeof(WORD);
630 entry++;
631 }
632 }
633 }
634 else
635 {
636 if (bundle->first == bundle->last)
637 {
638 bundle->first += nr_entries;
639 bundle->last += nr_entries;
640 }
641 else
642 {
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));
649 }
650 }
651 }
652 return entry;
653 }
654
655
656 /***********************************************************************
657 * build_module
658 *
659 * Build the in-memory module from the on-disk data.
660 */
661 static HMODULE16 build_module( const void *mapping, SIZE_T mapping_size, LPCSTR path )
662 {
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;
673
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)
680 {
681 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path);
682 return 12;
683 }
684 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE) return ERROR_BAD_FORMAT;
685
686 /* We now have a valid NE header */
687
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;
694
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;
713
714 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
715 if (!hModule) return ERROR_BAD_FORMAT;
716
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;
724
725 pModule->self = hModule;
726 pModule->mapping = mapping;
727 pModule->mapping_size = mapping_size;
728
729 pData = (BYTE *)(pModule + 1);
730
731 /* Clear internal Wine flags in case they are set in the EXE file */
732
733 pModule->ne_flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
734
735 /* Get the segment table */
736
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++)
742 {
743 memcpy( pData, pSeg, sizeof(*pSeg) );
744 pData += sizeof(SEGTABLEENTRY);
745 }
746
747 /* Get the resource table */
748
749 if (ne_header->ne_rsrctab < ne_header->ne_restab)
750 {
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;
755 }
756 else pModule->ne_rsrctab = 0; /* No resource table */
757
758 /* Get the resident names table */
759
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;
764
765 /* Get the module references table */
766
767 if (ne_header->ne_cmod > 0)
768 {
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);
773 }
774 else pModule->ne_modtab = 0; /* No module references */
775
776 /* Get the imported names table */
777
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;
782
783 /* Load entry table, convert it to the optimized version used by Windows */
784
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 );
789
790 pData += ne_header->ne_cbenttab + sizeof(ET_BUNDLE) +
791 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6);
792
793 if (end > pData)
794 {
795 FIXME( "not enough space for entry table for %s\n", debugstr_a(path) );
796 goto failed;
797 }
798
799 /* Store the filename information */
800
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 );
808
809 /* Get the non-resident names table */
810
811 if (ne_header->ne_cbnrestab)
812 {
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 ))
818 {
819 GlobalFree16( pModule->nrname_handle );
820 goto failed;
821 }
822 }
823 else pModule->nrname_handle = 0;
824
825 /* Allocate a segment for the implicitly-loaded DLLs */
826
827 if (pModule->ne_cmod)
828 {
829 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
830 (pModule->ne_cmod+1)*sizeof(HMODULE16) );
831 if (!pModule->dlls_to_init)
832 {
833 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
834 goto failed;
835 }
836 FarSetOwner16( pModule->dlls_to_init, hModule );
837 }
838 else pModule->dlls_to_init = 0;
839
840 NE_RegisterModule( pModule );
841 return hModule;
842
843 failed:
844 GlobalFree16( hModule );
845 return ERROR_BAD_FORMAT;
846 }
847
848
849 /***********************************************************************
850 * NE_LoadDLLs
851 *
852 * Load all DLLs implicitly linked to a module.
853 */
854 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
855 {
856 int i;
857 WORD *pModRef = (WORD *)((char *)pModule + pModule->ne_modtab);
858 WORD *pDLLs = GlobalLock16( pModule->dlls_to_init );
859
860 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
861 {
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 */
866
867 TRACE("Loading '%s'\n", buffer );
868 if (!(*pModRef = GetModuleHandle16( buffer )))
869 {
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;
873
874 /* Append .DLL to name if no extension present */
875 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
876 strcat( buffer, ".DLL" );
877
878 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
879 {
880 /* FIXME: cleanup what was done */
881
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;
886 }
887 *pModRef = GetExePtr( hDLL );
888 *pDLLs++ = *pModRef;
889 }
890 else /* Increment the reference count of the DLL */
891 {
892 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
893 if (pOldDLL) pOldDLL->count++;
894 }
895 }
896 return TRUE;
897 }
898
899
900 /**********************************************************************
901 * NE_DoLoadModule
902 *
903 * Load first instance of NE module from file.
904 *
905 * pModule must point to a module structure prepared by build_module.
906 * This routine must never be called twice on a module.
907 */
908 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
909 {
910 /* Allocate the segments for this module */
911
912 if (!NE_CreateAllSegments( pModule ))
913 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
914
915 /* Load the referenced DLLs */
916
917 if (!NE_LoadDLLs( pModule ))
918 return ERROR_FILE_NOT_FOUND; /* 2 */
919
920 /* Load the segments */
921
922 NE_LoadAllSegments( pModule );
923
924 /* Make sure the usage count is 1 on the first loading of */
925 /* the module, even if it contains circular DLL references */
926
927 pModule->count = 1;
928
929 return NE_GetInstance( pModule );
930 }
931
932 /**********************************************************************
933 * NE_LoadModule
934 *
935 * Load first instance of NE module. (Note: caller is responsible for
936 * ensuring the module isn't already loaded!)
937 *
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.
942 *
943 * If lib_only is TRUE, however, the module is perforce treated
944 * like a DLL module, even if it is an executable module.
945 *
946 */
947 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
948 {
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;
957
958 /* Open file */
959 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
960 return ERROR_FILE_NOT_FOUND;
961
962 mapping = CreateFileMappingW( DosFileHandleToWin32Handle(hFile), NULL, PAGE_WRITECOPY, 0, 0, NULL );
963 _lclose16( hFile );
964 if (!mapping) return ERROR_BAD_FORMAT;
965
966 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 0 );
967 CloseHandle( mapping );
968 if (!ptr) return ERROR_BAD_FORMAT;
969
970 VirtualQuery( ptr, &info, sizeof(info) );
971 hModule = build_module( ptr, info.RegionSize, ofs.szPathName );
972
973 if (hModule < 32)
974 {
975 UnmapViewOfFile( ptr );
976 return hModule;
977 }
978
979 SNOOP16_RegisterDLL( hModule, ofs.szPathName );
980 NE_InitResourceHandler( hModule );
981
982 pModule = NE_GetPtr( hModule );
983
984 if ( !lib_only && !( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) )
985 return hModule;
986
987 hInstance = NE_DoLoadModule( pModule );
988 if ( hInstance < 32 )
989 {
990 /* cleanup ... */
991 NE_FreeModule( hModule, 0 );
992 }
993
994 return hInstance;
995 }
996
997
998 /***********************************************************************
999 * NE_DoLoadBuiltinModule
1000 *
1001 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1002 */
1003 static HMODULE16 NE_DoLoadBuiltinModule( const IMAGE_DOS_HEADER *mz_header, const char *file_name,
1004 HMODULE owner32 )
1005 {
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... */
1011
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;
1017
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 );
1022
1023 hInstance = NE_DoLoadModule( pModule );
1024 if (hInstance < 32) NE_FreeModule( hModule, 0 );
1025
1026 NE_InitResourceHandler( hModule );
1027
1028 if (pModule->ne_heap)
1029 {
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 );
1034 }
1035
1036 patch_code_segment( pModule );
1037
1038 return hInstance;
1039 }
1040
1041
1042 /**********************************************************************
1043 * MODULE_LoadModule16
1044 *
1045 * Load a NE module in the order of the loadorder specification.
1046 * The caller is responsible that the module is not loaded already.
1047 *
1048 */
1049 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1050 {
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;
1060
1061 /* strip path information */
1062
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;
1067
1068 if (strlen(basename) < sizeof(dllname)-6)
1069 {
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;
1074
1075 strcpy( p, "16" );
1076 if ((mod32 = LoadLibraryA( dllname )))
1077 {
1078 if (!(descr = (void *)GetProcAddress( mod32, "__wine_spec_dos_header" )))
1079 {
1080 WARN( "loaded %s but does not contain a 16-bit module\n", debugstr_a(dllname) );
1081 FreeLibrary( mod32 );
1082 }
1083 else
1084 {
1085 TRACE( "found %s with embedded 16-bit module\n", debugstr_a(dllname) );
1086 file_name = basename;
1087
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" )))
1090 {
1091 LDR_MODULE *ldr;
1092 HMODULE main_owner = LoadLibraryA( main_module );
1093
1094 if (!main_owner)
1095 {
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;
1099 }
1100 /* check if module was loaded native */
1101 if (LdrFindEntryForAddress( main_owner, &ldr ) || !(ldr->Flags & LDR_WINE_INTERNAL))
1102 {
1103 FreeLibrary( mod32 );
1104 descr = NULL;
1105 }
1106 FreeLibrary( main_owner );
1107 }
1108 }
1109 }
1110 *p = 0;
1111
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)
1114 {
1115 mod32 = LoadLibraryA( owner );
1116 if (mod32)
1117 {
1118 if (!(descr = find_dll_descr( dllname, &file_name )))
1119 {
1120 FreeLibrary( mod32 );
1121 owner_exists = 0;
1122 }
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 )))
1126 {
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;
1132 }
1133 }
1134 else
1135 {
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;
1139 }
1140 }
1141 }
1142
1143 if (descr)
1144 {
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));
1148 }
1149 else
1150 {
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 */
1155 }
1156
1157 if (hinst > 32 && !implicit)
1158 {
1159 hModule = GetModuleHandle16(libname);
1160 if(!hModule)
1161 {
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;
1165 }
1166
1167 pModule = NE_GetPtr(hModule);
1168 if(!pModule)
1169 {
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;
1173 }
1174
1175 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1176
1177 /*
1178 * Call initialization routines for all loaded DLLs. Note that
1179 * when we load implicitly linked DLLs this will be done by InitTask().
1180 */
1181 if(pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1182 {
1183 NE_InitializeDLLs(hModule);
1184 NE_DllProcessAttach(hModule);
1185 }
1186 }
1187 return hinst; /* The last error that occurred */
1188 }
1189
1190
1191 /**********************************************************************
1192 * NE_CreateThread
1193 *
1194 * Create the thread for a 16-bit module.
1195 */
1196 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1197 {
1198 HANDLE hThread;
1199 TDB *pTask;
1200 HTASK16 hTask;
1201 HINSTANCE16 instance = 0;
1202
1203 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1204 return 0;
1205
1206 /* Post event to start the task */
1207 PostEvent16( hTask );
1208
1209 /* Wait until we get the instance handle */
1210 do
1211 {
1212 DirectedYield16( hTask );
1213 if (!IsTask16( hTask )) /* thread has died */
1214 {
1215 DWORD exit_code;
1216 WaitForSingleObject( hThread, INFINITE );
1217 GetExitCodeThread( hThread, &exit_code );
1218 CloseHandle( hThread );
1219 return exit_code;
1220 }
1221 if (!(pTask = GlobalLock16( hTask ))) break;
1222 instance = pTask->hInstance;
1223 GlobalUnlock16( hTask );
1224 } while (!instance);
1225
1226 CloseHandle( hThread );
1227 return instance;
1228 }
1229
1230
1231 /**********************************************************************
1232 * LoadModule (KERNEL.45)
1233 */
1234 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1235 {
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 */
1242
1243 if (name == NULL) return 0;
1244
1245 TRACE("name %s, paramBlock %p\n", name, paramBlock);
1246
1247 /* Load module */
1248
1249 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1250 {
1251 /* Special case: second instance of an already loaded NE module */
1252
1253 if ( !( pModule = NE_GetPtr( hModule ) ) ) return ERROR_BAD_FORMAT;
1254 if ( pModule->module32 ) return (HINSTANCE16)21;
1255
1256 /* Increment refcount */
1257
1258 pModule->count++;
1259 }
1260 else
1261 {
1262 /* Main case: load first instance of NE module */
1263
1264 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1265 return hModule;
1266
1267 if ( !(pModule = NE_GetPtr( hModule )) )
1268 return ERROR_BAD_FORMAT;
1269 }
1270
1271 /* If library module, we just retrieve the instance handle */
1272
1273 if ( ( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1274 return NE_GetInstance( pModule );
1275
1276 /*
1277 * At this point, we need to create a new process.
1278 *
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.
1283 */
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 );
1289 }
1290
1291
1292 /**********************************************************************
1293 * NE_StartTask
1294 *
1295 * Startup code for a new 16-bit task.
1296 */
1297 DWORD NE_StartTask(void)
1298 {
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;
1304
1305 if ( pModule->count > 0 )
1306 {
1307 /* Second instance of an already loaded NE module */
1308 /* Note that the refcount was already incremented by the parent */
1309
1310 hPrevInstance = NE_GetInstance( pModule );
1311
1312 if ( pModule->ne_autodata )
1313 if ( NE_CreateSegment( pModule, pModule->ne_autodata ) )
1314 NE_LoadSegment( pModule, pModule->ne_autodata );
1315
1316 hInstance = NE_GetInstance( pModule );
1317 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->ne_autodata, hPrevInstance);
1318
1319 }
1320 else
1321 {
1322 /* Load first instance of NE module */
1323
1324 pModule->ne_flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1325
1326 hInstance = NE_DoLoadModule( pModule );
1327 hPrevInstance = 0;
1328 }
1329
1330 if ( hInstance >= 32 )
1331 {
1332 CONTEXT86 context;
1333
1334 /* Enter instance handles into task struct */
1335
1336 pTask->hInstance = hInstance;
1337 pTask->hPrevInstance = hPrevInstance;
1338
1339 /* Use DGROUP for 16-bit stack */
1340
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 );
1346
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
1358 */
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;
1370
1371 /* Now call 16-bit entry point */
1372
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) );
1377
1378 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1379 ExitThread( LOWORD(context.Eax) );
1380 }
1381 return hInstance; /* error code */
1382 }
1383
1384 /***********************************************************************
1385 * LoadLibrary (KERNEL.95)
1386 * LoadLibrary16 (KERNEL32.35)
1387 */
1388 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1389 {
1390 return LoadModule16(libname, (LPVOID)-1 );
1391 }
1392
1393
1394 /**********************************************************************
1395 * MODULE_CallWEP
1396 *
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
1399 */
1400 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1401 {
1402 BOOL16 ret;
1403 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1404 if (!WEP) return FALSE;
1405
1406 __TRY
1407 {
1408 WORD args[1];
1409 DWORD dwRet;
1410
1411 args[0] = WEP_FREE_DLL;
1412 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1413 ret = LOWORD(dwRet);
1414 }
1415 __EXCEPT_PAGE_FAULT
1416 {
1417 WARN("Page fault\n");
1418 ret = 0;
1419 }
1420 __ENDTRY
1421
1422 return ret;
1423 }
1424
1425
1426 /**********************************************************************
1427 * NE_FreeModule
1428 *
1429 * Implementation of FreeModule16().
1430 */
1431 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1432 {
1433 HMODULE16 *hPrevModule;
1434 NE_MODULE *pModule;
1435 HMODULE16 *pModRef;
1436 int i;
1437
1438 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1439 hModule = pModule->self;
1440
1441 TRACE("%04x count %d\n", hModule, pModule->count );
1442
1443 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1444 else pModule->count = 0;
1445
1446 if (call_wep && !(pModule->ne_flags & NE_FFLAGS_WIN32))
1447 {
1448 /* Free the objects owned by the DLL module */
1449 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1450
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 */
1455 }
1456
1457 TRACE_(loaddll)("Unloaded module %s : %s\n", debugstr_a(NE_MODULE_NAME(pModule)),
1458 (pModule->ne_flags & NE_FFLAGS_BUILTIN) ? "builtin" : "native");
1459
1460 /* Clear magic number just in case */
1461
1462 pModule->ne_magic = pModule->self = 0;
1463 if (pModule->owner32) FreeLibrary( pModule->owner32 );
1464 else if (pModule->mapping) UnmapViewOfFile( pModule->mapping );
1465
1466 /* Remove it from the linked list */
1467
1468 hPrevModule = &hFirstModule;
1469 while (*hPrevModule && (*hPrevModule != hModule))
1470 {
1471 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1472 }
1473 if (*hPrevModule) *hPrevModule = pModule->next;
1474
1475 /* Free the referenced modules */
1476
1477 pModRef = (HMODULE16*)((char *)pModule + pModule->ne_modtab);
1478 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
1479 {
1480 NE_FreeModule( *pModRef, call_wep );
1481 }
1482
1483 /* Free the module storage */
1484
1485 GlobalFreeAll16( hModule );
1486 return TRUE;
1487 }
1488
1489
1490 /**********************************************************************
1491 * FreeModule (KERNEL.46)
1492 */
1493 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1494 {
1495 return NE_FreeModule( hModule, TRUE );
1496 }
1497
1498
1499 /***********************************************************************
1500 * FreeLibrary (KERNEL.96)
1501 * FreeLibrary16 (KERNEL32.36)
1502 */
1503 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1504 {
1505 TRACE("%04x\n", handle );
1506 FreeModule16( handle );
1507 }
1508
1509
1510 /***********************************************************************
1511 * GetModuleHandle16 (KERNEL32.@)
1512 */
1513 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1514 {
1515 HMODULE16 hModule = hFirstModule;
1516 LPSTR s;
1517 BYTE len, *name_table;
1518 char tmpstr[MAX_PATH];
1519 NE_MODULE *pModule;
1520
1521 TRACE("(%s)\n", name);
1522
1523 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1524
1525 len = strlen(name);
1526 if (!len) return 0;
1527
1528 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1529
1530 /* If 'name' matches exactly the module name of a module:
1531 * Return its handle.
1532 */
1533 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1534 {
1535 pModule = NE_GetPtr( hModule );
1536 if (!pModule) break;
1537 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1538
1539 name_table = (BYTE *)pModule + pModule->ne_restab;
1540 if ((*name_table == len) && !strncmp(name, (char*) name_table+1, len))
1541 return hModule;
1542 }
1543
1544 /* If uppercased 'name' matches exactly the module name of a module:
1545 * Return its handle
1546 */
1547 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1548
1549 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1550 {
1551 pModule = NE_GetPtr( hModule );
1552 if (!pModule) break;
1553 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1554
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
1561 */
1562 if ((*name_table == len) && !NE_strncasecmp(tmpstr, (const char*)name_table+1, len))
1563 return hModule;
1564 }
1565
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.
1569 */
1570
1571 /* basename: search backwards in passed name to \ / or : */
1572 s = tmpstr + strlen(tmpstr);
1573 while (s > tmpstr)
1574 {
1575 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1576 break;
1577 s--;
1578 }
1579
1580 /* search this in loaded filename list */
1581 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1582 {
1583 char *loadedfn;
1584 OFSTRUCT *ofs;
1585
1586 pModule = NE_GetPtr( hModule );
1587 if (!pModule) break;
1588 if (!pModule->fileinfo) continue;
1589 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1590
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)
1595 {
1596 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1597 break;
1598 loadedfn--;
1599 }
1600 /* case insensitive compare ... */
1601 if (!NE_strcasecmp(loadedfn, s))
1602 return hModule;
1603 }
1604 return 0;
1605 }
1606
1607
1608 /**********************************************************************
1609 * GetModuleName (KERNEL.27)
1610 */
1611 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1612 {
1613 NE_MODULE *pModule;
1614 BYTE *p;
1615
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)
1620 {
1621 memcpy( buf, p + 1, count - 1 );
1622 buf[count-1] = '\0';
1623 }
1624 return TRUE;
1625 }
1626
1627
1628 /**********************************************************************
1629 * GetModuleFileName (KERNEL.49)
1630 *
1631 * Comment: see GetModuleFileNameA
1632 *
1633 * Even if invoked by second instance of a program,
1634 * it still returns path of first one.
1635 */
1636 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1637 INT16 nSize )
1638 {
1639 NE_MODULE *pModule;
1640
1641 /* Win95 does not query hModule if set to 0 !
1642 * Is this wrong or maybe Win3.1 only ? */
1643 if (!hModule) hModule = GetCurrentTask();
1644
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);
1651 }
1652
1653
1654 /**********************************************************************
1655 * GetModuleUsage (KERNEL.48)
1656 */
1657 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1658 {
1659 NE_MODULE *pModule = NE_GetPtr( hModule );
1660 return pModule ? pModule->count : 0;
1661 }
1662
1663
1664 /**********************************************************************
1665 * GetExpWinVer (KERNEL.167)
1666 */
1667 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1668 {
1669 NE_MODULE *pModule = NE_GetPtr( hModule );
1670 if ( !pModule ) return 0;
1671 return pModule->ne_expver;
1672 }
1673
1674
1675 /***********************************************************************
1676 * WinExec (KERNEL.166)
1677 */
1678 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1679 {
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];
1688
1689 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1690 {
1691 name_beg = lpCmdLine+1;
1692 p = strchr ( lpCmdLine+1, '"' );
1693 if (p)
1694 {
1695 name_end = p;
1696 args = strchr ( p, ' ' );
1697 }
1698 else /* yes, even valid with trailing '"' missing */
1699 name_end = lpCmdLine+strlen(lpCmdLine);
1700 }
1701 else
1702 {
1703 name_beg = lpCmdLine;
1704 args = strchr( lpCmdLine, ' ' );
1705 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1706 }
1707
1708 if ((name_beg == lpCmdLine) && (!args))
1709 { /* just use the original cmdline string as file name */
1710 name = (LPSTR)lpCmdLine;
1711 }
1712 else
1713 {
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';
1718 }
1719
1720 if (args)
1721 {
1722 args++;
1723 arglen = strlen(args);
1724 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1725 cmdline[0] = (BYTE)arglen;
1726 strcpy( cmdline + 1, args );
1727 }
1728 else
1729 {
1730 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1731 cmdline[0] = cmdline[1] = 0;
1732 }
1733
1734 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1735
1736 showCmd[0] = 2;
1737 showCmd[1] = nCmdShow;
1738
1739 params.hEnvironment = 0;
1740 params.cmdLine = MapLS( cmdline );
1741 params.showCmd = MapLS( showCmd );
1742 params.reserved = 0;
1743
1744 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1745 {
1746 ret = LoadModule16( buffer, ¶ms );
1747 }
1748 else if (!contains_path( name )) /* try 16-bit builtin */
1749 {
1750 lstrcpynA( buffer, name, sizeof(buffer) );
1751 if (strlen( buffer ) < sizeof(buffer) - 4 && !strchr( buffer, '.' )) strcat( buffer, ".exe" );
1752 ret = LoadModule16( buffer, ¶ms );
1753 if (ret == ERROR_FILE_NOT_FOUND) ret = 21; /* it might be a 32-bit builtin too */
1754 }
1755 else ret = ERROR_FILE_NOT_FOUND;
1756
1757 UnMapLS( params.cmdLine );
1758 UnMapLS( params.showCmd );
1759
1760 HeapFree( GetProcessHeap(), 0, cmdline );
1761 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1762
1763 if (ret == 21 || ret == ERROR_BAD_FORMAT) /* 32-bit module or unknown executable*/
1764 {
1765 LOADPARAMS16 params;
1766 WORD showCmd[2];
1767 showCmd[0] = 2;
1768 showCmd[1] = nCmdShow;
1769
1770 arglen = strlen( lpCmdLine );
1771 cmdline = HeapAlloc( GetProcessHeap(), 0, arglen + 1 );
1772 cmdline[0] = (BYTE)arglen;
1773 memcpy( cmdline + 1, lpCmdLine, arglen );
1774
1775 params.hEnvironment = 0;
1776 params.cmdLine = MapLS( cmdline );
1777 params.showCmd = MapLS( showCmd );
1778 params.reserved = 0;
1779
1780 ret = LoadModule16( "winoldap.mod", ¶ms );
1781 UnMapLS( params.cmdLine );
1782 UnMapLS( params.showCmd );
1783 }
1784 return ret;
1785 }
1786
1787 /***********************************************************************
1788 * GetProcAddress (KERNEL.50)
1789 */
1790 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1791 {
1792 WORD ordinal;
1793 FARPROC16 ret;
1794
1795 if (!hModule) hModule = GetCurrentTask();
1796 hModule = GetExePtr( hModule );
1797
1798 if (HIWORD(name) != 0)
1799 {
1800 ordinal = NE_GetOrdinal( hModule, name );
1801 TRACE("%04x '%s'\n", hModule, name );
1802 }
1803 else
1804 {
1805 ordinal = LOWORD(name);
1806 TRACE("%04x %04x\n", hModule, ordinal );
1807 }
1808 if (!ordinal) return NULL;
1809
1810 ret = NE_GetEntryPoint( hModule, ordinal );
1811
1812 TRACE("returning %p\n", ret );
1813 return ret;
1814 }
1815
1816
1817 /***************************************************************************
1818 * HasGPHandler (KERNEL.338)
1819 */
1820 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1821 {
1822 HMODULE16 hModule;
1823 int gpOrdinal;
1824 SEGPTR gpPtr;
1825 GPHANDLERDEF *gpHandler;
1826
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 )
1832 {
1833 while (gpHandler->selector)
1834 {
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++;
1840 }
1841 }
1842
1843 return 0;
1844 }
1845
1846
1847 /**********************************************************************
1848 * GetModuleHandle (KERNEL.47)
1849 *
1850 * Find a module from a module name.
1851 *
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)
1856 *
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
1863 */
1864 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1865 {
1866 if (HIWORD(name) == 0)
1867 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1868 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1869 }
1870
1871 /**********************************************************************
1872 * NE_GetModuleByFilename
1873 */
1874 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1875 {
1876 HMODULE16 hModule;
1877 LPSTR s, p;
1878 BYTE len, *name_table;
1879 char tmpstr[MAX_PATH];
1880 NE_MODULE *pModule;
1881
1882 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1883
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.
1887 */
1888
1889 /* basename: search backwards in passed name to \ / or : */
1890 s = tmpstr + strlen(tmpstr);
1891 while (s > tmpstr)
1892 {
1893 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1894 break;
1895 s--;
1896 }
1897
1898 /* search this in loaded filename list */
1899 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1900 {
1901 char *loadedfn;
1902 OFSTRUCT *ofs;
1903
1904 pModule = NE_GetPtr( hModule );
1905 if (!pModule) break;
1906 if (!pModule->fileinfo) continue;
1907 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1908
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)
1913 {
1914 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1915 break;
1916 loadedfn--;
1917 }
1918 /* case insensitive compare ... */
1919 if (!NE_strcasecmp(loadedfn, s))
1920 return hModule;
1921 }
1922 /* If basename (without ext) matches the module name of a module:
1923 * Return its handle.
1924 */
1925
1926 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1927 len = strlen(s);
1928
1929 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1930 {
1931 pModule = NE_GetPtr( hModule );
1932 if (!pModule) break;
1933 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1934
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;
1938 }
1939
1940 return 0;
1941 }
1942
1943 /***********************************************************************
1944 * GetProcAddress16 (KERNEL32.37)
1945 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1946 */
1947 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1948 {
1949 if (!hModule) return 0;
1950 if (HIWORD(hModule))
1951 {
1952 WARN("hModule is Win32 handle (%p)\n", hModule );
1953 return 0;
1954 }
1955 return GetProcAddress16( LOWORD(hModule), name );
1956 }
1957
1958 /**********************************************************************
1959 * ModuleFirst (TOOLHELP.59)
1960 */
1961 BOOL16 WINAPI ModuleFirst16( MODULEENTRY *lpme )
1962 {
1963 lpme->wNext = hFirstModule;
1964 return ModuleNext16( lpme );
1965 }
1966
1967
1968 /**********************************************************************
1969 * ModuleNext (TOOLHELP.60)
1970 */
1971 BOOL16 WINAPI ModuleNext16( MODULEENTRY *lpme )
1972 {
1973 NE_MODULE *pModule;
1974 char *name;
1975
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;
1986 }
1987
1988
1989 /**********************************************************************
1990 * ModuleFindName (TOOLHELP.61)
1991 */
1992 BOOL16 WINAPI ModuleFindName16( MODULEENTRY *lpme, LPCSTR name )
1993 {
1994 lpme->wNext = GetModuleHandle16( name );
1995 return ModuleNext16( lpme );
1996 }
1997
1998
1999 /**********************************************************************
2000 * ModuleFindHandle (TOOLHELP.62)
2001 */
2002 BOOL16 WINAPI ModuleFindHandle16( MODULEENTRY *lpme, HMODULE16 hModule )
2003 {
2004 hModule = GetExePtr( hModule );
2005 lpme->wNext = hModule;
2006 return ModuleNext16( lpme );
2007 }
2008
2009
2010 /***************************************************************************
2011 * IsRomModule (KERNEL.323)
2012 */
2013 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
2014 {
2015 return FALSE;
2016 }
2017
2018 /***************************************************************************
2019 * IsRomFile (KERNEL.326)
2020 */
2021 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
2022 {
2023 return FALSE;
2024 }
2025
2026 /***********************************************************************
2027 * create_dummy_module
2028 *
2029 * Create a dummy NE module for Win32 or Winelib.
2030 */
2031 static HMODULE16 create_dummy_module( HMODULE module32 )
2032 {
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 );
2043
2044 if (!nt) return ERROR_BAD_FORMAT;
2045
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;
2054
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 */
2066 8;
2067
2068 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2069 if (!hModule) return ERROR_BAD_FORMAT;
2070
2071 FarSetOwner16( hModule, hModule );
2072 pModule = GlobalLock16( hModule );
2073
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;
2091
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;
2097
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 );
2103
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++;
2114
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;
2122
2123 /* All tables zero terminated */
2124 pModule->ne_rsrctab = pModule->ne_imptab = pModule->ne_enttab = pStr - (char *)pModule;
2125
2126 NE_RegisterModule( pModule );
2127 pModule->owner32 = LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2128 return hModule;
2129 }
2130
2131 /***********************************************************************
2132 * PrivateLoadLibrary (KERNEL32.@)
2133 *
2134 * FIXME: rough guesswork, don't know what "Private" means
2135 */
2136 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2137 {
2138 return LoadLibrary16(libname);
2139 }
2140
2141 /***********************************************************************
2142 * PrivateFreeLibrary (KERNEL32.@)
2143 *
2144 * FIXME: rough guesswork, don't know what "Private" means
2145 */
2146 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2147 {
2148 FreeLibrary16(handle);
2149 }
2150
2151 /***********************************************************************
2152 * LoadLibrary32 (KERNEL.452)
2153 * LoadSystemLibrary32 (KERNEL.482)
2154 */
2155 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2156 {
2157 HMODULE hModule;
2158 DWORD count;
2159
2160 ReleaseThunkLock( &count );
2161 hModule = LoadLibraryA( libname );
2162 RestoreThunkLock( count );
2163 return hModule;
2164 }
2165
2166 /***************************************************************************
2167 * MapHModuleLS (KERNEL32.@)
2168 */
2169 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2170 {
2171 HMODULE16 ret;
2172 NE_MODULE *pModule;
2173
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);
2183 }
2184 if ((ret = create_dummy_module( hmod )) < 32)
2185 {
2186 SetLastError(ret);
2187 ret = 0;
2188 }
2189 return ret;
2190 }
2191
2192 /***************************************************************************
2193 * MapHModuleSL (KERNEL32.@)
2194 */
2195 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2196 {
2197 NE_MODULE *pModule;
2198
2199 if (!hmod) {
2200 TDB *pTask = TASK_GetCurrent();
2201 hmod = pTask->hModule;
2202 }
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;
2207 }
2208
2209 /***************************************************************************
2210 * MapHInstLS (KERNEL.472)
2211 */
2212 void WINAPI MapHInstLS16( CONTEXT86 *context )
2213 {
2214 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2215 }
2216
2217 /***************************************************************************
2218 * MapHInstSL (KERNEL.473)
2219 */
2220 void WINAPI MapHInstSL16( CONTEXT86 *context )
2221 {
2222 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2223 }
2224
2225 #ifdef __i386__
2226
2227 /***************************************************************************
2228 * MapHInstLS (KERNEL32.@)
2229 */
2230 __ASM_STDCALL_FUNC( MapHInstLS, 0,
2231 "pushl %eax\n\t"
2232 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n\t"
2233 "ret" )
2234
2235 /***************************************************************************
2236 * MapHInstSL (KERNEL32.@)
2237 */
2238 __ASM_STDCALL_FUNC( MapHInstSL, 0,
2239 "pushl %eax\n\t"
2240 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n\t"
2241 "ret" )
2242
2243 /***************************************************************************
2244 * MapHInstLS_PN (KERNEL32.@)
2245 */
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" )
2252
2253 /***************************************************************************
2254 * MapHInstSL_PN (KERNEL32.@)
2255 */
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" )
2262
2263 #endif /* __i386__ */
2264
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.