Improved Winelib apps initialisation code. No longer need to link
winestub.o with Winelib apps.

diff --git a/loader/main.c b/loader/main.c
index 48dca56..4ad2956 100644
--- a/loader/main.c
+++ b/loader/main.c
@@ -55,7 +55,7 @@
 #include "server.h"
 #include "loadorder.h"
 
-DEFAULT_DEBUG_CHANNEL(server)
+DEFAULT_DEBUG_CHANNEL(server);
 
 /***********************************************************************
  *           Main initialisation routine
@@ -92,20 +92,7 @@
     /* Initialize module loadorder */
     if (!MODULE_InitLoadOrder()) return FALSE;
 
-    /* Initialize DOS memory */
-    if (!DOSMEM_Init(0)) return FALSE;
-
-    /* Initialize communications */
-    COMM_Init();
-
-    /* Initialize IO-port permissions */
-    IO_port_init();
-
-    /* Read DOS config.sys */
-    if (!DOSCONF_ReadConfig()) return FALSE;
-
     /* Initialize KERNEL */
-    if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
     if (!LoadLibraryA( "KERNEL32" )) return FALSE;
 
     if (!LoadLibraryA( "x11drv" )) return FALSE;
@@ -127,6 +114,11 @@
     if ( initDone ) return TRUE;
     initDone = TRUE;
 
+    /* Initialize DOS memory */
+    if (!DOSMEM_Init(0)) return FALSE;
+
+    if (!LoadLibrary16( "KRNL386.EXE" )) return FALSE;
+
     /* Initialize special KERNEL entry points */
     hModule = GetModuleHandle16( "KERNEL" );
     if ( hModule )
@@ -164,55 +156,20 @@
     /* Initialize relay code */
     if (!RELAY_Init()) return FALSE;
 
+    /* Initialize communications */
+    COMM_Init();
+
+    /* Initialize IO-port permissions */
+    IO_port_init();
+
+    /* Read DOS config.sys */
+    if (!DOSCONF_ReadConfig()) return FALSE;
+
     return TRUE;
 }
 
 
 /***********************************************************************
- *           Winelib initialisation routine
- */
-HINSTANCE MAIN_WinelibInit( int *argc, char *argv[] )
-{
-    NE_MODULE *pModule;
-    HMODULE16 hModule;
-    PDB	      *curr;
-
-    /* Main initialization */
-    if (!MAIN_MainInit( *argc, argv, TRUE )) return 0;
-    *argc = Options.argc;
-
-    /* Load WineLib EXE module */
-    if ( (hModule = BUILTIN32_LoadExeModule()) < 32 ) return 0;
-    pModule = (NE_MODULE *)GlobalLock16( hModule );
-
-    /* Create initial task */
-    if (!TASK_Create( pModule, FALSE )) return 0;
-
-    /* Create 32-bit MODREF */
-    if ( !PE_CreateModule( pModule->module32, NE_MODULE_NAME(pModule), 0, FALSE ) )
-        return 0;
-
-    /* Increment EXE refcount */
-    curr = PROCESS_Current();
-    assert( curr->exe_modref );
-    curr->exe_modref->refCount++;
-
-    /* Load system DLLs into the initial process (and initialize them) */
-    if (   !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
-        || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
-        ExitProcess( 1 );
-
-    /* attach the imported DLLs */
-    if ( !MODULE_DllProcessAttach( curr->exe_modref, NULL ) )
-       ExitProcess( 1 );
-
-    /* Get pointers to USER routines called by KERNEL */
-    THUNK_InitCallout();
-
-    return pModule->module32;
-}
-
-/***********************************************************************
  *           ExitKernel16 (KERNEL.2)
  *
  * Clean-up everything and exit the Wine process.
diff --git a/loader/module.c b/loader/module.c
index 7bb5667..10189e3 100644
--- a/loader/module.c
+++ b/loader/module.c
@@ -336,7 +336,7 @@
  *
  * Create a dummy NE module for Win32 or Winelib.
  */
-HMODULE MODULE_CreateDummyModule( LPCSTR filename, WORD version )
+HMODULE MODULE_CreateDummyModule( LPCSTR filename, HMODULE module32 )
 {
     HMODULE hModule;
     NE_MODULE *pModule;
@@ -388,8 +388,19 @@
     pModule->nrname_size      = 0;
     pModule->fileinfo         = sizeof(NE_MODULE);
     pModule->os_flags         = NE_OSFLAGS_WINDOWS;
-    pModule->expected_version = version;
     pModule->self             = hModule;
+    pModule->module32         = module32;
+
+    /* Set version and flags */
+    if (module32)
+    {
+        pModule->expected_version =
+            ((PE_HEADER(module32)->OptionalHeader.MajorSubsystemVersion & 0xff) << 8 ) |
+             (PE_HEADER(module32)->OptionalHeader.MinorSubsystemVersion & 0xff);
+        pModule->flags |= NE_FFLAGS_WIN32;
+        if (PE_HEADER(module32)->FileHeader.Characteristics & IMAGE_FILE_DLL)
+            pModule->flags |= NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA;
+    }
 
     /* Set loaded file information */
     ofs = (OFSTRUCT *)(pModule + 1);
diff --git a/loader/pe_image.c b/loader/pe_image.c
index 27c5e0d..2ce45c4 100644
--- a/loader/pe_image.c
+++ b/loader/pe_image.c
@@ -892,7 +892,6 @@
         struct load_dll_request *req = get_req_buffer();
 	HMODULE		hModule32;
 	HMODULE16	hModule16;
-	NE_MODULE	*pModule;
 	WINE_MODREF	*wm;
 	char        	filename[256];
 	HANDLE		hFile;
@@ -916,15 +915,12 @@
 	}
 
 	/* Create 16-bit dummy module */
-	if ((hModule16 = MODULE_CreateDummyModule( filename, version )) < 32)
+	if ((hModule16 = MODULE_CreateDummyModule( filename, hModule32 )) < 32)
 	{
                 CloseHandle( hFile );
 		SetLastError( (DWORD)hModule16 );	/* This should give the correct error */
 		return NULL;
 	}
-	pModule = (NE_MODULE *)GlobalLock16( hModule16 );
-	pModule->flags    = NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA | NE_FFLAGS_WIN32;
-	pModule->module32 = hModule32;
 
 	/* Create 32-bit MODREF */
 	if ( !(wm = PE_CreateModule( hModule32, filename, flags, FALSE )) )
@@ -994,14 +990,12 @@
 #endif
 
     /* Create 16-bit dummy module */
-    if ( (hModule16 = MODULE_CreateDummyModule( filename, version )) < 32 ) 
+    if ( (hModule16 = MODULE_CreateDummyModule( filename, hModule32 )) < 32 ) 
     {
         SetLastError( hModule16 );
         return FALSE;
     }
     pModule = (NE_MODULE *)GlobalLock16( hModule16 );
-    pModule->flags    = NE_FFLAGS_WIN32;
-    pModule->module32 = hModule32;
 
     /* Create new process */
     if ( !PROCESS_Create( pModule, hFile, cmd_line, env,