Index: trunk/src/kernel32/KERNEL32.DEF
===================================================================
--- trunk/src/kernel32/KERNEL32.DEF	(revision 505)
+++ trunk/src/kernel32/KERNEL32.DEF	(revision 506)
@@ -1,3 +1,3 @@
-; $Id: KERNEL32.DEF,v 1.19 1999-08-13 18:38:19 sandervl Exp $
+; $Id: KERNEL32.DEF,v 1.20 1999-08-16 13:54:32 sandervl Exp $
 
 ;Created by BLAST for IBM's compiler
@@ -980,4 +980,6 @@
    GetThreadTHDB        = _GetThreadTHDB@0			   @1235
 
+   CreateWin32Exe       = _CreateWin32Exe@4                        @1236
+
 ; HEAP Management (heap.cpp, heapstring.cpp)
     HEAP_xalloc                = _HEAP_xalloc@12                   @1240
Index: trunk/src/kernel32/winexe.cpp
===================================================================
--- trunk/src/kernel32/winexe.cpp	(revision 505)
+++ trunk/src/kernel32/winexe.cpp	(revision 506)
@@ -1,3 +1,3 @@
-/* $Id: winexe.cpp,v 1.8 1999-07-07 08:11:10 sandervl Exp $ */
+/* $Id: winexe.cpp,v 1.9 1999-08-16 13:54:32 sandervl Exp $ */
 
 /*
@@ -34,4 +34,11 @@
 Win32Exe *WinExe = NULL;
 
+//******************************************************************************
+//Called by ring 3 pe loader to create win32 executable
+//******************************************************************************
+Win32Exe* WIN32API CreateWin32Exe(char *szFileName) 
+{
+  return new Win32Exe(szFileName);
+}
 //******************************************************************************
 //******************************************************************************
Index: trunk/src/peldr/initterm.cpp
===================================================================
--- trunk/src/peldr/initterm.cpp	(revision 506)
+++ trunk/src/peldr/initterm.cpp	(revision 506)
@@ -0,0 +1,99 @@
+/* $Id: initterm.cpp,v 1.1 1999-08-16 13:54:06 sandervl Exp $ */
+
+/*
+ * PELDR DLL entry point
+ *
+ * Copyright 1998 Sander van Leeuwen
+ *
+ *
+ * Project Odin Software License can be found in LICENSE.TXT
+ *
+ */
+
+/*-------------------------------------------------------------*/
+/* INITERM.C -- Source for a custom dynamic link library       */
+/*              initialization and termination (_DLL_InitTerm) */
+/*              function.                                      */
+/*                                                             */
+/* When called to perform initialization, this sample function */
+/* gets storage for an array of integers, and initializes its  */
+/* elements with random integers.  At termination time, it     */
+/* frees the array.  Substitute your own special processing.   */
+/*-------------------------------------------------------------*/
+
+
+/* Include files */
+#define  INCL_DOSMODULEMGR
+#define  INCL_DOSPROCESS
+#include <os2.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <odin.h>
+#include <misc.h>       /*PLF Wed  98-03-18 23:18:15*/
+
+/*-------------------------------------------------------------------*/
+/* A clean up routine registered with DosExitList must be used if    */
+/* runtime calls are required and the runtime is dynamically linked. */
+/* This will guarantee that this clean up routine is run before the  */
+/* library DLL is terminated.                                        */
+/*-------------------------------------------------------------------*/
+static void APIENTRY cleanup(ULONG reason);
+
+ULONG reservedMemory = 0;
+
+/****************************************************************************/
+/* _DLL_InitTerm is the function that gets called by the operating system   */
+/* loader when it loads and frees this DLL for each process that accesses   */
+/* this DLL.  However, it only gets called the first time the DLL is loaded */
+/* and the last time it is freed for a particular process.  The system      */
+/* linkage convention MUST be used because the operating system loader is   */
+/* calling this function.                                                   */
+/****************************************************************************/
+unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long
+                                    ulFlag)
+{
+   size_t i;
+   APIRET rc;
+
+   /*-------------------------------------------------------------------------*/
+   /* If ulFlag is zero then the DLL is being loaded so initialization should */
+   /* be performed.  If ulFlag is 1 then the DLL is being freed so            */
+   /* termination should be performed.                                        */
+   /*-------------------------------------------------------------------------*/
+
+   switch (ulFlag) {
+      case 0 :
+
+         /*******************************************************************/
+         /* The C run-time environment initialization function must be      */
+         /* called before any calls to C run-time functions that are not    */
+         /* inlined.                                                        */
+         /*******************************************************************/
+
+	 //SvL: Reserve memory for win32 exes without fixups
+         //     This is done before any Odin or PMWIN dll is loaded, so we'll get
+         //     a very low virtual address. (which is exactly what we want)
+	 rc = DosAllocMem((PPVOID)&reservedMemory, 16*1024*1024, PAG_WRITE | PAG_READ);
+
+         /*******************************************************************/
+         /* A DosExitList routine must be used to clean up if runtime calls */
+         /* are required and the runtime is dynamically linked.             */
+         /*******************************************************************/
+         break;
+      case 1 :
+         break;
+      default  :
+         return 0UL;
+   }
+
+   /***********************************************************/
+   /* A non-zero value must be returned to indicate success.  */
+   /***********************************************************/
+   return 1UL;
+}
+
+ULONG SYSTEM ReserveMem()
+{
+   return reservedMemory;
+}
Index: trunk/src/peldr/makefile
===================================================================
--- trunk/src/peldr/makefile	(revision 505)
+++ trunk/src/peldr/makefile	(revision 506)
@@ -1,3 +1,3 @@
-# $Id: makefile,v 1.4 1999-08-09 21:03:19 phaller Exp $
+# $Id: makefile,v 1.5 1999-08-16 13:54:06 sandervl Exp $
 
 #
