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

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

Dll dependency changes

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