4 * Copyright 1995 Alexandre Julliard
13 #include "wine/winbase16.h"
28 #include "builtin16.h"
29 #include "stackframe.h"
30 #include "debugtools.h"
31 #include "loadorder.h"
34 DEFAULT_DEBUG_CHANNEL(module
)
36 #define hFirstModule (pThhook->hExeHead)
38 static NE_MODULE
*pCachedModule
= 0; /* Module cached by NE_OpenFile */
40 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
);
41 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
);
43 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
);
45 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
);
47 /***********************************************************************
50 NE_MODULE
*NE_GetPtr( HMODULE16 hModule
)
52 return (NE_MODULE
*)GlobalLock16( GetExePtr(hModule
) );
56 /***********************************************************************
59 void NE_DumpModule( HMODULE16 hModule
)
69 if (!(pModule
= NE_GetPtr( hModule
)))
71 MESSAGE( "**** %04x is not a module handle\n", hModule
);
75 /* Dump the module info */
77 DPRINTF( "Module %04x:\n", hModule
);
78 DPRINTF( "count=%d flags=%04x heap=%d stack=%d\n",
79 pModule
->count
, pModule
->flags
,
80 pModule
->heap_size
, pModule
->stack_size
);
81 DPRINTF( "cs:ip=%04x:%04x ss:sp=%04x:%04x ds=%04x nb seg=%d modrefs=%d\n",
82 pModule
->cs
, pModule
->ip
, pModule
->ss
, pModule
->sp
, pModule
->dgroup
,
83 pModule
->seg_count
, pModule
->modref_count
);
84 DPRINTF( "os_flags=%d swap_area=%d version=%04x\n",
85 pModule
->os_flags
, pModule
->min_swap_area
,
86 pModule
->expected_version
);
87 if (pModule
->flags
& NE_FFLAGS_WIN32
)
88 DPRINTF( "PE module=%08x\n", pModule
->module32
);
90 /* Dump the file info */
92 DPRINTF( "Filename: '%s'\n", NE_MODULE_NAME(pModule
) );
94 /* Dump the segment table */
96 DPRINTF( "Segment table:\n" );
97 pSeg
= NE_SEG_TABLE( pModule
);
98 for (i
= 0; i
< pModule
->seg_count
; i
++, pSeg
++)
99 DPRINTF( "%02x: pos=%d size=%d flags=%04x minsize=%d hSeg=%04x\n",
100 i
+ 1, pSeg
->filepos
, pSeg
->size
, pSeg
->flags
,
101 pSeg
->minsize
, pSeg
->hSeg
);
103 /* Dump the resource table */
105 DPRINTF( "Resource table:\n" );
106 if (pModule
->res_table
)
108 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->res_table
);
109 DPRINTF( "Alignment: %d\n", *pword
++ );
112 struct resource_typeinfo_s
*ptr
= (struct resource_typeinfo_s
*)pword
;
113 struct resource_nameinfo_s
*pname
= (struct resource_nameinfo_s
*)(ptr
+ 1);
114 DPRINTF( "id=%04x count=%d\n", ptr
->type_id
, ptr
->count
);
115 for (i
= 0; i
< ptr
->count
; i
++, pname
++)
116 DPRINTF( "offset=%d len=%d id=%04x\n",
117 pname
->offset
, pname
->length
, pname
->id
);
118 pword
= (WORD
*)pname
;
121 else DPRINTF( "None\n" );
123 /* Dump the resident name table */
125 DPRINTF( "Resident-name table:\n" );
126 pstr
= (char *)pModule
+ pModule
->name_table
;
129 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
130 *(WORD
*)(pstr
+ *pstr
+ 1) );
131 pstr
+= *pstr
+ 1 + sizeof(WORD
);
134 /* Dump the module reference table */
136 DPRINTF( "Module ref table:\n" );
137 if (pModule
->modref_table
)
139 pword
= (WORD
*)((BYTE
*)pModule
+ pModule
->modref_table
);
140 for (i
= 0; i
< pModule
->modref_count
; i
++, pword
++)
143 GetModuleName16( *pword
, name
, sizeof(name
) );
144 DPRINTF( "%d: %04x -> '%s'\n", i
, *pword
, name
);
147 else DPRINTF( "None\n" );
149 /* Dump the entry table */
151 DPRINTF( "Entry table:\n" );
152 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+pModule
->entry_table
);
154 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
155 DPRINTF( "Bundle %d-%d: %02x\n", bundle
->first
, bundle
->last
, entry
->type
);
156 ordinal
= bundle
->first
;
157 while (ordinal
< bundle
->last
)
159 if (entry
->type
== 0xff)
160 DPRINTF("%d: %02x:%04x (moveable)\n", ordinal
++, entry
->segnum
, entry
->offs
);
162 DPRINTF("%d: %02x:%04x (fixed)\n", ordinal
++, entry
->segnum
, entry
->offs
);
165 } while ( (bundle
->next
)
166 && (bundle
= ((ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
))) );
168 /* Dump the non-resident names table */
170 DPRINTF( "Non-resident names table:\n" );
171 if (pModule
->nrname_handle
)
173 pstr
= (char *)GlobalLock16( pModule
->nrname_handle
);
176 DPRINTF( "%*.*s: %d\n", *pstr
, *pstr
, pstr
+ 1,
177 *(WORD
*)(pstr
+ *pstr
+ 1) );
178 pstr
+= *pstr
+ 1 + sizeof(WORD
);
185 /***********************************************************************
188 * Walk the module list and print the modules.
190 void NE_WalkModules(void)
192 HMODULE16 hModule
= hFirstModule
;
193 MESSAGE( "Module Flags Name\n" );
196 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
199 MESSAGE( "Bad module %04x in list\n", hModule
);
202 MESSAGE( " %04x %04x %.*s\n", hModule
, pModule
->flags
,
203 *((char *)pModule
+ pModule
->name_table
),
204 (char *)pModule
+ pModule
->name_table
+ 1 );
205 hModule
= pModule
->next
;
210 /**********************************************************************
213 void NE_RegisterModule( NE_MODULE
*pModule
)
215 pModule
->next
= hFirstModule
;
216 hFirstModule
= pModule
->self
;
220 /***********************************************************************
223 * Lookup the ordinal for a given name.
225 WORD
NE_GetOrdinal( HMODULE16 hModule
, const char *name
)
227 unsigned char buffer
[256], *cpnt
;
231 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
232 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
234 TRACE("(%04x,'%s')\n", hModule
, name
);
236 /* First handle names of the form '#xxxx' */
238 if (name
[0] == '#') return atoi( name
+ 1 );
240 /* Now copy and uppercase the string */
242 strcpy( buffer
, name
);
243 CharUpperA( buffer
);
244 len
= strlen( buffer
);
246 /* First search the resident names */
248 cpnt
= (char *)pModule
+ pModule
->name_table
;
250 /* Skip the first entry (module name) */
251 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
254 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
256 TRACE(" Found: ordinal=%d\n",
257 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
258 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
260 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
263 /* Now search the non-resident names table */
265 if (!pModule
->nrname_handle
) return 0; /* No non-resident table */
266 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
268 /* Skip the first entry (module description string) */
269 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
272 if (((BYTE
)*cpnt
== len
) && !memcmp( cpnt
+1, buffer
, len
))
274 TRACE(" Found: ordinal=%d\n",
275 *(WORD
*)(cpnt
+ *cpnt
+ 1) );
276 return *(WORD
*)(cpnt
+ *cpnt
+ 1);
278 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
284 /***********************************************************************
285 * NE_GetEntryPoint (WPROCS.27)
287 * Return the entry point for a given ordinal.
289 FARPROC16 WINAPI
WIN16_NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
291 FARPROC16 ret
= NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
292 CURRENT_STACK16
->ecx
= hModule
; /* FIXME: might be incorrect value */
295 FARPROC16 WINAPI
NE_GetEntryPoint( HMODULE16 hModule
, WORD ordinal
)
297 return NE_GetEntryPointEx( hModule
, ordinal
, TRUE
);
299 FARPROC16
NE_GetEntryPointEx( HMODULE16 hModule
, WORD ordinal
, BOOL16 snoop
)
307 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
308 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
310 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
311 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
315 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
318 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
319 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
323 offset
= entry
->offs
;
325 if (sel
== 0xfe) sel
= 0xffff; /* constant entry */
326 else sel
= GlobalHandleToSel16(NE_SEG_TABLE(pModule
)[sel
-1].hSeg
);
328 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
330 return (FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
);
332 return (FARPROC16
)SNOOP16_GetProcAddress16(hModule
,ordinal
,(FARPROC16
)PTR_SEG_OFF_TO_SEGPTR( sel
, offset
));
336 /***********************************************************************
339 * Change the value of an entry point. Use with caution!
340 * It can only change the offset value, not the selector.
342 BOOL16
NE_SetEntryPoint( HMODULE16 hModule
, WORD ordinal
, WORD offset
)
349 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
350 assert( !(pModule
->flags
& NE_FFLAGS_WIN32
) );
352 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ pModule
->entry_table
);
353 while ((ordinal
< bundle
->first
+ 1) || (ordinal
> bundle
->last
))
355 bundle
= (ET_BUNDLE
*)((BYTE
*)pModule
+ bundle
->next
);
360 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
361 for (i
=0; i
< (ordinal
- bundle
->first
- 1); i
++)
364 entry
->offs
= offset
;
369 /***********************************************************************
372 HANDLE
NE_OpenFile( NE_MODULE
*pModule
)
376 static HANDLE cachedfd
= -1;
378 TRACE("(%p) cache: mod=%p fd=%d\n",
379 pModule
, pCachedModule
, cachedfd
);
380 if (pCachedModule
== pModule
) return cachedfd
;
381 CloseHandle( cachedfd
);
382 pCachedModule
= pModule
;
383 name
= NE_MODULE_NAME( pModule
);
384 if ((cachedfd
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
385 NULL
, OPEN_EXISTING
, 0, -1 )) == -1)
386 MESSAGE( "Can't open file '%s' for module %04x\n", name
, pModule
->self
);
388 /* FIXME: should not be necessary */
389 cachedfd
= ConvertToGlobalHandle(cachedfd
);
390 TRACE("opened '%s' -> %d\n",
396 /***********************************************************************
399 static HMODULE16
NE_LoadExeHeader( LPCSTR filename
)
401 IMAGE_DOS_HEADER mz_header
;
402 IMAGE_OS2_HEADER ne_header
;
406 BYTE
*pData
, *pTempEntryTable
;
407 char *buffer
, *fastload
= NULL
;
408 int fastload_offset
= 0, fastload_length
= 0;
410 ET_BUNDLE
*bundle
, *oldbundle
;
415 if ((hFile
= OpenFile16( filename
, &ofs
, OF_READ
)) == HFILE_ERROR16
)
416 return (HMODULE16
)2; /* File not found */
418 /* Read a block from either the file or the fast-load area. */
419 #define READ(offset,size,buffer) \
420 ((fastload && ((offset) >= fastload_offset) && \
421 ((offset)+(size) <= fastload_offset+fastload_length)) ? \
422 (memcpy( buffer, fastload+(offset)-fastload_offset, (size) ), TRUE) : \
423 (_llseek16( hFile, (offset), SEEK_SET), \
424 _hread16( hFile, (buffer), (size) ) == (size)))
426 _llseek16( hFile
, 0, SEEK_SET
);
427 if ((_hread16(hFile
,&mz_header
,sizeof(mz_header
)) != sizeof(mz_header
)) ||
428 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
431 return (HMODULE16
)11; /* invalid exe */
434 _llseek16( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
435 if (_hread16( hFile
, &ne_header
, sizeof(ne_header
) ) != sizeof(ne_header
))
438 return (HMODULE16
)11; /* invalid exe */
441 if (ne_header
.ne_magic
== IMAGE_NT_SIGNATURE
) return (HMODULE16
)21; /* win32 exe */
442 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return (HMODULE16
)11; /* invalid exe */
444 if (ne_header
.ne_magic
== IMAGE_OS2_SIGNATURE_LX
) {
445 MESSAGE("Sorry, this is an OS/2 linear executable (LX) file !\n");
447 return (HMODULE16
)12;
450 /* We now have a valid NE header */
452 size
= sizeof(NE_MODULE
) +
454 ne_header
.n_segment_tab
* sizeof(SEGTABLEENTRY
) +
456 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
+
457 /* resident names table */
458 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
+
459 /* module ref table */
460 ne_header
.n_mod_ref_tab
* sizeof(WORD
) +
461 /* imported names table */
462 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
+
463 /* entry table length */
464 ne_header
.entry_tab_length
+
465 /* entry table extra conversion space */
467 2 * (ne_header
.entry_tab_length
- ne_header
.n_mov_entry_points
*6) +
468 /* loaded file info */
469 sizeof(OFSTRUCT
)-sizeof(ofs
.szPathName
)+strlen(ofs
.szPathName
)+1;
471 hModule
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, size
);
475 return (HMODULE16
)11; /* invalid exe */
478 FarSetOwner16( hModule
, hModule
);
479 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
480 memcpy( pModule
, &ne_header
, sizeof(ne_header
) );
482 /* check *programs* for default minimal stack size */
483 if ( (!(pModule
->flags
& NE_FFLAGS_LIBMODULE
))
484 && (pModule
->stack_size
< 0x1400) )
485 pModule
->stack_size
= 0x1400;
486 pModule
->module32
= 0;
487 pModule
->self
= hModule
;
488 pModule
->self_loading_sel
= 0;
489 pData
= (BYTE
*)(pModule
+ 1);
491 /* Clear internal Wine flags in case they are set in the EXE file */
493 pModule
->flags
&= ~(NE_FFLAGS_BUILTIN
| NE_FFLAGS_WIN32
);
495 /* Read the fast-load area */
497 if (ne_header
.additional_flags
& NE_AFLAGS_FASTLOAD
)
499 fastload_offset
=ne_header
.fastload_offset
<<ne_header
.align_shift_count
;
500 fastload_length
=ne_header
.fastload_length
<<ne_header
.align_shift_count
;
501 TRACE("Using fast-load area offset=%x len=%d\n",
502 fastload_offset
, fastload_length
);
503 if ((fastload
= HeapAlloc( GetProcessHeap(), 0, fastload_length
)) != NULL
)
505 _llseek16( hFile
, fastload_offset
, SEEK_SET
);
506 if (_hread16(hFile
, fastload
, fastload_length
) != fastload_length
)
508 HeapFree( GetProcessHeap(), 0, fastload
);
509 WARN("Error reading fast-load area!\n");
515 /* Get the segment table */
517 pModule
->seg_table
= (int)pData
- (int)pModule
;
518 buffer
= HeapAlloc( GetProcessHeap(), 0, ne_header
.n_segment_tab
*
519 sizeof(struct ne_segment_table_entry_s
));
523 struct ne_segment_table_entry_s
*pSeg
;
525 if (!READ( mz_header
.e_lfanew
+ ne_header
.segment_tab_offset
,
526 ne_header
.n_segment_tab
* sizeof(struct ne_segment_table_entry_s
),
529 HeapFree( GetProcessHeap(), 0, buffer
);
531 HeapFree( GetProcessHeap(), 0, fastload
);
532 GlobalFree16( hModule
);
534 return (HMODULE16
)11; /* invalid exe */
536 pSeg
= (struct ne_segment_table_entry_s
*)buffer
;
537 for (i
= ne_header
.n_segment_tab
; i
> 0; i
--, pSeg
++)
539 memcpy( pData
, pSeg
, sizeof(*pSeg
) );
540 pData
+= sizeof(SEGTABLEENTRY
);
542 HeapFree( GetProcessHeap(), 0, buffer
);
547 HeapFree( GetProcessHeap(), 0, fastload
);
548 GlobalFree16( hModule
);
550 return (HMODULE16
)11; /* invalid exe */
553 /* Get the resource table */
555 if (ne_header
.resource_tab_offset
< ne_header
.rname_tab_offset
)
557 pModule
->res_table
= (int)pData
- (int)pModule
;
558 if (!READ(mz_header
.e_lfanew
+ ne_header
.resource_tab_offset
,
559 ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
,
563 return (HMODULE16
)11; /* invalid exe */
565 pData
+= ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
;
566 NE_InitResourceHandler( hModule
);
568 else pModule
->res_table
= 0; /* No resource table */
570 /* Get the resident names table */
572 pModule
->name_table
= (int)pData
- (int)pModule
;
573 if (!READ( mz_header
.e_lfanew
+ ne_header
.rname_tab_offset
,
574 ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
,
578 HeapFree( GetProcessHeap(), 0, fastload
);
579 GlobalFree16( hModule
);
581 return (HMODULE16
)11; /* invalid exe */
583 pData
+= ne_header
.moduleref_tab_offset
- ne_header
.rname_tab_offset
;
585 /* Get the module references table */
587 if (ne_header
.n_mod_ref_tab
> 0)
589 pModule
->modref_table
= (int)pData
- (int)pModule
;
590 if (!READ( mz_header
.e_lfanew
+ ne_header
.moduleref_tab_offset
,
591 ne_header
.n_mod_ref_tab
* sizeof(WORD
),
595 HeapFree( GetProcessHeap(), 0, fastload
);
596 GlobalFree16( hModule
);
598 return (HMODULE16
)11; /* invalid exe */
600 pData
+= ne_header
.n_mod_ref_tab
* sizeof(WORD
);
602 else pModule
->modref_table
= 0; /* No module references */
604 /* Get the imported names table */
606 pModule
->import_table
= (int)pData
- (int)pModule
;
607 if (!READ( mz_header
.e_lfanew
+ ne_header
.iname_tab_offset
,
608 ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
,
612 HeapFree( GetProcessHeap(), 0, fastload
);
613 GlobalFree16( hModule
);
615 return (HMODULE16
)11; /* invalid exe */
617 pData
+= ne_header
.entry_tab_offset
- ne_header
.iname_tab_offset
;
619 /* Load entry table, convert it to the optimized version used by Windows */
621 if ((pTempEntryTable
= HeapAlloc( GetProcessHeap(), 0, ne_header
.entry_tab_length
)) != NULL
)
623 BYTE nr_entries
, type
, *s
;
625 TRACE("Converting entry table.\n");
626 pModule
->entry_table
= (int)pData
- (int)pModule
;
627 if (!READ( mz_header
.e_lfanew
+ ne_header
.entry_tab_offset
,
628 ne_header
.entry_tab_length
, pTempEntryTable
))
630 HeapFree( GetProcessHeap(), 0, pTempEntryTable
);
632 HeapFree( GetProcessHeap(), 0, fastload
);
633 GlobalFree16( hModule
);
635 return (HMODULE16
)11; /* invalid exe */
639 TRACE("entry table: offs %04x, len %04x, entries %d\n", ne_header
.entry_tab_offset
, ne_header
.entry_tab_length
, *s
);
641 bundle
= (ET_BUNDLE
*)pData
;
642 TRACE("first bundle: %p\n", bundle
);
643 memset(bundle
, 0, sizeof(ET_BUNDLE
)); /* in case no entry table exists */
644 entry
= (ET_ENTRY
*)((BYTE
*)bundle
+6);
646 while ((nr_entries
= *s
++))
650 bundle
->last
+= nr_entries
;
657 entry
->segnum
= *s
++;
658 entry
->offs
= *(WORD
*)s
; s
+= 2;
659 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
667 entry
->segnum
= type
;
668 entry
->offs
= *(WORD
*)s
; s
+= 2;
669 /*TRACE(module, "entry: %p, type: %d, flags: %d, segnum: %d, offs: %04x\n", entry, entry->type, entry->flags, entry->segnum, entry->offs);*/
675 if (bundle
->first
== bundle
->last
)
677 bundle
->first
+= nr_entries
;
678 bundle
->last
+= nr_entries
;
683 oldbundle
->next
= ((int)entry
- (int)pModule
);
684 bundle
= (ET_BUNDLE
*)entry
;
685 TRACE("new bundle: %p\n", bundle
);
686 bundle
->first
= bundle
->last
=
687 oldbundle
->last
+ nr_entries
;
689 (BYTE
*)entry
+= sizeof(ET_BUNDLE
);
693 HeapFree( GetProcessHeap(), 0, pTempEntryTable
);
698 HeapFree( GetProcessHeap(), 0, fastload
);
699 GlobalFree16( hModule
);
701 return (HMODULE16
)11; /* invalid exe */
704 pData
+= ne_header
.entry_tab_length
+ sizeof(ET_BUNDLE
) +
705 2 * (ne_header
.entry_tab_length
- ne_header
.n_mov_entry_points
*6);
707 if ((DWORD
)entry
> (DWORD
)pData
)
708 ERR("converted entry table bigger than reserved space !!!\nentry: %p, pData: %p. Please report !\n", entry
, pData
);
710 /* Store the filename information */
712 pModule
->fileinfo
= (int)pData
- (int)pModule
;
713 size
= sizeof(OFSTRUCT
)-sizeof(ofs
.szPathName
)+strlen(ofs
.szPathName
)+1;
714 ofs
.cBytes
= size
- 1;
715 memcpy( pData
, &ofs
, size
);
718 /* Free the fast-load area */
722 HeapFree( GetProcessHeap(), 0, fastload
);
724 /* Get the non-resident names table */
726 if (ne_header
.nrname_tab_length
)
728 pModule
->nrname_handle
= GLOBAL_Alloc( 0, ne_header
.nrname_tab_length
,
729 hModule
, FALSE
, FALSE
, FALSE
);
730 if (!pModule
->nrname_handle
)
732 GlobalFree16( hModule
);
734 return (HMODULE16
)11; /* invalid exe */
736 buffer
= GlobalLock16( pModule
->nrname_handle
);
737 _llseek16( hFile
, ne_header
.nrname_tab_offset
, SEEK_SET
);
738 if (_hread16( hFile
, buffer
, ne_header
.nrname_tab_length
)
739 != ne_header
.nrname_tab_length
)
741 GlobalFree16( pModule
->nrname_handle
);
742 GlobalFree16( hModule
);
744 return (HMODULE16
)11; /* invalid exe */
747 else pModule
->nrname_handle
= 0;
749 /* Allocate a segment for the implicitly-loaded DLLs */
751 if (pModule
->modref_count
)
753 pModule
->dlls_to_init
= GLOBAL_Alloc(GMEM_ZEROINIT
,
754 (pModule
->modref_count
+1)*sizeof(HMODULE16
),
755 hModule
, FALSE
, FALSE
, FALSE
);
756 if (!pModule
->dlls_to_init
)
758 if (pModule
->nrname_handle
) GlobalFree16( pModule
->nrname_handle
);
759 GlobalFree16( hModule
);
761 return (HMODULE16
)11; /* invalid exe */
764 else pModule
->dlls_to_init
= 0;
766 NE_RegisterModule( pModule
);
767 SNOOP16_RegisterDLL(pModule
,ofs
.szPathName
);
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 */
819 pOldDLL
= NE_GetPtr( *pModRef
);
820 if (pOldDLL
) pOldDLL
->count
++;
827 /**********************************************************************
830 * Load first instance of NE module from file.
832 * pModule must point to a module structure prepared by NE_LoadExeHeader.
833 * This routine must never be called twice on a module.
836 static HINSTANCE16
NE_DoLoadModule( NE_MODULE
*pModule
)
838 /* Allocate the segments for this module */
840 if (!NE_CreateAllSegments( pModule
))
841 return ERROR_NOT_ENOUGH_MEMORY
; /* 8 */
843 /* Load the referenced DLLs */
845 if (!NE_LoadDLLs( pModule
))
846 return ERROR_FILE_NOT_FOUND
; /* 2 */
848 /* Load the segments */
850 NE_LoadAllSegments( pModule
);
852 /* Make sure the usage count is 1 on the first loading of */
853 /* the module, even if it contains circular DLL references */
857 return NE_GetInstance( pModule
);
860 /**********************************************************************
863 * Load first instance of NE module. (Note: caller is responsible for
864 * ensuring the module isn't already loaded!)
866 * If the module turns out to be an executable module, only a
867 * handle to a module stub is returned; this needs to be initialized
868 * by calling NE_DoLoadModule later, in the context of the newly
871 * If lib_only is TRUE, however, the module is perforce treated
872 * like a DLL module, even if it is an executable module.
875 static HINSTANCE16
NE_LoadModule( LPCSTR name
, BOOL lib_only
)
879 HINSTANCE16 hInstance
;
881 hModule
= NE_LoadExeHeader( name
);
882 if (hModule
< 32) return hModule
;
884 pModule
= NE_GetPtr( hModule
);
885 if ( !pModule
) return hModule
;
887 if ( !lib_only
&& !( pModule
->flags
& NE_FFLAGS_LIBMODULE
) )
890 hInstance
= NE_DoLoadModule( pModule
);
891 if ( hInstance
< 32 )
894 NE_FreeModule( hModule
, 0 );
901 /**********************************************************************
902 * MODULE_LoadModule16
904 * Load a NE module in the order of the loadorder specification.
905 * The caller is responsible that the module is not loaded already.
908 static HINSTANCE16
MODULE_LoadModule16( LPCSTR libname
, BOOL implicit
, BOOL lib_only
)
912 module_loadorder_t
*plo
;
914 plo
= MODULE_GetLoadOrder(libname
);
916 for(i
= 0; i
< MODULE_LOADORDER_NTYPES
; i
++)
918 switch(plo
->loadorder
[i
])
920 case MODULE_LOADORDER_DLL
:
921 TRACE("Trying native dll '%s'\n", libname
);
922 hinst
= NE_LoadModule(libname
, lib_only
);
925 case MODULE_LOADORDER_ELFDLL
:
926 TRACE("Trying elfdll '%s'\n", libname
);
927 hinst
= ELFDLL_LoadModule16(libname
);
930 case MODULE_LOADORDER_BI
:
931 TRACE("Trying built-in '%s'\n", libname
);
932 hinst
= BUILTIN_LoadModule(libname
);
936 ERR("Got invalid loadorder type %d (%s index %d)\n", plo
->loadorder
[i
], plo
->modulename
, i
);
939 case MODULE_LOADORDER_SO
: /* This is not supported for NE modules */
940 case MODULE_LOADORDER_INVALID
: /* We ignore this as it is an empty entry */
952 hModule
= GetModuleHandle16(libname
);
955 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get module handle\n",
957 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
960 pModule
= NE_GetPtr(hModule
);
963 ERR("Serious trouble. Just loaded module '%s' (hinst=0x%04x), but can't get NE_MODULE pointer\n",
965 return 6; /* ERROR_INVALID_HANDLE seems most appropriate */
968 TRACE("Loaded module '%s' at 0x%04x, \n", libname
, hinst
);
971 * Call initialization routines for all loaded DLLs. Note that
972 * when we load implicitly linked DLLs this will be done by InitTask().
974 if(pModule
->flags
& NE_FFLAGS_LIBMODULE
)
976 NE_InitializeDLLs(hModule
);
977 NE_DllProcessAttach(hModule
);
985 /* We quit searching when we get another error than 'File not found' */
989 return hinst
; /* The last error that occured */
993 /**********************************************************************
994 * LoadModule16 (KERNEL.45)
996 HINSTANCE16 WINAPI
LoadModule16( LPCSTR name
, LPVOID paramBlock
)
998 BOOL lib_only
= !paramBlock
|| (paramBlock
== (LPVOID
)-1);
999 LOADPARAMS16
*params
;
1000 LPSTR cmd_line
, new_cmd_line
;
1002 STARTUPINFOA startup
;
1003 PROCESS_INFORMATION info
;
1010 if ( (hModule
= NE_GetModuleByFilename(name
) ) != 0 )
1012 /* Special case: second instance of an already loaded NE module */
1014 if ( !( pModule
= NE_GetPtr( hModule
) ) ) return (HINSTANCE16
)11;
1015 if ( pModule
->module32
) return (HINSTANCE16
)21;
1017 /* Increment refcount */
1023 /* Main case: load first instance of NE module */
1025 if ( (hModule
= MODULE_LoadModule16( name
, FALSE
, lib_only
)) < 32 )
1028 if ( !(pModule
= NE_GetPtr( hModule
)) )
1029 return (HINSTANCE16
)11;
1032 /* If library module, we just retrieve the instance handle */
1034 if ( ( pModule
->flags
& NE_FFLAGS_LIBMODULE
) || lib_only
)
1035 return NE_GetInstance( pModule
);
1038 * At this point, we need to create a new process.
1040 * pModule points either to an already loaded module, whose refcount
1041 * has already been incremented (to avoid having the module vanish
1042 * in the meantime), or else to a stub module which contains only header
1045 * All remaining initialization (really loading the module in the second
1046 * case, and creating the new instance in both cases) are to be done in
1047 * the context of the new process. This happens in the NE_InitProcess
1048 * routine, which will be called from the 32-bit process initialization.
1052 params
= (LOADPARAMS16
*)paramBlock
;
1053 cmd_line
= (LPSTR
)PTR_SEG_TO_LIN( params
->cmdLine
);
1054 if (!cmd_line
) cmd_line
= "";
1055 else if (*cmd_line
) cmd_line
++; /* skip the length byte */
1057 if (!(new_cmd_line
= HeapAlloc( GetProcessHeap(), 0,
1058 strlen(cmd_line
)+strlen(name
)+2 )))
1060 strcpy( new_cmd_line
, name
);
1061 strcat( new_cmd_line
, " " );
1062 strcat( new_cmd_line
, cmd_line
);
1064 if (params
->hEnvironment
) env
= GlobalLock16( params
->hEnvironment
);
1066 memset( &info
, '\0', sizeof(info
) );
1067 memset( &startup
, '\0', sizeof(startup
) );
1068 startup
.cb
= sizeof(startup
);
1069 if (params
->showCmd
)
1071 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
1072 startup
.wShowWindow
= ((UINT16
*)PTR_SEG_TO_LIN(params
->showCmd
))[1];
1075 SYSLEVEL_ReleaseWin16Lock();
1076 pdb
= PROCESS_Create( pModule
, -1, new_cmd_line
, env
,
1077 NULL
, NULL
, TRUE
, 0, &startup
, &info
);
1078 SYSLEVEL_RestoreWin16Lock();
1080 CloseHandle( info
.hThread
);
1081 CloseHandle( info
.hProcess
);
1083 if (params
->hEnvironment
) GlobalUnlock16( params
->hEnvironment
);
1084 HeapFree( GetProcessHeap(), 0, new_cmd_line
);
1086 return GetProcessDword( info
.dwProcessId
, GPD_HINSTANCE16
);
1089 /**********************************************************************
1092 BOOL
NE_CreateProcess( HANDLE hFile
, LPCSTR filename
, LPCSTR cmd_line
, LPCSTR env
,
1093 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
1094 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
1095 LPPROCESS_INFORMATION info
)
1100 SYSLEVEL_EnterWin16Lock();
1102 /* Special case: second instance of an already loaded NE module
1103 * FIXME: maybe we should mark the module in a special way during
1104 * "second instance" loading stage ?
1105 * NE_CreateSegment and NE_LoadSegment might get confused without it,
1106 * especially when it comes to self-loaders */
1108 if ( ( hModule
= NE_GetModuleByFilename( filename
) ) != 0 )
1110 if ( !( pModule
= NE_GetPtr( hModule
) )
1111 || ( pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1112 || pModule
->module32
)
1114 SetLastError( ERROR_BAD_FORMAT
);
1121 /* Main case: load first instance of NE module */
1126 hModule
= NE_LoadExeHeader( filename
);
1129 SetLastError( hModule
);
1133 if ( !( pModule
= NE_GetPtr( hModule
) )
1134 || ( pModule
->flags
& NE_FFLAGS_LIBMODULE
) )
1136 GlobalFreeAll16( hModule
);
1137 SetLastError( ERROR_BAD_FORMAT
);
1142 SYSLEVEL_LeaveWin16Lock();
1144 if ( !PROCESS_Create( pModule
, hFile
, cmd_line
, env
,
1145 psa
, tsa
, inherit
, flags
, startup
, info
) )
1151 SYSLEVEL_LeaveWin16Lock();
1155 /**********************************************************************
1158 BOOL
NE_InitProcess( NE_MODULE
*pModule
)
1160 HINSTANCE16 hInstance
, hPrevInstance
;
1163 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
1167 SYSLEVEL_EnterWin16Lock();
1169 if ( pModule
->count
> 0 )
1171 /* Second instance of an already loaded NE module */
1172 /* Note that the refcount was already incremented by the parent */
1174 hPrevInstance
= NE_GetInstance( pModule
);
1176 if ( pModule
->dgroup
)
1177 if ( NE_CreateSegment( pModule
, pModule
->dgroup
) )
1178 NE_LoadSegment( pModule
, pModule
->dgroup
);
1180 hInstance
= NE_GetInstance( pModule
);
1181 TRACE("created second instance %04x[%d] of instance %04x.\n", hInstance
, pModule
->dgroup
, hPrevInstance
);
1186 /* Load first instance of NE module */
1188 pModule
->flags
|= NE_FFLAGS_GUI
; /* FIXME: is this necessary? */
1190 hInstance
= NE_DoLoadModule( pModule
);
1194 if ( hInstance
< 32 )
1196 SYSLEVEL_LeaveWin16Lock();
1198 SetLastError( hInstance
);
1202 /* Enter instance handles into task struct */
1204 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1205 pTask
->hInstance
= hInstance
;
1206 pTask
->hPrevInstance
= hPrevInstance
;
1208 /* Use DGROUP for 16-bit stack */
1210 if (!(sp
= pModule
->sp
))
1211 sp
= pSegTable
[pModule
->ss
-1].minsize
+ pModule
->stack_size
;
1212 sp
&= ~1; sp
-= sizeof(STACK16FRAME
);
1213 pTask
->teb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( hInstance
, sp
);
1215 SYSLEVEL_LeaveWin16Lock();
1220 /***********************************************************************
1221 * LoadLibrary16 (KERNEL.95)
1223 HINSTANCE16 WINAPI
LoadLibrary16( LPCSTR libname
)
1225 return LoadModule16(libname
, (LPVOID
)-1 );
1229 /**********************************************************************
1232 * Call a DLL's WEP, allowing it to shut down.
1233 * FIXME: we always pass the WEP WEP_FREE_DLL, never WEP_SYSTEM_EXIT
1235 static BOOL16
MODULE_CallWEP( HMODULE16 hModule
)
1237 FARPROC16 WEP
= (FARPROC16
)0;
1238 WORD ordinal
= NE_GetOrdinal( hModule
, "WEP" );
1240 if (ordinal
) WEP
= NE_GetEntryPoint( hModule
, ordinal
);
1243 WARN("module %04x doesn't have a WEP\n", hModule
);
1246 return Callbacks
->CallWindowsExitProc( WEP
, WEP_FREE_DLL
);
1250 /**********************************************************************
1253 * Implementation of FreeModule16().
1255 static BOOL16
NE_FreeModule( HMODULE16 hModule
, BOOL call_wep
)
1257 HMODULE16
*hPrevModule
;
1262 if (!(pModule
= NE_GetPtr( hModule
))) return FALSE
;
1263 hModule
= pModule
->self
;
1265 TRACE("%04x count %d\n", hModule
, pModule
->count
);
1267 if (((INT16
)(--pModule
->count
)) > 0 ) return TRUE
;
1268 else pModule
->count
= 0;
1270 if (pModule
->flags
& NE_FFLAGS_BUILTIN
)
1271 return FALSE
; /* Can't free built-in module */
1273 if (call_wep
&& !(pModule
->flags
& NE_FFLAGS_WIN32
))
1275 if (pModule
->flags
& NE_FFLAGS_LIBMODULE
)
1277 MODULE_CallWEP( hModule
);
1279 /* Free the objects owned by the DLL module */
1280 TASK_CallTaskSignalProc( USIG16_DLL_UNLOAD
, hModule
);
1281 PROCESS_CallUserSignalProc( USIG_DLL_UNLOAD_WIN16
, hModule
);
1284 call_wep
= FALSE
; /* We are freeing a task -> no more WEPs */
1288 /* Clear magic number just in case */
1290 pModule
->magic
= pModule
->self
= 0;
1292 /* Remove it from the linked list */
1294 hPrevModule
= &hFirstModule
;
1295 while (*hPrevModule
&& (*hPrevModule
!= hModule
))
1297 hPrevModule
= &(NE_GetPtr( *hPrevModule
))->next
;
1299 if (*hPrevModule
) *hPrevModule
= pModule
->next
;
1301 /* Free the referenced modules */
1303 pModRef
= (HMODULE16
*)NE_MODULE_TABLE( pModule
);
1304 for (i
= 0; i
< pModule
->modref_count
; i
++, pModRef
++)
1306 NE_FreeModule( *pModRef
, call_wep
);
1309 /* Free the module storage */
1311 GlobalFreeAll16( hModule
);
1313 /* Remove module from cache */
1315 if (pCachedModule
== pModule
) pCachedModule
= NULL
;
1320 /**********************************************************************
1321 * FreeModule16 (KERNEL.46)
1323 BOOL16 WINAPI
FreeModule16( HMODULE16 hModule
)
1325 return NE_FreeModule( hModule
, TRUE
);
1329 /***********************************************************************
1330 * FreeLibrary16 (KERNEL.96)
1332 void WINAPI
FreeLibrary16( HINSTANCE16 handle
)
1334 TRACE("%04x\n", handle
);
1335 FreeModule16( handle
);
1339 /**********************************************************************
1340 * GetModuleName (KERNEL.27)
1342 BOOL16 WINAPI
GetModuleName16( HINSTANCE16 hinst
, LPSTR buf
, INT16 count
)
1347 if (!(pModule
= NE_GetPtr( hinst
))) return FALSE
;
1348 p
= (BYTE
*)pModule
+ pModule
->name_table
;
1349 if (count
> *p
) count
= *p
+ 1;
1352 memcpy( buf
, p
+ 1, count
- 1 );
1353 buf
[count
-1] = '\0';
1359 /**********************************************************************
1360 * GetModuleUsage (KERNEL.48)
1362 INT16 WINAPI
GetModuleUsage16( HINSTANCE16 hModule
)
1364 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1365 return pModule
? pModule
->count
: 0;
1369 /**********************************************************************
1370 * GetExpWinVer (KERNEL.167)
1372 WORD WINAPI
GetExpWinVer16( HMODULE16 hModule
)
1374 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
1375 if ( !pModule
) return 0;
1378 * For built-in modules, fake the expected version the module should
1379 * have according to the Windows version emulated by Wine
1381 if ( !pModule
->expected_version
)
1383 OSVERSIONINFOA versionInfo
;
1384 versionInfo
.dwOSVersionInfoSize
= sizeof(versionInfo
);
1386 if ( GetVersionExA( &versionInfo
) )
1387 pModule
->expected_version
=
1388 (versionInfo
.dwMajorVersion
& 0xff) << 8
1389 | (versionInfo
.dwMinorVersion
& 0xff);
1392 return pModule
->expected_version
;
1396 /**********************************************************************
1397 * GetModuleFileName16 (KERNEL.49)
1399 * Comment: see GetModuleFileNameA
1401 * Even if invoked by second instance of a program,
1402 * it still returns path of first one.
1404 INT16 WINAPI
GetModuleFileName16( HINSTANCE16 hModule
, LPSTR lpFileName
,
1409 /* Win95 does not query hModule if set to 0 !
1410 * Is this wrong or maybe Win3.1 only ? */
1411 if (!hModule
) hModule
= GetCurrentTask();
1413 if (!(pModule
= NE_GetPtr( hModule
))) return 0;
1414 if (pModule
->expected_version
>= 0x400)
1415 GetLongPathNameA(NE_MODULE_NAME(pModule
), lpFileName
, nSize
);
1417 lstrcpynA( lpFileName
, NE_MODULE_NAME(pModule
), nSize
);
1418 TRACE("%s\n", lpFileName
);
1419 return strlen(lpFileName
);
1423 /**********************************************************************
1424 * GetModuleHandle16 (KERNEL.47)
1426 * Find a module from a module name.
1428 * NOTE: The current implementation works the same way the Windows 95 one
1429 * does. Do not try to 'fix' it, fix the callers.
1430 * + It does not do ANY extension handling (except that strange .EXE bit)!
1431 * + It does not care about paths, just about basenames. (same as Windows)
1435 * the win16 module handle if found
1437 * HIWORD (undocumented, see "Undocumented Windows", chapter 5):
1438 * Always hFirstModule
1440 DWORD WINAPI
WIN16_GetModuleHandle( SEGPTR name
)
1442 if (HIWORD(name
) == 0)
1443 return MAKELONG(GetExePtr( (HINSTANCE16
)name
), hFirstModule
);
1444 return MAKELONG(GetModuleHandle16( PTR_SEG_TO_LIN(name
)), hFirstModule
);
1447 HMODULE16 WINAPI
GetModuleHandle16( LPCSTR name
)
1449 HMODULE16 hModule
= hFirstModule
;
1451 BYTE len
, *name_table
;
1452 char tmpstr
[MAX_PATH
];
1455 TRACE("(%s)\n", name
);
1458 return GetExePtr(LOWORD(name
));
1464 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1466 /* If 'name' matches exactly the module name of a module:
1467 * Return its handle.
1469 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1471 pModule
= NE_GetPtr( hModule
);
1472 if (!pModule
) break;
1473 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1475 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1476 if ((*name_table
== len
) && !strncmp(name
, name_table
+1, len
))
1480 /* If uppercased 'name' matches exactly the module name of a module:
1483 for (s
= tmpstr
; *s
; s
++)
1486 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1488 pModule
= NE_GetPtr( hModule
);
1489 if (!pModule
) break;
1490 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1492 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1493 /* FIXME: the lstrncmpiA is WRONG. It should not be case insensitive,
1494 * but case sensitive! (Unfortunately Winword 6 and subdlls have
1495 * lowercased module names, but try to load uppercase DLLs, so this
1496 * 'i' compare is just a quickfix until the loader handles that
1497 * correctly. -MM 990705
1499 if ((*name_table
== len
) && !lstrncmpiA(tmpstr
, name_table
+1, len
))
1503 /* If the base filename of 'name' matches the base filename of the module
1504 * filename of some module (case-insensitive compare):
1505 * Return its handle.
1508 /* basename: search backwards in passed name to \ / or : */
1509 s
= tmpstr
+ strlen(tmpstr
);
1512 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1517 /* search this in loaded filename list */
1518 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1523 pModule
= NE_GetPtr( hModule
);
1524 if (!pModule
) break;
1525 if (!pModule
->fileinfo
) continue;
1526 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1528 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1529 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1530 /* basename: search backwards in pathname to \ / or : */
1531 while (loadedfn
> (char*)ofs
->szPathName
)
1533 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1537 /* case insensitive compare ... */
1538 if (!lstrcmpiA(loadedfn
, s
))
1542 /* If the extension of 'name' is '.EXE' and the base filename of 'name'
1543 * matches the base filename of the module filename of some 32-bit module:
1544 * Return the corresponding 16-bit dummy module handle.
1546 if (len
>= 4 && !strcasecmp(name
+len
-4, ".EXE"))
1548 HMODULE hModule
= GetModuleHandleA( name
);
1550 return MapHModuleLS( hModule
);
1553 if (!strcmp(tmpstr
,"MSDOS"))
1556 if (!strcmp(tmpstr
,"TIMER"))
1558 FIXME("Eh... Should return caller's code segment, expect crash\n");
1565 /**********************************************************************
1566 * NE_GetModuleByFilename
1568 static HMODULE16
NE_GetModuleByFilename( LPCSTR name
)
1572 BYTE len
, *name_table
;
1573 char tmpstr
[MAX_PATH
];
1576 lstrcpynA(tmpstr
, name
, sizeof(tmpstr
));
1578 /* If the base filename of 'name' matches the base filename of the module
1579 * filename of some module (case-insensitive compare):
1580 * Return its handle.
1583 /* basename: search backwards in passed name to \ / or : */
1584 s
= tmpstr
+ strlen(tmpstr
);
1587 if (s
[-1]=='/' || s
[-1]=='\\' || s
[-1]==':')
1592 /* search this in loaded filename list */
1593 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1598 pModule
= NE_GetPtr( hModule
);
1599 if (!pModule
) break;
1600 if (!pModule
->fileinfo
) continue;
1601 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1603 ofs
= (OFSTRUCT
*)((BYTE
*)pModule
+ pModule
->fileinfo
);
1604 loadedfn
= ((char*)ofs
->szPathName
) + strlen(ofs
->szPathName
);
1605 /* basename: search backwards in pathname to \ / or : */
1606 while (loadedfn
> (char*)ofs
->szPathName
)
1608 if (loadedfn
[-1]=='/' || loadedfn
[-1]=='\\' || loadedfn
[-1]==':')
1612 /* case insensitive compare ... */
1613 if (!lstrcmpiA(loadedfn
, s
))
1617 /* If basename (without ext) matches the module name of a module:
1618 * Return its handle.
1621 if ( (p
= strrchr( s
, '.' )) != NULL
) *p
= '\0';
1624 for (hModule
= hFirstModule
; hModule
; hModule
= pModule
->next
)
1626 pModule
= NE_GetPtr( hModule
);
1627 if (!pModule
) break;
1628 if (pModule
->flags
& NE_FFLAGS_WIN32
) continue;
1630 name_table
= (BYTE
*)pModule
+ pModule
->name_table
;
1631 if ((*name_table
== len
) && !lstrncmpiA(s
, name_table
+1, len
))
1638 /**********************************************************************
1639 * ModuleFirst (TOOLHELP.59)
1641 BOOL16 WINAPI
ModuleFirst16( MODULEENTRY
*lpme
)
1643 lpme
->wNext
= hFirstModule
;
1644 return ModuleNext16( lpme
);
1648 /**********************************************************************
1649 * ModuleNext (TOOLHELP.60)
1651 BOOL16 WINAPI
ModuleNext16( MODULEENTRY
*lpme
)
1656 if (!lpme
->wNext
) return FALSE
;
1657 if (!(pModule
= NE_GetPtr( lpme
->wNext
))) return FALSE
;
1658 name
= (char *)pModule
+ pModule
->name_table
;
1659 memcpy( lpme
->szModule
, name
+ 1, min(*name
, MAX_MODULE_NAME
) );
1660 lpme
->szModule
[min(*name
, MAX_MODULE_NAME
)] = '\0';
1661 lpme
->hModule
= lpme
->wNext
;
1662 lpme
->wcUsage
= pModule
->count
;
1663 lstrcpynA( lpme
->szExePath
, NE_MODULE_NAME(pModule
), sizeof(lpme
->szExePath
) );
1664 lpme
->wNext
= pModule
->next
;
1669 /**********************************************************************
1670 * ModuleFindName (TOOLHELP.61)
1672 BOOL16 WINAPI
ModuleFindName16( MODULEENTRY
*lpme
, LPCSTR name
)
1674 lpme
->wNext
= GetModuleHandle16( name
);
1675 return ModuleNext16( lpme
);
1679 /**********************************************************************
1680 * ModuleFindHandle (TOOLHELP.62)
1682 BOOL16 WINAPI
ModuleFindHandle16( MODULEENTRY
*lpme
, HMODULE16 hModule
)
1684 hModule
= GetExePtr( hModule
);
1685 lpme
->wNext
= hModule
;
1686 return ModuleNext16( lpme
);
1690 /***************************************************************************
1691 * IsRomModule16 (KERNEL.323)
1693 BOOL16 WINAPI
IsRomModule16( HMODULE16 unused
)
1698 /***************************************************************************
1699 * IsRomFile16 (KERNEL.326)
1701 BOOL16 WINAPI
IsRomFile16( HFILE16 unused
)
1706 /***************************************************************************
1707 * MapHModuleLS (KERNEL32.520)
1709 HMODULE16 WINAPI
MapHModuleLS(HMODULE hmod
) {
1713 return ((TDB
*)GlobalLock16(GetCurrentTask()))->hInstance
;
1715 return hmod
; /* we already have a 16 bit module handle */
1716 pModule
= (NE_MODULE
*)GlobalLock16(hFirstModule
);
1718 if (pModule
->module32
== hmod
)
1719 return pModule
->self
;
1720 pModule
= (NE_MODULE
*)GlobalLock16(pModule
->next
);
1725 /***************************************************************************
1726 * MapHModuleSL (KERNEL32.521)
1728 HMODULE WINAPI
MapHModuleSL(HMODULE16 hmod
) {
1732 TDB
*pTask
= (TDB
*)GlobalLock16(GetCurrentTask());
1734 hmod
= pTask
->hModule
;
1736 pModule
= (NE_MODULE
*)GlobalLock16(hmod
);
1737 if ( (pModule
->magic
!=IMAGE_OS2_SIGNATURE
) ||
1738 !(pModule
->flags
& NE_FFLAGS_WIN32
)
1741 return pModule
->module32
;
1744 /***************************************************************************
1745 * MapHInstLS (KERNEL32.516)(KERNEL.472)
1747 void WINAPI
MapHInstLS( CONTEXT86
*context
)
1749 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1752 /***************************************************************************
1753 * MapHInstSL (KERNEL32.518)(KERNEL.473)
1755 void WINAPI
MapHInstSL( CONTEXT86
*context
)
1757 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));
1760 /***************************************************************************
1761 * MapHInstLS_PN (KERNEL32.517)
1763 void WINAPI
MapHInstLS_PN( CONTEXT86
*context
)
1765 if (EAX_reg(context
))
1766 EAX_reg(context
) = MapHModuleLS(EAX_reg(context
));
1769 /***************************************************************************
1770 * MapHInstSL_PN (KERNEL32.519)
1772 void WINAPI
MapHInstSL_PN( CONTEXT86
*context
)
1774 if (EAX_reg(context
))
1775 EAX_reg(context
) = MapHModuleSL(EAX_reg(context
));