| 1 | /* $Id: winexepeldr.cpp,v 1.7 2000-02-16 14:22:11 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 PE loader Exe 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 <winexepeldr.h> | 
|---|
| 26 | #include <wprocess.h> | 
|---|
| 27 | #include <pefile.h> | 
|---|
| 28 |  | 
|---|
| 29 | #include "conwin.h"          // Windows Header for console only | 
|---|
| 30 | #include "console.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include "exceptions.h" | 
|---|
| 33 | #include "exceptutil.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include "cio.h" | 
|---|
| 36 | #include "oslibmisc.h" | 
|---|
| 37 |  | 
|---|
| 38 | #define DBG_LOCALLOG    DBG_winexepeldr | 
|---|
| 39 | #include "dbglocal.h" | 
|---|
| 40 |  | 
|---|
| 41 | extern char szErrorTitle[]; | 
|---|
| 42 | extern char szErrorModule[]; | 
|---|
| 43 |  | 
|---|
| 44 | //****************************************************************************** | 
|---|
| 45 | //Called by ring 3 pe loader to create win32 executable | 
|---|
| 46 | //****************************************************************************** | 
|---|
| 47 | BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, ULONG reservedMem) | 
|---|
| 48 | { | 
|---|
| 49 | APIRET  rc; | 
|---|
| 50 | PPIB   ppib; | 
|---|
| 51 | PTIB   ptib; | 
|---|
| 52 | WINEXCEPTION_FRAME exceptFrame; | 
|---|
| 53 | Win32PeLdrExe *WinExe; | 
|---|
| 54 | char  *szCmdLine; | 
|---|
| 55 |  | 
|---|
| 56 | WinExe = new Win32PeLdrExe(szFileName); | 
|---|
| 57 |  | 
|---|
| 58 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 59 | if(rc) { | 
|---|
| 60 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 61 | delete WinExe; | 
|---|
| 62 | return FALSE; | 
|---|
| 63 | } | 
|---|
| 64 | //TODO: Create full path for executable? | 
|---|
| 65 | szCmdLine = ppib->pib_pchcmd + strlen(ppib->pib_pchcmd) + 1; | 
|---|
| 66 | while(*szCmdLine != 0 && *szCmdLine == ' ') | 
|---|
| 67 | szCmdLine++; | 
|---|
| 68 |  | 
|---|
| 69 | WinExe->setCommandLine(szCmdLine); | 
|---|
| 70 | dprintf(("Cmd line: %s", szCmdLine)); | 
|---|
| 71 |  | 
|---|
| 72 | if(getenv("WIN32_IOPL2")) { | 
|---|
| 73 | io_init1(); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | OS2SetExceptionHandler(&exceptFrame); | 
|---|
| 77 | if(WinExe->init(reservedMem) == FALSE) { | 
|---|
| 78 | if(szErrorModule[0] != 0) { | 
|---|
| 79 | char szErrorMsg[128]; | 
|---|
| 80 |  | 
|---|
| 81 | sprintf(szErrorMsg, "Can't execute %s due to bad or missing %s", OSLibStripPath(szFileName), szErrorModule); | 
|---|
| 82 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 83 | } | 
|---|
| 84 | delete WinExe; | 
|---|
| 85 | return FALSE; | 
|---|
| 86 | } | 
|---|
| 87 | OS2UnsetExceptionHandler(&exceptFrame); | 
|---|
| 88 | if(WinExe->isConsoleApp()) { | 
|---|
| 89 | dprintf(("Console application!\n")); | 
|---|
| 90 |  | 
|---|
| 91 | APIRET rc = iConsoleInit();                     /* initialize console subsystem */ | 
|---|
| 92 | if (rc != NO_ERROR)                                  /* check for errors */ | 
|---|
| 93 | dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc)); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | WinExe->start(); | 
|---|
| 97 |  | 
|---|
| 98 | delete WinExe; | 
|---|
| 99 |  | 
|---|
| 100 | return TRUE; | 
|---|
| 101 | } | 
|---|
| 102 | //****************************************************************************** | 
|---|
| 103 | //****************************************************************************** | 
|---|
| 104 | Win32PeLdrExe::Win32PeLdrExe(char *szFileName) : | 
|---|
| 105 | Win32ImageBase(-1), | 
|---|
| 106 | Win32ExeBase(-1), | 
|---|
| 107 | Win32PeLdrImage(szFileName, TRUE) | 
|---|
| 108 | { | 
|---|
| 109 | dprintf(("Win32PeLdrExe ctor: %s", szFileName)); | 
|---|
| 110 | } | 
|---|
| 111 | //****************************************************************************** | 
|---|
| 112 | //****************************************************************************** | 
|---|
| 113 | Win32PeLdrExe::~Win32PeLdrExe() | 
|---|
| 114 | { | 
|---|
| 115 | } | 
|---|
| 116 | //****************************************************************************** | 
|---|
| 117 | //****************************************************************************** | 
|---|
| 118 | BOOL Win32PeLdrExe::init(ULONG reservedMem) | 
|---|
| 119 | { | 
|---|
| 120 | BOOL rc; | 
|---|
| 121 |  | 
|---|
| 122 | rc = Win32PeLdrImage::init(reservedMem); | 
|---|
| 123 | fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI); | 
|---|
| 124 | return rc; | 
|---|
| 125 | } | 
|---|
| 126 | //****************************************************************************** | 
|---|
| 127 | //****************************************************************************** | 
|---|