@@ -17,20 +17,30 @@
 CFLAGS = $(CFLAGS) -Ge+ /Gd-  -I$(PDWIN32_INCLUDE)
 CXXFLAGS = $(CXXFLAGS) -Ge+ /Gd- -I$(PDWIN32_INCLUDE)
-LDFLAGS = $(LDFLAGS) -Ge+ /B"/pmtype:pm /stack:0x30000 /NOBASE /Map" \
-          ..\..\lib\kernel32.lib os2386.lib ..\..\lib\odincrt.lib cppom30.lib
+LDFLAGSEXE = $(LDFLAGS) -Ge+ /B"/pmtype:pm /stack:0x30000 /NOBASE /Map" \
+             $(PDWIN32_LIB)\peldr.lib os2386.lib $(PDWIN32_LIB)\odincrt.lib cppom30.lib
 
 TARGET = pe
+TARGETDLL = peldr
 
 OBJS =  pe.obj
+OBJSDLL =  initterm.obj
 
-all: $(TARGET).exe
+all: $(TARGETDLL).dll $(TARGETDLL).lib $(TARGET).exe
 
 
 $(TARGET).exe: $(OBJS)
-        $(LD) $(LDFLAGS) -Fe$@ $(OBJS)
+        $(LD) $(LDFLAGSEXE) -Fe$@ $(OBJS)
         $(CP) $(TARGET).exe $(PDWIN32_BIN)
 
+$(TARGETDLL).dll: $(OBJSDLL) $(TARGETDLL).def 
+	$(LD) $(LDFLAGS) -Fe$@ $(OBJSDLL) sbsstub.obj $(TARGETDLL).def OS2386.LIB CPPOM30.LIB
+	$(CP) $@ $(PDWIN32_BIN)
+
+$(TARGETDLL).lib: $(TARGETDLL).dll
+	$(IMPLIB) $(IMPLIBFLAGS) $@ $(TARGETDLL).dll
+	$(CP) $@ $(PDWIN32_LIB)
 
 
+initterm.obj: initterm.cpp
 misc.obj: misc.cpp
 pe.obj: pe.cpp pe.h $(PDWIN32_INCLUDE)\winimage.h $(PDWIN32_INCLUDE)\winexe.h
@@ -39,2 +49,4 @@
         $(RM) *.obj *.lib *.dll *~ *.map *.pch *.exe
         $(RM) $(PDWIN32_BIN)\$(TARGET).exe
+	$(RM) $(PDWIN32_BIN)\$(TARGETDLL).dll
+	$(RM) $(PDWIN32_LIB)\$(TARGETDLL).lib
Index: trunk/src/peldr/pe.cpp
===================================================================
--- trunk/src/peldr/pe.cpp	(revision 505)
+++ trunk/src/peldr/pe.cpp	(revision 506)
@@ -1,13 +1,11 @@
-/* $Id: pe.cpp,v 1.4 1999-06-20 14:02:13 sandervl Exp $ */
+/* $Id: pe.cpp,v 1.5 1999-08-16 13:54:07 sandervl Exp $ */
 
-/*
- *
- * Project Odin Software License can be found in LICENSE.TXT
- *
- */
 /*
  * PELDR main exe loader code
  *
  * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
+ *
+ *
+ * Project Odin Software License can be found in LICENSE.TXT
  *
  */
@@ -16,6 +14,8 @@
 #define INCL_DOSPROCESS          /* DOS Process values       */
 #define INCL_DOSMISC             /* DOS Miscellanous values  */
+#define INCL_DOSMODULEMGR
 #define INCL_WIN
-#include <os2wrap.h>	//Odin32 OS/2 api wrappers
+#include <os2.h>
+#include <bseord.h>
 #include <stdio.h>
 #include <string.h>
@@ -28,4 +28,5 @@
 #include <windll.h>
 #include <wprocess.h>
+#include "pe.h"
 
 char INFO_BANNER[]      = "Usage: PE winexe commandline";
@@ -40,5 +41,22 @@
 char fullpath[CCHMAXPATH];
 
