| 1 | /* $Id: winexebase.cpp,v 1.22 2003-01-13 16:51:40 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 exe base class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 | #define INCL_DOSFILEMGR          /* File Manager values      */ | 
|---|
| 13 | #define INCL_DOSERRORS           /* DOS Error values         */ | 
|---|
| 14 | #define INCL_DOSPROCESS          /* DOS Process values       */ | 
|---|
| 15 | #define INCL_DOSMISC             /* DOS Miscellanous values  */ | 
|---|
| 16 | #define INCL_WIN | 
|---|
| 17 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 18 | #include <stdio.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 | #include <stdlib.h> | 
|---|
| 21 | #ifndef __GNUC__ | 
|---|
| 22 | #include <iostream.h> | 
|---|
| 23 | #include <fstream.h> | 
|---|
| 24 | #endif | 
|---|
| 25 | #include <misc.h> | 
|---|
| 26 | #include <win32type.h> | 
|---|
| 27 | #include "winexebase.h" | 
|---|
| 28 | #include "windllbase.h" | 
|---|
| 29 | #include <wprocess.h> | 
|---|
| 30 | #include <pefile.h> | 
|---|
| 31 | #include <cpuhlp.h> | 
|---|
| 32 | #include "exceptions.h" | 
|---|
| 33 | #include "exceptutil.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include "conwin.h"          // Windows Header for console only | 
|---|
| 36 | #include "console.h" | 
|---|
| 37 |  | 
|---|
| 38 | #define DBG_LOCALLOG    DBG_winexebase | 
|---|
| 39 | #include "dbglocal.h" | 
|---|
| 40 |  | 
|---|
| 41 | BOOL         fExeStarted = FALSE; | 
|---|
| 42 | Win32ExeBase *WinExe = NULL; | 
|---|
| 43 |  | 
|---|
| 44 | //****************************************************************************** | 
|---|
| 45 | //****************************************************************************** | 
|---|
| 46 | extern "C" BOOL IsExeStarted() | 
|---|
| 47 | { | 
|---|
| 48 | return fExeStarted; | 
|---|
| 49 | } | 
|---|
| 50 | //****************************************************************************** | 
|---|
| 51 | //****************************************************************************** | 
|---|
| 52 | Win32ExeBase::Win32ExeBase(HINSTANCE hInstance) | 
|---|
| 53 | : Win32ImageBase(hInstance), | 
|---|
| 54 | fConsoleApp(FALSE) | 
|---|
| 55 | { | 
|---|
| 56 | WinExe = this; | 
|---|
| 57 | } | 
|---|
| 58 | //****************************************************************************** | 
|---|
| 59 | //****************************************************************************** | 
|---|
| 60 | Win32ExeBase::~Win32ExeBase() | 
|---|
| 61 | { | 
|---|
| 62 | QueueItem    *item; | 
|---|
| 63 | Win32DllBase *dll; | 
|---|
| 64 |  | 
|---|
| 65 | //First delete all dlls loaded by LoadLibrary | 
|---|
| 66 | //Then delete all dlls that were loaded by the exe | 
|---|
| 67 | //(NOTE: This is what NT does; first delete loadlib dlls in LIFO order and | 
|---|
| 68 | //       then the exe dlls) | 
|---|
| 69 | Win32DllBase::deleteDynamicLibs(); | 
|---|
| 70 |  | 
|---|
| 71 | dprintf(("Win32ExeBase::~Win32ExeBase")); | 
|---|
| 72 | #ifdef DEBUG_ENABLELOG_LEVEL2 | 
|---|
| 73 | item = loadedDlls.Head(); | 
|---|
| 74 | dll = (Win32DllBase *)loadedDlls.getItem(item); | 
|---|
| 75 | dll->printListOfDlls(); | 
|---|
| 76 | #endif | 
|---|
| 77 |  | 
|---|
| 78 | item = loadedDlls.Head(); | 
|---|
| 79 | while(item) { | 
|---|
| 80 | dll = (Win32DllBase *)loadedDlls.getItem(item); | 
|---|
| 81 | if(dll == NULL) { | 
|---|
| 82 | dprintf(("ERROR: Win32ExeBase::~Win32ExeBase: dll item == NULL!!")); | 
|---|
| 83 | DebugInt3(); | 
|---|
| 84 | break; | 
|---|
| 85 | } | 
|---|
| 86 | dll->Release(); | 
|---|
| 87 | item = loadedDlls.getNext(item); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | Win32DllBase::deleteAll(); | 
|---|
| 91 |  | 
|---|
| 92 | WinExe = NULL; | 
|---|
| 93 | } | 
|---|
| 94 | //****************************************************************************** | 
|---|
| 95 | //****************************************************************************** | 
|---|
| 96 | ULONG Win32ExeBase::start() | 
|---|
| 97 | { | 
|---|
| 98 | WINEXCEPTION_FRAME exceptFrame; | 
|---|
| 99 | ULONG rc; | 
|---|
| 100 |  | 
|---|
| 101 | #ifdef DEBUG | 
|---|
| 102 | TEB *teb = GetThreadTEB(); | 
|---|
| 103 | dprintf(("Start executable %x\n", WinExe)); | 
|---|
| 104 | dprintf(("Stack top 0x%x, stack end 0x%x", teb->stack_top, teb->stack_low)); | 
|---|
| 105 | #endif | 
|---|
| 106 |  | 
|---|
| 107 | fExeStarted  = TRUE; | 
|---|
| 108 |  | 
|---|
| 109 | //Note: The Win32 exception structure references by FS:[0] is the same | 
|---|
| 110 | //      in OS/2 | 
|---|
| 111 | OS2SetExceptionHandler((void *)&exceptFrame); | 
|---|
| 112 | USHORT sel = SetWin32TIB(isPEImage() ? TIB_SWITCH_FORCE_WIN32 : TIB_SWITCH_DEFAULT); | 
|---|
| 113 |  | 
|---|
| 114 | //Set FPU control word to 0x27F (same as in NT) | 
|---|
| 115 | CONTROL87(0x27F, 0xFFF); | 
|---|
| 116 | dprintf(("KERNEL32: Win32ExeBase::start exe at %08xh\n", | 
|---|
| 117 | (void*)entryPoint )); | 
|---|
| 118 | rc = CallEntryPoint(entryPoint, NULL); | 
|---|
| 119 |  | 
|---|
| 120 | SetFS(sel);           //restore FS | 
|---|
| 121 |  | 
|---|
| 122 | OS2UnsetExceptionHandler((void *)&exceptFrame); | 
|---|
| 123 |  | 
|---|
| 124 | return rc; | 
|---|
| 125 | } | 
|---|
| 126 | //****************************************************************************** | 
|---|
| 127 | //Default stack size is 1MB; the PE loader reads it from the executable header | 
|---|
| 128 | //****************************************************************************** | 
|---|
| 129 | ULONG Win32ExeBase::getDefaultStackSize() | 
|---|
| 130 | { | 
|---|
| 131 | return 0x100000; | 
|---|
| 132 | } | 
|---|
| 133 | //****************************************************************************** | 
|---|
| 134 | //****************************************************************************** | 
|---|
| 135 | BOOL Win32ExeBase::isDll() | 
|---|
| 136 | { | 
|---|
| 137 | return FALSE; | 
|---|
| 138 | } | 
|---|
| 139 | //****************************************************************************** | 
|---|
| 140 | //****************************************************************************** | 
|---|