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

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

registry api fixes + version keys added

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