-void UpCase(char *mixedcase);
+typedef HAB  (* APIENTRY WININITIALIZEPROC)(ULONG flOptions);
+typedef BOOL (* APIENTRY WINTERMINATEPROC)(HAB hab);
+typedef HMQ  (* APIENTRY WINCREATEMSGQUEUEPROC) (HAB hab, LONG cmsg);
+typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq);
+typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent,
+		                              HWND hwndOwner,
+                		              PCSZ  pszText,
+                                	      PCSZ  pszCaption,
+                                	      ULONG idWindow,
+                                	      ULONG flStyle);
+
+WININITIALIZEPROC      MyWinInitialize      = 0;
+WINTERMINATEPROC       MyWinTerminate       = 0;
+WINCREATEMSGQUEUEPROC  MyWinCreateMsgQueue  = 0;
+WINDESTROYMSGQUEUEPROC MyWinDestroyMsgQueue = 0;
+WINMESSAGEBOXPROC      MyWinMessageBox      = 0;
+
+WIN32CTOR              CreateWin32Exe       = 0;
 
 int main(int argc, char *argv[])
@@ -51,29 +69,42 @@
  PTIB   ptib;
  Win32Exe *WinExe;
- APIRET rc;
+ APIRET  rc;
+ HMODULE hmodPMWin, hmodKernel32;
 
-  if ((hab = WinInitialize(0)) == 0L) /* Initialize PM     */
-        return(1);
+  printf("memory allocated at %X\n", ReserveMem());
 
-  hmq = WinCreateMsgQueue(hab, 0);
+  rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN.DLL", &hmodPMWin);
+  rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
+  rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
+  rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
+  rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
+  rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
 
+  rc = DosLoadModule(exeName, sizeof(exeName), "KERNEL32.DLL", &hmodKernel32);
+  rc = DosQueryProcAddr(hmodKernel32, 0, "CreateWin32Exe", (PFN *)&CreateWin32Exe);
+
+  if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */
+	return(1);
+
+  hmq = MyWinCreateMsgQueue(hab, 0);
+  
   if(argc < 2) {
-        WinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-        return(0);
+	MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
+	return(0);
   }
 
   strcpy(exeName, argv[1]);
-  UpCase(exeName);
+  strupr(exeName);
   if(strstr(exeName, ".EXE") == NULL) {
         strcat(exeName, ".EXE");
   }
-  WinExe = new Win32Exe(exeName);
+  WinExe = CreateWin32Exe(exeName);
   if(WinExe == NULL) {
-        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
+        MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
         return(1);
   }
   rc = DosGetInfoBlocks(&ptib, &ppib);
   if(rc) {
-        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
+        MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szInteralErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
         delete WinExe;
         return(1);
@@ -107,20 +138,11 @@
   delete WinExe;
 
-  if(hmq) WinDestroyMsgQueue( hmq );             /* Tidy up...                   */
-  WinTerminate( hab );                   /* Terminate the application    */
+  if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
+  MyWinTerminate( hab );                   /* Terminate the application    */
+
+  DosFreeModule(hmodPMWin);
+  DosFreeModule(hmodKernel32);
   return(0);
 }
 //******************************************************************************
 //******************************************************************************
-void UpCase(char *mixedcase)
-{
- int i;
-
-  for(i=0;i<strlen(mixedcase);i++) {
-        if(mixedcase[i] >= 'a' && mixedcase[i] <= 'z') {
-                mixedcase[i] += 'A' - 'a';
-        }
-  }
-}
-//******************************************************************************
-//******************************************************************************
Index: trunk/src/peldr/pe.h
===================================================================
--- trunk/src/peldr/pe.h	(revision 505)
+++ trunk/src/peldr/pe.h	(revision 506)
@@ -1,3 +1,3 @@
-/* $Id: pe.h,v 1.3 1999-06-10 16:56:36 phaller Exp $ */
+/* $Id: pe.h,v 1.4 1999-08-16 13:54:07 sandervl Exp $ */
 
 /*
@@ -23,16 +23,5 @@
 extern char szInteralErrorMsg[];
 
-#define DWORD  ULONG
-#define LPVOID VOID *
-#define WORD   USHORT
-#define UNALIGNED
-#define PDWORD DWORD *
-#define PWORD  WORD *
-#define NTAPI
-#define BOOLEAN ULONG
-#define WCHAR   USHORT
-#define HANDLE  ULONG
-
-#define WIN32API __stdcall
+ULONG SYSTEM ReserveMem();
 
 #endif // NOIMAGE
Index: trunk/src/peldr/peldr.def
===================================================================
--- trunk/src/peldr/peldr.def	(revision 506)
+++ trunk/src/peldr/peldr.def	(revision 506)
@@ -0,0 +1,7 @@
+; $Id: peldr.def,v 1.1 1999-08-16 13:54:07 sandervl Exp $
+
+LIBRARY PELDR INITINSTANCE
+DATA MULTIPLE NONSHARED
+EXPORTS
+
+   ReserveMem         = ReserveMem				   @1
