| 1 | /* $Id: winexebase.cpp,v 1.9 2000-05-02 20:53:14 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 | #include <iostream.h>
|
|---|
| 22 | #include <fstream.h>
|
|---|
| 23 | #include <misc.h>
|
|---|
| 24 | #include <win32type.h>
|
|---|
| 25 | #include <winexebase.h>
|
|---|
| 26 | #include <windllbase.h>
|
|---|
| 27 | #include <wprocess.h>
|
|---|
| 28 | #include <pefile.h>
|
|---|
| 29 | #include "exceptions.h"
|
|---|
| 30 | #include "exceptutil.h"
|
|---|
| 31 | #include "cio.h"
|
|---|
| 32 |
|
|---|
| 33 | #include "conwin.h" // Windows Header for console only
|
|---|
| 34 | #include "console.h"
|
|---|
| 35 |
|
|---|
| 36 | #define DBG_LOCALLOG DBG_winexebase
|
|---|
| 37 | #include "dbglocal.h"
|
|---|
| 38 |
|
|---|
| 39 | BOOL fExeStarted = FALSE;
|
|---|
| 40 | Win32ExeBase *WinExe = NULL;
|
|---|
| 41 |
|
|---|
| 42 | //******************************************************************************
|
|---|
| 43 | //******************************************************************************
|
|---|
| 44 | BOOL IsExeStarted()
|
|---|
| 45 | {
|
|---|
| 46 | return fExeStarted;
|
|---|
| 47 | }
|
|---|
| 48 | //******************************************************************************
|
|---|
| 49 | //******************************************************************************
|
|---|
| 50 | Win32ExeBase::Win32ExeBase(HINSTANCE hInstance)
|
|---|
| 51 | : Win32ImageBase(hInstance),
|
|---|
| 52 | fConsoleApp(FALSE),
|
|---|
| 53 | cmdLineA(NULL), cmdLineW(NULL)
|
|---|
| 54 | {
|
|---|
| 55 | WinExe = this;
|
|---|
| 56 | }
|
|---|
| 57 | //******************************************************************************
|
|---|
| 58 | //******************************************************************************
|
|---|
| 59 | Win32ExeBase::~Win32ExeBase()
|
|---|
| 60 | {
|
|---|
| 61 | QueueItem *item;
|
|---|
| 62 | Win32DllBase *dll;
|
|---|
| 63 |
|
|---|
| 64 | //First delete all dlls loaded by LoadLibrary
|
|---|
| 65 | //Then delete all dlls that were loaded by the exe
|
|---|
| 66 | //(NOTE: This is what NT does; first delete loadlib dlls in LIFO order and
|
|---|
| 67 | // then the exe dlls)
|
|---|
| 68 | Win32DllBase::deleteDynamicLibs();
|
|---|
| 69 |
|
|---|
| 70 | dprintf(("Win32ExeBase::~Win32ExeBase"));
|
|---|
| 71 | #ifdef DEBUG_ENABLELOG_LEVEL2
|
|---|
| 72 | item = loadedDlls.Head();
|
|---|
| 73 | dll = (Win32DllBase *)loadedDlls.getItem(item);
|
|---|
| 74 | dll->printListOfDlls();
|
|---|
| 75 | #endif
|
|---|
| 76 |
|
|---|
| 77 | item = loadedDlls.Head();
|
|---|
| 78 | while(item) {
|
|---|
| 79 | dll = (Win32DllBase *)loadedDlls.getItem(item);
|
|---|
| 80 | if(dll == NULL) {
|
|---|
| 81 | dprintf(("ERROR: Win32ExeBase::~Win32ExeBase: dll item == NULL!!"));
|
|---|
| 82 | DebugInt3();
|
|---|
| 83 | break;
|
|---|
| 84 | }
|
|---|
| 85 | dll->Release();
|
|---|
| 86 | item = loadedDlls.getNext(item);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | Win32DllBase::deleteAll();
|
|---|
| 90 |
|
|---|
| 91 | WinExe = NULL;
|
|---|
| 92 | if(cmdLineA)
|
|---|
| 93 | free(cmdLineA);
|
|---|
| 94 | if(cmdLineW)
|
|---|
| 95 | free(cmdLineW);
|
|---|
| 96 | }
|
|---|
| 97 | //******************************************************************************
|
|---|
| 98 | //******************************************************************************
|
|---|
| 99 | ULONG Win32ExeBase::start()
|
|---|
| 100 | {
|
|---|
| 101 | WINEXCEPTION_FRAME exceptFrame;
|
|---|
| 102 | ULONG rc;
|
|---|
| 103 |
|
|---|
| 104 | if(getenv("WIN32_IOPL2")) {
|
|---|
| 105 | io_init1();
|
|---|
| 106 | }
|
|---|
| 107 | dprintf(("Start executable %X\n", WinExe));
|
|---|
| 108 |
|
|---|
| 109 | fExeStarted = TRUE;
|
|---|
| 110 |
|
|---|
| 111 | //Note: The Win32 exception structure references by FS:[0] is the same
|
|---|
| 112 | // in OS/2
|
|---|
| 113 | OS2SetExceptionHandler((void *)&exceptFrame);
|
|---|
| 114 | SetWin32TIB();
|
|---|
| 115 |
|
|---|
| 116 | //Allocate TLS index for this module
|
|---|
| 117 | tlsAlloc();
|
|---|
| 118 | tlsAttachThread(); //setup TLS (main thread)
|
|---|
| 119 |
|
|---|
| 120 | //Set default FPU control word (no exceptions); same as in NT
|
|---|
| 121 | _control87(0x27F, 0xFFF);
|
|---|
| 122 | ULONG Size = 16, Attr;
|
|---|
| 123 | rc = DosQueryMem((PVOID) (entryPoint), &Size, &Attr);
|
|---|
| 124 | dprintf(("Start exe; Attr = %x, rc = %d", Attr, rc));
|
|---|
| 125 | rc = ((WIN32EXEENTRY)entryPoint)(NULL);
|
|---|
| 126 | RestoreOS2TIB();
|
|---|
| 127 |
|
|---|
| 128 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
|---|
| 129 |
|
|---|
| 130 | return rc;
|
|---|
| 131 | }
|
|---|
| 132 | //******************************************************************************
|
|---|
| 133 | //******************************************************************************
|
|---|
| 134 | BOOL Win32ExeBase::isDll()
|
|---|
| 135 | {
|
|---|
| 136 | return FALSE;
|
|---|
| 137 | }
|
|---|
| 138 | //******************************************************************************
|
|---|
| 139 | //******************************************************************************
|
|---|
| 140 | void Win32ExeBase::setCommandLine(char *cline)
|
|---|
| 141 | {
|
|---|
| 142 | ULONG cmdlength = strlen(cline) + 1;
|
|---|
| 143 |
|
|---|
| 144 | cmdLineA = (LPSTR)malloc(cmdlength);
|
|---|
| 145 | strcpy(cmdLineA, cline);
|
|---|
| 146 | cmdLineW = (LPWSTR)malloc(cmdlength*sizeof(WCHAR));
|
|---|
| 147 | AsciiToUnicode(cmdLineA, cmdLineW);
|
|---|
| 148 | }
|
|---|
| 149 | //******************************************************************************
|
|---|
| 150 | //******************************************************************************
|
|---|