| 1 | /* $Id: windllpe2lx.cpp,v 1.11 2001-07-10 20:18:12 bird Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 PE2LX Dll class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * Copyright 1999 knut st. osmundsen (knut.stange.osmundsen@pmsc.no) | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 | /******************************************************************************* | 
|---|
| 14 | *   Defined Constants And Macros                                               * | 
|---|
| 15 | *******************************************************************************/ | 
|---|
| 16 | #define INCL_DOSERRORS      /* DOS Error values */ | 
|---|
| 17 | #define INCL_DOSMODULEMGR   /* DOS Module management */ | 
|---|
| 18 | #define INCL_DOSSEMAPHORES | 
|---|
| 19 |  | 
|---|
| 20 | /******************************************************************************* | 
|---|
| 21 | *   Header Files                                                               * | 
|---|
| 22 | *******************************************************************************/ | 
|---|
| 23 | #include <os2wrap.h>        //Odin32 OS/2 api wrappers | 
|---|
| 24 |  | 
|---|
| 25 | #include <stdlib.h> | 
|---|
| 26 |  | 
|---|
| 27 | #include <win32type.h> | 
|---|
| 28 | #include <misc.h> | 
|---|
| 29 | #include <windllpe2lx.h> | 
|---|
| 30 | #include <winexepe2lx.h> | 
|---|
| 31 | #include <wprocess.h> | 
|---|
| 32 |  | 
|---|
| 33 | #include "cio.h"            // I/O | 
|---|
| 34 | #include "oslibmisc.h"      // OSLibGetDllName | 
|---|
| 35 | #include "conwin.h"         // Windows Header for console only | 
|---|
| 36 | #include "console.h" | 
|---|
| 37 |  | 
|---|
| 38 | #define DBG_LOCALLOG    DBG_windllpe2lx | 
|---|
| 39 | #include "dbglocal.h" | 
|---|
| 40 |  | 
|---|
| 41 | /******************************************************************************* | 
|---|
| 42 | *   Global Variables                                                           * | 
|---|
| 43 | *******************************************************************************/ | 
|---|
| 44 | extern BOOL fPeLoader; | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 | * Register an Pe2Lx Dll module. Called from TIBFix code in Dll Pe2Lx module. | 
|---|
| 48 | * @returns   1 on success. | 
|---|
| 49 | *            0 on failure. | 
|---|
| 50 | * @param     Pe2LxVersion  Pe2Lx version. High bit is Win32k indicator and must be masked off! | 
|---|
| 51 | * @param     hInstance     Module handle (OS/2). | 
|---|
| 52 | * @param     dwAttachType  0 = attach dll | 
|---|
| 53 | *                          1 = detach dll | 
|---|
| 54 | * @sketch    Try find module. | 
|---|
| 55 | *            IF attach process THEN | 
|---|
| 56 | *            BEGIN | 
|---|
| 57 | *            END | 
|---|
| 58 | *                Init I/O. | 
|---|
| 59 | *                Get Lib name and match Pe2Lx version with kernel32 version. | 
|---|
| 60 | *                Write info to the log. | 
|---|
| 61 | *                Try create pe2lx dll object. | 
|---|
| 62 | *                Console devices initialization. | 
|---|
| 63 | *                Add reference and attach dll to process. | 
|---|
| 64 | *            ELSE | 
|---|
| 65 | *            BEGIN | 
|---|
| 66 | *                IF module found AND not freelibrary THEN | 
|---|
| 67 | *                    fail (return 0) due to OS/2 bug. | 
|---|
| 68 | *            END | 
|---|
| 69 | *            return successfully (return 1) | 
|---|
| 70 | * @status    completely implemented. | 
|---|
| 71 | * @author    Sander van Leeuwen, knut st. osmundsen | 
|---|
| 72 | */ | 
|---|
| 73 | ULONG WIN32API RegisterPe2LxDll(ULONG ulPe2LxVersion, HINSTANCE hinstance, ULONG ulAttachType) | 
|---|
| 74 | { | 
|---|
| 75 | char *pszName; | 
|---|
| 76 |  | 
|---|
| 77 | #if 1 /* temporary fix */ | 
|---|
| 78 | if (ulAttachType != 0UL) | 
|---|
| 79 | return 0; | 
|---|
| 80 | #endif | 
|---|
| 81 |  | 
|---|
| 82 | Win32Pe2LxDll *pWinMod = (Win32Pe2LxDll *)Win32DllBase::findModule(hinstance); | 
|---|
| 83 | if (ulAttachType == 0UL) | 
|---|
| 84 | {   /* Process attach */ | 
|---|
| 85 |  | 
|---|
| 86 | /* Init I/O */ | 
|---|
| 87 | if (getenv("WIN32_IOPL2")) | 
|---|
| 88 | io_init1(); | 
|---|
| 89 |  | 
|---|
| 90 | /* Get Lib name and match Pe2Lx version with kernel32 version. */ | 
|---|
| 91 | pszName = OSLibGetDllName(hinstance); | 
|---|
| 92 | CheckVersion(ulPe2LxVersion & ~0x80000000UL, pszName); | 
|---|
| 93 |  | 
|---|
| 94 | /* Write info to the log. */ | 
|---|
| 95 | dprintf(("RegisterPe2LxExe: ulPe2LxVersion = %#x\n", ulPe2LxVersion)); | 
|---|
| 96 | dprintf(("RegisterPe2LxExe: hinstance = %#x\n", hinstance)); | 
|---|
| 97 | dprintf(("RegisterPe2LxExe: ulAttachType = %#x (reason)\n", ulAttachType)); | 
|---|
| 98 | dprintf(("RegisterPe2LxExe: name = %s\n", OSLibGetDllName(hinstance))); | 
|---|
| 99 |  | 
|---|
| 100 | /* Try create pe2lx dll object. */ | 
|---|
| 101 | pWinMod = new Win32Pe2LxDll(hinstance, (ulPe2LxVersion & 0x80000000UL) == 0x80000000UL); | 
|---|
| 102 | if (pWinMod == NULL) | 
|---|
| 103 | { | 
|---|
| 104 | eprintf(("RegisterPe2LxDll: new returned a NULL-pointer\n")); | 
|---|
| 105 | return 0; | 
|---|
| 106 | } | 
|---|
| 107 | if (!pWinMod->init()) | 
|---|
| 108 | { | 
|---|
| 109 | eprintf(("RegisterPe2LxDll: init-method failed.\n")); | 
|---|
| 110 | delete pWinMod; | 
|---|
| 111 | return 0; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | /* @@@PH 1998/03/17 Console devices initialization */ | 
|---|
| 115 | iConsoleDevicesRegister(); | 
|---|
| 116 |  | 
|---|
| 117 | /* | 
|---|
| 118 | * Before we attach the DLL we must make sure that we have a valid executable | 
|---|
| 119 | * Should perhaps find a good way of checking for native LX binaries... | 
|---|
| 120 | */ | 
|---|
| 121 | if (!WinExe && !fPeLoader) | 
|---|
| 122 | { | 
|---|
| 123 | dprintf(("RegisterPe2LxDll: tries to do an early exe init.\n")); | 
|---|
| 124 | Win32Pe2LxExe::earlyInit(); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /* Add reference and attach dll to process. */ | 
|---|
| 128 | pWinMod->AddRef(); | 
|---|
| 129 | pWinMod->attachProcess(); | 
|---|
| 130 | } | 
|---|
| 131 | else | 
|---|
| 132 | {   /* process detach */ | 
|---|
| 133 | if (pWinMod != NULL) | 
|---|
| 134 | return 0;   /* don't unload (OS/2 dll unload bug) - see OS2.bugs in root dir. */ | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | return 1;   /* success */ | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 | * Constructor - creates an pe2lx dll object from a module handle to a pe2lx dll module. | 
|---|
| 143 | * @param     hinstance   Module handle. | 
|---|
| 144 | * @param     fWin32k     TRUE:  Win32k module. | 
|---|
| 145 | *                        FALSE: Pe2Lx module. | 
|---|
| 146 | * @status    completely implemented. | 
|---|
| 147 | * @author    Sander van Leeuwen, knut st. osmundsen | 
|---|
| 148 | */ | 
|---|
| 149 | Win32Pe2LxDll::Win32Pe2LxDll(HINSTANCE hinstance, BOOL fWin32k) | 
|---|
| 150 | : Win32ImageBase(hinstance), | 
|---|
| 151 | Win32DllBase(hinstance, NULL), | 
|---|
| 152 | Win32Pe2LxImage(hinstance, fWin32k) | 
|---|
| 153 | { | 
|---|
| 154 | dprintf(("Win32Pe2LxDll::Win32Pe2LxDll %s", szModule)); | 
|---|
| 155 | fDll = TRUE; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 |  | 
|---|
| 159 | /** | 
|---|
| 160 | * Destructor - does nothing. | 
|---|
| 161 | * @status    completely implemented. | 
|---|
| 162 | * @author    Sander van Leeuwen | 
|---|
| 163 | */ | 
|---|
| 164 | Win32Pe2LxDll::~Win32Pe2LxDll() | 
|---|
| 165 | { | 
|---|
| 166 | dprintf(("Win32Pe2LxDll::~Win32Pe2LxDll %s", szModule)); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 |  | 
|---|
| 170 | /** | 
|---|
| 171 | * Init object. | 
|---|
| 172 | * Must be called immedeately after objecte construction. | 
|---|
| 173 | * @returns   Success indicator. (TRUE == success) | 
|---|
| 174 | * @sketch    call init method of the parten class. | 
|---|
| 175 | *            set dllEntryPoint | 
|---|
| 176 | * @status    completely implemented. | 
|---|
| 177 | * @author    knut st. osmundsen | 
|---|
| 178 | */ | 
|---|
| 179 | BOOL Win32Pe2LxDll::init() | 
|---|
| 180 | { | 
|---|
| 181 | if (Win32Pe2LxImage::init()) | 
|---|
| 182 | { | 
|---|
| 183 | /* set entry point. */ | 
|---|
| 184 | dllEntryPoint = (WIN32DLLENTRY)entryPoint; | 
|---|
| 185 | } | 
|---|
| 186 | else | 
|---|
| 187 | return FALSE; | 
|---|
| 188 | return TRUE; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 |  | 
|---|
| 192 | /** | 
|---|
| 193 | * Simple question: Pe2Lx DLL? Yes! | 
|---|
| 194 | * @returns   TRUE. | 
|---|
| 195 | * @status    completely implemented. | 
|---|
| 196 | * @author    knut st. osmundsen | 
|---|
| 197 | */ | 
|---|
| 198 | BOOL Win32Pe2LxDll::isPe2LxDll() const | 
|---|
| 199 | { | 
|---|
| 200 | return TRUE; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | /** | 
|---|
| 205 | * Simple question: -Native LX dll? | 
|---|
| 206 | *                  -No! | 
|---|
| 207 | * @returns   FALSE. | 
|---|
| 208 | * @status    completely implemented. | 
|---|
| 209 | * @author    Sander van Leeuwen | 
|---|
| 210 | */ | 
|---|
| 211 | BOOL Win32Pe2LxDll::isLxDll() const | 
|---|
| 212 | { | 
|---|
| 213 | return FALSE; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|