source: trunk/src/peldr/pe.cpp@ 506

Last change on this file since 506 was 506, checked in by sandervl, 26 years ago

* empty log message *

File size: 5.0 KB
Line 
1/* $Id: pe.cpp,v 1.5 1999-08-16 13:54:07 sandervl Exp $ */
2
3/*
4 * PELDR main exe loader code
5 *
6 * Copyright 1998 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_DOSMODULEMGR
17#define INCL_WIN
18#include <os2.h>
19#include <bseord.h>
20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25#include <win32type.h>
26#include <misc.h>
27#include <winexe.h>
28#include <windll.h>
29#include <wprocess.h>
30#include "pe.h"
31
32char INFO_BANNER[] = "Usage: PE winexe commandline";
33char szErrorTitle[] = "Odin";
34char szMemErrorMsg[] = "Memory allocation failure";
35char szFileErrorMsg[] = "File IO error";
36char szPEErrorMsg[] = "Not a valid win32 exe. (perhaps 16 bits windows)";
37char szCPUErrorMsg[] = "Executable doesn't run on x86 machines";
38char szExeErrorMsg[] = "File isn't an executable";
39char szInteralErrorMsg[]= "Internal Error";
40
41char fullpath[CCHMAXPATH];
42
43typedef HAB (* APIENTRY WININITIALIZEPROC)(ULONG flOptions);
44typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab);
45typedef HMQ (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg);
46typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq);
47typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent,
48 HWND hwndOwner,
49 PCSZ pszText,
50 PCSZ pszCaption,
51 ULONG idWindow,
52 ULONG flStyle);
53
54WININITIALIZEPROC MyWinInitialize = 0;
55WINTERMINATEPROC MyWinTerminate = 0;
56WINCREATEMSGQUEUEPROC MyWinCreateMsgQueue = 0;
57WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0;
58WINMESSAGEBOXPROC MyWinMessageBox = 0;
59
60WIN32CTOR CreateWin32Exe = 0;
61
62int main(int argc, char *argv[])
63{
64 HAB hab; /* PM anchor block handle */
65 HMQ hmq; /* Message queue handle */
66 char *szCmdLine;
67 char exeName[CCHMAXPATH];
68 PPIB ppib;
69 PTIB ptib;
70 Win32Exe *WinExe;
71 APIRET rc;
72 HMODULE hmodPMWin, hmodKernel32;
73
74 printf("memory allocated at %X\n", ReserveMem());
75
76 rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
77 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
78 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
79 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
80 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
81 rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
82
83 rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32);
84 rc = DosQueryProcAddr(hmodKernel32, 0, "CreateWin32Exe", (PFN *)&CreateWin32Exe);
85
86 if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
87 return(1);
88
89 hmq = MyWinCreateMsgQueue(hab, 0);
90
91 if(argc < 2) {
92 MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
93 return(0);
94 }
95
96 strcpy(exeName, argv[1]);
97 strupr(exeName);
98 if(strstr(exeName, ".EXE") == NULL) {
99 strcat(exeName, ".EXE");
100 }
101 WinExe = CreateWin32Exe(exeName);
102 if(WinExe == NULL) {
103 MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
104 return(1);
105 }
106 rc = DosGetInfoBlocks(&ptib, &ppib);
107 if(rc) {
108 MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
109 delete WinExe;
110 return(1);
111 }
112 szCmdLine = ppib->pib_pchcmd;
113 while(*szCmdLine == ' ' && *szCmdLine ) //skip leading spaces
114 szCmdLine++;
115 while(*szCmdLine != ' ' && *szCmdLine ) //skip pe (.exe)
116 szCmdLine++;
117 while(*szCmdLine == ' ' && *szCmdLine ) //skip spaces
118 szCmdLine++;
119
120 ULONG curdisk, curlogdisk, flength = CCHMAXPATH;
121
122 DosQueryCurrentDisk(&curdisk, &curlogdisk);
123 DosQueryCurrentDir(curdisk, &fullpath[3], &flength);
124 fullpath[0] = 'A' + (curdisk-1);
125 fullpath[1] = ':';
126 fullpath[2] = '\\';
127 strcat(fullpath, "\\");
128 strcat(fullpath, exeName);
129 WinExe->setFullPath(fullpath);
130 WinExe->setCommandLine(szCmdLine);
131
132 if(WinExe->init() == FALSE) {
133 delete WinExe;
134 return(1);
135 }
136 WinExe->start();
137
138 delete WinExe;
139
140 if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
141 MyWinTerminate( hab ); /* Terminate the application */
142
143 DosFreeModule(hmodPMWin);
144 DosFreeModule(hmodKernel32);
145 return(0);
146}
147//******************************************************************************
148//******************************************************************************
Note: See TracBrowser for help on using the repository browser.