Index: trunk/src/kernel32/KERNEL32.DEF
===================================================================
--- trunk/src/kernel32/KERNEL32.DEF	(revision 125)
+++ trunk/src/kernel32/KERNEL32.DEF	(revision 126)
@@ -1,3 +1,3 @@
-; $Id: KERNEL32.DEF,v 1.11 1999-06-17 22:06:10 phaller Exp $
+; $Id: KERNEL32.DEF,v 1.12 1999-06-20 10:55:35 sandervl Exp $
 
 ;Created by BLAST for IBM's compiler
@@ -964,2 +964,5 @@
    GetFS		= GetFS					   @1226
    SetFS		= SetFS					   @1227
+
+   RestoreOS2TIB        = _RestoreOS2TIB@0                         @1228
+   SetWin32TIB          = _SetWin32TIB@0                           @1229
Index: trunk/src/kernel32/conwin.h
===================================================================
--- trunk/src/kernel32/conwin.h	(revision 125)
+++ trunk/src/kernel32/conwin.h	(revision 126)
@@ -1,50 +1,6 @@
-/* $Id: conwin.h,v 1.1 1999-06-17 18:22:43 phaller Exp $ */
+/* $Id: conwin.h,v 1.2 1999-06-20 10:55:36 sandervl Exp $ */
 
 #ifndef _CONWIN_H_
 #define _CONWIN_H_
-
-#ifndef _FILETIME_
-#define _FILETIME_
-/* 64 bit number of 100 nanoseconds intervals since January 1, 1601 */
-typedef struct
-{
-  DWORD  dwLowDateTime;
-  DWORD  dwHighDateTime;
-} FILETIME, *LPFILETIME;
-#endif /* _FILETIME_ */
-
-typedef struct
-{
-  int dwFileAttributes;
-  FILETIME ftCreationTime;
-  FILETIME ftLastAccessTime;
-  FILETIME ftLastWriteTime;
-  int dwVolumeSerialNumber;
-  int nFileSizeHigh;
-  int nFileSizeLow;
-  int nNumberOfLinks;
-  int nFileIndexHigh;
-  int nFileIndexLow;
-} BY_HANDLE_FILE_INFORMATION ;
-
-
-#define OFS_MAXPATHNAME 128
-typedef struct
-{
-    BYTE cBytes;
-    BYTE fFixedDisk;
-    WORD nErrCode;
-    BYTE reserved[4];
-    BYTE szPathName[OFS_MAXPATHNAME];
-} OFSTRUCT, *LPOFSTRUCT;
-
-
-/* The security attributes structure */
-typedef struct
-{
-    DWORD   nLength;
-    LPVOID  lpSecurityDescriptor;
-    BOOL  bInheritHandle;
-} SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES;
 
 #ifndef INVALID_HANDLE_VALUE
Index: trunk/src/kernel32/except.h
===================================================================
--- trunk/src/kernel32/except.h	(revision 125)
+++ trunk/src/kernel32/except.h	(revision 126)
@@ -1,3 +1,3 @@
-/* $Id: except.h,v 1.4 1999-06-19 13:57:51 sandervl Exp $ */
+/* $Id: except.h,v 1.5 1999-06-20 10:55:36 sandervl Exp $ */
 
 /*
@@ -14,8 +14,11 @@
 
 //SvL: Replace original startup code exception handler
+#ifndef WIN32_TIBSEL
 void  ReplaceExceptionHandler(void);
+void  ChangeTIBStack();
+#endif
+void __cdecl SetExceptionChain(DWORD);
+
 void  DisableFPUExceptions();
-void __cdecl SetExceptionChain(DWORD);
-void  ChangeTIBStack();
 ULONG GetDllEntryPoint();
 
Index: trunk/src/kernel32/initterm.cpp
===================================================================
--- trunk/src/kernel32/initterm.cpp	(revision 125)
+++ trunk/src/kernel32/initterm.cpp	(revision 126)
@@ -1,3 +1,3 @@
-/* $Id: initterm.cpp,v 1.4 1999-06-19 13:57:51 sandervl Exp $ */
+/* $Id: initterm.cpp,v 1.5 1999-06-20 10:55:36 sandervl Exp $ */
 
 /*
@@ -34,4 +34,5 @@
 #include <misc.h>
 #include <wprocess.h>
+#include <handlemanager.h>
 
 extern "C" {
@@ -130,4 +131,7 @@
 
 	    InitializeTIB(TRUE);
+	    //SvL: Do it here instead of during the exe object creation
+	    //(std handles can be used in win32 dll initialization routines
+	    HMInitialize();             /* store standard handles within HandleManager */
             break;
         case 1 :
