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

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

moved registry apis into kernel32 + cleanup

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