[9667] | 1 | /* $Id: winexebase.cpp,v 1.22 2003-01-13 16:51:40 sandervl Exp $ */
|
---|
[956] | 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>
|
---|
[21916] | 21 | #ifndef __GNUC__
|
---|
[956] | 22 | #include <iostream.h>
|
---|
| 23 | #include <fstream.h>
|
---|
[21916] | 24 | #endif
|
---|
[956] | 25 | #include <misc.h>
|
---|
| 26 | #include <win32type.h>
|
---|
[21916] | 27 | #include "winexebase.h"
|
---|
| 28 | #include "windllbase.h"
|
---|
[956] | 29 | #include <wprocess.h>
|
---|
| 30 | #include <pefile.h>
|
---|
[5090] | 31 | #include <cpuhlp.h>
|
---|
[956] | 32 | #include "exceptions.h"
|
---|
| 33 | #include "exceptutil.h"
|
---|
| 34 |
|
---|
| 35 | #include "conwin.h" // Windows Header for console only
|
---|
| 36 | #include "console.h"
|
---|
| 37 |
|
---|
[2802] | 38 | #define DBG_LOCALLOG DBG_winexebase
|
---|
| 39 | #include "dbglocal.h"
|
---|
| 40 |
|
---|
[956] | 41 | BOOL fExeStarted = FALSE;
|
---|
| 42 | Win32ExeBase *WinExe = NULL;
|
---|
| 43 |
|
---|
| 44 | //******************************************************************************
|
---|
| 45 | //******************************************************************************
|
---|
[21916] | 46 | extern "C" BOOL IsExeStarted()
|
---|
[1670] | 47 | {
|
---|
| 48 | return fExeStarted;
|
---|
| 49 | }
|
---|
| 50 | //******************************************************************************
|
---|
| 51 | //******************************************************************************
|
---|
[4236] | 52 | Win32ExeBase::Win32ExeBase(HINSTANCE hInstance)
|
---|
[956] | 53 | : Win32ImageBase(hInstance),
|
---|
[4244] | 54 | fConsoleApp(FALSE)
|
---|
[956] | 55 | {
|
---|
| 56 | WinExe = this;
|
---|
| 57 | }
|
---|
| 58 | //******************************************************************************
|
---|
| 59 | //******************************************************************************
|
---|
| 60 | Win32ExeBase::~Win32ExeBase()
|
---|
| 61 | {
|
---|
[3059] | 62 | QueueItem *item;
|
---|
| 63 | Win32DllBase *dll;
|
---|
| 64 |
|
---|
[4236] | 65 | //First delete all dlls loaded by LoadLibrary
|
---|
[3059] | 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 |
|
---|
[956] | 90 | Win32DllBase::deleteAll();
|
---|
[3005] | 91 |
|
---|
[956] | 92 | WinExe = NULL;
|
---|
| 93 | }
|
---|
| 94 | //******************************************************************************
|
---|
| 95 | //******************************************************************************
|
---|
| 96 | ULONG Win32ExeBase::start()
|
---|
| 97 | {
|
---|
| 98 | WINEXCEPTION_FRAME exceptFrame;
|
---|
| 99 | ULONG rc;
|
---|
| 100 |
|
---|
[8457] | 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
|
---|
[956] | 106 |
|
---|
| 107 | fExeStarted = TRUE;
|
---|
| 108 |
|
---|
[1670] | 109 | //Note: The Win32 exception structure references by FS:[0] is the same
|
---|
| 110 | // in OS/2
|
---|
| 111 | OS2SetExceptionHandler((void *)&exceptFrame);
|
---|
[7811] | 112 | USHORT sel = SetWin32TIB(isPEImage() ? TIB_SWITCH_FORCE_WIN32 : TIB_SWITCH_DEFAULT);
|
---|
[1670] | 113 |
|
---|
[6132] | 114 | //Set FPU control word to 0x27F (same as in NT)
|
---|
| 115 | CONTROL87(0x27F, 0xFFF);
|
---|
[5962] | 116 | dprintf(("KERNEL32: Win32ExeBase::start exe at %08xh\n",
|
---|
| 117 | (void*)entryPoint ));
|
---|
[6133] | 118 | rc = CallEntryPoint(entryPoint, NULL);
|
---|
[956] | 119 |
|
---|
[7811] | 120 | SetFS(sel); //restore FS
|
---|
| 121 |
|
---|
[956] | 122 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
---|
| 123 |
|
---|
| 124 | return rc;
|
---|
| 125 | }
|
---|
| 126 | //******************************************************************************
|
---|
[9667] | 127 | //Default stack size is 1MB; the PE loader reads it from the executable header
|
---|
[956] | 128 | //******************************************************************************
|
---|
[9667] | 129 | ULONG Win32ExeBase::getDefaultStackSize()
|
---|
| 130 | {
|
---|
| 131 | return 0x100000;
|
---|
| 132 | }
|
---|
| 133 | //******************************************************************************
|
---|
| 134 | //******************************************************************************
|
---|
[956] | 135 | BOOL Win32ExeBase::isDll()
|
---|
| 136 | {
|
---|
[9667] | 137 | return FALSE;
|
---|
[956] | 138 | }
|
---|
| 139 | //******************************************************************************
|
---|
| 140 | //******************************************************************************
|
---|