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

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

initterm.cpp

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