Index: trunk/src/NTDLL/initntdll.cpp
===================================================================
--- trunk/src/NTDLL/initntdll.cpp	(revision 9704)
+++ trunk/src/NTDLL/initntdll.cpp	(revision 9704)
@@ -0,0 +1,84 @@
+/*
+ * DLL entry point
+ *
+ * Copyright 1998 Sander van Leeuwen
+ * Copyright 1998 Peter Fitzsimmons
+ *
+ *
+ * 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 <os2wrap.h>    //Odin32 OS/2 api wrappers
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <odin.h>
+#include <win32type.h>
+#include <winconst.h>
+#include <odinlx.h>
+#include <misc.h>       /*PLF Wed  98-03-18 23:18:15*/
+#include <initdll.h>
+#include <exitlist.h>
+
+
+extern "C" {
+BOOL WIN32API NTDLL_LibMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
+extern DWORD _Resource_PEResTab;
+}
+
+static HMODULE dllHandle = 0;
+
+//******************************************************************************
+//******************************************************************************
+ULONG APIENTRY inittermNTDLL(ULONG hModule, ULONG ulFlag)
+{
+   /*-------------------------------------------------------------------------*/
+   /* 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 :
+
+         CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed  98-03-18 05:28:48*/
+         dllHandle = RegisterLxDll(hModule, (WIN32DLLENTRY)NTDLL_LibMain, (PVOID)&_Resource_PEResTab,
+                                   0, 0, 0);
+         if(dllHandle == 0)
+             return 0UL;
+
+         dprintf(("ntdll init %s %s (%x)", __DATE__, __TIME__, inittermNTDLL));
+
+         break;
+      case 1 :
+         if(dllHandle) {
+             UnregisterLxDll(dllHandle);
+         }
+         break;
+      default  :
+         return 0UL;
+   }
+
+   /***********************************************************/
+   /* A non-zero value must be returned to indicate success.  */
+   /***********************************************************/
+   return 1UL;
+}
+//******************************************************************************
+//******************************************************************************
Index: trunk/src/NTDLL/initterm.cpp
===================================================================
--- trunk/src/NTDLL/initterm.cpp	(revision 9701)
+++ trunk/src/NTDLL/initterm.cpp	(revision 9704)
@@ -35,33 +35,16 @@
 #include <misc.h>       /*PLF Wed  98-03-18 23:18:15*/
 #include <initdll.h>
+#include <exitlist.h>
 
 extern "C" {
- //Win32 resource table (produced by wrc)
- extern DWORD _Resource_PEResTab;
  void __ctordtorInit (void);
  void __ctordtorTerm (void);
+ int _CRT_init (void);
+ void _CRT_term (void);
 }
-static HMODULE dllHandle = 0;
 
-BOOL WINAPI NTDLL_LibMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
 
-//******************************************************************************
-//******************************************************************************
-BOOL WIN32API LibMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
-{
-   switch (fdwReason)
-   {
-   case DLL_PROCESS_ATTACH:
-   case DLL_THREAD_ATTACH:
-   case DLL_THREAD_DETACH:
-	return NTDLL_LibMain(hinstDLL, fdwReason, fImpLoad);
+static void APIENTRY cleanup(ULONG reason);
 
-   case DLL_PROCESS_DETACH:
-	NTDLL_LibMain(hinstDLL, fdwReason, fImpLoad);
-	__ctordtorTerm();
-	return TRUE;
-   }
-   return FALSE;
-}
 /****************************************************************************/
 /* _DLL_InitTerm is the function that gets called by the operating system   */
