source: trunk/src/kernel32/winexepeldr.cpp@ 7029

Last change on this file since 7029 was 7025, checked in by phaller, 24 years ago

improvement of profiler

File size: 5.6 KB
Line 
1/* $Id: winexepeldr.cpp,v 1.16 2001-10-12 00:49:23 phaller Exp $ */
2
3/*
4 * Win32 PE loader Exe class
5 *
6 * Copyright 1998-2000 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 <win32api.h>
26#include <winexepeldr.h>
27#include <wprocess.h>
28#include <pefile.h>
29
30#include "conwin.h" // Windows Header for console only
31#include "console.h"
32
33#include "exceptions.h"
34#include "exceptutil.h"
35
36#include "cio.h"
37#include "oslibmisc.h"
38
39#define DBG_LOCALLOG DBG_winexepeldr
40#include "dbglocal.h"
41
42
43#ifdef PROFILE
44#include <perfview.h>
45#endif /* PROFILE */
46
47
48extern char szErrorTitle[];
49extern char szErrorModule[];
50
51BOOL fPeLoader = FALSE;
52
53//******************************************************************************
54//Called by ring 3 pe loader to create win32 executable
55//PE.EXE command line options:
56// /OPT:[x1=y,x2=z,..]
57// x = CURDIR -> set current directory to y
58// (not other options available at this time)
59//******************************************************************************
60BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine,
61 char *peoptions,
62 ULONG reservedMem, BOOL fConsoleApp,
63 BOOL fVioConsole)
64{
65 APIRET rc;
66 PPIB ppib;
67 PTIB ptib;
68 WINEXCEPTION_FRAME exceptFrame;
69 Win32PeLdrExe *WinExe;
70 char *szFullCmdLine;
71
72 fPeLoader = TRUE;
73
74 WinExe = new Win32PeLdrExe(szFileName, fConsoleApp);
75
76 rc = DosGetInfoBlocks(&ptib, &ppib);
77 if(rc) {
78 WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
79 delete WinExe;
80 return FALSE;
81 }
82 //Handle special pe cmd line options here (/OPT:[x1=y,x2=z,..])
83 if(peoptions) {
84 char *option;
85
86 option = strchr(peoptions, '[');
87 if(option) {
88 option++;
89 option = strstr(option, "CURDIR=");
90 if(option) {
91 char *curdir, *tmp;
92 int curdirlength;
93
94 option += 7;
95 tmp = option;
96 while(*tmp != ']' && *tmp != ',' && *tmp != 0) {
97 tmp++;
98 }
99 curdirlength = (int)(tmp-option);
100 curdir = (char *)malloc(curdirlength+1);
101 memcpy(curdir, option, curdirlength);
102 curdir[curdirlength] = 0;
103 SetCurrentDirectoryA((LPCSTR)curdir);
104 free(curdir);
105 }
106 }
107 }
108 //exe length + space + (possibly) 2x'"' + cmd line length + 0 terminator
109 szFullCmdLine = (char *)malloc(strlen(szFileName) + 3 + strlen(szCmdLine) + 1);
110 //Enclose executable name in quotes if it (or it's directory) contains spaces
111 if(strchr(szFileName, ' ') != NULL) {
112 sprintf(szFullCmdLine, "\"%s\"", szFileName);
113 }
114 else strcpy(szFullCmdLine, szFileName);
115 strcat(szFullCmdLine, " ");
116 strcat(szFullCmdLine, szCmdLine);
117 InitCommandLine(szFullCmdLine);
118 dprintf(("Cmd line: %s", szFullCmdLine));
119 free(szFullCmdLine);
120
121 if(getenv("WIN32_IOPL2")) {
122 io_init1();
123 }
124 //Init console before loading executable as dlls might want to print
125 //something on the console while being loaded
126 if(WinExe->isConsoleApp())
127 {
128 dprintf(("Console application!\n"));
129
130 APIRET rc = iConsoleInit(fVioConsole); /* initialize console subsystem */
131 if (rc != NO_ERROR) /* check for errors */
132 dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
133 }
134
135 OS2SetExceptionHandler(&exceptFrame);
136 if(WinExe->init(reservedMem) == FALSE)
137 {
138 if(szErrorModule[0] != 0) {
139 char szErrorMsg[128];
140
141 sprintf(szErrorMsg, "Can't execute %s due to bad or missing %s", OSLibStripPath(szFileName), szErrorModule);
142 WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
143 }
144 delete WinExe;
145 OS2UnsetExceptionHandler(&exceptFrame);
146 return FALSE;
147 }
148 OS2UnsetExceptionHandler(&exceptFrame);
149
150#ifdef PROFILE
151 // Note: after this point, we might start collecting performance
152 // information about the called functions.
153 PerfView_Initialize();
154#endif /* PROFILE */
155
156
157 WinExe->start();
158
159 delete WinExe;
160
161 return TRUE;
162}
163//******************************************************************************
164//******************************************************************************
165Win32PeLdrExe::Win32PeLdrExe(char *szFileName, BOOL fConsoleApp) :
166 Win32ImageBase(-1),
167 Win32ExeBase(-1),
168 Win32PeLdrImage(szFileName, TRUE)
169{
170 dprintf(("Win32PeLdrExe ctor: %s", szFileName));
171 this->fConsoleApp = fConsoleApp;
172
173 //SvL: set temporary full path here as console init needs it
174 setFullPath(szFileName);
175}
176//******************************************************************************
177//******************************************************************************
178Win32PeLdrExe::~Win32PeLdrExe()
179{
180 fExitProcess = TRUE;
181}
182//******************************************************************************
183//******************************************************************************
184BOOL Win32PeLdrExe::init(ULONG reservedMem)
185{
186 BOOL rc;
187
188 rc = Win32PeLdrImage::init(reservedMem);
189 return rc;
190}
191//******************************************************************************
192//******************************************************************************
Note: See TracBrowser for help on using the repository browser.