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

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

thread, SearchPath + handlemanager fixes

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