| 1 | /* $Id: windlllx.cpp,v 1.14 2000-08-12 16:58:39 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 LX Dll class (compiled in OS/2 using Odin32 api)
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1999-2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | *
|
|---|
| 8 | * TODO: Unloading of dlls probably needs to be fixed due to OS/2 bug
|
|---|
| 9 | * (wrong unload order of dlls)
|
|---|
| 10 | *
|
|---|
| 11 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 12 | *
|
|---|
| 13 | */
|
|---|
| 14 | #define INCL_DOSFILEMGR /* File Manager values */
|
|---|
| 15 | #define INCL_DOSERRORS /* DOS Error values */
|
|---|
| 16 | #define INCL_DOSPROCESS /* DOS Process values */
|
|---|
| 17 | #define INCL_DOSMODULEMGR
|
|---|
| 18 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
|---|
| 19 | #define INCL_WIN
|
|---|
| 20 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 21 | #include <os2newapi.h>
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <string.h>
|
|---|
| 24 | #include <stdlib.h>
|
|---|
| 25 | #include <iostream.h>
|
|---|
| 26 | #include <fstream.h>
|
|---|
| 27 | #include <misc.h>
|
|---|
| 28 | #include <win32type.h>
|
|---|
| 29 | #include <windllbase.h>
|
|---|
| 30 | #include <windlllx.h>
|
|---|
| 31 | #include "winexepe2lx.h"
|
|---|
| 32 | #include "winexepeldr.h"
|
|---|
| 33 | #include <odinlx.h>
|
|---|
| 34 | #include "oslibmisc.h"
|
|---|
| 35 |
|
|---|
| 36 | #include <exe386.h>
|
|---|
| 37 |
|
|---|
| 38 | #define DBG_LOCALLOG DBG_windlllx
|
|---|
| 39 | #include "dbglocal.h"
|
|---|
| 40 |
|
|---|
| 41 | //******************************************************************************
|
|---|
| 42 | //Create LX Dll object and send process attach message
|
|---|
| 43 | //System dlls set EntryPoint to 0
|
|---|
| 44 | //Parameters:
|
|---|
| 45 | // HINSTANCE hInstance - OS/2 module handle
|
|---|
| 46 | // WIN32DLLENTRY EntryPoint - Win32 dll entrypoint address
|
|---|
| 47 | // PVOID pResData - pointer to win32 resource data
|
|---|
| 48 | // DWORD MajorImageVersion - major image/os version (for fake win32 header)
|
|---|
| 49 | // DWORD MinorImageVersion - minor image/os version (for fake win32 header)
|
|---|
| 50 | // DWORD Subsystem - subsystem type (for fake win32 header)
|
|---|
| 51 | // (IMAGE_SUBSYSTEM_WINDOWS_CUI/IMAGE_SUBSYSTEM_WINDOWS_GUI)
|
|---|
| 52 | //
|
|---|
| 53 | //Returns: Odin32 module handle
|
|---|
| 54 | //******************************************************************************
|
|---|
| 55 | DWORD WIN32API RegisterLxDll(HINSTANCE hInstance, WIN32DLLENTRY EntryPoint,
|
|---|
| 56 | PVOID pResData,
|
|---|
| 57 | DWORD MajorImageVersion,
|
|---|
| 58 | DWORD MinorImageVersion,
|
|---|
| 59 | DWORD Subsystem)
|
|---|
| 60 | {
|
|---|
| 61 | APIRET rc;
|
|---|
| 62 | Win32LxDll *windll;
|
|---|
| 63 | Win32DllBase *windlldep;
|
|---|
| 64 | char szFileName[CCHMAXPATH], szErrName[CCHMAXPATH];
|
|---|
| 65 |
|
|---|
| 66 | if(OSLibGetDllName(hInstance, szFileName, sizeof(szFileName)) == FALSE) {
|
|---|
| 67 | dprintf(("ERROR: RegisterLxDll: OSLibGetDllName %x failed!!", hInstance));
|
|---|
| 68 | return 0;
|
|---|
| 69 | }
|
|---|
| 70 | //Make sure DosLoadModule is called at least
|
|---|
| 71 | //once for a dll (to make sure OS/2 doesn't unload the dll when it's
|
|---|
| 72 | //still needed)
|
|---|
| 73 | rc = DosLoadModule(szErrName, sizeof(szErrName), szFileName, &hInstance);
|
|---|
| 74 | if(rc != 0) {
|
|---|
| 75 | dprintf(("ERROR: RegisterLxDll: DosLoadModule %s failed (rc=%d)!!", szFileName, rc));
|
|---|
| 76 | return 0;
|
|---|
| 77 | }
|
|---|
| 78 | windll = new Win32LxDll(hInstance, EntryPoint, pResData, MajorImageVersion,
|
|---|
| 79 | MinorImageVersion, Subsystem);
|
|---|
| 80 | if(windll == NULL) {
|
|---|
| 81 | dprintf(("RegisterLxDll: windll == NULL!!!"));
|
|---|
| 82 | return 0;
|
|---|
| 83 | }
|
|---|
| 84 | if(!fPeLoader) {
|
|---|
| 85 | windll->AddRef();
|
|---|
| 86 |
|
|---|
| 87 | if(windll->attachProcess() == 0)
|
|---|
| 88 | return 0;
|
|---|
| 89 |
|
|---|
| 90 | return windll->getInstanceHandle();
|
|---|
| 91 | }
|
|---|
| 92 | IMAGE_DOS_HEADER doshdr;
|
|---|
| 93 | struct e32_exe lxhdr;
|
|---|
| 94 | ULONG offset;
|
|---|
| 95 | char modulename[CCHMAXPATH];
|
|---|
| 96 | char modsize;
|
|---|
| 97 | int i;
|
|---|
| 98 |
|
|---|
| 99 | //SvL: This code reads the import name table of the dll to get the dependencies
|
|---|
| 100 | // on other dlls.
|
|---|
| 101 | //DosQueryHeaderInfo is an undocumented api, but works very well.
|
|---|
| 102 | //(no need to save FS here as we'll return to OS/2 immediately)
|
|---|
| 103 | rc = DosQueryHeaderInfo(hInstance, 0, &doshdr, sizeof(IMAGE_DOS_HEADER), QHINF_READFILE);
|
|---|
| 104 | if(rc) {
|
|---|
| 105 | goto hdrerror;
|
|---|
| 106 | }
|
|---|
| 107 | rc = DosQueryHeaderInfo(hInstance, doshdr.e_lfanew, &lxhdr, sizeof(e32_exe), QHINF_READFILE);
|
|---|
| 108 | if(rc) {
|
|---|
| 109 | goto hdrerror;
|
|---|
| 110 | }
|
|---|
| 111 | offset = doshdr.e_lfanew + lxhdr.e32_impmod;
|
|---|
| 112 | for(i=0;i<lxhdr.e32_impmodcnt;i++) {
|
|---|
| 113 | rc = DosQueryHeaderInfo(hInstance, offset, &modsize, 1, QHINF_READFILE);
|
|---|
| 114 | if(rc) {
|
|---|
| 115 | goto hdrerror;
|
|---|
| 116 | }
|
|---|
| 117 | rc = DosQueryHeaderInfo(hInstance, offset+1, &modulename, min(modsize, sizeof(modulename)), QHINF_READFILE);
|
|---|
| 118 | if(rc) {
|
|---|
| 119 | goto hdrerror;
|
|---|
| 120 | }
|
|---|
| 121 | modulename[modsize] = 0;
|
|---|
| 122 | windlldep = Win32DllBase::findModule(modulename, TRUE);
|
|---|
| 123 | if(windlldep) {
|
|---|
| 124 | dprintf(("RegisterLxDll: Add dependency %s -> %s", windll->getModuleName(), modulename));
|
|---|
| 125 | windll->addDependency(windlldep);
|
|---|
| 126 | }
|
|---|
| 127 | offset += modsize + 1;
|
|---|
| 128 | }
|
|---|
| 129 | return windll->getInstanceHandle();
|
|---|
| 130 |
|
|---|
| 131 | hdrerror:
|
|---|
| 132 | dprintf(("DosQueryHeaderInfo returned %d", rc));
|
|---|
| 133 | return windll->getInstanceHandle();
|
|---|
| 134 | }
|
|---|
| 135 | //******************************************************************************
|
|---|
| 136 | //Destroy LX Dll object
|
|---|
| 137 | //******************************************************************************
|
|---|
| 138 | BOOL WIN32API UnregisterLxDll(HINSTANCE hInstance)
|
|---|
| 139 | {
|
|---|
| 140 | Win32DllBase *windll;
|
|---|
| 141 |
|
|---|
| 142 | //Don't proceed for pe2lx/win32k (os/2 dll unload dependency bug)
|
|---|
| 143 | //Don't do it either after ExitProcess has been called
|
|---|
| 144 | if(!fPeLoader || WinExe == NULL)
|
|---|
| 145 | return TRUE;
|
|---|
| 146 |
|
|---|
| 147 | windll = Win32DllBase::findModule(hInstance);
|
|---|
| 148 | if(!windll) {
|
|---|
| 149 | dprintf(("UnregisterLxDll: Can't find dll with handle %x (already deleted)", hInstance));
|
|---|
| 150 | return TRUE; //already deleted by Win32LxDll::Release
|
|---|
| 151 | }
|
|---|
| 152 | dprintf(("UnregisterLxDll %s", windll->getModuleName()));
|
|---|
| 153 | //This can only happen for LX dependencies (i.e. wininet loads wsock32)
|
|---|
| 154 | delete windll;
|
|---|
| 155 | return TRUE;
|
|---|
| 156 | }
|
|---|
| 157 | //******************************************************************************
|
|---|
| 158 | //******************************************************************************
|
|---|
| 159 | Win32LxDll::Win32LxDll(HINSTANCE hInstance, WIN32DLLENTRY EntryPoint, PVOID pResData,
|
|---|
| 160 | DWORD MajorImageVersion, DWORD MinorImageVersion,
|
|---|
| 161 | DWORD Subsystem)
|
|---|
| 162 | : Win32ImageBase(hInstance),
|
|---|
| 163 | Win32LxImage(hInstance, pResData),
|
|---|
| 164 | Win32DllBase(hInstance, EntryPoint)
|
|---|
| 165 | {
|
|---|
| 166 | if(EntryPoint == NULL) {
|
|---|
| 167 | fSkipThreadEntryCalls = TRUE;
|
|---|
| 168 | fAttachedToProcess = TRUE;
|
|---|
| 169 | }
|
|---|
| 170 | hinstanceOS2 = hInstance;
|
|---|
| 171 | //new win32 instance handle must be pointer to PE header
|
|---|
| 172 | hinstance = (HINSTANCE)buildHeader(MajorImageVersion, MinorImageVersion,
|
|---|
| 173 | Subsystem);
|
|---|
| 174 | }
|
|---|
| 175 | //******************************************************************************
|
|---|
| 176 | //******************************************************************************
|
|---|
| 177 | Win32LxDll::~Win32LxDll()
|
|---|
| 178 | {
|
|---|
| 179 | }
|
|---|
| 180 | //******************************************************************************
|
|---|
| 181 | //******************************************************************************
|
|---|
| 182 | #ifdef DEBUG
|
|---|
| 183 | ULONG Win32LxDll::AddRef(char *parentname)
|
|---|
| 184 | #else
|
|---|
| 185 | ULONG Win32LxDll::AddRef()
|
|---|
| 186 | #endif
|
|---|
| 187 | {
|
|---|
| 188 | Win32DllBase *dll;
|
|---|
| 189 | QueueItem *item;
|
|---|
| 190 | ULONG ret;
|
|---|
| 191 |
|
|---|
| 192 | #ifdef DEBUG
|
|---|
| 193 | ret = Win32DllBase::AddRef(parentname);
|
|---|
| 194 | #else
|
|---|
| 195 | ret = Win32DllBase::AddRef();
|
|---|
| 196 | #endif
|
|---|
| 197 |
|
|---|
| 198 | if(!fPeLoader)
|
|---|
| 199 | return ret;
|
|---|
| 200 |
|
|---|
| 201 | if(referenced == 1)
|
|---|
| 202 | {
|
|---|
| 203 | item = loadedDlls.Head();
|
|---|
| 204 | while(item) {
|
|---|
| 205 | dll = (Win32DllBase *)loadedDlls.getItem(item);
|
|---|
| 206 | if(dll == NULL) {
|
|---|
| 207 | dprintf(("ERROR: Win32DllBase::AddRef: dll item == NULL!!"));
|
|---|
| 208 | DebugInt3();
|
|---|
| 209 | return -1;
|
|---|
| 210 | }
|
|---|
| 211 | #ifdef DEBUG
|
|---|
| 212 | dll->AddRef(getModuleName());
|
|---|
| 213 | #else
|
|---|
| 214 | dll->AddRef();
|
|---|
| 215 | #endif
|
|---|
| 216 | item = loadedDlls.getNext(item);
|
|---|
| 217 | }
|
|---|
| 218 | if(attachProcess() == 0)
|
|---|
| 219 | return 0;
|
|---|
| 220 | }
|
|---|
| 221 | return ret;
|
|---|
| 222 | }
|
|---|
| 223 | //******************************************************************************
|
|---|
| 224 | //******************************************************************************
|
|---|
| 225 | ULONG Win32LxDll::Release()
|
|---|
| 226 | {
|
|---|
| 227 | HINSTANCE hinst;
|
|---|
| 228 | ULONG ret;
|
|---|
| 229 | APIRET rc;
|
|---|
| 230 | BOOL fNoUnload = fDisableUnload; //only set for kernel32.dll
|
|---|
| 231 |
|
|---|
| 232 | hinst = hinstanceOS2;
|
|---|
| 233 | ret = Win32DllBase::Release();
|
|---|
| 234 | if(ret == 0 && !fNoUnload) {//only set for kernel32.dll (fDisableUnload)
|
|---|
| 235 | //DosFreeModule sends a termination message to the dll.
|
|---|
| 236 | //The LX dll informs us when it's removed (UnregisterDll call)
|
|---|
| 237 | rc = DosFreeModule(hinst);
|
|---|
| 238 | if(rc) {
|
|---|
| 239 | dprintf(("Win32LxDll::Release: DosFreeModule %x returned %d", hinst, rc));
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 | return(ret);
|
|---|
| 243 | }
|
|---|
| 244 | //******************************************************************************
|
|---|
| 245 | //******************************************************************************
|
|---|
| 246 | BOOL Win32LxDll::isLxDll()
|
|---|
| 247 | {
|
|---|
| 248 | return TRUE;
|
|---|
| 249 | }
|
|---|
| 250 | //******************************************************************************
|
|---|
| 251 | //******************************************************************************
|
|---|
| 252 | void Win32LxDll::setDllHandleOS2(HINSTANCE hInstanceOS2)
|
|---|
| 253 | {
|
|---|
| 254 | //Loaded with LoadLibrary(Ex); no need for a 2nd DosLoadModule
|
|---|
| 255 | //Dlls that are indirectly loaded (i.e. GDI32->KERNEL32 dependancy) need
|
|---|
| 256 | //this additional DosLoadModule (and setDllHandleOS2 isn't called for those)
|
|---|
| 257 | if(this->hinstanceOS2) {
|
|---|
| 258 | DosFreeModule(this->hinstanceOS2);
|
|---|
| 259 | }
|
|---|
| 260 | this->hinstanceOS2 = hInstanceOS2;
|
|---|
| 261 | }
|
|---|
| 262 | //******************************************************************************
|
|---|
| 263 | //******************************************************************************
|
|---|
| 264 | Win32LxDll *Win32LxDll::findModuleByOS2Handle(HINSTANCE hinstance)
|
|---|
| 265 | {
|
|---|
| 266 | dlllistmutex.enter();
|
|---|
| 267 |
|
|---|
| 268 | Win32DllBase *mod = Win32DllBase::getFirst();
|
|---|
| 269 | while(mod != NULL) {
|
|---|
| 270 | if(mod->isLxDll()) {
|
|---|
| 271 | Win32LxDll *lxdll = (Win32LxDll *)mod;
|
|---|
| 272 | if(lxdll->hinstanceOS2 == hinstance) {
|
|---|
| 273 | dlllistmutex.leave();
|
|---|
| 274 | return(lxdll);
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|
| 277 | mod = mod->getNext();
|
|---|
| 278 | }
|
|---|
| 279 | dlllistmutex.leave();
|
|---|
| 280 | return(NULL);
|
|---|
| 281 | }
|
|---|
| 282 | //******************************************************************************
|
|---|
| 283 | //******************************************************************************
|
|---|