Index: trunk/src/kernel32/kernel32exp.def
===================================================================
--- trunk/src/kernel32/kernel32exp.def	(revision 125)
+++ trunk/src/kernel32/kernel32exp.def	(revision 126)
@@ -1,3 +1,3 @@
-; $Id: kernel32exp.def,v 1.4 1999-06-10 20:47:59 phaller Exp $
+; $Id: kernel32exp.def,v 1.5 1999-06-20 10:55:36 sandervl Exp $
 
 ;Created by BLAST for IBM's compiler
@@ -830,2 +830,4 @@
 GetFS					   @1226
 SetFS					   @1227
+_RestoreOS2TIB@0                         @1228
+_SetWin32TIB@0                           @1229
Index: trunk/src/kernel32/makefile
===================================================================
--- trunk/src/kernel32/makefile	(revision 125)
+++ trunk/src/kernel32/makefile	(revision 126)
@@ -1,3 +1,3 @@
-# $Id: makefile,v 1.7 1999-06-19 17:58:32 sandervl Exp $
+# $Id: makefile,v 1.8 1999-06-20 10:55:36 sandervl Exp $
 
 #
@@ -95,5 +95,5 @@
     $(PDWIN32_INCLUDE)\unicode.h \
     heap.h \
-    wprocess.h \
+    $(PDWIN32_INCLUDE)\wprocess.h \
     os2util.h \
     $(PDWIN32_INCLUDE)\nameid.h
@@ -232,5 +232,5 @@
 lfile.OBJ: \
     .\lfile.cpp \
-    wprocess.h \
+    $(PDWIN32_INCLUDE)\wprocess.h \
     os2util.h
 
Index: trunk/src/kernel32/thread.H
===================================================================
--- trunk/src/kernel32/thread.H	(revision 125)
+++ trunk/src/kernel32/thread.H	(revision 126)
@@ -1,13 +1,11 @@
-/* $Id: thread.H,v 1.4 1999-06-19 17:58:33 sandervl Exp $ */
+/* $Id: thread.H,v 1.5 1999-06-20 10:55:36 sandervl Exp $ */
 
-/*
- *
- * Project Odin Software License can be found in LICENSE.TXT
- *
- */
 /*
  * Win32 Thread API functions
  *
  * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
+ *
+ *
+ * Project Odin Software License can be found in LICENSE.TXT
  *
  */
Index: trunk/src/kernel32/thread.cpp
===================================================================
--- trunk/src/kernel32/thread.cpp	(revision 125)
+++ trunk/src/kernel32/thread.cpp	(revision 126)
@@ -1,13 +1,11 @@
-/* $Id: thread.cpp,v 1.5 1999-06-19 17:58:33 sandervl Exp $ */
+/* $Id: thread.cpp,v 1.6 1999-06-20 10:55:36 sandervl Exp $ */
 
-/*
- *
- * Project Odin Software License can be found in LICENSE.TXT
- *
- */
 /*
  * Win32 Thread API functions
  *
  * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
+ *
+ *
+ * Project Odin Software License can be found in LICENSE.TXT
  *
  */
@@ -161,4 +159,16 @@
 
 #ifdef WIN32_TIBSEL
