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