| 1 | /* $Id: inituser32.cpp,v 1.16 2003-10-22 12:43:13 sandervl Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | * USER32 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_DOSPROCESS
|
|---|
| 28 | #define INCL_DOSSEMAPHORES
|
|---|
| 29 | #define INCL_DOSMISC
|
|---|
| 30 | #define INCL_DOSERRORS
|
|---|
| 31 | #define INCL_WINSHELLDATA
|
|---|
| 32 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 | #include <string.h>
|
|---|
| 36 | #include <odin.h>
|
|---|
| 37 | #include <misc.h> /*PLF Wed 98-03-18 23:18:29*/
|
|---|
| 38 | #include <win32type.h>
|
|---|
| 39 | #include <win32api.h>
|
|---|
| 40 | #include <winconst.h>
|
|---|
| 41 | #include <odinlx.h>
|
|---|
| 42 | #include <spy.h>
|
|---|
| 43 | #include <monitor.h>
|
|---|
| 44 | #include <kbdhook.h>
|
|---|
| 45 | #include "pmwindow.h"
|
|---|
| 46 | #include "win32wdesktop.h"
|
|---|
| 47 | #include "win32wndhandle.h"
|
|---|
| 48 | #include "syscolor.h"
|
|---|
| 49 | #include "initterm.h"
|
|---|
| 50 | #include <exitlist.h>
|
|---|
| 51 | #include <initdll.h>
|
|---|
| 52 | #include <stats.h>
|
|---|
| 53 |
|
|---|
| 54 | #define DBG_LOCALLOG DBG_initterm
|
|---|
| 55 | #include "dbglocal.h"
|
|---|
| 56 |
|
|---|
| 57 | /*-------------------------------------------------------------------*/
|
|---|
| 58 | /* A clean up routine registered with DosExitList must be used if */
|
|---|
| 59 | /* runtime calls are required and the runtime is dynamically linked. */
|
|---|
| 60 | /* This will guarantee that this clean up routine is run before the */
|
|---|
| 61 | /* library DLL is terminated. */
|
|---|
| 62 | /*-------------------------------------------------------------------*/
|
|---|
| 63 | static void APIENTRY cleanup(ULONG reason);
|
|---|
| 64 |
|
|---|
| 65 | extern "C" {
|
|---|
| 66 | //Win32 resource table (produced by wrc)
|
|---|
| 67 | extern DWORD user32_PEResTab;
|
|---|
| 68 | }
|
|---|
| 69 | DWORD hInstanceUser32 = 0;
|
|---|
| 70 |
|
|---|
| 71 | extern INT __cdecl wsnprintfA(LPSTR,UINT,LPCSTR,...);
|
|---|
| 72 |
|
|---|
| 73 | static char PMKBDHK_MODULE[16] = STD_PMKBDHK_MODULE;
|
|---|
| 74 |
|
|---|
| 75 | static BOOL pmkbdhk_installed = FALSE;
|
|---|
| 76 | static HMODULE hmodPMKBDHK;
|
|---|
| 77 |
|
|---|
| 78 | static PFN_HOOKINIT pfnHookInit = NULL;
|
|---|
| 79 | static PFN_HOOKTERM pfnHookTerm = NULL;
|
|---|
| 80 |
|
|---|
| 81 | // defined initialized in pmwindow.cpp: InitPM()
|
|---|
| 82 | extern HAB hab;
|
|---|
| 83 |
|
|---|
| 84 | //******************************************************************************
|
|---|
| 85 | //******************************************************************************
|
|---|
| 86 | void WIN32API SetCustomPMHookDll(LPSTR pszKbdDllName)
|
|---|
| 87 | {
|
|---|
| 88 | strcpy(PMKBDHK_MODULE, pszKbdDllName);
|
|---|
| 89 | }
|
|---|
| 90 | //******************************************************************************
|
|---|
| 91 | //******************************************************************************
|
|---|
| 92 | void pmkbdhk_initialize(HAB _hab)
|
|---|
| 93 | {
|
|---|
| 94 | APIRET rc;
|
|---|
| 95 |
|
|---|
| 96 | if ((pmkbdhk_installed == FALSE) && PMKBDHK_MODULE[0])
|
|---|
| 97 | {
|
|---|
| 98 | CHAR szBuf[260];
|
|---|
| 99 |
|
|---|
| 100 | // load the DLL
|
|---|
| 101 | rc = DosLoadModule(szBuf,
|
|---|
| 102 | sizeof(szBuf),
|
|---|
| 103 | PMKBDHK_MODULE,
|
|---|
| 104 | &hmodPMKBDHK);
|
|---|
| 105 | if (NO_ERROR != rc)
|
|---|
| 106 | {
|
|---|
| 107 | dprintf(("USER32: pmkbdhk_initalize(%08xh) failed rc=%d\n",
|
|---|
| 108 | _hab,
|
|---|
| 109 | rc));
|
|---|
| 110 |
|
|---|
| 111 | return;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | // get the entry points
|
|---|
| 115 | rc = DosQueryProcAddr(hmodPMKBDHK,
|
|---|
| 116 | 0,
|
|---|
| 117 | PMKBDHK_HOOK_INIT,
|
|---|
| 118 | (PFN*)&pfnHookInit);
|
|---|
| 119 | if (NO_ERROR == rc)
|
|---|
| 120 | rc = DosQueryProcAddr(hmodPMKBDHK,
|
|---|
| 121 | 0,
|
|---|
| 122 | PMKBDHK_HOOK_TERM,
|
|---|
| 123 | (PFN*)&pfnHookTerm);
|
|---|
| 124 |
|
|---|
| 125 | if (NO_ERROR != rc)
|
|---|
| 126 | {
|
|---|
| 127 | dprintf(("USER32: pmkbdhk_initalize(%08xh) failed importing functions, rc=%d\n",
|
|---|
| 128 | _hab,
|
|---|
| 129 | rc));
|
|---|
| 130 |
|
|---|
| 131 | // free the DLL again
|
|---|
| 132 | DosFreeModule(hmodPMKBDHK);
|
|---|
| 133 | hmodPMKBDHK = NULLHANDLE;
|
|---|
| 134 |
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | // now finally call the initializer function
|
|---|
| 139 | if(pfnHookInit(_hab, WIN32_STDCLASS) == FALSE) DebugInt3();
|
|---|
| 140 |
|
|---|
| 141 | // OK, hook is armed
|
|---|
| 142 | pmkbdhk_installed = TRUE;
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | //******************************************************************************
|
|---|
| 146 | //******************************************************************************
|
|---|
| 147 | void pmkbdhk_terminate(void)
|
|---|
| 148 | {
|
|---|
| 149 | if (pmkbdhk_installed == TRUE)
|
|---|
| 150 | {
|
|---|
| 151 | // call the terminator function
|
|---|
| 152 | pfnHookTerm();
|
|---|
| 153 |
|
|---|
| 154 | // OK, hook is disarmed
|
|---|
| 155 | pmkbdhk_installed = FALSE;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | // unload the dll
|
|---|
| 159 | if (NULLHANDLE != hmodPMKBDHK)
|
|---|
| 160 | {
|
|---|
| 161 | APIRET rc = DosFreeModule(hmodPMKBDHK);
|
|---|
| 162 | if (NO_ERROR != rc)
|
|---|
| 163 | {
|
|---|
| 164 | dprintf(("USER32: pmkbdhk_terminate() failed rc=%d\n",
|
|---|
| 165 | rc));
|
|---|
| 166 |
|
|---|
| 167 | hmodPMKBDHK = NULLHANDLE;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 | //******************************************************************************
|
|---|
| 172 | #define FONTSDIRECTORY "Fonts"
|
|---|
| 173 | #define REGPATH "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
|
|---|
| 174 | //******************************************************************************
|
|---|
| 175 | void MigrateWindowsFonts()
|
|---|
| 176 | {
|
|---|
| 177 | HKEY hkFonts,hkOS2Fonts;
|
|---|
| 178 | char buffer[512];
|
|---|
| 179 | UINT len = GetWindowsDirectoryA( NULL, 0 );
|
|---|
| 180 |
|
|---|
| 181 | if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, REGPATH ,0, KEY_ALL_ACCESS, &hkFonts) == 0)
|
|---|
| 182 | {
|
|---|
| 183 | DWORD dwIndex, dwType;
|
|---|
| 184 | char subKeyName[255], dataArray[512];
|
|---|
| 185 | DWORD sizeOfSubKeyName = 254, sizeOfDataArray = 511;
|
|---|
| 186 |
|
|---|
| 187 | // loop over all values of the current key
|
|---|
| 188 | for (dwIndex=0;
|
|---|
| 189 | RegEnumValueA(hkFonts, dwIndex, subKeyName, &sizeOfSubKeyName, NULL, &dwType ,(LPBYTE)dataArray, &sizeOfDataArray) != ERROR_NO_MORE_ITEMS_W;
|
|---|
| 190 | ++dwIndex, sizeOfSubKeyName = 254, sizeOfDataArray = 511)
|
|---|
| 191 | {
|
|---|
| 192 | //Check OS/2 INI profile for font entry
|
|---|
| 193 | if (!PrfQueryProfileString(HINI_PROFILE, "PM_Fonts", dataArray,
|
|---|
| 194 | NULL, (PVOID)subKeyName, (LONG)sizeof(subKeyName)))
|
|---|
| 195 | {
|
|---|
| 196 | HDIR hdirFindHandle = HDIR_CREATE;
|
|---|
| 197 | FILEFINDBUF3 FindBuffer = {0};
|
|---|
| 198 | ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
|
|---|
| 199 | ULONG ulFindCount = 1;
|
|---|
| 200 | APIRET rc = NO_ERROR;
|
|---|
| 201 |
|
|---|
| 202 | dprintf(("Migrating font %s to OS/2",dataArray));
|
|---|
| 203 |
|
|---|
| 204 | GetWindowsDirectoryA( buffer, len + 1 );
|
|---|
| 205 | wsnprintfA( buffer, sizeof(buffer), "%s\\%s\\%s", buffer, FONTSDIRECTORY, dataArray );
|
|---|
| 206 |
|
|---|
| 207 | rc = DosFindFirst( buffer, &hdirFindHandle, FILE_NORMAL,&FindBuffer, ulResultBufLen, &ulFindCount, FIL_STANDARD);
|
|---|
| 208 |
|
|---|
| 209 | //Check that file actaully exist
|
|---|
| 210 | if ( rc == NO_ERROR && !(FindBuffer.attrFile & FILE_DIRECTORY))
|
|---|
| 211 | {
|
|---|
| 212 | PrfWriteProfileString(HINI_PROFILE,"PM_Fonts",dataArray, buffer);
|
|---|
| 213 | DosFindClose(hdirFindHandle);
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | RegCloseKey(hkFonts);
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 | /****************************************************************************/
|
|---|
| 221 | /* _DLL_InitTerm is the function that gets called by the operating system */
|
|---|
| 222 | /* loader when it loads and frees this DLL for each process that accesses */
|
|---|
| 223 | /* this DLL. However, it only gets called the first time the DLL is loaded */
|
|---|
| 224 | /* and the last time it is freed for a particular process. The system */
|
|---|
| 225 | /* linkage convention MUST be used because the operating system loader is */
|
|---|
| 226 | /* calling this function. */
|
|---|
| 227 | /****************************************************************************/
|
|---|
| 228 | ULONG APIENTRY inittermUser32(ULONG hModule, ULONG ulFlag)
|
|---|
| 229 | {
|
|---|
| 230 | size_t i;
|
|---|
| 231 | APIRET rc;
|
|---|
| 232 | ULONG version[2];
|
|---|
| 233 |
|
|---|
| 234 | /*-------------------------------------------------------------------------*/
|
|---|
| 235 | /* If ulFlag is zero then the DLL is being loaded so initialization should */
|
|---|
| 236 | /* be performed. If ulFlag is 1 then the DLL is being freed so */
|
|---|
| 237 | /* termination should be performed. */
|
|---|
| 238 | /*-------------------------------------------------------------------------*/
|
|---|
| 239 |
|
|---|
| 240 | switch (ulFlag) {
|
|---|
| 241 | case 0 :
|
|---|
| 242 | ParseLogStatusUSER32();
|
|---|
| 243 |
|
|---|
| 244 | InitializeKernel32();
|
|---|
| 245 | CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
|
|---|
| 246 |
|
|---|
| 247 | hInstanceUser32 = RegisterLxDll(hModule, 0, (PVOID)&user32_PEResTab,
|
|---|
| 248 | USER32_MAJORIMAGE_VERSION, USER32_MINORIMAGE_VERSION,
|
|---|
| 249 | IMAGE_SUBSYSTEM_WINDOWS_GUI);
|
|---|
| 250 | if(hInstanceUser32 == 0)
|
|---|
| 251 | return 0UL;
|
|---|
| 252 |
|
|---|
| 253 | dprintf(("user32 init %s %s (%x)", __DATE__, __TIME__, inittermUser32));
|
|---|
| 254 |
|
|---|
| 255 | //SvL: Try to start communication with our message spy queue server
|
|---|
| 256 | InitSpyQueue();
|
|---|
| 257 |
|
|---|
| 258 | //SvL: Init win32 PM classes
|
|---|
| 259 | //PH: initializes HAB!
|
|---|
| 260 | if (FALSE == InitPM())
|
|---|
| 261 | return 0UL;
|
|---|
| 262 |
|
|---|
| 263 | // try to install the keyboard hook
|
|---|
| 264 | pmkbdhk_initialize(hab);
|
|---|
| 265 |
|
|---|
| 266 | InitializeWindowHandles();
|
|---|
| 267 |
|
|---|
| 268 | //SvL: 18-7-'98, Register system window classes (button, listbox etc etc)
|
|---|
| 269 | //CB: register internal classes
|
|---|
| 270 | RegisterSystemClasses(hModule);
|
|---|
| 271 |
|
|---|
| 272 | //CB: initialize PM monitor driver
|
|---|
| 273 | MONITOR_Initialize(&MONITOR_PrimaryMonitor);
|
|---|
| 274 |
|
|---|
| 275 | //PF: migrate windows fonts
|
|---|
| 276 | MigrateWindowsFonts();
|
|---|
| 277 |
|
|---|
| 278 | break;
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 | case 1 :
|
|---|
| 282 | if(hInstanceUser32) {
|
|---|
| 283 | UnregisterLxDll(hInstanceUser32);
|
|---|
| 284 | }
|
|---|
| 285 | break;
|
|---|
| 286 | default :
|
|---|
| 287 | return 0UL;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | /***********************************************************/
|
|---|
| 291 | /* A non-zero value must be returned to indicate success. */
|
|---|
| 292 | /***********************************************************/
|
|---|
| 293 | return 1UL;
|
|---|
| 294 | }
|
|---|
| 295 | //******************************************************************************
|
|---|
| 296 | //******************************************************************************
|
|---|
| 297 | void APIENTRY cleanupUser32(ULONG ulReason)
|
|---|
| 298 | {
|
|---|
| 299 | dprintf(("user32 exit\n"));
|
|---|
| 300 |
|
|---|
| 301 | // try to unistall the keyboard hook
|
|---|
| 302 | pmkbdhk_terminate();
|
|---|
| 303 |
|
|---|
| 304 | //SvL: Causes PM hangs on some (a lot?) machines. Reason unknown.
|
|---|
| 305 | //// RestoreCursor();
|
|---|
| 306 |
|
|---|
| 307 | //Destroy CD notification window
|
|---|
| 308 | WinDestroyWindow(hwndCD);
|
|---|
| 309 | DestroyDesktopWindow();
|
|---|
| 310 | Win32BaseWindow::DestroyAll();
|
|---|
| 311 | UnregisterSystemClasses();
|
|---|
| 312 | Win32WndClass::DestroyAll();
|
|---|
| 313 | MONITOR_Finalize(&MONITOR_PrimaryMonitor);
|
|---|
| 314 | SYSCOLOR_Save();
|
|---|
| 315 | CloseSpyQueue();
|
|---|
| 316 | FinalizeWindowHandles();
|
|---|
| 317 | STATS_DumpStatsUSER32();
|
|---|
| 318 | dprintf(("user32 exit done\n"));
|
|---|
| 319 | }
|
|---|
| 320 | //******************************************************************************
|
|---|
| 321 | //******************************************************************************
|
|---|