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

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

initterm call fixes

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