@@ -86,18 +69,16 @@
    switch (ulFlag) {
       case 0 :
-         __ctordtorInit();
+         if (_CRT_init () != 0)
+           return 0;
+         __ctordtorInit ();
 
-         CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed  98-03-18 05:28:48*/
+         rc = DosExitList(EXITLIST_NONCRITDLL|EXLST_ADD, cleanup);
+         if (rc)
+            return 0UL;
 
-	 dllHandle = RegisterLxDll(hModule, (WIN32DLLENTRY)LibMain, (PVOID)&_Resource_PEResTab);
-         if(dllHandle == 0) 
-		return 0UL;
+         return inittermNTDLL(hModule, ulFlag);
 
-         break;
       case 1 :
-         if(dllHandle) {
-	 	UnregisterLxDll(dllHandle);
-         }
-         break;
+         return inittermNTDLL(hModule, ulFlag);
 
       default  :
@@ -112,2 +93,13 @@
 //******************************************************************************
 //******************************************************************************
+static void APIENTRY cleanup(ULONG ulReason)
+{
+    dprintf(("NTDLL exit"));
+    __ctordtorTerm();
+    _CRT_term ();
+
+    DosExitList(EXLST_EXIT, cleanup);
+    return ;
+}
+//******************************************************************************
+//******************************************************************************
Index: trunk/src/NTDLL/makefile
===================================================================
--- trunk/src/NTDLL/makefile	(revision 9701)
+++ trunk/src/NTDLL/makefile	(revision 9704)
@@ -1,3 +1,3 @@
-# $Id: makefile,v 1.33 2003-01-16 15:22:36 sandervl Exp $
+# $Id: makefile,v 1.34 2003-01-22 12:56:07 sandervl Exp $
 
 #
@@ -33,4 +33,5 @@
 $(OBJDIR)\sync.obj \
 $(OBJDIR)\initterm.obj \
+$(OBJDIR)\initntdll.obj \
 $(OBJDIR)\time.obj \
 $(OBJDIR)\unknown.obj \
Index: trunk/src/NTDLL/ntdll.c
===================================================================
--- trunk/src/NTDLL/ntdll.c	(revision 9704)
+++ trunk/src/NTDLL/ntdll.c	(revision 9704)
@@ -0,0 +1,128 @@
+/* $Id: ntdll.c,v 1.4 2003-01-22 12:56:08 sandervl Exp $ */
+
+/*
+ *
+ * Project Odin Software License can be found in LICENSE.TXT
+ * Win32 NT Runtime / NTDLL for OS/2
+ *
+ * Copyright 1998, 1999 Patrick Haller (phaller@gmx.net)
+ *
+ * @(#) ntdll.cpp  1.0.1   1999/05/08 SvL: Changes for compilation with Wine headers
+ *                 1.0.0   1998/05/20 PH Start from WINE/NTDLL.C
+ *
+ * NT basis DLL
+ *
+ * Copyright 1996 Marcus Meissner
+ * Copyright 1998 Patrick Haller (adapted for win32os2)
+ */
+
+ /* Changes to the original NTDLL.C from the WINE project
+
+  - includes replaced by the win32os2 standard includes
+  - replaced WINAPI by WIN32API
+  - moved in some string functions
+  - replaced HANDLE32 by HANDLE
+  - lstrlen32A -> OS2lstrlenA
+  - lstrlen32W -> OS2lstrlenW
+*/
+
+/*****************************************************************************
+ * Includes                                                                  *
+ *****************************************************************************/
+
+#include <windows.h>
+#include <winnt.h>
+#include <ntdef.h>
+#ifndef __EMX__
+#include <builtin.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <misc.h>
+#include "unicode.h"
+
+#include "ntdll.h"
+#include <ntdllsec.h>
+#include <versionos2.h>
+/*****************************************************************************
+ * Types & Defines                                                           *
+ *****************************************************************************/
+
+#define NTSTATUS DWORD
+
+//SvL: per process heap for NTDLL
+HANDLE NTDLL_hHeap = 0;
+
+PROCESSTHREAD_SECURITYINFO ProcSecInfo = {0};
+
+/*****************************************************************************
+ * Name      : DbgPrint
+ * Purpose   : print a debug line to somewhere?
+ * Parameters:
+ * Variables :
+ * Result    :
+ * Remark    : NTDLL.21
+ * Status    : UNTESTED STUB
+ *
+ * Author    : Patrick Haller [Tue, 1999/06/01 09:00]
+ *****************************************************************************/
+void __cdecl DbgPrint(LPCSTR lpcstrFormat,LPVOID args)
+{
+  UCHAR   szBuffer[600]; // as in original NTDLL.DLL
+  int     rc;
+
+  rc = wvsnprintfA((LPSTR)szBuffer,
+                   sizeof(szBuffer),
+                   lpcstrFormat,
+                   (va_list)args);
+
+  dprintf(("NTDLL: DbgPrint[%s]\n",
+           szBuffer));
+
+  //@@@PH raise debug exception if running in debugger
+}
+
+//******************************************************************************
+BOOL WIN32API NTDLL_LibMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
+{
+    dprintf(("NTDLL_LibMain: 0x%x 0x%lx %p\n", hinstDLL, fdwReason, lpvReserved));
+
+    switch (fdwReason) {
+    case DLL_PROCESS_ATTACH:
+    {
+        SID_IDENTIFIER_AUTHORITY sidIdAuth = {0};
+
+     	NTDLL_hHeap = HeapCreate(0, 0x10000, 0);
+
+	ProcSecInfo.dwType = SECTYPE_PROCESS | SECTYPE_INITIALIZED;
+        RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &ProcSecInfo.SidUser.User.Sid);
+	ProcSecInfo.SidUser.User.Attributes = 0; //?????????
+
+        ProcSecInfo.pTokenGroups = (TOKEN_GROUPS*)Heap_Alloc(sizeof(TOKEN_GROUPS));
+	ProcSecInfo.pTokenGroups->GroupCount = 1;
+        RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &ProcSecInfo.PrimaryGroup.PrimaryGroup);
+	ProcSecInfo.pTokenGroups->Groups[0].Sid = ProcSecInfo.PrimaryGroup.PrimaryGroup;
+	ProcSecInfo.pTokenGroups->Groups[0].Attributes = 0; //????
+
+//        pPrivilegeSet   = NULL;
+//        pTokenPrivileges= NULL;
+//        TokenOwner      = {0};
+//        DefaultDACL     = {0};
+//        TokenSource     = {0};
+        ProcSecInfo.TokenType = TokenPrimary;
+	break;
+    }
+    case DLL_PROCESS_DETACH:
+        HeapDestroy(NTDLL_hHeap);
+        NTDLL_hHeap = 0;
+	break;
+    case DLL_THREAD_ATTACH:
+	break;
+    case DLL_THREAD_DETACH:
+	break;
+    default:
+	break;
+    }
+    return TRUE;
+}
Index: trunk/src/NTDLL/ntdll.cpp
===================================================================
--- trunk/src/NTDLL/ntdll.cpp	(revision 9701)
+++ 	(revision )
@@ -1,129 +1,0 @@
-/* $Id: ntdll.cpp,v 1.7 2003-01-16 15:22:37 sandervl Exp $ */
-
-/*
- *
- * Project Odin Software License can be found in LICENSE.TXT
- * Win32 NT Runtime / NTDLL for OS/2
- *
- * Copyright 1998, 1999 Patrick Haller (phaller@gmx.net)
- *
- * @(#) ntdll.cpp  1.0.1   1999/05/08 SvL: Changes for compilation with Wine headers
- *                 1.0.0   1998/05/20 PH Start from WINE/NTDLL.C
- *
- * NT basis DLL
- *
- * Copyright 1996 Marcus Meissner
- * Copyright 1998 Patrick Haller (adapted for win32os2)
- */
-
- /* Changes to the original NTDLL.C from the WINE project
-
-  - includes replaced by the win32os2 standard includes
-  - replaced WINAPI by WIN32API
-  - moved in some string functions
-  - replaced HANDLE32 by HANDLE
-  - lstrlen32A -> OS2lstrlenA
-  - lstrlen32W -> OS2lstrlenW
-*/
-
-/*****************************************************************************
- * Includes                                                                  *
- *****************************************************************************/
-
-#include <os2win.h>
-#include <winnt.h>
-#include <ntdef.h>
-#ifndef __EMX__
-#include <builtin.h>
-#endif
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <misc.h>
-#include "unicode.h"
-
-#include "ntdll.h"
-#include <ntdllsec.h>
-
-/*****************************************************************************
- * Types & Defines                                                           *
- *****************************************************************************/
-
-#define NTSTATUS DWORD
-
-//SvL: per process heap for NTDLL
-HANDLE NTDLL_hHeap = 0;
-
-PROCESSTHREAD_SECURITYINFO ProcSecInfo = {0};
-
-/*****************************************************************************
- * Name      : DbgPrint
- * Purpose   : print a debug line to somewhere?
- * Parameters:
- * Variables :
- * Result    :
- * Remark    : NTDLL.21
- * Status    : UNTESTED STUB
- *
- * Author    : Patrick Haller [Tue, 1999/06/01 09:00]
- *****************************************************************************/
-void __cdecl DbgPrint(LPCSTR lpcstrFormat,LPVOID args)
-{
-  UCHAR   szBuffer[600]; // as in original NTDLL.DLL
-  int     rc;
-
-  rc = wvsnprintfA((LPSTR)szBuffer,
-                   sizeof(szBuffer),
-                   lpcstrFormat,
-                   (va_list)args);
-
-  dprintf(("NTDLL: DbgPrint[%s]\n",
-           szBuffer));
-
-  //@@@PH raise debug exception if running in debugger
-}
-
-
-BOOL WINAPI NTDLL_LibMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
-{
-    dprintf(("NTDLL_LibMain: 0x%x 0x%lx %p\n", hinstDLL, fdwReason, lpvReserved));
-
-    switch (fdwReason) {
-    case DLL_PROCESS_ATTACH:
-    {
-     SID_IDENTIFIER_AUTHORITY sidIdAuth = {0};
-
-     	NTDLL_hHeap = HeapCreate(0, 0x10000, 0);
-
-	ProcSecInfo.dwType = SECTYPE_PROCESS | SECTYPE_INITIALIZED;
-        RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &ProcSecInfo.SidUser.User.Sid);
-	ProcSecInfo.SidUser.User.Attributes = 0; //?????????
-
-        ProcSecInfo.pTokenGroups = (TOKEN_GROUPS*)Heap_Alloc(sizeof(TOKEN_GROUPS));
-	ProcSecInfo.pTokenGroups->GroupCount = 1;
-        RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &ProcSecInfo.PrimaryGroup.PrimaryGroup);
-	ProcSecInfo.pTokenGroups->Groups[0].Sid = ProcSecInfo.PrimaryGroup.PrimaryGroup;
-	ProcSecInfo.pTokenGroups->Groups[0].Attributes = 0; //????
-
-//        pPrivilegeSet   = NULL;
-//        pTokenPrivileges= NULL;
-//        TokenOwner      = {0};
-//        DefaultDACL     = {0};
-//        TokenSource     = {0};
-        ProcSecInfo.TokenType = TokenPrimary;
-	break;
-    }
-    case DLL_PROCESS_DETACH:
-        HeapDestroy(NTDLL_hHeap);
-        NTDLL_hHeap = 0;
-	break;
-    case DLL_THREAD_ATTACH:
-	break;
-    case DLL_THREAD_DETACH:
-	break;
-    default:
-	break;
-    }
-    return TRUE;
-}
-
