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

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

device driver handlemanager class added

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