| 1 | /* $Id: pe.cpp,v 1.10 1999-10-10 08:58:51 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 <winexepeldr.h>
|
|---|
| 28 | #include <wprocess.h>
|
|---|
| 29 | #include "pe.h"
|
|---|
| 30 |
|
|---|
| 31 | char INFO_BANNER[] = "Usage: PE winexe commandline";
|
|---|
| 32 | char szErrorTitle[] = "Odin";
|
|---|
| 33 | char szLoadErrorMsg[] = "Can't load executable";
|
|---|
| 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 | typedef HAB (* APIENTRY WININITIALIZEPROC)(ULONG flOptions);
|
|---|
| 43 | typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab);
|
|---|
| 44 | typedef HMQ (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg);
|
|---|
| 45 | typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq);
|
|---|
| 46 | typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent,
|
|---|
| 47 | HWND hwndOwner,
|
|---|
| 48 | PCSZ pszText,
|
|---|
| 49 | PCSZ pszCaption,
|
|---|
| 50 | ULONG idWindow,
|
|---|
| 51 | ULONG flStyle);
|
|---|
| 52 | typedef void (* KRNL32EXCEPTPROC) (void *exceptframe);
|
|---|
| 53 |
|
|---|
| 54 | WININITIALIZEPROC MyWinInitialize = 0;
|
|---|
| 55 | WINTERMINATEPROC MyWinTerminate = 0;
|
|---|
| 56 | WINCREATEMSGQUEUEPROC MyWinCreateMsgQueue = 0;
|
|---|
| 57 | WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0;
|
|---|
| 58 | WINMESSAGEBOXPROC MyWinMessageBox = 0;
|
|---|
| 59 | KRNL32EXCEPTPROC Krnl32SetExceptionHandler = 0;
|
|---|
| 60 | KRNL32EXCEPTPROC Krnl32UnsetExceptionHandler = 0;
|
|---|
| 61 |
|
|---|
| 62 | WIN32CTOR CreateWin32Exe = 0;
|
|---|
| 63 |
|
|---|
| 64 | int main(int argc, char *argv[])
|
|---|
| 65 | {
|
|---|
| 66 | HAB hab = 0; /* PM anchor block handle */
|
|---|
| 67 | HMQ hmq = 0; /* Message queue handle */
|
|---|
| 68 | char exeName[CCHMAXPATH];
|
|---|
| 69 | APIRET rc;
|
|---|
| 70 | HMODULE hmodPMWin = 0, hmodKernel32 = 0;
|
|---|
| 71 |
|
|---|
| 72 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
|
|---|
| 73 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
|
|---|
| 74 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
|
|---|
| 75 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
|
|---|
| 76 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
|
|---|
| 77 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
|
|---|
| 78 |
|
|---|
| 79 | rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32);
|
|---|
| 80 | rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@8", (PFN *)&CreateWin32Exe);
|
|---|
| 81 |
|
|---|
| 82 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
|
|---|
| 83 | goto fail;
|
|---|
| 84 |
|
|---|
| 85 | hmq = MyWinCreateMsgQueue(hab, 0);
|
|---|
| 86 |
|
|---|
| 87 | if(argc < 2) {
|
|---|
| 88 | MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 89 | goto fail;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | strcpy(exeName, argv[1]);
|
|---|
| 93 | strupr(exeName);
|
|---|
| 94 | if(strstr(exeName, ".EXE") == NULL) {
|
|---|
| 95 | strcat(exeName, ".EXE");
|
|---|
| 96 | }
|
|---|
| 97 | if(CreateWin32Exe(exeName, ReserveMem()) == FALSE) {
|
|---|
| 98 | MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szLoadErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 99 | goto fail;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
|---|
| 103 | MyWinTerminate( hab ); /* Terminate the application */
|
|---|
| 104 |
|
|---|
| 105 | DosFreeModule(hmodPMWin);
|
|---|
| 106 | DosFreeModule(hmodKernel32);
|
|---|
| 107 | return 0;
|
|---|
| 108 |
|
|---|
| 109 | fail:
|
|---|
| 110 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
|---|
| 111 | if(hab) MyWinTerminate( hab ); /* Terminate the application */
|
|---|
| 112 |
|
|---|
| 113 | if(hmodPMWin) DosFreeModule(hmodPMWin);
|
|---|
| 114 | if(hmodKernel32) DosFreeModule(hmodKernel32);
|
|---|
| 115 | return(1);
|
|---|
| 116 | }
|
|---|
| 117 | //******************************************************************************
|
|---|
| 118 | //******************************************************************************
|
|---|