1 | /* $Id: winexepeldr.cpp,v 1.10 2000-09-12 04:29:59 bird 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 | BOOL fPeLoader = FALSE;
|
---|
45 |
|
---|
46 | //******************************************************************************
|
---|
47 | //Called by ring 3 pe loader to create win32 executable
|
---|
48 | //******************************************************************************
|
---|
49 | BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine, ULONG reservedMem)
|
---|
50 | {
|
---|
51 | APIRET rc;
|
---|
52 | PPIB ppib;
|
---|
53 | PTIB ptib;
|
---|
54 | WINEXCEPTION_FRAME exceptFrame;
|
---|
55 | Win32PeLdrExe *WinExe;
|
---|
56 | char *szFullCmdLine;
|
---|
57 |
|
---|
58 | fPeLoader = TRUE;
|
---|
59 |
|
---|
60 | WinExe = new Win32PeLdrExe(szFileName);
|
---|
61 |
|
---|
62 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
63 | if(rc) {
|
---|
64 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
65 | delete WinExe;
|
---|
66 | return FALSE;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* Initiate the commandline.
|
---|
70 | * (Since PE.EXE takes no options we'll currently just skip the
|
---|
71 | * first parameter and used the passed in executable name.)
|
---|
72 | */
|
---|
73 | szCmdLine = szCmdLine; /* ignored */
|
---|
74 | InitCommandLine(szFileName);
|
---|
75 |
|
---|
76 | if(getenv("WIN32_IOPL2")) {
|
---|
77 | io_init1();
|
---|
78 | }
|
---|
79 |
|
---|
80 | OS2SetExceptionHandler(&exceptFrame);
|
---|
81 | if(WinExe->init(reservedMem) == FALSE) {
|
---|
82 | if(szErrorModule[0] != 0) {
|
---|
83 | char szErrorMsg[128];
|
---|
84 |
|
---|
85 | sprintf(szErrorMsg, "Can't execute %s due to bad or missing %s", OSLibStripPath(szFileName), szErrorModule);
|
---|
86 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
---|
87 | }
|
---|
88 | delete WinExe;
|
---|
89 | return FALSE;
|
---|
90 | }
|
---|
91 |
|
---|
92 | OS2UnsetExceptionHandler(&exceptFrame);
|
---|
93 | if(WinExe->isConsoleApp()) {
|
---|
94 | dprintf(("Console application!\n"));
|
---|
95 |
|
---|
96 | APIRET rc = iConsoleInit(); /* initialize console subsystem */
|
---|
97 | if (rc != NO_ERROR) /* check for errors */
|
---|
98 | dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
|
---|
99 | }
|
---|
100 |
|
---|
101 | WinExe->start();
|
---|
102 |
|
---|
103 | delete WinExe;
|
---|
104 |
|
---|
105 | return TRUE;
|
---|
106 | }
|
---|
107 | //******************************************************************************
|
---|
108 | //******************************************************************************
|
---|
109 | Win32PeLdrExe::Win32PeLdrExe(char *szFileName) :
|
---|
110 | Win32ImageBase(-1),
|
---|
111 | Win32ExeBase(-1),
|
---|
112 | Win32PeLdrImage(szFileName, TRUE)
|
---|
113 | {
|
---|
114 | dprintf(("Win32PeLdrExe ctor: %s", szFileName));
|
---|
115 | }
|
---|
116 | //******************************************************************************
|
---|
117 | //******************************************************************************
|
---|
118 | Win32PeLdrExe::~Win32PeLdrExe()
|
---|
119 | {
|
---|
120 | }
|
---|
121 | //******************************************************************************
|
---|
122 | //******************************************************************************
|
---|
123 | BOOL Win32PeLdrExe::init(ULONG reservedMem)
|
---|
124 | {
|
---|
125 | BOOL rc;
|
---|
126 |
|
---|
127 | rc = Win32PeLdrImage::init(reservedMem);
|
---|
128 | fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
|
---|
129 | return rc;
|
---|
130 | }
|
---|
131 | //******************************************************************************
|
---|
132 | //******************************************************************************
|
---|