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

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

Rewrite of PE loader code, EB's fixes + VirtualProtect bugfix

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