+  TEB *winteb = (TEB *)InitializeTIB();
+  if(winteb == NULL) {
+	dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
+	DebugInt3();
+	return 0;
+  }
+  winteb->flags = me->dwFlags;
+
+  THDB *thdb   = (THDB *)(winteb+1);
+  thdb->entry_point = (void *)winthread;
+  thdb->entry_arg   = (void *)userdata;
+  SetWin32TIB();
 #else
   ReplaceExceptionHandler();
Index: trunk/src/kernel32/winexe.cpp
===================================================================
--- trunk/src/kernel32/winexe.cpp	(revision 125)
+++ trunk/src/kernel32/winexe.cpp	(revision 126)
@@ -1,3 +1,3 @@
-/* $Id: winexe.cpp,v 1.6 1999-06-19 10:54:43 sandervl Exp $ */
+/* $Id: winexe.cpp,v 1.7 1999-06-20 10:55:36 sandervl Exp $ */
 
 /*
@@ -21,16 +21,14 @@
 #include <iostream.h>
 #include <fstream.h>
-#include "misc.h"
-#include "nameid.h"
-#include "win32type.h"
+#include <misc.h>
+#include <nameid.h>
+#include <win32type.h>
 #include <winexe.h>
-#include "wprocess.h"
-#include "pefile.h"
+#include <wprocess.h>
+#include <pefile.h>
 #include "cio.h"
 
 #include "conwin.h"          // Windows Header for console only
 #include "console.h"
-#include "handlemanager.h"
-
 
 Win32Exe *WinExe = NULL;
@@ -46,6 +44,4 @@
   dprintf(("Win32Exe ctor: %s", szFileName));
 
-  HMInitialize();             /* store standard handles within HandleManager */
-  dprintf(("KERNEL32/WINEXE: HandleManager Initialized.\n"));
   if(fConsoleApp) {
    dprintf(("Console application!\n"));
@@ -62,7 +58,4 @@
          fConsoleApp(FALSE), cmdline(NULL), OS2InstanceHandle(-1)
 {
-  HMInitialize();             /* store standard handles within HandleManager */
-  dprintf(("KERNEL32/WINEXE: HandleManager Initialized.\n"));
-
   if(GET_CONSOLE(Win32TableId) == 1) {//console app
    dprintf(("Console application!\n"));
@@ -86,4 +79,6 @@
 ULONG Win32Exe::start()
 {
+ ULONG rc;
+
   if(getenv("WIN32_IOPL2")) {
    io_init1();
@@ -92,5 +87,8 @@
 
   fExeStarted  = TRUE;
-  return ((WIN32EXEENTRY)entryPoint)();
+  SetWin32TIB();
+  rc = ((WIN32EXEENTRY)entryPoint)();
+  RestoreOS2TIB();
+  return rc;
 }
 //******************************************************************************
Index: trunk/src/kernel32/wprocess.cpp
===================================================================
--- trunk/src/kernel32/wprocess.cpp	(revision 125)
+++ trunk/src/kernel32/wprocess.cpp	(revision 126)
@@ -1,3 +1,3 @@
-/* $Id: wprocess.cpp,v 1.10 1999-06-19 17:58:33 sandervl Exp $ */
+/* $Id: wprocess.cpp,v 1.11 1999-06-20 10:55:36 sandervl Exp $ */
 
 /*
@@ -28,7 +28,5 @@
 #include "cio.h"
 #include "versionos2.h"    /*PLF Wed  98-03-18 02:36:51*/
-
-#include <winprocess.h>
-#include <thread.h>
+#include <wprocess.h>
 
 BOOL      fExeStarted = FALSE;
@@ -43,5 +41,5 @@
 // Set up the TIB selector and memory for the current thread
 //******************************************************************************
-void InitializeTIB(BOOL fMainThread)
+TEB *InitializeTIB(BOOL fMainThread)
 {
 #ifdef WIN32_TIBSEL
@@ -56,5 +54,5 @@
 	dprintf(("InitializeTIB: local thread memory alloc failed!!"));
 	DebugInt3();
-	return;
+	return NULL;
    }
    if(OS2AllocSel(PAGE_SIZE, &tibsel) == FALSE)
@@ -62,5 +60,5 @@
 	dprintf(("InitializeTIB: selector alloc failed!!"));
 	DebugInt3();
-	return;
+	return NULL;
    }
    winteb = (TEB *)OS2SelToFlat(tibsel);
@@ -69,9 +67,9 @@
 	dprintf(("InitializeTIB: DosSelToFlat failed!!"));
 	DebugInt3();
-	return;
+	return NULL;
    }
    memset(winteb, 0, PAGE_SIZE);
    thdb       = (THDB *)(winteb+1);
-   TIBFlatPtr = (DWORD)winteb;
+   TIBFlatPtr = (DWORD *)winteb;
 
    winteb->except      = (PVOID)-1;               /* 00 Head of exception handling chain */
@@ -105,7 +103,8 @@
 	ProcessTIBSel = tibsel;
    }
-   SetFS(tibsel);
-   dprintf(("InitializeTIB set up TEB with selector %x", tibsel));
-   return;
+   dprintf(("InitializeTIB setup TEB with selector %x", tibsel));
+   return winteb;
+#else
+   return 0;
 #endif
 }
