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 "kernel_private.h"
40 #include "kernel16_private.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(module);
45 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
47
48 #include "pshpack1.h"
49 typedef struct _GPHANDLERDEF
50 {
51 WORD selector;
52 WORD rangeStart;
53 WORD rangeEnd;
54 WORD handler;
55 } GPHANDLERDEF;
56 #include "poppack.h"
57
58 /*
59 * Segment table entry
60 */
61 struct ne_segment_table_entry_s
62 {
63 WORD seg_data_offset; /* Sector offset of segment data */
64 WORD seg_data_length; /* Length of segment data */
65 WORD seg_flags; /* Flags associated with this segment */
66 WORD min_alloc; /* Minimum allocation size for this */
67 };
68
69 #define hFirstModule (pThhook->hExeHead)
70
71 struct builtin_dll
72 {
73 const IMAGE_DOS_HEADER *header; /* module headers */
74 const char *file_name; /* module file name */
75 };
76
77 /* Table of all built-in DLLs */
78
79 #define MAX_DLLS 50
80
81 static struct builtin_dll builtin_dlls[MAX_DLLS];
82
83 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only );
84 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep );
85
86 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only );
87
88 static HMODULE16 NE_GetModuleByFilename( LPCSTR name );
89
90
91 /* patch all the flat cs references of the code segment if necessary */
92 static inline void patch_code_segment( NE_MODULE *pModule )
93 {
94 #ifdef __i386__
95 int i;
96 CALLFROM16 *call;
97 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule );
98
99 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
100 if (!(pSeg->flags & NE_SEGFLAGS_DATA)) break; /* found the code segment */
101
102 call = GlobalLock16( pSeg->hSeg );
103
104 /* patch glue code address and code selector */
105 for (i = 0; call[i].pushl == 0x68; i++)
106 {
107 if (call[i].ret[0] == 0xca66 || call[i].ret[0] == 0xcb66) /* register entry point? */
108 call[i].glue = __wine_call_from_16_regs;
109 else
110 call[i].glue = __wine_call_from_16;
111 call[i].flatcs = wine_get_cs();
112 }
113
114 if (TRACE_ON(relay)) /* patch relay functions to all point to relay_call_from_16 */
115 for (i = 0; call[i].pushl == 0x68; i++) call[i].relay = relay_call_from_16;
116 #endif
117 }
118
119
120 /***********************************************************************
121 * contains_path
122 */
123 static inline int contains_path( LPCSTR name )
124 {
125 return ((*name && (name[1] == ':')) || strchr(name, '/') || strchr(name, '\\'));
126 }
127
128
129 /***********************************************************************
130 * NE_strcasecmp
131 *
132 * locale-independent case conversion for module lookups
133 */
134 static int NE_strcasecmp( const char *str1, const char *str2 )
135 {
136 int ret = 0;
137 for ( ; ; str1++, str2++)
138 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
139 return ret;
140 }
141
142
143 /***********************************************************************
144 * NE_strncasecmp
145 *
146 * locale-independent case conversion for module lookups
147 */
148 static int NE_strncasecmp( const char *str1, const char *str2, int len )
149 {
150 int ret = 0;
151 for ( ; len > 0; len--, str1++, str2++)
152 if ((ret = RtlUpperChar(*str1) - RtlUpperChar(*str2)) || !*str1) break;
153 return ret;
154 }
155
156
157 /***********************************************************************
158 * find_dll_descr
159 *
160 * Find a descriptor in the list
161 */
162 static const IMAGE_DOS_HEADER *find_dll_descr( const char *dllname, const char **file_name )
163 {
164 int i;
165 const IMAGE_DOS_HEADER *mz_header;
166 const IMAGE_OS2_HEADER *ne_header;
167 const BYTE *name_table;
168
169 for (i = 0; i < MAX_DLLS; i++)
170 {
171 mz_header = builtin_dlls[i].header;
172 if (mz_header)
173 {
174 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mz_header + mz_header->e_lfanew);
175 name_table = (const BYTE *)ne_header + ne_header->ne_restab;
176
177 /* check the dll file name */
178 if (!NE_strcasecmp( builtin_dlls[i].file_name, dllname ) ||
179 /* check the dll module name (without extension) */
180 (!NE_strncasecmp( dllname, (const char*)name_table+1, *name_table ) &&
181 !strcmp( dllname + *name_table, ".dll" )))
182 {
183 *file_name = builtin_dlls[i].file_name;
184 return builtin_dlls[i].header;
185 }
186 }
187 }
188 return NULL;
189 }
190
191
192 /***********************************************************************
193 * __wine_dll_register_16 (KERNEL32.@)
194 *
195 * Register a built-in DLL descriptor.
196 */
197 void __wine_dll_register_16( const IMAGE_DOS_HEADER *header, const char *file_name )
198 {
199 int i;
200
201 for (i = 0; i < MAX_DLLS; i++)
202 {
203 if (builtin_dlls[i].header) continue;
204 builtin_dlls[i].header = header;
205 builtin_dlls[i].file_name = file_name;
206 break;
207 }
208 assert( i < MAX_DLLS );
209 }
210
211
212 /***********************************************************************
213 * __wine_dll_unregister_16 (KERNEL32.@)
214 *
215 * Unregister a built-in DLL descriptor.
216 */
217 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER *header )
218 {
219 int i;
220
221 for (i = 0; i < MAX_DLLS; i++)
222 {
223 if (builtin_dlls[i].header != header) continue;
224 builtin_dlls[i].header = NULL;
225 break;
226 }
227 }
228
229
230 /***********************************************************************
231 * NE_GetPtr
232 */
233 NE_MODULE *NE_GetPtr( HMODULE16 hModule )
234 {
235 return GlobalLock16( GetExePtr(hModule) );
236 }
237
238
239 /**********************************************************************
240 * NE_RegisterModule
241 */
242 static void NE_RegisterModule( NE_MODULE *pModule )
243 {
244 pModule->next = hFirstModule;
245 hFirstModule = pModule->self;
246 }
247
248
249 /***********************************************************************
250 * NE_DumpModule
251 */
252 void NE_DumpModule( HMODULE16 hModule )
253 {
254 int i, ordinal;
255 SEGTABLEENTRY *pSeg;
256 BYTE *pstr;
257 WORD *pword;
258 NE_MODULE *pModule;
259 ET_BUNDLE *bundle;
260 ET_ENTRY *entry;
261
262 if (!(pModule = NE_GetPtr( hModule )))
263 {
264 ERR( "**** %04x is not a module handle\n", hModule );
265 return;
266 }
267
268 /* Dump the module info */
269 TRACE( "---\n" );
270 TRACE( "Module %04x:\n", hModule );
271 TRACE( "count=%d flags=%04x heap=%d stack=%d\n",
272 pModule->count, pModule->ne_flags,
273 pModule->ne_heap, pModule->ne_stack );
274 TRACE( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
275 SELECTOROF(pModule->ne_csip), OFFSETOF(pModule->ne_csip),
276 SELECTOROF(pModule->ne_sssp), OFFSETOF(pModule->ne_sssp),
277 pModule->ne_autodata, pModule->ne_cseg, pModule->ne_cmod );
278 TRACE( "os_flags=%d swap_area=%d version=%04x\n",
279 pModule->ne_exetyp, pModule->ne_swaparea, pModule->ne_expver );
280 if (pModule->ne_flags & NE_FFLAGS_WIN32)
281 TRACE( "PE module=%p\n", pModule->module32 );
282
283 /* Dump the file info */
284 TRACE( "---\n" );
285 TRACE( "Filename: '%s'\n", NE_MODULE_NAME(pModule) );
286
287 /* Dump the segment table */
288 TRACE( "---\n" );
289 TRACE( "Segment table:\n" );
290 pSeg = NE_SEG_TABLE( pModule );
291 for (i = 0; i < pModule->ne_cseg; i++, pSeg++)
292 TRACE( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
293 i + 1, pSeg->filepos, pSeg->size, pSeg->flags,
294 pSeg->minsize, pSeg->hSeg );
295
296 /* Dump the resource table */
297 TRACE( "---\n" );
298 TRACE( "Resource table:\n" );
299 if (pModule->ne_rsrctab)
300 {
301 pword = (WORD *)((BYTE *)pModule + pModule->ne_rsrctab);
302 TRACE( "Alignment: %d\n", *pword++ );
303 while (*pword)
304 {
305 NE_TYPEINFO *ptr = (NE_TYPEINFO *)pword;
306 NE_NAMEINFO *pname = (NE_NAMEINFO *)(ptr + 1);
307 TRACE( "id=%04x count=%d\n", ptr->type_id, ptr->count );
308 for (i = 0; i < ptr->count; i++, pname++)
309 TRACE( "offset=%d len=%d id=%04x\n",
310 pname->offset, pname->length, pname->id );
311 pword = (WORD *)pname;
312 }
313 }
314 else TRACE( "None\n" );
315
316 /* Dump the resident name table */
317 TRACE( "---\n" );
318 TRACE( "Resident-name table:\n" );
319 pstr = (BYTE*) pModule + pModule->ne_restab;
320 while (*pstr)
321 {
322 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
323 *(WORD *)(pstr + *pstr + 1) );
324 pstr += *pstr + 1 + sizeof(WORD);
325 }
326
327 /* Dump the module reference table */
328 TRACE( "---\n" );
329 TRACE( "Module ref table:\n" );
330 if (pModule->ne_modtab)
331 {
332 pword = (WORD *)((BYTE *)pModule + pModule->ne_modtab);
333 for (i = 0; i < pModule->ne_cmod; i++, pword++)
334 {
335 char name[10];
336 GetModuleName16( *pword, name, sizeof(name) );
337 TRACE( "%d: %04x -> '%s'\n", i, *pword, name );
338 }
339 }
340 else TRACE( "None\n" );
341
342 /* Dump the entry table */
343 TRACE( "---\n" );
344 TRACE( "Entry table:\n" );
345 bundle = (ET_BUNDLE *)((BYTE *)pModule+pModule->ne_enttab);
346 do {
347 entry = (ET_ENTRY *)((BYTE *)bundle+6);
348 TRACE( "Bundle %d-%d: %02x\n", bundle->first, bundle->last, entry->type);
349 ordinal = bundle->first;
350 while (ordinal < bundle->last)
351 {
352 if (entry->type == 0xff)
353 TRACE("%d: %02x:%04x (moveable)\n", ordinal++, entry->segnum, entry->offs);
354 else
355 TRACE("%d: %02x:%04x (fixed)\n", ordinal++, entry->segnum, entry->offs);
356 entry++;
357 }
358 } while ( (bundle->next) && (bundle = ((ET_BUNDLE *)((BYTE *)pModule + bundle->next))) );
359
360 /* Dump the non-resident names table */
361 TRACE( "---\n" );
362 TRACE( "Non-resident names table:\n" );
363 if (pModule->nrname_handle)
364 {
365 pstr = GlobalLock16( pModule->nrname_handle );
366 while (*pstr)
367 {
368 TRACE( "%*.*s: %d\n", *pstr, *pstr, pstr + 1,
369 *(WORD *)(pstr + *pstr + 1) );
370 pstr += *pstr + 1 + sizeof(WORD);
371 }
372 }
373 TRACE( "\n" );
374 }
375
376
377 /***********************************************************************
378 * NE_WalkModules
379 *
380 * Walk the module list and print the modules.
381 */
382 void NE_WalkModules(void)
383 {
384 HMODULE16 hModule = hFirstModule;
385 MESSAGE( "Module Flags Name\n" );
386 while (hModule)
387 {
388 NE_MODULE *pModule = NE_GetPtr( hModule );
389 if (!pModule)
390 {
391 MESSAGE( "Bad module %04x in list\n", hModule );
392 return;
393 }
394 MESSAGE( " %04x %04x %.*s\n", hModule, pModule->ne_flags,
395 *((char *)pModule + pModule->ne_restab),
396 (char *)pModule + pModule->ne_restab + 1 );
397 hModule = pModule->next;
398 }
399 }
400
401
402 /***********************************************************************
403 * NE_InitResourceHandler
404 *
405 * Fill in 'resloader' fields in the resource table.
406 */
407 static void NE_InitResourceHandler( HMODULE16 hModule )
408 {
409 static FARPROC16 proc;
410
411 NE_TYPEINFO *pTypeInfo;
412 NE_MODULE *pModule;
413
414 if (!(pModule = NE_GetPtr( hModule )) || !pModule->ne_rsrctab) return;
415
416 TRACE("InitResourceHandler[%04x]\n", hModule );
417
418 if (!proc) proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
419
420 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
421 while(pTypeInfo->type_id)
422 {
423 memcpy_unaligned( &pTypeInfo->resloader, &proc, sizeof(FARPROC16) );
424 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo + 1) + pTypeInfo->count * sizeof(NE_NAMEINFO));
425 }
426 }
427
428
429 /***********************************************************************
430 * NE_GetOrdinal
431 *
432 * Lookup the ordinal for a given name.
433 */
434 WORD NE_GetOrdinal( HMODULE16 hModule, const char *name )
435 {
436 char buffer[256], *p;
437 BYTE *cpnt;
438 BYTE len;
439 NE_MODULE *pModule;
440
441 if (!(pModule = NE_GetPtr( hModule ))) return 0;
442 if (pModule->ne_flags & NE_FFLAGS_WIN32) return 0;
443
444 TRACE("(%04x,'%s')\n", hModule, name );
445
446 /* First handle names of the form '#xxxx' */
447
448 if (name[0] == '#') return atoi( name + 1 );
449
450 /* Now copy and uppercase the string */
451
452 strcpy( buffer, name );
453 for (p = buffer; *p; p++) *p = RtlUpperChar(*p);
454 len = p - buffer;
455
456 /* First search the resident names */
457
458 cpnt = (BYTE *)pModule + pModule->ne_restab;
459
460 /* Skip the first entry (module name) */
461 cpnt += *cpnt + 1 + sizeof(WORD);
462 while (*cpnt)
463 {
464 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
465 {
466 WORD ordinal;
467 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
468 TRACE(" Found: ordinal=%d\n", ordinal );
469 return ordinal;
470 }
471 cpnt += *cpnt + 1 + sizeof(WORD);
472 }
473
474 /* Now search the non-resident names table */
475
476 if (!pModule->nrname_handle) return 0; /* No non-resident table */
477 cpnt = GlobalLock16( pModule->nrname_handle );
478
479 /* Skip the first entry (module description string) */
480 cpnt += *cpnt + 1 + sizeof(WORD);
481 while (*cpnt)
482 {
483 if ((*cpnt == len) && !memcmp( cpnt+1, buffer, len ))
484 {
485 WORD ordinal;
486 memcpy( &ordinal, cpnt + *cpnt + 1, sizeof(ordinal) );
487 TRACE(" Found: ordinal=%d\n", ordinal );
488 return ordinal;
489 }
490 cpnt += *cpnt + 1 + sizeof(WORD);
491 }
492 return 0;
493 }
494
495
496 /***********************************************************************
497 * NE_GetEntryPoint
498 */
499 FARPROC16 WINAPI NE_GetEntryPoint( HMODULE16 hModule, WORD ordinal )
500 {
501 return NE_GetEntryPointEx( hModule, ordinal, TRUE );
502 }
503
504 /***********************************************************************
505 * NE_GetEntryPointEx
506 */
507 FARPROC16 NE_GetEntryPointEx( HMODULE16 hModule, WORD ordinal, BOOL16 snoop )
508 {
509 NE_MODULE *pModule;
510 WORD sel, offset, i;
511
512 ET_ENTRY *entry;
513 ET_BUNDLE *bundle;
514
515 if (!(pModule = NE_GetPtr( hModule ))) return 0;
516 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
517
518 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
519 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
520 {
521 if (!(bundle->next))
522 return 0;
523 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
524 }
525
526 entry = (ET_ENTRY *)((BYTE *)bundle+6);
527 for (i=0; i < (ordinal - bundle->first - 1); i++)
528 entry++;
529
530 sel = entry->segnum;
531 memcpy( &offset, &entry->offs, sizeof(WORD) );
532
533 if (sel == 0xfe) sel = 0xffff; /* constant entry */
534 else sel = GlobalHandleToSel16(NE_SEG_TABLE(pModule)[sel-1].hSeg);
535 if (sel==0xffff)
536 return (FARPROC16)MAKESEGPTR( sel, offset );
537 if (!snoop)
538 return (FARPROC16)MAKESEGPTR( sel, offset );
539 else
540 return SNOOP16_GetProcAddress16(hModule,ordinal,(FARPROC16)MAKESEGPTR( sel, offset ));
541 }
542
543
544 /***********************************************************************
545 * EntryAddrProc (KERNEL.667) Wine-specific export
546 *
547 * Return the entry point for a given ordinal.
548 */
549 FARPROC16 WINAPI EntryAddrProc16( HMODULE16 hModule, WORD ordinal )
550 {
551 FARPROC16 ret = NE_GetEntryPointEx( hModule, ordinal, TRUE );
552 CURRENT_STACK16->ecx = hModule; /* FIXME: might be incorrect value */
553 return ret;
554 }
555
556 /***********************************************************************
557 * NE_SetEntryPoint
558 *
559 * Change the value of an entry point. Use with caution!
560 * It can only change the offset value, not the selector.
561 */
562 BOOL16 NE_SetEntryPoint( HMODULE16 hModule, WORD ordinal, WORD offset )
563 {
564 NE_MODULE *pModule;
565 ET_ENTRY *entry;
566 ET_BUNDLE *bundle;
567 int i;
568
569 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
570 assert( !(pModule->ne_flags & NE_FFLAGS_WIN32) );
571
572 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->ne_enttab);
573 while ((ordinal < bundle->first + 1) || (ordinal > bundle->last))
574 {
575 bundle = (ET_BUNDLE *)((BYTE *)pModule + bundle->next);
576 if (!(bundle->next)) return 0;
577 }
578
579 entry = (ET_ENTRY *)((BYTE *)bundle+6);
580 for (i=0; i < (ordinal - bundle->first - 1); i++)
581 entry++;
582
583 memcpy( &entry->offs, &offset, sizeof(WORD) );
584 return TRUE;
585 }
586
587
588 /***********************************************************************
589 * build_bundle_data
590 *
591 * Build the entry table bundle data from the on-disk format. Helper for build_module.
592 */
593 static void *build_bundle_data( NE_MODULE *pModule, void *dest, const BYTE *table )
594 {
595 ET_BUNDLE *oldbundle, *bundle = dest;
596 ET_ENTRY *entry;
597 BYTE nr_entries, type;
598
599 memset(bundle, 0, sizeof(ET_BUNDLE)); /* in case no entry table exists */
600 entry = (ET_ENTRY *)((BYTE *)bundle+6);
601
602 while ((nr_entries = *table++))
603 {
604 if ((type = *table++))
605 {
606 bundle->last += nr_entries;
607 if (type == 0xff)
608 {
609 while (nr_entries--)
610 {
611 entry->type = type;
612 entry->flags = *table++;
613 table += sizeof(WORD);
614 entry->segnum = *table++;
615 entry->offs = *(const WORD *)table;
616 table += sizeof(WORD);
617 entry++;
618 }
619 }
620 else
621 {
622 while (nr_entries--)
623 {
624 entry->type = type;
625 entry->flags = *table++;
626 entry->segnum = type;
627 entry->offs = *(const WORD *)table;
628 table += sizeof(WORD);
629 entry++;
630 }
631 }
632 }
633 else
634 {
635 if (bundle->first == bundle->last)
636 {
637 bundle->first += nr_entries;
638 bundle->last += nr_entries;
639 }
640 else
641 {
642 oldbundle = bundle;
643 oldbundle->next = (char *)entry - (char *)pModule;
644 bundle = (ET_BUNDLE *)entry;
645 bundle->first = bundle->last = oldbundle->last + nr_entries;
646 bundle->next = 0;
647 entry = (ET_ENTRY*)(((BYTE*)entry)+sizeof(ET_BUNDLE));
648 }
649 }
650 }
651 return entry;
652 }
653
654
655 /***********************************************************************
656 * build_module
657 *
658 * Build the in-memory module from the on-disk data.
659 */
660 static HMODULE16 build_module( const void *mapping, SIZE_T mapping_size, LPCSTR path )
661 {
662 const IMAGE_DOS_HEADER *mz_header = mapping;
663 const IMAGE_OS2_HEADER *ne_header;
664 const struct ne_segment_table_entry_s *pSeg;
665 const void *ptr;
666 int i;
667 size_t size;
668 HMODULE16 hModule;
669 NE_MODULE *pModule;
670 BYTE *buffer, *pData, *end;
671 OFSTRUCT *ofs;
672
673 if (mapping_size < sizeof(*mz_header)) return ERROR_BAD_FORMAT;
674 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE) return ERROR_BAD_FORMAT;
675 ne_header = (const IMAGE_OS2_HEADER *)((const char *)mapping + mz_header->e_lfanew);
676 if (mz_header->e_lfanew + sizeof(*ne_header) > mapping_size) return ERROR_BAD_FORMAT;
677 if (ne_header->ne_magic == IMAGE_NT_SIGNATURE) return 21; /* win32 exe */
678 if (ne_header->ne_magic == IMAGE_OS2_SIGNATURE_LX)
679 {
680 MESSAGE("Sorry, %s is an OS/2 linear executable (LX) file!\n", path);
681 return 12;
682 }
683 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE) return ERROR_BAD_FORMAT;
684
685 /* We now have a valid NE header */
686
687 /* check to be able to fall back to loading OS/2 programs as DOS
688 * FIXME: should this check be reversed in order to be less strict?
689 * (only fail for OS/2 ne_exetyp 0x01 here?) */
690 if ((ne_header->ne_exetyp != 0x02 /* Windows */)
691 && (ne_header->ne_exetyp != 0x04) /* Windows 386 */)
692 return ERROR_BAD_FORMAT;
693
694 size = sizeof(NE_MODULE) +
695 /* segment table */
696 ne_header->ne_cseg * sizeof(SEGTABLEENTRY) +
697 /* resource table */
698 ne_header->ne_restab - ne_header->ne_rsrctab +
699 /* resident names table */
700 ne_header->ne_modtab - ne_header->ne_restab +
701 /* module ref table */
702 ne_header->ne_cmod * sizeof(WORD) +
703 /* imported names table */
704 ne_header->ne_enttab - ne_header->ne_imptab +
705 /* entry table length */
706 ne_header->ne_cbenttab +
707 /* entry table extra conversion space */
708 sizeof(ET_BUNDLE) +
709 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6) +
710 /* loaded file info */
711 sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path) + 1;
712
713 hModule = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, size );
714 if (!hModule) return ERROR_BAD_FORMAT;
715
716 FarSetOwner16( hModule, hModule );
717 pModule = GlobalLock16( hModule );
718 memcpy( pModule, ne_header, sizeof(*ne_header) );
719 pModule->count = 0;
720 /* check programs for default minimal stack size */
721 if (!(pModule->ne_flags & NE_FFLAGS_LIBMODULE) && (pModule->ne_stack < 0x1400))
722 pModule->ne_stack = 0x1400;
723
724 pModule->self = hModule;
725 pModule->mapping = mapping;
726 pModule->mapping_size = mapping_size;
727
728 pData = (BYTE *)(pModule + 1);
729
730 /* Clear internal Wine flags in case they are set in the EXE file */
731
732 pModule->ne_flags &= ~(NE_FFLAGS_BUILTIN | NE_FFLAGS_WIN32);
733
734 /* Get the segment table */
735
736 pModule->ne_segtab = pData - (BYTE *)pModule;
737 if (!(pSeg = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_segtab,
738 ne_header->ne_cseg * sizeof(struct ne_segment_table_entry_s) )))
739 goto failed;
740 for (i = ne_header->ne_cseg; i > 0; i--, pSeg++)
741 {
742 memcpy( pData, pSeg, sizeof(*pSeg) );
743 pData += sizeof(SEGTABLEENTRY);
744 }
745
746 /* Get the resource table */
747
748 if (ne_header->ne_rsrctab < ne_header->ne_restab)
749 {
750 pModule->ne_rsrctab = pData - (BYTE *)pModule;
751 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_rsrctab,
752 ne_header->ne_restab - ne_header->ne_rsrctab )) goto failed;
753 pData += ne_header->ne_restab - ne_header->ne_rsrctab;
754 }
755 else pModule->ne_rsrctab = 0; /* No resource table */
756
757 /* Get the resident names table */
758
759 pModule->ne_restab = pData - (BYTE *)pModule;
760 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_restab,
761 ne_header->ne_modtab - ne_header->ne_restab )) goto failed;
762 pData += ne_header->ne_modtab - ne_header->ne_restab;
763
764 /* Get the module references table */
765
766 if (ne_header->ne_cmod > 0)
767 {
768 pModule->ne_modtab = pData - (BYTE *)pModule;
769 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_modtab,
770 ne_header->ne_cmod * sizeof(WORD) )) goto failed;
771 pData += ne_header->ne_cmod * sizeof(WORD);
772 }
773 else pModule->ne_modtab = 0; /* No module references */
774
775 /* Get the imported names table */
776
777 pModule->ne_imptab = pData - (BYTE *)pModule;
778 if (!NE_READ_DATA( pModule, pData, mz_header->e_lfanew + ne_header->ne_imptab,
779 ne_header->ne_enttab - ne_header->ne_imptab )) goto failed;
780 pData += ne_header->ne_enttab - ne_header->ne_imptab;
781
782 /* Load entry table, convert it to the optimized version used by Windows */
783
784 pModule->ne_enttab = pData - (BYTE *)pModule;
785 if (!(ptr = NE_GET_DATA( pModule, mz_header->e_lfanew + ne_header->ne_enttab,
786 ne_header->ne_cbenttab ))) goto failed;
787 end = build_bundle_data( pModule, pData, ptr );
788
789 pData += ne_header->ne_cbenttab + sizeof(ET_BUNDLE) +
790 2 * (ne_header->ne_cbenttab - ne_header->ne_cmovent*6);
791
792 if (end > pData)
793 {
794 FIXME( "not enough space for entry table for %s\n", debugstr_a(path) );
795 goto failed;
796 }
797
798 /* Store the filename information */
799
800 pModule->fileinfo = pData - (BYTE *)pModule;
801 ofs = (OFSTRUCT *)pData;
802 ofs->cBytes = sizeof(OFSTRUCT) - sizeof(ofs->szPathName) + strlen(path);
803 ofs->fFixedDisk = 1;
804 strcpy( ofs->szPathName, path );
805 pData += ofs->cBytes + 1;
806 assert( (BYTE *)pModule + size <= pData );
807
808 /* Get the non-resident names table */
809
810 if (ne_header->ne_cbnrestab)
811 {
812 pModule->nrname_handle = GlobalAlloc16( 0, ne_header->ne_cbnrestab );
813 if (!pModule->nrname_handle) goto failed;
814 FarSetOwner16( pModule->nrname_handle, hModule );
815 buffer = GlobalLock16( pModule->nrname_handle );
816 if (!NE_READ_DATA( pModule, buffer, ne_header->ne_nrestab, ne_header->ne_cbnrestab ))
817 {
818 GlobalFree16( pModule->nrname_handle );
819 goto failed;
820 }
821 }
822 else pModule->nrname_handle = 0;
823
824 /* Allocate a segment for the implicitly-loaded DLLs */
825
826 if (pModule->ne_cmod)
827 {
828 pModule->dlls_to_init = GlobalAlloc16( GMEM_ZEROINIT,
829 (pModule->ne_cmod+1)*sizeof(HMODULE16) );
830 if (!pModule->dlls_to_init)
831 {
832 if (pModule->nrname_handle) GlobalFree16( pModule->nrname_handle );
833 goto failed;
834 }
835 FarSetOwner16( pModule->dlls_to_init, hModule );
836 }
837 else pModule->dlls_to_init = 0;
838
839 NE_RegisterModule( pModule );
840 return hModule;
841
842 failed:
843 GlobalFree16( hModule );
844 return ERROR_BAD_FORMAT;
845 }
846
847
848 /***********************************************************************
849 * NE_LoadDLLs
850 *
851 * Load all DLLs implicitly linked to a module.
852 */
853 static BOOL NE_LoadDLLs( NE_MODULE *pModule )
854 {
855 int i;
856 WORD *pModRef = (WORD *)((char *)pModule + pModule->ne_modtab);
857 WORD *pDLLs = GlobalLock16( pModule->dlls_to_init );
858
859 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
860 {
861 char buffer[260], *p;
862 BYTE *pstr = (BYTE *)pModule + pModule->ne_imptab + *pModRef;
863 memcpy( buffer, pstr + 1, *pstr );
864 *(buffer + *pstr) = 0; /* terminate it */
865
866 TRACE("Loading '%s'\n", buffer );
867 if (!(*pModRef = GetModuleHandle16( buffer )))
868 {
869 /* If the DLL is not loaded yet, load it and store */
870 /* its handle in the list of DLLs to initialize. */
871 HMODULE16 hDLL;
872
873 /* Append .DLL to name if no extension present */
874 if (!(p = strrchr( buffer, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
875 strcat( buffer, ".DLL" );
876
877 if ((hDLL = MODULE_LoadModule16( buffer, TRUE, TRUE )) < 32)
878 {
879 /* FIXME: cleanup what was done */
880
881 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
882 buffer, *((BYTE*)pModule + pModule->ne_restab),
883 (char *)pModule + pModule->ne_restab + 1, hDLL );
884 return FALSE;
885 }
886 *pModRef = GetExePtr( hDLL );
887 *pDLLs++ = *pModRef;
888 }
889 else /* Increment the reference count of the DLL */
890 {
891 NE_MODULE *pOldDLL = NE_GetPtr( *pModRef );
892 if (pOldDLL) pOldDLL->count++;
893 }
894 }
895 return TRUE;
896 }
897
898
899 /**********************************************************************
900 * NE_DoLoadModule
901 *
902 * Load first instance of NE module from file.
903 *
904 * pModule must point to a module structure prepared by build_module.
905 * This routine must never be called twice on a module.
906 */
907 static HINSTANCE16 NE_DoLoadModule( NE_MODULE *pModule )
908 {
909 /* Allocate the segments for this module */
910
911 if (!NE_CreateAllSegments( pModule ))
912 return ERROR_NOT_ENOUGH_MEMORY; /* 8 */
913
914 /* Load the referenced DLLs */
915
916 if (!NE_LoadDLLs( pModule ))
917 return ERROR_FILE_NOT_FOUND; /* 2 */
918
919 /* Load the segments */
920
921 NE_LoadAllSegments( pModule );
922
923 /* Make sure the usage count is 1 on the first loading of */
924 /* the module, even if it contains circular DLL references */
925
926 pModule->count = 1;
927
928 return NE_GetInstance( pModule );
929 }
930
931 /**********************************************************************
932 * NE_LoadModule
933 *
934 * Load first instance of NE module. (Note: caller is responsible for
935 * ensuring the module isn't already loaded!)
936 *
937 * If the module turns out to be an executable module, only a
938 * handle to a module stub is returned; this needs to be initialized
939 * by calling NE_DoLoadModule later, in the context of the newly
940 * created process.
941 *
942 * If lib_only is TRUE, however, the module is perforce treated
943 * like a DLL module, even if it is an executable module.
944 *
945 */
946 static HINSTANCE16 NE_LoadModule( LPCSTR name, BOOL lib_only )
947 {
948 NE_MODULE *pModule;
949 HMODULE16 hModule;
950 HINSTANCE16 hInstance;
951 HFILE16 hFile;
952 OFSTRUCT ofs;
953 HANDLE mapping;
954 void *ptr;
955 MEMORY_BASIC_INFORMATION info;
956
957 /* Open file */
958 if ((hFile = OpenFile16( name, &ofs, OF_READ|OF_SHARE_DENY_WRITE )) == HFILE_ERROR16)
959 return ERROR_FILE_NOT_FOUND;
960
961 mapping = CreateFileMappingW( DosFileHandleToWin32Handle(hFile), NULL, PAGE_WRITECOPY, 0, 0, NULL );
962 _lclose16( hFile );
963 if (!mapping) return ERROR_BAD_FORMAT;
964
965 ptr = MapViewOfFile( mapping, FILE_MAP_COPY, 0, 0, 0 );
966 CloseHandle( mapping );
967 if (!ptr) return ERROR_BAD_FORMAT;
968
969 VirtualQuery( ptr, &info, sizeof(info) );
970 hModule = build_module( ptr, info.RegionSize, ofs.szPathName );
971
972 if (hModule < 32)
973 {
974 UnmapViewOfFile( ptr );
975 return hModule;
976 }
977
978 SNOOP16_RegisterDLL( hModule, ofs.szPathName );
979 NE_InitResourceHandler( hModule );
980
981 pModule = NE_GetPtr( hModule );
982
983 if ( !lib_only && !( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) )
984 return hModule;
985
986 hInstance = NE_DoLoadModule( pModule );
987 if ( hInstance < 32 )
988 {
989 /* cleanup ... */
990 NE_FreeModule( hModule, 0 );
991 }
992
993 return hInstance;
994 }
995
996
997 /***********************************************************************
998 * NE_DoLoadBuiltinModule
999 *
1000 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
1001 */
1002 static HMODULE16 NE_DoLoadBuiltinModule( const IMAGE_DOS_HEADER *mz_header, const char *file_name,
1003 HMODULE owner32 )
1004 {
1005 NE_MODULE *pModule;
1006 HMODULE16 hModule;
1007 HINSTANCE16 hInstance;
1008 OSVERSIONINFOW versionInfo;
1009 SIZE_T mapping_size = ~0UL; /* assume builtins don't contain invalid offsets... */
1010
1011 hModule = build_module( mz_header, mapping_size, file_name );
1012 if (hModule < 32) return hModule;
1013 pModule = GlobalLock16( hModule );
1014 pModule->ne_flags |= NE_FFLAGS_BUILTIN;
1015 pModule->owner32 = owner32;
1016
1017 /* fake the expected version the module should have according to the current Windows version */
1018 versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
1019 if (GetVersionExW( &versionInfo ))
1020 pModule->ne_expver = MAKEWORD( versionInfo.dwMinorVersion, versionInfo.dwMajorVersion );
1021
1022 hInstance = NE_DoLoadModule( pModule );
1023 if (hInstance < 32) NE_FreeModule( hModule, 0 );
1024
1025 NE_InitResourceHandler( hModule );
1026
1027 if (pModule->ne_heap)
1028 {
1029 SEGTABLEENTRY *pSeg = NE_SEG_TABLE( pModule ) + pModule->ne_autodata - 1;
1030 unsigned int size = pSeg->minsize + pModule->ne_heap;
1031 if (size > 0xfffe) size = 0xfffe;
1032 LocalInit16( GlobalHandleToSel16(pSeg->hSeg), pSeg->minsize, size );
1033 }
1034
1035 patch_code_segment( pModule );
1036
1037 return hInstance;
1038 }
1039
1040
1041 /**********************************************************************
1042 * MODULE_LoadModule16
1043 *
1044 * Load a NE module in the order of the loadorder specification.
1045 * The caller is responsible that the module is not loaded already.
1046 *
1047 */
1048 static HINSTANCE16 MODULE_LoadModule16( LPCSTR libname, BOOL implicit, BOOL lib_only )
1049 {
1050 HINSTANCE16 hinst = 2;
1051 HMODULE16 hModule;
1052 HMODULE mod32 = 0;
1053 NE_MODULE *pModule;
1054 const IMAGE_DOS_HEADER *descr = NULL;
1055 const char *file_name = NULL;
1056 char dllname[32], owner[20], *p;
1057 const char *basename, *main_module;
1058 int owner_exists = FALSE;
1059
1060 /* strip path information */
1061
1062 basename = libname;
1063 if (basename[0] && basename[1] == ':') basename += 2; /* strip drive specification */
1064 if ((p = strrchr( basename, '\\' ))) basename = p + 1;
1065 if ((p = strrchr( basename, '/' ))) basename = p + 1;
1066
1067 if (strlen(basename) < sizeof(dllname)-6)
1068 {
1069 strcpy( dllname, basename );
1070 p = strrchr( dllname, '.' );
1071 if (!p) strcat( dllname, ".dll" );
1072 for (p = dllname; *p; p++) if (*p >= 'A' && *p <= 'Z') *p += 32;
1073
1074 strcpy( p, "16" );
1075 if ((mod32 = LoadLibraryA( dllname )))
1076 {
1077 if (!(descr = (void *)GetProcAddress( mod32, "__wine_spec_dos_header" )))
1078 {
1079 WARN( "loaded %s but does not contain a 16-bit module\n", debugstr_a(dllname) );
1080 FreeLibrary( mod32 );
1081 }
1082 else
1083 {
1084 TRACE( "found %s with embedded 16-bit module\n", debugstr_a(dllname) );
1085 file_name = basename;
1086
1087 /* if module has a 32-bit owner, match the load order of the owner */
1088 if ((main_module = (void *)GetProcAddress( mod32, "__wine_spec_main_module" )))
1089 {
1090 LDR_MODULE *ldr;
1091 HMODULE main_owner = LoadLibraryA( main_module );
1092
1093 if (!main_owner)
1094 {
1095 WARN( "couldn't load owner %s for 16-bit dll %s\n", main_module, dllname );
1096 FreeLibrary( mod32 );
1097 return ERROR_FILE_NOT_FOUND;
1098 }
1099 /* check if module was loaded native */
1100 if (LdrFindEntryForAddress( main_owner, &ldr ) || !(ldr->Flags & LDR_WINE_INTERNAL))
1101 {
1102 FreeLibrary( mod32 );
1103 descr = NULL;
1104 }
1105 FreeLibrary( main_owner );
1106 }
1107 }
1108 }
1109 *p = 0;
1110
1111 /* old-style 16-bit placeholders support, to be removed at some point */
1112 if (!mod32 && wine_dll_get_owner( dllname, owner, sizeof(owner), &owner_exists ) != -1)
1113 {
1114 mod32 = LoadLibraryA( owner );
1115 if (mod32)
1116 {
1117 if (!(descr = find_dll_descr( dllname, &file_name )))
1118 {
1119 FreeLibrary( mod32 );
1120 owner_exists = 0;
1121 }
1122 /* loading the 32-bit library can have the side effect of loading the module */
1123 /* if so, simply incr the ref count and return the module */
1124 if ((hModule = GetModuleHandle16( libname )))
1125 {
1126 TRACE( "module %s already loaded by owner\n", libname );
1127 pModule = NE_GetPtr( hModule );
1128 if (pModule) pModule->count++;
1129 FreeLibrary( mod32 );
1130 return hModule;
1131 }
1132 }
1133 else
1134 {
1135 /* it's probably disabled by the load order config */
1136 WARN( "couldn't load owner %s for 16-bit dll %s\n", owner, dllname );
1137 return ERROR_FILE_NOT_FOUND;
1138 }
1139 }
1140 }
1141
1142 if (descr)
1143 {
1144 TRACE("Trying built-in '%s'\n", libname);
1145 hinst = NE_DoLoadBuiltinModule( descr, file_name, mod32 );
1146 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : builtin\n", debugstr_a(file_name));
1147 }
1148 else
1149 {
1150 TRACE("Trying native dll '%s'\n", libname);
1151 hinst = NE_LoadModule(libname, lib_only);
1152 if (hinst > 32) TRACE_(loaddll)("Loaded module %s : native\n", debugstr_a(libname));
1153 if (hinst == ERROR_FILE_NOT_FOUND && owner_exists) hinst = 21; /* win32 module */
1154 }
1155
1156 if (hinst > 32 && !implicit)
1157 {
1158 hModule = GetModuleHandle16(libname);
1159 if(!hModule)
1160 {
1161 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1162 libname, hinst);
1163 return ERROR_INVALID_HANDLE;
1164 }
1165
1166 pModule = NE_GetPtr(hModule);
1167 if(!pModule)
1168 {
1169 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1170 libname, hinst);
1171 return ERROR_INVALID_HANDLE;
1172 }
1173
1174 TRACE("Loaded module '%s' at 0x%04x.\n", libname, hinst);
1175
1176 /*
1177 * Call initialization routines for all loaded DLLs. Note that
1178 * when we load implicitly linked DLLs this will be done by InitTask().
1179 */
1180 if(pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1181 {
1182 NE_InitializeDLLs(hModule);
1183 NE_DllProcessAttach(hModule);
1184 }
1185 }
1186 return hinst; /* The last error that occurred */
1187 }
1188
1189
1190 /**********************************************************************
1191 * NE_CreateThread
1192 *
1193 * Create the thread for a 16-bit module.
1194 */
1195 static HINSTANCE16 NE_CreateThread( NE_MODULE *pModule, WORD cmdShow, LPCSTR cmdline )
1196 {
1197 HANDLE hThread;
1198 TDB *pTask;
1199 HTASK16 hTask;
1200 HINSTANCE16 instance = 0;
1201
1202 if (!(hTask = TASK_SpawnTask( pModule, cmdShow, cmdline + 1, *cmdline, &hThread )))
1203 return 0;
1204
1205 /* Post event to start the task */
1206 PostEvent16( hTask );
1207
1208 /* Wait until we get the instance handle */
1209 do
1210 {
1211 DirectedYield16( hTask );
1212 if (!IsTask16( hTask )) /* thread has died */
1213 {
1214 DWORD exit_code;
1215 WaitForSingleObject( hThread, INFINITE );
1216 GetExitCodeThread( hThread, &exit_code );
1217 CloseHandle( hThread );
1218 return exit_code;
1219 }
1220 if (!(pTask = GlobalLock16( hTask ))) break;
1221 instance = pTask->hInstance;
1222 GlobalUnlock16( hTask );
1223 } while (!instance);
1224
1225 CloseHandle( hThread );
1226 return instance;
1227 }
1228
1229
1230 /**********************************************************************
1231 * LoadModule (KERNEL.45)
1232 */
1233 HINSTANCE16 WINAPI LoadModule16( LPCSTR name, LPVOID paramBlock )
1234 {
1235 BOOL lib_only = !paramBlock || (paramBlock == (LPVOID)-1);
1236 LOADPARAMS16 *params;
1237 HMODULE16 hModule;
1238 NE_MODULE *pModule;
1239 LPSTR cmdline;
1240 WORD cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
1241
1242 if (name == NULL) return 0;
1243
1244 TRACE("name %s, paramBlock %p\n", name, paramBlock);
1245
1246 /* Load module */
1247
1248 if ( (hModule = NE_GetModuleByFilename(name) ) != 0 )
1249 {
1250 /* Special case: second instance of an already loaded NE module */
1251
1252 if ( !( pModule = NE_GetPtr( hModule ) ) ) return ERROR_BAD_FORMAT;
1253 if ( pModule->module32 ) return (HINSTANCE16)21;
1254
1255 /* Increment refcount */
1256
1257 pModule->count++;
1258 }
1259 else
1260 {
1261 /* Main case: load first instance of NE module */
1262
1263 if ( (hModule = MODULE_LoadModule16( name, FALSE, lib_only )) < 32 )
1264 return hModule;
1265
1266 if ( !(pModule = NE_GetPtr( hModule )) )
1267 return ERROR_BAD_FORMAT;
1268 }
1269
1270 /* If library module, we just retrieve the instance handle */
1271
1272 if ( ( pModule->ne_flags & NE_FFLAGS_LIBMODULE ) || lib_only )
1273 return NE_GetInstance( pModule );
1274
1275 /*
1276 * At this point, we need to create a new process.
1277 *
1278 * pModule points either to an already loaded module, whose refcount
1279 * has already been incremented (to avoid having the module vanish
1280 * in the meantime), or else to a stub module which contains only header
1281 * information.
1282 */
1283 params = paramBlock;
1284 if (params->showCmd)
1285 cmdShow = ((WORD *)MapSL( params->showCmd ))[1];
1286 cmdline = MapSL( params->cmdLine );
1287 return NE_CreateThread( pModule, cmdShow, cmdline );
1288 }
1289
1290
1291 /**********************************************************************
1292 * NE_StartTask
1293 *
1294 * Startup code for a new 16-bit task.
1295 */
1296 DWORD NE_StartTask(void)
1297 {
1298 TDB *pTask = TASK_GetCurrent();
1299 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
1300 HINSTANCE16 hInstance, hPrevInstance;
1301 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
1302 WORD sp;
1303
1304 if ( pModule->count > 0 )
1305 {
1306 /* Second instance of an already loaded NE module */
1307 /* Note that the refcount was already incremented by the parent */
1308
1309 hPrevInstance = NE_GetInstance( pModule );
1310
1311 if ( pModule->ne_autodata )
1312 if ( NE_CreateSegment( pModule, pModule->ne_autodata ) )
1313 NE_LoadSegment( pModule, pModule->ne_autodata );
1314
1315 hInstance = NE_GetInstance( pModule );
1316 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance, pModule->ne_autodata, hPrevInstance);
1317
1318 }
1319 else
1320 {
1321 /* Load first instance of NE module */
1322
1323 pModule->ne_flags |= NE_FFLAGS_GUI; /* FIXME: is this necessary? */
1324
1325 hInstance = NE_DoLoadModule( pModule );
1326 hPrevInstance = 0;
1327 }
1328
1329 if ( hInstance >= 32 )
1330 {
1331 CONTEXT86 context;
1332
1333 /* Enter instance handles into task struct */
1334
1335 pTask->hInstance = hInstance;
1336 pTask->hPrevInstance = hPrevInstance;
1337
1338 /* Use DGROUP for 16-bit stack */
1339
1340 if (!(sp = OFFSETOF(pModule->ne_sssp)))
1341 sp = pSegTable[SELECTOROF(pModule->ne_sssp)-1].minsize + pModule->ne_stack;
1342 sp &= ~1;
1343 sp -= sizeof(STACK16FRAME);
1344 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( GlobalHandleToSel16(hInstance), sp );
1345
1346 /* Registers at initialization must be:
1347 * ax zero
1348 * bx stack size in bytes
1349 * cx heap size in bytes
1350 * si previous app instance
1351 * di current app instance
1352 * bp zero
1353 * es selector to the PSP
1354 * ds dgroup of the application
1355 * ss stack selector
1356 * sp top of the stack
1357 */
1358 memset( &context, 0, sizeof(context) );
1359 context.SegCs = GlobalHandleToSel16(pSegTable[SELECTOROF(pModule->ne_csip) - 1].hSeg);
1360 context.SegDs = GlobalHandleToSel16(pTask->hInstance);
1361 context.SegEs = pTask->hPDB;
1362 context.SegFs = wine_get_fs();
1363 context.SegGs = wine_get_gs();
1364 context.Eip = OFFSETOF(pModule->ne_csip);
1365 context.Ebx = pModule->ne_stack;
1366 context.Ecx = pModule->ne_heap;
1367 context.Edi = pTask->hInstance;
1368 context.Esi = pTask->hPrevInstance;
1369
1370 /* Now call 16-bit entry point */
1371
1372 TRACE("Starting main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
1373 context.SegCs, context.Eip, context.SegDs,
1374 SELECTOROF(NtCurrentTeb()->WOW32Reserved),
1375 OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
1376
1377 WOWCallback16Ex( 0, WCB16_REGS, 0, NULL, (DWORD *)&context );
1378 ExitThread( LOWORD(context.Eax) );
1379 }
1380 return hInstance; /* error code */
1381 }
1382
1383 /***********************************************************************
1384 * LoadLibrary (KERNEL.95)
1385 * LoadLibrary16 (KERNEL32.35)
1386 */
1387 HINSTANCE16 WINAPI LoadLibrary16( LPCSTR libname )
1388 {
1389 return LoadModule16(libname, (LPVOID)-1 );
1390 }
1391
1392
1393 /**********************************************************************
1394 * MODULE_CallWEP
1395 *
1396 * Call a DLL's WEP, allowing it to shut down.
1397 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1398 */
1399 static BOOL16 MODULE_CallWEP( HMODULE16 hModule )
1400 {
1401 BOOL16 ret;
1402 FARPROC16 WEP = GetProcAddress16( hModule, "WEP" );
1403 if (!WEP) return FALSE;
1404
1405 __TRY
1406 {
1407 WORD args[1];
1408 DWORD dwRet;
1409
1410 args[0] = WEP_FREE_DLL;
1411 WOWCallback16Ex( (DWORD)WEP, WCB16_PASCAL, sizeof(args), args, &dwRet );
1412 ret = LOWORD(dwRet);
1413 }
1414 __EXCEPT_PAGE_FAULT
1415 {
1416 WARN("Page fault\n");
1417 ret = 0;
1418 }
1419 __ENDTRY
1420
1421 return ret;
1422 }
1423
1424
1425 /**********************************************************************
1426 * NE_FreeModule
1427 *
1428 * Implementation of FreeModule16().
1429 */
1430 static BOOL16 NE_FreeModule( HMODULE16 hModule, BOOL call_wep )
1431 {
1432 HMODULE16 *hPrevModule;
1433 NE_MODULE *pModule;
1434 HMODULE16 *pModRef;
1435 int i;
1436
1437 if (!(pModule = NE_GetPtr( hModule ))) return FALSE;
1438 hModule = pModule->self;
1439
1440 TRACE("%04x count %d\n", hModule, pModule->count );
1441
1442 if (((INT16)(--pModule->count)) > 0 ) return TRUE;
1443 else pModule->count = 0;
1444
1445 if (call_wep && !(pModule->ne_flags & NE_FFLAGS_WIN32))
1446 {
1447 /* Free the objects owned by the DLL module */
1448 NE_CallUserSignalProc( hModule, USIG16_DLL_UNLOAD );
1449
1450 if (pModule->ne_flags & NE_FFLAGS_LIBMODULE)
1451 MODULE_CallWEP( hModule );
1452 else
1453 call_wep = FALSE; /* We are freeing a task -> no more WEPs */
1454 }
1455
1456 TRACE_(loaddll)("Unloaded module %s : %s\n", debugstr_a(NE_MODULE_NAME(pModule)),
1457 (pModule->ne_flags & NE_FFLAGS_BUILTIN) ? "builtin" : "native");
1458
1459 /* Clear magic number just in case */
1460
1461 pModule->ne_magic = pModule->self = 0;
1462 if (pModule->owner32) FreeLibrary( pModule->owner32 );
1463 else if (pModule->mapping) UnmapViewOfFile( pModule->mapping );
1464
1465 /* Remove it from the linked list */
1466
1467 hPrevModule = &hFirstModule;
1468 while (*hPrevModule && (*hPrevModule != hModule))
1469 {
1470 hPrevModule = &(NE_GetPtr( *hPrevModule ))->next;
1471 }
1472 if (*hPrevModule) *hPrevModule = pModule->next;
1473
1474 /* Free the referenced modules */
1475
1476 pModRef = (HMODULE16*)((char *)pModule + pModule->ne_modtab);
1477 for (i = 0; i < pModule->ne_cmod; i++, pModRef++)
1478 {
1479 NE_FreeModule( *pModRef, call_wep );
1480 }
1481
1482 /* Free the module storage */
1483
1484 GlobalFreeAll16( hModule );
1485 return TRUE;
1486 }
1487
1488
1489 /**********************************************************************
1490 * FreeModule (KERNEL.46)
1491 */
1492 BOOL16 WINAPI FreeModule16( HMODULE16 hModule )
1493 {
1494 return NE_FreeModule( hModule, TRUE );
1495 }
1496
1497
1498 /***********************************************************************
1499 * FreeLibrary (KERNEL.96)
1500 * FreeLibrary16 (KERNEL32.36)
1501 */
1502 void WINAPI FreeLibrary16( HINSTANCE16 handle )
1503 {
1504 TRACE("%04x\n", handle );
1505 FreeModule16( handle );
1506 }
1507
1508
1509 /***********************************************************************
1510 * GetModuleHandle16 (KERNEL32.@)
1511 */
1512 HMODULE16 WINAPI GetModuleHandle16( LPCSTR name )
1513 {
1514 HMODULE16 hModule = hFirstModule;
1515 LPSTR s;
1516 BYTE len, *name_table;
1517 char tmpstr[MAX_PATH];
1518 NE_MODULE *pModule;
1519
1520 TRACE("(%s)\n", name);
1521
1522 if (!HIWORD(name)) return GetExePtr(LOWORD(name));
1523
1524 len = strlen(name);
1525 if (!len) return 0;
1526
1527 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1528
1529 /* If 'name' matches exactly the module name of a module:
1530 * Return its handle.
1531 */
1532 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1533 {
1534 pModule = NE_GetPtr( hModule );
1535 if (!pModule) break;
1536 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1537
1538 name_table = (BYTE *)pModule + pModule->ne_restab;
1539 if ((*name_table == len) && !strncmp(name, (char*) name_table+1, len))
1540 return hModule;
1541 }
1542
1543 /* If uppercased 'name' matches exactly the module name of a module:
1544 * Return its handle
1545 */
1546 for (s = tmpstr; *s; s++) *s = RtlUpperChar(*s);
1547
1548 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1549 {
1550 pModule = NE_GetPtr( hModule );
1551 if (!pModule) break;
1552 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1553
1554 name_table = (BYTE *)pModule + pModule->ne_restab;
1555 /* FIXME: the strncasecmp is WRONG. It should not be case insensitive,
1556 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1557 * lowercased module names, but try to load uppercase DLLs, so this
1558 * 'i' compare is just a quickfix until the loader handles that
1559 * correctly. -MM 990705
1560 */
1561 if ((*name_table == len) && !NE_strncasecmp(tmpstr, (const char*)name_table+1, len))
1562 return hModule;
1563 }
1564
1565 /* If the base filename of 'name' matches the base filename of the module
1566 * filename of some module (case-insensitive compare):
1567 * Return its handle.
1568 */
1569
1570 /* basename: search backwards in passed name to \ / or : */
1571 s = tmpstr + strlen(tmpstr);
1572 while (s > tmpstr)
1573 {
1574 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1575 break;
1576 s--;
1577 }
1578
1579 /* search this in loaded filename list */
1580 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1581 {
1582 char *loadedfn;
1583 OFSTRUCT *ofs;
1584
1585 pModule = NE_GetPtr( hModule );
1586 if (!pModule) break;
1587 if (!pModule->fileinfo) continue;
1588 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1589
1590 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1591 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1592 /* basename: search backwards in pathname to \ / or : */
1593 while (loadedfn > (char*)ofs->szPathName)
1594 {
1595 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1596 break;
1597 loadedfn--;
1598 }
1599 /* case insensitive compare ... */
1600 if (!NE_strcasecmp(loadedfn, s))
1601 return hModule;
1602 }
1603 return 0;
1604 }
1605
1606
1607 /**********************************************************************
1608 * GetModuleName (KERNEL.27)
1609 */
1610 BOOL16 WINAPI GetModuleName16( HINSTANCE16 hinst, LPSTR buf, INT16 count )
1611 {
1612 NE_MODULE *pModule;
1613 BYTE *p;
1614
1615 if (!(pModule = NE_GetPtr( hinst ))) return FALSE;
1616 p = (BYTE *)pModule + pModule->ne_restab;
1617 if (count > *p) count = *p + 1;
1618 if (count > 0)
1619 {
1620 memcpy( buf, p + 1, count - 1 );
1621 buf[count-1] = '\0';
1622 }
1623 return TRUE;
1624 }
1625
1626
1627 /**********************************************************************
1628 * GetModuleFileName (KERNEL.49)
1629 *
1630 * Comment: see GetModuleFileNameA
1631 *
1632 * Even if invoked by second instance of a program,
1633 * it still returns path of first one.
1634 */
1635 INT16 WINAPI GetModuleFileName16( HINSTANCE16 hModule, LPSTR lpFileName,
1636 INT16 nSize )
1637 {
1638 NE_MODULE *pModule;
1639
1640 /* Win95 does not query hModule if set to 0 !
1641 * Is this wrong or maybe Win3.1 only ? */
1642 if (!hModule) hModule = GetCurrentTask();
1643
1644 if (!(pModule = NE_GetPtr( hModule ))) return 0;
1645 lstrcpynA( lpFileName, NE_MODULE_NAME(pModule), nSize );
1646 if (pModule->ne_expver < 0x400)
1647 GetShortPathNameA(NE_MODULE_NAME(pModule), lpFileName, nSize);
1648 TRACE("%04x -> '%s'\n", hModule, lpFileName );
1649 return strlen(lpFileName);
1650 }
1651
1652
1653 /**********************************************************************
1654 * GetModuleUsage (KERNEL.48)
1655 */
1656 INT16 WINAPI GetModuleUsage16( HINSTANCE16 hModule )
1657 {
1658 NE_MODULE *pModule = NE_GetPtr( hModule );
1659 return pModule ? pModule->count : 0;
1660 }
1661
1662
1663 /**********************************************************************
1664 * GetExpWinVer (KERNEL.167)
1665 */
1666 WORD WINAPI GetExpWinVer16( HMODULE16 hModule )
1667 {
1668 NE_MODULE *pModule = NE_GetPtr( hModule );
1669 if ( !pModule ) return 0;
1670 return pModule->ne_expver;
1671 }
1672
1673
1674 /***********************************************************************
1675 * WinExec (KERNEL.166)
1676 */
1677 HINSTANCE16 WINAPI WinExec16( LPCSTR lpCmdLine, UINT16 nCmdShow )
1678 {
1679 LPCSTR p, args = NULL;
1680 LPCSTR name_beg, name_end;
1681 LPSTR name, cmdline;
1682 int arglen;
1683 HINSTANCE16 ret;
1684 char buffer[MAX_PATH];
1685 LOADPARAMS16 params;
1686 WORD showCmd[2];
1687
1688 if (*lpCmdLine == '"') /* has to be only one and only at beginning ! */
1689 {
1690 name_beg = lpCmdLine+1;
1691 p = strchr ( lpCmdLine+1, '"' );
1692 if (p)
1693 {
1694 name_end = p;
1695 args = strchr ( p, ' ' );
1696 }
1697 else /* yes, even valid with trailing '"' missing */
1698 name_end = lpCmdLine+strlen(lpCmdLine);
1699 }
1700 else
1701 {
1702 name_beg = lpCmdLine;
1703 args = strchr( lpCmdLine, ' ' );
1704 name_end = args ? args : lpCmdLine+strlen(lpCmdLine);
1705 }
1706
1707 if ((name_beg == lpCmdLine) && (!args))
1708 { /* just use the original cmdline string as file name */
1709 name = (LPSTR)lpCmdLine;
1710 }
1711 else
1712 {
1713 if (!(name = HeapAlloc( GetProcessHeap(), 0, name_end - name_beg + 1 )))
1714 return ERROR_NOT_ENOUGH_MEMORY;
1715 memcpy( name, name_beg, name_end - name_beg );
1716 name[name_end - name_beg] = '\0';
1717 }
1718
1719 if (args)
1720 {
1721 args++;
1722 arglen = strlen(args);
1723 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 + arglen );
1724 cmdline[0] = (BYTE)arglen;
1725 strcpy( cmdline + 1, args );
1726 }
1727 else
1728 {
1729 cmdline = HeapAlloc( GetProcessHeap(), 0, 2 );
1730 cmdline[0] = cmdline[1] = 0;
1731 }
1732
1733 TRACE("name: '%s', cmdline: '%.*s'\n", name, cmdline[0], &cmdline[1]);
1734
1735 showCmd[0] = 2;
1736 showCmd[1] = nCmdShow;
1737
1738 params.hEnvironment = 0;
1739 params.cmdLine = MapLS( cmdline );
1740 params.showCmd = MapLS( showCmd );
1741 params.reserved = 0;
1742
1743 if (SearchPathA( NULL, name, ".exe", sizeof(buffer), buffer, NULL ))
1744 {
1745 ret = LoadModule16( buffer, ¶ms );
1746 }
1747 else if (!contains_path( name )) /* try 16-bit builtin */
1748 {
1749 lstrcpynA( buffer, name, sizeof(buffer) );
1750 if (strlen( buffer ) < sizeof(buffer) - 4 && !strchr( buffer, '.' )) strcat( buffer, ".exe" );
1751 ret = LoadModule16( buffer, ¶ms );
1752 if (ret == ERROR_FILE_NOT_FOUND) ret = 21; /* it might be a 32-bit builtin too */
1753 }
1754 else ret = ERROR_FILE_NOT_FOUND;
1755
1756 UnMapLS( params.cmdLine );
1757 UnMapLS( params.showCmd );
1758
1759 HeapFree( GetProcessHeap(), 0, cmdline );
1760 if (name != lpCmdLine) HeapFree( GetProcessHeap(), 0, name );
1761
1762 if (ret == 21 || ret == ERROR_BAD_FORMAT) /* 32-bit module or unknown executable*/
1763 {
1764 LOADPARAMS16 params;
1765 WORD showCmd[2];
1766 showCmd[0] = 2;
1767 showCmd[1] = nCmdShow;
1768
1769 arglen = strlen( lpCmdLine );
1770 cmdline = HeapAlloc( GetProcessHeap(), 0, arglen + 1 );
1771 cmdline[0] = (BYTE)arglen;
1772 memcpy( cmdline + 1, lpCmdLine, arglen );
1773
1774 params.hEnvironment = 0;
1775 params.cmdLine = MapLS( cmdline );
1776 params.showCmd = MapLS( showCmd );
1777 params.reserved = 0;
1778
1779 ret = LoadModule16( "winoldap.mod", ¶ms );
1780 UnMapLS( params.cmdLine );
1781 UnMapLS( params.showCmd );
1782 }
1783 return ret;
1784 }
1785
1786 /***********************************************************************
1787 * GetProcAddress (KERNEL.50)
1788 */
1789 FARPROC16 WINAPI GetProcAddress16( HMODULE16 hModule, LPCSTR name )
1790 {
1791 WORD ordinal;
1792 FARPROC16 ret;
1793
1794 if (!hModule) hModule = GetCurrentTask();
1795 hModule = GetExePtr( hModule );
1796
1797 if (HIWORD(name) != 0)
1798 {
1799 ordinal = NE_GetOrdinal( hModule, name );
1800 TRACE("%04x '%s'\n", hModule, name );
1801 }
1802 else
1803 {
1804 ordinal = LOWORD(name);
1805 TRACE("%04x %04x\n", hModule, ordinal );
1806 }
1807 if (!ordinal) return NULL;
1808
1809 ret = NE_GetEntryPoint( hModule, ordinal );
1810
1811 TRACE("returning %p\n", ret );
1812 return ret;
1813 }
1814
1815
1816 /***************************************************************************
1817 * HasGPHandler (KERNEL.338)
1818 */
1819 SEGPTR WINAPI HasGPHandler16( SEGPTR address )
1820 {
1821 HMODULE16 hModule;
1822 int gpOrdinal;
1823 SEGPTR gpPtr;
1824 GPHANDLERDEF *gpHandler;
1825
1826 if ( (hModule = FarGetOwner16( SELECTOROF(address) )) != 0
1827 && (gpOrdinal = NE_GetOrdinal( hModule, "__GP" )) != 0
1828 && (gpPtr = (SEGPTR)NE_GetEntryPointEx( hModule, gpOrdinal, FALSE )) != 0
1829 && !IsBadReadPtr16( gpPtr, sizeof(GPHANDLERDEF) )
1830 && (gpHandler = MapSL( gpPtr )) != NULL )
1831 {
1832 while (gpHandler->selector)
1833 {
1834 if ( SELECTOROF(address) == gpHandler->selector
1835 && OFFSETOF(address) >= gpHandler->rangeStart
1836 && OFFSETOF(address) < gpHandler->rangeEnd )
1837 return MAKESEGPTR( gpHandler->selector, gpHandler->handler );
1838 gpHandler++;
1839 }
1840 }
1841
1842 return 0;
1843 }
1844
1845
1846 /**********************************************************************
1847 * GetModuleHandle (KERNEL.47)
1848 *
1849 * Find a module from a module name.
1850 *
1851 * NOTE: The current implementation works the same way the Windows 95 one
1852 * does. Do not try to 'fix' it, fix the callers.
1853 * + It does not do ANY extension handling (except that strange .EXE bit)!
1854 * + It does not care about paths, just about basenames. (same as Windows)
1855 *
1856 * RETURNS
1857 * LOWORD:
1858 * the win16 module handle if found
1859 * 0 if not
1860 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1861 * Always hFirstModule
1862 */
1863 DWORD WINAPI WIN16_GetModuleHandle( SEGPTR name )
1864 {
1865 if (HIWORD(name) == 0)
1866 return MAKELONG(GetExePtr( (HINSTANCE16)name), hFirstModule );
1867 return MAKELONG(GetModuleHandle16( MapSL(name)), hFirstModule );
1868 }
1869
1870 /**********************************************************************
1871 * NE_GetModuleByFilename
1872 */
1873 static HMODULE16 NE_GetModuleByFilename( LPCSTR name )
1874 {
1875 HMODULE16 hModule;
1876 LPSTR s, p;
1877 BYTE len, *name_table;
1878 char tmpstr[MAX_PATH];
1879 NE_MODULE *pModule;
1880
1881 lstrcpynA(tmpstr, name, sizeof(tmpstr));
1882
1883 /* If the base filename of 'name' matches the base filename of the module
1884 * filename of some module (case-insensitive compare):
1885 * Return its handle.
1886 */
1887
1888 /* basename: search backwards in passed name to \ / or : */
1889 s = tmpstr + strlen(tmpstr);
1890 while (s > tmpstr)
1891 {
1892 if (s[-1]=='/' || s[-1]=='\\' || s[-1]==':')
1893 break;
1894 s--;
1895 }
1896
1897 /* search this in loaded filename list */
1898 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1899 {
1900 char *loadedfn;
1901 OFSTRUCT *ofs;
1902
1903 pModule = NE_GetPtr( hModule );
1904 if (!pModule) break;
1905 if (!pModule->fileinfo) continue;
1906 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1907
1908 ofs = (OFSTRUCT*)((BYTE *)pModule + pModule->fileinfo);
1909 loadedfn = ((char*)ofs->szPathName) + strlen(ofs->szPathName);
1910 /* basename: search backwards in pathname to \ / or : */
1911 while (loadedfn > (char*)ofs->szPathName)
1912 {
1913 if (loadedfn[-1]=='/' || loadedfn[-1]=='\\' || loadedfn[-1]==':')
1914 break;
1915 loadedfn--;
1916 }
1917 /* case insensitive compare ... */
1918 if (!NE_strcasecmp(loadedfn, s))
1919 return hModule;
1920 }
1921 /* If basename (without ext) matches the module name of a module:
1922 * Return its handle.
1923 */
1924
1925 if ( (p = strrchr( s, '.' )) != NULL ) *p = '\0';
1926 len = strlen(s);
1927
1928 for (hModule = hFirstModule; hModule ; hModule = pModule->next)
1929 {
1930 pModule = NE_GetPtr( hModule );
1931 if (!pModule) break;
1932 if (pModule->ne_flags & NE_FFLAGS_WIN32) continue;
1933
1934 name_table = (BYTE *)pModule + pModule->ne_restab;
1935 if ((*name_table == len) && !NE_strncasecmp(s, (const char*)name_table+1, len))
1936 return hModule;
1937 }
1938
1939 return 0;
1940 }
1941
1942 /***********************************************************************
1943 * GetProcAddress16 (KERNEL32.37)
1944 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1945 */
1946 FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE hModule, LPCSTR name )
1947 {
1948 if (!hModule) return 0;
1949 if (HIWORD(hModule))
1950 {
1951 WARN("hModule is Win32 handle (%p)\n", hModule );
1952 return 0;
1953 }
1954 return GetProcAddress16( LOWORD(hModule), name );
1955 }
1956
1957 /***************************************************************************
1958 * IsRomModule (KERNEL.323)
1959 */
1960 BOOL16 WINAPI IsRomModule16( HMODULE16 unused )
1961 {
1962 return FALSE;
1963 }
1964
1965 /***************************************************************************
1966 * IsRomFile (KERNEL.326)
1967 */
1968 BOOL16 WINAPI IsRomFile16( HFILE16 unused )
1969 {
1970 return FALSE;
1971 }
1972
1973 /***********************************************************************
1974 * create_dummy_module
1975 *
1976 * Create a dummy NE module for Win32 or Winelib.
1977 */
1978 static HMODULE16 create_dummy_module( HMODULE module32 )
1979 {
1980 HMODULE16 hModule;
1981 NE_MODULE *pModule;
1982 SEGTABLEENTRY *pSegment;
1983 char *pStr,*s;
1984 unsigned int len;
1985 const char* basename;
1986 OFSTRUCT *ofs;
1987 int of_size, size;
1988 char filename[MAX_PATH];
1989 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module32 );
1990
1991 if (!nt) return ERROR_BAD_FORMAT;
1992
1993 /* Extract base filename */
1994 len = GetModuleFileNameA( module32, filename, sizeof(filename) );
1995 if (!len || len >= sizeof(filename)) return ERROR_BAD_FORMAT;
1996 basename = strrchr(filename, '\\');
1997 if (!basename) basename = filename;
1998 else basename++;
1999 len = strlen(basename);
2000 if ((s = strchr(basename, '.'))) len = s - basename;
2001
2002 /* Allocate module */
2003 of_size = sizeof(OFSTRUCT) - sizeof(ofs->szPathName)
2004 + strlen(filename) + 1;
2005 size = sizeof(NE_MODULE) +
2006 /* loaded file info */
2007 ((of_size + 3) & ~3) +
2008 /* segment table: DS,CS */
2009 2 * sizeof(SEGTABLEENTRY) +
2010 /* name table */
2011 len + 2 +
2012 /* several empty tables */
2013 8;
2014
2015 hModule = GlobalAlloc16( GMEM_MOVEABLE | GMEM_ZEROINIT, size );
2016 if (!hModule) return ERROR_BAD_FORMAT;
2017
2018 FarSetOwner16( hModule, hModule );
2019 pModule = GlobalLock16( hModule );
2020
2021 /* Set all used entries */
2022 pModule->ne_magic = IMAGE_OS2_SIGNATURE;
2023 pModule->count = 1;
2024 pModule->next = 0;
2025 pModule->ne_flags = NE_FFLAGS_WIN32;
2026 pModule->ne_autodata = 0;
2027 pModule->ne_sssp = MAKESEGPTR( 0, 1 );
2028 pModule->ne_csip = MAKESEGPTR( 0, 2 );
2029 pModule->ne_heap = 0;
2030 pModule->ne_stack = 0;
2031 pModule->ne_cseg = 2;
2032 pModule->ne_cmod = 0;
2033 pModule->ne_cbnrestab = 0;
2034 pModule->fileinfo = sizeof(NE_MODULE);
2035 pModule->ne_exetyp = NE_OSFLAGS_WINDOWS;
2036 pModule->self = hModule;
2037 pModule->module32 = module32;
2038
2039 /* Set version and flags */
2040 pModule->ne_expver = ((nt->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
2041 (nt->OptionalHeader.MinorSubsystemVersion & 0xff);
2042 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
2043 pModule->ne_flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
2044
2045 /* Set loaded file information */
2046 ofs = (OFSTRUCT *)(pModule + 1);
2047 memset( ofs, 0, of_size );
2048 ofs->cBytes = of_size < 256 ? of_size : 255; /* FIXME */
2049 strcpy( ofs->szPathName, filename );
2050
2051 pSegment = (SEGTABLEENTRY*)((char*)(pModule + 1) + ((of_size + 3) & ~3));
2052 pModule->ne_segtab = (char *)pSegment - (char *)pModule;
2053 /* Data segment */
2054 pSegment->size = 0;
2055 pSegment->flags = NE_SEGFLAGS_DATA;
2056 pSegment->minsize = 0x1000;
2057 pSegment++;
2058 /* Code segment */
2059 pSegment->flags = 0;
2060 pSegment++;
2061
2062 /* Module name */
2063 pStr = (char *)pSegment;
2064 pModule->ne_restab = pStr - (char *)pModule;
2065 assert(len<256);
2066 *pStr = len;
2067 lstrcpynA( pStr+1, basename, len+1 );
2068 pStr += len+2;
2069
2070 /* All tables zero terminated */
2071 pModule->ne_rsrctab = pModule->ne_imptab = pModule->ne_enttab = pStr - (char *)pModule;
2072
2073 NE_RegisterModule( pModule );
2074 pModule->owner32 = LoadLibraryA( filename ); /* increment the ref count of the 32-bit module */
2075 return hModule;
2076 }
2077
2078 /***********************************************************************
2079 * PrivateLoadLibrary (KERNEL32.@)
2080 *
2081 * FIXME: rough guesswork, don't know what "Private" means
2082 */
2083 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
2084 {
2085 return LoadLibrary16(libname);
2086 }
2087
2088 /***********************************************************************
2089 * PrivateFreeLibrary (KERNEL32.@)
2090 *
2091 * FIXME: rough guesswork, don't know what "Private" means
2092 */
2093 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
2094 {
2095 FreeLibrary16(handle);
2096 }
2097
2098 /***********************************************************************
2099 * LoadLibrary32 (KERNEL.452)
2100 * LoadSystemLibrary32 (KERNEL.482)
2101 */
2102 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
2103 {
2104 HMODULE hModule;
2105 DWORD count;
2106
2107 ReleaseThunkLock( &count );
2108 hModule = LoadLibraryA( libname );
2109 RestoreThunkLock( count );
2110 return hModule;
2111 }
2112
2113 /***************************************************************************
2114 * MapHModuleLS (KERNEL32.@)
2115 */
2116 HMODULE16 WINAPI MapHModuleLS(HMODULE hmod)
2117 {
2118 HMODULE16 ret;
2119 NE_MODULE *pModule;
2120
2121 if (!hmod)
2122 return TASK_GetCurrent()->hInstance;
2123 if (!HIWORD(hmod))
2124 return LOWORD(hmod); /* we already have a 16 bit module handle */
2125 pModule = GlobalLock16(hFirstModule);
2126 while (pModule) {
2127 if (pModule->module32 == hmod)
2128 return pModule->self;
2129 pModule = GlobalLock16(pModule->next);
2130 }
2131 if ((ret = create_dummy_module( hmod )) < 32)
2132 {
2133 SetLastError(ret);
2134 ret = 0;
2135 }
2136 return ret;
2137 }
2138
2139 /***************************************************************************
2140 * MapHModuleSL (KERNEL32.@)
2141 */
2142 HMODULE WINAPI MapHModuleSL(HMODULE16 hmod)
2143 {
2144 NE_MODULE *pModule;
2145
2146 if (!hmod) {
2147 TDB *pTask = TASK_GetCurrent();
2148 hmod = pTask->hModule;
2149 }
2150 pModule = GlobalLock16(hmod);
2151 if ((pModule->ne_magic != IMAGE_OS2_SIGNATURE) || !(pModule->ne_flags & NE_FFLAGS_WIN32))
2152 return 0;
2153 return pModule->module32;
2154 }
2155
2156 /***************************************************************************
2157 * MapHInstLS (KERNEL.472)
2158 */
2159 void WINAPI MapHInstLS16( CONTEXT86 *context )
2160 {
2161 context->Eax = MapHModuleLS( (HMODULE)context->Eax );
2162 }
2163
2164 /***************************************************************************
2165 * MapHInstSL (KERNEL.473)
2166 */
2167 void WINAPI MapHInstSL16( CONTEXT86 *context )
2168 {
2169 context->Eax = (DWORD)MapHModuleSL( context->Eax );
2170 }
2171
2172 #ifdef __i386__
2173
2174 /***************************************************************************
2175 * MapHInstLS (KERNEL32.@)
2176 */
2177 __ASM_STDCALL_FUNC( MapHInstLS, 0,
2178 "pushl %eax\n\t"
2179 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n\t"
2180 "ret" )
2181
2182 /***************************************************************************
2183 * MapHInstSL (KERNEL32.@)
2184 */
2185 __ASM_STDCALL_FUNC( MapHInstSL, 0,
2186 "pushl %eax\n\t"
2187 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n\t"
2188 "ret" )
2189
2190 /***************************************************************************
2191 * MapHInstLS_PN (KERNEL32.@)
2192 */
2193 __ASM_STDCALL_FUNC( MapHInstLS_PN, 0,
2194 "testl %eax,%eax\n\t"
2195 "jz 1f\n\t"
2196 "pushl %eax\n\t"
2197 "call " __ASM_NAME("MapHModuleLS") __ASM_STDCALL(4) "\n"
2198 "1:\tret" )
2199
2200 /***************************************************************************
2201 * MapHInstSL_PN (KERNEL32.@)
2202 */
2203 __ASM_STDCALL_FUNC( MapHInstSL_PN, 0,
2204 "andl $0xffff,%eax\n\t"
2205 "jz 1f\n\t"
2206 "pushl %eax\n\t"
2207 "call " __ASM_NAME("MapHModuleSL") __ASM_STDCALL(4) "\n"
2208 "1:\tret" )
2209
2210 #endif /* __i386__ */
2211
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.