| 1 | /* $Id: winexe.cpp,v 1.1 1999-09-15 23:33:02 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 exe class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998 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 <nameid.h>
|
|---|
| 25 | #include <win32type.h>
|
|---|
| 26 | #include <winexe.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 | Win32Exe *WinExe = NULL;
|
|---|
| 37 |
|
|---|
| 38 | //******************************************************************************
|
|---|
| 39 | //Called by ring 3 pe loader to create win32 executable
|
|---|
| 40 | //******************************************************************************
|
|---|
| 41 | Win32Exe* WIN32API CreateWin32Exe(char *szFileName)
|
|---|
| 42 | {
|
|---|
| 43 | return new Win32Exe(szFileName);
|
|---|
| 44 | }
|
|---|
| 45 | //******************************************************************************
|
|---|
| 46 | //******************************************************************************
|
|---|
| 47 | Win32Exe::Win32Exe(char *szFileName) : Win32Image(szFileName), fConsoleApp(FALSE),
|
|---|
| 48 | cmdline(NULL)
|
|---|
| 49 | {
|
|---|
| 50 | fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
|
|---|
| 51 | WinExe = this;
|
|---|
| 52 |
|
|---|
| 53 | dprintf(("Win32Exe ctor: %s", szFileName));
|
|---|
| 54 |
|
|---|
| 55 | if(fConsoleApp) {
|
|---|
| 56 | dprintf(("Console application!\n"));
|
|---|
| 57 |
|
|---|
| 58 | APIRET rc = iConsoleInit(); /* initialize console subsystem */
|
|---|
| 59 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 60 | dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | //******************************************************************************
|
|---|
| 64 | //******************************************************************************
|
|---|
| 65 | Win32Exe::Win32Exe(HINSTANCE hinstance, int NameTableId, int Win32TableId) :
|
|---|
| 66 | Win32Image(hinstance, NameTableId, Win32TableId),
|
|---|
| 67 | fConsoleApp(FALSE), cmdline(NULL)
|
|---|
| 68 | {
|
|---|
| 69 | if(GET_CONSOLE(Win32TableId) == 1) {//console app
|
|---|
| 70 | dprintf(("Console application!\n"));
|
|---|
| 71 |
|
|---|
| 72 | fConsoleApp = TRUE;
|
|---|
| 73 | APIRET rc = iConsoleInit(); /* initialize console subsystem */
|
|---|
| 74 | if (rc != NO_ERROR) /* check for errors */
|
|---|
| 75 | dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
|
|---|
| 76 | }
|
|---|
| 77 | WinExe = this;
|
|---|
| 78 | }
|
|---|
| 79 | //******************************************************************************
|
|---|
| 80 | //******************************************************************************
|
|---|
| 81 | Win32Exe::~Win32Exe()
|
|---|
| 82 | {
|
|---|
| 83 | Win32Dll::deleteAll();
|
|---|
| 84 | WinExe = NULL;
|
|---|
| 85 | }
|
|---|
| 86 | //******************************************************************************
|
|---|
| 87 | //******************************************************************************
|
|---|
| 88 | ULONG Win32Exe::start()
|
|---|
| 89 | {
|
|---|
| 90 | WINEXCEPTION_FRAME exceptFrame;
|
|---|
| 91 | ULONG rc;
|
|---|
| 92 |
|
|---|
| 93 | if(getenv("WIN32_IOPL2")) {
|
|---|
| 94 | io_init1();
|
|---|
| 95 | }
|
|---|
| 96 | dprintf(("Start executable %X\n", WinExe));
|
|---|
| 97 |
|
|---|
| 98 | fExeStarted = TRUE;
|
|---|
| 99 |
|
|---|
| 100 | //Allocate TLS index for this module
|
|---|
| 101 | tlsAlloc();
|
|---|
| 102 | tlsAttachThread(); //setup TLS (main thread)
|
|---|
| 103 |
|
|---|
| 104 | //Note: The Win32 exception structure references by FS:[0] is the same
|
|---|
| 105 | // in OS/2
|
|---|
| 106 | OS2SetExceptionHandler((void *)&exceptFrame);
|
|---|
| 107 |
|
|---|
| 108 | SetWin32TIB();
|
|---|
| 109 | rc = ((WIN32EXEENTRY)entryPoint)();
|
|---|
| 110 | RestoreOS2TIB();
|
|---|
| 111 |
|
|---|
| 112 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
|---|
| 113 |
|
|---|
| 114 | return rc;
|
|---|
| 115 | }
|
|---|
| 116 | //******************************************************************************
|
|---|
| 117 | //******************************************************************************
|
|---|
| 118 | BOOL Win32Exe::isDll()
|
|---|
| 119 | {
|
|---|
| 120 | return FALSE;
|
|---|
| 121 | }
|
|---|
| 122 | //******************************************************************************
|
|---|
| 123 | //******************************************************************************
|
|---|