| 1 | /* $Id: pe.cpp,v 1.6 1999-08-18 12:24:32 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 |  | 
|---|
| 32 | char INFO_BANNER[]      = "Usage: PE winexe commandline"; | 
|---|
| 33 | char szErrorTitle[]     = "Odin"; | 
|---|
| 34 | char szMemErrorMsg[]    = "Memory allocation failure"; | 
|---|
| 35 | char szFileErrorMsg[]   = "File IO error"; | 
|---|
| 36 | char szPEErrorMsg[]     = "Not a valid win32 exe. (perhaps 16 bits windows)"; | 
|---|
| 37 | char szCPUErrorMsg[]    = "Executable doesn't run on x86 machines"; | 
|---|
| 38 | char szExeErrorMsg[]    = "File isn't an executable"; | 
|---|
| 39 | char szInteralErrorMsg[]= "Internal Error"; | 
|---|
| 40 |  | 
|---|
| 41 | char fullpath[CCHMAXPATH]; | 
|---|
| 42 |  | 
|---|
| 43 | typedef HAB  (* APIENTRY WININITIALIZEPROC)(ULONG flOptions); | 
|---|
| 44 | typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab); | 
|---|
| 45 | typedef HMQ  (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg); | 
|---|
| 46 | typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq); | 
|---|
| 47 | typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent, | 
|---|
| 48 | HWND hwndOwner, | 
|---|
| 49 | PCSZ  pszText, | 
|---|
| 50 | PCSZ  pszCaption, | 
|---|
| 51 | ULONG idWindow, | 
|---|
| 52 | ULONG flStyle); | 
|---|
| 53 |  | 
|---|
| 54 | WININITIALIZEPROC      MyWinInitialize      = 0; | 
|---|
| 55 | WINTERMINATEPROC       MyWinTerminate       = 0; | 
|---|
| 56 | WINCREATEMSGQUEUEPROC  MyWinCreateMsgQueue  = 0; | 
|---|
| 57 | WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0; | 
|---|
| 58 | WINMESSAGEBOXPROC      MyWinMessageBox      = 0; | 
|---|
| 59 |  | 
|---|
| 60 | WIN32CTOR              CreateWin32Exe       = 0; | 
|---|
| 61 |  | 
|---|
| 62 | int 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 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin); | 
|---|
| 75 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize); | 
|---|
| 76 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate); | 
|---|
| 77 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue); | 
|---|
| 78 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue); | 
|---|
| 79 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox); | 
|---|
| 80 |  | 
|---|
| 81 | rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32); | 
|---|
| 82 | rc = DosQueryProcAddr(hmodKernel32, 0, "CreateWin32Exe", (PFN *)&CreateWin32Exe); | 
|---|
| 83 |  | 
|---|
| 84 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */ | 
|---|
| 85 | return(1); | 
|---|
| 86 |  | 
|---|
| 87 | hmq = MyWinCreateMsgQueue(hab, 0); | 
|---|
| 88 |  | 
|---|
| 89 | if(argc < 2) { | 
|---|
| 90 | MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 91 | return(0); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | strcpy(exeName, argv[1]); | 
|---|
| 95 | strupr(exeName); | 
|---|
| 96 | if(strstr(exeName, ".EXE") == NULL) { | 
|---|
| 97 | strcat(exeName, ".EXE"); | 
|---|
| 98 | } | 
|---|
| 99 | WinExe = CreateWin32Exe(exeName); | 
|---|
| 100 | if(WinExe == NULL) { | 
|---|
| 101 | MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 102 | return(1); | 
|---|
| 103 | } | 
|---|
| 104 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 105 | if(rc) { | 
|---|
| 106 | MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 107 | delete WinExe; | 
|---|
| 108 | return(1); | 
|---|
| 109 | } | 
|---|
| 110 | szCmdLine = ppib->pib_pchcmd; | 
|---|
| 111 | while(*szCmdLine == ' ' && *szCmdLine )       //skip leading spaces | 
|---|
| 112 | szCmdLine++; | 
|---|
| 113 | while(*szCmdLine != ' ' && *szCmdLine )       //skip pe (.exe) | 
|---|
| 114 | szCmdLine++; | 
|---|
| 115 | while(*szCmdLine == ' ' && *szCmdLine )       //skip spaces | 
|---|
| 116 | szCmdLine++; | 
|---|
| 117 |  | 
|---|
| 118 | ULONG curdisk, curlogdisk, flength = CCHMAXPATH; | 
|---|
| 119 |  | 
|---|
| 120 | DosQueryCurrentDisk(&curdisk, &curlogdisk); | 
|---|
| 121 | DosQueryCurrentDir(curdisk, &fullpath[3], &flength); | 
|---|
| 122 | fullpath[0] = 'A' + (curdisk-1); | 
|---|
| 123 | fullpath[1] = ':'; | 
|---|
| 124 | fullpath[2] = '\\'; | 
|---|
| 125 | strcat(fullpath, "\\"); | 
|---|
| 126 | strcat(fullpath, exeName); | 
|---|
| 127 | WinExe->setFullPath(fullpath); | 
|---|
| 128 | WinExe->setCommandLine(szCmdLine); | 
|---|
| 129 |  | 
|---|
| 130 | if(WinExe->init(ReserveMem()) == FALSE) { | 
|---|
| 131 | delete WinExe; | 
|---|
| 132 | return(1); | 
|---|
| 133 | } | 
|---|
| 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 | //****************************************************************************** | 
|---|
| 146 | //****************************************************************************** | 
|---|