1 | /* $Id: initterm.cpp,v 1.67 2003-01-21 11:21:52 sandervl Exp $
|
---|
2 | *
|
---|
3 | * KERNEL32 DLL entry point
|
---|
4 | *
|
---|
5 | * Copyright 1998 Sander van Leeuwen
|
---|
6 | * Copyright 1998 Peter Fitzsimmons
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | /*-------------------------------------------------------------*/
|
---|
14 | /* INITERM.C -- Source for a custom dynamic link library */
|
---|
15 | /* initialization and termination (_DLL_InitTerm) */
|
---|
16 | /* function. */
|
---|
17 | /* */
|
---|
18 | /* When called to perform initialization, this sample function */
|
---|
19 | /* gets storage for an array of integers, and initializes its */
|
---|
20 | /* elements with random integers. At termination time, it */
|
---|
21 | /* frees the array. Substitute your own special processing. */
|
---|
22 | /*-------------------------------------------------------------*/
|
---|
23 |
|
---|
24 |
|
---|
25 | /* Include files */
|
---|
26 | #define INCL_DOSMODULEMGR
|
---|
27 | #define INCL_DOSMISC
|
---|
28 | #define INCL_DOSPROCESS
|
---|
29 | #define INCL_DOSSEMAPHORES
|
---|
30 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <string.h>
|
---|
34 | #include <misc.h>
|
---|
35 | #include <wprocess.h>
|
---|
36 | #include "handlemanager.h"
|
---|
37 | #include "profile.h"
|
---|
38 | #include <options.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 "winexepe2lx.h"
|
---|
50 | #include <exitlist.h>
|
---|
51 | #include "oslibdos.h"
|
---|
52 | #include <cpuhlp.h>
|
---|
53 | #include <Win32k.h>
|
---|
54 | #include <initdll.h>
|
---|
55 | #include <codepage.h>
|
---|
56 | #include <process.h>
|
---|
57 |
|
---|
58 | #define DBG_LOCALLOG DBG_initterm
|
---|
59 | #include "dbglocal.h"
|
---|
60 |
|
---|
61 | BOOL fVersionWarp3 = FALSE;
|
---|
62 | BOOL fCustomBuild = FALSE;
|
---|
63 |
|
---|
64 | //Global DLL Data
|
---|
65 | #pragma data_seg(_GLOBALDATA)
|
---|
66 | int globLoadNr = 0;
|
---|
67 | #pragma data_seg()
|
---|
68 |
|
---|
69 | /*-------------------------------------------------------------------*/
|
---|
70 | /* A clean up routine registered with DosExitList must be used if */
|
---|
71 | /* runtime calls are required and the runtime is dynamically linked. */
|
---|
72 | /* This will guarantee that this clean up routine is run before the */
|
---|
73 | /* library DLL is terminated. */
|
---|
74 | /*-------------------------------------------------------------------*/
|
---|
75 | static void APIENTRY cleanup(ULONG reason);
|
---|
76 |
|
---|
77 | /****************************************************************************/
|
---|
78 | /* _DLL_InitTerm is the function that gets called by the operating system */
|
---|
79 | /* loader when it loads and frees this DLL for each process that accesses */
|
---|
80 | /* this DLL. However, it only gets called the first time the DLL is loaded */
|
---|
81 | /* and the last time it is freed for a particular process. The system */
|
---|
82 | /* linkage convention MUST be used because the operating system loader is */
|
---|
83 | /* calling this function. */
|
---|
84 | /****************************************************************************/
|
---|
85 | ULONG DLLENTRYPOINT_CCONV DLLENTRYPOINT_NAME(ULONG hModule, ULONG ulFlag)
|
---|
86 | {
|
---|
87 | size_t i;
|
---|
88 | APIRET rc;
|
---|
89 | ULONG ulSysinfo, version[2];
|
---|
90 |
|
---|
91 | /*-------------------------------------------------------------------------*/
|
---|
92 | /* If ulFlag is zero then the DLL is being loaded so initialization should */
|
---|
93 | /* be performed. If ulFlag is 1 then the DLL is being freed so */
|
---|
94 | /* termination should be performed. */
|
---|
95 | /*-------------------------------------------------------------------------*/
|
---|
96 |
|
---|
97 | switch (ulFlag)
|
---|
98 | {
|
---|
99 | case 0:
|
---|
100 | {
|
---|
101 | if (fInit)
|
---|
102 | return 1; // already initialized
|
---|
103 |
|
---|
104 | ctordtorInit();
|
---|
105 |
|
---|
106 | /*******************************************************************/
|
---|
107 | /* A DosExitList routine must be used to clean up if runtime calls */
|
---|
108 | /* are required and the runtime is dynamically linked. */
|
---|
109 | /*******************************************************************/
|
---|
110 |
|
---|
111 | rc = DosExitList(EXITLIST_KERNEL32|EXLST_ADD, cleanup);
|
---|
112 | if (rc)
|
---|
113 | return 0;
|
---|
114 |
|
---|
115 | rc = DosQuerySysInfo(QSV_VERSION_MAJOR, QSV_VERSION_MINOR,
|
---|
116 | version, sizeof(version));
|
---|
117 | if (rc == 0)
|
---|
118 | if(version[0] >= 20 && version[1] <= 30)
|
---|
119 | fVersionWarp3 = TRUE;
|
---|
120 |
|
---|
121 | rc = inittermKernel32(hModule, ulFlag);
|
---|
122 | break;
|
---|
123 | }
|
---|
124 |
|
---|
125 | case 1:
|
---|
126 | rc = inittermKernel32(hModule, ulFlag);
|
---|
127 | break;
|
---|
128 |
|
---|
129 | default:
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /***********************************************************/
|
---|
134 | /* A non-zero value must be returned to indicate success. */
|
---|
135 | /***********************************************************/
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 | //******************************************************************************
|
---|
139 | //******************************************************************************
|
---|
140 | static void APIENTRY cleanup(ULONG ulReason)
|
---|
141 | {
|
---|
142 | cleanupKernel32(ulReason);
|
---|
143 |
|
---|
144 | ctordtorTerm();
|
---|
145 |
|
---|
146 | DosExitList(EXLST_EXIT, cleanup);
|
---|
147 | return ;
|
---|
148 | }
|
---|
149 | //******************************************************************************
|
---|
150 | ULONG APIENTRY _O32__DLL_InitTerm(ULONG handle, ULONG flag);
|
---|
151 | //******************************************************************************
|
---|
152 | ULONG APIENTRY InitializeKernel32()
|
---|
153 | {
|
---|
154 | HMODULE hModule;
|
---|
155 |
|
---|
156 | if (!fInit)
|
---|
157 | loadNr = globLoadNr++;
|
---|
158 |
|
---|
159 | BOOL WGSS_OK = FALSE;
|
---|
160 |
|
---|
161 | if (DosQueryModuleHandleStrict("WGSS50", &hModule) == NO_ERROR)
|
---|
162 | {
|
---|
163 | if (_O32__DLL_InitTerm(hModule, 0) != 0)
|
---|
164 | {
|
---|
165 | WGSS_OK = TRUE;
|
---|
166 |
|
---|
167 | if (DosQueryModuleHandleStrict("KERNEL32", &hModule) == NO_ERROR)
|
---|
168 | return DLLENTRYPOINT_NAME(hModule, 0);
|
---|
169 | else
|
---|
170 | ReportFatalDllInitError("KERNEL32");
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (!WGSS_OK)
|
---|
175 | ReportFatalDllInitError("WGSS50");
|
---|
176 |
|
---|
177 | return 0; // failure
|
---|
178 | }
|
---|
179 | //******************************************************************************
|
---|
180 | //******************************************************************************
|
---|
181 | VOID APIENTRY ReportFatalDllInitError(CHAR *pszModName)
|
---|
182 | {
|
---|
183 | static const char msg1[] =
|
---|
184 | "Failed to initialize the ";
|
---|
185 | static const char msg2[] =
|
---|
186 | " library while starting \"";
|
---|
187 | static const char msg3[] =
|
---|
188 | "\".\n\r"
|
---|
189 | "\n\r"
|
---|
190 | "It is possible that there is not enough memory in the system to "
|
---|
191 | "run this application. Please close other applications and try "
|
---|
192 | "again. If the problem persists, please report the details by "
|
---|
193 | "creating a ticket at http://svn.netlabs.org/odin32/.\n\r";
|
---|
194 |
|
---|
195 | char msg[sizeof(msg1) + 8 + sizeof(msg2) + CCHMAXPATH + sizeof(msg3)];
|
---|
196 |
|
---|
197 | strcpy(msg, msg1);
|
---|
198 | strncat(msg, pszModName, 8);
|
---|
199 | strcat(msg, msg2);
|
---|
200 |
|
---|
201 | PPIB ppib;
|
---|
202 | DosGetInfoBlocks(NULL, &ppib);
|
---|
203 | if (DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH,
|
---|
204 | msg + strlen(msg)) != NO_ERROR)
|
---|
205 | strcat(msg, "<unknown executable>");
|
---|
206 | strcat(msg, msg3);
|
---|
207 |
|
---|
208 | BOOL haveHMQ = FALSE;
|
---|
209 | MQINFO mqinfo;
|
---|
210 | if (WinQueryQueueInfo(1 /*HMQ_CURRENT*/, &mqinfo, sizeof(mqinfo)) == FALSE)
|
---|
211 | {
|
---|
212 | // attempt to initialize PM and try again
|
---|
213 | HAB hab = WinInitialize(0);
|
---|
214 | if (hab)
|
---|
215 | {
|
---|
216 | HMQ hmq = WinCreateMsgQueue(hab, 0);
|
---|
217 | if (hmq)
|
---|
218 | haveHMQ = TRUE;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | else
|
---|
222 | haveHMQ = TRUE;
|
---|
223 |
|
---|
224 | WinMessageBox(HWND_DESKTOP, NULL, msg, "Odin: Fatal Error", 0,
|
---|
225 | MB_APPLMODAL | MB_MOVEABLE | MB_ERROR | MB_OK);
|
---|
226 |
|
---|
227 | // duplicate the message to the console just in case (PM may be not
|
---|
228 | // available)
|
---|
229 | ULONG dummy;
|
---|
230 | DosWrite((HFILE)1, (PVOID)&msg, strlen(msg), &dummy);
|
---|
231 | }
|
---|
232 |
|
---|
233 | //******************************************************************************
|
---|
234 | //******************************************************************************
|
---|
235 |
|
---|