4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
35 #include "wine/winbase16.h"
38 #include "wine/library.h"
45 #include "builtin16.h"
46 #include "stackframe.h"
48 #include "wine/exception.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(module
);
52 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
55 typedef struct _GPHANDLERDEF
67 struct ne_segment_table_entry_s
69 WORD seg_data_offset
; /* Sector offset of segment data */
70 WORD seg_data_length
; /* Length of segment data */
71 WORD seg_flags
; /* Flags associated with this segment */
72 WORD min_alloc
; /* Minimum allocation size for this */
75 #define hFirstModule (pThhook->hExeHead)
77 static NE_MODULE
*pCachedModule
= 0; /* Module cached by NE_OpenFile */
81 void *module_start
; /* 32-bit address of the module data */
82 int module_size
; /* Size of the module data */
83 void *code_start
; /* 32-bit address of DLL code */
84 void *data_start
; /* 32-bit address of DLL data */
85 const char *owner
; /* 32-bit dll that contains this dll */
86 const void *rsrc
; /* resources data */
87 } BUILTIN16_DESCRIPTOR
;
89 /* Table of all built-in DLLs */
93 static const BUILTIN16_DESCRIPTOR
*builtin_dlls
[MAX_DLLS
];
96 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
);
97 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
);
99 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
);
101 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
);
104 static WINE_EXCEPTION_FILTER(page_fault
)
106 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
||
107 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION
)
108 return EXCEPTION_EXECUTE_HANDLER
;
109 return EXCEPTION_CONTINUE_SEARCH
;
113 /* patch all the flat cs references of the code segment if necessary */
114 inline static void patch_code_segment( void *code_segment
)
117 CALLFROM16
*call
= code_segment
;
118 if (call
->flatcs
== wine_get_cs()) return; /* nothing to patch */
119 while (call
->pushl
== 0x68)
121 call
->flatcs
= wine_get_cs();
128 /***********************************************************************
131 * Find a descriptor in the list
133 static const BUILTIN16_DESCRIPTOR
*find_dll_descr( const char *dllname
)
136 for (i
= 0; i
< MAX_DLLS
; i
++)
138 const BUILTIN16_DESCRIPTOR
*descr
= builtin_dlls
[i
];
141 NE_MODULE
*pModule
= (NE_MODULE
*)descr
->module_start
;
142 OFSTRUCT
*pOfs
= (OFSTRUCT
*)((LPBYTE
)pModule
+ pModule
->fileinfo
);
143 BYTE
*name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
145 /* check the dll file name */
146 if (!FILE_strcasecmp( pOfs
->szPathName
, dllname
)) return descr
;
147 /* check the dll module name (without extension) */
148 if (!FILE_strncasecmp( dllname
, name_table
+1, *name_table
) &&
149 !strcmp( dllname
+ *name_table
, ".dll" ))
157 /***********************************************************************
160 * Check if a builtin dll descriptor is present (because we loaded its 32-bit counterpart).
162 static BOOL
is_builtin_present( LPCSTR name
)
164 char dllname
[20], *p
;
166 if (strlen(name
) >= sizeof(dllname
)-4) return FALSE
;
167 strcpy( dllname
, name
);
168 p
= strrchr( dllname
, '.' );
169 if (!p
) strcat( dllname
, ".dll" );
170 for (p
= dllname
; *p
; p
++) *p
= FILE_tolower(*p
);
172 return (find_dll_descr( dllname
) != NULL
);
176 /***********************************************************************
177 * __wine_register_dll_16 (KERNEL32.@)
179 * Register a built-in DLL descriptor.
181 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR
*descr
)
185 for (i
= 0; i
< MAX_DLLS
; i
++)
187 if (builtin_dlls
[i
]) continue;
188 builtin_dlls
[i
] = descr
;
191 assert( i
< MAX_DLLS
);
195 /***********************************************************************
196 * __wine_unregister_dll_16 (KERNEL32.@)
198 * Unregister a built-in DLL descriptor.
200 void __wine_unregister_dll_16( const BUILTIN16_DESCRIPTOR
*descr
)
204 for (i
= 0; i
< MAX_DLLS
; i
++)
206 if (builtin_dlls
[i
] != descr
) continue;
207 builtin_dlls
[i
] = NULL
;
213 /***********************************************************************
216 void NE_DumpModule( HMODULE16 hModule
)
226 if (!(pModule
= NE_GetPtr( hModule
)))
228 MESSAGE( "**** %04x is not a module handle\n", hModule
);
232 /* Dump the module info */
234 DPRINTF( "Module %04x:\n", hModule
);
235 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
236 pModule
->count
, pModule
->flags
,
237 pModule
->heap_size
, pModule
->stack_size
);
238 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
239 pModule
->cs
, pModule
->ip
, pModule
->ss
, pModule
->sp
, pModule
->dgroup
,
240 pModule
->seg_count
, pModule
->modref_count
);
241 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
242 pModule
->os_flags
, pModule
->min_swap_area
,
243 pModule
->expected_version
);
244 if (pModule
->flags
& NE_FFLAGS_WIN32
)
245 DPRINTF( "PE module=%p\n", pModule
->module32
);
247 /* Dump the file info */
249 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
251 /* Dump the segment table */
253 DPRINTF( "Segment table:\n" );
254 pSeg
= NE_SEG_TABLE( pModule
);
255 for (i
= 0; i
< pModule
->seg_count
; i
++, pSeg
++)
256 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
257 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
258 pSeg
->minsize
, pSeg
->hSeg
);
260 /* Dump the resource table */
262 DPRINTF( "Resource table:\n" );
263 if (pModule
->res_table
)
265 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->res_table
);
266 DPRINTF( "Alignment: %d\n", *pword
++ );
269 NE_TYPEINFO
*ptr
= (NE_TYPEINFO
*)pword
;
270 NE_NAMEINFO
*pname
= (NE_NAMEINFO
*)(ptr
+ 1);
271 DPRINTF( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
272 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
273 DPRINTF( "offset=%d len=%d id=%04x\n",
274 pname
->offset
, pname
->length
, pname
->id
);
275 pword
= (WORD
*)pname
;
278 else DPRINTF( "None\n" );
280 /* Dump the resident name table */
282 DPRINTF( "Resident-name table:\n" );
283 pstr
= (char *)pModule
+ pModule
->name_table
;
286 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
287 *(WORD
*)(pstr
+ *pstr
+ 1) );
288 pstr
+= *pstr
+ 1 + sizeof(WORD
);
291 /* Dump the module reference table */
293 DPRINTF( "Module ref table:\n" );
294 if (pModule
->modref_table
)
296 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->modref_table
);
297 for (i
= 0; i
< pModule
->modref_count
; i
++, pword
++)
300 GetModuleName16( *pword
, name
, sizeof(name
) );
301 DPRINTF( "%d: %04x -> '%s'\n", i
, *pword
, name
);
304 else DPRINTF( "None\n" );
306 /* Dump the entry table */
308 DPRINTF( "Entry table:\n" );
309 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+pModule
->entry_table
);
311 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
312 DPRINTF( "Bundle %d-%d: %02x\n", bundle
->first
, bundle
->last
, entry
->type
);
313 ordinal
= bundle
->first
;
314 while (ordinal
< bundle
->last
)
316 if (entry
->type
== 0xff)
317 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal
++, entry
->segnum
, entry
->offs
);
319 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal
++, entry
->segnum
, entry
->offs
);
322 } while ( (bundle
->next
) && (bundle
= ((ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
))) );
324 /* Dump the non-resident names table */
326 DPRINTF( "Non-resident names table:\n" );
327 if (pModule
->nrname_handle
)
329 pstr
= (char *)GlobalLock16( pModule
->nrname_handle
);
332 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
333 *(WORD
*)(pstr
+ *pstr
+ 1) );
334 pstr
+= *pstr
+ 1 + sizeof(WORD
);
341 /***********************************************************************
344 * Walk the module list and print the modules.
346 void NE_WalkModules(void)
348 HMODULE16 hModule
= hFirstModule
;
349 MESSAGE( "Module Flags Name\n" );
352 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
355 MESSAGE( "Bad module %04x in list\n", hModule
);
358 MESSAGE( " %04x %04x %.*s\n", hModule
, pModule
->flags
,
359 *((char *)pModule
+ pModule
->name_table
),
360 (char *)pModule
+ pModule
->name_table
+ 1 );
361 hModule
= pModule
->next
;
366 /***********************************************************************
367 * EntryAddrProc (KERNEL.667) Wine-specific export
369 * Return the entry point for a given ordinal.
371 FARPROC16 WINAPI
EntryAddrProc16( HMODULE16 hModule
, WORD ordinal
)
373 FARPROC16 ret
= NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
374 CURRENT_STACK16
->ecx
= hModule
; /* FIXME: might be incorrect value */
378 /***********************************************************************
381 * Change the value of an entry point. Use with caution!
382 * It can only change the offset value, not the selector.
384 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
391 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
392 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
394 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
395 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
397 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
398 if (!(bundle
->next
)) return 0;
401 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
402 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
405 memcpy( &entry
->offs
, &offset
, sizeof(WORD
) );
410 /***********************************************************************
413 HANDLE
NE_OpenFile( NE_MODULE
*pModule
)
418 TRACE("(%p)\n", pModule
);
419 /* mjm - removed module caching because it keeps open file handles
420 thus preventing CDROMs from being ejected */
421 name
= NE_MODULE_NAME( pModule
);
422 if ((handle
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
423 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
424 MESSAGE( "Can't open file '%s' for module %04x\n", name
, pModule
->self
);
425 TRACE("opened '%s' -> %p\n", name
, handle
);
430 /* wrapper for SetFilePointer and ReadFile */
431 static BOOL
read_data( HANDLE handle
, LONG offset
, void *buffer
, DWORD size
)
435 if (SetFilePointer( handle
, offset
, NULL
, SEEK_SET
) == INVALID_SET_FILE_POINTER
) return FALSE
;
436 if (!ReadFile( handle
, buffer
, size
, &result
, NULL
)) return FALSE
;
437 return (result
== size
);
440 /***********************************************************************
443 static HMODULE16
NE_LoadExeHeader( HANDLE handle
, LPCSTR path
)
445 IMAGE_DOS_HEADER mz_header
;
446 IMAGE_OS2_HEADER ne_header
;
450 BYTE
*pData
, *pTempEntryTable
;
451 char *buffer
, *fastload
= NULL
;
452 int fastload_offset
= 0, fastload_length
= 0;
454 ET_BUNDLE
*bundle
, *oldbundle
;
457 /* Read a block from either the file or the fast-load area. */
458 #define READ(offset,size,buffer) \
459 ((fastload && ((offset) >= fastload_offset) && \
460 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
461 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
462 read_data( handle, (offset), (buffer), (size)))
464 if (!read_data( handle
, 0, &mz_header
, sizeof(mz_header
)) ||
465 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
466 return (HMODULE16
)11; /* invalid exe */
468 if (!read_data( handle
, mz_header
.e_lfanew
, &ne_header
, sizeof(ne_header
) ))
469 return (HMODULE16
)11; /* invalid exe */
471 if (ne_header
.ne_magic
== IMAGE_NT_SIGNATURE
) return (HMODULE16
)21; /* win32 exe */
472 if (ne_header
.ne_magic
== IMAGE_OS2_SIGNATURE_LX
) {
473 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
474 return (HMODULE16
)12;
476 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return (HMODULE16
)11; /* invalid exe */
478 /* We now have a valid NE header */
480 size
= sizeof(NE_MODULE
) +
482 ne_header
.ne_cseg
* sizeof(SEGTABLEENTRY
) +
484 ne_header
.ne_restab
- ne_header
.ne_rsrctab
+
485 /* resident names table */
486 ne_header
.ne_modtab
- ne_header
.ne_restab
+
487 /* module ref table */
488 ne_header
.ne_cmod
* sizeof(WORD
) +
489 /* imported names table */
490 ne_header
.ne_enttab
- ne_header
.ne_imptab
+
491 /* entry table length */
492 ne_header
.ne_cbenttab
+
493 /* entry table extra conversion space */
495 2 * (ne_header
.ne_cbenttab
- ne_header
.ne_cmovent
*6) +
496 /* loaded file info */
497 sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
) + 1;
499 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
500 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
502 FarSetOwner16( hModule
, hModule
);
503 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
504 memcpy( pModule
, &ne_header
, sizeof(ne_header
) );
506 /* check *programs* for default minimal stack size */
507 if ( (!(pModule
->flags
& NE_FFLAGS_LIBMODULE
))
508 && (pModule
->stack_size
< 0x1400) )
509 pModule
->stack_size
= 0x1400;
510 pModule
->module32
= 0;
511 pModule
->self
= hModule
;
512 pModule
->self_loading_sel
= 0;
513 pData
= (BYTE
*)(pModule
+ 1);
515 /* Clear internal Wine flags in case they are set in the EXE file */
517 pModule
->flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
519 /* Read the fast-load area */
521 if (ne_header
.ne_flagsothers
& NE_AFLAGS_FASTLOAD
)
523 fastload_offset
=ne_header
.ne_pretthunks
<< ne_header
.ne_align
;
524 fastload_length
=ne_header
.ne_psegrefbytes
<< ne_header
.ne_align
;
525 TRACE("Using fast-load area offset=%x len=%d\n",
526 fastload_offset
, fastload_length
);
527 if ((fastload
= HeapAlloc( GetProcessHeap(), 0, fastload_length
)) != NULL
)
529 if (!read_data( handle
, fastload_offset
, fastload
, fastload_length
))
531 HeapFree( GetProcessHeap(), 0, fastload
);
532 WARN("Error reading fast-load area!\n");
538 /* Get the segment table */
540 pModule
->seg_table
= pData
- (BYTE
*)pModule
;
541 buffer
= HeapAlloc( GetProcessHeap(), 0, ne_header
.ne_cseg
*
542 sizeof(struct ne_segment_table_entry_s
));
546 struct ne_segment_table_entry_s
*pSeg
;
548 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_segtab
,
549 ne_header
.ne_cseg
* sizeof(struct ne_segment_table_entry_s
),
552 HeapFree( GetProcessHeap(), 0, buffer
);
553 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
554 GlobalFree16( hModule
);
555 return (HMODULE16
)11; /* invalid exe */
557 pSeg
= (struct ne_segment_table_entry_s
*)buffer
;
558 for (i
= ne_header
.ne_cseg
; i
> 0; i
--, pSeg
++)
560 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
561 pData
+= sizeof(SEGTABLEENTRY
);
563 HeapFree( GetProcessHeap(), 0, buffer
);
567 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
568 GlobalFree16( hModule
);
569 return (HMODULE16
)11; /* invalid exe */
572 /* Get the resource table */
574 if (ne_header
.ne_rsrctab
< ne_header
.ne_restab
)
576 pModule
->res_table
= pData
- (BYTE
*)pModule
;
577 if (!READ(mz_header
.e_lfanew
+ ne_header
.ne_rsrctab
,
578 ne_header
.ne_restab
- ne_header
.ne_rsrctab
,
580 return (HMODULE16
)11; /* invalid exe */
581 pData
+= ne_header
.ne_restab
- ne_header
.ne_rsrctab
;
582 NE_InitResourceHandler( pModule
);
584 else pModule
->res_table
= 0; /* No resource table */
586 /* Get the resident names table */
588 pModule
->name_table
= pData
- (BYTE
*)pModule
;
589 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_restab
,
590 ne_header
.ne_modtab
- ne_header
.ne_restab
,
593 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
594 GlobalFree16( hModule
);
595 return (HMODULE16
)11; /* invalid exe */
597 pData
+= ne_header
.ne_modtab
- ne_header
.ne_restab
;
599 /* Get the module references table */
601 if (ne_header
.ne_cmod
> 0)
603 pModule
->modref_table
= pData
- (BYTE
*)pModule
;
604 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_modtab
,
605 ne_header
.ne_cmod
* sizeof(WORD
),
608 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
609 GlobalFree16( hModule
);
610 return (HMODULE16
)11; /* invalid exe */
612 pData
+= ne_header
.ne_cmod
* sizeof(WORD
);
614 else pModule
->modref_table
= 0; /* No module references */
616 /* Get the imported names table */
618 pModule
->import_table
= pData
- (BYTE
*)pModule
;
619 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_imptab
,
620 ne_header
.ne_enttab
- ne_header
.ne_imptab
,
623 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
624 GlobalFree16( hModule
);
625 return (HMODULE16
)11; /* invalid exe */
627 pData
+= ne_header
.ne_enttab
- ne_header
.ne_imptab
;
629 /* Load entry table, convert it to the optimized version used by Windows */
631 if ((pTempEntryTable
= HeapAlloc( GetProcessHeap(), 0, ne_header
.ne_cbenttab
)) != NULL
)
633 BYTE nr_entries
, type
, *s
;
635 TRACE("Converting entry table.\n");
636 pModule
->entry_table
= pData
- (BYTE
*)pModule
;
637 if (!READ( mz_header
.e_lfanew
+ ne_header
.ne_enttab
,
638 ne_header
.ne_cbenttab
, pTempEntryTable
))
640 HeapFree( GetProcessHeap(), 0, pTempEntryTable
);
641 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
642 GlobalFree16( hModule
);
643 return (HMODULE16
)11; /* invalid exe */
647 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header
.ne_enttab
, ne_header
.ne_cbenttab
, *s
);
649 bundle
= (ET_BUNDLE
*)pData
;
650 TRACE("first bundle: %p\n", bundle
);
651 memset(bundle
, 0, sizeof(ET_BUNDLE
)); /* in case no entry table exists */
652 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
654 while ((nr_entries
= *s
++))
658 bundle
->last
+= nr_entries
;
665 entry
->segnum
= *s
++;
666 entry
->offs
= *(WORD
*)s
; s
+= 2;
667 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
675 entry
->segnum
= type
;
676 entry
->offs
= *(WORD
*)s
; s
+= 2;
677 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
683 if (bundle
->first
== bundle
->last
)
685 bundle
->first
+= nr_entries
;
686 bundle
->last
+= nr_entries
;
691 oldbundle
->next
= ((int)entry
- (int)pModule
);
692 bundle
= (ET_BUNDLE
*)entry
;
693 TRACE("new bundle: %p\n", bundle
);
694 bundle
->first
= bundle
->last
=
695 oldbundle
->last
+ nr_entries
;
697 (BYTE
*)entry
+= sizeof(ET_BUNDLE
);
701 HeapFree( GetProcessHeap(), 0, pTempEntryTable
);
705 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
706 GlobalFree16( hModule
);
707 return (HMODULE16
)11; /* invalid exe */
710 pData
+= ne_header
.ne_cbenttab
+ sizeof(ET_BUNDLE
) +
711 2 * (ne_header
.ne_cbenttab
- ne_header
.ne_cmovent
*6);
713 if ((DWORD
)entry
> (DWORD
)pData
)
714 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry
, pData
);
716 /* Store the filename information */
718 pModule
->fileinfo
= pData
- (BYTE
*)pModule
;
719 size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
) + strlen(path
) + 1;
720 ofs
= (OFSTRUCT
*)pData
;
721 ofs
->cBytes
= size
- 1;
723 strcpy( ofs
->szPathName
, path
);
726 /* Free the fast-load area */
729 if (fastload
) HeapFree( GetProcessHeap(), 0, fastload
);
731 /* Get the non-resident names table */
733 if (ne_header
.ne_cbnrestab
)
735 pModule
->nrname_handle
= GlobalAlloc16( 0, ne_header
.ne_cbnrestab
);
736 if (!pModule
->nrname_handle
)
738 GlobalFree16( hModule
);
739 return (HMODULE16
)11; /* invalid exe */
741 FarSetOwner16( pModule
->nrname_handle
, hModule
);
742 buffer
= GlobalLock16( pModule
->nrname_handle
);
743 if (!read_data( handle
, ne_header
.ne_nrestab
, buffer
, ne_header
.ne_cbnrestab
))
745 GlobalFree16( pModule
->nrname_handle
);
746 GlobalFree16( hModule
);
747 return (HMODULE16
)11; /* invalid exe */
750 else pModule
->nrname_handle
= 0;
752 /* Allocate a segment for the implicitly-loaded DLLs */
754 if (pModule
->modref_count
)
756 pModule
->dlls_to_init
= GlobalAlloc16( GMEM_ZEROINIT
,
757 (pModule
->modref_count
+1)*sizeof(HMODULE16
) );
758 if (!pModule
->dlls_to_init
)
760 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
761 GlobalFree16( hModule
);
762 return (HMODULE16
)11; /* invalid exe */
764 FarSetOwner16( pModule
->dlls_to_init
, hModule
);
766 else pModule
->dlls_to_init
= 0;
768 NE_RegisterModule( pModule
);
769 SNOOP16_RegisterDLL(pModule
,path
);
774 /***********************************************************************
777 * Load all DLLs implicitly linked to a module.
779 static BOOL
NE_LoadDLLs( NE_MODULE
*pModule
)
782 WORD
*pModRef
= (WORD
*)((char *)pModule
+ pModule
->modref_table
);
783 WORD
*pDLLs
= (WORD
*)GlobalLock16( pModule
->dlls_to_init
);
785 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
787 char buffer
[260], *p
;
788 BYTE
*pstr
= (BYTE
*)pModule
+ pModule
->import_table
+ *pModRef
;
789 memcpy( buffer
, pstr
+ 1, *pstr
);
790 *(buffer
+ *pstr
) = 0; /* terminate it */
792 TRACE("Loading '%s'\n", buffer
);
793 if (!(*pModRef
= GetModuleHandle16( buffer
)))
795 /* If the DLL is not loaded yet, load it and store */
796 /* its handle in the list of DLLs to initialize. */
799 /* Append .DLL to name if no extension present */
800 if (!(p
= strrchr( buffer
, '.')) || strchr( p
, '/' ) || strchr( p
, '\\'))
801 strcat( buffer
, ".DLL" );
803 if ((hDLL
= MODULE_LoadModule16( buffer
, TRUE
, TRUE
)) < 32)
805 /* FIXME: cleanup what was done */
807 MESSAGE( "Could not load '%s' required by '%.*s', error=%d\n",
808 buffer
, *((BYTE
*)pModule
+ pModule
->name_table
),
809 (char *)pModule
+ pModule
->name_table
+ 1, hDLL
);
812 *pModRef
= GetExePtr( hDLL
);
815 else /* Increment the reference count of the DLL */
817 NE_MODULE
*pOldDLL
= NE_GetPtr( *pModRef
);
818 if (pOldDLL
) pOldDLL
->count
++;
825 /**********************************************************************
828 * Load first instance of NE module from file.
830 * pModule must point to a module structure prepared by NE_LoadExeHeader.
831 * This routine must never be called twice on a module.
834 static HINSTANCE16
NE_DoLoadModule( NE_MODULE
*pModule
)
836 /* Allocate the segments for this module */
838 if (!NE_CreateAllSegments( pModule
))
839 return ERROR_NOT_ENOUGH_MEMORY
; /* 8 */
841 /* Load the referenced DLLs */
843 if (!NE_LoadDLLs( pModule
))
844 return ERROR_FILE_NOT_FOUND
; /* 2 */
846 /* Load the segments */
848 NE_LoadAllSegments( pModule
);
850 /* Make sure the usage count is 1 on the first loading of */
851 /* the module, even if it contains circular DLL references */
855 return NE_GetInstance( pModule
);
858 /**********************************************************************
861 * Load first instance of NE module. (Note: caller is responsible for
862 * ensuring the module isn't already loaded!)
864 * If the module turns out to be an executable module, only a
865 * handle to a module stub is returned; this needs to be initialized
866 * by calling NE_DoLoadModule later, in the context of the newly
869 * If lib_only is TRUE, however, the module is perforce treated
870 * like a DLL module, even if it is an executable module.
873 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
)
877 HINSTANCE16 hInstance
;
882 if ((hFile
= OpenFile16( name
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
883 return (HMODULE16
)2; /* File not found */
885 hModule
= NE_LoadExeHeader( DosFileHandleToWin32Handle(hFile
), ofs
.szPathName
);
887 if (hModule
< 32) return hModule
;
889 pModule
= NE_GetPtr( hModule
);
890 if ( !pModule
) return hModule
;
892 if ( !lib_only
&& !( pModule
->flags
& NE_FFLAGS_LIBMODULE
) )
895 hInstance
= NE_DoLoadModule( pModule
);
896 if ( hInstance
< 32 )
899 NE_FreeModule( hModule
, 0 );
906 /***********************************************************************
907 * NE_DoLoadBuiltinModule
909 * Load a built-in Win16 module. Helper function for NE_LoadBuiltinModule.
911 static HMODULE16
NE_DoLoadBuiltinModule( const BUILTIN16_DESCRIPTOR
*descr
)
915 SEGTABLEENTRY
*pSegTable
;
918 hModule
= GLOBAL_CreateBlock( GMEM_MOVEABLE
, descr
->module_start
,
919 descr
->module_size
, 0, WINE_LDT_FLAGS_DATA
);
920 if (!hModule
) return 0;
921 FarSetOwner16( hModule
, hModule
);
923 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
924 pModule
->self
= hModule
;
925 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
926 pModule
->hRsrcMap
= (void *)descr
->rsrc
;
928 /* Allocate the code segment */
930 pSegTable
= NE_SEG_TABLE( pModule
);
931 pSegTable
->hSeg
= GLOBAL_CreateBlock( GMEM_FIXED
, descr
->code_start
,
932 pSegTable
->minsize
, hModule
,
933 WINE_LDT_FLAGS_CODE
|WINE_LDT_FLAGS_32BIT
);
934 if (!pSegTable
->hSeg
) return 0;
935 patch_code_segment( descr
->code_start
);
938 /* Allocate the data segment */
940 minsize
= pSegTable
->minsize
? pSegTable
->minsize
: 0x10000;
941 minsize
+= pModule
->heap_size
;
942 if (minsize
> 0x10000) minsize
= 0x10000;
943 pSegTable
->hSeg
= GlobalAlloc16( GMEM_FIXED
, minsize
);
944 if (!pSegTable
->hSeg
) return 0;
945 FarSetOwner16( pSegTable
->hSeg
, hModule
);
946 if (pSegTable
->minsize
) memcpy( GlobalLock16( pSegTable
->hSeg
),
947 descr
->data_start
, pSegTable
->minsize
);
948 if (pModule
->heap_size
)
949 LocalInit16( GlobalHandleToSel16(pSegTable
->hSeg
), pSegTable
->minsize
, minsize
);
951 if (descr
->rsrc
) NE_InitResourceHandler(pModule
);
953 NE_RegisterModule( pModule
);
955 /* make sure the 32-bit library containing this one is loaded too */
956 LoadLibraryA( descr
->owner
);
962 /***********************************************************************
963 * NE_LoadBuiltinModule
965 * Load a built-in module.
967 static HMODULE16
NE_LoadBuiltinModule( LPCSTR name
)
969 const BUILTIN16_DESCRIPTOR
*descr
;
970 char error
[256], dllname
[20], *p
;
974 /* Fix the name in case we have a full path and extension */
976 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
977 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
979 if (strlen(name
) >= sizeof(dllname
)-4) return (HMODULE16
)2;
981 strcpy( dllname
, name
);
982 p
= strrchr( dllname
, '.' );
983 if (!p
) strcat( dllname
, ".dll" );
984 for (p
= dllname
; *p
; p
++) *p
= FILE_tolower(*p
);
986 if ((descr
= find_dll_descr( dllname
)))
987 return NE_DoLoadBuiltinModule( descr
);
989 if ((handle
= wine_dll_load( dllname
, error
, sizeof(error
), &file_exists
)))
991 if ((descr
= find_dll_descr( dllname
)))
992 return NE_DoLoadBuiltinModule( descr
);
994 ERR( "loaded .so but dll %s still not found\n", dllname
);
998 if (!file_exists
) WARN("cannot open .so lib for 16-bit builtin %s: %s\n", name
, error
);
999 else ERR("failed to load .so lib for 16-bit builtin %s: %s\n", name
, error
);
1001 return (HMODULE16
)2;
1005 /**********************************************************************
1006 * MODULE_LoadModule16
1008 * Load a NE module in the order of the loadorder specification.
1009 * The caller is responsible that the module is not loaded already.
1012 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
)
1014 HINSTANCE16 hinst
= 2;
1015 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
1017 const char *filetype
= "";
1020 /* strip path information */
1022 if (libname
[0] && libname
[1] == ':') libname
+= 2; /* strip drive specification */
1023 if ((ptr
= strrchr( libname
, '\\' ))) libname
= ptr
+ 1;
1024 if ((ptr
= strrchr( libname
, '/' ))) libname
= ptr
+ 1;
1026 if (is_builtin_present(libname
))
1028 TRACE( "forcing loadorder to builtin for %s\n", debugstr_a(libname
) );
1029 /* force builtin loadorder since the dll is already in memory */
1030 loadorder
[0] = LOADORDER_BI
;
1031 loadorder
[1] = LOADORDER_INVALID
;
1033 else MODULE_GetLoadOrder(loadorder
, libname
, FALSE
);
1035 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
1037 if (loadorder
[i
] == LOADORDER_INVALID
) break;
1039 switch(loadorder
[i
])
1042 TRACE("Trying native dll '%s'\n", libname
);
1043 hinst
= NE_LoadModule(libname
, lib_only
);
1044 filetype
= "native";
1048 TRACE("Trying built-in '%s'\n", libname
);
1049 hinst
= NE_LoadBuiltinModule(libname
);
1050 filetype
= "builtin";
1060 TRACE_(loaddll
)("Loaded module '%s' : %s\n", libname
, filetype
);
1066 hModule
= GetModuleHandle16(libname
);
1069 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle. Filename too long ?\n",
1071 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1074 pModule
= NE_GetPtr(hModule
);
1077 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
1079 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
1082 TRACE("Loaded module '%s' at 0x%04x.\n", libname
, hinst
);
1085 * Call initialization routines for all loaded DLLs. Note that
1086 * when we load implicitly linked DLLs this will be done by InitTask().
1088 if(pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1090 NE_InitializeDLLs(hModule
);
1091 NE_DllProcessAttach(hModule
);
1099 /* We quit searching when we get another error than 'File not found' */
1103 return hinst
; /* The last error that occurred */
1107 /**********************************************************************
1110 * Create the thread for a 16-bit module.
1112 static HINSTANCE16
NE_CreateThread( NE_MODULE
*pModule
, WORD cmdShow
, LPCSTR cmdline
)
1117 HINSTANCE16 instance
= 0;
1119 if (!(hTask
= TASK_SpawnTask( pModule
, cmdShow
, cmdline
+ 1, *cmdline
, &hThread
)))
1122 /* Post event to start the task */
1123 PostEvent16( hTask
);
1125 /* Wait until we get the instance handle */
1128 DirectedYield16( hTask
);
1129 if (!IsTask16( hTask
)) /* thread has died */
1132 WaitForSingleObject( hThread
, INFINITE
);
1133 GetExitCodeThread( hThread
, &exit_code
);
1134 CloseHandle( hThread
);
1137 if (!(pTask
= GlobalLock16( hTask
))) break;
1138 instance
= pTask
->hInstance
;
1139 GlobalUnlock16( hTask
);
1140 } while (!instance
);
1142 CloseHandle( hThread
);
1147 /**********************************************************************
1148 * LoadModule (KERNEL.45)
1150 HINSTANCE16 WINAPI
LoadModule16( LPCSTR name
, LPVOID paramBlock
)
1152 BOOL lib_only
= !paramBlock
|| (paramBlock
== (LPVOID
)-1);
1153 LOADPARAMS16
*params
;
1161 if ( (hModule
= NE_GetModuleByFilename(name
) ) != 0 )
1163 /* Special case: second instance of an already loaded NE module */
1165 if ( !( pModule
= NE_GetPtr( hModule
) ) ) return (HINSTANCE16
)11;
1166 if ( pModule
->module32
) return (HINSTANCE16
)21;
1168 /* Increment refcount */
1174 /* Main case: load first instance of NE module */
1176 if ( (hModule
= MODULE_LoadModule16( name
, FALSE
, lib_only
)) < 32 )
1179 if ( !(pModule
= NE_GetPtr( hModule
)) )
1180 return (HINSTANCE16
)11;
1183 /* If library module, we just retrieve the instance handle */
1185 if ( ( pModule
->flags
& NE_FFLAGS_LIBMODULE
) || lib_only
)
1186 return NE_GetInstance( pModule
);
1189 * At this point, we need to create a new process.
1191 * pModule points either to an already loaded module, whose refcount
1192 * has already been incremented (to avoid having the module vanish
1193 * in the meantime), or else to a stub module which contains only header
1196 params
= (LOADPARAMS16
*)paramBlock
;
1197 cmdShow
= ((WORD
*)MapSL(params
->showCmd
))[1];
1198 cmdline
= MapSL( params
->cmdLine
);
1199 return NE_CreateThread( pModule
, cmdShow
, cmdline
);
1203 /**********************************************************************
1206 * Startup code for a new 16-bit task.
1208 DWORD
NE_StartTask(void)
1210 TDB
*pTask
= TASK_GetCurrent();
1211 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
1212 HINSTANCE16 hInstance
, hPrevInstance
;
1213 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
1216 if ( pModule
->count
> 0 )
1218 /* Second instance of an already loaded NE module */
1219 /* Note that the refcount was already incremented by the parent */
1221 hPrevInstance
= NE_GetInstance( pModule
);
1223 if ( pModule
->dgroup
)
1224 if ( NE_CreateSegment( pModule
, pModule
->dgroup
) )
1225 NE_LoadSegment( pModule
, pModule
->dgroup
);
1227 hInstance
= NE_GetInstance( pModule
);
1228 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance
, pModule
->dgroup
, hPrevInstance
);
1233 /* Load first instance of NE module */
1235 pModule
->flags
|= NE_FFLAGS_GUI
; /* FIXME: is this necessary? */
1237 hInstance
= NE_DoLoadModule( pModule
);
1241 if ( hInstance
>= 32 )
1245 /* Enter instance handles into task struct */
1247 pTask
->hInstance
= hInstance
;
1248 pTask
->hPrevInstance
= hPrevInstance
;
1250 /* Use DGROUP for 16-bit stack */
1252 if (!(sp
= pModule
->sp
))
1253 sp
= pSegTable
[pModule
->ss
-1].minsize
+ pModule
->stack_size
;
1255 sp
-= sizeof(STACK16FRAME
);
1256 pTask
->teb
->cur_stack
= MAKESEGPTR( GlobalHandleToSel16(hInstance
), sp
);
1258 /* Registers at initialization must be:
1260 * bx stack size in bytes
1261 * cx heap size in bytes
1262 * si previous app instance
1263 * di current app instance
1265 * es selector to the PSP
1266 * ds dgroup of the application
1268 * sp top of the stack
1270 memset( &context
, 0, sizeof(context
) );
1271 context
.SegCs
= GlobalHandleToSel16(pSegTable
[pModule
->cs
- 1].hSeg
);
1272 context
.SegDs
= GlobalHandleToSel16(pTask
->hInstance
);
1273 context
.SegEs
= pTask
->hPDB
;
1274 context
.Eip
= pModule
->ip
;
1275 context
.Ebx
= pModule
->stack_size
;
1276 context
.Ecx
= pModule
->heap_size
;
1277 context
.Edi
= pTask
->hInstance
;
1278 context
.Esi
= pTask
->hPrevInstance
;
1280 /* Now call 16-bit entry point */
1282 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
1283 context
.SegCs
, context
.Eip
, context
.SegDs
,
1284 SELECTOROF(pTask
->teb
->cur_stack
),
1285 OFFSETOF(pTask
->teb
->cur_stack
) );
1287 WOWCallback16Ex( 0, WCB16_REGS
, 0, NULL
, (DWORD
*)&context
);
1288 ExitThread( LOWORD(context
.Eax
) );
1290 return hInstance
; /* error code */
1293 /***********************************************************************
1294 * LoadLibrary (KERNEL.95)
1295 * LoadLibrary16 (KERNEL32.35)
1297 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
1299 return LoadModule16(libname
, (LPVOID
)-1 );
1303 /**********************************************************************
1306 * Call a DLL's WEP, allowing it to shut down.
1307 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1309 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
1312 FARPROC16 WEP
= GetProcAddress16( hModule
, "WEP" );
1313 if (!WEP
) return FALSE
;
1320 args
[0] = WEP_FREE_DLL
;
1321 WOWCallback16Ex( (DWORD
)WEP
, WCB16_PASCAL
, sizeof(args
), args
, &dwRet
);
1322 ret
= LOWORD(dwRet
);
1324 __EXCEPT(page_fault
)
1326 WARN("Page fault\n");
1335 /**********************************************************************
1338 * Implementation of FreeModule16().
1340 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
)
1342 HMODULE16
*hPrevModule
;
1347 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
1348 hModule
= pModule
->self
;
1350 TRACE("%04x count %d\n", hModule
, pModule
->count
);
1352 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
1353 else pModule
->count
= 0;
1355 if (pModule
->flags
& NE_FFLAGS_BUILTIN
)
1356 return FALSE
; /* Can't free built-in module */
1358 if (call_wep
&& !(pModule
->flags
& NE_FFLAGS_WIN32
))
1360 /* Free the objects owned by the DLL module */
1361 NE_CallUserSignalProc( hModule
, USIG16_DLL_UNLOAD
);
1363 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1364 MODULE_CallWEP( hModule
);
1366 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
1370 /* Clear magic number just in case */
1372 pModule
->magic
= pModule
->self
= 0;
1374 /* Remove it from the linked list */
1376 hPrevModule
= &hFirstModule
;
1377 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
1379 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
1381 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
1383 /* Free the referenced modules */
1385 pModRef
= (HMODULE16
*)NE_MODULE_TABLE( pModule
);
1386 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
1388 NE_FreeModule( *pModRef
, call_wep
);
1391 /* Free the module storage */
1393 GlobalFreeAll16( hModule
);
1395 /* Remove module from cache */
1397 if (pCachedModule
== pModule
) pCachedModule
= NULL
;
1402 /**********************************************************************
1403 * FreeModule (KERNEL.46)
1405 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
1407 return NE_FreeModule( hModule
, TRUE
);
1411 /***********************************************************************
1412 * FreeLibrary (KERNEL.96)
1413 * FreeLibrary16 (KERNEL32.36)
1415 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
1417 TRACE("%04x\n", handle
);
1418 FreeModule16( handle
);
1422 /**********************************************************************
1423 * GetModuleName (KERNEL.27)
1425 BOOL16 WINAPI
GetModuleName16( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
1430 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
1431 p
= (BYTE
*)pModule
+ pModule
->name_table
;
1432 if (count
> *p
) count
= *p
+ 1;
1435 memcpy( buf
, p
+ 1, count
- 1 );
1436 buf
[count
-1] = '\0';
1442 /**********************************************************************
1443 * GetModuleUsage (KERNEL.48)
1445 INT16 WINAPI
GetModuleUsage16( HINSTANCE16 hModule
)
1447 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1448 return pModule
? pModule
->count
: 0;
1452 /**********************************************************************
1453 * GetExpWinVer (KERNEL.167)
1455 WORD WINAPI
GetExpWinVer16( HMODULE16 hModule
)
1457 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1458 if ( !pModule
) return 0;
1461 * For built-in modules, fake the expected version the module should
1462 * have according to the Windows version emulated by Wine
1464 if ( !pModule
->expected_version
)
1466 OSVERSIONINFOA versionInfo
;
1467 versionInfo
.dwOSVersionInfoSize
= sizeof(versionInfo
);
1469 if ( GetVersionExA( &versionInfo
) )
1470 pModule
->expected_version
=
1471 (versionInfo
.dwMajorVersion
& 0xff) << 8
1472 | (versionInfo
.dwMinorVersion
& 0xff);
1475 return pModule
->expected_version
;
1479 /***********************************************************************
1480 * WinExec (KERNEL.166)
1482 HINSTANCE16 WINAPI
WinExec16( LPCSTR lpCmdLine
, UINT16 nCmdShow
)
1484 LPCSTR p
, args
= NULL
;
1485 LPCSTR name_beg
, name_end
;
1486 LPSTR name
, cmdline
;
1489 char buffer
[MAX_PATH
];
1491 if (*lpCmdLine
== '"') /* has to be only one and only at beginning ! */
1493 name_beg
= lpCmdLine
+1;
1494 p
= strchr ( lpCmdLine
+1, '"' );
1498 args
= strchr ( p
, ' ' );
1500 else /* yes, even valid with trailing '"' missing */
1501 name_end
= lpCmdLine
+strlen(lpCmdLine
);
1505 name_beg
= lpCmdLine
;
1506 args
= strchr( lpCmdLine
, ' ' );
1507 name_end
= args
? args
: lpCmdLine
+strlen(lpCmdLine
);
1510 if ((name_beg
== lpCmdLine
) && (!args
))
1511 { /* just use the original cmdline string as file name */
1512 name
= (LPSTR
)lpCmdLine
;
1516 if (!(name
= HeapAlloc( GetProcessHeap(), 0, name_end
- name_beg
+ 1 )))
1517 return ERROR_NOT_ENOUGH_MEMORY
;
1518 memcpy( name
, name_beg
, name_end
- name_beg
);
1519 name
[name_end
- name_beg
] = '\0';
1525 arglen
= strlen(args
);
1526 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 + arglen
);
1527 cmdline
[0] = (BYTE
)arglen
;
1528 strcpy( cmdline
+ 1, args
);
1532 cmdline
= HeapAlloc( GetProcessHeap(), 0, 2 );
1533 cmdline
[0] = cmdline
[1] = 0;
1536 TRACE("name: '%s', cmdline: '%.*s'\n", name
, cmdline
[0], &cmdline
[1]);
1538 if (SearchPathA( NULL
, name
, ".exe", sizeof(buffer
), buffer
, NULL
))
1540 LOADPARAMS16 params
;
1543 showCmd
[1] = nCmdShow
;
1545 params
.hEnvironment
= 0;
1546 params
.cmdLine
= MapLS( cmdline
);
1547 params
.showCmd
= MapLS( showCmd
);
1548 params
.reserved
= 0;
1550 ret
= LoadModule16( buffer
, ¶ms
);
1551 UnMapLS( params
.cmdLine
);
1552 UnMapLS( params
.showCmd
);
1554 else ret
= GetLastError();
1556 HeapFree( GetProcessHeap(), 0, cmdline
);
1557 if (name
!= lpCmdLine
) HeapFree( GetProcessHeap(), 0, name
);
1559 if (ret
== 21) /* 32-bit module */
1562 ReleaseThunkLock( &count
);
1563 ret
= LOWORD( WinExec( lpCmdLine
, nCmdShow
) );
1564 RestoreThunkLock( count
);
1569 /**********************************************************************
1570 * GetModuleHandle (KERNEL.47)
1572 * Find a module from a module name.
1574 * NOTE: The current implementation works the same way the Windows 95 one
1575 * does. Do not try to 'fix' it, fix the callers.
1576 * + It does not do ANY extension handling (except that strange .EXE bit)!
1577 * + It does not care about paths, just about basenames. (same as Windows)
1581 * the win16 module handle if found
1583 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1584 * Always hFirstModule
1586 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1588 if (HIWORD(name
) == 0)
1589 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1590 return MAKELONG(GetModuleHandle16( MapSL(name
)), hFirstModule
);
1593 /**********************************************************************
1594 * NE_GetModuleByFilename
1596 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
)
1600 BYTE len
, *name_table
;
1601 char tmpstr
[MAX_PATH
];
1604 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1606 /* If the base filename of 'name' matches the base filename of the module
1607 * filename of some module (case-insensitive compare):
1608 * Return its handle.
1611 /* basename: search backwards in passed name to \ / or : */
1612 s
= tmpstr
+ strlen(tmpstr
);
1615 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1620 /* search this in loaded filename list */
1621 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1626 pModule
= NE_GetPtr( hModule
);
1627 if (!pModule
) break;
1628 if (!pModule
->fileinfo
) continue;
1629 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1631 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1632 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1633 /* basename: search backwards in pathname to \ / or : */
1634 while (loadedfn
> (char*)ofs
->szPathName
)
1636 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1640 /* case insensitive compare ... */
1641 if (!FILE_strcasecmp(loadedfn
, s
))
1644 /* If basename (without ext) matches the module name of a module:
1645 * Return its handle.
1648 if ( (p
= strrchr( s
, '.' )) != NULL
) *p
= '\0';
1651 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1653 pModule
= NE_GetPtr( hModule
);
1654 if (!pModule
) break;
1655 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1657 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1658 if ((*name_table
== len
) && !FILE_strncasecmp(s
, name_table
+1, len
))
1665 /***********************************************************************
1666 * GetProcAddress16 (KERNEL32.37)
1667 * Get procaddress in 16bit module from win32... (kernel32 undoc. ordinal func)
1669 FARPROC16 WINAPI
WIN32_GetProcAddress16( HMODULE hModule
, LPCSTR name
)
1671 if (!hModule
) return 0;
1672 if (HIWORD(hModule
))
1674 WARN("hModule is Win32 handle (%p)\n", hModule
);
1677 return GetProcAddress16( LOWORD(hModule
), name
);
1680 /**********************************************************************
1681 * ModuleFirst (TOOLHELP.59)
1683 BOOL16 WINAPI
ModuleFirst16( MODULEENTRY
*lpme
)
1685 lpme
->wNext
= hFirstModule
;
1686 return ModuleNext16( lpme
);
1690 /**********************************************************************
1691 * ModuleNext (TOOLHELP.60)
1693 BOOL16 WINAPI
ModuleNext16( MODULEENTRY
*lpme
)
1698 if (!lpme
->wNext
) return FALSE
;
1699 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1700 name
= (char *)pModule
+ pModule
->name_table
;
1701 memcpy( lpme
->szModule
, name
+ 1, min(*name
, MAX_MODULE_NAME
) );
1702 lpme
->szModule
[min(*name
, MAX_MODULE_NAME
)] = '\0';
1703 lpme
->hModule
= lpme
->wNext
;
1704 lpme
->wcUsage
= pModule
->count
;
1705 lstrcpynA( lpme
->szExePath
, NE_MODULE_NAME(pModule
), sizeof(lpme
->szExePath
) );
1706 lpme
->wNext
= pModule
->next
;
1711 /**********************************************************************
1712 * ModuleFindName (TOOLHELP.61)
1714 BOOL16 WINAPI
ModuleFindName16( MODULEENTRY
*lpme
, LPCSTR name
)
1716 lpme
->wNext
= GetModuleHandle16( name
);
1717 return ModuleNext16( lpme
);
1721 /**********************************************************************
1722 * ModuleFindHandle (TOOLHELP.62)
1724 BOOL16 WINAPI
ModuleFindHandle16( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1726 hModule
= GetExePtr( hModule
);
1727 lpme
->wNext
= hModule
;
1728 return ModuleNext16( lpme
);
1732 /***************************************************************************
1733 * IsRomModule (KERNEL.323)
1735 BOOL16 WINAPI
IsRomModule16( HMODULE16 unused
)
1740 /***************************************************************************
1741 * IsRomFile (KERNEL.326)
1743 BOOL16 WINAPI
IsRomFile16( HFILE16 unused
)
1748 /***********************************************************************
1749 * create_dummy_module
1751 * Create a dummy NE module for Win32 or Winelib.
1753 static HMODULE16
create_dummy_module( HMODULE module32
)
1757 SEGTABLEENTRY
*pSegment
;
1760 const char* basename
;
1763 char filename
[MAX_PATH
];
1764 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( module32
);
1766 if (!nt
) return (HMODULE16
)11; /* invalid exe */
1768 /* Extract base filename */
1769 GetModuleFileNameA( module32
, filename
, sizeof(filename
) );
1770 basename
= strrchr(filename
, '\\');
1771 if (!basename
) basename
= filename
;
1773 len
= strlen(basename
);
1774 if ((s
= strchr(basename
, '.'))) len
= s
- basename
;
1776 /* Allocate module */
1777 of_size
= sizeof(OFSTRUCT
) - sizeof(ofs
->szPathName
)
1778 + strlen(filename
) + 1;
1779 size
= sizeof(NE_MODULE
) +
1780 /* loaded file info */
1781 ((of_size
+ 3) & ~3) +
1782 /* segment table: DS,CS */
1783 2 * sizeof(SEGTABLEENTRY
) +
1786 /* several empty tables */
1789 hModule
= GlobalAlloc16( GMEM_MOVEABLE
| GMEM_ZEROINIT
, size
);
1790 if (!hModule
) return (HMODULE16
)11; /* invalid exe */
1792 FarSetOwner16( hModule
, hModule
);
1793 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
1795 /* Set all used entries */
1796 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
1799 pModule
->flags
= NE_FFLAGS_WIN32
;
1800 pModule
->dgroup
= 0;
1803 pModule
->heap_size
= 0;
1804 pModule
->stack_size
= 0;
1805 pModule
->seg_count
= 2;
1806 pModule
->modref_count
= 0;
1807 pModule
->nrname_size
= 0;
1808 pModule
->fileinfo
= sizeof(NE_MODULE
);
1809 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
1810 pModule
->self
= hModule
;
1811 pModule
->module32
= module32
;
1813 /* Set version and flags */
1814 pModule
->expected_version
= ((nt
->OptionalHeader
.MajorSubsystemVersion
& 0xff) << 8 ) |
1815 (nt
->OptionalHeader
.MinorSubsystemVersion
& 0xff);
1816 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
1817 pModule
->flags
|= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
;
1819 /* Set loaded file information */
1820 ofs
= (OFSTRUCT
*)(pModule
+ 1);
1821 memset( ofs
, 0, of_size
);
1822 ofs
->cBytes
= of_size
< 256 ? of_size
: 255; /* FIXME */
1823 strcpy( ofs
->szPathName
, filename
);
1825 pSegment
= (SEGTABLEENTRY
*)((char*)(pModule
+ 1) + ((of_size
+ 3) & ~3));
1826 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
1829 pSegment
->flags
= NE_SEGFLAGS_DATA
;
1830 pSegment
->minsize
= 0x1000;
1833 pSegment
->flags
= 0;
1837 pStr
= (char *)pSegment
;
1838 pModule
->name_table
= (int)pStr
- (int)pModule
;
1841 lstrcpynA( pStr
+1, basename
, len
+1 );
1844 /* All tables zero terminated */
1845 pModule
->res_table
= pModule
->import_table
= pModule
->entry_table
= (int)pStr
- (int)pModule
;
1847 NE_RegisterModule( pModule
);
1848 LoadLibraryA( filename
); /* increment the ref count of the 32-bit module */
1852 /***********************************************************************
1853 * PrivateLoadLibrary (KERNEL32.@)
1855 * FIXME: rough guesswork, don't know what "Private" means
1857 HINSTANCE16 WINAPI
PrivateLoadLibrary(LPCSTR libname
)
1859 return LoadLibrary16(libname
);
1862 /***********************************************************************
1863 * PrivateFreeLibrary (KERNEL32.@)
1865 * FIXME: rough guesswork, don't know what "Private" means
1867 void WINAPI
PrivateFreeLibrary(HINSTANCE16 handle
)
1869 FreeLibrary16(handle
);
1872 /***************************************************************************
1873 * MapHModuleLS (KERNEL32.@)
1875 HMODULE16 WINAPI
MapHModuleLS(HMODULE hmod
)
1881 return TASK_GetCurrent()->hInstance
;
1883 return LOWORD(hmod
); /* we already have a 16 bit module handle */
1884 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
1886 if (pModule
->module32
== hmod
)
1887 return pModule
->self
;
1888 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
1890 if ((ret
= create_dummy_module( hmod
)) < 32)
1898 /***************************************************************************
1899 * MapHModuleSL (KERNEL32.@)
1901 HMODULE WINAPI
MapHModuleSL(HMODULE16 hmod
)
1906 TDB
*pTask
= TASK_GetCurrent();
1907 hmod
= pTask
->hModule
;
1909 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
1910 if ((pModule
->magic
!=IMAGE_OS2_SIGNATURE
) || !(pModule
->flags
& NE_FFLAGS_WIN32
))
1912 return pModule
->module32
;
1915 /***************************************************************************
1916 * MapHInstLS (KERNEL32.@)
1917 * MapHInstLS (KERNEL.472)
1919 void WINAPI
MapHInstLS( CONTEXT86
*context
)
1921 context
->Eax
= MapHModuleLS( (HMODULE
)context
->Eax
);
1924 /***************************************************************************
1925 * MapHInstSL (KERNEL32.@)
1926 * MapHInstSL (KERNEL.473)
1928 void WINAPI
MapHInstSL( CONTEXT86
*context
)
1930 context
->Eax
= (DWORD
)MapHModuleSL( context
->Eax
);
1933 /***************************************************************************
1934 * MapHInstLS_PN (KERNEL32.@)
1936 void WINAPI
MapHInstLS_PN( CONTEXT86
*context
)
1938 if (context
->Eax
) context
->Eax
= MapHModuleLS( (HMODULE
)context
->Eax
);
1941 /***************************************************************************
1942 * MapHInstSL_PN (KERNEL32.@)
1944 void WINAPI
MapHInstSL_PN( CONTEXT86
*context
)
1946 if (context
->Eax
) context
->Eax
= (DWORD
)MapHModuleSL( context
->Eax
);