| 1 | /* $Id: windllbase.h,v 1.11 2004-01-15 10:39:06 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 Dll base class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 10 | *
|
|---|
| 11 | */
|
|---|
| 12 | #ifndef __WINDLLBASE_H__
|
|---|
| 13 | #define __WINDLLBASE_H__
|
|---|
| 14 |
|
|---|
| 15 | #include <winimagebase.h>
|
|---|
| 16 | #include <odinlx.h>
|
|---|
| 17 | #include <vmutex.h>
|
|---|
| 18 |
|
|---|
| 19 | #ifndef HINSTANCE
|
|---|
| 20 | #define HINSTANCE ULONG
|
|---|
| 21 | #endif
|
|---|
| 22 |
|
|---|
| 23 | #define DLL_PROCESS_ATTACH 1
|
|---|
| 24 | #define DLL_THREAD_ATTACH 2
|
|---|
| 25 | #define DLL_THREAD_DETACH 3
|
|---|
| 26 | #define DLL_PROCESS_DETACH 0
|
|---|
| 27 |
|
|---|
| 28 | #define DONT_RESOLVE_DLL_REFERENCES 0x00000001
|
|---|
| 29 | #define LOAD_LIBRARY_AS_DATAFILE 0x00000002
|
|---|
| 30 | #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
|
|---|
| 31 |
|
|---|
| 32 | #define DLL_EXTENSION ".DLL"
|
|---|
| 33 |
|
|---|
| 34 | //odin.ini section names to lookup renamed dlls
|
|---|
| 35 | //i.e. OLE32 -> OLE32OS2
|
|---|
| 36 | #define DLLRENAMEWIN_SECTION "DLLRENAMEWIN"
|
|---|
| 37 | //i.e. OLE32OS2 -> OLE32
|
|---|
| 38 | #define DLLRENAMEOS2_SECTION "DLLRENAMEOS2"
|
|---|
| 39 |
|
|---|
| 40 | class Win32DllBase : public virtual Win32ImageBase
|
|---|
| 41 | {
|
|---|
| 42 | public:
|
|---|
| 43 | Win32DllBase(HINSTANCE hInstance, WIN32DLLENTRY DllEntryPoint, Win32ImageBase *parent = NULL);
|
|---|
| 44 | virtual ~Win32DllBase();
|
|---|
| 45 |
|
|---|
| 46 | #ifdef DEBUG
|
|---|
| 47 | virtual ULONG AddRef(char *parentname = NULL);
|
|---|
| 48 | #else
|
|---|
| 49 | virtual ULONG AddRef();
|
|---|
| 50 | #endif
|
|---|
| 51 | virtual ULONG Release();
|
|---|
| 52 |
|
|---|
| 53 | char *getName() { return szModule; };
|
|---|
| 54 |
|
|---|
| 55 | //do not call the ATTACH_THREAD, DETACH_THREAD functions
|
|---|
| 56 | void disableThreadLibraryCalls() { fSkipThreadEntryCalls = TRUE; };
|
|---|
| 57 | void disableLibraryCalls() { fSkipEntryCalls = fSkipThreadEntryCalls = TRUE; };
|
|---|
| 58 |
|
|---|
| 59 | Win32DllBase *getNext() { return next; };
|
|---|
| 60 | static Win32DllBase *getFirst();
|
|---|
| 61 |
|
|---|
| 62 | //Send DLL_THREAD_ATTACH message to all dlls for a new thread
|
|---|
| 63 | static void attachThreadToAllDlls();
|
|---|
| 64 |
|
|---|
| 65 | //Send DLL_THREAD_DETACH message to all dlls for thread that's about to die
|
|---|
| 66 | static void detachThreadFromAllDlls();
|
|---|
| 67 |
|
|---|
| 68 | //Send DLL_PROCESS_DETACH message to all dlls for process that's about to end
|
|---|
| 69 | static void detachProcessFromAllDlls();
|
|---|
| 70 |
|
|---|
| 71 | //Setup TLS structure for all dlls for a new thread
|
|---|
| 72 | static void tlsAttachThreadToAllDlls();
|
|---|
| 73 |
|
|---|
| 74 | //Destroy TLS structure for all dlls for a thread that's about to die
|
|---|
| 75 | static void tlsDetachThreadFromAllDlls();
|
|---|
| 76 |
|
|---|
| 77 | BOOL attachProcess();
|
|---|
| 78 | BOOL detachProcess();
|
|---|
| 79 | BOOL attachThread();
|
|---|
| 80 | BOOL detachThread();
|
|---|
| 81 |
|
|---|
| 82 | //This counter is incremented when the dll has been loaded with LoadLibrary(Ex)
|
|---|
| 83 | //(== not loaded on behalf of another dll or the main exe)
|
|---|
| 84 | void incDynamicLib();
|
|---|
| 85 | void decDynamicLib();
|
|---|
| 86 | BOOL isDynamicLib() { return nrDynamicLibRef != 0; };
|
|---|
| 87 |
|
|---|
| 88 | WIN32DLLENTRY getEntryPoint() { return dllEntryPoint; };
|
|---|
| 89 |
|
|---|
| 90 | void setUnloadOrder(Win32ImageBase *parent);
|
|---|
| 91 |
|
|---|
| 92 | void updateDependencies();
|
|---|
| 93 |
|
|---|
| 94 | BOOL RemoveCircularDependency(Win32DllBase *parent);
|
|---|
| 95 |
|
|---|
| 96 | //Only called for kernel32
|
|---|
| 97 | void DisableUnload() { fDisableUnload = TRUE; };
|
|---|
| 98 |
|
|---|
| 99 | BOOL IsUnloaded() { return fUnloaded; };
|
|---|
| 100 |
|
|---|
| 101 | static void deleteDynamicLibs();
|
|---|
| 102 | static void deleteAll();
|
|---|
| 103 |
|
|---|
| 104 | static BOOL isSystemDll(char *szFileName);
|
|---|
| 105 |
|
|---|
| 106 | virtual BOOL isPe2LxDll() const = 0;
|
|---|
| 107 | virtual BOOL isLxDll() const = 0;
|
|---|
| 108 | virtual BOOL isDll();
|
|---|
| 109 |
|
|---|
| 110 | static void renameDll(char *dllname, BOOL fWinToOS2=TRUE);
|
|---|
| 111 | static void setDefaultRenaming();
|
|---|
| 112 |
|
|---|
| 113 | static Win32DllBase *findModule(char *dllname, BOOL fRenameFirst = FALSE);
|
|---|
| 114 | static Win32DllBase *findModule(HINSTANCE hinstance);
|
|---|
| 115 | static Win32DllBase *findModule(WIN32DLLENTRY DllEntryPoint);
|
|---|
| 116 | static Win32DllBase *findModuleByAddr(ULONG address);
|
|---|
| 117 | static Win32DllBase *findModuleByOS2Handle(HINSTANCE hinstance);
|
|---|
| 118 |
|
|---|
| 119 | static int enumDlls(HMODULE *lphModule, int countMax);
|
|---|
| 120 |
|
|---|
| 121 | #ifdef DEBUG
|
|---|
| 122 | void printListOfDlls();
|
|---|
| 123 | #endif
|
|---|
| 124 |
|
|---|
| 125 | protected:
|
|---|
| 126 | #ifdef DEBUG
|
|---|
| 127 | void printDependencies(char *parent);
|
|---|
| 128 | #endif
|
|---|
| 129 |
|
|---|
| 130 | BOOL fSkipThreadEntryCalls, fUnloaded, fAttachedToProcess, fSkipEntryCalls;
|
|---|
| 131 |
|
|---|
| 132 | WIN32DLLENTRY dllEntryPoint;
|
|---|
| 133 |
|
|---|
| 134 | LONG referenced;
|
|---|
| 135 |
|
|---|
| 136 | //This counter is incremented when the dll has been loaded with LoadLibrary(Ex)
|
|---|
| 137 | //(== not loaded on behalf of another dll or the main exe)
|
|---|
| 138 | BOOL nrDynamicLibRef;
|
|---|
| 139 |
|
|---|
| 140 | BOOL fInserted; //inserted in dll list
|
|---|
| 141 |
|
|---|
| 142 | //This flag is set when a dll has been loaded with DosLoadModule
|
|---|
| 143 | BOOL fDisableUnload;
|
|---|
| 144 |
|
|---|
| 145 | static Win32DllBase *head;
|
|---|
| 146 | Win32DllBase *next;
|
|---|
| 147 | private:
|
|---|
| 148 | static Queue loadLibDlls;
|
|---|
| 149 | };
|
|---|
| 150 |
|
|---|
| 151 | extern VMutex dlllistmutex; //protects linked lists of heaps
|
|---|
| 152 |
|
|---|
| 153 | #endif
|
|---|