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

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

Rewrote dll entrypoint

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