| 1 | /* $Id: pe.cpp,v 1.12 1999-11-24 19:33:04 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 | char szNoKernel32Msg[] = "Can't load/find kernel32.dll";
|
|---|
| 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 | typedef void (* KRNL32EXCEPTPROC) (void *exceptframe);
|
|---|
| 54 |
|
|---|
| 55 | WININITIALIZEPROC MyWinInitialize = 0;
|
|---|
| 56 | WINTERMINATEPROC MyWinTerminate = 0;
|
|---|
| 57 | WINCREATEMSGQUEUEPROC MyWinCreateMsgQueue = 0;
|
|---|
| 58 | WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0;
|
|---|
| 59 | WINMESSAGEBOXPROC MyWinMessageBox = 0;
|
|---|
| 60 | KRNL32EXCEPTPROC Krnl32SetExceptionHandler = 0;
|
|---|
| 61 | KRNL32EXCEPTPROC Krnl32UnsetExceptionHandler = 0;
|
|---|
| 62 |
|
|---|
| 63 | WIN32CTOR CreateWin32Exe = 0;
|
|---|
| 64 |
|
|---|
| 65 | int main(int argc, char *argv[])
|
|---|
| 66 | {
|
|---|
| 67 | HAB hab = 0; /* PM anchor block handle */
|
|---|
| 68 | HMQ hmq = 0; /* Message queue handle */
|
|---|
| 69 | char exeName[CCHMAXPATH];
|
|---|
| 70 | APIRET rc;
|
|---|
| 71 | HMODULE hmodPMWin = 0, hmodKernel32 = 0;
|
|---|
| 72 |
|
|---|
| 73 | rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
|
|---|
| 74 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
|
|---|
| 75 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
|
|---|
| 76 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
|
|---|
| 77 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
|
|---|
| 78 | rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
|
|---|
| 79 |
|
|---|
| 80 | if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM */
|
|---|
| 81 | goto fail;
|
|---|
| 82 |
|
|---|
| 83 | hmq = MyWinCreateMsgQueue(hab, 0);
|
|---|
| 84 |
|
|---|
| 85 | if(argc < 2) {
|
|---|
| 86 | MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 87 | goto fail;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32);
|
|---|
| 91 | if(rc) {
|
|---|
| 92 | MyWinMessageBox(HWND_DESKTOP, NULL, szNoKernel32Msg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
|
|---|
| 93 | goto fail;
|
|---|
| 94 | }
|
|---|
| 95 | rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@8", (PFN *)&CreateWin32Exe);
|
|---|
| 96 |
|
|---|
| 97 | strcpy(exeName, argv[1]);
|
|---|
| 98 | strupr(exeName);
|
|---|
| 99 | if(CreateWin32Exe(exeName, ReserveMem()) == FALSE) {
|
|---|
| 100 | goto fail;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
|---|
| 104 | MyWinTerminate( hab ); /* Terminate the application */
|
|---|
| 105 |
|
|---|
| 106 | DosFreeModule(hmodPMWin);
|
|---|
| 107 | DosFreeModule(hmodKernel32);
|
|---|
| 108 | return 0;
|
|---|
| 109 |
|
|---|
| 110 | fail:
|
|---|
| 111 | if(hmq) MyWinDestroyMsgQueue( hmq ); /* Tidy up... */
|
|---|
| 112 | if(hab) MyWinTerminate( hab ); /* Terminate the application */
|
|---|
| 113 |
|
|---|
| 114 | if(hmodPMWin) DosFreeModule(hmodPMWin);
|
|---|
| 115 | if(hmodKernel32) DosFreeModule(hmodKernel32);
|
|---|
| 116 | return(1);
|
|---|
| 117 | }
|
|---|
| 118 | //******************************************************************************
|
|---|
| 119 | //******************************************************************************
|
|---|