| 1 | /* $Id: pe.cpp,v 1.8 1999-08-28 07:38:15 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 | #include "exceptions.h" | 
|---|
| 32 | #include "..\kernel32\exceptutil.h" | 
|---|
| 33 |  | 
|---|
| 34 | char INFO_BANNER[]      = "Usage: PE winexe commandline"; | 
|---|
| 35 | char szErrorTitle[]     = "Odin"; | 
|---|
| 36 | char szMemErrorMsg[]    = "Memory allocation failure"; | 
|---|
| 37 | char szFileErrorMsg[]   = "File IO error"; | 
|---|
| 38 | char szPEErrorMsg[]     = "Not a valid win32 exe. (perhaps 16 bits windows)"; | 
|---|
| 39 | char szCPUErrorMsg[]    = "Executable doesn't run on x86 machines"; | 
|---|
| 40 | char szExeErrorMsg[]    = "File isn't an executable"; | 
|---|
| 41 | char szInteralErrorMsg[]= "Internal Error"; | 
|---|
| 42 |  | 
|---|
| 43 | char fullpath[CCHMAXPATH]; | 
|---|
| 44 |  | 
|---|
| 45 | typedef HAB  (* APIENTRY WININITIALIZEPROC)(ULONG flOptions); | 
|---|
| 46 | typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab); | 
|---|
| 47 | typedef HMQ  (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg); | 
|---|
| 48 | typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq); | 
|---|
| 49 | typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent, | 
|---|
| 50 | HWND hwndOwner, | 
|---|
| 51 | PCSZ  pszText, | 
|---|
| 52 | PCSZ  pszCaption, | 
|---|
| 53 | ULONG idWindow, | 
|---|
| 54 | ULONG flStyle); | 
|---|
| 55 | typedef void (* KRNL32EXCEPTPROC) (void *exceptframe); | 
|---|
| 56 |  | 
|---|
| 57 | WININITIALIZEPROC      MyWinInitialize      = 0; | 
|---|
| 58 | WINTERMINATEPROC       MyWinTerminate       = 0; | 
|---|
| 59 | WINCREATEMSGQUEUEPROC  MyWinCreateMsgQueue  = 0; | 
|---|
| 60 | WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0; | 
|---|
| 61 | WINMESSAGEBOXPROC      MyWinMessageBox      = 0; | 
|---|
| 62 | KRNL32EXCEPTPROC       Krnl32SetExceptionHandler = 0; | 
|---|
| 63 | KRNL32EXCEPTPROC       Krnl32UnsetExceptionHandler = 0; | 
|---|
| 64 |  | 
|---|
| 65 | WIN32CTOR              CreateWin32Exe       = 0; | 
|---|
| 66 |  | 
|---|
| 67 | int main(int argc, char *argv[]) | 
|---|
| 68 | { | 
|---|
| 69 | HAB    hab = 0;                             /* PM anchor block handle       */ | 
|---|
| 70 | HMQ    hmq = 0;                             /* Message queue handle         */ | 
|---|
| 71 | char  *szCmdLine; | 
|---|
| 72 | char   exeName[CCHMAXPATH]; | 
|---|
| 73 | PPIB   ppib; | 
|---|
| 74 | PTIB   ptib; | 
|---|
| 75 | Win32Exe *WinExe; | 
|---|
| 76 | APIRET  rc; | 
|---|
| 77 | HMODULE hmodPMWin = 0, hmodKernel32 = 0; | 
|---|
| 78 | WINEXCEPTION_FRAME exceptFrame; | 
|---|
| 79 |  | 
|---|
| 80 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin); | 
|---|
| 81 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize); | 
|---|
| 82 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate); | 
|---|
| 83 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue); | 
|---|
| 84 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue); | 
|---|
| 85 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox); | 
|---|
| 86 |  | 
|---|
| 87 | rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32); | 
|---|
| 88 | rc = DosQueryProcAddr(hmodKernel32, 0, "CreateWin32Exe", (PFN *)&CreateWin32Exe); | 
|---|
| 89 | rc = DosQueryProcAddr(hmodKernel32, 0, "OS2SetExceptionHandler", (PFN *)&Krnl32SetExceptionHandler); | 
|---|
| 90 | rc = DosQueryProcAddr(hmodKernel32, 0, "OS2UnsetExceptionHandler", (PFN *)&Krnl32UnsetExceptionHandler); | 
|---|
| 91 |  | 
|---|
| 92 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */ | 
|---|
| 93 | goto fail; | 
|---|
| 94 |  | 
|---|
| 95 | hmq = MyWinCreateMsgQueue(hab, 0); | 
|---|
| 96 |  | 
|---|
| 97 | if(argc < 2) { | 
|---|
| 98 | MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 99 | goto fail; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | strcpy(exeName, argv[1]); | 
|---|
| 103 | strupr(exeName); | 
|---|
| 104 | if(strstr(exeName, ".EXE") == NULL) { | 
|---|
| 105 | strcat(exeName, ".EXE"); | 
|---|
| 106 | } | 
|---|
| 107 | WinExe = CreateWin32Exe(exeName); | 
|---|
| 108 | if(WinExe == NULL) { | 
|---|
| 109 | MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 110 | goto fail; | 
|---|
| 111 | } | 
|---|
| 112 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 113 | if(rc) { | 
|---|
| 114 | MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 115 | delete WinExe; | 
|---|
| 116 | goto fail; | 
|---|
| 117 | } | 
|---|
| 118 | szCmdLine = ppib->pib_pchcmd; | 
|---|
| 119 | while(*szCmdLine == ' ' && *szCmdLine )       //skip leading spaces | 
|---|
| 120 | szCmdLine++; | 
|---|
| 121 | while(*szCmdLine != ' ' && *szCmdLine )       //skip pe (.exe) | 
|---|
| 122 | szCmdLine++; | 
|---|
| 123 | while(*szCmdLine == ' ' && *szCmdLine )       //skip spaces | 
|---|
| 124 | szCmdLine++; | 
|---|
| 125 |  | 
|---|
| 126 | WinExe->setCommandLine(szCmdLine); | 
|---|
| 127 |  | 
|---|
| 128 | Krnl32SetExceptionHandler(&exceptFrame); | 
|---|
| 129 | if(WinExe->init(ReserveMem()) == FALSE) { | 
|---|
| 130 | delete WinExe; | 
|---|
| 131 | goto fail; | 
|---|
| 132 | } | 
|---|
| 133 | Krnl32UnsetExceptionHandler(&exceptFrame); | 
|---|
| 134 | WinExe->start(); | 
|---|
| 135 |  | 
|---|
| 136 | delete WinExe; | 
|---|
| 137 |  | 
|---|
| 138 | if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */ | 
|---|
| 139 | MyWinTerminate( hab );                   /* Terminate the application    */ | 
|---|
| 140 |  | 
|---|
| 141 | DosFreeModule(hmodPMWin); | 
|---|
| 142 | DosFreeModule(hmodKernel32); | 
|---|
| 143 | return 0; | 
|---|
| 144 |  | 
|---|
| 145 | fail: | 
|---|
| 146 | if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */ | 
|---|
| 147 | if(hab) MyWinTerminate( hab );                   /* Terminate the application    */ | 
|---|
| 148 |  | 
|---|
| 149 | if(hmodPMWin)         DosFreeModule(hmodPMWin); | 
|---|
| 150 | if(hmodKernel32)      DosFreeModule(hmodKernel32); | 
|---|
| 151 | return(1); | 
|---|
| 152 | } | 
|---|
| 153 | //****************************************************************************** | 
|---|
| 154 | //****************************************************************************** | 
|---|