source: trunk/src/kernel32/initterm.cpp@ 4310

Last change on this file since 4310 was 4201, checked in by sandervl, 25 years ago

initdirectories call done earlier

File size: 7.5 KB
Line 
1/* $Id: initterm.cpp,v 1.46 2000-09-07 18:12:56 sandervl Exp $ */
2
3/*
4 * KERNEL32 DLL entry point
5 *
6 * Copyright 1998 Sander van Leeuwen
7 * Copyright 1998 Peter Fitzsimmons
8 *
9 *
10 * Project Odin Software License can be found in LICENSE.TXT
11 *
12 */
13
14/*-------------------------------------------------------------*/
15/* INITERM.C -- Source for a custom dynamic link library */
16/* initialization and termination (_DLL_InitTerm) */
17/* function. */
18/* */
19/* When called to perform initialization, this sample function */
20/* gets storage for an array of integers, and initializes its */
21/* elements with random integers. At termination time, it */
22/* frees the array. Substitute your own special processing. */
23/*-------------------------------------------------------------*/
24
25
26/* Include files */
27#define INCL_DOSMODULEMGR
28#define INCL_DOSMISC
29#define INCL_DOSPROCESS
30#define INCL_DOSSEMAPHORES
31#include <os2wrap.h> //Odin32 OS/2 api wrappers
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <misc.h>
36#include <wprocess.h>
37#include "handlemanager.h"
38#include "profile.h"
39#include "initterm.h"
40#include <win32type.h>
41#include <odinlx.h>
42#include "oslibmisc.h"
43#include <heapshared.h>
44#include <heapcode.h>
45#include "mmap.h"
46#include "directory.h"
47#include "hmdevio.h"
48#include <windllbase.h>
49#include "winexepe2lx.h"
50#include <exitlist.h>
51#include "oslibdos.h"
52#include <cpuhlp.h>
53#include <Win32k.h>
54
55#define DBG_LOCALLOG DBG_initterm
56#include "dbglocal.h"
57
58
59/*-------------------------------------------------------------------*/
60/* A clean up routine registered with DosExitList must be used if */
61/* runtime calls are required and the runtime is dynamically linked. */
62/* This will guarantee that this clean up routine is run before the */
63/* library DLL is terminated. */
64/*-------------------------------------------------------------------*/
65static void APIENTRY cleanup(ULONG reason);
66
67extern "C" {
68void CDECL _ctordtorInit( void );
69void CDECL _ctordtorTerm( void );
70
71 //Win32 resource table (produced by wrc)
72 extern DWORD _Resource_PEResTab;
73}
74
75//Global DLL Data
76#pragma data_seg(_GLOBALDATA)
77int globLoadNr = 0;
78#pragma data_seg()
79
80/* Tue 03.03.1998: knut */
81ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
82ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
83int loadNr = 0;
84char kernel32Path[CCHMAXPATH] = "";
85static HMODULE dllHandle = 0;
86
87/****************************************************************************/
88/* _DLL_InitTerm is the function that gets called by the operating system */
89/* loader when it loads and frees this DLL for each process that accesses */
90/* this DLL. However, it only gets called the first time the DLL is loaded */
91/* and the last time it is freed for a particular process. The system */
92/* linkage convention MUST be used because the operating system loader is */
93/* calling this function. */
94/****************************************************************************/
95unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long
96 ulFlag)
97{
98 size_t i;
99 APIRET rc;
100 ULONG ulSysinfo;
101
102 /*-------------------------------------------------------------------------*/
103 /* If ulFlag is zero then the DLL is being loaded so initialization should */
104 /* be performed. If ulFlag is 1 then the DLL is being freed so */
105 /* termination should be performed. */
106 /*-------------------------------------------------------------------------*/
107
108 switch (ulFlag)
109 {
110 case 0 :
111 {
112 libWin32kInit();
113
114 ParseLogStatus();
115
116 loadNr = globLoadNr++;
117
118 strcpy(kernel32Path, OSLibGetDllName(hModule));
119 char *endofpath = strrchr(kernel32Path, '\\');
120 *(endofpath+1) = 0;
121 dprintf(("kernel32 init\n"));
122 _ctordtorInit();
123
124 CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
125
126 OpenPrivateLogFiles();
127
128 if (InitializeSharedHeap() == FALSE)
129 return 0UL;
130
131 if (InitializeCodeHeap() == FALSE)
132 return 0UL;
133
134 PROFILE_LoadOdinIni();
135 dllHandle = RegisterLxDll(hModule, 0, (PVOID)&_Resource_PEResTab);
136 if (dllHandle == 0)
137 return 0UL;
138
139 //SvL: Kernel32 is a special case; pe.exe loads it, so increase
140 // the reference count here
141 Win32DllBase *module = Win32DllBase::findModule(dllHandle);
142 if (module)
143 {
144 module->AddRef();
145 module->DisableUnload();
146 }
147
148 /*******************************************************************/
149 /* A DosExitList routine must be used to clean up if runtime calls */
150 /* are required and the runtime is dynamically linked. */
151 /*******************************************************************/
152
153 rc = DosExitList(EXITLIST_KERNEL32|EXLST_ADD, cleanup);
154 if (rc)
155 return 0UL;
156
157 /* knut: check for high memory support */
158 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
159 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
160 {
161 flAllocMem = PAG_ANY; // high memory support. Let's use it!
162 ulMaxAddr = ulSysinfo * (1024*1024);
163 OSLibInitWSeBFileIO();
164 }
165 else
166 flAllocMem = 0; // no high memory support
167
168 OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
169
170 //SvL: Do it here instead of during the exe object creation
171 //(std handles can be used in win32 dll initialization routines
172 HMInitialize(); /* store standard handles within HandleManager */
173 InitDirectories(); //Must be done before InitializeTIB (which loads NTDLL -> USER32)
174 InitializeTIB(TRUE); //Must be done after HMInitialize!
175 RegisterDevices();
176 Win32DllBase::setDefaultRenaming();
177 rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo));
178 if (rc != 0)
179 ulSysinfo = 1;
180
181 InitSystemInfo(ulSysinfo);
182 //Set up environment as found in NT
183 InitEnvironment(ulSysinfo);
184 break;
185 }
186
187 case 1 :
188 if (dllHandle)
189 {
190 UnregisterLxDll(dllHandle);
191 }
192 break;
193
194 default :
195 return 0UL;
196 }
197
198 /***********************************************************/
199 /* A non-zero value must be returned to indicate success. */
200 /***********************************************************/
201 return 1UL;
202}
203
204
205static void APIENTRY cleanup(ULONG ulReason)
206{
207 dprintf(("kernel32 exit %d\n", ulReason));
208 //Flush and delete all open memory mapped files
209 Win32MemMap::deleteAll();
210 WinExe = NULL;
211
212 WriteOutProfiles();
213 DestroyTIB();
214 DestroySharedHeap();
215 DestroyCodeHeap();
216 _ctordtorTerm();
217
218 //NOTE: Must be done after DestroyTIB
219 ClosePrivateLogFiles();
220 CloseLogFile();
221
222 DosExitList(EXLST_EXIT, cleanup);
223 return ;
224}
Note: See TracBrowser for help on using the repository browser.