| 1 | /* $Id: pe.cpp,v 1.4 1999-06-20 14:02:13 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * | 
|---|
| 5 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 6 | * | 
|---|
| 7 | */ | 
|---|
| 8 | /* | 
|---|
| 9 | * PELDR main exe loader code | 
|---|
| 10 | * | 
|---|
| 11 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 12 | * | 
|---|
| 13 | */ | 
|---|
| 14 | #define INCL_DOSFILEMGR          /* File Manager values      */ | 
|---|
| 15 | #define INCL_DOSERRORS           /* DOS Error values         */ | 
|---|
| 16 | #define INCL_DOSPROCESS          /* DOS Process values       */ | 
|---|
| 17 | #define INCL_DOSMISC             /* DOS Miscellanous values  */ | 
|---|
| 18 | #define INCL_WIN | 
|---|
| 19 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 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 |  | 
|---|
| 31 | char INFO_BANNER[]      = "Usage: PE winexe commandline"; | 
|---|
| 32 | char szErrorTitle[]     = "Odin"; | 
|---|
| 33 | char szMemErrorMsg[]    = "Memory allocation failure"; | 
|---|
| 34 | char szFileErrorMsg[]   = "File IO error"; | 
|---|
| 35 | char szPEErrorMsg[]     = "Not a valid win32 exe. (perhaps 16 bits windows)"; | 
|---|
| 36 | char szCPUErrorMsg[]    = "Executable doesn't run on x86 machines"; | 
|---|
| 37 | char szExeErrorMsg[]    = "File isn't an executable"; | 
|---|
| 38 | char szInteralErrorMsg[]= "Internal Error"; | 
|---|
| 39 |  | 
|---|
| 40 | char fullpath[CCHMAXPATH]; | 
|---|
| 41 |  | 
|---|
| 42 | void UpCase(char *mixedcase); | 
|---|
| 43 |  | 
|---|
| 44 | int main(int argc, char *argv[]) | 
|---|
| 45 | { | 
|---|
| 46 | HAB    hab;                             /* PM anchor block handle       */ | 
|---|
| 47 | HMQ    hmq;                             /* Message queue handle         */ | 
|---|
| 48 | char  *szCmdLine; | 
|---|
| 49 | char   exeName[CCHMAXPATH]; | 
|---|
| 50 | PPIB   ppib; | 
|---|
| 51 | PTIB   ptib; | 
|---|
| 52 | Win32Exe *WinExe; | 
|---|
| 53 | APIRET rc; | 
|---|
| 54 |  | 
|---|
| 55 | if ((hab = WinInitialize(0)) == 0L) /* Initialize PM     */ | 
|---|
| 56 | return(1); | 
|---|
| 57 |  | 
|---|
| 58 | hmq = WinCreateMsgQueue(hab, 0); | 
|---|
| 59 |  | 
|---|
| 60 | if(argc < 2) { | 
|---|
| 61 | WinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 62 | return(0); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | strcpy(exeName, argv[1]); | 
|---|
| 66 | UpCase(exeName); | 
|---|
| 67 | if(strstr(exeName, ".EXE") == NULL) { | 
|---|
| 68 | strcat(exeName, ".EXE"); | 
|---|
| 69 | } | 
|---|
| 70 | WinExe = new Win32Exe(exeName); | 
|---|
| 71 | if(WinExe == NULL) { | 
|---|
| 72 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 73 | return(1); | 
|---|
| 74 | } | 
|---|
| 75 | rc = DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 76 | if(rc) { | 
|---|
| 77 | WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE); | 
|---|
| 78 | delete WinExe; | 
|---|
| 79 | return(1); | 
|---|
| 80 | } | 
|---|
| 81 | szCmdLine = ppib->pib_pchcmd; | 
|---|
| 82 | while(*szCmdLine == ' ' && *szCmdLine )       //skip leading spaces | 
|---|
| 83 | szCmdLine++; | 
|---|
| 84 | while(*szCmdLine != ' ' && *szCmdLine )       //skip pe (.exe) | 
|---|
| 85 | szCmdLine++; | 
|---|
| 86 | while(*szCmdLine == ' ' && *szCmdLine )       //skip spaces | 
|---|
| 87 | szCmdLine++; | 
|---|
| 88 |  | 
|---|
| 89 | ULONG curdisk, curlogdisk, flength = CCHMAXPATH; | 
|---|
| 90 |  | 
|---|
| 91 | DosQueryCurrentDisk(&curdisk, &curlogdisk); | 
|---|
| 92 | DosQueryCurrentDir(curdisk, &fullpath[3], &flength); | 
|---|
| 93 | fullpath[0] = 'A' + (curdisk-1); | 
|---|
| 94 | fullpath[1] = ':'; | 
|---|
| 95 | fullpath[2] = '\\'; | 
|---|
| 96 | strcat(fullpath, "\\"); | 
|---|
| 97 | strcat(fullpath, exeName); | 
|---|
| 98 | WinExe->setFullPath(fullpath); | 
|---|
| 99 | WinExe->setCommandLine(szCmdLine); | 
|---|
| 100 |  | 
|---|
| 101 | if(WinExe->init() == FALSE) { | 
|---|
| 102 | delete WinExe; | 
|---|
| 103 | return(1); | 
|---|
| 104 | } | 
|---|
| 105 | WinExe->start(); | 
|---|
| 106 |  | 
|---|
| 107 | delete WinExe; | 
|---|
| 108 |  | 
|---|
| 109 | if(hmq) WinDestroyMsgQueue( hmq );             /* Tidy up...                   */ | 
|---|
| 110 | WinTerminate( hab );                   /* Terminate the application    */ | 
|---|
| 111 | return(0); | 
|---|
| 112 | } | 
|---|
| 113 | //****************************************************************************** | 
|---|
| 114 | //****************************************************************************** | 
|---|
| 115 | void UpCase(char *mixedcase) | 
|---|
| 116 | { | 
|---|
| 117 | int i; | 
|---|
| 118 |  | 
|---|
| 119 | for(i=0;i<strlen(mixedcase);i++) { | 
|---|
| 120 | if(mixedcase[i] >= 'a' && mixedcase[i] <= 'z') { | 
|---|
| 121 | mixedcase[i] += 'A' - 'a'; | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 | } | 
|---|
| 125 | //****************************************************************************** | 
|---|
| 126 | //****************************************************************************** | 
|---|