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

Last change on this file since 4445 was 4445, checked in by sandervl, 25 years ago

changes for setting current dir of new process

File size: 5.3 KB
Line 
1/* $Id: winexepeldr.cpp,v 1.13 2000-10-06 15:16:03 sandervl 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
42extern char szErrorTitle[];
43extern char szErrorModule[];
44
45BOOL fPeLoader = FALSE;
46
47//******************************************************************************
48//Called by ring 3 pe loader to create win32 executable
49//PE.EXE command line options:
50// /OPT:[x1=y,x2=z,..]
51// x = CURDIR -> set current directory to y
52// (not other options available at this time)
53//******************************************************************************
54BOOL WIN32API CreateWin32PeLdrExe(char *szFileName, char *szCmdLine,
55 char *peoptions,
56 ULONG reservedMem, BOOL fConsoleApp)
57{
58 APIRET rc;
59 PPIB ppib;
60 PTIB ptib;
61 WINEXCEPTION_FRAME exceptFrame;
62 Win32PeLdrExe *WinExe;
63 char *szFullCmdLine;
64
65 fPeLoader = TRUE;
66
67 WinExe = new Win32PeLdrExe(szFileName, fConsoleApp);
68
69 rc = DosGetInfoBlocks(&ptib, &ppib);
70 if(rc) {
71 WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
72 delete WinExe;
73 return FALSE;
74 }
75 //Handle special pe cmd line options here (/OPT:[x1=y,x2=z,..])
76 if(peoptions) {
77 char *option;
78
79 option = strchr(peoptions, '[');
80 if(option) {
81 option++;
82 option = strstr(option, "CURDIR=");
83 if(option) {
84 char *curdir, *tmp;
85 int curdirlength;
86
87 option += 7;
88 tmp = option;
89 while(*tmp != ']' && *tmp != ',' && *tmp != 0) {
90 tmp++;
91 }
92 curdirlength = (int)(tmp-option);
93 curdir = (char *)malloc(curdirlength+1);
94 memcpy(curdir, option, curdirlength);
95 curdir[curdirlength] = 0;
96 SetCurrentDirectoryA((LPCSTR)curdir);
97 free(curdir);
98 }
99 }
100 }
101 //exe length + space + (possibly) 2x'"' + cmd line length + 0 terminator
102 szFullCmdLine = (char *)malloc(strlen(szFileName) + 3 + strlen(szCmdLine) + 1);
103 //Enclose executable name in quotes if it (or it's directory) contains spaces
104 if(strchr(szFileName, ' ') != NULL) {
105 sprintf(szFullCmdLine, "\"%s\"", szFileName);
106 }
107 else strcpy(szFullCmdLine, szFileName);
108 strcat(szFullCmdLine, " ");
109 strcat(szFullCmdLine, szCmdLine);
110 InitCommandLine(szFullCmdLine);
111 dprintf(("Cmd line: %s", szFullCmdLine));
112 free(szFullCmdLine);
113
114 if(getenv("WIN32_IOPL2")) {
115 io_init1();
116 }
117 //Init console before loading executable as dlls might want to print
118 //something on the console while being loaded
119 if(WinExe->isConsoleApp())
120 {
121 dprintf(("Console application!\n"));
122
123 APIRET rc = iConsoleInit(); /* initialize console subsystem */
124 if (rc != NO_ERROR) /* check for errors */
125 dprintf(("KERNEL32:Win32Image:Init ConsoleInit failed with %u.\n", rc));
126 }
127
128 OS2SetExceptionHandler(&exceptFrame);
129 if(WinExe->init(reservedMem) == FALSE)
130 {
131 if(szErrorModule[0] != 0) {
132 char szErrorMsg[128];
133
134 sprintf(szErrorMsg, "Can't execute %s due to bad or missing %s", OSLibStripPath(szFileName), szErrorModule);
135 WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
136 }
137 delete WinExe;
138 OS2UnsetExceptionHandler(&exceptFrame);
139 return FALSE;
140 }
141 OS2UnsetExceptionHandler(&exceptFrame);
142
143 WinExe->start();
144
145 delete WinExe;
146
147 return TRUE;
148}
149//******************************************************************************
150//******************************************************************************
151Win32PeLdrExe::Win32PeLdrExe(char *szFileName, BOOL fConsoleApp) :
152 Win32ImageBase(-1),
153 Win32ExeBase(-1),
154 Win32PeLdrImage(szFileName, TRUE)
155{
156 dprintf(("Win32PeLdrExe ctor: %s", szFileName));
157 this->fConsoleApp = fConsoleApp;
158
159 //SvL: set temporary full path here as console init needs it
160 setFullPath(szFileName);
161}
162//******************************************************************************
163//******************************************************************************
164Win32PeLdrExe::~Win32PeLdrExe()
165{
166}
167//******************************************************************************
168//******************************************************************************
169BOOL Win32PeLdrExe::init(ULONG reservedMem)
170{
171 BOOL rc;
172
173 rc = Win32PeLdrImage::init(reservedMem);
174 return rc;
175}
176//******************************************************************************
177//******************************************************************************
Note: See TracBrowser for help on using the repository browser.