Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

inituser32.cpp

Go to the documentation of this file.
00001 /* $Id: inituser32.cpp,v 1.10 2001/12/16 15:30:14 sandervl Exp $ */
00002 /*
00003  * USER32 DLL entry point
00004  *
00005  * Copyright 1998 Sander van Leeuwen
00006  * Copyright 1998 Peter Fitzsimmons
00007  *
00008  *
00009  * Project Odin Software License can be found in LICENSE.TXT
00010  *
00011  */
00012 
00013 /*-------------------------------------------------------------*/
00014 /* INITERM.C -- Source for a custom dynamic link library       */
00015 /*              initialization and termination (_DLL_InitTerm) */
00016 /*              function.                                      */
00017 /*                                                             */
00018 /* When called to perform initialization, this sample function */
00019 /* gets storage for an array of integers, and initializes its  */
00020 /* elements with random integers.  At termination time, it     */
00021 /* frees the array.  Substitute your own special processing.   */
00022 /*-------------------------------------------------------------*/
00023 
00024 
00025 /* Include files */
00026 #define  INCL_DOSMODULEMGR
00027 #define  INCL_DOSPROCESS
00028 #define  INCL_DOSSEMAPHORES
00029 #define  INCL_DOSMISC
00030 #define  INCL_DOSERRORS
00031 #include <os2wrap.h>    //Odin32 OS/2 api wrappers
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <odin.h>
00036 #include <misc.h>       /*PLF Wed  98-03-18 23:18:29*/
00037 #include <win32type.h>
00038 #include <win32api.h>
00039 #include <winconst.h>
00040 #include <odinlx.h>
00041 #include <spy.h>
00042 #include <monitor.h>
00043 #include "pmwindow.h"
00044 #include "win32wdesktop.h"
00045 #include "syscolor.h"
00046 #include "initterm.h"
00047 #include <exitlist.h>
00048 #include <initdll.h>
00049 #include <stats.h>
00050 
00051 #define DBG_LOCALLOG    DBG_initterm
00052 #include "dbglocal.h"
00053 
00054 /*-------------------------------------------------------------------*/
00055 /* A clean up routine registered with DosExitList must be used if    */
00056 /* runtime calls are required and the runtime is dynamically linked. */
00057 /* This will guarantee that this clean up routine is run before the  */
00058 /* library DLL is terminated.                                        */
00059 /*-------------------------------------------------------------------*/
00060 static void APIENTRY cleanup(ULONG reason);
00061 
00062 extern "C" {
00063  //Win32 resource table (produced by wrc)
00064  extern DWORD user32_PEResTab;
00065 }
00066 DWORD hInstanceUser32 = 0;
00067 
00068 
00069 /**************************************************************/
00070 /* Try to load the Presentation Manager Keyboard Hook module. */
00071 /* If this fails, some hotkeys may not arrive properly at the */
00072 /* targetted window, but no more harmful things will happen.  */
00073 /**************************************************************/
00074 static char PMKBDHK_MODULE[16] = "PMKBDHK";
00075 #define PMKBDHK_HOOK_INIT "hookInit"
00076 #define PMKBDHK_HOOK_TERM "hookKill"
00077 
00078 static BOOL pmkbdhk_installed = FALSE;
00079 static HMODULE hmodPMKBDHK;
00080 
00081 static PVOID (*APIENTRY pfnHookInit)(HAB);
00082 static BOOL  (*APIENTRY pfnHookTerm)(void);
00083 
00084 // defined initialized in pmwindow.cpp: InitPM()
00085 extern HAB hab;
00086 
00087 //******************************************************************************
00088 //******************************************************************************
00089 void WIN32API SetCustomPMHookDll(LPSTR pszKbdDllName)
00090 {
00091    strcpy(PMKBDHK_MODULE, pszKbdDllName);
00092 }
00093 //******************************************************************************
00094 //******************************************************************************
00095 void pmkbdhk_initialize(HAB _hab)
00096 {
00097   APIRET rc;
00098   
00099   if (pmkbdhk_installed == FALSE)
00100   {
00101     CHAR szBuf[260];
00102     
00103     // load the DLL
00104     rc = DosLoadModule(szBuf,
00105                        sizeof(szBuf),
00106                        PMKBDHK_MODULE,
00107                        &hmodPMKBDHK);
00108     if (NO_ERROR != rc)
00109     {
00110       dprintf(("USER32: pmkbdhk_initalize(%08xh) failed rc=%d\n",
00111                _hab,
00112                rc));
00113       
00114       return;
00115     }
00116     
00117     // get the entry points
00118     rc = DosQueryProcAddr(hmodPMKBDHK,
00119                           0,
00120                           PMKBDHK_HOOK_INIT,
00121                           (PFN*)&pfnHookInit);
00122     if (NO_ERROR == rc)
00123       rc = DosQueryProcAddr(hmodPMKBDHK,
00124                             0,
00125                             PMKBDHK_HOOK_TERM,
00126                             (PFN*)&pfnHookTerm);
00127     
00128     if (NO_ERROR != rc)
00129     {
00130       dprintf(("USER32: pmkbdhk_initalize(%08xh) failed importing functions, rc=%d\n",
00131                _hab,
00132                rc));
00133       
00134       // free the DLL again
00135       DosFreeModule(hmodPMKBDHK);
00136       hmodPMKBDHK = NULLHANDLE;
00137       
00138       return;
00139     }
00140     
00141     // now finally call the initializer function
00142     pfnHookInit(_hab);
00143     
00144     // OK, hook is armed
00145     pmkbdhk_installed = TRUE;
00146   }
00147 }
00148 //******************************************************************************
00149 //******************************************************************************
00150 void pmkbdhk_terminate(void)
00151 {
00152   if (pmkbdhk_installed == TRUE)
00153   {
00154     // call the terminator function
00155     pfnHookTerm();
00156     
00157     // OK, hook is disarmed
00158     pmkbdhk_installed = TRUE;
00159   }
00160   
00161   // unload the dll
00162   if (NULLHANDLE != hmodPMKBDHK)
00163   {
00164     APIRET rc = DosFreeModule(hmodPMKBDHK);
00165     if (NO_ERROR != rc)
00166     {
00167       dprintf(("USER32: pmkbdhk_terminate() failed rc=%d\n",
00168                rc));
00169     
00170       hmodPMKBDHK = NULLHANDLE;
00171     }
00172   }
00173 }
00174 /****************************************************************************/
00175 /* _DLL_InitTerm is the function that gets called by the operating system   */
00176 /* loader when it loads and frees this DLL for each process that accesses   */
00177 /* this DLL.  However, it only gets called the first time the DLL is loaded */
00178 /* and the last time it is freed for a particular process.  The system      */
00179 /* linkage convention MUST be used because the operating system loader is   */
00180 /* calling this function.                                                   */
00181 /****************************************************************************/
00182 ULONG APIENTRY inittermUser32(ULONG hModule, ULONG ulFlag)
00183 {
00184    size_t i;
00185    APIRET rc;
00186    ULONG  version[2];
00187 
00188    /*-------------------------------------------------------------------------*/
00189    /* If ulFlag is zero then the DLL is being loaded so initialization should */
00190    /* be performed.  If ulFlag is 1 then the DLL is being freed so            */
00191    /* termination should be performed.                                        */
00192    /*-------------------------------------------------------------------------*/
00193 
00194    switch (ulFlag) {
00195       case 0 :
00196          ParseLogStatusUSER32();
00197 
00198          InitializeKernel32();
00199          CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed  98-03-18 05:28:48*/
00200 
00201          hInstanceUser32 = RegisterLxDll(hModule, 0, (PVOID)&user32_PEResTab,
00202                                          USER32_MAJORIMAGE_VERSION, USER32_MINORIMAGE_VERSION,
00203                                          IMAGE_SUBSYSTEM_WINDOWS_GUI);
00204          if(hInstanceUser32 == 0)
00205                 return 0UL;
00206 
00207          dprintf(("user32 init %s %s (%x)", __DATE__, __TIME__, inittermUser32));
00208 
00209          //SvL: Try to start communication with our message spy queue server
00210          InitSpyQueue();
00211 
00212          //SvL: Init win32 PM classes
00213          //PH:  initializes HAB!
00214          if (FALSE == InitPM())
00215            return 0UL;
00216      
00217          // try to install the keyboard hook
00218          pmkbdhk_initialize(hab);
00219 
00220          //SvL: 18-7-'98, Register system window classes (button, listbox etc etc)
00221          //CB: register internal classes
00222          RegisterSystemClasses(hModule);
00223 
00224          //CB: initialize PM monitor driver
00225          MONITOR_Initialize(&MONITOR_PrimaryMonitor);
00226 
00227        break;
00228      
00229      
00230        case 1 :
00231          if(hInstanceUser32) {
00232             UnregisterLxDll(hInstanceUser32);
00233          }
00234          break;
00235       default  :
00236          return 0UL;
00237    }
00238 
00239    /***********************************************************/
00240    /* A non-zero value must be returned to indicate success.  */
00241    /***********************************************************/
00242    return 1UL;
00243 }
00244 //******************************************************************************
00245 //******************************************************************************
00246 void APIENTRY cleanupUser32(ULONG ulReason)
00247 {
00248   dprintf(("user32 exit\n"));
00249   
00250   // try to unistall the keyboard hook
00251   pmkbdhk_terminate();
00252 
00253 //SvL: Causes PM hangs on some (a lot?) machines. Reason unknown.
00254 ////   RestoreCursor();
00255 
00256    //Destroy CD notification window
00257    WinDestroyWindow(hwndCD);
00258    DestroyDesktopWindow();
00259    Win32BaseWindow::DestroyAll();
00260    UnregisterSystemClasses();
00261    Win32WndClass::DestroyAll();
00262    MONITOR_Finalize(&MONITOR_PrimaryMonitor);
00263    SYSCOLOR_Save();
00264    CloseSpyQueue();
00265    STATS_DumpStatsUSER32();
00266    dprintf(("user32 exit done\n"));
00267 }
00268 //******************************************************************************
00269 //******************************************************************************

Generated on Wed Jan 23 23:17:32 2002 for ODIN-user32 by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001