@@ -133,7 +132,47 @@
    	OS2FreeSel(thdb->teb_sel);
    }
-   else	DebugInt3();
-
+   else dprintf(("Already destroyed TIB"));
+
+   TIBFlatPtr = NULL;
    return;
+#endif
+}
+/******************************************************************************/
+/******************************************************************************/
+void WIN32API RestoreOS2TIB()
+{
+#ifdef WIN32_TIBSEL
+ SHORT  orgtibsel;
+ TEB   *winteb;
+ THDB  *thdb;
+
+   winteb = (TEB *)TIBFlatPtr;
+   if(winteb) {
+	thdb = (THDB *)(winteb+1);
+	orgtibsel = thdb->OrgTIBSel;
+
+	//Restore our original FS selector
+   	SetFS(orgtibsel);
+   }
+#endif
+}
+/******************************************************************************/
+/******************************************************************************/
+void WIN32API SetWin32TIB()
+{
+#ifdef WIN32_TIBSEL
+ SHORT  win32tibsel;
+ TEB   *winteb;
+ THDB  *thdb;
+
+   winteb = (TEB *)TIBFlatPtr;
+   if(winteb) {
+	thdb = (THDB *)(winteb+1);
+	win32tibsel = thdb->teb_sel;
+
+	//Restore our win32 FS selector
+   	SetFS(win32tibsel);
+   }
+   else DebugInt3();
 #endif
 }
@@ -175,7 +214,5 @@
                     HINSTANCE hinstance)
 {
-#ifdef WIN32_TIBSEL
-  SetFS(ProcessTIBSel);
-#endif
+  SetWin32TIB();
 
   if(getenv("WIN32_IOPL2")) {
@@ -217,6 +254,6 @@
 //******************************************************************************
 VOID WIN32API RegisterDll(LONG Win32TableId, LONG NameTableId,
-                  LONG VersionResId, LONG Pe2lxVersion,
-              HINSTANCE hinstance)
+                          LONG VersionResId, LONG Pe2lxVersion,
+                          HINSTANCE hinstance)
 {
  WIN32DLLENTRY  pfnDllEntry;
@@ -237,4 +274,7 @@
   /* @@@PH 1998/03/17 console devices initialization */
   iConsoleDevicesRegister();
+
+  SetWin32TIB();
+  dprintf(("RegisterDll: FS = %x", GetFS()));
 }
 //******************************************************************************
Index: trunk/src/kernel32/wprocess.h
===================================================================
--- trunk/src/kernel32/wprocess.h	(revision 125)
+++ 	(revision )
@@ -1,8 +1,0 @@
-/* $Id: wprocess.h,v 1.3 1999-06-10 19:11:31 phaller Exp $ */
-
-#ifndef __WPROCESS_H__
-#define __WPROCESS_H__
-
-extern BOOL      fExeStarted;
-
-#endif
