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

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

console, pe loader + FormatMessage fixes

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