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

Last change on this file since 2513 was 2513, checked in by sandervl, 26 years ago

Allocate low memory at right address and with right size in peldr.dll (fixes install of Windows Media Player)

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